jeongyooneo commented on a change in pull request #21: [NEMO-46] Make the 
operations on ExecutorRegistry atomic
URL: https://github.com/apache/incubator-nemo/pull/21#discussion_r189777669
 
 

 ##########
 File path: 
runtime/master/src/main/java/edu/snu/nemo/runtime/master/scheduler/ExecutorRegistry.java
 ##########
 @@ -15,190 +15,98 @@
  */
 package edu.snu.nemo.runtime.master.scheduler;
 
+import com.google.common.annotations.VisibleForTesting;
+import edu.snu.nemo.common.Pair;
 import edu.snu.nemo.runtime.master.resource.ExecutorRepresenter;
 import org.apache.reef.annotations.audience.DriverSide;
 
-import javax.annotation.Nonnull;
-import javax.annotation.concurrent.NotThreadSafe;
+import javax.annotation.concurrent.ThreadSafe;
 import javax.inject.Inject;
 import java.util.*;
+import java.util.function.BiFunction;
+import java.util.function.Consumer;
+import java.util.stream.Collectors;
 
 /**
- * (WARNING) This class is not thread-safe.
- * (i.e., Only a SchedulingPolicy accesses this class)
- *
+ * (WARNING) This class must be thread-safe.
  * Maintains map between executor id and {@link ExecutorRepresenter}.
  */
 @DriverSide
-@NotThreadSafe
+@ThreadSafe
 public final class ExecutorRegistry {
-  private final Map<String, ExecutorRepresenter> runningExecutors;
-  private final Map<String, ExecutorRepresenter> failedExecutors;
-  private final Map<String, ExecutorRepresenter> completedExecutors;
-
-  @Inject
-  public ExecutorRegistry() {
-    this.runningExecutors = new HashMap<>();
-    this.failedExecutors = new HashMap<>();
-    this.completedExecutors = new HashMap<>();
-  }
-
-  @Override
-  public String toString() {
-    final StringBuffer sb = new StringBuffer();
-    sb.append("Running: ");
-    sb.append(runningExecutors.toString());
-    sb.append("/ Failed: ");
-    sb.append(failedExecutors.toString());
-    sb.append("/ Completed: ");
-    sb.append(completedExecutors.toString());
-    return sb.toString();
-  }
-
   /**
-   * @param executorId the executor id
-   * @return the corresponding {@link ExecutorRepresenter} that has not failed
-   * @throws NoSuchExecutorException when the executor was not found
+   * States of an executor.
    */
-  @Nonnull
-  public ExecutorRepresenter getExecutorRepresenter(final String executorId) 
throws NoSuchExecutorException {
-    try {
-      return getRunningExecutorRepresenter(executorId);
-    } catch (final NoSuchExecutorException e) {
-      return getFailedExecutorRepresenter(executorId);
-    }
+  enum ExecutorState {
+    RUNNING,
+    FAILED,
+    COMPLETED
   }
 
-  /**
-   * @param executorId the executor id
-   * @return the corresponding {@link ExecutorRepresenter} that has not failed
-   * @throws NoSuchExecutorException when the executor was not found
-   */
-  @Nonnull
-  public ExecutorRepresenter getRunningExecutorRepresenter(final String 
executorId) throws NoSuchExecutorException {
-    final ExecutorRepresenter representer = runningExecutors.get(executorId);
-    if (representer == null) {
-      throw new NoSuchExecutorException(executorId);
-    }
-    return representer;
+  private final Map<String, Pair<ExecutorRepresenter, ExecutorState>> 
executors;
+
+  @Inject
+  public ExecutorRegistry() {
+    this.executors = new HashMap<>();
   }
 
-  /**
-   * @param executorId the executor id
-   * @return the corresponding {@link ExecutorRepresenter} that has not failed
-   * @throws NoSuchExecutorException when the executor was not found
-   */
-  @Nonnull
-  public ExecutorRepresenter getFailedExecutorRepresenter(final String 
executorId) throws NoSuchExecutorException {
-    final ExecutorRepresenter representer = failedExecutors.get(executorId);
-    if (representer == null) {
-      throw new NoSuchExecutorException(executorId);
+  synchronized void registerExecutor(final ExecutorRepresenter executor) {
+    final String executorId = executor.getExecutorId();
+    if (executors.containsKey(executorId)) {
+      throw new IllegalArgumentException("Duplicate executor: " + 
executor.toString());
+    } else {
+      // System.out.println("executor registered " + executor.getExecutorId());
 
 Review comment:
   Maybe remove this comment or change it to `LOG.info`?

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to