use of com.twitter.distributedlog.LogRecordWithDLSN in project distributedlog by twitter.
the class TestLogSegmentMetadataStoreUpdater method testUpdateLastDLSN.
@Test(timeout = 60000)
public void testUpdateLastDLSN() throws Exception {
String ledgerPath = "/testUpdateLastDLSN";
zkc.get().create(ledgerPath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
// Create 1 completed log segment
LogSegmentMetadata completedLogSegment = DLMTestUtil.completedLogSegment(ledgerPath, 1L, 0L, 99L, 100, 1L, 99L, 0L);
completedLogSegment.write(zkc);
// Create 1 inprogress log segment
LogSegmentMetadata inprogressLogSegment = DLMTestUtil.inprogressLogSegment(ledgerPath, 2L, 100L, 2L);
inprogressLogSegment.write(zkc);
DLSN badLastDLSN = new DLSN(99L, 0L, 0L);
DLSN goodLastDLSN1 = new DLSN(1L, 100L, 0L);
DLSN goodLastDLSN2 = new DLSN(2L, 200L, 0L);
LogRecordWithDLSN badRecord = DLMTestUtil.getLogRecordWithDLSNInstance(badLastDLSN, 100L);
LogRecordWithDLSN goodRecord1 = DLMTestUtil.getLogRecordWithDLSNInstance(goodLastDLSN1, 100L);
LogRecordWithDLSN goodRecord2 = DLMTestUtil.getLogRecordWithDLSNInstance(goodLastDLSN2, 200L);
// Dryrun
MetadataUpdater dryrunUpdater = new DryrunLogSegmentMetadataStoreUpdater(conf, metadataStore);
try {
FutureUtils.result(dryrunUpdater.updateLastRecord(completedLogSegment, badRecord));
fail("Should fail on updating dlsn that in different log segment");
} catch (IllegalArgumentException iae) {
// expected
}
try {
FutureUtils.result(dryrunUpdater.updateLastRecord(inprogressLogSegment, goodRecord2));
fail("Should fail on updating dlsn for an inprogress log segment");
} catch (IllegalStateException ise) {
// expected
}
LogSegmentMetadata updatedCompletedLogSegment = FutureUtils.result(dryrunUpdater.updateLastRecord(completedLogSegment, goodRecord1));
assertEquals(goodLastDLSN1, updatedCompletedLogSegment.getLastDLSN());
assertEquals(goodRecord1.getTransactionId(), updatedCompletedLogSegment.getLastTxId());
assertTrue(updatedCompletedLogSegment.isRecordLastPositioninThisSegment(goodRecord1));
Map<Long, LogSegmentMetadata> segmentList = readLogSegments(ledgerPath);
assertEquals(2, segmentList.size());
LogSegmentMetadata readCompletedLogSegment = segmentList.get(1L);
assertNotNull(readCompletedLogSegment);
assertEquals(completedLogSegment, readCompletedLogSegment);
LogSegmentMetadata readInprogressLogSegment = segmentList.get(2L);
assertNotNull(readInprogressLogSegment);
assertEquals(inprogressLogSegment, readInprogressLogSegment);
// Fix the last dlsn
MetadataUpdater updater = LogSegmentMetadataStoreUpdater.createMetadataUpdater(conf, metadataStore);
try {
FutureUtils.result(updater.updateLastRecord(completedLogSegment, badRecord));
fail("Should fail on updating dlsn that in different log segment");
} catch (IllegalArgumentException iae) {
// expected
}
try {
FutureUtils.result(updater.updateLastRecord(inprogressLogSegment, goodRecord2));
fail("Should fail on updating dlsn for an inprogress log segment");
} catch (IllegalStateException ise) {
// expected
}
updatedCompletedLogSegment = FutureUtils.result(updater.updateLastRecord(completedLogSegment, goodRecord1));
assertEquals(goodLastDLSN1, updatedCompletedLogSegment.getLastDLSN());
assertEquals(goodRecord1.getTransactionId(), updatedCompletedLogSegment.getLastTxId());
assertTrue(updatedCompletedLogSegment.isRecordLastPositioninThisSegment(goodRecord1));
segmentList = readLogSegments(ledgerPath);
assertEquals(2, segmentList.size());
readCompletedLogSegment = segmentList.get(1L);
assertNotNull(readCompletedLogSegment);
assertEquals(goodLastDLSN1, readCompletedLogSegment.getLastDLSN());
assertEquals(goodRecord1.getTransactionId(), readCompletedLogSegment.getLastTxId());
assertTrue(readCompletedLogSegment.isRecordLastPositioninThisSegment(goodRecord1));
assertEquals(updatedCompletedLogSegment, readCompletedLogSegment);
assertEquals(completedLogSegment.getCompletionTime(), readCompletedLogSegment.getCompletionTime());
assertEquals(completedLogSegment.getFirstTxId(), readCompletedLogSegment.getFirstTxId());
assertEquals(completedLogSegment.getLedgerId(), readCompletedLogSegment.getLedgerId());
assertEquals(completedLogSegment.getLogSegmentSequenceNumber(), readCompletedLogSegment.getLogSegmentSequenceNumber());
assertEquals(completedLogSegment.getRegionId(), readCompletedLogSegment.getRegionId());
assertEquals(completedLogSegment.getZkPath(), readCompletedLogSegment.getZkPath());
assertEquals(completedLogSegment.getZNodeName(), readCompletedLogSegment.getZNodeName());
readInprogressLogSegment = segmentList.get(2L);
assertNotNull(readInprogressLogSegment);
assertEquals(inprogressLogSegment, readInprogressLogSegment);
}
Aggregations