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));
}
}
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));
}
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;
}
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);
}
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();
}
Aggregations