Search in sources :

Example 1 with LongVersion

use of org.apache.bookkeeper.versioning.LongVersion in project bookkeeper by apache.

the class ZKSubscriptionsStore method deleteSubscriber.

@Override
public CompletableFuture<Boolean> deleteSubscriber(String subscriberId) {
    subscribers.remove(subscriberId);
    String path = getSubscriberZKPath(subscriberId);
    return Utils.zkDeleteIfNotExist(zkc, path, new LongVersion(-1L));
}
Also used : LongVersion(org.apache.bookkeeper.versioning.LongVersion)

Example 2 with LongVersion

use of org.apache.bookkeeper.versioning.LongVersion in project bookkeeper by apache.

the class ZKLogStreamMetadataStore method executeCreateMissingPathTxn.

private static void executeCreateMissingPathTxn(ZooKeeper zk, List<Op> zkOps, List<byte[]> pathsToCreate, List<Versioned<byte[]>> metadatas, String logRootPath, CompletableFuture<List<Versioned<byte[]>>> promise) {
    zk.multi(zkOps, new AsyncCallback.MultiCallback() {

        @Override
        public void processResult(int rc, String path, Object ctx, List<OpResult> resultList) {
            if (KeeperException.Code.OK.intValue() == rc) {
                List<Versioned<byte[]>> finalMetadatas = Lists.newArrayListWithExpectedSize(metadatas.size());
                for (int i = 0; i < pathsToCreate.size(); i++) {
                    byte[] dataCreated = pathsToCreate.get(i);
                    if (null == dataCreated) {
                        finalMetadatas.add(metadatas.get(i));
                    } else {
                        finalMetadatas.add(new Versioned<byte[]>(dataCreated, new LongVersion(0)));
                    }
                }
                promise.complete(finalMetadatas);
            } else if (KeeperException.Code.NODEEXISTS.intValue() == rc) {
                promise.completeExceptionally(new LogExistsException("Someone just created log " + logRootPath));
            } else {
                if (LOG.isDebugEnabled()) {
                    StringBuilder builder = new StringBuilder();
                    for (OpResult result : resultList) {
                        if (result instanceof OpResult.ErrorResult) {
                            OpResult.ErrorResult errorResult = (OpResult.ErrorResult) result;
                            builder.append(errorResult.getErr()).append(",");
                        } else {
                            builder.append(0).append(",");
                        }
                    }
                    String resultCodeList = builder.substring(0, builder.length() - 1);
                    LOG.debug("Failed to create log, full rc list = {}", resultCodeList);
                }
                promise.completeExceptionally(new ZKException("Failed to create log " + logRootPath, KeeperException.Code.get(rc)));
            }
        }
    }, null);
}
Also used : LogExistsException(org.apache.distributedlog.exceptions.LogExistsException) Versioned(org.apache.bookkeeper.versioning.Versioned) AsyncCallback(org.apache.zookeeper.AsyncCallback) ZKException(org.apache.distributedlog.exceptions.ZKException) OpResult(org.apache.zookeeper.OpResult) LongVersion(org.apache.bookkeeper.versioning.LongVersion) List(java.util.List) LinkedList(java.util.LinkedList)

Example 3 with LongVersion

use of org.apache.bookkeeper.versioning.LongVersion in project bookkeeper by apache.

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 CompletableFuture<Versioned<byte[]>> zkGetData(ZooKeeper zk, String path, boolean watch) {
    final CompletableFuture<Versioned<byte[]>> promise = new CompletableFuture<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.complete(new Versioned<byte[]>(null, null));
                } else {
                    promise.complete(new Versioned<byte[]>(data, new LongVersion(stat.getVersion())));
                }
            } else if (KeeperException.Code.NONODE.intValue() == rc) {
                promise.complete(new Versioned<byte[]>(null, null));
            } else {
                promise.completeExceptionally(KeeperException.create(KeeperException.Code.get(rc)));
            }
        }
    }, null);
    return promise;
}
Also used : Versioned(org.apache.bookkeeper.versioning.Versioned) AsyncCallback(org.apache.zookeeper.AsyncCallback) CompletableFuture(java.util.concurrent.CompletableFuture) Stat(org.apache.zookeeper.data.Stat) LongVersion(org.apache.bookkeeper.versioning.LongVersion)

Example 4 with LongVersion

use of org.apache.bookkeeper.versioning.LongVersion in project bookkeeper by apache.

the class SimpleLedgerAllocator method createAllocationData.

private static CompletableFuture<Versioned<byte[]>> createAllocationData(final String allocatePath, final ZooKeeperClient zkc) {
    try {
        final CompletableFuture<Versioned<byte[]>> promise = new CompletableFuture<Versioned<byte[]>>();
        zkc.get().create(allocatePath, DistributedLogConstants.EMPTY_BYTES, zkc.getDefaultACL(), CreateMode.PERSISTENT, new org.apache.zookeeper.AsyncCallback.Create2Callback() {

            @Override
            public void processResult(int rc, String path, Object ctx, String name, Stat stat) {
                if (KeeperException.Code.OK.intValue() == rc) {
                    promise.complete(new Versioned<byte[]>(DistributedLogConstants.EMPTY_BYTES, new LongVersion(stat.getVersion())));
                } else if (KeeperException.Code.NODEEXISTS.intValue() == rc) {
                    FutureUtils.proxyTo(Utils.zkGetData(zkc, allocatePath, false), promise);
                } else {
                    promise.completeExceptionally(Utils.zkException(KeeperException.create(KeeperException.Code.get(rc)), allocatePath));
                }
            }
        }, null);
        return promise;
    } catch (ZooKeeperClient.ZooKeeperConnectionException e) {
        return FutureUtils.exception(Utils.zkException(e, allocatePath));
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        return FutureUtils.exception(Utils.zkException(e, allocatePath));
    }
}
Also used : Versioned(org.apache.bookkeeper.versioning.Versioned) CompletableFuture(java.util.concurrent.CompletableFuture) Stat(org.apache.zookeeper.data.Stat) ZooKeeperClient(org.apache.distributedlog.ZooKeeperClient) LongVersion(org.apache.bookkeeper.versioning.LongVersion)

Example 5 with LongVersion

use of org.apache.bookkeeper.versioning.LongVersion in project bookkeeper by apache.

the class ZKLogSegmentMetadataStore method storeMaxLogSegmentSequenceNumber.

// max sequence number and max transaction id
@Override
public void storeMaxLogSegmentSequenceNumber(Transaction<Object> txn, LogMetadata logMetadata, Versioned<Long> lssn, Transaction.OpListener<Version> listener) {
    Version version = lssn.getVersion();
    assert (version instanceof LongVersion);
    LongVersion zkVersion = (LongVersion) version;
    byte[] data = DLUtils.serializeLogSegmentSequenceNumber(lssn.getValue());
    Op setDataOp = Op.setData(logMetadata.getLogSegmentsPath(), data, (int) zkVersion.getLongVersion());
    ZKOp zkOp = new ZKVersionedSetOp(setDataOp, listener);
    txn.addOp(zkOp);
}
Also used : ZKOp(org.apache.distributedlog.zk.ZKOp) Op(org.apache.zookeeper.Op) DefaultZKOp(org.apache.distributedlog.zk.DefaultZKOp) ZKVersionedSetOp(org.apache.distributedlog.zk.ZKVersionedSetOp) LongVersion(org.apache.bookkeeper.versioning.LongVersion) Version(org.apache.bookkeeper.versioning.Version) LongVersion(org.apache.bookkeeper.versioning.LongVersion) ZKOp(org.apache.distributedlog.zk.ZKOp) DefaultZKOp(org.apache.distributedlog.zk.DefaultZKOp) ZKVersionedSetOp(org.apache.distributedlog.zk.ZKVersionedSetOp)

Aggregations

LongVersion (org.apache.bookkeeper.versioning.LongVersion)56 Test (org.junit.Test)37 Versioned (org.apache.bookkeeper.versioning.Versioned)29 Stat (org.apache.zookeeper.data.Stat)25 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)21 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)20 Version (org.apache.bookkeeper.versioning.Version)18 GenericCallbackFuture (org.apache.bookkeeper.test.TestCallbacks.GenericCallbackFuture)16 BKException (org.apache.bookkeeper.client.BKException)12 VoidCallback (org.apache.zookeeper.AsyncCallback.VoidCallback)12 Set (java.util.Set)11 LedgerMetadata (org.apache.bookkeeper.client.LedgerMetadata)10 StatCallback (org.apache.zookeeper.AsyncCallback.StatCallback)10 DataCallback (org.apache.zookeeper.AsyncCallback.DataCallback)9 Before (org.junit.Before)9 URI (java.net.URI)8 ZKException (org.apache.distributedlog.exceptions.ZKException)8 KeeperException (org.apache.zookeeper.KeeperException)8 Assert.assertEquals (org.junit.Assert.assertEquals)8 Assert.fail (org.junit.Assert.fail)8