use of org.apache.distributedlog.exceptions.LockingException in project bookkeeper by apache.
the class TestAsyncReaderLock method testReaderLockSessionExpires.
@Test(timeout = 60000)
public void testReaderLockSessionExpires() throws Exception {
String name = runtime.getMethodName();
URI uri = createDLMURI("/" + name);
ensureURICreated(uri);
Namespace ns0 = NamespaceBuilder.newBuilder().conf(conf).uri(uri).build();
DistributedLogManager dlm0 = ns0.openLog(name);
BKAsyncLogWriter writer = (BKAsyncLogWriter) (dlm0.startAsyncLogSegmentNonPartitioned());
writer.write(DLMTestUtil.getLogRecordInstance(1L));
writer.write(DLMTestUtil.getLogRecordInstance(2L));
writer.closeAndComplete();
Namespace ns1 = NamespaceBuilder.newBuilder().conf(conf).uri(uri).build();
DistributedLogManager dlm1 = ns1.openLog(name);
CompletableFuture<AsyncLogReader> futureReader1 = dlm1.getAsyncLogReaderWithLock(DLSN.InitialDLSN);
AsyncLogReader reader1 = Utils.ioResult(futureReader1);
ZooKeeperClientUtils.expireSession(((BKNamespaceDriver) ns1.getNamespaceDriver()).getWriterZKC(), zkServers, 1000);
// The result of expireSession is somewhat non-deterministic with this lock.
// It may fail with LockingException or it may succesfully reacquire, so for
// the moment rather than make it deterministic we accept either result.
boolean success = false;
try {
Utils.ioResult(reader1.readNext());
success = true;
} catch (LockingException ex) {
}
if (success) {
Utils.ioResult(reader1.readNext());
}
Utils.close(reader1);
dlm0.close();
ns0.close();
dlm1.close();
ns1.close();
}
use of org.apache.distributedlog.exceptions.LockingException in project bookkeeper by apache.
the class ZKSessionLock method waitForTry.
synchronized LockWaiter waitForTry(Stopwatch stopwatch, CompletableFuture<LockWaiter> tryFuture) throws LockingException {
boolean success = false;
boolean stateChanged = false;
LockWaiter waiter;
try {
waiter = FutureUtils.result(tryFuture, lockOpTimeout, TimeUnit.MILLISECONDS);
success = true;
} catch (LockStateChangedException ex) {
stateChanged = true;
throw ex;
} catch (LockingException ex) {
throw ex;
} catch (TimeoutException toe) {
tryTimeouts.inc();
throw new LockingException(lockPath, "Timeout during try phase of lock acquire", toe);
} catch (Exception ex) {
String message = getLockId() + " failed to lock " + lockPath;
throw new LockingException(lockPath, message, ex);
} finally {
if (success) {
tryStats.registerSuccessfulEvent(stopwatch.elapsed(TimeUnit.MICROSECONDS), TimeUnit.MICROSECONDS);
} else {
tryStats.registerFailedEvent(stopwatch.elapsed(TimeUnit.MICROSECONDS), TimeUnit.MICROSECONDS);
}
// Exception, i.e. an Error
if (!success && !stateChanged) {
unlock();
}
}
return waiter;
}
use of org.apache.distributedlog.exceptions.LockingException in project bookkeeper by apache.
the class ZKDistributedLock method reacquireLock.
private CompletableFuture<ZKDistributedLock> reacquireLock(boolean throwLockAcquireException) throws LockingException {
final Stopwatch stopwatch = Stopwatch.createStarted();
CompletableFuture<ZKDistributedLock> lockPromise;
synchronized (this) {
if (closed) {
throw newLockClosedException();
}
if (null != lockReacquireException) {
if (throwLockAcquireException) {
throw lockReacquireException;
} else {
return null;
}
}
if (null != lockReacquireFuture) {
return lockReacquireFuture;
}
LOG.info("reacquiring lock at {}", lockPath);
lockReacquireFuture = lockPromise = new CompletableFuture<ZKDistributedLock>();
lockReacquireFuture.whenComplete(new FutureEventListener<ZKDistributedLock>() {
@Override
public void onSuccess(ZKDistributedLock lock) {
// if re-acquire successfully, clear the state.
synchronized (ZKDistributedLock.this) {
lockReacquireFuture = null;
}
reacquireStats.registerSuccessfulEvent(stopwatch.elapsed(TimeUnit.MICROSECONDS), TimeUnit.MICROSECONDS);
}
@Override
public void onFailure(Throwable cause) {
synchronized (ZKDistributedLock.this) {
if (cause instanceof LockingException) {
lockReacquireException = (LockingException) cause;
} else {
lockReacquireException = new LockingException(lockPath, "Exception on re-acquiring lock", cause);
}
}
reacquireStats.registerFailedEvent(stopwatch.elapsed(TimeUnit.MICROSECONDS), TimeUnit.MICROSECONDS);
}
});
}
reacquireCountUpdater.incrementAndGet(this);
internalReacquireLock(new AtomicInteger(Integer.MAX_VALUE), 0, lockPromise);
return lockPromise;
}
Aggregations