Search in sources :

Example 41 with LogSegmentMetadata

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

the class TestZKLogSegmentMetadataStore method testCreateDeleteLogSegmentFailure.

@Test(timeout = 60000)
public void testCreateDeleteLogSegmentFailure() throws Exception {
    LogSegmentMetadata segment1 = createLogSegment(1L);
    LogSegmentMetadata segment2 = createLogSegment(2L);
    LogSegmentMetadata segment3 = createLogSegment(3L);
    // create log segment 1
    Transaction<Object> createTxn = lsmStore.transaction();
    lsmStore.createLogSegment(createTxn, segment1, null);
    Utils.ioResult(createTxn.execute());
    // the log segment should be created
    assertNotNull("LogSegment " + segment1 + " should be created", zkc.get().exists(segment1.getZkPath(), false));
    // delete log segment 1 and delete log segment 2
    Transaction<Object> createDeleteTxn = lsmStore.transaction();
    lsmStore.deleteLogSegment(createDeleteTxn, segment1, null);
    lsmStore.deleteLogSegment(createDeleteTxn, segment2, null);
    lsmStore.createLogSegment(createDeleteTxn, segment3, null);
    try {
        Utils.ioResult(createDeleteTxn.execute());
        fail("Should fail transaction if one operation failed");
    } catch (Throwable t) {
        assertTrue("Transaction is aborted", t instanceof ZKException);
        ZKException zke = (ZKException) t;
        assertEquals("Transaction is aborted", KeeperException.Code.NONODE, zke.getKeeperExceptionCode());
    }
    // segment 1 should not be deleted
    assertNotNull("LogSegment " + segment1 + " should not be deleted", zkc.get().exists(segment1.getZkPath(), false));
    // segment 3 should not be created
    assertNull("LogSegment " + segment3 + " should be created", zkc.get().exists(segment3.getZkPath(), false));
}
Also used : ZKException(org.apache.distributedlog.exceptions.ZKException) LogSegmentMetadata(org.apache.distributedlog.LogSegmentMetadata) Test(org.junit.Test)

Example 42 with LogSegmentMetadata

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

the class TestZKLogSegmentMetadataStore method testGetLogSegment.

@Test(timeout = 60000)
public void testGetLogSegment() throws Exception {
    LogSegmentMetadata segment = createLogSegment(1L, 99L);
    Transaction<Object> createTxn = lsmStore.transaction();
    lsmStore.createLogSegment(createTxn, segment, null);
    Utils.ioResult(createTxn.execute());
    // the log segment should be created
    assertNotNull("LogSegment " + segment + " should be created", zkc.get().exists(segment.getZkPath(), false));
    LogSegmentMetadata readSegment = Utils.ioResult(lsmStore.getLogSegment(segment.getZkPath()));
    assertEquals("Log segment should match", segment, readSegment);
}
Also used : LogSegmentMetadata(org.apache.distributedlog.LogSegmentMetadata) Test(org.junit.Test)

Example 43 with LogSegmentMetadata

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

the class TestZKLogSegmentMetadataStore method testCreateDeleteLogSegmentSuccess.

@Test(timeout = 60000)
public void testCreateDeleteLogSegmentSuccess() throws Exception {
    LogSegmentMetadata segment1 = createLogSegment(1L);
    LogSegmentMetadata segment2 = createLogSegment(2L);
    // create log segment 1
    Transaction<Object> createTxn = lsmStore.transaction();
    lsmStore.createLogSegment(createTxn, segment1, null);
    Utils.ioResult(createTxn.execute());
    // the log segment should be created
    assertNotNull("LogSegment " + segment1 + " should be created", zkc.get().exists(segment1.getZkPath(), false));
    // delete log segment 1 and create log segment 2
    Transaction<Object> createDeleteTxn = lsmStore.transaction();
    lsmStore.createLogSegment(createDeleteTxn, segment2, null);
    lsmStore.deleteLogSegment(createDeleteTxn, segment1, null);
    Utils.ioResult(createDeleteTxn.execute());
    // segment 1 should be deleted, segment 2 should be created
    assertNull("LogSegment " + segment1 + " should be deleted", zkc.get().exists(segment1.getZkPath(), false));
    assertNotNull("LogSegment " + segment2 + " should be created", zkc.get().exists(segment2.getZkPath(), false));
}
Also used : LogSegmentMetadata(org.apache.distributedlog.LogSegmentMetadata) Test(org.junit.Test)

Example 44 with LogSegmentMetadata

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

the class DLUtils method nextLogSegmentSequenceNumber.

/**
 * Assign next log segment sequence number based on a decreasing list of log segments.
 *
 * @param segmentListDesc
 *          a decreasing list of log segments
 * @return null if no log segments was assigned a sequence number in <code>segmentListDesc</code>.
 *         otherwise, return next log segment sequence number
 */
public static Long nextLogSegmentSequenceNumber(List<LogSegmentMetadata> segmentListDesc) {
    int lastAssignedLogSegmentIdx = -1;
    Long lastAssignedLogSegmentSeqNo = null;
    Long nextLogSegmentSeqNo = null;
    for (int i = 0; i < segmentListDesc.size(); i++) {
        LogSegmentMetadata metadata = segmentListDesc.get(i);
        if (LogSegmentMetadata.supportsLogSegmentSequenceNo(metadata.getVersion())) {
            lastAssignedLogSegmentSeqNo = metadata.getLogSegmentSequenceNumber();
            lastAssignedLogSegmentIdx = i;
            break;
        }
    }
    if (null != lastAssignedLogSegmentSeqNo) {
        // latest log segment is assigned with a sequence number, start with next sequence number
        nextLogSegmentSeqNo = lastAssignedLogSegmentSeqNo + lastAssignedLogSegmentIdx + 1;
    }
    return nextLogSegmentSeqNo;
}
Also used : LogSegmentMetadata(org.apache.distributedlog.LogSegmentMetadata)

Example 45 with LogSegmentMetadata

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

the class DLUtils method findLogSegmentNotLessThanTxnId.

/**
 * Find the log segment whose transaction ids are not less than provided <code>transactionId</code>.
 *
 * @param segments
 *          segments to search
 * @param transactionId
 *          transaction id to find
 * @return the first log segment whose transaction ids are not less than <code>transactionId</code>.
 */
public static int findLogSegmentNotLessThanTxnId(List<LogSegmentMetadata> segments, long transactionId) {
    int found = -1;
    for (int i = segments.size() - 1; i >= 0; i--) {
        LogSegmentMetadata segment = segments.get(i);
        if (segment.getFirstTxId() <= transactionId) {
            found = i;
            break;
        }
    }
    if (found <= -1) {
        return -1;
    }
    if (found == 0 && segments.get(0).getFirstTxId() == transactionId) {
        return 0;
    }
    LogSegmentMetadata foundSegment = segments.get(found);
    if (foundSegment.getFirstTxId() == transactionId) {
        for (int i = found - 1; i >= 0; i--) {
            LogSegmentMetadata segment = segments.get(i);
            if (segment.isInProgress()) {
                break;
            }
            if (segment.getLastTxId() < transactionId) {
                break;
            }
            found = i;
        }
        return found;
    } else {
        if (foundSegment.isInProgress() || found == segments.size() - 1) {
            return found;
        }
        if (foundSegment.getLastTxId() >= transactionId) {
            return found;
        }
        return found + 1;
    }
}
Also used : LogSegmentMetadata(org.apache.distributedlog.LogSegmentMetadata)

Aggregations

LogSegmentMetadata (org.apache.distributedlog.LogSegmentMetadata)50 Test (org.junit.Test)31 DistributedLogManager (org.apache.distributedlog.api.DistributedLogManager)12 List (java.util.List)9 DLSN (org.apache.distributedlog.DLSN)9 DistributedLogConfiguration (org.apache.distributedlog.DistributedLogConfiguration)9 LogRecordWithDLSN (org.apache.distributedlog.LogRecordWithDLSN)8 Entry (org.apache.distributedlog.Entry)7 Versioned (org.apache.bookkeeper.versioning.Versioned)5 LogSegmentNamesListener (org.apache.distributedlog.callback.LogSegmentNamesListener)5 ZKException (org.apache.distributedlog.exceptions.ZKException)5 CompletableFuture (java.util.concurrent.CompletableFuture)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)2 OrderedScheduler (org.apache.bookkeeper.common.util.OrderedScheduler)2 DLIllegalStateException (org.apache.distributedlog.exceptions.DLIllegalStateException)2