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 evaluatorConfigurationString the evaluator configuration from .NET.
058   * @param contextConfigurationString the context configuration from .NET.
059   * @param taskConfigurationString the task configuration from .NET.
060   */
061  public void submitContextAndTaskString(final String evaluatorConfigurationString,
062                                         final String contextConfigurationString,
063                                         final String taskConfigurationString) {
064    if (evaluatorConfigurationString.isEmpty()) {
065      throw new RuntimeException("empty evaluatorConfigurationString provided.");
066    }
067    if (contextConfigurationString.isEmpty()) {
068      throw new RuntimeException("empty contextConfigurationString provided.");
069    }
070    if (taskConfigurationString.isEmpty()) {
071      throw new RuntimeException("empty taskConfigurationString provided.");
072    }
073    //When submit over the bridge, we would keep the task configurations as a serialized strings.
074    //submitContextAndTask(final String contextConfiguration,
075    //final String taskConfiguration) is not exposed in the interface. Therefore cast is necessary.
076    ((AllocatedEvaluatorImpl)jallocatedEvaluator)
077        .submitContextAndTask(evaluatorConfigurationString, contextConfigurationString, taskConfigurationString);
078  }
079
080  /**
081   * Bridge function for REEF .NET to submit context configuration for the allocated evaluator.
082   * @param evaluatorConfigurationString the evaluator configuration from .NET.
083   * @param contextConfigurationString the context configuration from .NET.
084   */
085  public void submitContextString(final String evaluatorConfigurationString,
086                                  final String contextConfigurationString) {
087    if (evaluatorConfigurationString.isEmpty()) {
088      throw new RuntimeException("empty evaluatorConfigurationString provided.");
089    }
090    if (contextConfigurationString.isEmpty()) {
091      throw new RuntimeException("empty contextConfigurationString provided.");
092    }
093
094    //When submit over the bridge, we would keep the contextConfigurationString as serialized strings.
095    //public void submitContext(final String contextConfiguration)
096    // is not exposed in the interface. Therefore cast is necessary.
097    ((AllocatedEvaluatorImpl)jallocatedEvaluator).submitContext(
098        evaluatorConfigurationString, contextConfigurationString);
099  }
100
101  /**
102   * Bridge function for REEF .NET to submit context and service configurations for the allocated evaluator.
103   * @param evaluatorConfigurationString the evaluator configuration from .NET.
104   * @param contextConfigurationString the context configuration from .NET.
105   * @param serviceConfigurationString the service configuration from .NET.
106   */
107  public void submitContextAndServiceString(final String evaluatorConfigurationString,
108                                            final String contextConfigurationString,
109                                            final String serviceConfigurationString) {
110    if (evaluatorConfigurationString.isEmpty()) {
111      throw new RuntimeException("empty evaluatorConfigurationString provided.");
112    }
113    if (contextConfigurationString.isEmpty()) {
114      throw new RuntimeException("empty contextConfigurationString provided.");
115    }
116    if (serviceConfigurationString.isEmpty()) {
117      throw new RuntimeException("empty serviceConfigurationString provided.");
118    }
119
120    //When submit over the bridge, we would keep the configurations as serialized strings.
121    //public void submitContextAndService(final String contextConfiguration,
122    //final String serviceConfiguration) is not exposed in the interface. Therefore cast is necessary.
123    ((AllocatedEvaluatorImpl)jallocatedEvaluator)
124        .submitContextAndService(evaluatorConfigurationString, contextConfigurationString, serviceConfigurationString);
125  }
126
127  /**
128   * Bridge function for REEF .NET to submit context, service. and task configurations for the allocated evaluator.
129   * @param evaluatorConfigurationString the evaluator configuration from .NET.
130   * @param contextConfigurationString the context configuration from .NET.
131   * @param serviceConfigurationString the service configuration from .NET.
132   * @param taskConfigurationString the task configuration from .NET.
133   */
134  public void submitContextAndServiceAndTaskString(
135      final String evaluatorConfigurationString,
136      final String contextConfigurationString,
137      final String serviceConfigurationString,
138      final String taskConfigurationString) {
139    if (evaluatorConfigurationString.isEmpty()) {
140      throw new RuntimeException("empty evaluatorConfigurationString provided.");
141    }
142    if (contextConfigurationString.isEmpty()) {
143      throw new RuntimeException("empty contextConfigurationString provided.");
144    }
145    if (serviceConfigurationString.isEmpty()) {
146      throw new RuntimeException("empty serviceConfigurationString provided.");
147    }
148    if (taskConfigurationString.isEmpty()) {
149      throw new RuntimeException("empty taskConfigurationString provided.");
150    }
151
152    //When submit over the bridge, we would keep the task configuration as a serialized string.
153    //submitContextAndServiceAndTask(final Configuration contextConfiguration, final Configuration serviceConfiguration,
154    //final String taskConfiguration) is not exposed in the interface. Therefore cast is necessary.
155    ((AllocatedEvaluatorImpl)jallocatedEvaluator).submitContextAndServiceAndTask(
156        evaluatorConfigurationString, contextConfigurationString, serviceConfigurationString, taskConfigurationString);
157  }
158
159  /**
160   * Gets the serialized evaluator descriptor from the Java allocated evaluator.
161   * @return the serialized evaluator descriptor.
162   */
163  public String getEvaluatorDescriptorString() {
164    final String descriptorString =
165        Utilities.getEvaluatorDescriptorString(jallocatedEvaluator.getEvaluatorDescriptor());
166    LOG.log(Level.INFO, "allocated evaluator - serialized evaluator descriptor: " + descriptorString);
167    return descriptorString;
168  }
169
170  /**
171   * @return the nameServerInfo string.
172   */
173  public String getNameServerInfo() {
174    return nameServerInfo;
175  }
176
177  /**
178   * @return the evaluator id.
179   */
180  @Override
181  public String getId() {
182    return evaluatorId;
183  }
184
185  /**
186   * Closes the Java AllocatedEvaluator.
187   */
188  @Override
189  public void close() {
190    jallocatedEvaluator.close();
191  }
192}