Search in sources :

Example 66 with Versioned

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);
    }
}
Also used : MockLedgerManager(org.apache.bookkeeper.meta.MockLedgerManager) BookieId(org.apache.bookkeeper.net.BookieId) MockLedgerManager(org.apache.bookkeeper.meta.MockLedgerManager) LedgerManager(org.apache.bookkeeper.meta.LedgerManager) LedgerMetadata(org.apache.bookkeeper.client.api.LedgerMetadata) Versioned(org.apache.bookkeeper.versioning.Versioned) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 67 with Versioned

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;
}
Also used : Versioned(org.apache.bookkeeper.versioning.Versioned) AsyncCallback(org.apache.zookeeper.AsyncCallback) Promise(com.twitter.util.Promise) Stat(org.apache.zookeeper.data.Stat) ZkVersion(org.apache.bookkeeper.meta.ZkVersion)

Example 68 with Versioned

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);
}
Also used : Versioned(org.apache.bookkeeper.versioning.Versioned) Promise(com.twitter.util.Promise) ZKException(com.twitter.distributedlog.exceptions.ZKException) Stat(org.apache.zookeeper.data.Stat) Transaction(com.twitter.distributedlog.util.Transaction) ZkVersion(org.apache.bookkeeper.meta.ZkVersion) Version(org.apache.bookkeeper.versioning.Version) ZkVersion(org.apache.bookkeeper.meta.ZkVersion) KeeperException(org.apache.zookeeper.KeeperException) Test(org.junit.Test)

Example 69 with Versioned

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());
    }
}
Also used : Versioned(org.apache.bookkeeper.versioning.Versioned) Promise(com.twitter.util.Promise) ZKException(com.twitter.distributedlog.exceptions.ZKException) Transaction(com.twitter.distributedlog.util.Transaction) ZkVersion(org.apache.bookkeeper.meta.ZkVersion) Version(org.apache.bookkeeper.versioning.Version) ZkVersion(org.apache.bookkeeper.meta.ZkVersion) KeeperException(org.apache.zookeeper.KeeperException) Test(org.junit.Test)

Example 70 with Versioned

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());
}
Also used : Versioned(org.apache.bookkeeper.versioning.Versioned) Promise(com.twitter.util.Promise) Stat(org.apache.zookeeper.data.Stat) Transaction(com.twitter.distributedlog.util.Transaction) ZkVersion(org.apache.bookkeeper.meta.ZkVersion) Version(org.apache.bookkeeper.versioning.Version) ZkVersion(org.apache.bookkeeper.meta.ZkVersion) 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