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.runtime.mesos.util; 020 021import org.apache.avro.io.*; 022import org.apache.avro.specific.SpecificDatumReader; 023import org.apache.avro.specific.SpecificDatumWriter; 024import org.apache.reef.wake.remote.Codec; 025import org.apache.reef.wake.remote.exception.RemoteRuntimeException; 026 027import javax.inject.Inject; 028import java.io.*; 029import java.util.logging.Level; 030import java.util.logging.Logger; 031 032/** 033 * EvaluatorControl codec. 034 */ 035public class MesosRemoteManagerCodec implements Codec<EvaluatorControl> { 036 private static final Logger LOG = Logger.getLogger(MesosRemoteManagerCodec.class.getName()); 037 038 @Inject 039 public MesosRemoteManagerCodec() { 040 } 041 042 @Override 043 public byte[] encode(final EvaluatorControl evaluatorControl) { 044 try { 045 LOG.log(Level.FINEST, "Before Encoding: {0}", evaluatorControl.toString()); 046 final ByteArrayOutputStream out = new ByteArrayOutputStream(); 047 final BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(out, null); 048 final DatumWriter<EvaluatorControl> writer = new SpecificDatumWriter<>(EvaluatorControl.getClassSchema()); 049 writer.write(evaluatorControl, encoder); 050 encoder.flush(); 051 out.close(); 052 LOG.log(Level.FINEST, "After Encoding"); 053 return out.toByteArray(); 054 } catch (final IOException ex) { 055 throw new RemoteRuntimeException(ex); 056 } 057 } 058 059 @Override 060 public EvaluatorControl decode(final byte[] buf) { 061 try { 062 LOG.log(Level.FINEST, "Before Decoding: {0}", buf); 063 final SpecificDatumReader<EvaluatorControl> reader = new SpecificDatumReader<>(EvaluatorControl.getClassSchema()); 064 final Decoder decoder = DecoderFactory.get().binaryDecoder(buf, null); 065 LOG.log(Level.FINEST, "After Decoding"); 066 return reader.read(null, decoder); 067 } catch (final IOException ex) { 068 throw new RemoteRuntimeException(ex); 069 } 070 } 071}