sanha commented on a change in pull request #1: [NEMO-26] Implement 
SourceLocationAwareSchedulingPolicy
URL: https://github.com/apache/incubator-nemo/pull/1#discussion_r173045410
 
 

 ##########
 File path: 
tests/src/test/java/edu/snu/nemo/tests/runtime/master/scheduler/SourceLocationAwareSchedulingPolicyTest.java
 ##########
 @@ -0,0 +1,418 @@
+/*
+ * Copyright (C) 2018 Seoul National University
+ *
+ * Licensed 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 edu.snu.nemo.tests.runtime.master.scheduler;
+
+import 
edu.snu.nemo.common.ir.vertex.executionproperty.ExecutorPlacementProperty;
+import edu.snu.nemo.runtime.common.plan.physical.ScheduledTaskGroup;
+import edu.snu.nemo.runtime.common.state.TaskGroupState;
+import edu.snu.nemo.common.ir.Readable;
+import edu.snu.nemo.runtime.master.JobStateManager;
+import edu.snu.nemo.runtime.master.scheduler.ExecutorRegistry;
+import edu.snu.nemo.runtime.master.resource.ExecutorRepresenter;
+import edu.snu.nemo.runtime.master.scheduler.RoundRobinSchedulingPolicy;
+import edu.snu.nemo.runtime.master.scheduler.SchedulingPolicy;
+import 
edu.snu.nemo.runtime.master.scheduler.SourceLocationAwareSchedulingPolicy;
+import org.apache.reef.tang.Injector;
+import org.apache.reef.tang.Tang;
+import org.apache.reef.tang.exceptions.InjectionException;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+
+import java.util.*;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.stream.Collectors;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.*;
+
+/**
+ * Test cases for
+ */
+@RunWith(PowerMockRunner.class)
+@PrepareForTest({JobStateManager.class, ExecutorRepresenter.class, 
RoundRobinSchedulingPolicy.class,
+    ScheduledTaskGroup.class, Readable.class})
+public final class SourceLocationAwareSchedulingPolicyTest {
+  private static final String SITE_0 = "SEOUL";
+  private static final String SITE_1 = "JINJU";
+  private static final String SITE_2 = "BUSAN";
+  private static final int SCHEDULER_TIMEOUT_MS = 500;
+
+  private SourceLocationAwareSchedulingPolicy sourceLocationAware;
+  private SpiedSchedulingPolicyWrapper<RoundRobinSchedulingPolicy> roundRobin;
+  private MockJobStateManagerWrapper jobStateManager;
+
+  @Before
+  public void setup() throws InjectionException {
+    final Injector injector = Tang.Factory.getTang().newInjector();
+    jobStateManager = new MockJobStateManagerWrapper();
+
+    final ExecutorRegistry executorRegistry = new ExecutorRegistry();
+    final RoundRobinSchedulingPolicy roundRobinSchedulingPolicy =
+        new RoundRobinSchedulingPolicy(executorRegistry, SCHEDULER_TIMEOUT_MS);
+    roundRobin = new SpiedSchedulingPolicyWrapper(roundRobinSchedulingPolicy, 
jobStateManager.get());
+
+    injector.bindVolatileInstance(RoundRobinSchedulingPolicy.class, 
roundRobin.get());
+    injector.bindVolatileInstance(JobStateManager.class, 
jobStateManager.get());
+    injector.bindVolatileInstance(ExecutorRegistry.class, executorRegistry);
+    sourceLocationAware = 
injector.getInstance(SourceLocationAwareSchedulingPolicy.class);
+  }
+
+  @After
+  public void teardown() {
+    // All expectations should be resolved at this time.
+    roundRobin.ensureNoUnresolvedExpectation();
+  }
+
+  /**
+   * {@link SourceLocationAwareSchedulingPolicy} should delegate scheduling 
decision when the
+   * {@link ScheduledTaskGroup} does not have any source tasks.
+   */
+  @Test
+  public void testRoundRobinSchedulerFallback() {
+    // Prepare test scenario
+    final ScheduledTaskGroup tg0 = 
CreateScheduledTaskGroup.withoutReadables(ExecutorPlacementProperty.NONE);
+    final ScheduledTaskGroup tg1 = 
CreateScheduledTaskGroup.withReadablesWithoutSourceLocations(2,
+        ExecutorPlacementProperty.NONE);
+    final ScheduledTaskGroup tg2 = 
CreateScheduledTaskGroup.withReadablesWhichThrowException(5,
+        ExecutorPlacementProperty.NONE);
+    addExecutor(new MockExecutorRepresenterWrapper(SITE_0, 
ExecutorPlacementProperty.NONE, 1));
+    addExecutor(new MockExecutorRepresenterWrapper(SITE_1, 
ExecutorPlacementProperty.NONE, 1));
+
+    // Trying to schedule tg0: expected to fall back to 
RoundRobinSchedulingPolicy
+    roundRobin.expectSchedulingRequest(tg0);
+    // ...and scheduling attempt must success
+    assertTrue(sourceLocationAware.scheduleTaskGroup(tg0, 
jobStateManager.get()));
+    // ...thus the TaskGroup should be running
+    jobStateManager.assertTaskGroupState(tg0.getTaskGroupId(), 
TaskGroupState.State.EXECUTING);
+
+    // Trying to schedule tg1: expected to fall back to 
RoundRobinSchedulingPolicy
+    roundRobin.expectSchedulingRequest(tg1);
+    // ...and scheduling attempt must success
+    assertTrue(sourceLocationAware.scheduleTaskGroup(tg1, 
jobStateManager.get()));
+    // ...thus the TaskGroup should be running
+    jobStateManager.assertTaskGroupState(tg1.getTaskGroupId(), 
TaskGroupState.State.EXECUTING);
+
+    // Trying to schedule tg2: expected to fall back to 
RoundRobinSchedulingPolicy
+    roundRobin.expectSchedulingRequest(tg2);
+    // ...and scheduling attempt must success
+    assertTrue(sourceLocationAware.scheduleTaskGroup(tg2, 
jobStateManager.get()));
+    // ...thus the TaskGroup should be running
+    jobStateManager.assertTaskGroupState(tg2.getTaskGroupId(), 
TaskGroupState.State.EXECUTING);
+  }
+
+  /**
+   * {@link SourceLocationAwareSchedulingPolicy} should fail to schedule a 
{@link ScheduledTaskGroup} when
+   */
+  @Test
+  public void testSourceLocationAwareSchedulingNotAvailable() {
+    // Prepare test scenario
+    final ScheduledTaskGroup tg = 
CreateScheduledTaskGroup.withReadablesWithSourceLocations(
+        Collections.singletonList(Collections.singletonList(SITE_0)), 
ExecutorPlacementProperty.NONE);
+    final MockExecutorRepresenterWrapper e0 = addExecutor(
+        new MockExecutorRepresenterWrapper(SITE_1, 
ExecutorPlacementProperty.NONE, 1));
+    final MockExecutorRepresenterWrapper e1 = addExecutor(
+        new MockExecutorRepresenterWrapper(SITE_1, 
ExecutorPlacementProperty.NONE, 1));
+
+    // Attempt to schedule tg must fail (fallback to round robin policy is not 
expected)
+    assertFalse(sourceLocationAware.scheduleTaskGroup(tg, 
jobStateManager.get()));
+    // Thus executors should have no running TaskGroups at all
+    e0.assertScheduledTaskGroups(Collections.emptyList());
+    e1.assertScheduledTaskGroups(Collections.emptyList());
+  }
+
+  private static final String CONTAINER_TYPE_A = "A";
+
+  /**
+   * {@link SourceLocationAwareSchedulingPolicy} should schedule TG to one of 
the executors with appropriate
+   * location and container type.
+   */
+  @Test
+  public void testSourceLocationAwareSchedulingWithContainerType() {
+    // Prepare test scenario
+    final ScheduledTaskGroup tg = 
CreateScheduledTaskGroup.withReadablesWithSourceLocations(
+        Collections.singletonList(Collections.singletonList(SITE_0)), 
CONTAINER_TYPE_A);
+    final MockExecutorRepresenterWrapper e0 = addExecutor(
+        new MockExecutorRepresenterWrapper(SITE_0, 
ExecutorPlacementProperty.NONE, 1));
+    final MockExecutorRepresenterWrapper e1 = addExecutor(
+        new MockExecutorRepresenterWrapper(SITE_1, 
ExecutorPlacementProperty.NONE, 1));
+    final MockExecutorRepresenterWrapper e2 = addExecutor(
+        new MockExecutorRepresenterWrapper(SITE_2, 
ExecutorPlacementProperty.NONE, 1));
+    final MockExecutorRepresenterWrapper e3 = addExecutor(
+        new MockExecutorRepresenterWrapper(SITE_0, CONTAINER_TYPE_A, 1));
+    final MockExecutorRepresenterWrapper e4 = addExecutor(
+        new MockExecutorRepresenterWrapper(SITE_1, CONTAINER_TYPE_A, 1));
+    final MockExecutorRepresenterWrapper e5 = addExecutor(
+        new MockExecutorRepresenterWrapper(SITE_2, CONTAINER_TYPE_A, 1));
+
+    // Attempt to schedule tg must success (fallback to round robin is not 
expected)
+    assertTrue(sourceLocationAware.scheduleTaskGroup(tg, 
jobStateManager.get()));
+    // tg must run on e3
+    e0.assertScheduledTaskGroups(Collections.emptyList());
+    e1.assertScheduledTaskGroups(Collections.emptyList());
+    e2.assertScheduledTaskGroups(Collections.emptyList());
+    e3.assertScheduledTaskGroups(Collections.singletonList(tg));
+    e4.assertScheduledTaskGroups(Collections.emptyList());
+    e5.assertScheduledTaskGroups(Collections.emptyList());
+  }
+
+  /**
+   * {@link SourceLocationAwareSchedulingPolicy} should not schedule more TGs 
than executor capacity allows.
+   */
+  @Test
+  public void testSourceLocationAwareSchedulingDoesNotOverSchedule() {
+    // Prepare test scenario
+    final ScheduledTaskGroup tg0 = 
CreateScheduledTaskGroup.withReadablesWithSourceLocations(
+        Collections.singletonList(Collections.singletonList(SITE_0)), 
CONTAINER_TYPE_A);
+    final ScheduledTaskGroup tg1 = 
CreateScheduledTaskGroup.withReadablesWithSourceLocations(
+        Collections.singletonList(Collections.singletonList(SITE_0)), 
CONTAINER_TYPE_A);
+    final ScheduledTaskGroup tg2 = 
CreateScheduledTaskGroup.withReadablesWithSourceLocations(
+        Collections.singletonList(Collections.singletonList(SITE_0)), 
CONTAINER_TYPE_A);
+    final ScheduledTaskGroup tg3 = 
CreateScheduledTaskGroup.withReadablesWithSourceLocations(
+        Collections.singletonList(Collections.singletonList(SITE_0)), 
CONTAINER_TYPE_A);
+    final MockExecutorRepresenterWrapper e = addExecutor(
+        new MockExecutorRepresenterWrapper(SITE_0, CONTAINER_TYPE_A, 3));
+
+    // Attempt to schedule TG must success (fallback to round robin is not 
expected)
+    assertTrue(sourceLocationAware.scheduleTaskGroup(tg0, 
jobStateManager.get()));
+    assertTrue(sourceLocationAware.scheduleTaskGroup(tg1, 
jobStateManager.get()));
+    assertTrue(sourceLocationAware.scheduleTaskGroup(tg2, 
jobStateManager.get()));
+
+    // This must fail
+    assertFalse(sourceLocationAware.scheduleTaskGroup(tg3, 
jobStateManager.get()));
+
+    // Expected executor status
+    e.assertScheduledTaskGroups(Arrays.asList(tg0, tg1, tg2));
+  }
+
+  /**
+   * {@link SourceLocationAwareSchedulingPolicy} should properly schedule TGs 
with multiple source locations.
+   */
+  @Test
+  public void testSourceLocationAwareSchedulingWithMultiSource() {
+    // Prepare test scenario
+    final ScheduledTaskGroup tg0 = 
CreateScheduledTaskGroup.withReadablesWithSourceLocations(
+        Collections.singletonList(Collections.singletonList(SITE_1)), 
CONTAINER_TYPE_A);
+    final ScheduledTaskGroup tg1 = 
CreateScheduledTaskGroup.withReadablesWithSourceLocations(
+        Collections.singletonList(Arrays.asList(SITE_0, SITE_1, SITE_2)), 
CONTAINER_TYPE_A);
+    final ScheduledTaskGroup tg2 = 
CreateScheduledTaskGroup.withReadablesWithSourceLocations(
+        Arrays.asList(Collections.singletonList(SITE_0), 
Collections.singletonList(SITE_1),
+            Arrays.asList(SITE_1, SITE_2)), CONTAINER_TYPE_A);
+    final ScheduledTaskGroup tg3 = 
CreateScheduledTaskGroup.withReadablesWithSourceLocations(
+        Arrays.asList(Collections.singletonList(SITE_1), 
Collections.singletonList(SITE_0),
+            Arrays.asList(SITE_0, SITE_2)), CONTAINER_TYPE_A);
+    final MockExecutorRepresenterWrapper e = addExecutor(
+        new MockExecutorRepresenterWrapper(SITE_1, CONTAINER_TYPE_A, 4));
+
+    // Attempt to schedule TG must success (fallback to round robin is not 
expected)
+    assertTrue(sourceLocationAware.scheduleTaskGroup(tg0, 
jobStateManager.get()));
+    assertTrue(sourceLocationAware.scheduleTaskGroup(tg1, 
jobStateManager.get()));
+    assertTrue(sourceLocationAware.scheduleTaskGroup(tg2, 
jobStateManager.get()));
+    assertTrue(sourceLocationAware.scheduleTaskGroup(tg3, 
jobStateManager.get()));
+
+    // Expected executor status
+    e.assertScheduledTaskGroups(Arrays.asList(tg0, tg1, tg2, tg3));
+  }
+
+  private MockExecutorRepresenterWrapper addExecutor(final 
MockExecutorRepresenterWrapper executor) {
+    sourceLocationAware.onExecutorAdded(executor.get());
+    return executor;
+  }
+
 
 Review comment:
   It would be good to test whether the policy wakes up well when a container 
becomes available.
   (Same for RR policy.)

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to