Search in sources :

Example 1 with Int96

use of io.pravega.common.lang.Int96 in project pravega by pravega.

the class ZKCounterTest method testCounterConcurrentUpdates.

@Test(timeout = 30000)
public void testCounterConcurrentUpdates() {
    ZKStoreHelper storeHelper = spy(new ZKStoreHelper(cli, executor));
    storeHelper.createZNodeIfNotExist("/store/scope").join();
    ZkInt96Counter counter1 = spy(new ZkInt96Counter(storeHelper));
    ZkInt96Counter counter2 = spy(new ZkInt96Counter(storeHelper));
    ZkInt96Counter counter3 = spy(new ZkInt96Counter(storeHelper));
    // first call should get the new range from store
    Int96 counter = counter1.getNextCounter().join();
    // verify that the generated counter is from new range
    assertEquals(0, counter.getMsb());
    assertEquals(1L, counter.getLsb());
    assertEquals(counter1.getCounterForTesting(), counter);
    Int96 limit = counter1.getLimitForTesting();
    assertEquals(ZkInt96Counter.COUNTER_RANGE, limit.getLsb());
    counter3.getRefreshFuture().join();
    assertEquals(ZkInt96Counter.COUNTER_RANGE, counter3.getCounterForTesting().getLsb());
    assertEquals(ZkInt96Counter.COUNTER_RANGE * 2, counter3.getLimitForTesting().getLsb());
    counter2.getRefreshFuture().join();
    assertEquals(ZkInt96Counter.COUNTER_RANGE * 2, counter2.getCounterForTesting().getLsb());
    assertEquals(ZkInt96Counter.COUNTER_RANGE * 3, counter2.getLimitForTesting().getLsb());
    counter1.getRefreshFuture().join();
    assertEquals(ZkInt96Counter.COUNTER_RANGE * 3, counter1.getCounterForTesting().getLsb());
    assertEquals(ZkInt96Counter.COUNTER_RANGE * 4, counter1.getLimitForTesting().getLsb());
}
Also used : ZKStoreHelper(io.pravega.controller.store.ZKStoreHelper) Int96(io.pravega.common.lang.Int96) Test(org.junit.Test)

Example 2 with Int96

use of io.pravega.common.lang.Int96 in project pravega by pravega.

the class ZKCounterTest method testCounter.

@Test(timeout = 30000)
public void testCounter() throws Exception {
    ZKStoreHelper storeHelper = spy(new ZKStoreHelper(cli, executor));
    storeHelper.createZNodeIfNotExist("/store/scope").join();
    ZkInt96Counter zkStore = spy(new ZkInt96Counter(storeHelper));
    // first call should get the new range from store
    Int96 counter = zkStore.getNextCounter().join();
    // verify that the generated counter is from new range
    assertEquals(0, counter.getMsb());
    assertEquals(1L, counter.getLsb());
    assertEquals(zkStore.getCounterForTesting(), counter);
    Int96 limit = zkStore.getLimitForTesting();
    assertEquals(ZkInt96Counter.COUNTER_RANGE, limit.getLsb());
    // update the local counter to the end of the current range (limit - 1)
    zkStore.setCounterAndLimitForTesting(limit.getMsb(), limit.getLsb() - 1, limit.getMsb(), limit.getLsb());
    // now call three getNextCounters concurrently.. first one to execute should increment the counter to limit.
    // other two will result in refresh being called.
    CompletableFuture<Int96> future1 = zkStore.getNextCounter();
    CompletableFuture<Int96> future2 = zkStore.getNextCounter();
    CompletableFuture<Int96> future3 = zkStore.getNextCounter();
    List<Int96> values = Futures.allOfWithResults(Arrays.asList(future1, future2, future3)).join();
    // second and third should result in refresh being called. Verify method call count is 3, twice for now and
    // once for first time when counter is set
    verify(zkStore, times(3)).refreshRangeIfNeeded();
    verify(zkStore, times(2)).getRefreshFuture();
    assertTrue(values.stream().anyMatch(x -> x.compareTo(new Int96(limit.getMsb(), limit.getLsb())) == 0));
    assertTrue(values.stream().anyMatch(x -> x.compareTo(new Int96(0, limit.getLsb() + 1)) == 0));
    assertTrue(values.stream().anyMatch(x -> x.compareTo(new Int96(0, limit.getLsb() + 2)) == 0));
    // verify that counter and limits are increased
    Int96 newCounter = zkStore.getCounterForTesting();
    Int96 newLimit = zkStore.getLimitForTesting();
    assertEquals(ZkInt96Counter.COUNTER_RANGE * 2, newLimit.getLsb());
    assertEquals(0, newLimit.getMsb());
    assertEquals(ZkInt96Counter.COUNTER_RANGE + 2, newCounter.getLsb());
    assertEquals(0, newCounter.getMsb());
    // set range in store to have lsb = Long.Max - 100
    VersionedMetadata<Int96> data = new VersionedMetadata<>(new Int96(0, Long.MAX_VALUE - 100), null);
    doReturn(CompletableFuture.completedFuture(data)).when(storeHelper).getData(eq(ZkInt96Counter.COUNTER_PATH), any());
    // set local limit to {msb, Long.Max - 100}
    zkStore.setCounterAndLimitForTesting(0, Long.MAX_VALUE - 100, 0, Long.MAX_VALUE - 100);
    // now the call to getNextCounter should result in another refresh
    zkStore.getNextCounter().join();
    // verify that post refresh counter and limit have different msb
    Int96 newCounter2 = zkStore.getCounterForTesting();
    Int96 newLimit2 = zkStore.getLimitForTesting();
    assertEquals(1, newLimit2.getMsb());
    assertEquals(ZkInt96Counter.COUNTER_RANGE - 100, newLimit2.getLsb());
    assertEquals(0, newCounter2.getMsb());
    assertEquals(Long.MAX_VALUE - 99, newCounter2.getLsb());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) CuratorFrameworkFactory(org.apache.curator.framework.CuratorFrameworkFactory) Arrays(java.util.Arrays) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) CompletableFuture(java.util.concurrent.CompletableFuture) Mockito.spy(org.mockito.Mockito.spy) RetryOneTime(org.apache.curator.retry.RetryOneTime) VersionedMetadata(io.pravega.controller.store.VersionedMetadata) TestingServerStarter(io.pravega.test.common.TestingServerStarter) After(org.junit.After) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) TestingServer(org.apache.curator.test.TestingServer) Mockito.doReturn(org.mockito.Mockito.doReturn) Before(org.junit.Before) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Mockito.verify(org.mockito.Mockito.verify) ZKStoreHelper(io.pravega.controller.store.ZKStoreHelper) List(java.util.List) CuratorFramework(org.apache.curator.framework.CuratorFramework) Int96(io.pravega.common.lang.Int96) ExecutorServiceHelpers(io.pravega.common.concurrent.ExecutorServiceHelpers) Futures(io.pravega.common.concurrent.Futures) Assert.assertEquals(org.junit.Assert.assertEquals) ZKStoreHelper(io.pravega.controller.store.ZKStoreHelper) Int96(io.pravega.common.lang.Int96) VersionedMetadata(io.pravega.controller.store.VersionedMetadata) Test(org.junit.Test)

Aggregations

Int96 (io.pravega.common.lang.Int96)2 ZKStoreHelper (io.pravega.controller.store.ZKStoreHelper)2 Test (org.junit.Test)2 ExecutorServiceHelpers (io.pravega.common.concurrent.ExecutorServiceHelpers)1 Futures (io.pravega.common.concurrent.Futures)1 VersionedMetadata (io.pravega.controller.store.VersionedMetadata)1 TestingServerStarter (io.pravega.test.common.TestingServerStarter)1 Arrays (java.util.Arrays)1 List (java.util.List)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 CuratorFramework (org.apache.curator.framework.CuratorFramework)1 CuratorFrameworkFactory (org.apache.curator.framework.CuratorFrameworkFactory)1 RetryOneTime (org.apache.curator.retry.RetryOneTime)1 TestingServer (org.apache.curator.test.TestingServer)1 After (org.junit.After)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Assert.assertTrue (org.junit.Assert.assertTrue)1 Before (org.junit.Before)1 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)1