Search in sources :

Example 1 with LogRecord

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

the class TestDistributedLogAdmin method testChangeSequenceNumber.

/**
 * {@link https://issues.apache.org/jira/browse/DL-44}.
 */
@DistributedLogAnnotations.FlakyTest
@Ignore
@Test(timeout = 60000)
@SuppressWarnings("deprecation")
public void testChangeSequenceNumber() throws Exception {
    DistributedLogConfiguration confLocal = new DistributedLogConfiguration();
    confLocal.addConfiguration(conf);
    confLocal.setLogSegmentSequenceNumberValidationEnabled(false);
    confLocal.setLogSegmentCacheEnabled(false);
    DistributedLogConfiguration readConf = new DistributedLogConfiguration();
    readConf.addConfiguration(conf);
    readConf.setLogSegmentCacheEnabled(false);
    readConf.setLogSegmentSequenceNumberValidationEnabled(true);
    URI uri = createDLMURI("/change-sequence-number");
    zooKeeperClient.get().create(uri.getPath(), new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    Namespace namespace = NamespaceBuilder.newBuilder().conf(confLocal).uri(uri).build();
    Namespace readNamespace = NamespaceBuilder.newBuilder().conf(readConf).uri(uri).build();
    String streamName = "change-sequence-number";
    // create completed log segments
    DistributedLogManager dlm = namespace.openLog(streamName);
    DLMTestUtil.generateCompletedLogSegments(dlm, confLocal, 4, 10);
    DLMTestUtil.injectLogSegmentWithGivenLogSegmentSeqNo(dlm, confLocal, 5, 41, false, 10, true);
    dlm.close();
    // create a reader
    DistributedLogManager readDLM = readNamespace.openLog(streamName);
    AsyncLogReader reader = readDLM.getAsyncLogReader(DLSN.InitialDLSN);
    // read the records
    long expectedTxId = 1L;
    DLSN lastDLSN = DLSN.InitialDLSN;
    for (int i = 0; i < 4 * 10; i++) {
        LogRecordWithDLSN record = Utils.ioResult(reader.readNext());
        assertNotNull(record);
        DLMTestUtil.verifyLogRecord(record);
        assertEquals(expectedTxId, record.getTransactionId());
        expectedTxId++;
        lastDLSN = record.getDlsn();
    }
    LOG.info("Injecting bad log segment '3'");
    dlm = namespace.openLog(streamName);
    DLMTestUtil.injectLogSegmentWithGivenLogSegmentSeqNo(dlm, confLocal, 3L, 5 * 10 + 1, true, 10, false);
    LOG.info("Injected bad log segment '3'");
    // there isn't records should be read
    CompletableFuture<LogRecordWithDLSN> readFuture = reader.readNext();
    try {
        LogRecordWithDLSN record = Utils.ioResult(readFuture);
        fail("Should fail reading next record " + record + " when there is a corrupted log segment");
    } catch (UnexpectedException ue) {
    // expected
    }
    LOG.info("Dryrun fix inprogress segment that has lower sequence number");
    // Dryrun
    DistributedLogAdmin.fixInprogressSegmentWithLowerSequenceNumber(namespace, new DryrunLogSegmentMetadataStoreUpdater(confLocal, getLogSegmentMetadataStore(namespace)), streamName, false, false);
    try {
        reader = readDLM.getAsyncLogReader(lastDLSN);
        Utils.ioResult(reader.readNext());
        fail("Should fail reading next when there is a corrupted log segment");
    } catch (UnexpectedException ue) {
    // expected
    }
    LOG.info("Actual run fix inprogress segment that has lower sequence number");
    // Actual run
    DistributedLogAdmin.fixInprogressSegmentWithLowerSequenceNumber(namespace, LogSegmentMetadataStoreUpdater.createMetadataUpdater(confLocal, getLogSegmentMetadataStore(namespace)), streamName, false, false);
    // be able to read more after fix
    reader = readDLM.getAsyncLogReader(lastDLSN);
    // skip the first record
    Utils.ioResult(reader.readNext());
    readFuture = reader.readNext();
    expectedTxId = 51L;
    LogRecord record = Utils.ioResult(readFuture);
    assertNotNull(record);
    DLMTestUtil.verifyLogRecord(record);
    assertEquals(expectedTxId, record.getTransactionId());
    expectedTxId++;
    for (int i = 1; i < 10; i++) {
        record = Utils.ioResult(reader.readNext());
        assertNotNull(record);
        DLMTestUtil.verifyLogRecord(record);
        assertEquals(expectedTxId, record.getTransactionId());
        expectedTxId++;
    }
    Utils.close(reader);
    readDLM.close();
    dlm.close();
    namespace.close();
    readNamespace.close();
}
Also used : UnexpectedException(org.apache.distributedlog.exceptions.UnexpectedException) LogRecordWithDLSN(org.apache.distributedlog.LogRecordWithDLSN) DLSN(org.apache.distributedlog.DLSN) LogRecordWithDLSN(org.apache.distributedlog.LogRecordWithDLSN) AsyncLogReader(org.apache.distributedlog.api.AsyncLogReader) URI(java.net.URI) Namespace(org.apache.distributedlog.api.namespace.Namespace) DistributedLogConfiguration(org.apache.distributedlog.DistributedLogConfiguration) LogRecord(org.apache.distributedlog.LogRecord) DistributedLogManager(org.apache.distributedlog.api.DistributedLogManager) DryrunLogSegmentMetadataStoreUpdater(org.apache.distributedlog.metadata.DryrunLogSegmentMetadataStoreUpdater) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with LogRecord

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

the class TestBKLogSegmentEntryReader method generateCompletedLogSegments.

void generateCompletedLogSegments(DistributedLogManager dlm, DistributedLogConfiguration conf, long numCompletedSegments, long segmentSize) throws Exception {
    long txid = 1L;
    for (long i = 0; i < numCompletedSegments; i++) {
        AsyncLogWriter writer = Utils.ioResult(dlm.openAsyncLogWriter());
        for (long j = 1; j <= segmentSize; j++) {
            Utils.ioResult(writer.write(DLMTestUtil.getLogRecordInstance(txid++)));
            LogRecord ctrlRecord = DLMTestUtil.getLogRecordInstance(txid);
            ctrlRecord.setControl();
            Utils.ioResult(writer.write(ctrlRecord));
        }
        Utils.close(writer);
    }
}
Also used : LogRecord(org.apache.distributedlog.LogRecord) AsyncLogWriter(org.apache.distributedlog.api.AsyncLogWriter)

Example 3 with LogRecord

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

the class TestBKLogSegmentEntryReader method createInprogressLogSegment.

AsyncLogWriter createInprogressLogSegment(DistributedLogManager dlm, DistributedLogConfiguration conf, long segmentSize) throws Exception {
    AsyncLogWriter writer = Utils.ioResult(dlm.openAsyncLogWriter());
    for (long i = 1L; i <= segmentSize; i++) {
        Utils.ioResult(writer.write(DLMTestUtil.getLogRecordInstance(i)));
        LogRecord ctrlRecord = DLMTestUtil.getLogRecordInstance(i);
        ctrlRecord.setControl();
        Utils.ioResult(writer.write(ctrlRecord));
    }
    return writer;
}
Also used : LogRecord(org.apache.distributedlog.LogRecord) AsyncLogWriter(org.apache.distributedlog.api.AsyncLogWriter)

Example 4 with LogRecord

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

the class DLOutputStream method writeControlRecord.

private CompletableFuture<DLSN> writeControlRecord() {
    LogRecord record;
    synchronized (this) {
        record = new LogRecord(writePos, Unpooled.wrappedBuffer(CONTROL_RECORD_CONTENT));
        record.setControl();
    }
    return writer.write(record);
}
Also used : LogRecord(org.apache.distributedlog.LogRecord)

Example 5 with LogRecord

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

the class DLOutputStream method write.

private synchronized void write(ByteBuf buf) throws IOException {
    Throwable cause = exceptionUpdater.get(this);
    if (null != cause) {
        if (cause instanceof IOException) {
            throw (IOException) cause;
        } else {
            throw new UnexpectedException("Encountered unknown issue", cause);
        }
    }
    writePos += buf.readableBytes();
    LogRecord record = new LogRecord(writePos, buf);
    writer.write(record).whenComplete(new FutureEventListener<DLSN>() {

        @Override
        public void onSuccess(DLSN value) {
            synchronized (syncPos) {
                syncPos[0] = record.getTransactionId();
            }
        }

        @Override
        public void onFailure(Throwable cause) {
            exceptionUpdater.compareAndSet(DLOutputStream.this, null, cause);
        }
    });
}
Also used : UnexpectedException(org.apache.distributedlog.exceptions.UnexpectedException) DLSN(org.apache.distributedlog.DLSN) LogRecord(org.apache.distributedlog.LogRecord) IOException(java.io.IOException)

Aggregations

LogRecord (org.apache.distributedlog.LogRecord)5 DLSN (org.apache.distributedlog.DLSN)2 AsyncLogWriter (org.apache.distributedlog.api.AsyncLogWriter)2 UnexpectedException (org.apache.distributedlog.exceptions.UnexpectedException)2 IOException (java.io.IOException)1 URI (java.net.URI)1 DistributedLogConfiguration (org.apache.distributedlog.DistributedLogConfiguration)1 LogRecordWithDLSN (org.apache.distributedlog.LogRecordWithDLSN)1 AsyncLogReader (org.apache.distributedlog.api.AsyncLogReader)1 DistributedLogManager (org.apache.distributedlog.api.DistributedLogManager)1 Namespace (org.apache.distributedlog.api.namespace.Namespace)1 DryrunLogSegmentMetadataStoreUpdater (org.apache.distributedlog.metadata.DryrunLogSegmentMetadataStoreUpdater)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1