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));
}
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);
}
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));
}
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;
}
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;
}
}
Aggregations