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 063 if (evaluatorDescriptor != null) { 064 nodeId = evaluatorDescriptor.getNodeDescriptor().getId(); 065 nodeName = evaluatorDescriptor.getNodeDescriptor().getName(); 066 address = evaluatorDescriptor.getNodeDescriptor().getInetSocketAddress(); 067 memory = evaluatorDescriptor.getMemory(); 068 type = evaluatorDescriptor.getType().toString(); 069 } 070 071 evaluatorsInfo.add(AvroEvaluatorInfo.newBuilder() 072 .setEvaluatorId(id) 073 .setNodeId(nodeId != null ? nodeId : "") 074 .setNodeName(nodeName != null ? nodeName : "") 075 .setInternetAddress(address != null ? address.toString() : "") 076 .setMemory(memory) 077 .setType(type != null ? type : "") 078 .build()); 079 } 080 081 return AvroEvaluatorsInfo.newBuilder() 082 .setEvaluatorsInfo(evaluatorsInfo) 083 .build(); 084 } 085 086 /** 087 * Convert AvroEvaluatorsInfo object to JSON string 088 */ 089 @Override 090 public String toString(final AvroEvaluatorsInfo avroEvaluatorsInfo) { 091 final DatumWriter<AvroEvaluatorsInfo> evaluatorWriter = new SpecificDatumWriter<>(AvroEvaluatorsInfo.class); 092 try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) { 093 final JsonEncoder encoder = EncoderFactory.get().jsonEncoder(avroEvaluatorsInfo.getSchema(), out); 094 evaluatorWriter.write(avroEvaluatorsInfo, encoder); 095 encoder.flush(); 096 return out.toString(AvroHttpSerializer.JSON_CHARSET); 097 } catch (final IOException e) { 098 throw new RuntimeException(e); 099 } 100 } 101}