Search in sources :

Example 11 with SchedulerStateManagerAdaptor

use of com.twitter.heron.spi.statemgr.SchedulerStateManagerAdaptor in project heron by twitter.

the class SchedulerClientFactoryTest method testGetServiceSchedulerClientFail.

@Test(expected = SchedulerException.class)
public void testGetServiceSchedulerClientFail() throws Exception {
    // Instantiate mock objects
    Config config = Mockito.mock(Config.class);
    Config runtime = Mockito.mock(Config.class);
    SchedulerStateManagerAdaptor statemgr = Mockito.mock(SchedulerStateManagerAdaptor.class);
    // Get a ServiceSchedulerClient
    Mockito.when(config.getBooleanValue(Key.SCHEDULER_IS_SERVICE)).thenReturn(true);
    // Mock the runtime object
    Mockito.when(runtime.get(Key.SCHEDULER_STATE_MANAGER_ADAPTOR)).thenReturn(statemgr);
    Mockito.when(runtime.getStringValue(Key.TOPOLOGY_NAME)).thenReturn(TOPOLOGY_NAME);
    // Failed to getSchedulerLocation
    Mockito.when(statemgr.getSchedulerLocation(Mockito.eq(TOPOLOGY_NAME))).thenReturn(null);
    try {
        new SchedulerClientFactory(config, runtime).getSchedulerClient();
    } finally {
        Mockito.verify(statemgr).getSchedulerLocation(Mockito.eq(TOPOLOGY_NAME));
    }
}
Also used : Config(com.twitter.heron.spi.common.Config) SchedulerStateManagerAdaptor(com.twitter.heron.spi.statemgr.SchedulerStateManagerAdaptor) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 12 with SchedulerStateManagerAdaptor

use of com.twitter.heron.spi.statemgr.SchedulerStateManagerAdaptor in project heron by twitter.

the class LauncherUtilsTest method constructsConfigWithTopologyInfo.

@Test
public void constructsConfigWithTopologyInfo() throws Exception {
    TopologyAPI.Topology mockTopology = PowerMockito.mock(TopologyAPI.Topology.class);
    PowerMockito.when(mockTopology.getId()).thenReturn("testTopologyId");
    PowerMockito.when(mockTopology.getName()).thenReturn("testTopologyName");
    SchedulerStateManagerAdaptor mockStMgr = Mockito.mock(SchedulerStateManagerAdaptor.class);
    PowerMockito.spy(TopologyUtils.class);
    PowerMockito.doReturn(456).when(TopologyUtils.class, "getNumContainers", mockTopology);
    Config runtime = Config.newBuilder().putAll(LauncherUtils.getInstance().createPrimaryRuntime(mockTopology)).putAll(LauncherUtils.getInstance().createAdaptorRuntime(mockStMgr)).build();
    Assert.assertEquals("testTopologyId", Runtime.topologyId(runtime));
    Assert.assertEquals("testTopologyName", Runtime.topologyName(runtime));
    Assert.assertEquals(mockTopology, Runtime.topology(runtime));
    Assert.assertEquals(mockStMgr, Runtime.schedulerStateManagerAdaptor(runtime));
    Assert.assertEquals(456 + 1, Runtime.numContainers(runtime).longValue());
}
Also used : Config(com.twitter.heron.spi.common.Config) TopologyAPI(com.twitter.heron.api.generated.TopologyAPI) SchedulerStateManagerAdaptor(com.twitter.heron.spi.statemgr.SchedulerStateManagerAdaptor) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 13 with SchedulerStateManagerAdaptor

use of com.twitter.heron.spi.statemgr.SchedulerStateManagerAdaptor in project heron by twitter.

the class AuroraSchedulerTest method testOnSchedule.

/**
   * Tests that we can schedule
   */
@Test
public void testOnSchedule() throws Exception {
    AuroraController controller = Mockito.mock(AuroraController.class);
    doReturn(controller).when(scheduler).getController();
    SchedulerStateManagerAdaptor stateManager = mock(SchedulerStateManagerAdaptor.class);
    Config runtime = Mockito.mock(Config.class);
    when(runtime.get(Key.SCHEDULER_STATE_MANAGER_ADAPTOR)).thenReturn(stateManager);
    when(runtime.getStringValue(Key.TOPOLOGY_NAME)).thenReturn(TOPOLOGY_NAME);
    Config mConfig = Mockito.mock(Config.class);
    PowerMockito.mockStatic(Config.class);
    when(Config.toClusterMode(mConfig)).thenReturn(mConfig);
    when(mConfig.getStringValue(eq(AuroraContext.JOB_TEMPLATE), anyString())).thenReturn(AURORA_PATH);
    scheduler.initialize(mConfig, runtime);
    // Fail to schedule due to null PackingPlan
    Assert.assertFalse(scheduler.onSchedule(null));
    PackingPlan plan = new PackingPlan(PACKING_PLAN_ID, new HashSet<PackingPlan.ContainerPlan>());
    assertTrue(plan.getContainers().isEmpty());
    // Fail to schedule due to PackingPlan is empty
    Assert.assertFalse(scheduler.onSchedule(plan));
    // Construct valid PackingPlan
    Set<PackingPlan.ContainerPlan> containers = new HashSet<>();
    containers.add(PackingTestUtils.testContainerPlan(CONTAINER_ID));
    PackingPlan validPlan = new PackingPlan(PACKING_PLAN_ID, containers);
    // Failed to create job via controller
    doReturn(false).when(controller).createJob(Matchers.anyMapOf(AuroraField.class, String.class));
    doReturn(true).when(stateManager).updatePackingPlan(any(PackingPlans.PackingPlan.class), eq(TOPOLOGY_NAME));
    Assert.assertFalse(scheduler.onSchedule(validPlan));
    Mockito.verify(controller).createJob(Matchers.anyMapOf(AuroraField.class, String.class));
    Mockito.verify(stateManager).updatePackingPlan(any(PackingPlans.PackingPlan.class), eq(TOPOLOGY_NAME));
    // Happy path
    doReturn(true).when(controller).createJob(Matchers.anyMapOf(AuroraField.class, String.class));
    assertTrue(scheduler.onSchedule(validPlan));
    Mockito.verify(controller, Mockito.times(2)).createJob(Matchers.anyMapOf(AuroraField.class, String.class));
    Mockito.verify(stateManager, Mockito.times(2)).updatePackingPlan(any(PackingPlans.PackingPlan.class), eq(TOPOLOGY_NAME));
}
Also used : Config(com.twitter.heron.spi.common.Config) PackingPlan(com.twitter.heron.spi.packing.PackingPlan) Matchers.anyString(org.mockito.Matchers.anyString) SchedulerStateManagerAdaptor(com.twitter.heron.spi.statemgr.SchedulerStateManagerAdaptor) HashSet(java.util.HashSet) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 14 with SchedulerStateManagerAdaptor

use of com.twitter.heron.spi.statemgr.SchedulerStateManagerAdaptor in project heron by twitter.

the class UpdateTopologyManagerTest method requestsToAddAndRemoveContainers.

/**
   * Test scalable scheduler invocation
   */
@Test
@PrepareForTest(TMasterUtils.class)
public void requestsToAddAndRemoveContainers() throws Exception {
    Lock lock = mockLock(true);
    SchedulerStateManagerAdaptor mockStateMgr = mockStateManager(testTopology, this.currentProtoPlan, lock);
    IScalable mockScheduler = mock(IScalable.class);
    UpdateTopologyManager spyUpdateManager = spyUpdateManager(mockStateMgr, mockScheduler, testTopology);
    PowerMockito.spy(TMasterUtils.class);
    PowerMockito.doNothing().when(TMasterUtils.class, "sendToTMaster", any(String.class), eq(TOPOLOGY_NAME), eq(mockStateMgr), any(NetworkUtils.TunnelConfig.class));
    spyUpdateManager.updateTopology(currentProtoPlan, proposedProtoPlan);
    verify(spyUpdateManager).deactivateTopology(eq(mockStateMgr), eq(testTopology));
    verify(spyUpdateManager).reactivateTopology(eq(mockStateMgr), eq(testTopology), eq(2));
    verify(mockScheduler).addContainers(expectedContainersToAdd);
    verify(mockScheduler).removeContainers(expectedContainersToRemove);
    verify(lock).tryLock(any(Long.class), any(TimeUnit.class));
    verify(lock).unlock();
    PowerMockito.verifyStatic(times(1));
    TMasterUtils.transitionTopologyState(eq(TOPOLOGY_NAME), eq(TMasterUtils.TMasterCommand.DEACTIVATE), eq(mockStateMgr), eq(TopologyAPI.TopologyState.RUNNING), eq(TopologyAPI.TopologyState.PAUSED), any(NetworkUtils.TunnelConfig.class));
    PowerMockito.verifyStatic(times(1));
    TMasterUtils.transitionTopologyState(eq(TOPOLOGY_NAME), eq(TMasterUtils.TMasterCommand.ACTIVATE), eq(mockStateMgr), eq(TopologyAPI.TopologyState.PAUSED), eq(TopologyAPI.TopologyState.RUNNING), any(NetworkUtils.TunnelConfig.class));
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) IScalable(com.twitter.heron.spi.scheduler.IScalable) Lock(com.twitter.heron.spi.statemgr.Lock) SchedulerStateManagerAdaptor(com.twitter.heron.spi.statemgr.SchedulerStateManagerAdaptor) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 15 with SchedulerStateManagerAdaptor

use of com.twitter.heron.spi.statemgr.SchedulerStateManagerAdaptor in project heron by twitter.

the class LaunchRunner method call.

/**
   * Call launcher to launch topology
   *
   * @throws LauncherException
   * @throws PackingException
   * @throws SubmitDryRunResponse
   */
public void call() throws LauncherException, PackingException, SubmitDryRunResponse {
    SchedulerStateManagerAdaptor statemgr = Runtime.schedulerStateManagerAdaptor(runtime);
    TopologyAPI.Topology topology = Runtime.topology(runtime);
    String topologyName = Context.topologyName(config);
    PackingPlan packedPlan = LauncherUtils.getInstance().createPackingPlan(config, runtime);
    if (Context.dryRun(config)) {
        throw new SubmitDryRunResponse(topology, config, packedPlan);
    }
    // initialize the launcher
    launcher.initialize(config, runtime);
    // Set topology def first since we determine whether a topology is running
    // by checking the existence of topology def
    // store the trimmed topology definition into the state manager
    // TODO(rli): log-and-false anti-pattern is too nested on this path. will not refactor
    Boolean result = statemgr.setTopology(trimTopology(topology), topologyName);
    if (result == null || !result) {
        throw new LauncherException(String.format("Failed to set topology definition for topology '%s'", topologyName));
    }
    result = statemgr.setPackingPlan(createPackingPlan(packedPlan), topologyName);
    if (result == null || !result) {
        statemgr.deleteTopology(topologyName);
        throw new LauncherException(String.format("Failed to set packing plan for topology '%s'", topologyName));
    }
    // store the execution state into the state manager
    ExecutionEnvironment.ExecutionState executionState = createExecutionState();
    result = statemgr.setExecutionState(executionState, topologyName);
    if (result == null || !result) {
        statemgr.deletePackingPlan(topologyName);
        statemgr.deleteTopology(topologyName);
        throw new LauncherException(String.format("Failed to set execution state for topology '%s'", topologyName));
    }
    // launch the topology, clear the state if it fails
    if (!launcher.launch(packedPlan)) {
        statemgr.deleteExecutionState(topologyName);
        statemgr.deletePackingPlan(topologyName);
        statemgr.deleteTopology(topologyName);
        throw new LauncherException(String.format("Failed to launch topology '%s'", topologyName));
    }
}
Also used : SubmitDryRunResponse(com.twitter.heron.scheduler.dryrun.SubmitDryRunResponse) LauncherException(com.twitter.heron.spi.scheduler.LauncherException) ExecutionEnvironment(com.twitter.heron.proto.system.ExecutionEnvironment) PackingPlan(com.twitter.heron.spi.packing.PackingPlan) SchedulerStateManagerAdaptor(com.twitter.heron.spi.statemgr.SchedulerStateManagerAdaptor) TopologyAPI(com.twitter.heron.api.generated.TopologyAPI)

Aggregations

SchedulerStateManagerAdaptor (com.twitter.heron.spi.statemgr.SchedulerStateManagerAdaptor)34 Test (org.junit.Test)20 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)20 Config (com.twitter.heron.spi.common.Config)13 PackingPlan (com.twitter.heron.spi.packing.PackingPlan)12 Scheduler (com.twitter.heron.proto.scheduler.Scheduler)8 ISchedulerClient (com.twitter.heron.scheduler.client.ISchedulerClient)6 ILauncher (com.twitter.heron.spi.scheduler.ILauncher)6 PackingPlans (com.twitter.heron.proto.system.PackingPlans)5 HeronTopology (com.twitter.heron.api.HeronTopology)4 TopologyAPI (com.twitter.heron.api.generated.TopologyAPI)4 IStateManager (com.twitter.heron.spi.statemgr.IStateManager)4 ExecutionEnvironment (com.twitter.heron.proto.system.ExecutionEnvironment)3 IScheduler (com.twitter.heron.spi.scheduler.IScheduler)3 HashSet (java.util.HashSet)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 RoundRobinPacking (com.twitter.heron.packing.roundrobin.RoundRobinPacking)2 LauncherUtils (com.twitter.heron.scheduler.utils.LauncherUtils)2 PackingPlanProtoDeserializer (com.twitter.heron.spi.packing.PackingPlanProtoDeserializer)2 IScalable (com.twitter.heron.spi.scheduler.IScalable)2