use of org.apache.bookkeeper.versioning.Versioned in project bookkeeper by apache.
the class TestWatchEnsembleChange method testWatchMetadataRemoval.
private void testWatchMetadataRemoval(LedgerManagerFactory factory) throws Exception {
@Cleanup final LedgerManager manager = factory.newLedgerManager();
@Cleanup LedgerIdGenerator idGenerator = factory.newLedgerIdGenerator();
final ByteBuffer bbLedgerId = ByteBuffer.allocate(8);
final CountDownLatch createLatch = new CountDownLatch(1);
final CountDownLatch removeLatch = new CountDownLatch(1);
List<BookieId> ensemble = Lists.newArrayList(new BookieSocketAddress("192.0.2.1", 1234).toBookieId(), new BookieSocketAddress("192.0.2.2", 1234).toBookieId(), new BookieSocketAddress("192.0.2.3", 1234).toBookieId(), new BookieSocketAddress("192.0.2.4", 1234).toBookieId());
idGenerator.generateLedgerId(new GenericCallback<Long>() {
@Override
public void operationComplete(int rc, final Long lid) {
LedgerMetadata metadata = LedgerMetadataBuilder.create().withId(lid).withDigestType(digestType.toApiDigestType()).withPassword(new byte[0]).withEnsembleSize(4).withWriteQuorumSize(2).withAckQuorumSize(2).newEnsembleEntry(0L, ensemble).build();
manager.createLedgerMetadata(lid, metadata).whenComplete((result, exception) -> {
bbLedgerId.putLong(lid);
bbLedgerId.flip();
createLatch.countDown();
});
}
});
assertTrue(createLatch.await(2000, TimeUnit.MILLISECONDS));
final long createdLid = bbLedgerId.getLong();
manager.registerLedgerMetadataListener(createdLid, new LedgerMetadataListener() {
@Override
public void onChanged(long ledgerId, Versioned<LedgerMetadata> metadata) {
assertEquals(ledgerId, createdLid);
assertEquals(metadata, null);
removeLatch.countDown();
}
});
manager.removeLedgerMetadata(createdLid, Version.ANY).get(2, TimeUnit.SECONDS);
assertTrue(removeLatch.await(2, TimeUnit.SECONDS));
}
use of org.apache.bookkeeper.versioning.Versioned in project bookkeeper by apache.
the class MockRegistrationManager method writeCookie.
@Override
public void writeCookie(BookieId bookieId, Versioned<byte[]> cookieData) throws BookieException {
try {
cookies.compute(bookieId, (bookieId1, current) -> {
if (cookieData.getVersion() == Version.NEW) {
if (current == null) {
return new Versioned<byte[]>(cookieData.getValue(), new LongVersion(1));
} else {
throw new RuntimeException(new BookieException.CookieExistException(bookieId.getId()));
}
} else {
if (current != null && cookieData.getVersion().equals(current.getVersion())) {
LongVersion oldVersion = (LongVersion) current.getVersion();
LongVersion newVersion = new LongVersion(oldVersion.getLongVersion() + 1);
return new Versioned<byte[]>(cookieData.getValue(), newVersion);
} else {
throw new RuntimeException(new BookieException.CookieExistException(bookieId.getId()));
}
}
});
} catch (RuntimeException e) {
if (e.getCause() instanceof BookieException) {
throw (BookieException) e.getCause();
}
}
}
use of org.apache.bookkeeper.versioning.Versioned in project bookkeeper by apache.
the class MetadataAsyncIteratorTest method testAllLedgersErrorOnRead.
@Test
public void testAllLedgersErrorOnRead() throws Exception {
MockLedgerManager lm = new MockLedgerManager() {
@Override
public CompletableFuture<Versioned<LedgerMetadata>> readLedgerMetadata(long ledgerId) {
CompletableFuture<Versioned<LedgerMetadata>> promise = new CompletableFuture<>();
promise.completeExceptionally(new BKException.ZKException());
return promise;
}
};
ConcurrentHashMap<Long, LedgerMetadata> added = addLedgers(lm, 10000);
MetadataAsyncIterator iterator = new MetadataAsyncIterator(Schedulers.io(), lm, 100, /* inflight */
3, /* timeout */
TimeUnit.SECONDS);
try {
iterator.forEach((ledgerId, metadata) -> CompletableFuture.completedFuture(null)).get(10, TimeUnit.SECONDS);
} catch (ExecutionException ee) {
assertThat(ee.getCause(), instanceOf(BKException.ZKException.class));
}
}
use of org.apache.bookkeeper.versioning.Versioned in project bookkeeper by apache.
the class MetadataAsyncIteratorTest method testOneLedgerDisappearsBetweenListAndRead.
@Test
public void testOneLedgerDisappearsBetweenListAndRead() throws Exception {
MockLedgerManager lm = new MockLedgerManager() {
@Override
public CompletableFuture<Versioned<LedgerMetadata>> readLedgerMetadata(long ledgerId) {
if (ledgerId == 501) {
CompletableFuture<Versioned<LedgerMetadata>> promise = new CompletableFuture<>();
promise.completeExceptionally(new BKException.BKNoSuchLedgerExistsOnMetadataServerException());
return promise;
} else {
return super.readLedgerMetadata(ledgerId);
}
}
};
ConcurrentHashMap<Long, LedgerMetadata> added = addLedgers(lm, 10000);
MetadataAsyncIterator iterator = new MetadataAsyncIterator(Schedulers.io(), lm, 100, /* inflight */
3, /* timeout */
TimeUnit.SECONDS);
iterator.forEach((ledgerId, metadata) -> removeFromMap(added, ledgerId, metadata)).get(10, TimeUnit.SECONDS);
assertThat(added.size(), equalTo(1));
log.info("IKDEBUG {} {}", added, added.containsKey(5L));
assertThat(added.containsKey(501L), equalTo(true));
}
use of org.apache.bookkeeper.versioning.Versioned in project bookkeeper by apache.
the class MetadataAsyncIteratorTest method testEveryLedgerDisappearsBetweenListAndRead.
@Test
public void testEveryLedgerDisappearsBetweenListAndRead() throws Exception {
MockLedgerManager lm = new MockLedgerManager() {
@Override
public CompletableFuture<Versioned<LedgerMetadata>> readLedgerMetadata(long ledgerId) {
return FutureUtils.exception(new BKException.BKNoSuchLedgerExistsOnMetadataServerException());
}
};
int numLedgers = 10000;
ConcurrentHashMap<Long, LedgerMetadata> added = addLedgers(lm, numLedgers);
MetadataAsyncIterator iterator = new MetadataAsyncIterator(Schedulers.io(), lm, 100, 3, TimeUnit.SECONDS);
iterator.forEach((ledgerId, metadata) -> removeFromMap(added, ledgerId, metadata)).get(10, TimeUnit.SECONDS);
assertThat(added.size(), equalTo(numLedgers));
}
Aggregations