use of org.apache.distributedlog.LogSegmentMetadata in project bookkeeper by apache.
the class TestZKLogSegmentMetadataStore method testDeleteLogSegment.
@Test(timeout = 60000)
public void testDeleteLogSegment() throws Exception {
LogSegmentMetadata segment = createLogSegment(1L);
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));
Transaction<Object> deleteTxn = lsmStore.transaction();
lsmStore.deleteLogSegment(deleteTxn, segment, null);
Utils.ioResult(deleteTxn.execute());
assertNull("LogSegment " + segment + " should be deleted", zkc.get().exists(segment.getZkPath(), false));
}
use of org.apache.distributedlog.LogSegmentMetadata in project bookkeeper by apache.
the class TestZKLogSegmentMetadataStore method testLogSegmentNamesListenerOnDeletingLogStream.
@Test(timeout = 60000)
public void testLogSegmentNamesListenerOnDeletingLogStream() throws Exception {
int numSegments = 3;
Transaction<Object> createTxn = lsmStore.transaction();
for (int i = 0; i < numSegments; i++) {
LogSegmentMetadata segment = createLogSegment(i);
lsmStore.createLogSegment(createTxn, segment, null);
}
Utils.ioResult(createTxn.execute());
String rootPath = "/" + runtime.getMethodName();
List<String> children = zkc.get().getChildren(rootPath, false);
Collections.sort(children);
final AtomicInteger numNotifications = new AtomicInteger(0);
final List<List<String>> segmentLists = Lists.newArrayListWithExpectedSize(2);
final CountDownLatch deleteLatch = new CountDownLatch(1);
LogSegmentNamesListener listener = new LogSegmentNamesListener() {
@Override
public void onSegmentsUpdated(Versioned<List<String>> segments) {
logger.info("Received segments : {}", segments);
segmentLists.add(segments.getValue());
numNotifications.incrementAndGet();
}
@Override
public void onLogStreamDeleted() {
deleteLatch.countDown();
}
};
lsmStore.getLogSegmentNames(rootPath, listener);
assertEquals(1, lsmStore.listeners.size());
assertTrue("Should contain listener", lsmStore.listeners.containsKey(rootPath));
assertTrue("Should contain listener", lsmStore.listeners.get(rootPath).containsKey(listener));
while (numNotifications.get() < 1) {
TimeUnit.MILLISECONDS.sleep(10);
}
assertEquals("Should receive one segment list update", 1, numNotifications.get());
List<String> firstSegmentList = segmentLists.get(0);
Collections.sort(firstSegmentList);
assertEquals("List of segments should be same", children, firstSegmentList);
// delete all log segments, it should trigger segment list updated
Transaction<Object> deleteTxn = lsmStore.transaction();
for (int i = 0; i < numSegments; i++) {
LogSegmentMetadata segment = createLogSegment(i);
lsmStore.deleteLogSegment(deleteTxn, segment, null);
}
Utils.ioResult(deleteTxn.execute());
List<String> newChildren = zkc.get().getChildren(rootPath, false);
Collections.sort(newChildren);
while (numNotifications.get() < 2) {
TimeUnit.MILLISECONDS.sleep(10);
}
assertEquals("Should receive second segment list update", 2, numNotifications.get());
List<String> secondSegmentList = segmentLists.get(1);
Collections.sort(secondSegmentList);
assertEquals("List of segments should be updated", 0, secondSegmentList.size());
assertEquals("List of segments should be updated", newChildren, secondSegmentList);
// delete the root path
zkc.get().delete(rootPath, -1);
while (!lsmStore.listeners.isEmpty()) {
TimeUnit.MILLISECONDS.sleep(10);
}
assertTrue("listener should be removed after root path is deleted", lsmStore.listeners.isEmpty());
deleteLatch.await();
}
use of org.apache.distributedlog.LogSegmentMetadata in project bookkeeper by apache.
the class TestZKLogSegmentMetadataStore method testLogSegmentNamesListenerOnSessionExpired.
@Test(timeout = 60000)
public void testLogSegmentNamesListenerOnSessionExpired() throws Exception {
int numSegments = 3;
Transaction<Object> createTxn = lsmStore.transaction();
for (int i = 0; i < numSegments; i++) {
LogSegmentMetadata segment = createLogSegment(i);
lsmStore.createLogSegment(createTxn, segment, null);
}
Utils.ioResult(createTxn.execute());
String rootPath = "/" + runtime.getMethodName();
List<String> children = zkc.get().getChildren(rootPath, false);
Collections.sort(children);
final AtomicInteger numNotifications = new AtomicInteger(0);
final List<List<String>> segmentLists = Lists.newArrayListWithExpectedSize(2);
LogSegmentNamesListener listener = new LogSegmentNamesListener() {
@Override
public void onSegmentsUpdated(Versioned<List<String>> segments) {
logger.info("Received segments : {}", segments);
segmentLists.add(segments.getValue());
numNotifications.incrementAndGet();
}
@Override
public void onLogStreamDeleted() {
// no-op;
}
};
lsmStore.getLogSegmentNames(rootPath, listener);
assertEquals(1, lsmStore.listeners.size());
assertTrue("Should contain listener", lsmStore.listeners.containsKey(rootPath));
assertTrue("Should contain listener", lsmStore.listeners.get(rootPath).containsKey(listener));
while (numNotifications.get() < 1) {
TimeUnit.MILLISECONDS.sleep(10);
}
assertEquals("Should receive one segment list update", 1, numNotifications.get());
List<String> firstSegmentList = segmentLists.get(0);
Collections.sort(firstSegmentList);
assertEquals("List of segments should be same", children, firstSegmentList);
ZooKeeperClientUtils.expireSession(zkc, BKNamespaceDriver.getZKServersFromDLUri(uri), conf.getZKSessionTimeoutMilliseconds());
logger.info("Create another {} segments.", numSegments);
// create another log segment, it should trigger segment list updated
Transaction<Object> anotherCreateTxn = lsmStore.transaction();
for (int i = numSegments; i < 2 * numSegments; i++) {
LogSegmentMetadata segment = createLogSegment(i);
lsmStore.createLogSegment(anotherCreateTxn, segment, null);
}
Utils.ioResult(anotherCreateTxn.execute());
List<String> newChildren = zkc.get().getChildren(rootPath, false);
Collections.sort(newChildren);
logger.info("All log segments become {}", newChildren);
while (numNotifications.get() < 2) {
TimeUnit.MILLISECONDS.sleep(10);
}
assertEquals("Should receive third segment list update", 2, numNotifications.get());
List<String> thirdSegmentList = segmentLists.get(1);
Collections.sort(thirdSegmentList);
assertEquals("List of segments should be updated", 2 * numSegments, thirdSegmentList.size());
assertEquals("List of segments should be updated", newChildren, thirdSegmentList);
}
use of org.apache.distributedlog.LogSegmentMetadata in project bookkeeper by apache.
the class TestZKLogSegmentMetadataStore method testDeleteNonExistentLogSegment.
@Test(timeout = 60000)
public void testDeleteNonExistentLogSegment() throws Exception {
LogSegmentMetadata segment = createLogSegment(1L);
Transaction<Object> deleteTxn = lsmStore.transaction();
lsmStore.deleteLogSegment(deleteTxn, segment, null);
try {
Utils.ioResult(deleteTxn.execute());
fail("Should fail deletion if log segment doesn't exist");
} catch (Throwable t) {
assertTrue("Should throw NoNodeException if log segment doesn't exist", t instanceof ZKException);
ZKException zke = (ZKException) t;
assertEquals("Should throw NoNodeException if log segment doesn't exist", KeeperException.Code.NONODE, zke.getKeeperExceptionCode());
}
}
use of org.apache.distributedlog.LogSegmentMetadata in project bookkeeper by apache.
the class TestZKLogSegmentMetadataStore method testUpdateLogSegment.
@Test(timeout = 60000)
public void testUpdateLogSegment() 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 modifiedSegment = createLogSegment(1L, 999L);
Transaction<Object> updateTxn = lsmStore.transaction();
lsmStore.updateLogSegment(updateTxn, modifiedSegment);
Utils.ioResult(updateTxn.execute());
// the log segment should be updated
LogSegmentMetadata readSegment = Utils.ioResult(LogSegmentMetadata.read(zkc, segment.getZkPath(), true));
assertEquals("Last entry id should be changed from 99L to 999L", 999L, readSegment.getLastEntryId());
}
Aggregations