Search in sources :

Example 11 with Promise

use of com.twitter.util.Promise in project distributedlog by twitter.

the class BKLogHandler method getLastLogRecordAsync.

public Future<LogRecordWithDLSN> getLastLogRecordAsync(final boolean recover, final boolean includeEndOfStream) {
    final Promise<LogRecordWithDLSN> promise = new Promise<LogRecordWithDLSN>();
    checkLogStreamExistsAsync().addEventListener(new FutureEventListener<Void>() {

        @Override
        public void onSuccess(Void value) {
            asyncGetFullLedgerListDesc(true, true).addEventListener(new FutureEventListener<List<LogSegmentMetadata>>() {

                @Override
                public void onSuccess(List<LogSegmentMetadata> ledgerList) {
                    if (ledgerList.isEmpty()) {
                        promise.setException(new LogEmptyException("Log " + getFullyQualifiedName() + " has no records"));
                        return;
                    }
                    asyncGetLastLogRecord(ledgerList.iterator(), promise, recover, false, includeEndOfStream);
                }

                @Override
                public void onFailure(Throwable cause) {
                    promise.setException(cause);
                }
            });
        }

        @Override
        public void onFailure(Throwable cause) {
            promise.setException(cause);
        }
    });
    return promise;
}
Also used : Promise(com.twitter.util.Promise) LogEmptyException(com.twitter.distributedlog.exceptions.LogEmptyException) FutureEventListener(com.twitter.util.FutureEventListener) List(java.util.List) ArrayList(java.util.ArrayList)

Example 12 with Promise

use of com.twitter.util.Promise in project distributedlog by twitter.

the class BKLogReadHandler method ensureReadLockPathExist.

private Future<Void> ensureReadLockPathExist() {
    final Promise<Void> promise = new Promise<Void>();
    promise.setInterruptHandler(new com.twitter.util.Function<Throwable, BoxedUnit>() {

        @Override
        public BoxedUnit apply(Throwable t) {
            FutureUtils.setException(promise, new LockCancelledException(readLockPath, "Could not ensure read lock path", t));
            return null;
        }
    });
    Optional<String> parentPathShouldNotCreate = Optional.of(logMetadata.getLogRootPath());
    Utils.zkAsyncCreateFullPathOptimisticRecursive(zooKeeperClient, readLockPath, parentPathShouldNotCreate, new byte[0], zooKeeperClient.getDefaultACL(), CreateMode.PERSISTENT, new org.apache.zookeeper.AsyncCallback.StringCallback() {

        @Override
        public void processResult(final int rc, final String path, Object ctx, String name) {
            scheduler.submit(new Runnable() {

                @Override
                public void run() {
                    if (KeeperException.Code.NONODE.intValue() == rc) {
                        FutureUtils.setException(promise, new LogNotFoundException(String.format("Log %s does not exist or has been deleted", getFullyQualifiedName())));
                    } else if (KeeperException.Code.OK.intValue() == rc) {
                        FutureUtils.setValue(promise, null);
                        LOG.trace("Created path {}.", path);
                    } else if (KeeperException.Code.NODEEXISTS.intValue() == rc) {
                        FutureUtils.setValue(promise, null);
                        LOG.trace("Path {} is already existed.", path);
                    } else if (DistributedLogConstants.ZK_CONNECTION_EXCEPTION_RESULT_CODE == rc) {
                        FutureUtils.setException(promise, new ZooKeeperClient.ZooKeeperConnectionException(path));
                    } else if (DistributedLogConstants.DL_INTERRUPTED_EXCEPTION_RESULT_CODE == rc) {
                        FutureUtils.setException(promise, new DLInterruptedException(path));
                    } else {
                        FutureUtils.setException(promise, KeeperException.create(KeeperException.Code.get(rc)));
                    }
                }
            });
        }
    }, null);
    return promise;
}
Also used : LockCancelledException(com.twitter.distributedlog.exceptions.LockCancelledException) Promise(com.twitter.util.Promise) SafeRunnable(org.apache.bookkeeper.util.SafeRunnable) DLInterruptedException(com.twitter.distributedlog.exceptions.DLInterruptedException) BoxedUnit(scala.runtime.BoxedUnit) LogNotFoundException(com.twitter.distributedlog.exceptions.LogNotFoundException)

Example 13 with Promise

use of com.twitter.util.Promise in project distributedlog by twitter.

the class ZKSessionLock method asyncTryLock.

@Override
public Future<LockWaiter> asyncTryLock(final long timeout, final TimeUnit unit) {
    final Promise<String> result = new Promise<String>();
    final boolean wait = DistributedLogConstants.LOCK_IMMEDIATE != timeout;
    if (wait) {
        asyncTryLock(wait, result);
    } else {
        // try to check locks first
        zk.getChildren(lockPath, null, new AsyncCallback.Children2Callback() {

            @Override
            public void processResult(final int rc, String path, Object ctx, final List<String> children, Stat stat) {
                lockStateExecutor.submit(lockPath, new SafeRunnable() {

                    @Override
                    public void safeRun() {
                        if (!lockState.inState(State.INIT)) {
                            result.setException(new LockStateChangedException(lockPath, lockId, State.INIT, lockState.getState()));
                            return;
                        }
                        if (KeeperException.Code.OK.intValue() != rc) {
                            result.setException(KeeperException.create(KeeperException.Code.get(rc)));
                            return;
                        }
                        FailpointUtils.checkFailPointNoThrow(FailpointUtils.FailPointName.FP_LockTryAcquire);
                        Collections.sort(children, MEMBER_COMPARATOR);
                        if (children.size() > 0) {
                            asyncParseClientID(zk, lockPath, children.get(0)).addEventListener(new FutureEventListener<Pair<String, Long>>() {

                                @Override
                                public void onSuccess(Pair<String, Long> owner) {
                                    if (!checkOrClaimLockOwner(owner, result)) {
                                        acquireFuture.updateIfEmpty(new Return<Boolean>(false));
                                    }
                                }

                                @Override
                                public void onFailure(final Throwable cause) {
                                    lockStateExecutor.submit(lockPath, new SafeRunnable() {

                                        @Override
                                        public void safeRun() {
                                            result.setException(cause);
                                        }
                                    });
                                }
                            });
                        } else {
                            asyncTryLock(wait, result);
                        }
                    }
                });
            }
        }, null);
    }
    final Promise<Boolean> waiterAcquireFuture = new Promise<Boolean>(new com.twitter.util.Function<Throwable, BoxedUnit>() {

        @Override
        public BoxedUnit apply(Throwable t) {
            acquireFuture.raise(t);
            return BoxedUnit.UNIT;
        }
    });
    return result.map(new AbstractFunction1<String, LockWaiter>() {

        @Override
        public LockWaiter apply(final String currentOwner) {
            final Exception acquireException = new OwnershipAcquireFailedException(lockPath, currentOwner);
            FutureUtils.within(acquireFuture, timeout, unit, acquireException, lockStateExecutor, lockPath).addEventListener(new FutureEventListener<Boolean>() {

                @Override
                public void onSuccess(Boolean acquired) {
                    completeOrFail(acquireException);
                }

                @Override
                public void onFailure(final Throwable acquireCause) {
                    completeOrFail(acquireException);
                }

                private void completeOrFail(final Throwable acquireCause) {
                    if (isLockHeld()) {
                        waiterAcquireFuture.setValue(true);
                    } else {
                        asyncUnlock().addEventListener(new FutureEventListener<BoxedUnit>() {

                            @Override
                            public void onSuccess(BoxedUnit value) {
                                waiterAcquireFuture.setException(acquireCause);
                            }

                            @Override
                            public void onFailure(Throwable cause) {
                                waiterAcquireFuture.setException(acquireCause);
                            }
                        });
                    }
                }
            });
            ;
            return new LockWaiter(lockId.getLeft(), currentOwner, waiterAcquireFuture);
        }
    });
}
Also used : AsyncCallback(org.apache.zookeeper.AsyncCallback) Stat(org.apache.zookeeper.data.Stat) BoxedUnit(scala.runtime.BoxedUnit) Pair(org.apache.commons.lang3.tuple.Pair) OwnershipAcquireFailedException(com.twitter.distributedlog.exceptions.OwnershipAcquireFailedException) Return(com.twitter.util.Return) SafeRunnable(org.apache.bookkeeper.util.SafeRunnable) UnexpectedException(com.twitter.distributedlog.exceptions.UnexpectedException) ZKException(com.twitter.distributedlog.exceptions.ZKException) DLInterruptedException(com.twitter.distributedlog.exceptions.DLInterruptedException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) LockingException(com.twitter.distributedlog.exceptions.LockingException) KeeperException(org.apache.zookeeper.KeeperException) TimeoutException(com.twitter.util.TimeoutException) IOException(java.io.IOException) OwnershipAcquireFailedException(com.twitter.distributedlog.exceptions.OwnershipAcquireFailedException) Promise(com.twitter.util.Promise) FutureEventListener(com.twitter.util.FutureEventListener)

Example 14 with Promise

use of com.twitter.util.Promise in project distributedlog by twitter.

the class ZKSessionLock method asyncUnlock.

Future<BoxedUnit> asyncUnlock(final Throwable cause) {
    final Promise<BoxedUnit> promise = new Promise<BoxedUnit>();
    // Use lock executor here rather than lock action, because we want this opertaion to be applied
    // whether the epoch has changed or not. The member node is EPHEMERAL_SEQUENTIAL so there's no
    // risk of an ABA problem where we delete and recreate a node and then delete it again here.
    lockStateExecutor.submit(lockPath, new SafeRunnable() {

        @Override
        public void safeRun() {
            acquireFuture.updateIfEmpty(new Throw<Boolean>(cause));
            unlockInternal(promise);
            promise.addEventListener(new OpStatsListener<BoxedUnit>(unlockStats));
        }
    });
    return promise;
}
Also used : Promise(com.twitter.util.Promise) Throw(com.twitter.util.Throw) SafeRunnable(org.apache.bookkeeper.util.SafeRunnable) BoxedUnit(scala.runtime.BoxedUnit) OpStatsListener(com.twitter.distributedlog.stats.OpStatsListener)

Example 15 with Promise

use of com.twitter.util.Promise in project distributedlog by twitter.

the class TestZKLogSegmentMetadataStore method testStoreMaxTxnIdBadVersion.

@Test(timeout = 60000)
public void testStoreMaxTxnIdBadVersion() 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.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);
        }
    });
    try {
        FutureUtils.result(updateTxn.execute());
        fail("Should fail on storing log record transaction id if providing bad version");
    } catch (ZKException zke) {
        assertEquals(KeeperException.Code.BADVERSION, zke.getKeeperExceptionCode());
    }
    try {
        Await.result(result);
        fail("Should fail on storing log record transaction id 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)

Aggregations

Promise (com.twitter.util.Promise)48 IOException (java.io.IOException)11 Stat (org.apache.zookeeper.data.Stat)11 Test (org.junit.Test)10 FutureEventListener (com.twitter.util.FutureEventListener)9 ZkVersion (org.apache.bookkeeper.meta.ZkVersion)9 BoxedUnit (scala.runtime.BoxedUnit)9 ZKException (com.twitter.distributedlog.exceptions.ZKException)8 Future (com.twitter.util.Future)8 Versioned (org.apache.bookkeeper.versioning.Versioned)8 Stopwatch (com.google.common.base.Stopwatch)7 UnexpectedException (com.twitter.distributedlog.exceptions.UnexpectedException)7 List (java.util.List)7 AsyncCallback (org.apache.zookeeper.AsyncCallback)7 DLInterruptedException (com.twitter.distributedlog.exceptions.DLInterruptedException)6 Transaction (com.twitter.distributedlog.util.Transaction)6 Version (org.apache.bookkeeper.versioning.Version)6 KeeperException (org.apache.zookeeper.KeeperException)6 ByteBuffer (java.nio.ByteBuffer)5 ArrayList (java.util.ArrayList)5