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;
}
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;
}
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);
}
});
}
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;
}
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);
}
Aggregations