Search in sources :

Example 16 with DistributedLogManager

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

the class TestBKDistributedLogManager method testGetLastDLSN.

@Test(timeout = 60000)
public void testGetLastDLSN() throws Exception {
    String name = "distrlog-get-last-dlsn";
    DistributedLogConfiguration confLocal = new DistributedLogConfiguration();
    confLocal.loadConf(conf);
    confLocal.setFirstNumEntriesPerReadLastRecordScan(2);
    confLocal.setMaxNumEntriesPerReadLastRecordScan(4);
    confLocal.setImmediateFlushEnabled(true);
    confLocal.setOutputBufferSize(0);
    DistributedLogManager dlm = createNewDLM(confLocal, name);
    BKAsyncLogWriter writer = (BKAsyncLogWriter) dlm.startAsyncLogSegmentNonPartitioned();
    long txid = 1;
    LOG.info("Writing 10 control records");
    for (int i = 0; i < 10; i++) {
        LogRecord record = DLMTestUtil.getLogRecordInstance(txid++);
        record.setControl();
        Utils.ioResult(writer.writeControlRecord(record));
    }
    LOG.info("10 control records are written");
    try {
        dlm.getLastDLSN();
        fail("Should fail on getting last dlsn from an empty log.");
    } catch (LogEmptyException lee) {
    // expected
    }
    writer.closeAndComplete();
    LOG.info("Completed first log segment");
    writer = (BKAsyncLogWriter) dlm.startAsyncLogSegmentNonPartitioned();
    Utils.ioResult(writer.write(DLMTestUtil.getLogRecordInstance(txid++)));
    LOG.info("Completed second log segment");
    LOG.info("Writing another 10 control records");
    for (int i = 1; i < 10; i++) {
        LogRecord record = DLMTestUtil.getLogRecordInstance(txid++);
        record.setControl();
        Utils.ioResult(writer.write(record));
    }
    assertEquals(new DLSN(2, 0, 0), dlm.getLastDLSN());
    writer.closeAndComplete();
    LOG.info("Completed third log segment");
    assertEquals(new DLSN(2, 0, 0), dlm.getLastDLSN());
    writer.close();
    dlm.close();
}
Also used : LogEmptyException(org.apache.distributedlog.exceptions.LogEmptyException) DistributedLogManager(org.apache.distributedlog.api.DistributedLogManager) Test(org.junit.Test)

Example 17 with DistributedLogManager

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

the class TestBKDistributedLogManager method testWriteFailsAfterMarkEndOfStream.

@Test(timeout = 60000)
public void testWriteFailsAfterMarkEndOfStream() throws Exception {
    String name = "distrlog-mark-end-failure";
    DistributedLogManager dlm = createNewDLM(conf, name);
    long txid = 1;
    txid = writeAndMarkEndOfStream(dlm, txid);
    assertEquals(txid - 1, dlm.getLastTxId());
    LogRecord last = dlm.getLastLogRecord();
    assertEquals(txid - 1, last.getTransactionId());
    DLMTestUtil.verifyLogRecord(last);
    assertTrue(dlm.isEndOfStreamMarked());
    LogWriter writer = null;
    boolean exceptionEncountered = false;
    try {
        writer = dlm.startLogSegmentNonPartitioned();
        for (long j = 1; j <= DEFAULT_SEGMENT_SIZE / 2; j++) {
            writer.write(DLMTestUtil.getLogRecordInstance(txid++));
        }
    } catch (EndOfStreamException exc) {
        exceptionEncountered = true;
    }
    writer.close();
    assertTrue(exceptionEncountered);
}
Also used : LogWriter(org.apache.distributedlog.api.LogWriter) AsyncLogWriter(org.apache.distributedlog.api.AsyncLogWriter) DistributedLogManager(org.apache.distributedlog.api.DistributedLogManager) EndOfStreamException(org.apache.distributedlog.exceptions.EndOfStreamException) Test(org.junit.Test)

Example 18 with DistributedLogManager

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

the class TestBKDistributedLogManager method testDeleteLog.

@Test(timeout = 60000)
public void testDeleteLog() throws Exception {
    String name = "delete-log-should-delete-ledgers";
    DistributedLogManager dlm = createNewDLM(conf, name);
    long txid = 1;
    // Create the log and write some records
    BKSyncLogWriter writer = (BKSyncLogWriter) dlm.startLogSegmentNonPartitioned();
    for (long j = 1; j <= DEFAULT_SEGMENT_SIZE; j++) {
        writer.write(DLMTestUtil.getLogRecordInstance(txid++));
    }
    BKLogSegmentWriter perStreamLogWriter = writer.getCachedLogWriter();
    writer.closeAndComplete();
    BKLogWriteHandler blplm = ((BKDistributedLogManager) (dlm)).createWriteHandler(true);
    assertNotNull(zkc.exists(blplm.completedLedgerZNode(txid, txid - 1, perStreamLogWriter.getLogSegmentSequenceNumber()), false));
    Utils.ioResult(blplm.asyncClose());
    // Should be able to open the underline ledger using BK client
    long ledgerId = perStreamLogWriter.getLogSegmentId();
    BKNamespaceDriver driver = (BKNamespaceDriver) dlm.getNamespaceDriver();
    driver.getReaderBKC().get().openLedgerNoRecovery(ledgerId, BookKeeper.DigestType.CRC32, conf.getBKDigestPW().getBytes(UTF_8));
    // Delete the log and we shouldn't be able the open the ledger
    dlm.delete();
    try {
        driver.getReaderBKC().get().openLedgerNoRecovery(ledgerId, BookKeeper.DigestType.CRC32, conf.getBKDigestPW().getBytes(UTF_8));
        fail("Should fail to open ledger after we delete the log");
    } catch (BKException.BKNoSuchLedgerExistsException e) {
    // ignore
    }
    // delete again should not throw any exception
    try {
        dlm.delete();
    } catch (IOException ioe) {
        fail("Delete log twice should not throw any exception");
    }
}
Also used : DistributedLogManager(org.apache.distributedlog.api.DistributedLogManager) BKNamespaceDriver(org.apache.distributedlog.impl.BKNamespaceDriver) BKException(org.apache.bookkeeper.client.BKException) IOException(java.io.IOException) Test(org.junit.Test)

Example 19 with DistributedLogManager

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

the class TestBKLogReadHandler method prepareLogSegmentsNonPartitioned.

private void prepareLogSegmentsNonPartitioned(String name, int numSegments, int numEntriesPerSegment) throws Exception {
    DistributedLogManager dlm = createNewDLM(conf, name);
    long txid = 1;
    for (int sid = 0; sid < numSegments; ++sid) {
        LogWriter out = dlm.startLogSegmentNonPartitioned();
        for (int eid = 0; eid < numEntriesPerSegment; ++eid) {
            LogRecord record = DLMTestUtil.getLargeLogRecordInstance(txid);
            out.write(record);
            ++txid;
        }
        out.close();
    }
    dlm.close();
}
Also used : AsyncLogWriter(org.apache.distributedlog.api.AsyncLogWriter) LogWriter(org.apache.distributedlog.api.LogWriter) DistributedLogManager(org.apache.distributedlog.api.DistributedLogManager)

Example 20 with DistributedLogManager

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

the class TestBKLogReadHandler method testGetLogRecordCountAtLedgerBoundary.

@Test(timeout = 60000)
public void testGetLogRecordCountAtLedgerBoundary() throws Exception {
    String dlName = runtime.getMethodName();
    prepareLogSegmentsNonPartitioned(dlName, 11, 3);
    DistributedLogManager dlm = createNewDLM(conf, dlName);
    BKLogReadHandler readHandler = ((BKDistributedLogManager) dlm).createReadHandler();
    CompletableFuture<Long> count = null;
    count = readHandler.asyncGetLogRecordCount(new DLSN(2, 0, 0));
    assertEquals(30, Utils.ioResult(count).longValue());
    count = readHandler.asyncGetLogRecordCount(new DLSN(3, 0, 0));
    assertEquals(27, Utils.ioResult(count).longValue());
}
Also used : DistributedLogManager(org.apache.distributedlog.api.DistributedLogManager) 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