use of org.apache.bookkeeper.net.BookieSocketAddress in project bookkeeper by apache.
the class TestZkRegistrationClient method testWatchBookiesTwice.
private void testWatchBookiesTwice(boolean isWritable) throws Exception {
int zkCallbackDelayMs = 100;
Set<BookieSocketAddress> addresses = prepareNBookies(10);
List<String> children = Lists.newArrayList();
for (BookieSocketAddress address : addresses) {
children.add(address.toString());
}
Stat stat = mock(Stat.class);
when(stat.getCversion()).thenReturn(1234);
mockGetChildren(isWritable ? regPath : regReadonlyPath, true, Code.OK.intValue(), children, stat, zkCallbackDelayMs);
CompletableFuture<Versioned<Set<BookieSocketAddress>>> firstResult = new CompletableFuture<>();
RegistrationListener firstListener = bookies -> firstResult.complete(bookies);
CompletableFuture<Versioned<Set<BookieSocketAddress>>> secondResult = new CompletableFuture<>();
RegistrationListener secondListener = bookies -> secondResult.complete(bookies);
List<CompletableFuture<Void>> watchFutures = Lists.newArrayListWithExpectedSize(2);
if (isWritable) {
watchFutures.add(zkRegistrationClient.watchWritableBookies(firstListener));
watchFutures.add(zkRegistrationClient.watchWritableBookies(secondListener));
} else {
watchFutures.add(zkRegistrationClient.watchReadOnlyBookies(firstListener));
watchFutures.add(zkRegistrationClient.watchReadOnlyBookies(secondListener));
}
// trigger zkCallbackExecutor to execute getChildren callback
zkCallbackController.advance(Duration.ofMillis(zkCallbackDelayMs));
result(collect(watchFutures));
assertEquals(firstResult.get().getVersion(), secondResult.get().getVersion());
assertSetEquals(firstResult.get().getValue(), secondResult.get().getValue());
}
use of org.apache.bookkeeper.net.BookieSocketAddress in project bookkeeper by apache.
the class TestZkRegistrationClient method testWatchBookiesSuccess.
@SuppressWarnings("unchecked")
private void testWatchBookiesSuccess(boolean isWritable) throws Exception {
//
// 1. test watch bookies with a listener
//
LinkedBlockingQueue<Versioned<Set<BookieSocketAddress>>> updates = spy(new LinkedBlockingQueue<>());
RegistrationListener listener = bookies -> {
try {
updates.put(bookies);
} catch (InterruptedException e) {
log.warn("Interrupted on enqueue bookie updates", e);
}
};
Set<BookieSocketAddress> addresses = prepareNBookies(10);
List<String> children = Lists.newArrayList();
for (BookieSocketAddress address : addresses) {
children.add(address.toString());
}
Stat stat = mock(Stat.class);
when(stat.getCversion()).thenReturn(1234);
mockGetChildren(isWritable ? regPath : regReadonlyPath, true, Code.OK.intValue(), children, stat);
if (isWritable) {
result(zkRegistrationClient.watchWritableBookies(listener));
} else {
result(zkRegistrationClient.watchReadOnlyBookies(listener));
}
Versioned<Set<BookieSocketAddress>> update = updates.take();
verify(updates, times(1)).put(any(Versioned.class));
assertEquals(new LongVersion(1234), update.getVersion());
assertSetEquals(addresses, update.getValue());
verify(mockZk, times(1)).getChildren(anyString(), any(Watcher.class), any(Children2Callback.class), any());
//
// 2. test watch bookies with a second listener. the second listener returns cached bookies
// without calling `getChildren` again
//
// register another listener
LinkedBlockingQueue<Versioned<Set<BookieSocketAddress>>> secondUpdates = spy(new LinkedBlockingQueue<>());
RegistrationListener secondListener = bookies -> {
try {
secondUpdates.put(bookies);
} catch (InterruptedException e) {
log.warn("Interrupted on enqueue bookie updates", e);
}
};
if (isWritable) {
result(zkRegistrationClient.watchWritableBookies(secondListener));
} else {
result(zkRegistrationClient.watchReadOnlyBookies(secondListener));
}
Versioned<Set<BookieSocketAddress>> secondListenerUpdate = secondUpdates.take();
// first listener will not be notified with any update
verify(updates, times(1)).put(any(Versioned.class));
// second listener will receive same update as the first listener received before
verify(secondUpdates, times(1)).put(any(Versioned.class));
assertSame(update.getVersion(), secondListenerUpdate.getVersion());
assertSame(update.getValue(), secondListenerUpdate.getValue());
// the second listener will return the cached value without issuing another getChildren call
verify(mockZk, times(1)).getChildren(anyString(), any(Watcher.class), any(Children2Callback.class), any());
//
// 3. simulate session expire, it will trigger watcher to refetch bookies again.
// but since there is no updates on bookies, the registered listeners will not be notified.
//
notifyWatchedEvent(EventType.None, KeeperState.Expired, isWritable ? regPath : regReadonlyPath);
// if session expires, the watcher task will get into backoff state
controller.advance(Duration.ofMillis(ZK_CONNECT_BACKOFF_MS));
// the same updates returns, the getChildren calls increase to 2
// but since there is no updates, so no notification is sent.
verify(mockZk, times(2)).getChildren(anyString(), any(Watcher.class), any(Children2Callback.class), any());
assertNull(updates.poll());
// both listener and secondListener will not receive any old update
verify(updates, times(1)).put(any(Versioned.class));
verify(secondUpdates, times(1)).put(any(Versioned.class));
//
// 4. notify with new bookies. both listeners will be notified with new bookies.
//
Set<BookieSocketAddress> newAddresses = prepareNBookies(20);
List<String> newChildren = Lists.newArrayList();
for (BookieSocketAddress address : newAddresses) {
newChildren.add(address.toString());
}
Stat newStat = mock(Stat.class);
when(newStat.getCversion()).thenReturn(1235);
mockGetChildren(isWritable ? regPath : regReadonlyPath, true, Code.OK.intValue(), newChildren, newStat);
// trigger watcher
notifyWatchedEvent(EventType.NodeChildrenChanged, KeeperState.SyncConnected, isWritable ? regPath : regReadonlyPath);
update = updates.take();
assertEquals(new LongVersion(1235), update.getVersion());
assertSetEquals(newAddresses, update.getValue());
secondListenerUpdate = secondUpdates.take();
assertSame(update.getVersion(), secondListenerUpdate.getVersion());
assertSame(update.getValue(), secondListenerUpdate.getValue());
verify(mockZk, times(3)).getChildren(anyString(), any(Watcher.class), any(Children2Callback.class), any());
verify(updates, times(2)).put(any(Versioned.class));
verify(secondUpdates, times(2)).put(any(Versioned.class));
//
// 5. unwatch the second listener and notify with new bookies again. only first listener will
// be notified with new bookies.
//
newAddresses = prepareNBookies(25);
newChildren.clear();
newChildren = Lists.newArrayList();
for (BookieSocketAddress address : newAddresses) {
newChildren.add(address.toString());
}
newStat = mock(Stat.class);
when(newStat.getCversion()).thenReturn(1236);
mockGetChildren(isWritable ? regPath : regReadonlyPath, true, Code.OK.intValue(), newChildren, newStat);
if (isWritable) {
assertEquals(2, zkRegistrationClient.getWatchWritableBookiesTask().getNumListeners());
zkRegistrationClient.unwatchWritableBookies(secondListener);
assertEquals(1, zkRegistrationClient.getWatchWritableBookiesTask().getNumListeners());
} else {
assertEquals(2, zkRegistrationClient.getWatchReadOnlyBookiesTask().getNumListeners());
zkRegistrationClient.unwatchReadOnlyBookies(secondListener);
assertEquals(1, zkRegistrationClient.getWatchReadOnlyBookiesTask().getNumListeners());
}
// the watch task will not be closed since there is still a listener
verify(mockZk, times(0)).removeWatches(eq(isWritable ? regPath : regReadonlyPath), same(isWritable ? zkRegistrationClient.getWatchWritableBookiesTask() : zkRegistrationClient.getWatchReadOnlyBookiesTask()), eq(WatcherType.Children), eq(true), any(VoidCallback.class), any());
// trigger watcher
notifyWatchedEvent(EventType.NodeChildrenChanged, KeeperState.SyncConnected, isWritable ? regPath : regReadonlyPath);
update = updates.take();
assertEquals(new LongVersion(1236), update.getVersion());
assertSetEquals(newAddresses, update.getValue());
secondListenerUpdate = secondUpdates.poll();
assertNull(secondListenerUpdate);
verify(mockZk, times(4)).getChildren(anyString(), any(Watcher.class), any(Children2Callback.class), any());
verify(updates, times(3)).put(any(Versioned.class));
verify(secondUpdates, times(2)).put(any(Versioned.class));
//
// 6. unwatch the first listener. the watch task will be closed and zk watcher will be removed.
//
//
WatchTask expectedWatcher;
if (isWritable) {
expectedWatcher = zkRegistrationClient.getWatchWritableBookiesTask();
assertFalse(expectedWatcher.isClosed());
zkRegistrationClient.unwatchWritableBookies(listener);
assertNull(zkRegistrationClient.getWatchWritableBookiesTask());
} else {
expectedWatcher = zkRegistrationClient.getWatchReadOnlyBookiesTask();
assertFalse(expectedWatcher.isClosed());
zkRegistrationClient.unwatchReadOnlyBookies(listener);
assertNull(zkRegistrationClient.getWatchReadOnlyBookiesTask());
}
// the watch task will not be closed since there is still a listener
assertTrue(expectedWatcher.isClosed());
verify(mockZk, times(1)).removeWatches(eq(isWritable ? regPath : regReadonlyPath), same(expectedWatcher), eq(WatcherType.Children), eq(true), any(VoidCallback.class), any());
}
use of org.apache.bookkeeper.net.BookieSocketAddress in project bookkeeper by apache.
the class TestZkRegistrationClient method testGetWritableBookies.
@Test
public void testGetWritableBookies() throws Exception {
Set<BookieSocketAddress> addresses = prepareNBookies(10);
List<String> children = Lists.newArrayList();
for (BookieSocketAddress address : addresses) {
children.add(address.toString());
}
Stat stat = mock(Stat.class);
when(stat.getCversion()).thenReturn(1234);
mockGetChildren(regPath, false, Code.OK.intValue(), children, stat);
Versioned<Set<BookieSocketAddress>> result = result(zkRegistrationClient.getWritableBookies());
assertEquals(new LongVersion(1234), result.getVersion());
assertSetEquals(addresses, result.getValue());
}
use of org.apache.bookkeeper.net.BookieSocketAddress in project bookkeeper by apache.
the class LedgerRecoveryTest method ledgerRecoveryWithSlowBookie.
private void ledgerRecoveryWithSlowBookie(int ensembleSize, int writeQuorumSize, int ackQuorumSize, int numEntries, int slowBookieIdx) throws Exception {
// Create a ledger
LedgerHandle beforelh = null;
beforelh = bkc.createLedger(ensembleSize, writeQuorumSize, ackQuorumSize, digestType, "".getBytes());
// kill first bookie server to start a fake one to simulate a slow bookie
// and failed to add entry on crash
// until write succeed
BookieSocketAddress host = beforelh.getLedgerMetadata().currentEnsemble.get(slowBookieIdx);
ServerConfiguration conf = killBookie(host);
Bookie fakeBookie = new Bookie(conf) {
@Override
public void addEntry(ByteBuf entry, boolean ackBeforeSync, WriteCallback cb, Object ctx, byte[] masterKey) throws IOException, BookieException {
// drop request to simulate a slow and failed bookie
}
};
bsConfs.add(conf);
bs.add(startBookie(conf, fakeBookie));
// avoid not-enough-bookies case
startNewBookie();
// write would still succeed with 2 bookies ack
String tmp = "BookKeeper is cool!";
for (int i = 0; i < numEntries; i++) {
beforelh.addEntry(tmp.getBytes());
}
conf = killBookie(host);
bsConfs.add(conf);
// the bookie goes normally
bs.add(startBookie(conf));
/*
* Try to open ledger.
*/
LedgerHandle afterlh = bkc.openLedger(beforelh.getId(), digestType, "".getBytes());
/*
* Check if has recovered properly.
*/
assertEquals(numEntries - 1, afterlh.getLastAddConfirmed());
}
use of org.apache.bookkeeper.net.BookieSocketAddress in project bookkeeper by apache.
the class BookieWriteLedgerTest method testLedgerCreateAdv.
/**
* Verify the functionality of Advanced Ledger which returns
* LedgerHandleAdv. LedgerHandleAdv takes entryId for addEntry, and let
* user manage entryId allocation.
*
* @throws Exception
*/
@Test
public void testLedgerCreateAdv() throws Exception {
// Create a ledger
lh = bkc.createLedgerAdv(5, 3, 2, digestType, ledgerPassword);
for (int i = 0; i < numEntriesToWrite; i++) {
ByteBuffer entry = ByteBuffer.allocate(4);
entry.putInt(rng.nextInt(maxInt));
entry.position(0);
entries1.add(entry.array());
lh.addEntry(i, entry.array());
}
// Start one more bookies
startNewBookie();
// Shutdown one bookie in the last ensemble and continue writing
ArrayList<BookieSocketAddress> ensemble = lh.getLedgerMetadata().getEnsembles().entrySet().iterator().next().getValue();
killBookie(ensemble.get(0));
int i = numEntriesToWrite;
numEntriesToWrite = numEntriesToWrite + 50;
for (; i < numEntriesToWrite; i++) {
ByteBuffer entry = ByteBuffer.allocate(4);
entry.putInt(rng.nextInt(maxInt));
entry.position(0);
entries1.add(entry.array());
lh.addEntry(i, entry.array());
}
readEntries(lh, entries1);
lh.close();
}
Aggregations