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.javabridge; 020 021import org.apache.reef.annotations.audience.Interop; 022import org.apache.reef.annotations.audience.Private; 023import org.apache.reef.io.naming.Identifiable; 024import org.apache.reef.runtime.common.driver.evaluator.AllocatedEvaluatorImpl; 025import org.apache.reef.driver.evaluator.AllocatedEvaluator; 026 027import java.util.logging.Level; 028import java.util.logging.Logger; 029 030/** 031 * The AllocatedEvaluatorBridge object to bridge operations between REEF .NET and Java allocated evaluator operations. 032 */ 033@Private 034@Interop( 035 CppFiles = { "Clr2JavaImpl.h", "AllocatedEvaluatorClr2Java.cpp" }, 036 CsFiles = { "IAllocatedEvaluatorClr2Java.cs", "AllocatedEvaluator.cs" }) 037public final class AllocatedEvaluatorBridge extends NativeBridge implements Identifiable { 038 039 private static final Logger LOG = Logger.getLogger(AllocatedEvaluatorBridge.class.getName()); 040 041 private final AllocatedEvaluator jallocatedEvaluator; 042 private final String evaluatorId; 043 private final String nameServerInfo; 044 045 /** 046 * This constructor should only be called by the AllocatedEvaluatorBridgeFactory. 047 */ 048 AllocatedEvaluatorBridge(final AllocatedEvaluator allocatedEvaluator, 049 final String serverInfo) { 050 this.jallocatedEvaluator = allocatedEvaluator; 051 this.evaluatorId = allocatedEvaluator.getId(); 052 this.nameServerInfo = serverInfo; 053 } 054 055 /** 056 * Bridge function for REEF .NET to submit context and task configurations for the allocated evaluator. 057 * @param contextConfigurationString the context configuration from .NET. 058 * @param taskConfigurationString the task configuration from .NET. 059 */ 060 public void submitContextAndTaskString(final String contextConfigurationString, 061 final String taskConfigurationString) { 062 if (contextConfigurationString.isEmpty()) { 063 throw new RuntimeException("empty contextConfigurationString provided."); 064 } 065 if (taskConfigurationString.isEmpty()) { 066 throw new RuntimeException("empty taskConfigurationString provided."); 067 } 068 //When submit over the bridge, we would keep the task configurations as a serialized strings. 069 //submitContextAndTask(final String contextConfiguration, 070 //final String taskConfiguration) is not exposed in the interface. Therefore cast is necessary. 071 ((AllocatedEvaluatorImpl)jallocatedEvaluator) 072 .submitContextAndTask(contextConfigurationString, taskConfigurationString); 073 } 074 075 /** 076 * Bridge function for REEF .NET to submit context configuration for the allocated evaluator. 077 * @param contextConfigurationString the context configuration from .NET. 078 */ 079 public void submitContextString(final String contextConfigurationString) { 080 if (contextConfigurationString.isEmpty()) { 081 throw new RuntimeException("empty contextConfigurationString provided."); 082 } 083 084 //When submit over the bridge, we would keep the contextConfigurationString as serialized strings. 085 //public void submitContext(final String contextConfiguration) 086 // is not exposed in the interface. Therefore cast is necessary. 087 ((AllocatedEvaluatorImpl)jallocatedEvaluator).submitContext(contextConfigurationString); 088 } 089 090 /** 091 * Bridge function for REEF .NET to submit context and service configurations for the allocated evaluator. 092 * @param contextConfigurationString the context configuration from .NET. 093 * @param serviceConfigurationString the service configuration from .NET. 094 */ 095 public void submitContextAndServiceString(final String contextConfigurationString, 096 final String serviceConfigurationString) { 097 if (contextConfigurationString.isEmpty()) { 098 throw new RuntimeException("empty contextConfigurationString provided."); 099 } 100 if (serviceConfigurationString.isEmpty()) { 101 throw new RuntimeException("empty serviceConfigurationString provided."); 102 } 103 104 //When submit over the bridge, we would keep the configurations as serialized strings. 105 //public void submitContextAndService(final String contextConfiguration, 106 //final String serviceConfiguration) is not exposed in the interface. Therefore cast is necessary. 107 ((AllocatedEvaluatorImpl)jallocatedEvaluator) 108 .submitContextAndService(contextConfigurationString, serviceConfigurationString); 109 } 110 111 /** 112 * Bridge function for REEF .NET to submit context, service. and task configurations for the allocated evaluator. 113 * @param contextConfigurationString the context configuration from .NET. 114 * @param serviceConfigurationString the service configuration from .NET. 115 * @param taskConfigurationString the task configuration from .NET. 116 */ 117 public void submitContextAndServiceAndTaskString( 118 final String contextConfigurationString, 119 final String serviceConfigurationString, 120 final String taskConfigurationString) { 121 if (contextConfigurationString.isEmpty()) { 122 throw new RuntimeException("empty contextConfigurationString provided."); 123 } 124 if (serviceConfigurationString.isEmpty()) { 125 throw new RuntimeException("empty serviceConfigurationString provided."); 126 } 127 if (taskConfigurationString.isEmpty()) { 128 throw new RuntimeException("empty taskConfigurationString provided."); 129 } 130 131 //When submit over the bridge, we would keep the task configuration as a serialized string. 132 //submitContextAndServiceAndTask(final Configuration contextConfiguration, final Configuration serviceConfiguration, 133 //final String taskConfiguration) is not exposed in the interface. Therefore cast is necessary. 134 ((AllocatedEvaluatorImpl)jallocatedEvaluator).submitContextAndServiceAndTask( 135 contextConfigurationString, serviceConfigurationString, taskConfigurationString); 136 } 137 138 /** 139 * Gets the serialized evaluator descriptor from the Java allocated evaluator. 140 * @return the serialized evaluator descriptor. 141 */ 142 public String getEvaluatorDescriptorString() { 143 final String descriptorString = 144 Utilities.getEvaluatorDescriptorString(jallocatedEvaluator.getEvaluatorDescriptor()); 145 LOG.log(Level.INFO, "allocated evaluator - serialized evaluator descriptor: " + descriptorString); 146 return descriptorString; 147 } 148 149 /** 150 * @return the nameServerInfo string. 151 */ 152 public String getNameServerInfo() { 153 return nameServerInfo; 154 } 155 156 /** 157 * @return the evaluator id. 158 */ 159 @Override 160 public String getId() { 161 return evaluatorId; 162 } 163 164 /** 165 * Closes the Java AllocatedEvaluator. 166 */ 167 @Override 168 public void close() { 169 jallocatedEvaluator.close(); 170 } 171}