Search in sources :

Example 1 with Lock

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

the class UpdateTopologyManager method updateTopology.

/**
   * Scales the topology out or in based on the proposedPackingPlan
   *
   * @param existingProtoPackingPlan the current plan. If this isn't what's found in the state
   * manager, the update will fail
   * @param proposedProtoPackingPlan packing plan to change the topology to
   */
public void updateTopology(final PackingPlans.PackingPlan existingProtoPackingPlan, final PackingPlans.PackingPlan proposedProtoPackingPlan) throws ExecutionException, InterruptedException, ConcurrentModificationException {
    String topologyName = Runtime.topologyName(runtime);
    SchedulerStateManagerAdaptor stateManager = Runtime.schedulerStateManagerAdaptor(runtime);
    Lock lock = stateManager.getLock(topologyName, IStateManager.LockName.UPDATE_TOPOLOGY);
    if (lock.tryLock(5, TimeUnit.SECONDS)) {
        try {
            PackingPlans.PackingPlan foundPackingPlan = getPackingPlan(stateManager, topologyName);
            if (!deserializer.fromProto(existingProtoPackingPlan).equals(deserializer.fromProto(foundPackingPlan))) {
                throw new ConcurrentModificationException(String.format("The packing plan in state manager is not the same as the submitted existing " + "packing plan for topology %s. Another actor has changed it and has likely" + "performed an update on it. Failing this request, try again once other " + "update is complete", topologyName));
            }
            updateTopology(existingProtoPackingPlan, proposedProtoPackingPlan, stateManager);
        } finally {
            lock.unlock();
        }
    } else {
        throw new ConcurrentModificationException(String.format("The update lock can not be obtained for topology %s. Another actor is performing an " + "update on it. Failing this request, try again once current update is complete", topologyName));
    }
}
Also used : ConcurrentModificationException(java.util.ConcurrentModificationException) PackingPlans(com.twitter.heron.proto.system.PackingPlans) SchedulerStateManagerAdaptor(com.twitter.heron.spi.statemgr.SchedulerStateManagerAdaptor) Lock(com.twitter.heron.spi.statemgr.Lock)

Example 2 with Lock

use of com.twitter.heron.spi.statemgr.Lock 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 3 with Lock

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

the class UpdateTopologyManagerTest method mockLock.

private static Lock mockLock(boolean available) throws InterruptedException {
    Lock lock = mock(Lock.class);
    when(lock.tryLock(any(Long.class), any(TimeUnit.class))).thenReturn(available);
    return lock;
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) Lock(com.twitter.heron.spi.statemgr.Lock)

Example 4 with Lock

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

the class LocalFileSystemStateManagerTest method testLockTaken.

@Test
public void testLockTaken() throws Exception {
    String expectedLockPath = String.format("//locks/%s__%s", TOPOLOGY_NAME, LOCK_NAME.getName());
    byte[] expectedContents = Thread.currentThread().getName().getBytes(Charset.defaultCharset());
    PowerMockito.spy(FileUtils.class);
    PowerMockito.doReturn(false).when(FileUtils.class, "writeToFile", anyString(), any(byte[].class), anyBoolean());
    Lock lock = manager.getLock(TOPOLOGY_NAME, LOCK_NAME);
    assertFalse(lock.tryLock(0, TimeUnit.MILLISECONDS));
    assertWriteToFile(expectedLockPath, expectedContents, false);
}
Also used : Matchers.anyString(org.mockito.Matchers.anyString) Lock(com.twitter.heron.spi.statemgr.Lock) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 5 with Lock

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

the class LocalFileSystemStateManagerTest method testGetFilesystemLock.

@Test
public void testGetFilesystemLock() throws Exception {
    Path tempDir = Files.createTempDirectory("heron-testGetFilesystemLock");
    LocalFileSystemStateManager fsBackedManager = initMockManager(tempDir.toString(), true);
    Lock lock = fsBackedManager.getLock(TOPOLOGY_NAME, LOCK_NAME);
    assertTrue("Failed to get lock", lock.tryLock(0, TimeUnit.MILLISECONDS));
    lock.unlock();
}
Also used : Path(java.nio.file.Path) Lock(com.twitter.heron.spi.statemgr.Lock) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

Lock (com.twitter.heron.spi.statemgr.Lock)12 Test (org.junit.Test)8 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)8 SchedulerStateManagerAdaptor (com.twitter.heron.spi.statemgr.SchedulerStateManagerAdaptor)4 TimeUnit (java.util.concurrent.TimeUnit)4 Matchers.anyString (org.mockito.Matchers.anyString)4 PackingPlans (com.twitter.heron.proto.system.PackingPlans)2 IScalable (com.twitter.heron.spi.scheduler.IScalable)2 Path (java.nio.file.Path)2 ConcurrentModificationException (java.util.ConcurrentModificationException)2 PackingPlan (com.twitter.heron.spi.packing.PackingPlan)1 Resource (com.twitter.heron.spi.packing.Resource)1 HashSet (java.util.HashSet)1