use of org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.LedgerMetadataListener in project bookkeeper by apache.
the class AbstractZkLedgerManagerTest method testLedgerMetadataListenerOnLedgerDeletedEvent.
@Test
public void testLedgerMetadataListenerOnLedgerDeletedEvent() throws Exception {
long ledgerId = System.currentTimeMillis();
String ledgerStr = String.valueOf(ledgerId);
LinkedBlockingQueue<Optional<LedgerMetadata>> changes = new LinkedBlockingQueue<>();
LedgerMetadataListener listener = (ledgerId1, metadata) -> changes.add(Optional.ofNullable(metadata));
metadata.setVersion(new LongVersion(1234L));
Stat stat = mock(Stat.class);
when(stat.getVersion()).thenReturn(1234);
when(stat.getCtime()).thenReturn(metadata.getCtime());
mockZkGetData(ledgerStr, true, KeeperException.Code.OK.intValue(), metadata.serialize(), stat);
ledgerManager.registerLedgerMetadataListener(ledgerId, listener);
assertTrue(ledgerManager.listeners.containsKey(ledgerId));
// the listener will be notified with first get
LedgerMetadata change1 = changes.take().get();
assertEquals(metadata, change1);
verify(mockZk, times(1)).getData(anyString(), any(Watcher.class), any(DataCallback.class), any());
// the watcher is registered for receiving watched event
assertTrue(watchers.containsKey(ledgerStr));
// notify the watcher event
notifyWatchedEvent(EventType.NodeDeleted, KeeperState.SyncConnected, ledgerStr);
// the listener should be removed from listener set and a null change is notified.
Optional<LedgerMetadata> change2 = changes.take();
assertFalse(change2.isPresent());
// no more `getData` is called.
verify(mockZk, times(1)).getData(anyString(), any(Watcher.class), any(DataCallback.class), any());
// listener is automatically unregistered after a ledger is deleted.
assertFalse(ledgerManager.listeners.containsKey(ledgerId));
}
use of org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.LedgerMetadataListener in project bookkeeper by apache.
the class AbstractZkLedgerManagerTest method testLedgerMetadataListenerOnSessionExpired.
@Test
public void testLedgerMetadataListenerOnSessionExpired() throws Exception {
long ledgerId = System.currentTimeMillis();
String ledgerStr = String.valueOf(ledgerId);
LinkedBlockingQueue<LedgerMetadata> changes = new LinkedBlockingQueue<>();
LedgerMetadataListener listener = (ledgerId1, metadata) -> changes.add(metadata);
metadata.setVersion(new LongVersion(1234L));
Stat stat = mock(Stat.class);
when(stat.getVersion()).thenReturn(1234);
when(stat.getCtime()).thenReturn(metadata.getCtime());
mockZkGetData(ledgerStr, true, KeeperException.Code.OK.intValue(), metadata.serialize(), stat);
ledgerManager.registerLedgerMetadataListener(ledgerId, listener);
// the listener will be notified with first get
LedgerMetadata change1 = changes.take();
assertEquals(metadata, change1);
verify(mockZk, times(1)).getData(anyString(), any(Watcher.class), any(DataCallback.class), any());
// the watcher is registered for receiving watched event
assertTrue(watchers.containsKey(ledgerStr));
Set<Watcher> watcherSet1 = watchers.get(ledgerStr);
assertEquals(1, watcherSet1.size());
Watcher registeredWatcher1 = watcherSet1.stream().findFirst().get();
// simulate session expired
notifyWatchedEvent(EventType.None, KeeperState.Expired, ledgerStr);
// ledger manager will retry to read metadata again
LedgerMetadata change2 = changes.take();
assertEquals(metadata, change2);
verify(mockZk, times(2)).getData(anyString(), any(Watcher.class), any(DataCallback.class), any());
// the watcher is registered for receiving watched event
assertTrue(watchers.containsKey(ledgerStr));
Set<Watcher> watcherSet2 = watchers.get(ledgerStr);
assertEquals(1, watcherSet2.size());
Watcher registeredWatcher2 = watcherSet2.stream().findFirst().get();
assertSame(registeredWatcher1, registeredWatcher2);
}
use of org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.LedgerMetadataListener in project bookkeeper by apache.
the class AbstractZkLedgerManagerTest method testLedgerMetadataListenerOnLedgerDeleted.
@Test
public void testLedgerMetadataListenerOnLedgerDeleted() throws Exception {
long ledgerId = System.currentTimeMillis();
String ledgerStr = String.valueOf(ledgerId);
LinkedBlockingQueue<Optional<LedgerMetadata>> changes = new LinkedBlockingQueue<>();
LedgerMetadataListener listener = (ledgerId1, metadata) -> changes.add(Optional.ofNullable(metadata));
metadata.setVersion(new LongVersion(1234L));
Stat stat = mock(Stat.class);
when(stat.getVersion()).thenReturn(1234);
when(stat.getCtime()).thenReturn(metadata.getCtime());
mockZkGetData(ledgerStr, true, KeeperException.Code.OK.intValue(), metadata.serialize(), stat);
ledgerManager.registerLedgerMetadataListener(ledgerId, listener);
assertTrue(ledgerManager.listeners.containsKey(ledgerId));
// the listener will be notified with first get
LedgerMetadata change1 = changes.take().get();
assertEquals(metadata, change1);
verify(mockZk, times(1)).getData(anyString(), any(Watcher.class), any(DataCallback.class), any());
// the watcher is registered for receiving watched event
assertTrue(watchers.containsKey(ledgerStr));
// mock get data to simulate an ledger is deleted
mockZkGetData(ledgerStr, true, KeeperException.Code.NONODE.intValue(), null, null);
// notify the watcher event
notifyWatchedEvent(EventType.NodeDataChanged, KeeperState.SyncConnected, ledgerStr);
// the listener should be removed from listener set and not receive an updated metadata anymore
Optional<LedgerMetadata> change2 = changes.take();
assertFalse(change2.isPresent());
assertFalse(ledgerManager.listeners.containsKey(ledgerId));
// verify scheduler: the listener is only triggered once
verify(scheduler, times(1)).submit(any(Runnable.class));
verify(scheduler, times(0)).schedule(any(Runnable.class), anyLong(), any(TimeUnit.class));
// no watcher is registered
assertFalse(watchers.containsKey(ledgerStr));
}
Aggregations