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.io.data.loading.impl; 020 021import org.apache.commons.codec.binary.Base64; 022import org.apache.reef.driver.evaluator.EvaluatorRequest; 023 024import java.io.*; 025 026/** 027 * Serialize and deserialize EvaluatorRequest objects 028 * Currently only supports number & memory 029 * Does not take care of Resource Descriptor 030 */ 031public class EvaluatorRequestSerializer { 032 public static String serialize(EvaluatorRequest request) { 033 try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { 034 try (DataOutputStream daos = new DataOutputStream(baos)) { 035 036 daos.writeInt(request.getNumber()); 037 daos.writeInt(request.getMegaBytes()); 038 daos.writeInt(request.getNumberOfCores()); 039 040 } catch (IOException e) { 041 throw e; 042 } 043 044 return Base64.encodeBase64String(baos.toByteArray()); 045 } catch (IOException e1) { 046 throw new RuntimeException("Unable to serialize compute request", e1); 047 } 048 } 049 050 public static EvaluatorRequest deserialize(String serializedRequest) { 051 try (ByteArrayInputStream bais = new ByteArrayInputStream(Base64.decodeBase64(serializedRequest))) { 052 try (DataInputStream dais = new DataInputStream(bais)) { 053 return EvaluatorRequest.newBuilder() 054 .setNumber(dais.readInt()) 055 .setMemory(dais.readInt()) 056 .setNumberOfCores(dais.readInt()) 057 .build(); 058 } 059 } catch (IOException e) { 060 throw new RuntimeException("Unable to de-serialize compute request", e); 061 } 062 } 063}