Search in sources :

Example 36 with Versioned

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));
}
Also used : BookieId(org.apache.bookkeeper.net.BookieId) HierarchicalLedgerManagerFactory(org.apache.bookkeeper.meta.HierarchicalLedgerManagerFactory) Arrays(java.util.Arrays) BookKeeperClusterTestCase(org.apache.bookkeeper.test.BookKeeperClusterTestCase) GenericCallback(org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.GenericCallback) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) LoggerFactory(org.slf4j.LoggerFactory) Cleanup(lombok.Cleanup) ByteBuffer(java.nio.ByteBuffer) Lists(com.google.common.collect.Lists) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) MetadataDrivers.runFunctionWithLedgerManagerFactory(org.apache.bookkeeper.meta.MetadataDrivers.runFunctionWithLedgerManagerFactory) Versioned(org.apache.bookkeeper.versioning.Versioned) Parameterized(org.junit.runners.Parameterized) DigestType(org.apache.bookkeeper.client.BookKeeper.DigestType) Logger(org.slf4j.Logger) LongHierarchicalLedgerManagerFactory(org.apache.bookkeeper.meta.LongHierarchicalLedgerManagerFactory) LedgerMetadataListener(org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.LedgerMetadataListener) Collection(java.util.Collection) LedgerManager(org.apache.bookkeeper.meta.LedgerManager) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) LedgerIdGenerator(org.apache.bookkeeper.meta.LedgerIdGenerator) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) LedgerManagerFactory(org.apache.bookkeeper.meta.LedgerManagerFactory) List(java.util.List) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) LedgerMetadata(org.apache.bookkeeper.client.api.LedgerMetadata) Assert.assertEquals(org.junit.Assert.assertEquals) BookieId(org.apache.bookkeeper.net.BookieId) Version(org.apache.bookkeeper.versioning.Version) LedgerManager(org.apache.bookkeeper.meta.LedgerManager) LedgerIdGenerator(org.apache.bookkeeper.meta.LedgerIdGenerator) CountDownLatch(java.util.concurrent.CountDownLatch) Cleanup(lombok.Cleanup) ByteBuffer(java.nio.ByteBuffer) LedgerMetadata(org.apache.bookkeeper.client.api.LedgerMetadata) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) LedgerMetadataListener(org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.LedgerMetadataListener)

Example 37 with Versioned

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();
        }
    }
}
Also used : Versioned(org.apache.bookkeeper.versioning.Versioned) LongVersion(org.apache.bookkeeper.versioning.LongVersion) BookieException(org.apache.bookkeeper.bookie.BookieException)

Example 38 with Versioned

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));
    }
}
Also used : MockLedgerManager(org.apache.bookkeeper.meta.MockLedgerManager) Logger(org.slf4j.Logger) LedgerManager(org.apache.bookkeeper.meta.LedgerManager) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) MockLedgerManager(org.apache.bookkeeper.meta.MockLedgerManager) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.junit.Test) FutureUtils(org.apache.bookkeeper.common.concurrent.FutureUtils) BKException(org.apache.bookkeeper.client.BKException) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) Schedulers(io.reactivex.rxjava3.schedulers.Schedulers) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Lists(com.google.common.collect.Lists) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Versioned(org.apache.bookkeeper.versioning.Versioned) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) DigestType(org.apache.bookkeeper.client.api.DigestType) LedgerMetadata(org.apache.bookkeeper.client.api.LedgerMetadata) Assert(org.junit.Assert) LedgerMetadataBuilder(org.apache.bookkeeper.client.LedgerMetadataBuilder) BookieId(org.apache.bookkeeper.net.BookieId) CompletableFuture(java.util.concurrent.CompletableFuture) Versioned(org.apache.bookkeeper.versioning.Versioned) LedgerMetadata(org.apache.bookkeeper.client.api.LedgerMetadata) BKException(org.apache.bookkeeper.client.BKException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 39 with Versioned

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));
}
Also used : MockLedgerManager(org.apache.bookkeeper.meta.MockLedgerManager) Logger(org.slf4j.Logger) LedgerManager(org.apache.bookkeeper.meta.LedgerManager) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) MockLedgerManager(org.apache.bookkeeper.meta.MockLedgerManager) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.junit.Test) FutureUtils(org.apache.bookkeeper.common.concurrent.FutureUtils) BKException(org.apache.bookkeeper.client.BKException) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) Schedulers(io.reactivex.rxjava3.schedulers.Schedulers) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Lists(com.google.common.collect.Lists) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Versioned(org.apache.bookkeeper.versioning.Versioned) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) DigestType(org.apache.bookkeeper.client.api.DigestType) LedgerMetadata(org.apache.bookkeeper.client.api.LedgerMetadata) Assert(org.junit.Assert) LedgerMetadataBuilder(org.apache.bookkeeper.client.LedgerMetadataBuilder) BookieId(org.apache.bookkeeper.net.BookieId) CompletableFuture(java.util.concurrent.CompletableFuture) Versioned(org.apache.bookkeeper.versioning.Versioned) LedgerMetadata(org.apache.bookkeeper.client.api.LedgerMetadata) BKException(org.apache.bookkeeper.client.BKException) Test(org.junit.Test)

Example 40 with Versioned

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));
}
Also used : MockLedgerManager(org.apache.bookkeeper.meta.MockLedgerManager) Logger(org.slf4j.Logger) LedgerManager(org.apache.bookkeeper.meta.LedgerManager) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) MockLedgerManager(org.apache.bookkeeper.meta.MockLedgerManager) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.junit.Test) FutureUtils(org.apache.bookkeeper.common.concurrent.FutureUtils) BKException(org.apache.bookkeeper.client.BKException) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) Schedulers(io.reactivex.rxjava3.schedulers.Schedulers) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Lists(com.google.common.collect.Lists) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Versioned(org.apache.bookkeeper.versioning.Versioned) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) DigestType(org.apache.bookkeeper.client.api.DigestType) LedgerMetadata(org.apache.bookkeeper.client.api.LedgerMetadata) Assert(org.junit.Assert) LedgerMetadataBuilder(org.apache.bookkeeper.client.LedgerMetadataBuilder) BookieId(org.apache.bookkeeper.net.BookieId) Versioned(org.apache.bookkeeper.versioning.Versioned) LedgerMetadata(org.apache.bookkeeper.client.api.LedgerMetadata) BKException(org.apache.bookkeeper.client.BKException) Test(org.junit.Test)

Aggregations

Versioned (org.apache.bookkeeper.versioning.Versioned)135 LongVersion (org.apache.bookkeeper.versioning.LongVersion)72 Test (org.junit.Test)70 CompletableFuture (java.util.concurrent.CompletableFuture)51 LedgerMetadata (org.apache.bookkeeper.client.api.LedgerMetadata)42 Version (org.apache.bookkeeper.versioning.Version)36 List (java.util.List)35 BookieId (org.apache.bookkeeper.net.BookieId)35 Stat (org.apache.zookeeper.data.Stat)33 LedgerManager (org.apache.bookkeeper.meta.LedgerManager)28 Set (java.util.Set)26 IOException (java.io.IOException)23 BKException (org.apache.bookkeeper.client.BKException)23 HashSet (java.util.HashSet)22 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)21 ZkVersion (org.apache.bookkeeper.meta.ZkVersion)21 Lists (com.google.common.collect.Lists)20 Slf4j (lombok.extern.slf4j.Slf4j)20 URI (java.net.URI)19 Logger (org.slf4j.Logger)17