List of usage examples for org.apache.zookeeper ZooKeeper ZooKeeper
public ZooKeeper(String connectString, int sessionTimeout, Watcher watcher, long sessionId, byte[] sessionPasswd) throws IOException
From source file:blazingcache.server.SimpleZKSecureTest.java
@Test public void sessionExpirationTest_SingleCacheServer() throws Exception { byte[] data = "testdata".getBytes(StandardCharsets.UTF_8); ServerHostData hostData = new ServerHostData("localhost", 1234, "ciao", false, null); try (ZKTestEnv zkEnv = new ZKTestEnv(folderZk.getRoot().toPath()); CacheServer cacheServer = new CacheServer("ciao", hostData)) { cacheServer.setupCluster(zkEnv.getAddress(), zkEnv.getTimeout(), zkEnv.getPath(), hostData, true); cacheServer.start();//from ww w . ja va 2 s . c om try (CacheClient client1 = new CacheClient("theClient1", "ciao", new ZKCacheServerLocator(zkEnv.getAddress(), zkEnv.getTimeout(), zkEnv.getPath())); CacheClient client2 = new CacheClient("theClient2", "ciao", new ZKCacheServerLocator(zkEnv.getAddress(), zkEnv.getTimeout(), zkEnv.getPath()))) { client1.start(); client2.start(); assertTrue(client1.waitForConnection(10000)); assertTrue(client2.waitForConnection(10000)); client1.put("pippo", data, 0); client2.put("pippo", data, 0); Assert.assertArrayEquals(data, client1.get("pippo").getSerializedData()); Assert.assertArrayEquals(data, client2.get("pippo").getSerializedData()); assertEquals(1, client1.getCacheSize()); assertEquals(1, client2.getCacheSize()); final long lastStateChangeTS = cacheServer.getStateChangeTimestamp(); /* * Make's ZooKeeper's session expire: * * this is the session id and password to use on a second zookeeper * handle so as to make service monitor's handle to expire */ final long serviceZKSessionId = cacheServer.getZooKeeper().getSessionId(); final byte[] serviceZKpasswd = cacheServer.getZooKeeper().getSessionPasswd(); CountdownWatcher watch2 = new CountdownWatcher("zkexpire"); // make session on cache server's cluster manager zk handle expire final ZooKeeper zk = new ZooKeeper(zkEnv.getAddress(), zkEnv.getTimeout(), watch2, serviceZKSessionId, serviceZKpasswd); watch2.waitForConnected(10000); zk.close(); //first things first, make sure leadership is lost: state change ts has changed waitForCondition(() -> { return cacheServer.getStateChangeTimestamp() > lastStateChangeTS; }, 100); //when fake zk handle expires we are sure that origina cache server session is going to expire watch2.waitForExpired(10000); //first things first, make sure leadership is acquired again waitForCondition(() -> { return cacheServer.isLeader(); }, 100); //ensure clients reconnect assertTrue(client1.waitForConnection(10000)); assertTrue(client2.waitForConnection(10000)); assertEquals(0, client1.getCacheSize()); assertEquals(0, client2.getCacheSize()); } } }
From source file:blazingcache.server.SimpleZKSecureTest.java
@Test public void sessionExpirationTest_BackupServer() throws Exception { byte[] data = "testdata".getBytes(StandardCharsets.UTF_8); final ServerHostData leaderHostdata = new ServerHostData("localhost", 1234, "leader", false, null); final ServerHostData backupHostdata = new ServerHostData("localhost", 1235, "backup", false, null); try (ZKTestEnv zkEnv = new ZKTestEnv(folderZk.getRoot().toPath()); CacheServer cacheServer = new CacheServer("ciao", leaderHostdata); CacheServer cacheServerBk = new CacheServer("ciao", backupHostdata)) { cacheServer.setupCluster(zkEnv.getAddress(), zkEnv.getTimeout(), zkEnv.getPath(), leaderHostdata, true); cacheServer.start();//from w w w . j a va2 s.c o m waitForCondition(() -> { return cacheServer.isLeader(); }, 100); //start backupcluster: we are sure this is in backup mode cacheServerBk.setupCluster(zkEnv.getAddress(), zkEnv.getTimeout(), zkEnv.getPath(), backupHostdata, true); cacheServerBk.start(); try (CacheClient client1 = new CacheClient("theClient1", "ciao", new ZKCacheServerLocator(zkEnv.getAddress(), zkEnv.getTimeout(), zkEnv.getPath())); CacheClient client2 = new CacheClient("theClient2", "ciao", new ZKCacheServerLocator(zkEnv.getAddress(), zkEnv.getTimeout(), zkEnv.getPath()))) { client1.start(); client2.start(); assertTrue(client1.waitForConnection(10000)); assertTrue(client2.waitForConnection(10000)); client1.put("pippo", data, 0); client2.put("pippo", data, 0); Assert.assertArrayEquals(data, client1.get("pippo").getSerializedData()); Assert.assertArrayEquals(data, client2.get("pippo").getSerializedData()); assertEquals(1, client1.getCacheSize()); assertEquals(1, client2.getCacheSize()); final long lastStateChangeTS = cacheServer.getStateChangeTimestamp(); /* * Make's ZooKeeper's session expire: * * this is the session id and password to use on a second zookeeper * handle so as to make service monitor's handle to expire */ final long serviceZKSessionId = cacheServer.getZooKeeper().getSessionId(); final byte[] serviceZKpasswd = cacheServer.getZooKeeper().getSessionPasswd(); CountdownWatcher watch2 = new CountdownWatcher("zkexpire"); // make session on cache server's cluster manager zk handle expire final ZooKeeper zk = new ZooKeeper(zkEnv.getAddress(), zkEnv.getTimeout(), watch2, serviceZKSessionId, serviceZKpasswd); watch2.waitForConnected(10000); zk.close(); //first things first, make sure leadership is lost: state change ts has changed waitForCondition(() -> { return cacheServer.getStateChangeTimestamp() > lastStateChangeTS; }, 100); //when fake zk handle expires we are sure that original cache server session is going to expire watch2.waitForExpired(10000); //first things first, make sure leadership is acquired again waitForCondition(() -> { return client1.getCacheSize() == 0 && client2.getCacheSize() == 0; }, 100); waitForCondition(() -> { return !cacheServer.isLeader(); }, 100); waitForCondition(() -> { return cacheServerBk.isLeader(); }, 100); //ensure clients reconnect assertTrue(client1.waitForConnection(10000)); assertTrue(client2.waitForConnection(10000)); client1.put("pippo", data, 0); client2.put("pippo", data, 0); Assert.assertArrayEquals(data, client1.get("pippo").getSerializedData()); Assert.assertArrayEquals(data, client2.get("pippo").getSerializedData()); assertEquals(1, client1.getCacheSize()); assertEquals(1, client2.getCacheSize()); client1.invalidate("pippo"); assertNull(client1.get("pippo")); assertNull(client2.get("pippo")); assertEquals(0, client1.getCacheSize()); assertEquals(0, client2.getCacheSize()); } } }
From source file:blazingcache.server.SimpleZKTest.java
@Test public void sessionExpirationTest_SingleCacheServer() throws Exception { byte[] data = "testdata".getBytes(StandardCharsets.UTF_8); ServerHostData hostData = new ServerHostData("localhost", 1234, "ciao", false, null); try (ZKTestEnv zkEnv = new ZKTestEnv(folderZk.getRoot().toPath()); CacheServer cacheServer = new CacheServer("ciao", hostData)) { cacheServer.setupCluster(zkEnv.getAddress(), zkEnv.getTimeout(), zkEnv.getPath(), hostData, false); cacheServer.start();//from ww w . j a v a2s . c o m try (CacheClient client1 = new CacheClient("theClient1", "ciao", new ZKCacheServerLocator(zkEnv.getAddress(), zkEnv.getTimeout(), zkEnv.getPath())); CacheClient client2 = new CacheClient("theClient2", "ciao", new ZKCacheServerLocator(zkEnv.getAddress(), zkEnv.getTimeout(), zkEnv.getPath()))) { client1.start(); client2.start(); assertTrue(client1.waitForConnection(10000)); assertTrue(client2.waitForConnection(10000)); client1.put("pippo", data, 0); client2.put("pippo", data, 0); Assert.assertArrayEquals(data, client1.get("pippo").getSerializedData()); Assert.assertArrayEquals(data, client2.get("pippo").getSerializedData()); assertEquals(1, client1.getCacheSize()); assertEquals(1, client2.getCacheSize()); final long lastStateChangeTS = cacheServer.getStateChangeTimestamp(); /* * Make's ZooKeeper's session expire: * * this is the session id and password to use on a second zookeeper * handle so as to make service monitor's handle to expire */ final long serviceZKSessionId = cacheServer.getZooKeeper().getSessionId(); final byte[] serviceZKpasswd = cacheServer.getZooKeeper().getSessionPasswd(); CountdownWatcher watch2 = new CountdownWatcher("zkexpire"); // make session on cache server's cluster manager zk handle expire final ZooKeeper zk = new ZooKeeper(zkEnv.getAddress(), zkEnv.getTimeout(), watch2, serviceZKSessionId, serviceZKpasswd); watch2.waitForConnected(10000); zk.close(); //first things first, make sure leadership is lost: state change ts has changed waitForCondition(() -> { return cacheServer.getStateChangeTimestamp() > lastStateChangeTS; }, 100); //when fake zk handle expires we are sure that origina cache server session is going to expire watch2.waitForExpired(10000); //first things first, make sure leadership is acquired again waitForCondition(() -> { return cacheServer.isLeader(); }, 100); //ensure clients reconnect assertTrue(client1.waitForConnection(10000)); assertTrue(client2.waitForConnection(10000)); assertEquals(0, client1.getCacheSize()); assertEquals(0, client2.getCacheSize()); } } }
From source file:blazingcache.server.SimpleZKTest.java
@Test public void sessionExpirationTest_BackupServer() throws Exception { byte[] data = "testdata".getBytes(StandardCharsets.UTF_8); final ServerHostData leaderHostdata = new ServerHostData("localhost", 1234, "leader", false, null); final ServerHostData backupHostdata = new ServerHostData("localhost", 1235, "backup", false, null); try (ZKTestEnv zkEnv = new ZKTestEnv(folderZk.getRoot().toPath()); CacheServer cacheServer = new CacheServer("ciao", leaderHostdata); CacheServer cacheServerBk = new CacheServer("ciao", backupHostdata)) { cacheServer.setupCluster(zkEnv.getAddress(), zkEnv.getTimeout(), zkEnv.getPath(), leaderHostdata, false);//from w ww . ja v a 2s.c om cacheServer.start(); waitForCondition(() -> { return cacheServer.isLeader(); }, 100); //start backupcluster: we are sure this is in backup mode cacheServerBk.setupCluster(zkEnv.getAddress(), zkEnv.getTimeout(), zkEnv.getPath(), backupHostdata, false); cacheServerBk.start(); try (CacheClient client1 = new CacheClient("theClient1", "ciao", new ZKCacheServerLocator(zkEnv.getAddress(), zkEnv.getTimeout(), zkEnv.getPath())); CacheClient client2 = new CacheClient("theClient2", "ciao", new ZKCacheServerLocator(zkEnv.getAddress(), zkEnv.getTimeout(), zkEnv.getPath()))) { client1.start(); client2.start(); assertTrue(client1.waitForConnection(10000)); assertTrue(client2.waitForConnection(10000)); client1.put("pippo", data, 0); client2.put("pippo", data, 0); Assert.assertArrayEquals(data, client1.get("pippo").getSerializedData()); Assert.assertArrayEquals(data, client2.get("pippo").getSerializedData()); assertEquals(1, client1.getCacheSize()); assertEquals(1, client2.getCacheSize()); final long lastStateChangeTS = cacheServer.getStateChangeTimestamp(); /* * Make's ZooKeeper's session expire: * * this is the session id and password to use on a second zookeeper * handle so as to make service monitor's handle to expire */ final long serviceZKSessionId = cacheServer.getZooKeeper().getSessionId(); final byte[] serviceZKpasswd = cacheServer.getZooKeeper().getSessionPasswd(); CountdownWatcher watch2 = new CountdownWatcher("zkexpire"); // make session on cache server's cluster manager zk handle expire final ZooKeeper zk = new ZooKeeper(zkEnv.getAddress(), zkEnv.getTimeout(), watch2, serviceZKSessionId, serviceZKpasswd); watch2.waitForConnected(10000); zk.close(); //first things first, make sure leadership is lost: state change ts has changed waitForCondition(() -> { return cacheServer.getStateChangeTimestamp() > lastStateChangeTS; }, 100); //when fake zk handle expires we are sure that original cache server session is going to expire watch2.waitForExpired(10000); //first things first, make sure leadership is acquired again waitForCondition(() -> { return client1.getCacheSize() == 0 && client2.getCacheSize() == 0; }, 100); waitForCondition(() -> { return !cacheServer.isLeader(); }, 100); waitForCondition(() -> { return cacheServerBk.isLeader(); }, 100); //ensure clients reconnect assertTrue(client1.waitForConnection(10000)); assertTrue(client2.waitForConnection(10000)); client1.put("pippo", data, 0); client2.put("pippo", data, 0); Assert.assertArrayEquals(data, client1.get("pippo").getSerializedData()); Assert.assertArrayEquals(data, client2.get("pippo").getSerializedData()); assertEquals(1, client1.getCacheSize()); assertEquals(1, client2.getCacheSize()); client1.invalidate("pippo"); assertNull(client1.get("pippo")); assertNull(client2.get("pippo")); assertEquals(0, client1.getCacheSize()); assertEquals(0, client2.getCacheSize()); } } }
From source file:co.cask.tephra.distributed.ThriftTransactionServerTest.java
License:Apache License
private void expireZkSession(ZKClientService zkClientService) throws Exception { ZooKeeper zooKeeper = zkClientService.getZooKeeperSupplier().get(); final SettableFuture<?> connectFuture = SettableFuture.create(); Watcher watcher = new Watcher() { @Override// w w w . j a v a2 s.co m public void process(WatchedEvent event) { if (event.getState() == Event.KeeperState.SyncConnected) { connectFuture.set(null); } } }; // Create another Zookeeper session with the same sessionId so that the original one expires. final ZooKeeper dupZookeeper = new ZooKeeper(zkClientService.getConnectString(), zooKeeper.getSessionTimeout(), watcher, zooKeeper.getSessionId(), zooKeeper.getSessionPasswd()); connectFuture.get(30, TimeUnit.SECONDS); Assert.assertEquals("Failed to re-create current session", dupZookeeper.getState(), ZooKeeper.States.CONNECTED); dupZookeeper.close(); }
From source file:com.alibaba.otter.shared.arbitrate.zookeeper.ZooKeeperClientTest.java
License:Apache License
@Test public void testClient() { ZkClientx zk = ZooKeeperClient.getInstance(); // ?zk??//from www . j ava2 s. c om final ZooKeeper zkp = ((ZooKeeperx) zk.getConnection()).getZookeeper(); ClientCnxn cnxn = (ClientCnxn) ReflectionUtils.getField(clientCnxnField, zkp); HostProvider hostProvider = (HostProvider) ReflectionUtils.getField(hostProviderField, cnxn); List<InetSocketAddress> serverAddrs = (List<InetSocketAddress>) ReflectionUtils .getField(serverAddressesField, hostProvider); want.number(serverAddrs.size()).isEqualTo(3); String s1 = serverAddrs.get(0).getAddress().getHostAddress() + ":" + serverAddrs.get(0).getPort(); want.string(s1).isEqualTo(cluster1); Stat stat = new Stat(); try { zkp.getChildren("/otter/channel/304/388", false, stat); System.out.println(stat.getCversion()); } catch (KeeperException e2) { // TODO Auto-generated catch block e2.printStackTrace(); } catch (InterruptedException e2) { // TODO Auto-generated catch block e2.printStackTrace(); } // session timeout final CountDownLatch latch = new CountDownLatch(1); new Thread() { public void run() { try { zkp.getChildren("/", false); } catch (KeeperException e1) { want.fail(); } catch (InterruptedException e1) { want.fail(); } int sessionTimeout = zkp.getSessionTimeout(); long sessionId = zkp.getSessionId(); byte[] passwd = zkp.getSessionPasswd(); try { ZooKeeper newZk = new ZooKeeper(cluster1, sessionTimeout, new Watcher() { public void process(WatchedEvent event) { // do nothing } }, sessionId, passwd); // ?sessionIdclose??SESSION_EXPIRED newZk.close(); } catch (IOException e) { want.fail(); } catch (InterruptedException e) { want.fail(); } latch.countDown(); } }.start(); try { latch.await(); } catch (InterruptedException e) { want.fail(); } zk.getChildren("/"); }
From source file:com.bigdata.zookeeper.AbstractZooTestCase.java
License:Open Source License
/** * Return a new {@link ZooKeeper} instance that is connected to the same * zookeeper ensemble as the given instance and is using the same session * but is nevertheless a distinct instance. * <p>//from w w w . j a va2 s .c o m * Note: This is used by some unit tests to force the given * {@link ZooKeeper} to report a {@link SessionExpiredException} by closing * the returned instance. * * @param zookeeper * A zookeeper instance. * * @return A distinct instance associated with the same session. * * @throws IOException * @throws InterruptedException */ protected ZooKeeper getDistinctZooKeeperForSameSession(final ZooKeeper zookeeper1) throws IOException, InterruptedException { final ZooKeeper zookeeper2 = new ZooKeeper(zookeeperAccessor.hosts, zookeeperAccessor.sessionTimeout, new Watcher() { public void process(WatchedEvent e) { } }, zookeeper1.getSessionId(), zookeeper1.getSessionPasswd()); /* * Wait until this instance is connected. */ final long timeout = TimeUnit.MILLISECONDS.toNanos(1000/* ms */); final long begin = System.nanoTime(); while (zookeeper2.getState() != ZooKeeper.States.CONNECTED && zookeeper2.getState().isAlive()) { final long elapsed = System.nanoTime() - begin; if (elapsed > timeout) { fail("ZooKeeper session did not connect? elapsed=" + TimeUnit.NANOSECONDS.toMillis(elapsed)); } if (log.isInfoEnabled()) { log.info("Awaiting connected."); } Thread.sleep(100/* ms */); } if (!zookeeper2.getState().isAlive()) { fail("Zookeeper died?"); } if (log.isInfoEnabled()) log.info("Zookeeper connected."); return zookeeper2; }
From source file:com.cloudera.flume.master.TestZKBackedConfigStore.java
License:Apache License
/** * This test creates a zkcs and then hijacks its session through another * client. Then we try to use the zkcs to make sure that it's reconnected * correctly.//from w w w . j av a 2 s.c o m */ @Test @Ignore("Timing issue prevents this succeeding on Hudson") public void testLostSessionOK() throws IOException, InterruptedException, KeeperException { File tmp = FileUtil.mktempdir(); FlumeConfiguration cfg = FlumeConfiguration.createTestableConfiguration(); cfg.set(FlumeConfiguration.MASTER_ZK_LOGDIR, tmp.getAbsolutePath()); cfg.set(FlumeConfiguration.MASTER_ZK_SERVERS, "localhost:2181:3181:4181"); cfg.setInt(FlumeConfiguration.MASTER_SERVER_ID, 0); ZooKeeperService zk = new ZooKeeperService(); zk.init(cfg); ZooKeeperConfigStore store = new ZooKeeperConfigStore(zk); store.init(); String defaultFlowName = cfg.getDefaultFlowName(); store.setConfig("foo", defaultFlowName, "bar", "baz"); long sessionid = store.client.zk.getSessionId(); byte[] sessionpass = store.client.zk.getSessionPasswd(); // Force session expiration Watcher watcher = new Watcher() { @Override public void process(WatchedEvent event) { } }; ZooKeeper zkClient = new ZooKeeper("localhost:2181", 1000, watcher, sessionid, sessionpass); zkClient.close(); ZKClient updatingClient = new ZKClient("localhost:2181"); updatingClient.init(); Stat stat = new Stat(); store.client.getChildren("/flume-cfgs", false); byte[] bytes = updatingClient.getData("/flume-cfgs/cfg-0000000000", false, stat); String badCfg = new String(bytes) + "\n1,1,default-flow@@bur : null | null;"; // Force a cfg into ZK to be reloaded by the (hopefully functioning) store updatingClient.create("/flume-cfgs/cfg-", badCfg.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL); assertTrue(store.client.zk.getSessionId() != sessionid); // This sleep is ugly, but we have to wait for the watch to fire Clock.sleep(2000); assertEquals("null", store.getConfig("bur").getSinkConfig()); }
From source file:com.continuuity.weave.internal.zookeeper.KillZKSession.java
License:Apache License
/** * Kills a Zookeeper client to simulate failure scenarious during testing. * Callee will provide the amount of time to wait before it's considered failure * to kill a client./* w w w .j a v a 2 s . c o m*/ * * @param client that needs to be killed. * @param connectionString of Quorum * @param maxMs time in millisecond specifying the max time to kill a client. * @throws IOException When there is IO error * @throws InterruptedException When call has been interrupted. */ public static void kill(ZooKeeper client, String connectionString, int maxMs) throws IOException, InterruptedException { final CountDownLatch latch = new CountDownLatch(1); ZooKeeper zk = new ZooKeeper(connectionString, maxMs, new Watcher() { @Override public void process(WatchedEvent event) { if (event.getState() == Event.KeeperState.SyncConnected) { latch.countDown(); } } }, client.getSessionId(), client.getSessionPasswd()); try { Preconditions.checkState(latch.await(maxMs, TimeUnit.MILLISECONDS), "Fail to kill ZK connection."); } finally { zk.close(); } }
From source file:com.linecorp.armeria.client.endpoint.zookeeper.ZooKeeperEndpointGroupTest.java
License:Apache License
@Test public void testConnectionRecovery() throws Exception { ZooKeeper zkHandler1 = zkEndpointGroup.zkFuture().get(); CountDownLatch latch = new CountDownLatch(1); ZooKeeper zkHandler2;// w w w . j a v a 2 s . co m //create a new handler with the same sessionId and password zkHandler2 = new ZooKeeper(zkInstance.connectString().get(), sessionTimeout, event -> { if (event.getState() == KeeperState.SyncConnected) { latch.countDown(); } }, zkHandler1.getSessionId(), zkHandler1.getSessionPasswd()); latch.await(); //once connected, close the new handler to cause the original handler session expire zkHandler2.close(); for (KeeperState state : expectedStates) { assertEquals(state, zkEndpointGroup.stateQueue().take()); } }