Search in sources :

Example 1 with ValueStream

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());
}
Also used : IntStream(java.util.stream.IntStream) LongVersion(org.apache.bookkeeper.versioning.LongVersion) LedgerRangeIterator(org.apache.bookkeeper.meta.LedgerManager.LedgerRangeIterator) CompletableFuture(java.util.concurrent.CompletableFuture) Code(org.apache.bookkeeper.client.BKException.Code) ArrayList(java.util.ArrayList) Assert.assertSame(org.junit.Assert.assertSame) Lists(com.google.common.collect.Lists) After(org.junit.After) Versioned(org.apache.bookkeeper.versioning.Versioned) Assert.fail(org.junit.Assert.fail) RandomStringUtils(org.apache.commons.lang.RandomStringUtils) Before(org.junit.Before) DigestType(org.apache.bookkeeper.client.BookKeeper.DigestType) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Assert.assertNotNull(org.junit.Assert.assertNotNull) LedgerMetadataListener(org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.LedgerMetadataListener) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) FutureUtils(org.apache.bookkeeper.common.concurrent.FutureUtils) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Collectors(java.util.stream.Collectors) BKException(org.apache.bookkeeper.client.BKException) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Assert.assertNull(org.junit.Assert.assertNull) LedgerRange(org.apache.bookkeeper.meta.LedgerManager.LedgerRange) FutureUtils.result(org.apache.bookkeeper.common.concurrent.FutureUtils.result) EtcdTestBase(org.apache.bookkeeper.metadata.etcd.testing.EtcdTestBase) LedgerMetadata(org.apache.bookkeeper.client.api.LedgerMetadata) LedgerMetadataBuilder(org.apache.bookkeeper.client.LedgerMetadataBuilder) Assert.assertEquals(org.junit.Assert.assertEquals) BookieId(org.apache.bookkeeper.net.BookieId) Version(org.apache.bookkeeper.versioning.Version) ValueStream(org.apache.bookkeeper.metadata.etcd.helpers.ValueStream) LedgerMetadata(org.apache.bookkeeper.client.api.LedgerMetadata) Versioned(org.apache.bookkeeper.versioning.Versioned) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) LedgerMetadataListener(org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.LedgerMetadataListener) Test(org.junit.Test)

Aggregations

Lists (com.google.common.collect.Lists)1 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 TimeUnit (java.util.concurrent.TimeUnit)1 Collectors (java.util.stream.Collectors)1 IntStream (java.util.stream.IntStream)1 Slf4j (lombok.extern.slf4j.Slf4j)1 BKException (org.apache.bookkeeper.client.BKException)1 Code (org.apache.bookkeeper.client.BKException.Code)1 DigestType (org.apache.bookkeeper.client.BookKeeper.DigestType)1 LedgerMetadataBuilder (org.apache.bookkeeper.client.LedgerMetadataBuilder)1 LedgerMetadata (org.apache.bookkeeper.client.api.LedgerMetadata)1 FutureUtils (org.apache.bookkeeper.common.concurrent.FutureUtils)1 FutureUtils.result (org.apache.bookkeeper.common.concurrent.FutureUtils.result)1 LedgerRange (org.apache.bookkeeper.meta.LedgerManager.LedgerRange)1 LedgerRangeIterator (org.apache.bookkeeper.meta.LedgerManager.LedgerRangeIterator)1