use of org.apache.distributedlog.api.AsyncLogReader 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.api.AsyncLogReader in project bookkeeper by apache.
the class TestAsyncReaderLock method testReaderLockIfLockPathDoesntExist.
@Test(timeout = 60000)
public void testReaderLockIfLockPathDoesntExist() throws Exception {
final String name = runtime.getMethodName();
DistributedLogManager dlm = createNewDLM(conf, name);
BKAsyncLogWriter writer = (BKAsyncLogWriter) (dlm.startAsyncLogSegmentNonPartitioned());
writer.write(DLMTestUtil.getLogRecordInstance(1L));
writer.closeAndComplete();
CompletableFuture<AsyncLogReader> futureReader1 = dlm.getAsyncLogReaderWithLock(DLSN.InitialDLSN);
BKAsyncLogReader reader1 = (BKAsyncLogReader) Utils.ioResult(futureReader1);
LogRecordWithDLSN record = Utils.ioResult(reader1.readNext());
assertEquals(1L, record.getTransactionId());
assertEquals(0L, record.getSequenceId());
DLMTestUtil.verifyLogRecord(record);
String readLockPath = reader1.readHandler.getReadLockPath();
Utils.close(reader1);
// simulate a old stream created without readlock path
NamespaceDriver driver = dlm.getNamespaceDriver();
((BKNamespaceDriver) driver).getWriterZKC().get().delete(readLockPath, -1);
CompletableFuture<AsyncLogReader> futureReader2 = dlm.getAsyncLogReaderWithLock(DLSN.InitialDLSN);
AsyncLogReader reader2 = Utils.ioResult(futureReader2);
record = Utils.ioResult(reader2.readNext());
assertEquals(1L, record.getTransactionId());
assertEquals(0L, record.getSequenceId());
DLMTestUtil.verifyLogRecord(record);
}
use of org.apache.distributedlog.api.AsyncLogReader in project bookkeeper by apache.
the class TestAsyncReaderLock method testReaderLockCloseInAcquireCallback.
@Test(timeout = 60000)
public void testReaderLockCloseInAcquireCallback() throws Exception {
final String name = runtime.getMethodName();
DistributedLogManager dlm = createNewDLM(conf, name);
BKAsyncLogWriter writer = (BKAsyncLogWriter) (dlm.startAsyncLogSegmentNonPartitioned());
writer.write(DLMTestUtil.getLogRecordInstance(1L));
writer.closeAndComplete();
final CountDownLatch latch = new CountDownLatch(1);
CompletableFuture<AsyncLogReader> futureReader1 = dlm.getAsyncLogReaderWithLock(DLSN.InitialDLSN);
futureReader1.thenCompose(reader -> reader.asyncClose().thenApply(result -> {
latch.countDown();
return null;
}));
latch.await();
dlm.close();
}
use of org.apache.distributedlog.api.AsyncLogReader in project bookkeeper by apache.
the class TestAsyncReaderLock method testReaderLockManyLocks.
@Test(timeout = 60000)
public void testReaderLockManyLocks() throws Exception {
String name = runtime.getMethodName();
DistributedLogManager dlm = createNewDLM(conf, name);
BKAsyncLogWriter writer = (BKAsyncLogWriter) (dlm.startAsyncLogSegmentNonPartitioned());
writer.write(DLMTestUtil.getLogRecordInstance(1L));
writer.write(DLMTestUtil.getLogRecordInstance(2L));
writer.closeAndComplete();
int count = 5;
final CountDownLatch acquiredLatch = new CountDownLatch(count);
final ArrayList<CompletableFuture<AsyncLogReader>> readers = new ArrayList<CompletableFuture<AsyncLogReader>>(count);
for (int i = 0; i < count; i++) {
readers.add(null);
}
final DistributedLogManager[] dlms = new DistributedLogManager[count];
for (int i = 0; i < count; i++) {
dlms[i] = createNewDLM(conf, name);
readers.set(i, dlms[i].getAsyncLogReaderWithLock(DLSN.InitialDLSN));
readers.get(i).whenComplete(new FutureEventListener<AsyncLogReader>() {
@Override
public void onSuccess(AsyncLogReader reader) {
acquiredLatch.countDown();
reader.asyncClose();
}
@Override
public void onFailure(Throwable cause) {
fail("acquire shouldnt have failed");
}
});
}
acquiredLatch.await();
for (int i = 0; i < count; i++) {
dlms[i].close();
}
dlm.close();
}
use of org.apache.distributedlog.api.AsyncLogReader in project bookkeeper by apache.
the class TestAsyncReaderLock method testReaderLockDlmClosed.
@Test(timeout = 60000)
public void testReaderLockDlmClosed() throws Exception {
String name = runtime.getMethodName();
DistributedLogManager dlm0 = createNewDLM(conf, name);
BKAsyncLogWriter writer = (BKAsyncLogWriter) (dlm0.startAsyncLogSegmentNonPartitioned());
writer.write(DLMTestUtil.getLogRecordInstance(1L));
writer.write(DLMTestUtil.getLogRecordInstance(2L));
writer.closeAndComplete();
DistributedLogManager dlm1 = createNewDLM(conf, name);
CompletableFuture<AsyncLogReader> futureReader1 = dlm1.getAsyncLogReaderWithLock(DLSN.InitialDLSN);
AsyncLogReader reader1 = Utils.ioResult(futureReader1);
BKDistributedLogManager dlm2 = (BKDistributedLogManager) createNewDLM(conf, name);
CompletableFuture<AsyncLogReader> futureReader2 = dlm2.getAsyncLogReaderWithLock(DLSN.InitialDLSN);
dlm2.close();
try {
Utils.ioResult(futureReader2);
fail("should have thrown exception!");
} catch (CancellationException ce) {
} catch (LockClosedException ex) {
} catch (LockCancelledException ex) {
}
Utils.close(reader1);
dlm0.close();
dlm1.close();
}
Aggregations