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 Tue, 05 Jun 2018 20:42:01 GMT

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

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_r193212276
 
 

 ##########
 File path: lang/java/reef-bridge-proto-java/src/main/java/org/apache/reef/bridge/driver/client/grpc/DriverClientService.java
 ##########
 @@ -0,0 +1,663 @@
+/*
+ * 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.client.grpc;
+
+import com.google.common.collect.Lists;
+import io.grpc.Server;
+import io.grpc.ServerBuilder;
+import io.grpc.Status;
+import io.grpc.stub.StreamObserver;
+import org.apache.commons.lang.StringUtils;
+import org.apache.reef.bridge.driver.client.DriverClientDispatcher;
+import org.apache.reef.bridge.driver.client.IDriverClientService;
+import org.apache.reef.bridge.driver.client.JVMClientProcess;
+import org.apache.reef.bridge.driver.client.events.*;
+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.FailedContext;
+import org.apache.reef.driver.evaluator.EvaluatorDescriptor;
+import org.apache.reef.driver.restart.DriverRestartCompleted;
+import org.apache.reef.driver.restart.DriverRestarted;
+import org.apache.reef.driver.task.FailedTask;
+import org.apache.reef.exception.EvaluatorException;
+import org.apache.reef.runtime.common.driver.evaluator.EvaluatorDescriptorImpl;
+import org.apache.reef.runtime.common.utils.ExceptionCodec;
+import org.apache.reef.tang.InjectionFuture;
+import org.apache.reef.util.Optional;
+import org.apache.reef.wake.remote.ports.TcpPortProvider;
+import org.apache.reef.wake.time.Clock;
+import org.apache.reef.wake.time.Time;
+import org.apache.reef.wake.time.event.StartTime;
+import org.apache.reef.wake.time.event.StopTime;
+
+import javax.inject.Inject;
+import java.io.IOException;
+import java.util.*;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ * The driver client service that accepts incoming messages driver service and
+ * dispatches appropriate objects to the application.
+ */
+public final class DriverClientService extends DriverClientGrpc.DriverClientImplBase
+    implements IDriverClientService {
+
+  private static final Logger LOG = Logger.getLogger(DriverClientService.class.getName());
+
+  private Server server;
+
+  private final Object lock = new Object();
+
+  private final InjectionFuture<Clock> clock;
+
+  private final ExceptionCodec exceptionCodec;
+
+  private final DriverServiceClient driverServiceClient;
+
+  private final TcpPortProvider tcpPortProvider;
+
+  private final InjectionFuture<DriverClientDispatcher> clientDriverDispatcher;
+
+  private final Map<String, AllocatedEvaluatorBridge> evaluatorBridgeMap = new HashMap<>();
+
+  private final Map<String, ActiveContextBridge> activeContextBridgeMap = new HashMap<>();
+
+  private int outstandingEvaluatorCount = 0;
+
+  @Inject
+  private DriverClientService(
+      final ExceptionCodec exceptionCodec,
+      final DriverServiceClient driverServiceClient,
+      final TcpPortProvider tcpPortProvider,
+      final InjectionFuture<Clock> clock,
+      final InjectionFuture<DriverClientDispatcher> clientDriverDispatcher) {
+    this.exceptionCodec = exceptionCodec;
+    this.driverServiceClient = driverServiceClient;
+    this.tcpPortProvider = tcpPortProvider;
+    this.clock = clock;
+    this.clientDriverDispatcher = clientDriverDispatcher;
+  }
+
+  @Override
+  public void notifyEvaluatorRequest(final int count) {
+    synchronized (this.lock) {
+      this.outstandingEvaluatorCount += count;
+      this.lock.notify();
+    }
+  }
+
+  @Override
+  public void start() throws IOException {
+    for (final Integer port : this.tcpPortProvider) {
+      try {
+        this.server = ServerBuilder.forPort(port)
+            .addService(this)
+            .build()
+            .start();
+        LOG.info("Driver Client Server started, listening on " + port);
+        break;
+      } catch (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");
+    }
+    this.driverServiceClient.registerDriverClientService("localhost", this.server.getPort());
+  }
+
+  @Override
+  public void awaitTermination() throws InterruptedException {
+    if (this.server != null) {
+      this.server.awaitTermination();
+    }
+  }
+
+  @Override
+  public void idlenessCheckHandler(final Void request, final StreamObserver<IdleStatus>
responseObserver) {
+    if (isIdle()) {
+      LOG.log(Level.INFO, "possibly idle. waiting for some action.");
+      try {
+        synchronized (this.lock) {
+          this.lock.wait(1000); // wait a second
+        }
+      } catch (InterruptedException e) {
+        LOG.log(Level.WARNING, e.getMessage());
+      }
+    }
+    responseObserver.onNext(IdleStatus.newBuilder()
+        .setReason("DriverClient checking idleness")
+        .setIsIdle(this.isIdle())
+        .build());
+    responseObserver.onCompleted();
+  }
+
+  @Override
+  public void startHandler(final StartTimeInfo request, final StreamObserver<Void>
responseObserver) {
+    try {
+      LOG.log(Level.INFO, "StartHandler at time {0}", request.getStartTime());
+      final StartTime startTime = new StartTime(request.getStartTime());
+      this.clientDriverDispatcher.get().dispatch(startTime);
+    } finally {
+      responseObserver.onNext(null);
+      responseObserver.onCompleted();
+    }
+  }
+
+  @Override
+  public void stopHandler(final StopTimeInfo request, final StreamObserver<ExceptionInfo>
responseObserver) {
+    try {
+      LOG.log(Level.INFO, "StopHandler at time {0}", request.getStopTime());
+      final StopTime stopTime = new StopTime(request.getStopTime());
+      final Throwable error = this.clientDriverDispatcher.get().dispatch(stopTime);
+      if (error != null) {
+        responseObserver.onNext(GRPCUtils.createExceptionInfo(this.exceptionCodec, error));
+      } else {
+        responseObserver.onNext(ExceptionInfo.newBuilder().setNoError(true).build());
+      }
+    } finally {
+      responseObserver.onCompleted();
+      this.server.shutdown();
+    }
+  }
+
+  @Override
+  public void alarmTrigger(final AlarmTriggerInfo request, final StreamObserver<Void>
responseObserver) {
+    try {
+      LOG.log(Level.INFO, "Alarm Trigger id {0}", request.getAlarmId());
+      this.clientDriverDispatcher.get().dispatchAlarm(request.getAlarmId());
+    } finally {
+      responseObserver.onNext(null);
+      responseObserver.onCompleted();
+    }
+  }
+
+  @Override
+  public void allocatedEvaluatorHandler(final EvaluatorInfo request, final StreamObserver<Void>
responseObserver) {
+    try {
+      synchronized (this.lock) {
+        this.outstandingEvaluatorCount--;
+      }
+      LOG.log(Level.INFO, "Allocated evaluator id {0}", request.getEvaluatorId());
+      final AllocatedEvaluatorBridge eval = new AllocatedEvaluatorBridge(
+          request.getEvaluatorId(),
+          toEvaluatorDescriptor(request.getDescriptorInfo()),
+          this.driverServiceClient);
+      this.evaluatorBridgeMap.put(eval.getId(), eval);
+      this.clientDriverDispatcher.get().dispatch(eval);
+    } finally {
+      responseObserver.onNext(null);
+      responseObserver.onCompleted();
+    }
+  }
+
+  @Override
+  public void completedEvaluatorHandler(final EvaluatorInfo request, final StreamObserver<Void>
responseObserver) {
+    try {
+      LOG.log(Level.INFO, "Completed Evaluator id {0}", request.getEvaluatorId());
+      this.evaluatorBridgeMap.remove(request.getEvaluatorId());
+      this.clientDriverDispatcher.get().dispatch(new CompletedEvaluatorBridge(request.getEvaluatorId()));
+    } finally {
+      responseObserver.onNext(null);
+      responseObserver.onCompleted();
+    }
+  }
+
+  @Override
+  public void failedEvaluatorHandler(final EvaluatorInfo request, final StreamObserver<Void>
responseObserver) {
+    try {
+      if (!this.evaluatorBridgeMap.containsKey(request.getEvaluatorId())) {
+        LOG.log(Level.INFO, "Failed evalautor that we were not allocated");
+        synchronized (this.lock) {
+          if (this.outstandingEvaluatorCount > 0) {
+            this.outstandingEvaluatorCount--;
+          }
+        }
+        return;
+      }
+      LOG.log(Level.INFO, "Failed Evaluator id {0}", request.getEvaluatorId());
+      final AllocatedEvaluatorBridge eval = this.evaluatorBridgeMap.remove(request.getEvaluatorId());
+      List<FailedContext> failedContextList = new ArrayList<>();
+      if (request.getFailure().getFailedContextsList() != null) {
+        for (final String failedContextId : request.getFailure().getFailedContextsList())
{
+          final ActiveContextBridge context = this.activeContextBridgeMap.get(failedContextId);
+          failedContextList.add(new FailedContextBridge(
+              context.getId(),
+              eval.getId(),
+              request.getFailure().getMessage(),
+              eval.getEvaluatorDescriptor(),
+              context.getParentId().isPresent() ?
+                  Optional.<ActiveContext>of(this.activeContextBridgeMap.get(context.getParentId().get()))
:
+                  Optional.<ActiveContext>empty(),
+              Optional.<Throwable>empty()));
+        }
+        for (final String failedContextId : request.getFailure().getFailedContextsList())
{
+          this.activeContextBridgeMap.remove(failedContextId);
+        }
+      }
+      this.clientDriverDispatcher.get().dispatch(
+          new FailedEvaluatorBridge(
+              eval.getId(),
+              new EvaluatorException(request.getEvaluatorId(), request.getFailure().getMessage()),
+              failedContextList,
+              request.getFailure().getFailedTaskId() != null ?
+                  Optional.of(new FailedTask(
+                      request.getFailure().getFailedTaskId(),
+                      request.getFailure().getMessage(),
+                      Optional.<String>empty(),
+                      Optional.<Throwable>empty(),
+                      Optional.<byte[]>empty(),
+                      Optional.<ActiveContext>empty())) :
+                  Optional.<FailedTask>empty()));
+    } finally {
+      responseObserver.onNext(null);
+      responseObserver.onCompleted();
+    }
+  }
+
+  @Override
+  public void activeContextHandler(final ContextInfo request, final StreamObserver<Void>
responseObserver) {
+    try {
+      LOG.log(Level.INFO, "Active context id {0}", request.getContextId());
+      final AllocatedEvaluatorBridge eval = this.evaluatorBridgeMap.get(request.getEvaluatorId());
+      final ActiveContextBridge context = new ActiveContextBridge(
+          this.driverServiceClient,
+          request.getContextId(),
+          request.getParentId() != null ? Optional.of(request.getParentId()) : Optional.<String>empty(),
+          eval.getId(),
+          eval.getEvaluatorDescriptor());
+      this.activeContextBridgeMap.put(context.getId(), context);
+      this.clientDriverDispatcher.get().dispatch(context);
+    } finally {
+      responseObserver.onNext(null);
+      responseObserver.onCompleted();
+    }
+  }
+
+  @Override
+  public void closedContextHandler(final ContextInfo request, final StreamObserver<Void>
responseObserver) {
+    if (this.activeContextBridgeMap.containsKey(request.getContextId())) {
+      LOG.log(Level.INFO, "Closed context id {0}", request.getContextId());
+      try {
+        final ActiveContextBridge context = this.activeContextBridgeMap.remove(request.getContextId());
+        this.clientDriverDispatcher.get().dispatch(
+            new ClosedContextBridge(
+                context.getId(),
+                context.getEvaluatorId(),
+                this.activeContextBridgeMap.get(request.getParentId()),
+                context.getEvaluatorDescriptor()));
+      } finally {
+        responseObserver.onNext(null);
+        responseObserver.onCompleted();
+      }
+    } else {
+      responseObserver.onError(Status.INTERNAL
+          .withDescription("Unknown context id " + request.getContextId() + " in close")
+          .asRuntimeException());
+    }
+  }
+
+  @Override
+  public void failedContextHandler(final ContextInfo request, final StreamObserver<Void>
responseObserver) {
+    if (this.activeContextBridgeMap.containsKey(request.getContextId())) {
+      LOG.log(Level.INFO, "Failed context id {0}", request.getContextId());
+      try {
+        final ActiveContextBridge context = this.activeContextBridgeMap.remove(request.getContextId());
+        final Optional<ActiveContext> parent = context.getParentId().isPresent() ?
+            Optional.<ActiveContext>of(this.activeContextBridgeMap.get(context.getParentId().get()))
:
+            Optional.<ActiveContext>empty();
+        final Optional<Throwable> reason = !request.getException().getData().isEmpty()
 ?
+            this.exceptionCodec.fromBytes(request.getException().getData().toByteArray())
:
+            Optional.<Throwable>empty();
+        this.clientDriverDispatcher.get().dispatch(
+            new FailedContextBridge(
+                context.getId(),
+                context.getEvaluatorId(),
+                request.getException().getMessage(),
+                context.getEvaluatorDescriptor(),
+                parent,
+                reason));
+      } finally {
+        responseObserver.onNext(null);
+        responseObserver.onCompleted();
+      }
+    } else {
+      responseObserver.onError(Status.INTERNAL
+          .withDescription("Unknown context id " + request.getContextId() + " in close")
+          .asRuntimeException());
+    }
+  }
+
+  @Override
+  public void contextMessageHandler(final ContextMessageInfo request, final StreamObserver<Void>
responseObserver) {
+    if (this.activeContextBridgeMap.containsKey(request.getContextId())) {
+      LOG.log(Level.INFO, "Message context id {0}", request.getContextId());
+      try {
+        this.clientDriverDispatcher.get().dispatch(
+            new ContextMessageBridge(
+                request.getContextId(),
+                request.getMessageSourceId(),
+                request.getSequenceNumber(),
+                request.getPayload().toByteArray()));
+      } finally {
+        responseObserver.onNext(null);
+        responseObserver.onCompleted();
+      }
+    } else {
+      responseObserver.onError(Status.INTERNAL
+          .withDescription("Unknown context id " + request.getContextId() + " in close")
+          .asRuntimeException());
+    }
+  }
+
+  @Override
+  public void runningTaskHandler(final TaskInfo request, final StreamObserver<Void>
responseObserver) {
+    final ContextInfo contextInfo = request.getContext();
+    if (!this.activeContextBridgeMap.containsKey(contextInfo.getContextId())) {
+      this.activeContextBridgeMap.put(contextInfo.getContextId(), toActiveContext(contextInfo));
+    }
+
+    LOG.log(Level.INFO, "Running task id {0}", request.getTaskId());
+    try {
+      final ActiveContextBridge context = this.activeContextBridgeMap.get(contextInfo.getContextId());
+      this.clientDriverDispatcher.get().dispatch(
+          new RunningTaskBridge(this.driverServiceClient, request.getTaskId(), context));
+    } finally {
+      responseObserver.onNext(null);
+      responseObserver.onCompleted();
+    }
+  }
+
+  @Override
+  public void failedTaskHandler(final TaskInfo request, final StreamObserver<Void>
responseObserver) {
+    if (request.hasContext() && !this.activeContextBridgeMap.containsKey(request.getContext().getContextId()))
{
+      this.activeContextBridgeMap.put(request.getContext().getContextId(), toActiveContext(request.getContext()));
+    }
+    try {
+      LOG.log(Level.INFO, "Failed task id {0}", request.getTaskId());
+      final Optional<ActiveContext> context =
+          this.activeContextBridgeMap.containsKey(request.getContext().getContextId()) ?
+              Optional.<ActiveContext>of(this.activeContextBridgeMap.get(request.getContext().getContextId()))
:
+              Optional.<ActiveContext>empty();
+      this.clientDriverDispatcher.get().dispatch(
+          new FailedTask(
+              request.getTaskId(),
+              request.getException().getMessage(),
+              Optional.of(request.getException().getName()),
+              request.getException().getData().isEmpty() ?
+                  Optional.<Throwable>of(new EvaluatorException(request.getException().getMessage()))
:
+                  this.exceptionCodec.fromBytes(request.getException().getData().toByteArray()),
+              Optional.<byte[]>empty(),
+              context));
+    } finally {
+      responseObserver.onNext(null);
+      responseObserver.onCompleted();
+    }
+  }
+
+  @Override
+  public void completedTaskHandler(final TaskInfo request, final StreamObserver<Void>
responseObserver) {
+    final ContextInfo contextInfo = request.getContext();
+    if (!this.activeContextBridgeMap.containsKey(contextInfo.getContextId())) {
+      this.activeContextBridgeMap.put(contextInfo.getContextId(), toActiveContext(contextInfo));
+    }
+    LOG.log(Level.INFO, "Completed task id {0}", request.getTaskId());
+    try {
+      final ActiveContextBridge context = this.activeContextBridgeMap.get(request.getContext().getContextId());
+      this.clientDriverDispatcher.get().dispatch(
+          new CompletedTaskBridge(
+              request.getTaskId(),
+              context,
+              request.getResult() != null && !request.getResult().isEmpty() ?
+                  request.getResult().toByteArray() : null));
+    } finally {
+      responseObserver.onNext(null);
+      responseObserver.onCompleted();
+    }
 
 Review comment:
   I've just realized that we can have a small wrapper to make sure `StreamObserver` always
gets closed:
   ```java
   public static class ObserverCleanup<T> implements AutoCloseable {
   
     private final StreamObserver<T> observer;
     private final T nextValue;
   
     public static <V> ObserverCleanup<V> of(final StreamObserver<V> observer)
{
       return of(observer, null);
     }
   
     public static <V> ObserverCleanup<V> of(final StreamObserver<V> observer,
final V nextValue) {
       return new ObserverCleanup<>(observer, nextValue);
     }
   
     private ObserverCleanup(final StreamObserver<T> observer, final T nextValue) {
       this.observer = observer;
       this.nextValue = nextValue;
     }
   
     @Override
     public void close() {
       this.observer.onNext(this.nextValue);
       this.observer.onCompleted();
     }
   }
   ```
   then our `try` block will look like this:
   ```java
   try (final ObserverCleanup _cleanup = ObserverCleanup.of(responseObserver)) {
     // ...
   }
   ```

----------------------------------------------------------------
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