Search in sources :

Example 46 with Versioned

use of org.apache.bookkeeper.versioning.Versioned in project bookkeeper by apache.

the class ZKLogSegmentMetadataStore method processResult.

@Override
@SuppressWarnings("unchecked")
public void processResult(int rc, String path, Object ctx, List<String> children, Stat stat) {
    CompletableFuture<Versioned<List<String>>> result = ((CompletableFuture<Versioned<List<String>>>) ctx);
    if (KeeperException.Code.OK.intValue() == rc) {
        /**
         * cversion: the number of changes to the children of this znode *
         */
        LongVersion zkVersion = new LongVersion(stat.getCversion());
        result.complete(new Versioned(children, zkVersion));
    } else if (KeeperException.Code.NONODE.intValue() == rc) {
        result.completeExceptionally(new LogNotFoundException("Log " + path + " not found"));
    } else {
        result.completeExceptionally(new ZKException("Failed to get log segments from " + path, KeeperException.Code.get(rc)));
    }
}
Also used : ZKException(org.apache.distributedlog.exceptions.ZKException) Versioned(org.apache.bookkeeper.versioning.Versioned) LongVersion(org.apache.bookkeeper.versioning.LongVersion) LogNotFoundException(org.apache.distributedlog.exceptions.LogNotFoundException)

Example 47 with Versioned

use of org.apache.bookkeeper.versioning.Versioned in project bookkeeper by apache.

the class ZKLogSegmentMetadataStore method getLogSegmentNames.

@Override
public CompletableFuture<Versioned<List<String>>> getLogSegmentNames(String logSegmentsPath, LogSegmentNamesListener listener) {
    Watcher zkWatcher;
    if (null == listener) {
        zkWatcher = null;
    } else {
        closeLock.readLock().lock();
        try {
            if (closed) {
                zkWatcher = null;
            } else {
                Map<LogSegmentNamesListener, VersionedLogSegmentNamesListener> listenerSet = listeners.get(logSegmentsPath);
                if (null == listenerSet) {
                    Map<LogSegmentNamesListener, VersionedLogSegmentNamesListener> newListenerSet = new HashMap<LogSegmentNamesListener, VersionedLogSegmentNamesListener>();
                    Map<LogSegmentNamesListener, VersionedLogSegmentNamesListener> oldListenerSet = listeners.putIfAbsent(logSegmentsPath, newListenerSet);
                    if (null != oldListenerSet) {
                        listenerSet = oldListenerSet;
                    } else {
                        listenerSet = newListenerSet;
                    }
                }
                synchronized (listenerSet) {
                    listenerSet.put(listener, new VersionedLogSegmentNamesListener(listener));
                    if (!listeners.containsKey(logSegmentsPath)) {
                        // listener set has been removed, add it back
                        if (null != listeners.putIfAbsent(logSegmentsPath, listenerSet)) {
                            logger.debug("Listener set is already found for log segments path {}", logSegmentsPath);
                        }
                    }
                }
                zkWatcher = ZKLogSegmentMetadataStore.this;
            }
        } finally {
            closeLock.readLock().unlock();
        }
    }
    CompletableFuture<Versioned<List<String>>> getLogSegmentNamesResult = zkGetLogSegmentNames(logSegmentsPath, zkWatcher);
    if (null != listener) {
        getLogSegmentNamesResult.whenComplete(new ReadLogSegmentsTask(logSegmentsPath, this));
    }
    return zkGetLogSegmentNames(logSegmentsPath, zkWatcher);
}
Also used : Versioned(org.apache.bookkeeper.versioning.Versioned) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Watcher(org.apache.zookeeper.Watcher) LogSegmentNamesListener(org.apache.distributedlog.callback.LogSegmentNamesListener)

Example 48 with Versioned

use of org.apache.bookkeeper.versioning.Versioned in project bookkeeper by apache.

the class TestLogSegmentsZK method getMaxLogSegmentSequenceNo.

private static MaxLogSegmentSequenceNo getMaxLogSegmentSequenceNo(ZooKeeperClient zkc, URI uri, String streamName, DistributedLogConfiguration conf) throws Exception {
    Stat stat = new Stat();
    String logSegmentsPath = LogMetadata.getLogSegmentsPath(uri, streamName, conf.getUnpartitionedStreamName());
    byte[] data = zkc.get().getData(logSegmentsPath, false, stat);
    Versioned<byte[]> maxLSSNData = new Versioned<byte[]>(data, new LongVersion(stat.getVersion()));
    return new MaxLogSegmentSequenceNo(maxLSSNData);
}
Also used : Stat(org.apache.zookeeper.data.Stat) Versioned(org.apache.bookkeeper.versioning.Versioned) LongVersion(org.apache.bookkeeper.versioning.LongVersion)

Example 49 with Versioned

use of org.apache.bookkeeper.versioning.Versioned 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 50 with Versioned

use of org.apache.bookkeeper.versioning.Versioned 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

Versioned (org.apache.bookkeeper.versioning.Versioned)65 Test (org.junit.Test)45 LongVersion (org.apache.bookkeeper.versioning.LongVersion)32 Stat (org.apache.zookeeper.data.Stat)25 ZkVersion (org.apache.bookkeeper.meta.ZkVersion)21 URI (java.net.URI)19 CompletableFuture (java.util.concurrent.CompletableFuture)14 Version (org.apache.bookkeeper.versioning.Version)14 List (java.util.List)12 ZKException (org.apache.distributedlog.exceptions.ZKException)9 Promise (com.twitter.util.Promise)8 ZKException (com.twitter.distributedlog.exceptions.ZKException)7 LogMetadataForWriter (org.apache.distributedlog.metadata.LogMetadataForWriter)7 Transaction (org.apache.distributedlog.util.Transaction)7 AsyncCallback (org.apache.zookeeper.AsyncCallback)7 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)7 Transaction (com.twitter.distributedlog.util.Transaction)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 KeeperException (org.apache.zookeeper.KeeperException)6 LogSegmentMetadata (org.apache.distributedlog.LogSegmentMetadata)5