Search in sources :

Example 1 with Lock

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));
    }
}
Also used : ConcurrentModificationException(java.util.ConcurrentModificationException) PackingPlans(org.apache.heron.proto.system.PackingPlans) SchedulerStateManagerAdaptor(org.apache.heron.spi.statemgr.SchedulerStateManagerAdaptor) Lock(org.apache.heron.spi.statemgr.Lock)

Example 2 with Lock

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);
}
Also used : Matchers.anyString(org.mockito.Matchers.anyString) Lock(org.apache.heron.spi.statemgr.Lock) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with Lock

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);
}
Also used : Matchers.anyString(org.mockito.Matchers.anyString) Lock(org.apache.heron.spi.statemgr.Lock) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 4 with Lock

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();
}
Also used : Path(java.nio.file.Path) Lock(org.apache.heron.spi.statemgr.Lock) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 5 with Lock

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));
}
Also used : PackingPlan(org.apache.heron.spi.packing.PackingPlan) Resource(org.apache.heron.spi.packing.Resource) IScalable(org.apache.heron.spi.scheduler.IScalable) Lock(org.apache.heron.spi.statemgr.Lock) SchedulerStateManagerAdaptor(org.apache.heron.spi.statemgr.SchedulerStateManagerAdaptor) TimeUnit(java.util.concurrent.TimeUnit) HashSet(java.util.HashSet) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

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