use of org.apache.distributedlog.callback.NamespaceListener in project bookkeeper by apache.
the class TestZKNamespaceWatcher method testSessionExpired.
@Test(timeout = 60000)
public void testSessionExpired() throws Exception {
URI uri = createDLMURI("/" + runtime.getMethodName());
zkc.get().create(uri.getPath(), new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
DistributedLogConfiguration conf = new DistributedLogConfiguration();
conf.addConfiguration(baseConf);
ZKNamespaceWatcher watcher = new ZKNamespaceWatcher(conf, uri, zkc, scheduler);
final CountDownLatch[] latches = new CountDownLatch[10];
for (int i = 0; i < 10; i++) {
latches[i] = new CountDownLatch(1);
}
final AtomicInteger numUpdates = new AtomicInteger(0);
final AtomicReference<Set<String>> receivedLogs = new AtomicReference<Set<String>>(null);
watcher.registerListener(new NamespaceListener() {
@Override
public void onStreamsChanged(Iterator<String> streams) {
Set<String> streamSet = Sets.newHashSet(streams);
int updates = numUpdates.incrementAndGet();
receivedLogs.set(streamSet);
latches[updates - 1].countDown();
}
});
latches[0].await();
createLogInNamespace(uri, "test1");
latches[1].await();
createLogInNamespace(uri, "test2");
latches[2].await();
assertEquals(2, receivedLogs.get().size());
ZooKeeperClientUtils.expireSession(zkc, BKNamespaceDriver.getZKServersFromDLUri(uri), zkSessionTimeoutMs);
latches[3].await();
assertEquals(2, receivedLogs.get().size());
createLogInNamespace(uri, "test3");
latches[4].await();
assertEquals(3, receivedLogs.get().size());
}
use of org.apache.distributedlog.callback.NamespaceListener in project bookkeeper by apache.
the class TestZKNamespaceWatcher method testNamespaceListener.
@Test(timeout = 60000)
public void testNamespaceListener() throws Exception {
URI uri = createDLMURI("/" + runtime.getMethodName());
zkc.get().create(uri.getPath(), new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
DistributedLogConfiguration conf = new DistributedLogConfiguration();
conf.addConfiguration(baseConf);
ZKNamespaceWatcher watcher = new ZKNamespaceWatcher(conf, uri, zkc, scheduler);
final CountDownLatch[] latches = new CountDownLatch[10];
for (int i = 0; i < 10; i++) {
latches[i] = new CountDownLatch(1);
}
final AtomicInteger numUpdates = new AtomicInteger(0);
final AtomicReference<Set<String>> receivedLogs = new AtomicReference<Set<String>>(null);
watcher.registerListener(new NamespaceListener() {
@Override
public void onStreamsChanged(Iterator<String> streams) {
Set<String> streamSet = Sets.newHashSet(streams);
int updates = numUpdates.incrementAndGet();
receivedLogs.set(streamSet);
latches[updates - 1].countDown();
}
});
// first update
final Set<String> expectedLogs = Sets.newHashSet();
latches[0].await();
validateReceivedLogs(expectedLogs, receivedLogs.get());
// create test1
expectedLogs.add("test1");
createLogInNamespace(uri, "test1");
latches[1].await();
validateReceivedLogs(expectedLogs, receivedLogs.get());
// create invalid log
createLogInNamespace(uri, ".test1");
latches[2].await();
validateReceivedLogs(expectedLogs, receivedLogs.get());
// create test2
expectedLogs.add("test2");
createLogInNamespace(uri, "test2");
latches[3].await();
validateReceivedLogs(expectedLogs, receivedLogs.get());
// delete test1
expectedLogs.remove("test1");
deleteLogInNamespace(uri, "test1");
latches[4].await();
validateReceivedLogs(expectedLogs, receivedLogs.get());
}
use of org.apache.distributedlog.callback.NamespaceListener in project bookkeeper by apache.
the class TestBKDistributedLogNamespace method testNamespaceListener.
@Test(timeout = 60000)
public void testNamespaceListener() throws Exception {
URI uri = createDLMURI("/" + runtime.getMethodName());
zooKeeperClient.get().create(uri.getPath(), new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
Namespace namespace = NamespaceBuilder.newBuilder().conf(conf).uri(uri).build();
final CountDownLatch[] latches = new CountDownLatch[3];
for (int i = 0; i < 3; i++) {
latches[i] = new CountDownLatch(1);
}
final AtomicInteger numUpdates = new AtomicInteger(0);
final AtomicInteger numFailures = new AtomicInteger(0);
final AtomicReference<Collection<String>> receivedStreams = new AtomicReference<Collection<String>>(null);
namespace.registerNamespaceListener(new NamespaceListener() {
@Override
public void onStreamsChanged(Iterator<String> streams) {
Set<String> streamSet = Sets.newHashSet(streams);
int updates = numUpdates.incrementAndGet();
if (streamSet.size() != updates - 1) {
numFailures.incrementAndGet();
}
receivedStreams.set(streamSet);
latches[updates - 1].countDown();
}
});
latches[0].await();
namespace.createLog("test1");
latches[1].await();
namespace.createLog("test2");
latches[2].await();
assertEquals(0, numFailures.get());
assertNotNull(receivedStreams.get());
Set<String> streamSet = new HashSet<String>();
streamSet.addAll(receivedStreams.get());
assertEquals(2, receivedStreams.get().size());
assertEquals(2, streamSet.size());
assertTrue(streamSet.contains("test1"));
assertTrue(streamSet.contains("test2"));
}
use of org.apache.distributedlog.callback.NamespaceListener in project bookkeeper by apache.
the class ZKNamespaceWatcher method processResult.
@Override
public void processResult(int rc, String path, Object ctx, List<String> children, Stat stat) {
if (KeeperException.Code.OK.intValue() == rc) {
logger.info("Received updated logs under {} : {}", uri, children);
List<String> result = new ArrayList<String>(children.size());
for (String s : children) {
if (isReservedStreamName(s)) {
continue;
}
result.add(s);
}
for (NamespaceListener listener : listeners) {
listener.onStreamsChanged(result.iterator());
}
} else {
scheduleTask(this, conf.getZKSessionTimeoutMilliseconds());
}
}
Aggregations