Search in sources :

Example 26 with AsyncLogReader

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();
}
Also used : LockingException(org.apache.distributedlog.exceptions.LockingException) AsyncLogReader(org.apache.distributedlog.api.AsyncLogReader) DistributedLogManager(org.apache.distributedlog.api.DistributedLogManager) URI(java.net.URI) Namespace(org.apache.distributedlog.api.namespace.Namespace) Test(org.junit.Test)

Example 27 with AsyncLogReader

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);
}
Also used : AsyncLogReader(org.apache.distributedlog.api.AsyncLogReader) DistributedLogManager(org.apache.distributedlog.api.DistributedLogManager) BKNamespaceDriver(org.apache.distributedlog.impl.BKNamespaceDriver) NamespaceDriver(org.apache.distributedlog.namespace.NamespaceDriver) BKNamespaceDriver(org.apache.distributedlog.impl.BKNamespaceDriver) Test(org.junit.Test)

Example 28 with AsyncLogReader

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();
}
Also used : LockClosedException(org.apache.distributedlog.lock.LockClosedException) LockingException(org.apache.distributedlog.exceptions.LockingException) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DistributedLogManager(org.apache.distributedlog.api.DistributedLogManager) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) TestName(org.junit.rules.TestName) NamespaceBuilder(org.apache.distributedlog.api.namespace.NamespaceBuilder) Utils(org.apache.distributedlog.util.Utils) Assert.fail(org.junit.Assert.fail) URI(java.net.URI) ExecutorService(java.util.concurrent.ExecutorService) SubscriptionsStore(org.apache.distributedlog.api.subscription.SubscriptionsStore) Logger(org.slf4j.Logger) FutureEventListener(org.apache.bookkeeper.common.concurrent.FutureEventListener) CancellationException(java.util.concurrent.CancellationException) NamespaceDriver(org.apache.distributedlog.namespace.NamespaceDriver) BKNamespaceDriver(org.apache.distributedlog.impl.BKNamespaceDriver) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Executors(java.util.concurrent.Executors) Namespace(org.apache.distributedlog.api.namespace.Namespace) CountDownLatch(java.util.concurrent.CountDownLatch) Rule(org.junit.Rule) LockCancelledException(org.apache.distributedlog.exceptions.LockCancelledException) Assert.assertFalse(org.junit.Assert.assertFalse) AsyncLogReader(org.apache.distributedlog.api.AsyncLogReader) Assert.assertEquals(org.junit.Assert.assertEquals) OwnershipAcquireFailedException(org.apache.distributedlog.exceptions.OwnershipAcquireFailedException) AsyncLogReader(org.apache.distributedlog.api.AsyncLogReader) DistributedLogManager(org.apache.distributedlog.api.DistributedLogManager) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 29 with AsyncLogReader

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();
}
Also used : AsyncLogReader(org.apache.distributedlog.api.AsyncLogReader) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) CompletableFuture(java.util.concurrent.CompletableFuture) DistributedLogManager(org.apache.distributedlog.api.DistributedLogManager) Test(org.junit.Test)

Example 30 with AsyncLogReader

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();
}
Also used : LockCancelledException(org.apache.distributedlog.exceptions.LockCancelledException) AsyncLogReader(org.apache.distributedlog.api.AsyncLogReader) CancellationException(java.util.concurrent.CancellationException) DistributedLogManager(org.apache.distributedlog.api.DistributedLogManager) LockClosedException(org.apache.distributedlog.lock.LockClosedException) Test(org.junit.Test)

Aggregations

AsyncLogReader (org.apache.distributedlog.api.AsyncLogReader)34 DistributedLogManager (org.apache.distributedlog.api.DistributedLogManager)29 Test (org.junit.Test)27 DynamicDistributedLogConfiguration (org.apache.distributedlog.config.DynamicDistributedLogConfiguration)13 URI (java.net.URI)8 CountDownLatch (java.util.concurrent.CountDownLatch)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 CancellationException (java.util.concurrent.CancellationException)6 Namespace (org.apache.distributedlog.api.namespace.Namespace)6 LockCancelledException (org.apache.distributedlog.exceptions.LockCancelledException)5 LockClosedException (org.apache.distributedlog.lock.LockClosedException)5 CompletableFuture (java.util.concurrent.CompletableFuture)4 LockingException (org.apache.distributedlog.exceptions.LockingException)4 OwnershipAcquireFailedException (org.apache.distributedlog.exceptions.OwnershipAcquireFailedException)4 DLSN (org.apache.distributedlog.DLSN)3 LogRecord (org.apache.distributedlog.LogRecord)3 LogRecordWithDLSN (org.apache.distributedlog.LogRecordWithDLSN)3 ArrayList (java.util.ArrayList)2 ExecutorService (java.util.concurrent.ExecutorService)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2