use of org.apache.bookkeeper.metadata.etcd.helpers.ValueStream in project bookkeeper by apache.
the class EtcdLedgerManagerTest method testRegisterLedgerMetadataListener.
@Test
public void testRegisterLedgerMetadataListener() throws Exception {
long ledgerId = System.currentTimeMillis();
// create a ledger metadata
LedgerMetadata metadata = LedgerMetadataBuilder.create().withId(ledgerId).withEnsembleSize(3).withWriteQuorumSize(3).withAckQuorumSize(2).withPassword("test-password".getBytes(UTF_8)).withDigestType(DigestType.CRC32C.toApiDigestType()).newEnsembleEntry(0L, createNumBookies(3)).build();
result(lm.createLedgerMetadata(ledgerId, metadata));
Versioned<LedgerMetadata> readMetadata = lm.readLedgerMetadata(ledgerId).get();
log.info("Create ledger metadata : {}", readMetadata.getValue());
// register first listener
LinkedBlockingQueue<Versioned<LedgerMetadata>> metadataQueue1 = new LinkedBlockingQueue<>();
LedgerMetadataListener listener1 = (lid, m) -> {
log.info("[listener1] Received ledger {} metadata : {}", lid, m);
metadataQueue1.add(m);
};
log.info("Registered first listener for ledger {}", ledgerId);
lm.registerLedgerMetadataListener(ledgerId, listener1);
// we should receive a metadata notification when a ledger is created
Versioned<LedgerMetadata> notifiedMetadata = metadataQueue1.take();
assertEquals(readMetadata, notifiedMetadata);
ValueStream<LedgerMetadata> lms = lm.getLedgerMetadataStream(ledgerId);
assertNotNull(lms.waitUntilWatched());
assertNotNull(result(lms.waitUntilWatched()));
// register second listener
LinkedBlockingQueue<Versioned<LedgerMetadata>> metadataQueue2 = new LinkedBlockingQueue<>();
LedgerMetadataListener listener2 = (lid, m) -> {
log.info("[listener2] Received ledger {} metadata : {}", lid, m);
metadataQueue2.add(m);
};
log.info("Registered second listener for ledger {}", ledgerId);
lm.registerLedgerMetadataListener(ledgerId, listener2);
Versioned<LedgerMetadata> notifiedMetadata2 = metadataQueue2.take();
assertEquals(readMetadata, notifiedMetadata2);
assertNotNull(lm.getLedgerMetadataStream(ledgerId));
// update the metadata
lm.writeLedgerMetadata(ledgerId, LedgerMetadataBuilder.from(metadata).newEnsembleEntry(10L, createNumBookies(3)).build(), notifiedMetadata.getVersion()).get();
readMetadata = lm.readLedgerMetadata(ledgerId).get();
assertEquals(readMetadata, metadataQueue1.take());
assertEquals(readMetadata, metadataQueue2.take());
lms = lm.getLedgerMetadataStream(ledgerId);
assertNotNull(lms);
assertEquals(2, lms.getNumConsumers());
// remove listener2
lm.unregisterLedgerMetadataListener(ledgerId, listener2);
lms = lm.getLedgerMetadataStream(ledgerId);
assertNotNull(lms);
assertEquals(1, lms.getNumConsumers());
// update the metadata again
lm.writeLedgerMetadata(ledgerId, LedgerMetadataBuilder.from(metadata).newEnsembleEntry(20L, createNumBookies(3)).build(), readMetadata.getVersion()).get();
readMetadata = lm.readLedgerMetadata(ledgerId).get();
assertEquals(readMetadata, metadataQueue1.take());
assertNull(metadataQueue2.poll());
// remove listener1
lm.unregisterLedgerMetadataListener(ledgerId, listener1);
// the value stream will be removed
while (lm.getLedgerMetadataStream(ledgerId) != null) {
TimeUnit.MILLISECONDS.sleep(100);
}
assertEquals(0, lms.getNumConsumers());
// update the metadata again
lm.writeLedgerMetadata(ledgerId, LedgerMetadataBuilder.from(metadata).newEnsembleEntry(30L, createNumBookies(3)).build(), readMetadata.getVersion()).get();
readMetadata = lm.readLedgerMetadata(ledgerId).get();
assertNull(metadataQueue1.poll());
assertNull(metadataQueue2.poll());
log.info("Registered first listener for ledger {} again", ledgerId);
lm.registerLedgerMetadataListener(ledgerId, listener1);
notifiedMetadata = metadataQueue1.take();
assertEquals(readMetadata, notifiedMetadata);
lms = lm.getLedgerMetadataStream(ledgerId);
assertNotNull(lms);
assertEquals(1, lms.getNumConsumers());
// delete the ledger
lm.removeLedgerMetadata(ledgerId, readMetadata.getVersion()).get();
// the listener will eventually be removed
while (lm.getLedgerMetadataStream(ledgerId) != null) {
TimeUnit.MILLISECONDS.sleep(100);
}
assertEquals(1, lms.getNumConsumers());
assertNull(metadataQueue1.poll());
assertNull(metadataQueue2.poll());
}
Aggregations