Search in sources :

Example 1 with OrderedSafeExecutor

use of org.apache.bookkeeper.util.OrderedSafeExecutor in project pulsar by yahoo.

the class MockedBookKeeperTestCase method setUp.

@BeforeMethod
public void setUp(Method method) throws Exception {
    LOG.info(">>>>>> starting {}", method);
    try {
        // start bookkeeper service
        startBookKeeper();
    } catch (Exception e) {
        LOG.error("Error setting up", e);
        throw e;
    }
    executor = new OrderedSafeExecutor(2, "test");
    cachedExecutor = Executors.newCachedThreadPool();
    ManagedLedgerFactoryConfig conf = new ManagedLedgerFactoryConfig();
    conf.setUseProtobufBinaryFormatInZK(protobufFormat == ZNodeProtobufFormat.Binary);
    factory = new ManagedLedgerFactoryImpl(bkc, zkc, conf);
}
Also used : ManagedLedgerFactoryConfig(org.apache.bookkeeper.mledger.ManagedLedgerFactoryConfig) OrderedSafeExecutor(org.apache.bookkeeper.util.OrderedSafeExecutor) ManagedLedgerFactoryImpl(org.apache.bookkeeper.mledger.impl.ManagedLedgerFactoryImpl) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 2 with OrderedSafeExecutor

use of org.apache.bookkeeper.util.OrderedSafeExecutor in project pulsar by yahoo.

the class OwnershipCacheTest method setup.

@BeforeMethod
public void setup() throws Exception {
    final int port = 8080;
    selfBrokerUrl = "tcp://localhost:" + port;
    pulsar = mock(PulsarService.class);
    config = mock(ServiceConfiguration.class);
    executor = new OrderedSafeExecutor(1, "test");
    zkCache = new LocalZooKeeperCache(MockZooKeeper.newInstance(), executor);
    localCache = new LocalZooKeeperCacheService(zkCache, null);
    bundleFactory = new NamespaceBundleFactory(pulsar, Hashing.crc32());
    nsService = mock(NamespaceService.class);
    brokerService = mock(BrokerService.class);
    doReturn(CompletableFuture.completedFuture(1)).when(brokerService).unloadServiceUnit(anyObject());
    doReturn(zkCache).when(pulsar).getLocalZkCache();
    doReturn(localCache).when(pulsar).getLocalZkCacheService();
    doReturn(config).when(pulsar).getConfiguration();
    doReturn(nsService).when(pulsar).getNamespaceService();
    doReturn(port).when(config).getBrokerServicePort();
    doReturn(brokerService).when(pulsar).getBrokerService();
    doReturn(webAddress(config)).when(pulsar).getWebServiceAddress();
    doReturn(selfBrokerUrl).when(pulsar).getBrokerServiceUrl();
}
Also used : PulsarService(com.yahoo.pulsar.broker.PulsarService) ServiceConfiguration(com.yahoo.pulsar.broker.ServiceConfiguration) LocalZooKeeperCache(com.yahoo.pulsar.zookeeper.LocalZooKeeperCache) OrderedSafeExecutor(org.apache.bookkeeper.util.OrderedSafeExecutor) NamespaceBundleFactory(com.yahoo.pulsar.common.naming.NamespaceBundleFactory) BrokerService(com.yahoo.pulsar.broker.service.BrokerService) LocalZooKeeperCacheService(com.yahoo.pulsar.broker.cache.LocalZooKeeperCacheService) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 3 with OrderedSafeExecutor

use of org.apache.bookkeeper.util.OrderedSafeExecutor in project pulsar by yahoo.

the class ZookeeperCacheTest method testGlobalZooKeeperCache.

@Test
void testGlobalZooKeeperCache() throws Exception {
    OrderedSafeExecutor executor = new OrderedSafeExecutor(1, "test");
    ScheduledExecutorService scheduledExecutor = new ScheduledThreadPoolExecutor(1);
    MockZooKeeper zkc = MockZooKeeper.newInstance();
    ZooKeeperClientFactory zkClientfactory = new ZooKeeperClientFactory() {

        @Override
        public CompletableFuture<ZooKeeper> create(String serverList, SessionType sessionType, int zkSessionTimeoutMillis) {
            return CompletableFuture.completedFuture(zkc);
        }
    };
    GlobalZooKeeperCache zkCacheService = new GlobalZooKeeperCache(zkClientfactory, -1, "", executor, scheduledExecutor);
    zkCacheService.start();
    zkClient = (MockZooKeeper) zkCacheService.getZooKeeper();
    ZooKeeperDataCache<String> zkCache = new ZooKeeperDataCache<String>(zkCacheService) {

        @Override
        public String deserialize(String key, byte[] content) throws Exception {
            return new String(content);
        }
    };
    // Create callback counter
    AtomicInteger notificationCount = new AtomicInteger(0);
    ZooKeeperCacheListener<String> counter = (path, data, stat) -> {
        notificationCount.incrementAndGet();
    };
    // Register counter twice and unregister once, so callback should be counted correctly
    zkCache.registerListener(counter);
    zkCache.registerListener(counter);
    zkCache.unregisterListener(counter);
    String value = "test";
    zkClient.create("/my_test", value.getBytes(), null, null);
    assertEquals(zkCache.get("/my_test").get(), value);
    String newValue = "test2";
    // case 1: update and create znode directly and verify that the cache is retrieving the correct data
    assertEquals(notificationCount.get(), 0);
    zkClient.setData("/my_test", newValue.getBytes(), -1);
    zkClient.create("/my_test2", value.getBytes(), null, null);
    // Wait for the watch to be triggered
    while (notificationCount.get() < 1) {
        Thread.sleep(1);
    }
    // retrieve the data from the cache and verify it is the updated/new data
    assertEquals(zkCache.get("/my_test").get(), newValue);
    assertEquals(zkCache.get("/my_test2").get(), value);
    // The callback method should be called just only once
    assertEquals(notificationCount.get(), 1);
    // case 2: force the ZooKeeper session to be expired and verify that the data is still accessible
    zkCacheService.process(new WatchedEvent(Event.EventType.None, KeeperState.Expired, null));
    assertEquals(zkCache.get("/my_test").get(), newValue);
    assertEquals(zkCache.get("/my_test2").get(), value);
    // case 3: update the znode directly while the client session is marked as expired. Verify that the new updates
    // is not seen in the cache
    zkClient.create("/other", newValue.getBytes(), null, null);
    zkClient.failNow(Code.SESSIONEXPIRED);
    assertEquals(zkCache.get("/my_test").get(), newValue);
    assertEquals(zkCache.get("/my_test2").get(), value);
    try {
        zkCache.get("/other");
        fail("shuld have thrown exception");
    } catch (Exception e) {
    // Ok
    }
    // case 4: directly delete the znode while the session is not re-connected yet. Verify that the deletion is not
    // seen by the cache
    zkClient.failAfter(-1, Code.OK);
    zkClient.delete("/my_test2", -1);
    zkCacheService.process(new WatchedEvent(Event.EventType.None, KeeperState.SyncConnected, null));
    assertEquals(zkCache.get("/other").get(), newValue);
    // Make sure that the value is now directly from ZK and deleted
    assertFalse(zkCache.get("/my_test2").isPresent());
    // case 5: trigger a ZooKeeper disconnected event and make sure the cache content is not changed.
    zkCacheService.process(new WatchedEvent(Event.EventType.None, KeeperState.Disconnected, null));
    zkClient.create("/other2", newValue.getBytes(), null, null);
    // case 6: trigger a ZooKeeper SyncConnected event and make sure that the cache content is invalidated s.t. we
    // can see the updated content now
    zkCacheService.process(new WatchedEvent(Event.EventType.None, KeeperState.SyncConnected, null));
    // make sure that we get it
    assertEquals(zkCache.get("/other2").get(), newValue);
    zkCacheService.close();
    executor.shutdown();
    // Update shouldn't happen after the last check
    assertEquals(notificationCount.get(), 1);
}
Also used : MoreExecutors(com.google.common.util.concurrent.MoreExecutors) Event(org.apache.zookeeper.Watcher.Event) Assert.assertEquals(org.testng.Assert.assertEquals) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.testng.annotations.Test) AfterMethod(org.testng.annotations.AfterMethod) TreeSet(java.util.TreeSet) Lists(com.google.common.collect.Lists) Assert(org.testng.Assert) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AssertJUnit.assertNull(org.testng.AssertJUnit.assertNull) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) KeeperState(org.apache.zookeeper.Watcher.Event.KeeperState) Assert.assertFalse(org.testng.Assert.assertFalse) ZooKeeper(org.apache.zookeeper.ZooKeeper) OrderedSafeExecutor(org.apache.bookkeeper.util.OrderedSafeExecutor) Assert.fail(org.testng.Assert.fail) BeforeMethod(org.testng.annotations.BeforeMethod) Set(java.util.Set) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) MockZooKeeper(org.apache.zookeeper.MockZooKeeper) WatchedEvent(org.apache.zookeeper.WatchedEvent) Sets(com.google.common.collect.Sets) AssertJUnit.assertNotNull(org.testng.AssertJUnit.assertNotNull) Code(org.apache.zookeeper.KeeperException.Code) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) MockZooKeeper(org.apache.zookeeper.MockZooKeeper) WatchedEvent(org.apache.zookeeper.WatchedEvent) ZooKeeper(org.apache.zookeeper.ZooKeeper) MockZooKeeper(org.apache.zookeeper.MockZooKeeper) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) OrderedSafeExecutor(org.apache.bookkeeper.util.OrderedSafeExecutor) Test(org.testng.annotations.Test)

Example 4 with OrderedSafeExecutor

use of org.apache.bookkeeper.util.OrderedSafeExecutor in project pulsar by yahoo.

the class ZookeeperCacheTest method testChildrenCache.

@Test
void testChildrenCache() throws Exception {
    OrderedSafeExecutor executor = new OrderedSafeExecutor(1, "test");
    zkClient.create("/test", new byte[0], null, null);
    ZooKeeperCache zkCacheService = new LocalZooKeeperCache(zkClient, executor);
    ZooKeeperChildrenCache cache = new ZooKeeperChildrenCache(zkCacheService, "/test");
    // Create callback counter
    AtomicInteger notificationCount = new AtomicInteger(0);
    ZooKeeperCacheListener<Set<String>> counter = (path, data, stat) -> {
        notificationCount.incrementAndGet();
    };
    // Register counter twice and unregister once, so callback should be counted correctly
    cache.registerListener(counter);
    cache.registerListener(counter);
    cache.unregisterListener(counter);
    assertEquals(notificationCount.get(), 0);
    assertEquals(cache.get(), Sets.newTreeSet());
    zkClient.create("/test/z1", new byte[0], null, null);
    zkClient.create("/test/z2", new byte[0], null, null);
    // Wait for cache to be updated in background
    while (notificationCount.get() < 2) {
        Thread.sleep(1);
    }
    assertEquals(cache.get(), new TreeSet<String>(Lists.newArrayList("z1", "z2")));
    assertEquals(cache.get("/test"), new TreeSet<String>(Lists.newArrayList("z1", "z2")));
    assertEquals(notificationCount.get(), 2);
    zkClient.delete("/test/z2", -1);
    while (notificationCount.get() < 3) {
        Thread.sleep(1);
    }
    assertEquals(cache.get(), new TreeSet<String>(Lists.newArrayList("z1")));
    assertEquals(cache.get(), new TreeSet<String>(Lists.newArrayList("z1")));
    zkCacheService.process(new WatchedEvent(Event.EventType.None, KeeperState.Expired, null));
    zkClient.failNow(Code.SESSIONEXPIRED);
    try {
        cache.get();
        fail("shuld have thrown exception");
    } catch (Exception e) {
    // Ok
    }
    assertEquals(notificationCount.get(), 3);
}
Also used : MoreExecutors(com.google.common.util.concurrent.MoreExecutors) Event(org.apache.zookeeper.Watcher.Event) Assert.assertEquals(org.testng.Assert.assertEquals) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.testng.annotations.Test) AfterMethod(org.testng.annotations.AfterMethod) TreeSet(java.util.TreeSet) Lists(com.google.common.collect.Lists) Assert(org.testng.Assert) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AssertJUnit.assertNull(org.testng.AssertJUnit.assertNull) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) KeeperState(org.apache.zookeeper.Watcher.Event.KeeperState) Assert.assertFalse(org.testng.Assert.assertFalse) ZooKeeper(org.apache.zookeeper.ZooKeeper) OrderedSafeExecutor(org.apache.bookkeeper.util.OrderedSafeExecutor) Assert.fail(org.testng.Assert.fail) BeforeMethod(org.testng.annotations.BeforeMethod) Set(java.util.Set) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) MockZooKeeper(org.apache.zookeeper.MockZooKeeper) WatchedEvent(org.apache.zookeeper.WatchedEvent) Sets(com.google.common.collect.Sets) AssertJUnit.assertNotNull(org.testng.AssertJUnit.assertNotNull) Code(org.apache.zookeeper.KeeperException.Code) WatchedEvent(org.apache.zookeeper.WatchedEvent) TreeSet(java.util.TreeSet) Set(java.util.Set) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) OrderedSafeExecutor(org.apache.bookkeeper.util.OrderedSafeExecutor) Test(org.testng.annotations.Test)

Example 5 with OrderedSafeExecutor

use of org.apache.bookkeeper.util.OrderedSafeExecutor in project pulsar by yahoo.

the class ResourceQuotaCacheTest method setup.

@BeforeMethod
public void setup() throws Exception {
    pulsar = mock(PulsarService.class);
    OrderedSafeExecutor executor = new OrderedSafeExecutor(1, "test");
    zkCache = new LocalZooKeeperCache(MockZooKeeper.newInstance(), executor);
    localCache = new LocalZooKeeperCacheService(zkCache, null);
    bundleFactory = new NamespaceBundleFactory(pulsar, Hashing.crc32());
    doReturn(zkCache).when(pulsar).getLocalZkCache();
    doReturn(localCache).when(pulsar).getLocalZkCacheService();
}
Also used : PulsarService(com.yahoo.pulsar.broker.PulsarService) LocalZooKeeperCache(com.yahoo.pulsar.zookeeper.LocalZooKeeperCache) OrderedSafeExecutor(org.apache.bookkeeper.util.OrderedSafeExecutor) NamespaceBundleFactory(com.yahoo.pulsar.common.naming.NamespaceBundleFactory) LocalZooKeeperCacheService(com.yahoo.pulsar.broker.cache.LocalZooKeeperCacheService) BeforeMethod(org.testng.annotations.BeforeMethod)

Aggregations

OrderedSafeExecutor (org.apache.bookkeeper.util.OrderedSafeExecutor)5 BeforeMethod (org.testng.annotations.BeforeMethod)5 Lists (com.google.common.collect.Lists)2 Sets (com.google.common.collect.Sets)2 MoreExecutors (com.google.common.util.concurrent.MoreExecutors)2 PulsarService (com.yahoo.pulsar.broker.PulsarService)2 LocalZooKeeperCacheService (com.yahoo.pulsar.broker.cache.LocalZooKeeperCacheService)2 NamespaceBundleFactory (com.yahoo.pulsar.common.naming.NamespaceBundleFactory)2 LocalZooKeeperCache (com.yahoo.pulsar.zookeeper.LocalZooKeeperCache)2 Set (java.util.Set)2 TreeSet (java.util.TreeSet)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Code (org.apache.zookeeper.KeeperException.Code)2 MockZooKeeper (org.apache.zookeeper.MockZooKeeper)2 WatchedEvent (org.apache.zookeeper.WatchedEvent)2 Event (org.apache.zookeeper.Watcher.Event)2 KeeperState (org.apache.zookeeper.Watcher.Event.KeeperState)2