use of org.apache.bookkeeper.versioning.LongVersion in project bookkeeper by apache.
the class AbstractZkLedgerManagerTest method testLedgerMetadataListenerOnRetries.
@Test
public void testLedgerMetadataListenerOnRetries() throws Exception {
long ledgerId = System.currentTimeMillis();
String ledgerStr = String.valueOf(ledgerId);
LinkedBlockingQueue<LedgerMetadata> changes = new LinkedBlockingQueue<>();
LedgerMetadataListener listener = (ledgerId1, metadata) -> changes.add(metadata);
metadata.setVersion(new LongVersion(1234L));
Stat stat = mock(Stat.class);
when(stat.getVersion()).thenReturn(1234);
when(stat.getCtime()).thenReturn(metadata.getCtime());
// fail the first get, so the ledger manager will retry get data again.
mockZkGetData(ledgerStr, true, KeeperException.Code.SESSIONEXPIRED.intValue(), null, null);
ledgerManager.registerLedgerMetadataListener(ledgerId, listener);
assertTrue(ledgerManager.listeners.containsKey(ledgerId));
// the listener will not be notified with any updates
assertNull(changes.poll());
// an retry task is scheduled
verify(scheduler, times(1)).schedule(any(Runnable.class), anyLong(), any(TimeUnit.class));
// zookeeper is called once
verify(mockZk, times(1)).getData(anyString(), any(Watcher.class), any(DataCallback.class), any());
// watcher is not registered since getData call is failed
assertFalse(watchers.containsKey(ledgerStr));
// mock get data to return a valid response
mockZkGetData(ledgerStr, true, KeeperException.Code.OK.intValue(), metadata.serialize(), stat);
schedulerController.advance(Duration.ofMillis(ZK_CONNECT_BACKOFF_MS));
// the listener will be notified with first get
LedgerMetadata change = changes.take();
assertEquals(metadata, change);
verify(mockZk, times(2)).getData(anyString(), any(Watcher.class), any(DataCallback.class), any());
// watcher is registered after successfully `getData`
assertTrue(watchers.containsKey(ledgerStr));
}
use of org.apache.bookkeeper.versioning.LongVersion in project bookkeeper by apache.
the class AbstractZkLedgerManagerTest method testCreateLedgerMetadataSuccess.
@Test
public void testCreateLedgerMetadataSuccess() throws Exception {
long ledgerId = System.currentTimeMillis();
String ledgerStr = String.valueOf(ledgerId);
mockZkUtilsAsyncCreateFullPathOptimistic(ledgerStr, CreateMode.PERSISTENT, KeeperException.Code.OK.intValue(), ledgerStr);
assertEquals(Version.NEW, metadata.getVersion());
GenericCallbackFuture<Void> callbackFuture = new GenericCallbackFuture<>();
ledgerManager.createLedgerMetadata(ledgerId, metadata, callbackFuture);
callbackFuture.get();
assertEquals(new LongVersion(0), metadata.getVersion());
}
use of org.apache.bookkeeper.versioning.LongVersion in project bookkeeper by apache.
the class AbstractZkLedgerManager method readLedgerMetadata.
protected void readLedgerMetadata(final long ledgerId, final GenericCallback<LedgerMetadata> readCb, Watcher watcher) {
zk.getData(getLedgerPath(ledgerId), watcher, new DataCallback() {
@Override
public void processResult(int rc, String path, Object ctx, byte[] data, Stat stat) {
if (rc == KeeperException.Code.NONODE.intValue()) {
if (LOG.isDebugEnabled()) {
LOG.debug("No such ledger: " + ledgerId, KeeperException.create(KeeperException.Code.get(rc), path));
}
readCb.operationComplete(BKException.Code.NoSuchLedgerExistsException, null);
return;
}
if (rc != KeeperException.Code.OK.intValue()) {
LOG.error("Could not read metadata for ledger: " + ledgerId, KeeperException.create(KeeperException.Code.get(rc), path));
readCb.operationComplete(BKException.Code.ZKException, null);
return;
}
if (stat == null) {
LOG.error("Could not parse ledger metadata for ledger: {}. Stat object is null", ledgerId);
readCb.operationComplete(BKException.Code.ZKException, null);
return;
}
LedgerMetadata metadata;
try {
metadata = LedgerMetadata.parseConfig(data, new LongVersion(stat.getVersion()), Optional.of(stat.getCtime()));
} catch (IOException e) {
LOG.error("Could not parse ledger metadata for ledger: " + ledgerId, e);
readCb.operationComplete(BKException.Code.ZKException, null);
return;
}
readCb.operationComplete(BKException.Code.OK, metadata);
}
}, null);
}
use of org.apache.bookkeeper.versioning.LongVersion in project bookkeeper by apache.
the class AbstractZkLedgerManager method writeLedgerMetadata.
@Override
public void writeLedgerMetadata(final long ledgerId, final LedgerMetadata metadata, final GenericCallback<Void> cb) {
Version v = metadata.getVersion();
if (!(v instanceof LongVersion)) {
cb.operationComplete(BKException.Code.MetadataVersionException, null);
return;
}
final LongVersion zv = (LongVersion) v;
zk.setData(getLedgerPath(ledgerId), metadata.serialize(), (int) zv.getLongVersion(), new StatCallback() {
@Override
public void processResult(int rc, String path, Object ctx, Stat stat) {
if (KeeperException.Code.BADVERSION.intValue() == rc) {
cb.operationComplete(BKException.Code.MetadataVersionException, null);
} else if (KeeperException.Code.OK.intValue() == rc) {
// update metadata version
metadata.setVersion(zv.setLongVersion(stat.getVersion()));
cb.operationComplete(BKException.Code.OK, null);
} else {
LOG.warn("Conditional update ledger metadata failed: {}", KeeperException.Code.get(rc));
cb.operationComplete(BKException.Code.ZKException, null);
}
}
}, null);
}
use of org.apache.bookkeeper.versioning.LongVersion in project bookkeeper by apache.
the class ZKRegistrationManager method readCookie.
@Override
public Versioned<byte[]> readCookie(String bookieId) throws BookieException {
String zkPath = getCookiePath(bookieId);
try {
Stat stat = zk.exists(zkPath, false);
byte[] data = zk.getData(zkPath, false, stat);
// sets stat version from ZooKeeper
LongVersion version = new LongVersion(stat.getVersion());
return new Versioned<>(data, version);
} catch (NoNodeException nne) {
throw new CookieNotFoundException(bookieId);
} catch (KeeperException | InterruptedException e) {
throw new MetadataStoreException("Failed to read cookie for bookie " + bookieId);
}
}
Aggregations