wonook commented on a change in pull request #288: [NEMO-438] Create a 
Simulator for Simulating an Execution of an Execution Plan
URL: https://github.com/apache/incubator-nemo/pull/288#discussion_r386952862
 
 

 ##########
 File path: 
runtime/master/src/main/java/org/apache/nemo/runtime/master/scheduler/SimulatedTaskExecutor.java
 ##########
 @@ -0,0 +1,254 @@
+/*
+ * 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.nemo.runtime.master.scheduler;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.google.common.collect.Streams;
+import org.apache.commons.lang3.SerializationUtils;
+import org.apache.nemo.common.dag.DAG;
+import org.apache.nemo.common.ir.vertex.IRVertex;
+import org.apache.nemo.common.ir.vertex.executionproperty.ParallelismProperty;
+import org.apache.nemo.runtime.common.RuntimeIdManager;
+import org.apache.nemo.runtime.common.comm.ControlMessage;
+import org.apache.nemo.runtime.common.message.MessageEnvironment;
+import org.apache.nemo.runtime.common.message.MessageUtils;
+import org.apache.nemo.runtime.common.metric.JobMetric;
+import org.apache.nemo.runtime.common.metric.StateTransitionEvent;
+import org.apache.nemo.runtime.common.metric.TaskMetric;
+import org.apache.nemo.runtime.common.plan.RuntimeEdge;
+import org.apache.nemo.runtime.common.plan.Task;
+import org.apache.nemo.runtime.common.state.TaskState;
+import org.apache.nemo.runtime.master.metric.MetricStore;
+import org.apache.nemo.runtime.master.resource.ExecutorRepresenter;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.*;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.stream.Collectors;
+
+/**
+ * Class for simulated task execution.
+ */
+public final class SimulatedTaskExecutor {
+  private static final Logger LOG = 
LoggerFactory.getLogger(SimulatedTaskExecutor.class.getName());
+  private static final String TASK_METRIC_ID = "TaskMetric";
+
+  /**
+   * the simulation scheduler that the executor is associated with.
+   */
+  private final SimulationScheduler scheduler;
+  private final ExecutorRepresenter executorRepresenter;
+  private Long executorInitializationTime;
+  private final AtomicLong currentTime;
+  private Long timeCheckpoint;
+  private final MetricStore actualMetricStore;
+  private final ConcurrentMap<String, DAG<IRVertex, RuntimeEdge<IRVertex>>> 
stageIDToStageIRDAG;
+
+  /**
+   * Constructor.
+   * @param scheduler the simulation scheduler that the executor is associated 
with.
+   */
+  SimulatedTaskExecutor(final SimulationScheduler scheduler,
+                        final ExecutorRepresenter executorRepresenter,
+                        final MetricStore actualMetricStore) {
+    this.scheduler = scheduler;
+    this.executorRepresenter = executorRepresenter;
+    this.actualMetricStore = actualMetricStore;
+    this.stageIDToStageIRDAG = new ConcurrentHashMap<>();
+    this.currentTime = new AtomicLong(-1L);
+    this.executorInitializationTime = -1L;
+  }
+
+  /**
+   * Calculate the expected task duration.
+   * This only works if there exists metrics in the actual MetricStore, that 
contains information about the stage,
+   * as well as with the parallelism the task is associated with.
+   *
+   * @param task the task to calculate the task duration for.
+   * @return the expected task duration.
+   */
+  private long calculateExpectedTaskDuration(final Task task) {
+    final DAG<IRVertex, RuntimeEdge<IRVertex>> stageIRDAG = 
stageIDToStageIRDAG.computeIfAbsent(task.getStageId(),
+      i -> SerializationUtils.deserialize(task.getSerializedIRDag()));
+
+    final Map<String, Object> jobMetricMap = 
this.actualMetricStore.getMetricMap(JobMetric.class);
+    if (jobMetricMap.size() > 1) {
+      LOG.warn("MetricStore has more than one JobMetric. The results could be 
misleading.");
+    }
+    // Fetch first element.
+    final JobMetric jobMetric = (JobMetric) 
jobMetricMap.entrySet().iterator().next().getValue();
+    final JsonNode stageDAG = jobMetric.getStageDAG();
+
+    // Gather ID of stages that have the characteristics that the task 
possesses.
+    final Set<String> stageIdsToGatherMetricsFrom = Streams.stream(() -> 
stageDAG.get("vertices").iterator())
+      .filter(s -> s.get("properties").get("irDag").get("vertices").size()
+        == stageIRDAG.getVertices().size())  // same # of vertices.
+      .filter(s -> s.get("properties").get("irDag").get("edges").size()
+        == stageIRDAG.getEdges().size())  // same # of edges.
+      .filter(s -> s.get("properties").get("executionProperties")
+        
.get("org.apache.nemo.common.ir.vertex.executionproperty.ParallelismProperty").asInt(0)
+        == task.getPropertyValue(ParallelismProperty.class).orElse(0))  // 
same parallelism.
+      .map(s -> s.get("id").asText())
+      .collect(Collectors.toSet());
+
+    // Derive the average task duration from the stages.
+    final OptionalDouble average = 
this.actualMetricStore.getMetricMap(TaskMetric.class).entrySet().stream()
+      .filter(e -> 
stageIdsToGatherMetricsFrom.contains(RuntimeIdManager.getStageIdFromTaskId(e.getKey())))
+      .map(Map.Entry::getValue)  // stream of TaskMetric.
+      .mapToLong(tm -> ((TaskMetric) tm).getTaskDuration())
+      .filter(l -> l > 0)
+      .average();
+
+    // convert to long and save.
+    return (long) (average.orElse(0) + 0.5);  // 0 to indicate something went 
wrong
+  }
+
+  /**
+   * Handle the task and record metrics, as a real Executor#onTaskReceived 
would.
+   *
+   * @param task the task to execute.
+   */
+  public void onTaskReceived(final Task task) {
+    if (executorInitializationTime < 0) {
+      executorInitializationTime = System.currentTimeMillis();
+      currentTime.set(executorInitializationTime);
+      timeCheckpoint = executorInitializationTime;
+    }
+    final String taskId = task.getTaskId();
+    final int attemptIdx = task.getAttemptIdx();
+    String idOfVertexPutOnHold = null;
+
+    final long schedulingOverhead = System.currentTimeMillis() - 
this.timeCheckpoint;
+    this.timeCheckpoint = System.currentTimeMillis();
+    this.sendMetric(TASK_METRIC_ID, taskId, "schedulingOverhead",
+      SerializationUtils.serialize(schedulingOverhead));
+    final long executionStartTime = 
this.currentTime.getAndAdd(schedulingOverhead);
+
+    // Prepare (constructor of TaskExecutor)
+
+    // Deserialize task
+
+    // Connect incoming / outgoing edges.
+
+    // Execute
+    LOG.debug("{} started", taskId);
+
+    // Fetch external data (Read) and process them
 
 Review comment:
   ✅ 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to 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