List of usage examples for org.apache.zookeeper ZooKeeper getSessionPasswd
public byte[] getSessionPasswd()
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/*from w ww. j av a 2 s.com*/ 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??//w w w . ja v a 2 s.c o m 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 ww w . ja v a 2 s. c om*/ * 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.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./*from w ww . java 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;//from ww w. j av a 2s .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()); } }
From source file:com.linecorp.armeria.client.zookeeper.EndpointGroupTest.java
License:Apache License
@Test public void testConnectionRecovery() throws Exception { ZooKeeper zkHandler1 = endpointGroup.underlyingClient(); CountDownLatch latch = new CountDownLatch(1); ZooKeeper zkHandler2;//from www. j av a 2s. com //create a new handler with the same sessionId and password zkHandler2 = new ZooKeeper(instance().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, endpointGroup.stateQueue().take()); } testGetEndpointGroup(); }
From source file:com.linecorp.armeria.server.zookeeper.ZooKeeperRegistrationTest.java
License:Apache License
@Ignore // FIXME: https://github.com/line/armeria/issues/477 @Test/* www. j av a 2s .com*/ public void testConnectionRecovery() throws Exception { ZooKeeperRegistration zkConnector = zkConnectors.get(0); zkConnector.enableStateRecording(); ZooKeeper zkHandler1 = zkConnector.underlyingClient(); CountDownLatch latch = new CountDownLatch(1); ZooKeeper zkHandler2; //create a new handler with the same sessionId and password zkHandler2 = new ZooKeeper(instance().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, zkConnector.stateQueue().take()); } //connection will recover and our ZooKeeper node also exists testServerNodeCreateAndDelete(); }
From source file:com.linkedin.d2.discovery.stores.zk.ZKTestUtil.java
License:Apache License
public static void expireSession(String connectString, ZooKeeper zk, long timeout, TimeUnit timeoutUnit) throws IOException, TimeoutException, InterruptedException { expireSession(connectString, zk.getSessionTimeout(), zk.getSessionId(), zk.getSessionPasswd(), timeout, timeoutUnit);//from www.j a v a2 s . c o m }
From source file:com.linkedin.helix.TestZkClientWrapper.java
License:Apache License
@Test() void testSessioExpire() { IZkStateListener listener = new IZkStateListener() { @Override/*from w ww .jav a 2s . c om*/ public void handleStateChanged(KeeperState state) throws Exception { System.out.println("In Old connection New state " + state); } @Override public void handleNewSession() throws Exception { System.out.println("In Old connection New session"); } }; _zkClient.subscribeStateChanges(listener); ZkConnection connection = ((ZkConnection) _zkClient.getConnection()); ZooKeeper zookeeper = connection.getZookeeper(); System.out.println("old sessionId= " + zookeeper.getSessionId()); try { Watcher watcher = new Watcher() { @Override public void process(WatchedEvent event) { System.out.println("In New connection In process event:" + event); } }; ZooKeeper newZookeeper = new ZooKeeper(connection.getServers(), zookeeper.getSessionTimeout(), watcher, zookeeper.getSessionId(), zookeeper.getSessionPasswd()); Thread.sleep(3000); System.out.println("New sessionId= " + newZookeeper.getSessionId()); Thread.sleep(3000); newZookeeper.close(); Thread.sleep(10000); connection = ((ZkConnection) _zkClient.getConnection()); zookeeper = connection.getZookeeper(); System.out.println("After session expiry sessionId= " + zookeeper.getSessionId()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:com.linkedin.helix.ZkUnitTestBase.java
License:Apache License
protected void simulateSessionExpiry(ZkConnection zkConnection) throws IOException, InterruptedException { ZooKeeper oldZookeeper = zkConnection.getZookeeper(); LOG.info("Old sessionId = " + oldZookeeper.getSessionId()); Watcher watcher = new Watcher() { @Override// w w w .ja v a 2 s . c o m public void process(WatchedEvent event) { LOG.info("In New connection, process event:" + event); } }; ZooKeeper newZookeeper = new ZooKeeper(zkConnection.getServers(), oldZookeeper.getSessionTimeout(), watcher, oldZookeeper.getSessionId(), oldZookeeper.getSessionPasswd()); LOG.info("New sessionId = " + newZookeeper.getSessionId()); // Thread.sleep(3000); newZookeeper.close(); Thread.sleep(10000); oldZookeeper = zkConnection.getZookeeper(); LOG.info("After session expiry sessionId = " + oldZookeeper.getSessionId()); }