Search in sources :

Example 1 with ZKException

use of org.apache.bookkeeper.client.BKException.ZKException in project bookkeeper by apache.

the class ZKRegistrationClient method getChildren.

private CompletableFuture<Versioned<Set<BookieSocketAddress>>> getChildren(String regPath, Watcher watcher) {
    CompletableFuture<Versioned<Set<BookieSocketAddress>>> future = FutureUtils.createFuture();
    zk.getChildren(regPath, watcher, (rc, path, ctx, children, stat) -> {
        if (Code.OK != rc) {
            ZKException zke = new ZKException();
            zke.fillInStackTrace();
            future.completeExceptionally(zke);
            return;
        }
        Version version = new LongVersion(stat.getCversion());
        Set<BookieSocketAddress> bookies = convertToBookieAddresses(children);
        future.complete(new Versioned<>(bookies, version));
    }, null);
    return future;
}
Also used : ZKException(org.apache.bookkeeper.client.BKException.ZKException) Versioned(org.apache.bookkeeper.versioning.Versioned) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) LongVersion(org.apache.bookkeeper.versioning.LongVersion) Version(org.apache.bookkeeper.versioning.Version) LongVersion(org.apache.bookkeeper.versioning.LongVersion)

Example 2 with ZKException

use of org.apache.bookkeeper.client.BKException.ZKException in project bookkeeper by apache.

the class TestZkRegistrationClient method testWatchBookiesFailure.

private void testWatchBookiesFailure(boolean isWritable) throws Exception {
    int zkCallbackDelayMs = 100;
    mockGetChildren(isWritable ? regPath : regReadonlyPath, true, Code.NONODE.intValue(), null, null, zkCallbackDelayMs);
    CompletableFuture<Versioned<Set<BookieSocketAddress>>> listenerResult = new CompletableFuture<>();
    RegistrationListener listener = bookies -> listenerResult.complete(bookies);
    CompletableFuture<Void> watchFuture;
    WatchTask watchTask;
    if (isWritable) {
        watchFuture = zkRegistrationClient.watchWritableBookies(listener);
        watchTask = zkRegistrationClient.getWatchWritableBookiesTask();
    } else {
        watchFuture = zkRegistrationClient.watchReadOnlyBookies(listener);
        watchTask = zkRegistrationClient.getWatchReadOnlyBookiesTask();
    }
    assertNotNull(watchTask);
    assertEquals(1, watchTask.getNumListeners());
    // trigger zkCallbackExecutor to execute getChildren callback
    zkCallbackController.advance(Duration.ofMillis(zkCallbackDelayMs));
    try {
        result(watchFuture);
        fail("Should fail to watch writable bookies if reg path doesn't exist");
    } catch (ZKException zke) {
    // expected
    }
    assertEquals(0, watchTask.getNumListeners());
    assertTrue(watchTask.isClosed());
    if (isWritable) {
        assertNull(zkRegistrationClient.getWatchWritableBookiesTask());
    } else {
        assertNull(zkRegistrationClient.getWatchReadOnlyBookiesTask());
    }
}
Also used : WatchTask(org.apache.bookkeeper.discover.ZKRegistrationClient.WatchTask) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) ZKException(org.apache.bookkeeper.client.BKException.ZKException) VoidCallback(org.apache.zookeeper.AsyncCallback.VoidCallback) Stat(org.apache.zookeeper.data.Stat) RegistrationListener(org.apache.bookkeeper.discover.RegistrationClient.RegistrationListener) READONLY(org.apache.bookkeeper.util.BookKeeperConstants.READONLY) Duration(java.time.Duration) After(org.junit.After) KeeperState(org.apache.zookeeper.Watcher.Event.KeeperState) Assert.fail(org.junit.Assert.fail) Children2Callback(org.apache.zookeeper.AsyncCallback.Children2Callback) MockZooKeeperTestCase(org.apache.bookkeeper.zookeeper.MockZooKeeperTestCase) AVAILABLE_NODE(org.apache.bookkeeper.util.BookKeeperConstants.AVAILABLE_NODE) Set(java.util.Set) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) ZkUtils(org.apache.bookkeeper.util.ZkUtils) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) MockExecutorController(org.apache.bookkeeper.common.testing.executors.MockExecutorController) Assert.assertFalse(org.junit.Assert.assertFalse) MoreAsserts.assertSetEquals(org.apache.bookkeeper.common.testing.MoreAsserts.assertSetEquals) Mockito.mock(org.mockito.Mockito.mock) Code(org.apache.zookeeper.KeeperException.Code) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) LongVersion(org.apache.bookkeeper.versioning.LongVersion) RunWith(org.junit.runner.RunWith) CompletableFuture(java.util.concurrent.CompletableFuture) Mockito.spy(org.mockito.Mockito.spy) Assert.assertSame(org.junit.Assert.assertSame) HashSet(java.util.HashSet) Lists(com.google.common.collect.Lists) TestName(org.junit.rules.TestName) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) WatcherType(org.apache.zookeeper.Watcher.WatcherType) Versioned(org.apache.bookkeeper.versioning.Versioned) PowerMockRunner(org.powermock.modules.junit4.PowerMockRunner) Before(org.junit.Before) ZK_CONNECT_BACKOFF_MS(org.apache.bookkeeper.discover.ZKRegistrationClient.ZK_CONNECT_BACKOFF_MS) FutureUtils.collect(org.apache.bookkeeper.common.concurrent.FutureUtils.collect) Assert.assertNotNull(org.junit.Assert.assertNotNull) Watcher(org.apache.zookeeper.Watcher) Assert.assertTrue(org.junit.Assert.assertTrue) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) Assert.assertNull(org.junit.Assert.assertNull) Rule(org.junit.Rule) FutureUtils.result(org.apache.bookkeeper.common.concurrent.FutureUtils.result) EventType(org.apache.zookeeper.Watcher.Event.EventType) Assert.assertEquals(org.junit.Assert.assertEquals) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ArgumentMatchers.same(org.mockito.ArgumentMatchers.same) RegistrationListener(org.apache.bookkeeper.discover.RegistrationClient.RegistrationListener) CompletableFuture(java.util.concurrent.CompletableFuture) ZKException(org.apache.bookkeeper.client.BKException.ZKException) WatchTask(org.apache.bookkeeper.discover.ZKRegistrationClient.WatchTask) Versioned(org.apache.bookkeeper.versioning.Versioned) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress)

Aggregations

ZKException (org.apache.bookkeeper.client.BKException.ZKException)2 BookieSocketAddress (org.apache.bookkeeper.net.BookieSocketAddress)2 LongVersion (org.apache.bookkeeper.versioning.LongVersion)2 Versioned (org.apache.bookkeeper.versioning.Versioned)2 Lists (com.google.common.collect.Lists)1 Duration (java.time.Duration)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 Slf4j (lombok.extern.slf4j.Slf4j)1 FutureUtils.collect (org.apache.bookkeeper.common.concurrent.FutureUtils.collect)1 FutureUtils.result (org.apache.bookkeeper.common.concurrent.FutureUtils.result)1 MoreAsserts.assertSetEquals (org.apache.bookkeeper.common.testing.MoreAsserts.assertSetEquals)1 MockExecutorController (org.apache.bookkeeper.common.testing.executors.MockExecutorController)1 RegistrationListener (org.apache.bookkeeper.discover.RegistrationClient.RegistrationListener)1 WatchTask (org.apache.bookkeeper.discover.ZKRegistrationClient.WatchTask)1 ZK_CONNECT_BACKOFF_MS (org.apache.bookkeeper.discover.ZKRegistrationClient.ZK_CONNECT_BACKOFF_MS)1