Search in sources :

Example 21 with DistributedLogConfiguration

use of org.apache.distributedlog.DistributedLogConfiguration in project bookkeeper by apache.

the class TestBKLogSegmentEntryReader method testReadEntriesOnStateChange.

@Test(timeout = 60000)
public void testReadEntriesOnStateChange() throws Exception {
    DistributedLogConfiguration confLocal = new DistributedLogConfiguration();
    confLocal.addConfiguration(conf);
    confLocal.setOutputBufferSize(0);
    confLocal.setPeriodicFlushFrequencyMilliSeconds(0);
    confLocal.setImmediateFlushEnabled(false);
    confLocal.setNumPrefetchEntriesPerLogSegment(20);
    confLocal.setMaxPrefetchEntriesPerLogSegment(20);
    DistributedLogManager dlm = createNewDLM(confLocal, runtime.getMethodName());
    AsyncLogWriter writer = createInprogressLogSegment(dlm, confLocal, 5);
    List<LogSegmentMetadata> segments = dlm.getLogSegments();
    assertEquals(segments.size() + " log segments found, expected to be only one", 1, segments.size());
    BKLogSegmentEntryReader reader = createEntryReader(segments.get(0), 0, confLocal);
    reader.start();
    long expectedLastAddConfirmed = 8L;
    // wait until sending out all prefetch requests
    while (reader.readAheadEntries.size() < expectedLastAddConfirmed + 2) {
        TimeUnit.MILLISECONDS.sleep(10);
    }
    assertEquals(expectedLastAddConfirmed + 2, reader.getNextEntryId());
    long txId = 1L;
    long entryId = 0L;
    while (true) {
        Entry.Reader entryReader = Utils.ioResult(reader.readNext(1)).get(0);
        LogRecordWithDLSN record = entryReader.nextRecord();
        while (null != record) {
            if (!record.isControl()) {
                DLMTestUtil.verifyLogRecord(record);
                assertEquals(txId, record.getTransactionId());
                ++txId;
            }
            DLSN dlsn = record.getDlsn();
            assertEquals(1L, dlsn.getLogSegmentSequenceNo());
            assertEquals(entryId, dlsn.getEntryId());
            record = entryReader.nextRecord();
        }
        ++entryId;
        if (entryId == expectedLastAddConfirmed + 1) {
            break;
        }
    }
    assertEquals(6L, txId);
    CompletableFuture<List<Entry.Reader>> nextReadFuture = reader.readNext(1);
    // write another record to commit previous writes
    Utils.ioResult(writer.write(DLMTestUtil.getLogRecordInstance(txId)));
    // the long poll will be satisfied
    List<Entry.Reader> nextReadEntries = Utils.ioResult(nextReadFuture);
    assertEquals(1, nextReadEntries.size());
    Entry.Reader entryReader = nextReadEntries.get(0);
    LogRecordWithDLSN record = entryReader.nextRecord();
    assertNotNull(record);
    assertTrue(record.isControl());
    assertNull(entryReader.nextRecord());
    // once the read is advanced, we will prefetch next record
    while (reader.getNextEntryId() <= entryId) {
        TimeUnit.MILLISECONDS.sleep(10);
    }
    assertEquals(entryId + 2, reader.getNextEntryId());
    assertEquals(1, reader.readAheadEntries.size());
    // advance the entry id
    ++entryId;
    // close the writer, the write will be committed
    Utils.close(writer);
    entryReader = Utils.ioResult(reader.readNext(1)).get(0);
    record = entryReader.nextRecord();
    assertNotNull(record);
    assertFalse(record.isControl());
    assertNull(entryReader.nextRecord());
    while (reader.getNextEntryId() <= entryId + 1) {
        TimeUnit.MILLISECONDS.sleep(10);
    }
    assertEquals(entryId + 2, reader.getNextEntryId());
    assertEquals(1, reader.readAheadEntries.size());
    // get the new log segment
    List<LogSegmentMetadata> newSegments = dlm.getLogSegments();
    assertEquals(1, newSegments.size());
    assertFalse(newSegments.get(0).isInProgress());
    reader.onLogSegmentMetadataUpdated(newSegments.get(0));
    // should be cancelled and end of log segment should be signaled correctly
    try {
        // when we closed the log segment, another control record will be
        // written, so we loop over the reader until we reach end of log segment.
        Utils.ioResult(reader.readNext(1));
        Utils.ioResult(reader.readNext(1));
        fail("Should reach end of log segment");
    } catch (EndOfLogSegmentException eol) {
    // expected
    }
    Utils.close(reader);
}
Also used : LogRecordWithDLSN(org.apache.distributedlog.LogRecordWithDLSN) LogRecordWithDLSN(org.apache.distributedlog.LogRecordWithDLSN) DLSN(org.apache.distributedlog.DLSN) LogSegmentMetadata(org.apache.distributedlog.LogSegmentMetadata) AsyncLogWriter(org.apache.distributedlog.api.AsyncLogWriter) DistributedLogConfiguration(org.apache.distributedlog.DistributedLogConfiguration) Entry(org.apache.distributedlog.Entry) EndOfLogSegmentException(org.apache.distributedlog.exceptions.EndOfLogSegmentException) DistributedLogManager(org.apache.distributedlog.api.DistributedLogManager) List(java.util.List) Test(org.junit.Test)

Example 22 with DistributedLogConfiguration

use of org.apache.distributedlog.DistributedLogConfiguration in project bookkeeper by apache.

the class TestBKLogSegmentEntryReader method testCloseReaderToCancelPendingReads.

@Test(timeout = 60000)
public void testCloseReaderToCancelPendingReads() throws Exception {
    DistributedLogConfiguration confLocal = new DistributedLogConfiguration();
    confLocal.addConfiguration(conf);
    confLocal.setNumPrefetchEntriesPerLogSegment(10);
    confLocal.setMaxPrefetchEntriesPerLogSegment(10);
    DistributedLogManager dlm = createNewDLM(confLocal, runtime.getMethodName());
    DLMTestUtil.generateCompletedLogSegments(dlm, confLocal, 1, 20);
    List<LogSegmentMetadata> segments = dlm.getLogSegments();
    assertEquals(segments.size() + " log segments found, expected to be only one", 1, segments.size());
    BKLogSegmentEntryReader reader = createEntryReader(segments.get(0), 0, confLocal);
    List<CompletableFuture<List<Entry.Reader>>> futures = Lists.newArrayList();
    for (int i = 0; i < 5; i++) {
        futures.add(reader.readNext(1));
    }
    assertFalse("Reader should not be closed yet", reader.isClosed());
    Utils.close(reader);
    for (CompletableFuture<List<Entry.Reader>> future : futures) {
        try {
            Utils.ioResult(future);
            fail("The read request should be cancelled");
        } catch (ReadCancelledException rce) {
        // expected
        }
    }
    assertFalse(reader.hasCaughtUpOnInprogress());
    assertTrue("Reader should be closed yet", reader.isClosed());
}
Also used : LogSegmentMetadata(org.apache.distributedlog.LogSegmentMetadata) DistributedLogConfiguration(org.apache.distributedlog.DistributedLogConfiguration) CompletableFuture(java.util.concurrent.CompletableFuture) Entry(org.apache.distributedlog.Entry) ReadCancelledException(org.apache.distributedlog.exceptions.ReadCancelledException) DistributedLogManager(org.apache.distributedlog.api.DistributedLogManager) List(java.util.List) Test(org.junit.Test)

Example 23 with DistributedLogConfiguration

use of org.apache.distributedlog.DistributedLogConfiguration in project bookkeeper by apache.

the class TestBKLogSegmentEntryReader method testMaxPrefetchEntriesSmallSegment.

@Test(timeout = 60000)
public void testMaxPrefetchEntriesSmallSegment() throws Exception {
    DistributedLogConfiguration confLocal = new DistributedLogConfiguration();
    confLocal.addConfiguration(conf);
    confLocal.setOutputBufferSize(0);
    confLocal.setPeriodicFlushFrequencyMilliSeconds(0);
    confLocal.setImmediateFlushEnabled(false);
    confLocal.setNumPrefetchEntriesPerLogSegment(10);
    confLocal.setMaxPrefetchEntriesPerLogSegment(20);
    DistributedLogManager dlm = createNewDLM(confLocal, runtime.getMethodName());
    generateCompletedLogSegments(dlm, confLocal, 1, 5);
    List<LogSegmentMetadata> segments = dlm.getLogSegments();
    assertEquals(segments.size() + " log segments found, expected to be only one", 1, segments.size());
    BKLogSegmentEntryReader reader = createEntryReader(segments.get(0), 0, confLocal);
    reader.start();
    // wait for the read ahead entries to become available
    while (reader.readAheadEntries.size() < (reader.getLastAddConfirmed() + 1)) {
        TimeUnit.MILLISECONDS.sleep(10);
    }
    long txId = 1L;
    long entryId = 0L;
    assertEquals((reader.getLastAddConfirmed() + 1), reader.readAheadEntries.size());
    assertEquals((reader.getLastAddConfirmed() + 1), reader.getNextEntryId());
    // read first entry
    Entry.Reader entryReader = Utils.ioResult(reader.readNext(1)).get(0);
    LogRecordWithDLSN record = entryReader.nextRecord();
    while (null != record) {
        if (!record.isControl()) {
            DLMTestUtil.verifyLogRecord(record);
            assertEquals(txId, record.getTransactionId());
            ++txId;
        }
        DLSN dlsn = record.getDlsn();
        assertEquals(1L, dlsn.getLogSegmentSequenceNo());
        assertEquals(entryId, dlsn.getEntryId());
        record = entryReader.nextRecord();
    }
    ++entryId;
    assertEquals(2L, txId);
    assertEquals(reader.getLastAddConfirmed(), reader.readAheadEntries.size());
    assertEquals((reader.getLastAddConfirmed() + 1), reader.getNextEntryId());
    assertFalse(reader.hasCaughtUpOnInprogress());
    Utils.close(reader);
}
Also used : DistributedLogConfiguration(org.apache.distributedlog.DistributedLogConfiguration) Entry(org.apache.distributedlog.Entry) LogRecordWithDLSN(org.apache.distributedlog.LogRecordWithDLSN) LogRecordWithDLSN(org.apache.distributedlog.LogRecordWithDLSN) DLSN(org.apache.distributedlog.DLSN) DistributedLogManager(org.apache.distributedlog.api.DistributedLogManager) LogSegmentMetadata(org.apache.distributedlog.LogSegmentMetadata) Test(org.junit.Test)

Example 24 with DistributedLogConfiguration

use of org.apache.distributedlog.DistributedLogConfiguration in project bookkeeper by apache.

the class TestBKLogSegmentEntryReader method testReadEntriesFromCompleteLogSegment.

@Test(timeout = 60000)
public void testReadEntriesFromCompleteLogSegment() throws Exception {
    DistributedLogConfiguration confLocal = new DistributedLogConfiguration();
    confLocal.addConfiguration(conf);
    confLocal.setOutputBufferSize(0);
    confLocal.setPeriodicFlushFrequencyMilliSeconds(0);
    confLocal.setImmediateFlushEnabled(false);
    confLocal.setNumPrefetchEntriesPerLogSegment(10);
    confLocal.setMaxPrefetchEntriesPerLogSegment(10);
    DistributedLogManager dlm = createNewDLM(confLocal, runtime.getMethodName());
    generateCompletedLogSegments(dlm, confLocal, 1, 20);
    List<LogSegmentMetadata> segments = dlm.getLogSegments();
    assertEquals(segments.size() + " log segments found, expected to be only one", 1, segments.size());
    BKLogSegmentEntryReader reader = createEntryReader(segments.get(0), 0, confLocal);
    reader.start();
    boolean done = false;
    long txId = 1L;
    long entryId = 0L;
    while (!done) {
        Entry.Reader entryReader;
        try {
            entryReader = Utils.ioResult(reader.readNext(1)).get(0);
        } catch (EndOfLogSegmentException eol) {
            done = true;
            continue;
        }
        LogRecordWithDLSN record = entryReader.nextRecord();
        while (null != record) {
            if (!record.isControl()) {
                DLMTestUtil.verifyLogRecord(record);
                assertEquals(txId, record.getTransactionId());
                ++txId;
            }
            DLSN dlsn = record.getDlsn();
            assertEquals(1L, dlsn.getLogSegmentSequenceNo());
            assertEquals(entryId, dlsn.getEntryId());
            record = entryReader.nextRecord();
        }
        ++entryId;
    }
    assertEquals(21, txId);
    assertFalse(reader.hasCaughtUpOnInprogress());
    Utils.close(reader);
}
Also used : DistributedLogConfiguration(org.apache.distributedlog.DistributedLogConfiguration) Entry(org.apache.distributedlog.Entry) LogRecordWithDLSN(org.apache.distributedlog.LogRecordWithDLSN) LogRecordWithDLSN(org.apache.distributedlog.LogRecordWithDLSN) DLSN(org.apache.distributedlog.DLSN) EndOfLogSegmentException(org.apache.distributedlog.exceptions.EndOfLogSegmentException) DistributedLogManager(org.apache.distributedlog.api.DistributedLogManager) LogSegmentMetadata(org.apache.distributedlog.LogSegmentMetadata) Test(org.junit.Test)

Example 25 with DistributedLogConfiguration

use of org.apache.distributedlog.DistributedLogConfiguration in project bookkeeper by apache.

the class TestZkMetadataResolver method testEncodeRegionID.

@Test(timeout = 60000)
public void testEncodeRegionID() throws Exception {
    DistributedLogConfiguration dlConf = new DistributedLogConfiguration();
    URI uri = createURI("/messaging/distributedlog-testencoderegionid/dl1");
    DLMetadata meta1 = DLMetadata.create(new BKDLConfig("127.0.0.1:7000", "ledgers"));
    meta1.create(uri);
    BKDLConfig read1 = BKDLConfig.resolveDLConfig(zkc, uri);
    BKDLConfig.propagateConfiguration(read1, dlConf);
    assertFalse(dlConf.getEncodeRegionIDInLogSegmentMetadata());
    BKDLConfig.clearCachedDLConfigs();
    DLMetadata meta2 = DLMetadata.create(new BKDLConfig("127.0.0.1:7000", "ledgers").setEncodeRegionID(true));
    meta2.update(uri);
    BKDLConfig read2 = BKDLConfig.resolveDLConfig(zkc, uri);
    BKDLConfig.propagateConfiguration(read2, dlConf);
    assertTrue(dlConf.getEncodeRegionIDInLogSegmentMetadata());
    BKDLConfig.clearCachedDLConfigs();
    DLMetadata meta3 = DLMetadata.create(new BKDLConfig("127.0.0.1:7000", "ledgers").setEncodeRegionID(false));
    meta3.update(uri);
    BKDLConfig read3 = BKDLConfig.resolveDLConfig(zkc, uri);
    BKDLConfig.propagateConfiguration(read3, dlConf);
    assertFalse(dlConf.getEncodeRegionIDInLogSegmentMetadata());
    BKDLConfig.clearCachedDLConfigs();
}
Also used : DistributedLogConfiguration(org.apache.distributedlog.DistributedLogConfiguration) URI(java.net.URI) DLMetadata(org.apache.distributedlog.metadata.DLMetadata) Test(org.junit.Test)

Aggregations

DistributedLogConfiguration (org.apache.distributedlog.DistributedLogConfiguration)39 Test (org.junit.Test)25 URI (java.net.URI)10 DistributedLogManager (org.apache.distributedlog.api.DistributedLogManager)10 DLSN (org.apache.distributedlog.DLSN)9 LogRecordWithDLSN (org.apache.distributedlog.LogRecordWithDLSN)8 LogSegmentMetadata (org.apache.distributedlog.LogSegmentMetadata)8 Entry (org.apache.distributedlog.Entry)7 IOException (java.io.IOException)6 ZooKeeperClient (org.apache.distributedlog.ZooKeeperClient)5 Before (org.junit.Before)4 List (java.util.List)3 Feature (org.apache.bookkeeper.feature.Feature)3 Namespace (org.apache.distributedlog.api.namespace.Namespace)3 KeeperException (org.apache.zookeeper.KeeperException)3 Set (java.util.Set)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 AsyncLogWriter (org.apache.distributedlog.api.AsyncLogWriter)2