001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.reef.webserver; 020 021import org.apache.avro.io.DatumWriter; 022import org.apache.avro.io.EncoderFactory; 023import org.apache.avro.io.JsonEncoder; 024import org.apache.avro.specific.SpecificDatumWriter; 025import org.apache.reef.driver.evaluator.EvaluatorDescriptor; 026 027import javax.inject.Inject; 028import java.io.ByteArrayOutputStream; 029import java.io.IOException; 030import java.net.InetSocketAddress; 031import java.util.ArrayList; 032import java.util.List; 033import java.util.Map; 034 035/** 036 * Serialize Evaluator Info. 037 * It is the default implementation for interface EvaluatorInfoSerializer. 038 */ 039public class AvroEvaluatorInfoSerializer implements EvaluatorInfoSerializer { 040 041 @Inject 042 AvroEvaluatorInfoSerializer() { 043 } 044 045 /** 046 * Create AvroEvaluatorsInfo object. 047 */ 048 @Override 049 public AvroEvaluatorsInfo toAvro( 050 final List<String> ids, final Map<String, EvaluatorDescriptor> evaluators) { 051 052 final List<AvroEvaluatorInfo> evaluatorsInfo = new ArrayList<>(); 053 054 for (final String id : ids) { 055 056 final EvaluatorDescriptor evaluatorDescriptor = evaluators.get(id); 057 String nodeId = null; 058 String nodeName = null; 059 InetSocketAddress address = null; 060 int memory = 0; 061 String type = null; 062 String runtimeName = null; 063 064 if (evaluatorDescriptor != null) { 065 nodeId = evaluatorDescriptor.getNodeDescriptor().getId(); 066 nodeName = evaluatorDescriptor.getNodeDescriptor().getName(); 067 address = evaluatorDescriptor.getNodeDescriptor().getInetSocketAddress(); 068 memory = evaluatorDescriptor.getMemory(); 069 type = evaluatorDescriptor.getProcess().getType().toString(); 070 runtimeName = evaluatorDescriptor.getRuntimeName(); 071 } 072 073 evaluatorsInfo.add(AvroEvaluatorInfo.newBuilder() 074 .setEvaluatorId(id) 075 .setNodeId(nodeId != null ? nodeId : "") 076 .setNodeName(nodeName != null ? nodeName : "") 077 .setInternetAddress(address != null ? address.toString() : "") 078 .setMemory(memory) 079 .setType(type != null ? type : "") 080 .setRuntimeName(runtimeName != null ? runtimeName : "") 081 .build()); 082 } 083 084 return AvroEvaluatorsInfo.newBuilder() 085 .setEvaluatorsInfo(evaluatorsInfo) 086 .build(); 087 } 088 089 /** 090 * Convert AvroEvaluatorsInfo object to JSON string. 091 */ 092 @Override 093 public String toString(final AvroEvaluatorsInfo avroEvaluatorsInfo) { 094 final DatumWriter<AvroEvaluatorsInfo> evaluatorWriter = new SpecificDatumWriter<>(AvroEvaluatorsInfo.class); 095 try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) { 096 final JsonEncoder encoder = EncoderFactory.get().jsonEncoder(avroEvaluatorsInfo.getSchema(), out); 097 evaluatorWriter.write(avroEvaluatorsInfo, encoder); 098 encoder.flush(); 099 return out.toString(AvroHttpSerializer.JSON_CHARSET); 100 } catch (final IOException e) { 101 throw new RuntimeException(e); 102 } 103 } 104}