reef-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "ASF GitHub Bot (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (REEF-2025) A new module containing the new Java bridge
Date Sat, 09 Jun 2018 01:46:02 GMT

    [ https://issues.apache.org/jira/browse/REEF-2025?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16506774#comment-16506774
] 

ASF GitHub Bot commented on REEF-2025:
--------------------------------------

motus commented on a change in pull request #1466: [REEF-2025] A new module containing the
new Java bridge
URL: https://github.com/apache/reef/pull/1466#discussion_r194208322
 
 

 ##########
 File path: lang/java/reef-bridge-proto-java/src/main/java/org/apache/reef/bridge/driver/service/grpc/GRPCDriverService.java
 ##########
 @@ -0,0 +1,1060 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.reef.bridge.driver.service.grpc;
+
+import com.google.protobuf.ByteString;
+import io.grpc.*;
+import io.grpc.stub.StreamObserver;
+import org.apache.commons.lang.StringUtils;
+import org.apache.reef.bridge.driver.client.grpc.GRPCUtils;
+import org.apache.reef.bridge.driver.service.DriverClientException;
+import org.apache.reef.bridge.driver.service.IDriverService;
+import org.apache.reef.bridge.service.parameters.DriverClientCommand;
+import org.apache.reef.bridge.proto.*;
+import org.apache.reef.bridge.proto.Void;
+import org.apache.reef.driver.context.ActiveContext;
+import org.apache.reef.driver.context.ClosedContext;
+import org.apache.reef.driver.context.ContextMessage;
+import org.apache.reef.driver.context.FailedContext;
+import org.apache.reef.driver.evaluator.*;
+import org.apache.reef.driver.restart.DriverRestartCompleted;
+import org.apache.reef.driver.restart.DriverRestarted;
+import org.apache.reef.driver.task.*;
+import org.apache.reef.runtime.common.driver.context.EvaluatorContext;
+import org.apache.reef.runtime.common.driver.evaluator.AllocatedEvaluatorImpl;
+import org.apache.reef.runtime.common.driver.idle.IdleMessage;
+import org.apache.reef.runtime.common.files.REEFFileNames;
+import org.apache.reef.runtime.common.utils.ExceptionCodec;
+import org.apache.reef.tang.annotations.Parameter;
+import org.apache.reef.tang.formats.ConfigurationSerializer;
+import org.apache.reef.util.OSUtils;
+import org.apache.reef.util.Optional;
+import org.apache.reef.wake.EventHandler;
+import org.apache.reef.wake.remote.ports.TcpPortProvider;
+import org.apache.reef.wake.time.Clock;
+import org.apache.reef.wake.time.event.Alarm;
+import org.apache.reef.wake.time.event.StartTime;
+import org.apache.reef.wake.time.event.StopTime;
+
+import javax.inject.Inject;
+import java.io.*;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ * GRPC DriverBridgeService that interacts with higher-level languages.
+ */
+public final class GRPCDriverService implements IDriverService {
+  private static final Logger LOG = Logger.getLogger(GRPCDriverService.class.getName());
+
+  private static final Void VOID = Void.newBuilder().build();
+
+  private Process driverProcess;
+
+  private enum StreamType { STDOUT, STDERR }
+
+  private Server server;
+
+  private DriverClientGrpc.DriverClientFutureStub clientStub;
+
+  private final Clock clock;
+
+  private final REEFFileNames reefFileNames;
+
+  private final ExceptionCodec exceptionCodec;
+
+  private final ConfigurationSerializer configurationSerializer;
+
+  private final EvaluatorRequestor evaluatorRequestor;
+
+  private final JVMProcessFactory jvmProcessFactory;
+
+  private final CLRProcessFactory clrProcessFactory;
+
+  private final TcpPortProvider tcpPortProvider;
+
+  private final String driverClientCommand;
+
+  private final Map<String, AllocatedEvaluator> allocatedEvaluatorMap = new HashMap<>();
+
+  private final Map<String, ActiveContext> activeContextMap = new HashMap<>();
+
+  private final Map<String, RunningTask> runningTaskMap = new HashMap<>();
+
+  private boolean stopped = false;
+
+  @Inject
+  private GRPCDriverService(
+      final Clock clock,
+      final REEFFileNames reefFileNames,
+      final EvaluatorRequestor evaluatorRequestor,
+      final ConfigurationSerializer configurationSerializer,
+      final JVMProcessFactory jvmProcessFactory,
+      final CLRProcessFactory clrProcessFactory,
+      final TcpPortProvider tcpPortProvider,
+      final ExceptionCodec exceptionCodec,
+      @Parameter(DriverClientCommand.class) final String driverClientCommand) {
+    this.clock = clock;
+    this.reefFileNames = reefFileNames;
+    this.exceptionCodec = exceptionCodec;
+    this.configurationSerializer = configurationSerializer;
+    this.jvmProcessFactory = jvmProcessFactory;
+    this.clrProcessFactory = clrProcessFactory;
+    this.evaluatorRequestor = evaluatorRequestor;
+    this.driverClientCommand = driverClientCommand;
+    this.tcpPortProvider = tcpPortProvider;
+  }
+
+  private void start() throws IOException, InterruptedException {
+    for (final int port : this.tcpPortProvider) {
+      try {
+        this.server = ServerBuilder.forPort(port)
+            .addService(new DriverBridgeServiceImpl())
+            .build()
+            .start();
+        LOG.log(Level.INFO, "Server started, listening on port [{0}]", port);
+        break;
+      } catch (final IOException e) {
+        LOG.log(Level.WARNING, "Unable to bind to port [{0}]", port);
+      }
+    }
+    if (this.server == null || this.server.isTerminated()) {
+      throw new IOException("Unable to start gRPC server");
+    }
+    final String cmd = this.driverClientCommand + " " + this.server.getPort();
+    final List<String> cmdOs = OSUtils.isWindows() ?
+        Arrays.asList("cmd.exe", "/c", cmd) : Arrays.asList("/bin/sh", "-c", cmd);
+    LOG.log(Level.INFO, "CMD: {0}", cmdOs);
+    this.driverProcess = new ProcessBuilder()
+        .command(cmdOs)
+        .redirectError(new File(this.reefFileNames.getDriverClientStderrFileName()))
+        .redirectOutput(new File(this.reefFileNames.getDriverClientStdoutFileName()))
+        .start();
+    synchronized (this) {
+      int attempts = 10; // give some time
+      /* wait for driver client process to register
+       * Note: attempts and wait time have been given reasonable hardcoded values for a driver
+       * client to register with the driver service (us). Making these values configurable
would
+       * require additions to the ClientProtocol buffer such that they can be passed to the
+       * GRPCDriverServiceConfigurationProvider and bound to the appropriate NamedParameters.
It
+       * is the opinion at the time of this writing that a driver client should be able to
register
+       * within 10 seconds.
+       */
+      while (attempts-- > 0 && this.clientStub == null && driverProcessIsAlive())
{
+        LOG.log(Level.INFO, "waiting for driver process to register");
+        this.wait(1000); // a second
+      }
+    }
+    if (driverProcessIsAlive()) {
+      final Thread closeChildThread = new Thread() {
+        public void run() {
+          synchronized (GRPCDriverService.this) {
+            if (GRPCDriverService.this.driverProcess != null) {
+              GRPCDriverService.this.driverProcess.destroy();
+              GRPCDriverService.this.driverProcess = null;
+            }
+          }
+        }
+      };
+      // This is probably overkill since shutdown should be called in the stop handler.
+      Runtime.getRuntime().addShutdownHook(closeChildThread);
+    }
+  }
+
+  private void stop() {
+    stop(null);
+  }
+
+  private void stop(final Throwable t) {
+    LOG.log(Level.INFO, "STOP: gRPC Driver Service", t);
+    if (!stopped) {
+      try {
+        if (!clock.isClosed()) {
+          if (t != null) {
+            clock.stop(t);
+          } else {
+            clock.stop();
+          }
+        }
+        if (server != null) {
+          LOG.log(Level.INFO, "Shutdown gRPC");
+          this.server.shutdown();
+          this.server = null;
+        }
+        if (this.driverProcess != null) {
+          LOG.log(Level.INFO, "shutdown driver process");
+          dump();
+          this.driverProcess.destroy();
+          this.driverProcess = null;
+        }
+      } finally {
+        LOG.log(Level.INFO, "COMPLETED STOP: gRPC Driver Service");
+        stopped = true;
+      }
+    }
+  }
+
+  private void dump() {
+    if (!driverProcessIsAlive()) {
+      LOG.log(Level.INFO, "Exit code: {0}", this.driverProcess.exitValue());
+    }
+    dumpStream(StreamType.STDOUT);
+    dumpStream(StreamType.STDERR);
+  }
+
+  private void dumpStream(final StreamType type) {
+    final StringBuilder stringBuilder = new StringBuilder();
+
+    final String name;
+    final InputStream stream;
+    switch(type) {
+    case STDOUT:
+      name = "stdout";
+      stream = this.driverProcess.getInputStream();
+      break;
+    case STDERR:
+      name = "stderr";
+      stream = this.driverProcess.getErrorStream();
+      break;
+    default:
+      throw new RuntimeException("Invalid stream type value");
+    }
+
+    LOG.log(Level.INFO, "capturing driver process {0}", name);
+    try {
+      stringBuilder.append("\n==============================================\n");
+      try (final BufferedReader reader = new BufferedReader(new InputStreamReader(stream)))
{
+        while (reader.ready()) {
+          stringBuilder.append(reader.readLine()).append('\n');
+        }
+      }
+      stringBuilder.append("\n==============================================\n");
+    } catch (final IOException e) {
+      LOG.log(Level.WARNING, "Error while capturing output stream", e);
+    }
+    LOG.log(Level.INFO, "{0}", stringBuilder);
+  }
+
+  /**
+   * Determines if the driver process is still alive by
+   * testing for its exit value, which throws {@link IllegalThreadStateException}
+   * if process is still running.
+   * @return true if driver process is alive, false otherwise
+   */
+  private boolean driverProcessIsAlive() {
+    if (this.driverProcess != null) {
+      try {
+        this.driverProcess.exitValue();
+      } catch (final IllegalThreadStateException e) {
+        return true;
+      }
+    }
+    return false;
+  }
+
+  private EvaluatorDescriptorInfo toEvaluatorDescriptorInfo(final EvaluatorDescriptor descriptor)
{
 
 Review comment:
   make it `static`

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


> A new module containing the new Java bridge
> -------------------------------------------
>
>                 Key: REEF-2025
>                 URL: https://issues.apache.org/jira/browse/REEF-2025
>             Project: REEF
>          Issue Type: Sub-task
>          Components: REEF Bridge
>    Affects Versions: 0.17
>            Reporter: Tyson Condie
>            Assignee: Tyson Condie
>            Priority: Major
>             Fix For: 0.17
>
>
> This Jira introduces the module containing the new bridge. 



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Mime
View raw message