use of org.apache.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 org.apache.heron.spi.statemgr.Lock in project heron by twitter.
the class LocalFileSystemStateManagerTest method testGetLock.
@Test
public void testGetLock() throws Exception {
initMocks();
String expectedLockPath = String.format("//locks/%s__%s", TOPOLOGY_NAME, LOCK_NAME.getName());
byte[] expectedContents = Thread.currentThread().getName().getBytes(Charset.defaultCharset());
Lock lock = manager.getLock(TOPOLOGY_NAME, LOCK_NAME);
assertTrue(lock.tryLock(0, TimeUnit.MILLISECONDS));
assertWriteToFile(expectedLockPath, expectedContents, false);
lock.unlock();
assertDeleteFile(expectedLockPath);
}
use of org.apache.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 org.apache.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 = initSpyManager(tempDir.toString(), true);
Lock lock = fsBackedManager.getLock(TOPOLOGY_NAME, LOCK_NAME);
assertTrue("Failed to get lock", lock.tryLock(0, TimeUnit.MILLISECONDS));
lock.unlock();
}
use of org.apache.heron.spi.statemgr.Lock in project heron by twitter.
the class UpdateTopologyManagerTest method requestsToAddAndRemoveContainers.
/**
* Test scalable scheduler invocation
*/
@Test
@PrepareForTest(TManagerUtils.class)
public void requestsToAddAndRemoveContainers() throws Exception {
Lock lock = mockLock(true);
SchedulerStateManagerAdaptor mockStateMgr = mockStateManager(testTopology, this.currentProtoPlan, lock);
IScalable mockScheduler = mock(IScalable.class);
HashSet<PackingPlan.ContainerPlan> mockRetrunSet = new HashSet<>();
mockRetrunSet.add(new PackingPlan.ContainerPlan(0, new HashSet<>(), new Resource(5, ByteAmount.ZERO, ByteAmount.ZERO)));
mockRetrunSet.add(new PackingPlan.ContainerPlan(1, new HashSet<>(), new Resource(6, ByteAmount.ZERO, ByteAmount.ZERO)));
when(mockScheduler.addContainers(any())).thenReturn(mockRetrunSet);
UpdateTopologyManager spyUpdateManager = spyUpdateManager(mockStateMgr, mockScheduler, testTopology);
PowerMockito.spy(TManagerUtils.class);
PowerMockito.doNothing().when(TManagerUtils.class, "sendToTManager", any(String.class), eq(TOPOLOGY_NAME), eq(mockStateMgr), any(NetworkUtils.TunnelConfig.class));
// reactivation won't happen since topology state is still running due to mock state manager
PowerMockito.doNothing().when(TManagerUtils.class, "transitionTopologyState", eq(TOPOLOGY_NAME), eq(TManagerUtils.TManagerCommand.ACTIVATE), eq(mockStateMgr), eq(TopologyAPI.TopologyState.PAUSED), eq(TopologyAPI.TopologyState.RUNNING), any(NetworkUtils.TunnelConfig.class));
spyUpdateManager.updateTopology(currentProtoPlan, proposedProtoPlan);
verify(spyUpdateManager).deactivateTopology(eq(mockStateMgr), eq(testTopology), eq(proposedPacking));
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));
TManagerUtils.transitionTopologyState(eq(TOPOLOGY_NAME), eq(TManagerUtils.TManagerCommand.DEACTIVATE), eq(mockStateMgr), eq(TopologyAPI.TopologyState.RUNNING), eq(TopologyAPI.TopologyState.PAUSED), any(NetworkUtils.TunnelConfig.class));
PowerMockito.verifyStatic(times(1));
TManagerUtils.transitionTopologyState(eq(TOPOLOGY_NAME), eq(TManagerUtils.TManagerCommand.ACTIVATE), eq(mockStateMgr), eq(TopologyAPI.TopologyState.PAUSED), eq(TopologyAPI.TopologyState.RUNNING), any(NetworkUtils.TunnelConfig.class));
}
Aggregations