use of io.pravega.segmentstore.server.ContainerHandle in project pravega by pravega.
the class ZKSegmentContainerMonitorTest method testClose.
@Test
public void testClose() throws Exception {
@Cleanup CuratorFramework zkClient = startClient();
initializeHostContainerMapping(zkClient);
SegmentContainerRegistry containerRegistry = mock(SegmentContainerRegistry.class);
ContainerHandle containerHandle1 = mock(ContainerHandle.class);
when(containerHandle1.getContainerId()).thenReturn(1);
when(containerRegistry.startContainer(eq(1), any())).thenReturn(CompletableFuture.completedFuture(containerHandle1));
when(containerRegistry.stopContainer(any(), any())).thenReturn(CompletableFuture.completedFuture(null));
ZKSegmentContainerMonitor segMonitor = createContainerMonitor(containerRegistry, zkClient);
segMonitor.initialize(Duration.ofSeconds(1));
segMonitor.close();
assertEquals(0, segMonitor.getRegisteredContainers().size());
}
use of io.pravega.segmentstore.server.ContainerHandle in project pravega by pravega.
the class ZKSegmentContainerMonitorTest method createMockContainerRegistry.
private SegmentContainerRegistry createMockContainerRegistry() {
SegmentContainerRegistry containerRegistry = mock(SegmentContainerRegistry.class);
ContainerHandle containerHandle1 = mock(ContainerHandle.class);
when(containerHandle1.getContainerId()).thenReturn(1);
when(containerRegistry.startContainer(anyInt(), any())).thenReturn(CompletableFuture.completedFuture(containerHandle1));
when(containerRegistry.stopContainer(any(), any())).thenReturn(CompletableFuture.completedFuture(null));
return containerRegistry;
}
use of io.pravega.segmentstore.server.ContainerHandle in project pravega by pravega.
the class ZKSegmentContainerMonitorTest method testStartAndStopContainer.
@Test
public void testStartAndStopContainer() throws Exception {
@Cleanup CuratorFramework zkClient = startClient();
initializeHostContainerMapping(zkClient);
SegmentContainerRegistry containerRegistry = createMockContainerRegistry();
@Cleanup ZKSegmentContainerMonitor segMonitor = createContainerMonitor(containerRegistry, zkClient);
segMonitor.initialize(Duration.ofSeconds(1));
// Simulate a container that starts successfully.
CompletableFuture<ContainerHandle> startupFuture = new CompletableFuture<>();
ContainerHandle containerHandle = mock(ContainerHandle.class);
when(containerHandle.getContainerId()).thenReturn(2);
when(containerRegistry.startContainer(eq(2), any())).thenReturn(startupFuture);
// Now modify the ZK entry.
HashMap<Host, Set<Integer>> currentData = deserialize(zkClient, PATH);
currentData.put(PRAVEGA_SERVICE_ENDPOINT, Collections.singleton(2));
zkClient.setData().forPath(PATH, SerializationUtils.serialize(currentData));
// Container finished starting.
startupFuture.complete(containerHandle);
verify(containerRegistry, timeout(1000).atLeastOnce()).startContainer(eq(2), any());
Thread.sleep(2000);
assertEquals(1, segMonitor.getRegisteredContainers().size());
assertTrue(segMonitor.getRegisteredContainers().contains(2));
// Now modify the ZK entry. Remove container 2 and add 1.
HashMap<Host, Set<Integer>> newMapping = new HashMap<>();
newMapping.put(PRAVEGA_SERVICE_ENDPOINT, Collections.singleton(1));
zkClient.setData().forPath(PATH, SerializationUtils.serialize(newMapping));
// Verify that stop is called and only the newly added container is in running state.
when(containerRegistry.stopContainer(any(), any())).thenReturn(CompletableFuture.completedFuture(null));
verify(containerRegistry, timeout(1000).atLeastOnce()).stopContainer(any(), any());
// Using wait here to ensure the private data structure is updated.
// TODO: Removing dependency on sleep here and other places in this class
// - https://github.com/pravega/pravega/issues/1079
Thread.sleep(2000);
assertEquals(1, segMonitor.getRegisteredContainers().size());
assertTrue(segMonitor.getRegisteredContainers().contains(1));
}
use of io.pravega.segmentstore.server.ContainerHandle in project pravega by pravega.
the class ZKSegmentContainerMonitorTest method testRetryOnStartFailures.
@Test
public void testRetryOnStartFailures() throws Exception {
@Cleanup CuratorFramework zkClient = startClient();
initializeHostContainerMapping(zkClient);
SegmentContainerRegistry containerRegistry = createMockContainerRegistry();
@Cleanup ZKSegmentContainerMonitor segMonitor = createContainerMonitor(containerRegistry, zkClient);
segMonitor.initialize(Duration.ofSeconds(1));
// Simulate a container that fails to start.
CompletableFuture<ContainerHandle> failedFuture = Futures.failedFuture(new RuntimeException());
when(containerRegistry.startContainer(eq(2), any())).thenReturn(failedFuture);
// Use ZK to send that information to the Container Manager.
HashMap<Host, Set<Integer>> currentData = deserialize(zkClient, PATH);
currentData.put(PRAVEGA_SERVICE_ENDPOINT, Collections.singleton(2));
zkClient.setData().forPath(PATH, SerializationUtils.serialize(currentData));
// Verify that it does not start.
verify(containerRegistry, timeout(1000).atLeastOnce()).startContainer(eq(2), any());
assertEquals(0, segMonitor.getRegisteredContainers().size());
// Now simulate success for the same container.
ContainerHandle containerHandle = mock(ContainerHandle.class);
when(containerHandle.getContainerId()).thenReturn(2);
when(containerRegistry.startContainer(eq(2), any())).thenReturn(CompletableFuture.completedFuture(containerHandle));
// Verify that it retries and starts the same container again.
verify(containerRegistry, timeout(1000).atLeastOnce()).startContainer(eq(2), any());
Thread.sleep(2000);
assertEquals(1, segMonitor.getRegisteredContainers().size());
}
use of io.pravega.segmentstore.server.ContainerHandle in project pravega by pravega.
the class StreamSegmentContainerRegistryTests method testGetContainer.
/**
* Tests the getContainer method for registered and unregistered containers.
*/
@Test
public void testGetContainer() throws Exception {
final int containerCount = 1000;
TestContainerFactory factory = new TestContainerFactory();
@Cleanup StreamSegmentContainerRegistry registry = new StreamSegmentContainerRegistry(factory, executorService());
HashSet<Integer> expectedContainerIds = new HashSet<>();
List<CompletableFuture<ContainerHandle>> handleFutures = new ArrayList<>();
for (int containerId = 0; containerId < containerCount; containerId++) {
handleFutures.add(registry.startContainer(containerId, TIMEOUT));
expectedContainerIds.add(containerId);
}
List<ContainerHandle> handles = Futures.allOfWithResults(handleFutures).join();
HashSet<Integer> actualHandleIds = new HashSet<>();
for (ContainerHandle handle : handles) {
actualHandleIds.add(handle.getContainerId());
SegmentContainer container = registry.getContainer(handle.getContainerId());
Assert.assertTrue("Wrong container Java type.", container instanceof TestContainer);
Assert.assertEquals("Unexpected container Id.", handle.getContainerId(), container.getId());
container.close();
}
AssertExtensions.assertContainsSameElements("Unexpected container ids registered.", expectedContainerIds, actualHandleIds);
AssertExtensions.assertThrows("getContainer did not throw when passed an invalid container id.", () -> registry.getContainer(containerCount + 1), ex -> ex instanceof ContainerNotFoundException);
}
Aggregations