Search in sources :

Example 46 with DistributedLogManager

use of org.apache.distributedlog.api.DistributedLogManager in project bookkeeper by apache.

the class TestAsyncReaderWriter method writeRecordsWithOutstandingWriteLimit.

public void writeRecordsWithOutstandingWriteLimit(int stream, int global, boolean shouldFail) throws Exception {
    DistributedLogConfiguration confLocal = new DistributedLogConfiguration();
    confLocal.addConfiguration(testConf);
    confLocal.setOutputBufferSize(0);
    confLocal.setImmediateFlushEnabled(true);
    confLocal.setPerWriterOutstandingWriteLimit(stream);
    confLocal.setOutstandingWriteLimitDarkmode(false);
    DistributedLogManager dlm;
    if (global > -1) {
        dlm = createNewDLM(confLocal, runtime.getMethodName(), new SimplePermitLimiter(false, global, new NullStatsLogger(), true, new FixedValueFeature("", 0)));
    } else {
        dlm = createNewDLM(confLocal, runtime.getMethodName());
    }
    BKAsyncLogWriter writer = (BKAsyncLogWriter) (dlm.startAsyncLogSegmentNonPartitioned());
    ArrayList<CompletableFuture<DLSN>> results = new ArrayList<CompletableFuture<DLSN>>(1000);
    for (int i = 0; i < 1000; i++) {
        results.add(writer.write(DLMTestUtil.getLogRecordInstance(1L)));
    }
    for (CompletableFuture<DLSN> result : results) {
        try {
            Utils.ioResult(result);
            if (shouldFail) {
                fail("should fail due to no outstanding writes permitted");
            }
        } catch (OverCapacityException ex) {
            assertTrue(shouldFail);
        }
    }
    writer.closeAndComplete();
    dlm.close();
}
Also used : FixedValueFeature(org.apache.bookkeeper.feature.FixedValueFeature) ArrayList(java.util.ArrayList) DynamicDistributedLogConfiguration(org.apache.distributedlog.config.DynamicDistributedLogConfiguration) CompletableFuture(java.util.concurrent.CompletableFuture) NullStatsLogger(org.apache.bookkeeper.stats.NullStatsLogger) DistributedLogManager(org.apache.distributedlog.api.DistributedLogManager) SimplePermitLimiter(org.apache.distributedlog.util.SimplePermitLimiter) OverCapacityException(org.apache.distributedlog.exceptions.OverCapacityException)

Example 47 with DistributedLogManager

use of org.apache.distributedlog.api.DistributedLogManager in project bookkeeper by apache.

the class TestAsyncReaderWriter method testAsyncReadMissingLogSegmentsNotification.

@DistributedLogAnnotations.FlakyTest
@Test(timeout = 60000)
public void testAsyncReadMissingLogSegmentsNotification() throws Exception {
    String name = "distrlog-async-reader-missing-zk-notification";
    DistributedLogConfiguration confLocal = new DistributedLogConfiguration();
    confLocal.loadConf(testConf);
    confLocal.setOutputBufferSize(0);
    confLocal.setImmediateFlushEnabled(true);
    confLocal.setReadAheadBatchSize(1);
    confLocal.setReadAheadMaxRecords(1);
    confLocal.setReadLACLongPollTimeout(49);
    confLocal.setReaderIdleWarnThresholdMillis(100);
    confLocal.setReaderIdleErrorThresholdMillis(20000);
    final DistributedLogManager dlm = createNewDLM(confLocal, name);
    final Thread currentThread = Thread.currentThread();
    final int segmentSize = 10;
    final int numSegments = 3;
    final CountDownLatch latch = new CountDownLatch(1);
    final CountDownLatch readLatch = new CountDownLatch(1);
    final ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
    executor.schedule(new Runnable() {

        @Override
        public void run() {
            try {
                int txid = 1;
                for (long i = 0; i < numSegments; i++) {
                    BKSyncLogWriter writer = (BKSyncLogWriter) dlm.startLogSegmentNonPartitioned();
                    for (long j = 1; j <= segmentSize; j++) {
                        writer.write(DLMTestUtil.getLargeLogRecordInstance(txid++));
                        if ((i == 0) && (j == 1)) {
                            latch.countDown();
                        } else {
                            // wait for reader to start
                            readLatch.await();
                        }
                    }
                    writer.closeAndComplete();
                    Thread.sleep(100);
                }
            } catch (Exception exc) {
                if (!executor.isShutdown()) {
                    currentThread.interrupt();
                }
            }
        }
    }, 0, TimeUnit.MILLISECONDS);
    latch.await();
    BKAsyncLogReader reader = (BKAsyncLogReader) dlm.getAsyncLogReader(DLSN.InitialDLSN);
    reader.disableReadAheadLogSegmentsNotification();
    boolean exceptionEncountered = false;
    int recordCount = 0;
    try {
        while (true) {
            CompletableFuture<LogRecordWithDLSN> record = reader.readNext();
            Utils.ioResult(record);
            if (recordCount == 0) {
                readLatch.countDown();
            }
            recordCount++;
            if (recordCount >= segmentSize * numSegments) {
                break;
            }
        }
    } catch (IdleReaderException exc) {
        exceptionEncountered = true;
    }
    assertTrue(!exceptionEncountered);
    Assert.assertEquals(recordCount, segmentSize * numSegments);
    assertTrue(!currentThread.isInterrupted());
    Utils.close(reader);
    executor.shutdown();
}
Also used : ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) CountDownLatch(java.util.concurrent.CountDownLatch) IdleReaderException(org.apache.distributedlog.exceptions.IdleReaderException) OverCapacityException(org.apache.distributedlog.exceptions.OverCapacityException) ReadCancelledException(org.apache.distributedlog.exceptions.ReadCancelledException) DLIllegalStateException(org.apache.distributedlog.exceptions.DLIllegalStateException) WriteException(org.apache.distributedlog.exceptions.WriteException) LockingException(org.apache.distributedlog.exceptions.LockingException) BKTransmitException(org.apache.distributedlog.exceptions.BKTransmitException) EndOfStreamException(org.apache.distributedlog.exceptions.EndOfStreamException) IOException(java.io.IOException) LogRecordTooLongException(org.apache.distributedlog.exceptions.LogRecordTooLongException) DynamicDistributedLogConfiguration(org.apache.distributedlog.config.DynamicDistributedLogConfiguration) IdleReaderException(org.apache.distributedlog.exceptions.IdleReaderException) DistributedLogManager(org.apache.distributedlog.api.DistributedLogManager) Test(org.junit.Test)

Example 48 with DistributedLogManager

use of org.apache.distributedlog.api.DistributedLogManager in project bookkeeper by apache.

the class TestAsyncReaderWriter method testIdleReaderExceptionWhenKeepAliveIsDisabled.

@Test(timeout = 60000)
public void testIdleReaderExceptionWhenKeepAliveIsDisabled() throws Exception {
    String name = runtime.getMethodName();
    DistributedLogConfiguration confLocal = new DistributedLogConfiguration();
    confLocal.addConfiguration(testConf);
    confLocal.setOutputBufferSize(0);
    confLocal.setImmediateFlushEnabled(false);
    confLocal.setPeriodicFlushFrequencyMilliSeconds(0);
    confLocal.setPeriodicKeepAliveMilliSeconds(0);
    confLocal.setReadLACLongPollTimeout(9);
    confLocal.setReaderIdleWarnThresholdMillis(20);
    confLocal.setReaderIdleErrorThresholdMillis(40);
    URI uri = createDLMURI("/" + name);
    ensureURICreated(uri);
    DistributedLogManager dlm = createNewDLM(confLocal, name);
    BKAsyncLogWriter writer = (BKAsyncLogWriter) Utils.ioResult(dlm.openAsyncLogWriter());
    writer.write(DLMTestUtil.getLogRecordInstance(1L));
    AsyncLogReader reader = Utils.ioResult(dlm.openAsyncLogReader(DLSN.InitialDLSN));
    try {
        Utils.ioResult(reader.readNext());
        fail("Should fail when stream is idle");
    } catch (IdleReaderException ire) {
    // expected
    }
    Utils.close(reader);
    writer.close();
    dlm.close();
}
Also used : DynamicDistributedLogConfiguration(org.apache.distributedlog.config.DynamicDistributedLogConfiguration) IdleReaderException(org.apache.distributedlog.exceptions.IdleReaderException) AsyncLogReader(org.apache.distributedlog.api.AsyncLogReader) DistributedLogManager(org.apache.distributedlog.api.DistributedLogManager) URI(java.net.URI) Test(org.junit.Test)

Example 49 with DistributedLogManager

use of org.apache.distributedlog.api.DistributedLogManager in project bookkeeper by apache.

the class TestAsyncReaderWriter method testSimpleAsyncReadPosition.

/**
 * Test Async Read by positioning to a given position in the log.
 * @throws Exception
 */
@Test(timeout = 60000)
public void testSimpleAsyncReadPosition() throws Exception {
    String name = runtime.getMethodName();
    DistributedLogConfiguration confLocal = new DistributedLogConfiguration();
    confLocal.loadConf(testConf);
    confLocal.setOutputBufferSize(1024);
    confLocal.setReadAheadWaitTime(10);
    confLocal.setReadAheadBatchSize(10);
    DistributedLogManager dlm = createNewDLM(confLocal, name);
    int numLogSegments = 3;
    int numRecordsPerLogSegment = 10;
    long txid = 1L;
    // write 3 log segments, 10 records per log segment
    txid = writeRecords(dlm, numLogSegments, numRecordsPerLogSegment, txid, false);
    // write another log segment with 5 records
    txid = writeLogSegment(dlm, 5, txid, Integer.MAX_VALUE, false);
    final CountDownLatch syncLatch = new CountDownLatch((int) (txid - 14));
    final CountDownLatch doneLatch = new CountDownLatch(1);
    final AtomicBoolean errorsFound = new AtomicBoolean(false);
    final AsyncLogReader reader = dlm.getAsyncLogReader(new DLSN(2, 2, 4));
    assertEquals(name, reader.getStreamName());
    boolean monotonic = LogSegmentMetadata.supportsSequenceId(confLocal.getDLLedgerMetadataLayoutVersion());
    TestAsyncReaderWriter.readNext(reader, new DLSN(2, 3, 0), monotonic ? 13L : Long.MIN_VALUE, monotonic, syncLatch, doneLatch, errorsFound);
    doneLatch.await();
    assertFalse("Errors found on reading records", errorsFound.get());
    syncLatch.await();
    Utils.close(reader);
    dlm.close();
}
Also used : DynamicDistributedLogConfiguration(org.apache.distributedlog.config.DynamicDistributedLogConfiguration) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AsyncLogReader(org.apache.distributedlog.api.AsyncLogReader) DistributedLogManager(org.apache.distributedlog.api.DistributedLogManager) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 50 with DistributedLogManager

use of org.apache.distributedlog.api.DistributedLogManager in project bookkeeper by apache.

the class TestAsyncReaderWriter method testGetLastTxId.

@Test(timeout = 60000)
public void testGetLastTxId() throws Exception {
    String name = runtime.getMethodName();
    DistributedLogConfiguration confLocal = new DistributedLogConfiguration();
    confLocal.addConfiguration(testConf);
    confLocal.setOutputBufferSize(0);
    confLocal.setImmediateFlushEnabled(true);
    DistributedLogManager dlm = createNewDLM(confLocal, name);
    AsyncLogWriter writer = dlm.startAsyncLogSegmentNonPartitioned();
    int numRecords = 10;
    for (int i = 0; i < numRecords; i++) {
        Utils.ioResult(writer.write(DLMTestUtil.getLogRecordInstance(i)));
        assertEquals("last tx id should become " + i, i, writer.getLastTxId());
    }
    // open a writer to recover the inprogress log segment
    AsyncLogWriter recoverWriter = dlm.startAsyncLogSegmentNonPartitioned();
    assertEquals("recovered last tx id should be " + (numRecords - 1), numRecords - 1, recoverWriter.getLastTxId());
}
Also used : DynamicDistributedLogConfiguration(org.apache.distributedlog.config.DynamicDistributedLogConfiguration) DistributedLogManager(org.apache.distributedlog.api.DistributedLogManager) AsyncLogWriter(org.apache.distributedlog.api.AsyncLogWriter) Test(org.junit.Test)

Aggregations

DistributedLogManager (org.apache.distributedlog.api.DistributedLogManager)174 Test (org.junit.Test)139 AsyncLogReader (org.apache.distributedlog.api.AsyncLogReader)34 DynamicDistributedLogConfiguration (org.apache.distributedlog.config.DynamicDistributedLogConfiguration)33 URI (java.net.URI)29 LogReader (org.apache.distributedlog.api.LogReader)26 Namespace (org.apache.distributedlog.api.namespace.Namespace)26 AsyncLogWriter (org.apache.distributedlog.api.AsyncLogWriter)23 CountDownLatch (java.util.concurrent.CountDownLatch)18 DLSN (org.apache.distributedlog.DLSN)17 LogRecordWithDLSN (org.apache.distributedlog.LogRecordWithDLSN)16 EndOfStreamException (org.apache.distributedlog.exceptions.EndOfStreamException)14 IOException (java.io.IOException)13 AppendOnlyStreamWriter (org.apache.distributedlog.AppendOnlyStreamWriter)13 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)12 LogSegmentMetadata (org.apache.distributedlog.LogSegmentMetadata)12 ArrayList (java.util.ArrayList)11 CompletableFuture (java.util.concurrent.CompletableFuture)11 DistributedLogConfiguration (org.apache.distributedlog.DistributedLogConfiguration)11 List (java.util.List)8