use of org.apache.bookkeeper.versioning.Versioned in project bookkeeper by apache.
the class MetadataUpdateLoopTest method testBasicUpdate.
/**
* Test that we can update the metadata using the update loop.
*/
@Test
public void testBasicUpdate() throws Exception {
try (LedgerManager lm = new MockLedgerManager()) {
long ledgerId = 1234L;
LedgerMetadata initMeta = LedgerMetadataBuilder.create().withId(ledgerId).withEnsembleSize(5).withDigestType(DigestType.CRC32C).withPassword(new byte[0]).newEnsembleEntry(0L, Lists.newArrayList(BookieId.parse("0.0.0.0:3181"), BookieId.parse("0.0.0.1:3181"), BookieId.parse("0.0.0.2:3181"), BookieId.parse("0.0.0.3:3181"), BookieId.parse("0.0.0.4:3181"))).build();
Versioned<LedgerMetadata> writtenMetadata = lm.createLedgerMetadata(ledgerId, initMeta).get();
AtomicReference<Versioned<LedgerMetadata>> reference = new AtomicReference<>(writtenMetadata);
BookieId newAddress = BookieId.parse("0.0.0.5:3181");
MetadataUpdateLoop loop = new MetadataUpdateLoop(lm, ledgerId, reference::get, (currentMetadata) -> true, (currentMetadata) -> {
List<BookieId> ensemble = Lists.newArrayList(currentMetadata.getEnsembleAt(0L));
ensemble.set(0, newAddress);
return LedgerMetadataBuilder.from(currentMetadata).replaceEnsembleEntry(0L, ensemble).build();
}, reference::compareAndSet);
loop.run().get();
Assert.assertNotEquals(reference.get(), writtenMetadata);
Assert.assertEquals(reference.get().getValue().getEnsembleAt(0L).get(0), newAddress);
}
}
use of org.apache.bookkeeper.versioning.Versioned in project distributedlog by twitter.
the class Utils method zkGetData.
/**
* Retrieve data from zookeeper <code>path</code>.
*
* @param path
* zookeeper path to retrieve data
* @param watch
* whether to watch the path
* @return future representing the versioned value. null version or null value means path doesn't exist.
*/
public static Future<Versioned<byte[]>> zkGetData(ZooKeeper zk, String path, boolean watch) {
final Promise<Versioned<byte[]>> promise = new Promise<Versioned<byte[]>>();
zk.getData(path, watch, new AsyncCallback.DataCallback() {
@Override
public void processResult(int rc, String path, Object ctx, byte[] data, Stat stat) {
if (KeeperException.Code.OK.intValue() == rc) {
if (null == stat) {
promise.setValue(new Versioned<byte[]>(null, null));
} else {
promise.setValue(new Versioned<byte[]>(data, new ZkVersion(stat.getVersion())));
}
} else if (KeeperException.Code.NONODE.intValue() == rc) {
promise.setValue(new Versioned<byte[]>(null, null));
} else {
promise.setException(KeeperException.create(KeeperException.Code.get(rc)));
}
}
}, null);
return promise;
}
use of org.apache.bookkeeper.versioning.Versioned in project distributedlog by twitter.
the class TestZKLogSegmentMetadataStore method testStoreMaxLogSegmentSequenceNumberBadVersion.
@Test(timeout = 60000)
public void testStoreMaxLogSegmentSequenceNumberBadVersion() throws Exception {
Transaction<Object> updateTxn = lsmStore.transaction();
Versioned<Long> value = new Versioned<Long>(999L, new ZkVersion(10));
final Promise<Version> result = new Promise<Version>();
lsmStore.storeMaxLogSegmentSequenceNumber(updateTxn, rootZkPath, value, new Transaction.OpListener<Version>() {
@Override
public void onCommit(Version r) {
result.setValue(r);
}
@Override
public void onAbort(Throwable t) {
result.setException(t);
}
});
try {
FutureUtils.result(updateTxn.execute());
fail("Should fail on storing log segment sequence number if providing bad version");
} catch (ZKException zke) {
assertEquals(KeeperException.Code.BADVERSION, zke.getKeeperExceptionCode());
}
try {
Await.result(result);
fail("Should fail on storing log segment sequence number if providing bad version");
} catch (KeeperException ke) {
assertEquals(KeeperException.Code.BADVERSION, ke.code());
}
Stat stat = new Stat();
byte[] data = zkc.get().getData(rootZkPath, false, stat);
assertEquals(0, stat.getVersion());
assertEquals(0, data.length);
}
use of org.apache.bookkeeper.versioning.Versioned in project distributedlog by twitter.
the class TestZKLogSegmentMetadataStore method testStoreMaxTxnIdOnNonExistentPath.
@Test(timeout = 60000)
public void testStoreMaxTxnIdOnNonExistentPath() throws Exception {
Transaction<Object> updateTxn = lsmStore.transaction();
Versioned<Long> value = new Versioned<Long>(999L, new ZkVersion(10));
final Promise<Version> result = new Promise<Version>();
String nonExistentPath = rootZkPath + "/non-existent";
lsmStore.storeMaxLogSegmentSequenceNumber(updateTxn, nonExistentPath, value, new Transaction.OpListener<Version>() {
@Override
public void onCommit(Version r) {
result.setValue(r);
}
@Override
public void onAbort(Throwable t) {
result.setException(t);
}
});
try {
FutureUtils.result(updateTxn.execute());
fail("Should fail on storing log record transaction id if path doesn't exist");
} catch (ZKException zke) {
assertEquals(KeeperException.Code.NONODE, zke.getKeeperExceptionCode());
}
try {
Await.result(result);
fail("Should fail on storing log record transaction id if path doesn't exist");
} catch (KeeperException ke) {
assertEquals(KeeperException.Code.NONODE, ke.code());
}
}
use of org.apache.bookkeeper.versioning.Versioned in project distributedlog by twitter.
the class TestZKLogSegmentMetadataStore method testStoreMaxTxnId.
@Test(timeout = 60000)
public void testStoreMaxTxnId() throws Exception {
Transaction<Object> updateTxn = lsmStore.transaction();
Versioned<Long> value = new Versioned<Long>(999L, new ZkVersion(0));
final Promise<Version> result = new Promise<Version>();
lsmStore.storeMaxTxnId(updateTxn, rootZkPath, value, new Transaction.OpListener<Version>() {
@Override
public void onCommit(Version r) {
result.setValue(r);
}
@Override
public void onAbort(Throwable t) {
result.setException(t);
}
});
FutureUtils.result(updateTxn.execute());
assertEquals(1, ((ZkVersion) FutureUtils.result(result)).getZnodeVersion());
Stat stat = new Stat();
byte[] data = zkc.get().getData(rootZkPath, false, stat);
assertEquals(999L, DLUtils.deserializeTransactionId(data));
assertEquals(1, stat.getVersion());
}
Aggregations