Search in sources :

Example 6 with LedgerMetadataListener

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));
}
Also used : CreateMode(org.apache.zookeeper.CreateMode) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) VoidCallback(org.apache.zookeeper.AsyncCallback.VoidCallback) Stat(org.apache.zookeeper.data.Stat) Code(org.apache.bookkeeper.client.BKException.Code) CALLS_REAL_METHODS(org.mockito.Mockito.CALLS_REAL_METHODS) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Duration(java.time.Duration) After(org.junit.After) KeeperState(org.apache.zookeeper.Watcher.Event.KeeperState) Assert.fail(org.junit.Assert.fail) MockZooKeeperTestCase(org.apache.bookkeeper.zookeeper.MockZooKeeperTestCase) DigestType(org.apache.bookkeeper.client.BookKeeper.DigestType) LedgerMetadataListener(org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.LedgerMetadataListener) Set(java.util.Set) StatCallback(org.apache.zookeeper.AsyncCallback.StatCallback) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Executors(java.util.concurrent.Executors) BKException(org.apache.bookkeeper.client.BKException) ZkUtils(org.apache.bookkeeper.util.ZkUtils) MockExecutorController(org.apache.bookkeeper.common.testing.executors.MockExecutorController) Assert.assertFalse(org.junit.Assert.assertFalse) Optional(java.util.Optional) Mockito.withSettings(org.mockito.Mockito.withSettings) Version(org.apache.bookkeeper.versioning.Version) Mockito.mock(org.mockito.Mockito.mock) GenericCallbackFuture(org.apache.bookkeeper.test.TestCallbacks.GenericCallbackFuture) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) LongVersion(org.apache.bookkeeper.versioning.LongVersion) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) RunWith(org.junit.runner.RunWith) LedgerMetadata(org.apache.bookkeeper.client.LedgerMetadata) Assert.assertSame(org.junit.Assert.assertSame) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) DataCallback(org.apache.zookeeper.AsyncCallback.DataCallback) PowerMockRunner(org.powermock.modules.junit4.PowerMockRunner) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) PowerMockito(org.powermock.api.mockito.PowerMockito) Before(org.junit.Before) ZK_CONNECT_BACKOFF_MS(org.apache.bookkeeper.meta.AbstractZkLedgerManager.ZK_CONNECT_BACKOFF_MS) KeeperException(org.apache.zookeeper.KeeperException) Watcher(org.apache.zookeeper.Watcher) Assert.assertTrue(org.junit.Assert.assertTrue) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) Assert.assertNull(org.junit.Assert.assertNull) FutureUtils.result(org.apache.bookkeeper.common.concurrent.FutureUtils.result) ClientConfiguration(org.apache.bookkeeper.conf.ClientConfiguration) EventType(org.apache.zookeeper.Watcher.Event.EventType) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Optional(java.util.Optional) Watcher(org.apache.zookeeper.Watcher) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) DataCallback(org.apache.zookeeper.AsyncCallback.DataCallback) Stat(org.apache.zookeeper.data.Stat) LedgerMetadata(org.apache.bookkeeper.client.LedgerMetadata) LongVersion(org.apache.bookkeeper.versioning.LongVersion) LedgerMetadataListener(org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.LedgerMetadataListener) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 7 with LedgerMetadataListener

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);
}
Also used : CreateMode(org.apache.zookeeper.CreateMode) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) VoidCallback(org.apache.zookeeper.AsyncCallback.VoidCallback) Stat(org.apache.zookeeper.data.Stat) Code(org.apache.bookkeeper.client.BKException.Code) CALLS_REAL_METHODS(org.mockito.Mockito.CALLS_REAL_METHODS) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Duration(java.time.Duration) After(org.junit.After) KeeperState(org.apache.zookeeper.Watcher.Event.KeeperState) Assert.fail(org.junit.Assert.fail) MockZooKeeperTestCase(org.apache.bookkeeper.zookeeper.MockZooKeeperTestCase) DigestType(org.apache.bookkeeper.client.BookKeeper.DigestType) LedgerMetadataListener(org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.LedgerMetadataListener) Set(java.util.Set) StatCallback(org.apache.zookeeper.AsyncCallback.StatCallback) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Executors(java.util.concurrent.Executors) BKException(org.apache.bookkeeper.client.BKException) ZkUtils(org.apache.bookkeeper.util.ZkUtils) MockExecutorController(org.apache.bookkeeper.common.testing.executors.MockExecutorController) Assert.assertFalse(org.junit.Assert.assertFalse) Optional(java.util.Optional) Mockito.withSettings(org.mockito.Mockito.withSettings) Version(org.apache.bookkeeper.versioning.Version) Mockito.mock(org.mockito.Mockito.mock) GenericCallbackFuture(org.apache.bookkeeper.test.TestCallbacks.GenericCallbackFuture) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) LongVersion(org.apache.bookkeeper.versioning.LongVersion) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) RunWith(org.junit.runner.RunWith) LedgerMetadata(org.apache.bookkeeper.client.LedgerMetadata) Assert.assertSame(org.junit.Assert.assertSame) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) DataCallback(org.apache.zookeeper.AsyncCallback.DataCallback) PowerMockRunner(org.powermock.modules.junit4.PowerMockRunner) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) PowerMockito(org.powermock.api.mockito.PowerMockito) Before(org.junit.Before) ZK_CONNECT_BACKOFF_MS(org.apache.bookkeeper.meta.AbstractZkLedgerManager.ZK_CONNECT_BACKOFF_MS) KeeperException(org.apache.zookeeper.KeeperException) Watcher(org.apache.zookeeper.Watcher) Assert.assertTrue(org.junit.Assert.assertTrue) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) Assert.assertNull(org.junit.Assert.assertNull) FutureUtils.result(org.apache.bookkeeper.common.concurrent.FutureUtils.result) ClientConfiguration(org.apache.bookkeeper.conf.ClientConfiguration) EventType(org.apache.zookeeper.Watcher.Event.EventType) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Stat(org.apache.zookeeper.data.Stat) LedgerMetadata(org.apache.bookkeeper.client.LedgerMetadata) LongVersion(org.apache.bookkeeper.versioning.LongVersion) Watcher(org.apache.zookeeper.Watcher) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) DataCallback(org.apache.zookeeper.AsyncCallback.DataCallback) LedgerMetadataListener(org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.LedgerMetadataListener) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 8 with LedgerMetadataListener

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));
}
Also used : CreateMode(org.apache.zookeeper.CreateMode) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) VoidCallback(org.apache.zookeeper.AsyncCallback.VoidCallback) Stat(org.apache.zookeeper.data.Stat) Code(org.apache.bookkeeper.client.BKException.Code) CALLS_REAL_METHODS(org.mockito.Mockito.CALLS_REAL_METHODS) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Duration(java.time.Duration) After(org.junit.After) KeeperState(org.apache.zookeeper.Watcher.Event.KeeperState) Assert.fail(org.junit.Assert.fail) MockZooKeeperTestCase(org.apache.bookkeeper.zookeeper.MockZooKeeperTestCase) DigestType(org.apache.bookkeeper.client.BookKeeper.DigestType) LedgerMetadataListener(org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.LedgerMetadataListener) Set(java.util.Set) StatCallback(org.apache.zookeeper.AsyncCallback.StatCallback) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Executors(java.util.concurrent.Executors) BKException(org.apache.bookkeeper.client.BKException) ZkUtils(org.apache.bookkeeper.util.ZkUtils) MockExecutorController(org.apache.bookkeeper.common.testing.executors.MockExecutorController) Assert.assertFalse(org.junit.Assert.assertFalse) Optional(java.util.Optional) Mockito.withSettings(org.mockito.Mockito.withSettings) Version(org.apache.bookkeeper.versioning.Version) Mockito.mock(org.mockito.Mockito.mock) GenericCallbackFuture(org.apache.bookkeeper.test.TestCallbacks.GenericCallbackFuture) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) LongVersion(org.apache.bookkeeper.versioning.LongVersion) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) RunWith(org.junit.runner.RunWith) LedgerMetadata(org.apache.bookkeeper.client.LedgerMetadata) Assert.assertSame(org.junit.Assert.assertSame) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) DataCallback(org.apache.zookeeper.AsyncCallback.DataCallback) PowerMockRunner(org.powermock.modules.junit4.PowerMockRunner) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) PowerMockito(org.powermock.api.mockito.PowerMockito) Before(org.junit.Before) ZK_CONNECT_BACKOFF_MS(org.apache.bookkeeper.meta.AbstractZkLedgerManager.ZK_CONNECT_BACKOFF_MS) KeeperException(org.apache.zookeeper.KeeperException) Watcher(org.apache.zookeeper.Watcher) Assert.assertTrue(org.junit.Assert.assertTrue) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) Assert.assertNull(org.junit.Assert.assertNull) FutureUtils.result(org.apache.bookkeeper.common.concurrent.FutureUtils.result) ClientConfiguration(org.apache.bookkeeper.conf.ClientConfiguration) EventType(org.apache.zookeeper.Watcher.Event.EventType) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Optional(java.util.Optional) Watcher(org.apache.zookeeper.Watcher) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) DataCallback(org.apache.zookeeper.AsyncCallback.DataCallback) Stat(org.apache.zookeeper.data.Stat) LedgerMetadata(org.apache.bookkeeper.client.LedgerMetadata) LongVersion(org.apache.bookkeeper.versioning.LongVersion) TimeUnit(java.util.concurrent.TimeUnit) LedgerMetadataListener(org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.LedgerMetadataListener) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

LedgerMetadataListener (org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.LedgerMetadataListener)8 Duration (java.time.Duration)6 Collections (java.util.Collections)6 Optional (java.util.Optional)6 Set (java.util.Set)6 Executors (java.util.concurrent.Executors)6 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)6 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)6 TimeUnit (java.util.concurrent.TimeUnit)6 BKException (org.apache.bookkeeper.client.BKException)6 Code (org.apache.bookkeeper.client.BKException.Code)6 DigestType (org.apache.bookkeeper.client.BookKeeper.DigestType)6 LedgerMetadata (org.apache.bookkeeper.client.LedgerMetadata)6 FutureUtils.result (org.apache.bookkeeper.common.concurrent.FutureUtils.result)6 MockExecutorController (org.apache.bookkeeper.common.testing.executors.MockExecutorController)6 ClientConfiguration (org.apache.bookkeeper.conf.ClientConfiguration)6 ZK_CONNECT_BACKOFF_MS (org.apache.bookkeeper.meta.AbstractZkLedgerManager.ZK_CONNECT_BACKOFF_MS)6 GenericCallbackFuture (org.apache.bookkeeper.test.TestCallbacks.GenericCallbackFuture)6 ZkUtils (org.apache.bookkeeper.util.ZkUtils)6 LongVersion (org.apache.bookkeeper.versioning.LongVersion)6