Search in sources :

Example 1 with LogWriter

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

the class TestBKDistributedLogManager method testNonPartitionedWritesInternal.

private void testNonPartitionedWritesInternal(String name, DistributedLogConfiguration conf) throws Exception {
    BKDistributedLogManager dlm = createNewDLM(conf, name);
    long txid = 1;
    for (long i = 0; i < 3; i++) {
        long start = txid;
        BKSyncLogWriter writer = dlm.startLogSegmentNonPartitioned();
        for (long j = 1; j <= DEFAULT_SEGMENT_SIZE; j++) {
            writer.write(DLMTestUtil.getLogRecordInstance(txid++));
        }
        BKLogSegmentWriter perStreamLogWriter = writer.getCachedLogWriter();
        writer.closeAndComplete();
        BKLogWriteHandler blplm = dlm.createWriteHandler(true);
        assertNotNull(zkc.exists(blplm.completedLedgerZNode(start, txid - 1, perStreamLogWriter.getLogSegmentSequenceNumber()), false));
        Utils.ioResult(blplm.asyncClose());
    }
    LogWriter writer = dlm.startLogSegmentNonPartitioned();
    for (long j = 1; j <= DEFAULT_SEGMENT_SIZE / 2; j++) {
        writer.write(DLMTestUtil.getLogRecordInstance(txid++));
    }
    writer.flush();
    writer.commit();
    writer.close();
    LogReader reader = dlm.getInputStream(1);
    long numTrans = 0;
    LogRecord record = reader.readNext(false);
    long lastTxId = -1;
    while (null != record) {
        DLMTestUtil.verifyLogRecord(record);
        assert (lastTxId < record.getTransactionId());
        lastTxId = record.getTransactionId();
        numTrans++;
        record = reader.readNext(false);
    }
    reader.close();
    assertEquals((txid - 1), numTrans);
}
Also used : LogWriter(org.apache.distributedlog.api.LogWriter) AsyncLogWriter(org.apache.distributedlog.api.AsyncLogWriter) LogReader(org.apache.distributedlog.api.LogReader) AsyncLogReader(org.apache.distributedlog.api.AsyncLogReader)

Example 2 with LogWriter

use of org.apache.distributedlog.api.LogWriter 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 3 with LogWriter

use of org.apache.distributedlog.api.LogWriter 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 4 with LogWriter

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

the class TestAsyncReaderWriter method writeLogSegment.

/**
 * Write <code>numRecords</code> records to the log, starting with <code>startTxId</code>.
 * It flushes every <code>flushPerNumRecords</code> records.
 *
 * @param dlm
 *          distributedlog manager
 * @param numRecords
 *          num records to write
 * @param startTxId
 *          start tx id
 * @param flushPerNumRecords
 *          number records to flush
 * @return next tx id
 * @throws IOException
 */
private static long writeLogSegment(DistributedLogManager dlm, int numRecords, long startTxId, int flushPerNumRecords, boolean emptyRecord) throws IOException {
    long txid = startTxId;
    LogWriter writer = dlm.startLogSegmentNonPartitioned();
    for (long j = 1; j <= numRecords; j++) {
        if (emptyRecord) {
            writer.write(DLMTestUtil.getEmptyLogRecordInstance(txid++));
        } else {
            writer.write(DLMTestUtil.getLargeLogRecordInstance(txid++));
        }
        if (j % flushPerNumRecords == 0) {
            writer.flush();
            writer.commit();
        }
    }
    writer.flush();
    writer.commit();
    writer.close();
    return txid;
}
Also used : LogWriter(org.apache.distributedlog.api.LogWriter) AsyncLogWriter(org.apache.distributedlog.api.AsyncLogWriter)

Example 5 with LogWriter

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

the class TestBKDistributedLogNamespace method testInvalidStreamName.

@Test(timeout = 60000)
public void testInvalidStreamName() throws Exception {
    assertFalse(DLUtils.isReservedStreamName("test"));
    assertTrue(DLUtils.isReservedStreamName(".test"));
    URI uri = createDLMURI("/" + runtime.getMethodName());
    Namespace namespace = NamespaceBuilder.newBuilder().conf(conf).uri(uri).build();
    try {
        namespace.openLog(".test1");
        fail("Should fail to create invalid stream .test");
    } catch (InvalidStreamNameException isne) {
    // expected
    }
    DistributedLogManager dlm = namespace.openLog("test1");
    LogWriter writer = dlm.startLogSegmentNonPartitioned();
    writer.write(DLMTestUtil.getLogRecordInstance(1));
    writer.close();
    dlm.close();
    try {
        namespace.openLog(".test2");
        fail("Should fail to create invalid stream .test2");
    } catch (InvalidStreamNameException isne) {
    // expected
    }
    try {
        namespace.openLog("/ test2");
        fail("should fail to create invalid stream / test2");
    } catch (InvalidStreamNameException isne) {
    // expected
    }
    try {
        char[] chars = new char[6];
        for (int i = 0; i < chars.length; i++) {
            chars[i] = 'a';
        }
        chars[0] = 0;
        String streamName = new String(chars);
        namespace.openLog(streamName);
        fail("should fail to create invalid stream " + streamName);
    } catch (InvalidStreamNameException isne) {
    // expected
    }
    try {
        char[] chars = new char[6];
        for (int i = 0; i < chars.length; i++) {
            chars[i] = 'a';
        }
        chars[3] = '\u0010';
        String streamName = new String(chars);
        namespace.openLog(streamName);
        fail("should fail to create invalid stream " + streamName);
    } catch (InvalidStreamNameException isne) {
    // expected
    }
    DistributedLogManager newDLM = namespace.openLog("test_2-3");
    LogWriter newWriter = newDLM.startLogSegmentNonPartitioned();
    newWriter.write(DLMTestUtil.getLogRecordInstance(1));
    newWriter.close();
    newDLM.close();
    Iterator<String> streamIter = namespace.getLogs();
    Set<String> streamSet = Sets.newHashSet(streamIter);
    assertEquals(2, streamSet.size());
    assertTrue(streamSet.contains("test1"));
    assertTrue(streamSet.contains("test_2-3"));
    namespace.close();
}
Also used : InvalidStreamNameException(org.apache.distributedlog.exceptions.InvalidStreamNameException) LogWriter(org.apache.distributedlog.api.LogWriter) DistributedLogManager(org.apache.distributedlog.api.DistributedLogManager) URI(java.net.URI) Namespace(org.apache.distributedlog.api.namespace.Namespace) Test(org.junit.Test)

Aggregations

LogWriter (org.apache.distributedlog.api.LogWriter)9 DistributedLogManager (org.apache.distributedlog.api.DistributedLogManager)7 URI (java.net.URI)5 AsyncLogWriter (org.apache.distributedlog.api.AsyncLogWriter)5 Namespace (org.apache.distributedlog.api.namespace.Namespace)5 Test (org.junit.Test)4 IOException (java.io.IOException)2 AsyncLogReader (org.apache.distributedlog.api.AsyncLogReader)1 LogReader (org.apache.distributedlog.api.LogReader)1 EndOfStreamException (org.apache.distributedlog.exceptions.EndOfStreamException)1 InvalidStreamNameException (org.apache.distributedlog.exceptions.InvalidStreamNameException)1