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)));
}
}
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);
}
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);
}
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;
}
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());
}
}
Aggregations