Search in sources :

Example 46 with LongVersion

use of org.apache.bookkeeper.versioning.LongVersion in project bookkeeper by apache.

the class AbstractZkLedgerManagerTest method testReadLedgerMetadataDataCorrupted.

@Test
public void testReadLedgerMetadataDataCorrupted() throws Exception {
    long ledgerId = System.currentTimeMillis();
    String ledgerStr = String.valueOf(ledgerId);
    metadata.setVersion(new LongVersion(1234L));
    Stat stat = mock(Stat.class);
    when(stat.getVersion()).thenReturn(1234);
    when(stat.getCtime()).thenReturn(metadata.getCtime());
    mockZkGetData(ledgerStr, false, KeeperException.Code.OK.intValue(), new byte[0], stat);
    GenericCallbackFuture<LedgerMetadata> callbackFuture = new GenericCallbackFuture<>();
    ledgerManager.readLedgerMetadata(ledgerId, callbackFuture);
    try {
        result(callbackFuture);
        fail("Should fail on reading ledger metadata if a ledger doesn't exist");
    } catch (BKException bke) {
        assertEquals(Code.ZKException, bke.getCode());
    }
    verify(mockZk, times(1)).getData(eq(ledgerStr), eq(null), any(DataCallback.class), any());
}
Also used : Stat(org.apache.zookeeper.data.Stat) LedgerMetadata(org.apache.bookkeeper.client.LedgerMetadata) LongVersion(org.apache.bookkeeper.versioning.LongVersion) BKException(org.apache.bookkeeper.client.BKException) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DataCallback(org.apache.zookeeper.AsyncCallback.DataCallback) GenericCallbackFuture(org.apache.bookkeeper.test.TestCallbacks.GenericCallbackFuture) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 47 with LongVersion

use of org.apache.bookkeeper.versioning.LongVersion 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 48 with LongVersion

use of org.apache.bookkeeper.versioning.LongVersion in project bookkeeper by apache.

the class AbstractZkLedgerManagerTest method testWriteLedgerMetadataSuccess.

@Test
public void testWriteLedgerMetadataSuccess() throws Exception {
    long ledgerId = System.currentTimeMillis();
    String ledgerStr = String.valueOf(ledgerId);
    metadata.setVersion(new LongVersion(1234L));
    Stat stat = mock(Stat.class);
    when(stat.getVersion()).thenReturn(1235);
    when(stat.getCtime()).thenReturn(metadata.getCtime());
    mockZkSetData(ledgerStr, metadata.serialize(), 1234, KeeperException.Code.OK.intValue(), stat);
    assertEquals(new LongVersion(1234L), metadata.getVersion());
    GenericCallbackFuture<Void> callbackFuture = new GenericCallbackFuture<>();
    ledgerManager.writeLedgerMetadata(ledgerId, metadata, callbackFuture);
    result(callbackFuture);
    assertEquals(new LongVersion(1235L), metadata.getVersion());
    verify(mockZk, times(1)).setData(eq(ledgerStr), any(byte[].class), eq(1234), any(StatCallback.class), any());
}
Also used : Stat(org.apache.zookeeper.data.Stat) LongVersion(org.apache.bookkeeper.versioning.LongVersion) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) GenericCallbackFuture(org.apache.bookkeeper.test.TestCallbacks.GenericCallbackFuture) StatCallback(org.apache.zookeeper.AsyncCallback.StatCallback) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 49 with LongVersion

use of org.apache.bookkeeper.versioning.LongVersion in project bookkeeper by apache.

the class AbstractZkLedgerManagerTest method testWriteLedgerMetadataException.

@Test
public void testWriteLedgerMetadataException() throws Exception {
    long ledgerId = System.currentTimeMillis();
    String ledgerStr = String.valueOf(ledgerId);
    metadata.setVersion(new LongVersion(1234L));
    mockZkSetData(ledgerStr, metadata.serialize(), 1234, KeeperException.Code.CONNECTIONLOSS.intValue(), null);
    assertEquals(new LongVersion(1234L), metadata.getVersion());
    GenericCallbackFuture<Void> callbackFuture = new GenericCallbackFuture<>();
    ledgerManager.writeLedgerMetadata(ledgerId, metadata, callbackFuture);
    try {
        result(callbackFuture);
        fail("Should fail on writing ledger metadata if encountering zookeeper exceptions");
    } catch (BKException bke) {
        assertEquals(Code.ZKException, bke.getCode());
    }
    // version remain unchanged
    assertEquals(new LongVersion(1234L), metadata.getVersion());
    verify(mockZk, times(1)).setData(eq(ledgerStr), any(byte[].class), eq(1234), any(StatCallback.class), any());
}
Also used : LongVersion(org.apache.bookkeeper.versioning.LongVersion) BKException(org.apache.bookkeeper.client.BKException) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) GenericCallbackFuture(org.apache.bookkeeper.test.TestCallbacks.GenericCallbackFuture) StatCallback(org.apache.zookeeper.AsyncCallback.StatCallback) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 50 with LongVersion

use of org.apache.bookkeeper.versioning.LongVersion in project bookkeeper by apache.

the class AbstractZkLedgerManagerTest method testRemoveLedgerMetadataNoNode.

@Test
public void testRemoveLedgerMetadataNoNode() throws Exception {
    long ledgerId = System.currentTimeMillis();
    String ledgerStr = String.valueOf(ledgerId);
    LongVersion version = new LongVersion(1234L);
    mockZkDelete(ledgerStr, (int) version.getLongVersion(), KeeperException.Code.NONODE.intValue());
    GenericCallbackFuture<Void> callbackFuture = new GenericCallbackFuture<>();
    ledgerManager.removeLedgerMetadata(ledgerId, version, callbackFuture);
    try {
        result(callbackFuture);
        fail("Should fail to remove metadata if no such ledger exists");
    } catch (BKException bke) {
        assertEquals(Code.NoSuchLedgerExistsException, bke.getCode());
    }
    verify(mockZk, times(1)).delete(eq(ledgerStr), eq(1234), any(VoidCallback.class), eq(null));
}
Also used : VoidCallback(org.apache.zookeeper.AsyncCallback.VoidCallback) LongVersion(org.apache.bookkeeper.versioning.LongVersion) BKException(org.apache.bookkeeper.client.BKException) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) GenericCallbackFuture(org.apache.bookkeeper.test.TestCallbacks.GenericCallbackFuture) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

LongVersion (org.apache.bookkeeper.versioning.LongVersion)56 Test (org.junit.Test)37 Versioned (org.apache.bookkeeper.versioning.Versioned)29 Stat (org.apache.zookeeper.data.Stat)25 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)21 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)20 Version (org.apache.bookkeeper.versioning.Version)18 GenericCallbackFuture (org.apache.bookkeeper.test.TestCallbacks.GenericCallbackFuture)16 BKException (org.apache.bookkeeper.client.BKException)12 VoidCallback (org.apache.zookeeper.AsyncCallback.VoidCallback)12 Set (java.util.Set)11 LedgerMetadata (org.apache.bookkeeper.client.LedgerMetadata)10 StatCallback (org.apache.zookeeper.AsyncCallback.StatCallback)10 DataCallback (org.apache.zookeeper.AsyncCallback.DataCallback)9 Before (org.junit.Before)9 URI (java.net.URI)8 ZKException (org.apache.distributedlog.exceptions.ZKException)8 KeeperException (org.apache.zookeeper.KeeperException)8 Assert.assertEquals (org.junit.Assert.assertEquals)8 Assert.fail (org.junit.Assert.fail)8