Search in sources :

Example 1 with DrillbitTracker

use of org.apache.drill.yarn.zk.ZKRegistry.DrillbitTracker in project drill by apache.

the class TestZkRegistry method testZKRegistry.

@Test
public void testZKRegistry() throws Exception {
    TestingServer server = new TestingServer();
    server.start();
    String connStr = server.getConnectString();
    CuratorFramework probeZk = connectToZk(connStr);
    addDrillbit(probeZk, FRED_HOST);
    ZKClusterCoordinatorDriver driver = new ZKClusterCoordinatorDriver().setConnect(connStr, ZK_ROOT, CLUSTER_ID).setPorts(TEST_USER_PORT, TEST_CONTROL_PORT, TEST_DATA_PORT).build();
    ZKRegistry registry = new ZKRegistry(driver);
    TestRegistryHandler handler = new TestRegistryHandler();
    registry.start(handler);
    // We started with one "stray" drillbit that will be reported as unmanaged.
    assertEquals(FRED_HOST, handler.reserved);
    List<String> unmanaged = registry.listUnmanagedDrillits();
    assertEquals(1, unmanaged.size());
    String fredsKey = makeKey(FRED_HOST);
    assertEquals(fredsKey, unmanaged.get(0));
    Map<String, DrillbitTracker> trackers = registry.getRegistryForTesting();
    assertEquals(1, trackers.size());
    assertTrue(trackers.containsKey(fredsKey));
    DrillbitTracker fredsTracker = trackers.get(fredsKey);
    assertEquals(fredsKey, fredsTracker.key);
    assertEquals(DrillbitTracker.State.UNMANAGED, fredsTracker.state);
    assertNull(fredsTracker.task);
    assertEquals(fredsKey, ZKClusterCoordinatorDriver.asString(fredsTracker.endpoint));
    // The handler should have been told about the initial stray.
    assertEquals(FRED_HOST, handler.reserved);
    // Pretend to start a new Drillbit.
    Task wilmasTask = new TestTask(WILMA_HOST);
    EventContext context = new EventContext(wilmasTask);
    // Registry ignores the created event.
    registry.stateChange(Event.CREATED, context);
    assertEquals(1, registry.getRegistryForTesting().size());
    // But, does care about the allocated event.
    registry.stateChange(Event.ALLOCATED, context);
    assertEquals(2, registry.getRegistryForTesting().size());
    String wilmasKey = makeKey(WILMA_HOST);
    DrillbitTracker wilmasTracker = registry.getRegistryForTesting().get(wilmasKey);
    assertNotNull(wilmasTracker);
    assertEquals(wilmasTask, wilmasTracker.task);
    assertNull(wilmasTracker.endpoint);
    assertEquals(wilmasKey, wilmasTracker.key);
    assertEquals(DrillbitTracker.State.NEW, wilmasTracker.state);
    handler.clear();
    // Time goes on. The Drillbit starts and registers itself.
    addDrillbit(probeZk, WILMA_HOST);
    Thread.sleep(100);
    assertEquals(wilmasTask, handler.start);
    assertEquals(DrillbitTracker.State.REGISTERED, wilmasTracker.state);
    assertEquals(handler.start, wilmasTask);
    // Create another task: Barney
    Task barneysTask = new TestTask(BARNEY_HOST);
    context = new EventContext(barneysTask);
    registry.stateChange(Event.CREATED, context);
    // Start Barney, but assume a latency in Yarn, but not ZK.
    // We get the ZK registration before the YARN launch confirmation.
    handler.clear();
    addDrillbit(probeZk, BARNEY_HOST);
    Thread.sleep(100);
    assertEquals(BARNEY_HOST, handler.reserved);
    String barneysKey = makeKey(BARNEY_HOST);
    DrillbitTracker barneysTracker = registry.getRegistryForTesting().get(barneysKey);
    assertNotNull(barneysTracker);
    assertEquals(DrillbitTracker.State.UNMANAGED, barneysTracker.state);
    assertNull(barneysTracker.task);
    assertEquals(2, registry.listUnmanagedDrillits().size());
    handler.clear();
    registry.stateChange(Event.ALLOCATED, context);
    assertEquals(DrillbitTracker.State.REGISTERED, barneysTracker.state);
    assertEquals(handler.start, barneysTask);
    assertEquals(barneysTask, barneysTracker.task);
    assertEquals(1, registry.listUnmanagedDrillits().size());
    // Barney is having problems, it it drops out of ZK.
    handler.clear();
    removeDrillbit(probeZk, BARNEY_HOST);
    Thread.sleep(100);
    assertEquals(barneysTask, handler.end);
    assertEquals(DrillbitTracker.State.DEREGISTERED, barneysTracker.state);
    // Barney comes alive (presumably before the controller gives up and kills
    // the Drillbit.)
    handler.clear();
    addDrillbit(probeZk, BARNEY_HOST);
    Thread.sleep(100);
    assertEquals(barneysTask, handler.start);
    assertEquals(DrillbitTracker.State.REGISTERED, barneysTracker.state);
    // Barney is killed by the controller.
    // ZK entry drops. Tracker is removed, controller is notified.
    handler.clear();
    removeDrillbit(probeZk, BARNEY_HOST);
    Thread.sleep(100);
    assertNotNull(registry.getRegistryForTesting().get(barneysKey));
    assertEquals(barneysTask, handler.end);
    // The controller tells the registry to stop tracking the Drillbit.
    handler.clear();
    registry.stateChange(Event.ENDED, context);
    assertNull(handler.end);
    assertNull(registry.getRegistryForTesting().get(barneysKey));
    // The stray drillbit deregisters from ZK. The tracker is removed.
    handler.clear();
    removeDrillbit(probeZk, FRED_HOST);
    Thread.sleep(100);
    assertNull(registry.getRegistryForTesting().get(fredsKey));
    assertNull(handler.end);
    assertEquals(FRED_HOST, handler.released);
    // Wilma is killed by the controller.
    handler.clear();
    removeDrillbit(probeZk, WILMA_HOST);
    Thread.sleep(100);
    assertEquals(wilmasTask, handler.end);
    assertNull(handler.released);
    assertEquals(DrillbitTracker.State.DEREGISTERED, wilmasTracker.state);
    assertNotNull(registry.getRegistryForTesting().get(wilmasKey));
    handler.clear();
    context = new EventContext(wilmasTask);
    registry.stateChange(Event.ENDED, context);
    assertNull(registry.getRegistryForTesting().get(wilmasKey));
    assertNull(handler.released);
    assertNull(handler.end);
    // All drillbits should be gone.
    assertTrue(registry.getRegistryForTesting().isEmpty());
    probeZk.close();
    driver.close();
    server.stop();
    server.close();
}
Also used : TestingServer(org.apache.curator.test.TestingServer) EventContext(org.apache.drill.yarn.appMaster.EventContext) DrillbitTracker(org.apache.drill.yarn.zk.ZKRegistry.DrillbitTracker) CuratorFramework(org.apache.curator.framework.CuratorFramework) Task(org.apache.drill.yarn.appMaster.Task) Test(org.junit.Test) BaseTest(org.apache.drill.test.BaseTest)

Example 2 with DrillbitTracker

use of org.apache.drill.yarn.zk.ZKRegistry.DrillbitTracker in project drill by axbaretto.

the class TestZkRegistry method testZKRegistry.

@Test
public void testZKRegistry() throws Exception {
    TestingServer server = new TestingServer();
    server.start();
    String connStr = server.getConnectString();
    CuratorFramework probeZk = connectToZk(connStr);
    addDrillbit(probeZk, FRED_HOST);
    ZKClusterCoordinatorDriver driver = new ZKClusterCoordinatorDriver().setConnect(connStr, ZK_ROOT, CLUSTER_ID).setPorts(TEST_USER_PORT, TEST_CONTROL_PORT, TEST_DATA_PORT).build();
    ZKRegistry registry = new ZKRegistry(driver);
    TestRegistryHandler handler = new TestRegistryHandler();
    registry.start(handler);
    // We started with one "stray" drillbit that will be reported as unmanaged.
    assertEquals(FRED_HOST, handler.reserved);
    List<String> unmanaged = registry.listUnmanagedDrillits();
    assertEquals(1, unmanaged.size());
    String fredsKey = makeKey(FRED_HOST);
    assertEquals(fredsKey, unmanaged.get(0));
    Map<String, DrillbitTracker> trackers = registry.getRegistryForTesting();
    assertEquals(1, trackers.size());
    assertTrue(trackers.containsKey(fredsKey));
    DrillbitTracker fredsTracker = trackers.get(fredsKey);
    assertEquals(fredsKey, fredsTracker.key);
    assertEquals(DrillbitTracker.State.UNMANAGED, fredsTracker.state);
    assertNull(fredsTracker.task);
    assertEquals(fredsKey, ZKClusterCoordinatorDriver.asString(fredsTracker.endpoint));
    // The handler should have been told about the initial stray.
    assertEquals(FRED_HOST, handler.reserved);
    // Pretend to start a new Drillbit.
    Task wilmasTask = new TestTask(WILMA_HOST);
    EventContext context = new EventContext(wilmasTask);
    // Registry ignores the created event.
    registry.stateChange(Event.CREATED, context);
    assertEquals(1, registry.getRegistryForTesting().size());
    // But, does care about the allocated event.
    registry.stateChange(Event.ALLOCATED, context);
    assertEquals(2, registry.getRegistryForTesting().size());
    String wilmasKey = makeKey(WILMA_HOST);
    DrillbitTracker wilmasTracker = registry.getRegistryForTesting().get(wilmasKey);
    assertNotNull(wilmasTracker);
    assertEquals(wilmasTask, wilmasTracker.task);
    assertNull(wilmasTracker.endpoint);
    assertEquals(wilmasKey, wilmasTracker.key);
    assertEquals(DrillbitTracker.State.NEW, wilmasTracker.state);
    handler.clear();
    // Time goes on. The Drillbit starts and registers itself.
    addDrillbit(probeZk, WILMA_HOST);
    Thread.sleep(100);
    assertEquals(wilmasTask, handler.start);
    assertEquals(DrillbitTracker.State.REGISTERED, wilmasTracker.state);
    assertEquals(handler.start, wilmasTask);
    // Create another task: Barney
    Task barneysTask = new TestTask(BARNEY_HOST);
    context = new EventContext(barneysTask);
    registry.stateChange(Event.CREATED, context);
    // Start Barney, but assume a latency in Yarn, but not ZK.
    // We get the ZK registration before the YARN launch confirmation.
    handler.clear();
    addDrillbit(probeZk, BARNEY_HOST);
    Thread.sleep(100);
    assertEquals(BARNEY_HOST, handler.reserved);
    String barneysKey = makeKey(BARNEY_HOST);
    DrillbitTracker barneysTracker = registry.getRegistryForTesting().get(barneysKey);
    assertNotNull(barneysTracker);
    assertEquals(DrillbitTracker.State.UNMANAGED, barneysTracker.state);
    assertNull(barneysTracker.task);
    assertEquals(2, registry.listUnmanagedDrillits().size());
    handler.clear();
    registry.stateChange(Event.ALLOCATED, context);
    assertEquals(DrillbitTracker.State.REGISTERED, barneysTracker.state);
    assertEquals(handler.start, barneysTask);
    assertEquals(barneysTask, barneysTracker.task);
    assertEquals(1, registry.listUnmanagedDrillits().size());
    // Barney is having problems, it it drops out of ZK.
    handler.clear();
    removeDrillbit(probeZk, BARNEY_HOST);
    Thread.sleep(100);
    assertEquals(barneysTask, handler.end);
    assertEquals(DrillbitTracker.State.DEREGISTERED, barneysTracker.state);
    // Barney comes alive (presumably before the controller gives up and kills
    // the Drillbit.)
    handler.clear();
    addDrillbit(probeZk, BARNEY_HOST);
    Thread.sleep(100);
    assertEquals(barneysTask, handler.start);
    assertEquals(DrillbitTracker.State.REGISTERED, barneysTracker.state);
    // Barney is killed by the controller.
    // ZK entry drops. Tracker is removed, controller is notified.
    handler.clear();
    removeDrillbit(probeZk, BARNEY_HOST);
    Thread.sleep(100);
    assertNotNull(registry.getRegistryForTesting().get(barneysKey));
    assertEquals(barneysTask, handler.end);
    // The controller tells the registry to stop tracking the Drillbit.
    handler.clear();
    registry.stateChange(Event.ENDED, context);
    assertNull(handler.end);
    assertNull(registry.getRegistryForTesting().get(barneysKey));
    // The stray drillbit deregisters from ZK. The tracker is removed.
    handler.clear();
    removeDrillbit(probeZk, FRED_HOST);
    Thread.sleep(100);
    assertNull(registry.getRegistryForTesting().get(fredsKey));
    assertNull(handler.end);
    assertEquals(FRED_HOST, handler.released);
    // Wilma is killed by the controller.
    handler.clear();
    removeDrillbit(probeZk, WILMA_HOST);
    Thread.sleep(100);
    assertEquals(wilmasTask, handler.end);
    assertNull(handler.released);
    assertEquals(DrillbitTracker.State.DEREGISTERED, wilmasTracker.state);
    assertNotNull(registry.getRegistryForTesting().get(wilmasKey));
    handler.clear();
    context = new EventContext(wilmasTask);
    registry.stateChange(Event.ENDED, context);
    assertNull(registry.getRegistryForTesting().get(wilmasKey));
    assertNull(handler.released);
    assertNull(handler.end);
    // All drillbits should be gone.
    assertTrue(registry.getRegistryForTesting().isEmpty());
    probeZk.close();
    driver.close();
    server.stop();
    server.close();
}
Also used : TestingServer(org.apache.curator.test.TestingServer) EventContext(org.apache.drill.yarn.appMaster.EventContext) DrillbitTracker(org.apache.drill.yarn.zk.ZKRegistry.DrillbitTracker) CuratorFramework(org.apache.curator.framework.CuratorFramework) Task(org.apache.drill.yarn.appMaster.Task) Test(org.junit.Test)

Aggregations

CuratorFramework (org.apache.curator.framework.CuratorFramework)2 TestingServer (org.apache.curator.test.TestingServer)2 EventContext (org.apache.drill.yarn.appMaster.EventContext)2 Task (org.apache.drill.yarn.appMaster.Task)2 DrillbitTracker (org.apache.drill.yarn.zk.ZKRegistry.DrillbitTracker)2 Test (org.junit.Test)2 BaseTest (org.apache.drill.test.BaseTest)1