Search in sources :

Example 1 with MockZooKeeper

use of org.apache.zookeeper.MockZooKeeper in project pulsar by yahoo.

the class MockedPulsarServiceBaseTest method createMockZooKeeper.

private MockZooKeeper createMockZooKeeper() throws Exception {
    MockZooKeeper zk = MockZooKeeper.newInstance(MoreExecutors.sameThreadExecutor());
    List<ACL> dummyAclList = new ArrayList<ACL>(0);
    ZkUtils.createFullPathOptimistic(zk, "/ledgers/available/192.168.1.1:" + 5000, "".getBytes(ZookeeperClientFactoryImpl.ENCODING_SCHEME), dummyAclList, CreateMode.PERSISTENT);
    zk.create("/ledgers/LAYOUT", "1\nflat:1".getBytes(ZookeeperClientFactoryImpl.ENCODING_SCHEME), dummyAclList, CreateMode.PERSISTENT);
    return zk;
}
Also used : MockZooKeeper(org.apache.zookeeper.MockZooKeeper) ArrayList(java.util.ArrayList) ACL(org.apache.zookeeper.data.ACL)

Example 2 with MockZooKeeper

use of org.apache.zookeeper.MockZooKeeper in project pulsar by yahoo.

the class OwnershipCacheTest method testGetOwner.

@Test
public void testGetOwner() throws Exception {
    OwnershipCache cache = new OwnershipCache(this.pulsar, bundleFactory);
    NamespaceBundle testBundle = bundleFactory.getFullBundle(new NamespaceName("pulsar/test/ns-3"));
    // case 1: no one owns the namespace
    assertFalse(cache.getOwnerAsync(testBundle).get().isPresent());
    // case 2: someone owns the namespace
    ServiceUnitZkUtils.acquireNameSpace(zkCache.getZooKeeper(), ServiceUnitZkUtils.path(testBundle), new NamespaceEphemeralData("pulsar://otherhost:8881", "pulsar://otherhost:8884", "http://otherhost:8080", "https://otherhost:4443", false));
    // try to acquire, which will load the read-only cache
    NamespaceEphemeralData data1 = cache.tryAcquiringOwnership(testBundle).get();
    assertEquals(data1.getNativeUrl(), "pulsar://otherhost:8881");
    assertEquals(data1.getNativeUrlTls(), "pulsar://otherhost:8884");
    assertTrue(!data1.isDisabled());
    // Now do getOwner and compare w/ the returned values
    NamespaceEphemeralData readOnlyData = cache.getOwnerAsync(testBundle).get().get();
    assertEquals(data1, readOnlyData);
    MockZooKeeper mockZk = (MockZooKeeper) zkCache.getZooKeeper();
    mockZk.failNow(KeeperException.Code.NONODE);
    Optional<NamespaceEphemeralData> res = cache.getOwnerAsync(bundleFactory.getFullBundle(new NamespaceName("pulsar/test/ns-none"))).get();
    assertFalse(res.isPresent());
}
Also used : NamespaceBundle(com.yahoo.pulsar.common.naming.NamespaceBundle) MockZooKeeper(org.apache.zookeeper.MockZooKeeper) NamespaceName(com.yahoo.pulsar.common.naming.NamespaceName) Test(org.testng.annotations.Test)

Example 3 with MockZooKeeper

use of org.apache.zookeeper.MockZooKeeper 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 MockZooKeeper

use of org.apache.zookeeper.MockZooKeeper in project pulsar by yahoo.

the class BaseDiscoveryTestSetup method createMockZooKeeper.

protected MockZooKeeper createMockZooKeeper() throws Exception {
    MockZooKeeper zk = MockZooKeeper.newInstance(MoreExecutors.sameThreadExecutor());
    ZkUtils.createFullPathOptimistic(zk, LOADBALANCE_BROKERS_ROOT, "".getBytes(ZookeeperClientFactoryImpl.ENCODING_SCHEME), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    return zk;
}
Also used : MockZooKeeper(org.apache.zookeeper.MockZooKeeper)

Example 5 with MockZooKeeper

use of org.apache.zookeeper.MockZooKeeper in project pulsar by yahoo.

the class MockedZooKeeperClientFactoryImpl method create.

@Override
public CompletableFuture<ZooKeeper> create(String serverList, SessionType sessionType, int zkSessionTimeoutMillis) {
    MockZooKeeper mockZooKeeper = MockZooKeeper.newInstance();
    // not used for mock mode
    List<ACL> dummyAclList = new ArrayList<ACL>(0);
    try {
        ZkUtils.createFullPathOptimistic(mockZooKeeper, "/ledgers/available/192.168.1.1:" + 5000, "".getBytes(ZookeeperClientFactoryImpl.ENCODING_SCHEME), dummyAclList, CreateMode.PERSISTENT);
        mockZooKeeper.create("/ledgers/LAYOUT", "1\nflat:1".getBytes(ZookeeperClientFactoryImpl.ENCODING_SCHEME), dummyAclList, CreateMode.PERSISTENT);
        return CompletableFuture.completedFuture(mockZooKeeper);
    } catch (KeeperException | InterruptedException e) {
        CompletableFuture<ZooKeeper> future = new CompletableFuture<>();
        future.completeExceptionally(e);
        return future;
    }
}
Also used : MockZooKeeper(org.apache.zookeeper.MockZooKeeper) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) ACL(org.apache.zookeeper.data.ACL) KeeperException(org.apache.zookeeper.KeeperException)

Aggregations

MockZooKeeper (org.apache.zookeeper.MockZooKeeper)7 Test (org.testng.annotations.Test)3 ArrayList (java.util.ArrayList)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 ACL (org.apache.zookeeper.data.ACL)2 Lists (com.google.common.collect.Lists)1 Sets (com.google.common.collect.Sets)1 MoreExecutors (com.google.common.util.concurrent.MoreExecutors)1 NamespaceBundle (com.yahoo.pulsar.common.naming.NamespaceBundle)1 NamespaceName (com.yahoo.pulsar.common.naming.NamespaceName)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 OrderedSafeExecutor (org.apache.bookkeeper.util.OrderedSafeExecutor)1 KeeperException (org.apache.zookeeper.KeeperException)1 Code (org.apache.zookeeper.KeeperException.Code)1 WatchedEvent (org.apache.zookeeper.WatchedEvent)1 Event (org.apache.zookeeper.Watcher.Event)1