Java Code Examples for org.apache.zookeeper.ZooKeeper#getSessionPasswd()

The following examples show how to use org.apache.zookeeper.ZooKeeper#getSessionPasswd() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ThriftTransactionServerTest.java    From phoenix-tephra with Apache License 2.0 6 votes vote down vote up
private void expireZkSession(ZKClientService zkClientService) throws Exception {
  ZooKeeper zooKeeper = zkClientService.getZooKeeperSupplier().get();
  final SettableFuture<?> connectFuture = SettableFuture.create();
  Watcher watcher = new Watcher() {
    @Override
    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.
  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();
}
 
Example 2
Source File: KillZKSession.java    From twill with Apache License 2.0 6 votes vote down vote up
/**
 * 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.
 *
 * @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();
  }
}
 
Example 3
Source File: TestZooKeeperClient.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
private void expireZooKeeperSession(ZooKeeper zk, int timeout)
        throws IOException, InterruptedException, KeeperException {
    final CountDownLatch latch = new CountDownLatch(1);

    ZooKeeper newZk = new ZooKeeper(zkServers, timeout, new Watcher() {
        @Override
        public void process(WatchedEvent event) {
            if (event.getType() == EventType.None && event.getState() == KeeperState.SyncConnected) {
                latch.countDown();
            }
        }},
        zk.getSessionId(),
        zk.getSessionPasswd());

    if (!latch.await(timeout, TimeUnit.MILLISECONDS)) {
        throw KeeperException.create(KeeperException.Code.CONNECTIONLOSS);
    }

    newZk.close();
}
 
Example 4
Source File: TestZooKeeperClient.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
private void expireZooKeeperSession(ZooKeeper zk, int timeout)
        throws IOException, InterruptedException, KeeperException {
    final CountDownLatch latch = new CountDownLatch(1);

    ZooKeeper newZk = new ZooKeeper(zkServers, timeout, new Watcher() {
        @Override
        public void process(WatchedEvent event) {
            if (event.getType() == EventType.None && event.getState() == KeeperState.SyncConnected) {
                latch.countDown();
            }
        }},
        zk.getSessionId(),
        zk.getSessionPasswd());

    if (!latch.await(timeout, TimeUnit.MILLISECONDS)) {
        throw KeeperException.create(KeeperException.Code.CONNECTIONLOSS);
    }

    newZk.close();
}
 
Example 5
Source File: ZkTestBase.java    From helix with Apache License 2.0 6 votes vote down vote up
protected void simulateSessionExpiry(ZkConnection zkConnection)
    throws IOException, InterruptedException {
  ZooKeeper oldZookeeper = zkConnection.getZookeeper();
  LOG.info("Old sessionId = " + oldZookeeper.getSessionId());

  Watcher watcher = new Watcher() {
    @Override
    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());
}
 
Example 6
Source File: ZookeeperMetadataStorageManagerTest.java    From herddb with Apache License 2.0 5 votes vote down vote up
@Test
public void testSessionExpired() throws Exception {
    try (ZookeeperMetadataStorageManager man = new ZookeeperMetadataStorageManager(testEnv.getAddress(),
            testEnv.getTimeout(), testEnv.getPath())) {
        man.start();

        TableSpace tableSpace = TableSpace
                .builder()
                .leader("test")
                .replica("test")
                .name(TableSpace.DEFAULT)
                .build();
        man.registerTableSpace(tableSpace);
        assertEquals(1, man.listTableSpaces().size());
        ZooKeeper actual = man.getZooKeeper();
        long sessionId = actual.getSessionId();
        byte[] passwd = actual.getSessionPasswd();
        expireZkSession(sessionId, passwd);
        for (int i = 0; i < 10; i++) {
            try {
                man.listTableSpaces();
                fail("session should be expired or not connected");
            } catch (MetadataStorageManagerException ok) {
                System.out.println("ok: " + ok);
                assertTrue(ok.getCause() instanceof KeeperException.ConnectionLossException
                        || ok.getCause() instanceof KeeperException.SessionExpiredException);
                if (ok.getCause() instanceof KeeperException.SessionExpiredException) {
                    break;
                }
            }
            Thread.sleep(500);
        }
        assertNotSame(actual, man.getZooKeeper());
        assertEquals(1, man.listTableSpaces().size());

        assertNotNull(tableSpace = man.describeTableSpace(TableSpace.DEFAULT));
        man.dropTableSpace(TableSpace.DEFAULT, tableSpace);
    }
}
 
Example 7
Source File: ZkTestHelper.java    From helix with Apache License 2.0 5 votes vote down vote up
/**
 * expire zk session asynchronously
 * @param client
 * @throws Exception
 */
public static void asyncExpireSession(RealmAwareZkClient client) throws Exception {
  final ZkClient zkClient = (ZkClient) client;
  ZkConnection connection = ((ZkConnection) zkClient.getConnection());
  ZooKeeper curZookeeper = connection.getZookeeper();
  LOG.info("Before expiry. sessionId: " + Long.toHexString(curZookeeper.getSessionId()));

  Watcher watcher = new Watcher() {
    @Override
    public void process(WatchedEvent event) {
      LOG.info("Process watchEvent: " + event);
    }
  };

  final ZooKeeper dupZookeeper =
      new ZooKeeper(connection.getServers(), curZookeeper.getSessionTimeout(), watcher,
          curZookeeper.getSessionId(), curZookeeper.getSessionPasswd());
  // wait until connected, then close
  while (dupZookeeper.getState() != States.CONNECTED) {
    Thread.sleep(10);
  }
  dupZookeeper.close();

  connection = (ZkConnection) zkClient.getConnection();
  curZookeeper = connection.getZookeeper();

  // System.err.println("zk: " + oldZookeeper);
  LOG.info("After expiry. sessionId: " + Long.toHexString(curZookeeper.getSessionId()));
}
 
Example 8
Source File: ZkTestHelper.java    From helix with Apache License 2.0 5 votes vote down vote up
/**
 * expire zk session asynchronously
 * @param client
 * @throws Exception
 */
public static void asyncExpireSession(RealmAwareZkClient client)
    throws Exception {
  final ZkClient zkClient = (ZkClient) client;
  ZkConnection connection = ((ZkConnection) zkClient.getConnection());
  ZooKeeper curZookeeper = connection.getZookeeper();
  LOG.info("Before expiry. sessionId: " + Long.toHexString(curZookeeper.getSessionId()));

  Watcher watcher = new Watcher() {
    @Override
    public void process(WatchedEvent event) {
      LOG.info("Process watchEvent: " + event);
    }
  };

  final ZooKeeper dupZookeeper =
      new ZooKeeper(connection.getServers(), curZookeeper.getSessionTimeout(), watcher,
          curZookeeper.getSessionId(), curZookeeper.getSessionPasswd());
  // wait until connected, then close
  while (dupZookeeper.getState() != States.CONNECTED) {
    Thread.sleep(10);
  }
  dupZookeeper.close();

  connection = (ZkConnection) zkClient.getConnection();
  curZookeeper = connection.getZookeeper();

  // System.err.println("zk: " + oldZookeeper);
  LOG.info("After expiry. sessionId: " + Long.toHexString(curZookeeper.getSessionId()));
}
 
Example 9
Source File: HBaseTestingUtility.java    From hbase with Apache License 2.0 4 votes vote down vote up
/**
 * Expire a ZooKeeper session as recommended in ZooKeeper documentation
 * http://hbase.apache.org/book.html#trouble.zookeeper
 * There are issues when doing this:
 * [1] http://www.mail-archive.com/[email protected]/msg01942.html
 * [2] https://issues.apache.org/jira/browse/ZOOKEEPER-1105
 *
 * @param nodeZK - the ZK watcher to expire
 * @param checkStatus - true to check if we can create a Table with the
 *                    current configuration.
 */
public void expireSession(ZKWatcher nodeZK, boolean checkStatus)
  throws Exception {
  Configuration c = new Configuration(this.conf);
  String quorumServers = ZKConfig.getZKQuorumServersString(c);
  ZooKeeper zk = nodeZK.getRecoverableZooKeeper().getZooKeeper();
  byte[] password = zk.getSessionPasswd();
  long sessionID = zk.getSessionId();

  // Expiry seems to be asynchronous (see comment from P. Hunt in [1]),
  //  so we create a first watcher to be sure that the
  //  event was sent. We expect that if our watcher receives the event
  //  other watchers on the same machine will get is as well.
  // When we ask to close the connection, ZK does not close it before
  //  we receive all the events, so don't have to capture the event, just
  //  closing the connection should be enough.
  ZooKeeper monitor = new ZooKeeper(quorumServers,
    1000, new org.apache.zookeeper.Watcher(){
    @Override
    public void process(WatchedEvent watchedEvent) {
      LOG.info("Monitor ZKW received event="+watchedEvent);
    }
  } , sessionID, password);

  // Making it expire
  ZooKeeper newZK = new ZooKeeper(quorumServers,
      1000, EmptyWatcher.instance, sessionID, password);

  //ensure that we have connection to the server before closing down, otherwise
  //the close session event will be eaten out before we start CONNECTING state
  long start = System.currentTimeMillis();
  while (newZK.getState() != States.CONNECTED
       && System.currentTimeMillis() - start < 1000) {
     Thread.sleep(1);
  }
  newZK.close();
  LOG.info("ZK Closed Session 0x" + Long.toHexString(sessionID));

  // Now closing & waiting to be sure that the clients get it.
  monitor.close();

  if (checkStatus) {
    getConnection().getTable(TableName.META_TABLE_NAME).close();
  }
}
 
Example 10
Source File: ZkTestHelper.java    From helix with Apache License 2.0 4 votes vote down vote up
/**
 * Expire current zk session and wait for {@link IZkStateListener#handleNewSession(String)} invoked
 * @param client
 * @throws Exception
 */

public static void disconnectSession(HelixZkClient client) throws Exception {
  final ZkClient zkClient = (ZkClient) client;
  IZkStateListener listener = new IZkStateListener() {
    @Override
    public void handleStateChanged(KeeperState state) throws Exception {
      // System.err.println("disconnectSession handleStateChanged. state: " + state);
    }

    @Override
    public void handleNewSession(final String sessionId) throws Exception {
      // make sure zkclient is connected again
      zkClient.waitUntilConnected(HelixZkClient.DEFAULT_CONNECTION_TIMEOUT, TimeUnit.SECONDS);

      LOG.info("handleNewSession. sessionId: {}.", sessionId);
    }

    @Override
    public void handleSessionEstablishmentError(Throwable var1) throws Exception {
    }
  };

  zkClient.subscribeStateChanges(listener);
  ZkConnection connection = (ZkConnection) zkClient.getConnection();
  ZooKeeper curZookeeper = connection.getZookeeper();
  LOG.info("Before expiry. sessionId: " + Long.toHexString(curZookeeper.getSessionId()));

  Watcher watcher = new Watcher() {
    @Override
    public void process(WatchedEvent event) {
      LOG.info("Process watchEvent: " + event);
    }
  };

  final ZooKeeper dupZookeeper =
      new ZooKeeper(connection.getServers(), curZookeeper.getSessionTimeout(), watcher,
          curZookeeper.getSessionId(), curZookeeper.getSessionPasswd());
  // wait until connected, then close
  while (dupZookeeper.getState() != States.CONNECTED) {
    Thread.sleep(10);
  }
  dupZookeeper.close();

  connection = (ZkConnection) zkClient.getConnection();
  curZookeeper = connection.getZookeeper();
  zkClient.unsubscribeStateChanges(listener);

  // System.err.println("zk: " + oldZookeeper);
  LOG.info("After expiry. sessionId: " + Long.toHexString(curZookeeper.getSessionId()));
}
 
Example 11
Source File: ZkTestHelper.java    From helix with Apache License 2.0 4 votes vote down vote up
public static void expireSession(RealmAwareZkClient client) throws Exception {
  final CountDownLatch waitNewSession = new CountDownLatch(1);
  final ZkClient zkClient = (ZkClient) client;

  IZkStateListener listener = new IZkStateListener() {
    @Override
    public void handleStateChanged(KeeperState state) throws Exception {
      LOG.info("IZkStateListener#handleStateChanged, state: " + state);
    }

    @Override
    public void handleNewSession(final String sessionId) throws Exception {
      // make sure zkclient is connected again
      zkClient.waitUntilConnected(HelixZkClient.DEFAULT_CONNECTION_TIMEOUT, TimeUnit.SECONDS);

      LOG.info("handleNewSession. sessionId: {}.", sessionId);
      waitNewSession.countDown();
    }

    @Override
    public void handleSessionEstablishmentError(Throwable var1) throws Exception {
    }
  };

  zkClient.subscribeStateChanges(listener);

  ZkConnection connection = ((ZkConnection) zkClient.getConnection());
  ZooKeeper curZookeeper = connection.getZookeeper();
  String oldSessionId = Long.toHexString(curZookeeper.getSessionId());
  LOG.info("Before session expiry. sessionId: " + oldSessionId + ", zk: " + curZookeeper);

  Watcher watcher = new Watcher() {
    @Override
    public void process(WatchedEvent event) {
      LOG.info("Watcher#process, event: " + event);
    }
  };

  final ZooKeeper dupZookeeper =
      new ZooKeeper(connection.getServers(), curZookeeper.getSessionTimeout(), watcher,
          curZookeeper.getSessionId(), curZookeeper.getSessionPasswd());
  // wait until connected, then close
  while (dupZookeeper.getState() != States.CONNECTED) {
    Thread.sleep(10);
  }
  Assert.assertEquals(dupZookeeper.getState(), States.CONNECTED,
      "Fail to connect to zk using current session info");
  dupZookeeper.close();

  // make sure session expiry really happens
  waitNewSession.await();
  zkClient.unsubscribeStateChanges(listener);

  connection = (ZkConnection) zkClient.getConnection();
  curZookeeper = connection.getZookeeper();

  String newSessionId = Long.toHexString(curZookeeper.getSessionId());
  LOG.info("After session expiry. sessionId: " + newSessionId + ", zk: " + curZookeeper);
  Assert.assertFalse(newSessionId.equals(oldSessionId),
      "Fail to expire current session, zk: " + curZookeeper);
}
 
Example 12
Source File: ZkTestBase.java    From helix with Apache License 2.0 4 votes vote down vote up
protected void simulateSessionExpiry(HelixZkClient client)
    throws IOException, InterruptedException, IOException {
  ZkClient zkClient = (ZkClient) client;

  IZkStateListener listener = new IZkStateListener() {
    @Override
    public void handleStateChanged(Watcher.Event.KeeperState state) throws Exception {
      LOG.info("In Old connection, state changed:" + state);
    }

    @Override
    public void handleNewSession(final String sessionId) throws Exception {
      LOG.info("In Old connection, new session: {}.", sessionId);
    }

    @Override
    public void handleSessionEstablishmentError(Throwable var1) throws Exception {
    }
  };
  zkClient.subscribeStateChanges(listener);
  ZkConnection connection = ((ZkConnection) zkClient.getConnection());
  ZooKeeper oldZookeeper = connection.getZookeeper();
  LOG.info("Old sessionId = " + oldZookeeper.getSessionId());

  Watcher watcher = new Watcher() {
    @Override
    public void process(WatchedEvent event) {
      LOG.info("In New connection, process event:" + event);
    }
  };

  ZooKeeper newZookeeper =
      new ZooKeeper(connection.getServers(), oldZookeeper.getSessionTimeout(), watcher,
          oldZookeeper.getSessionId(), oldZookeeper.getSessionPasswd());
  LOG.info("New sessionId = " + newZookeeper.getSessionId());
  // Thread.sleep(3000);
  newZookeeper.close();
  Thread.sleep(10000);
  connection = (ZkConnection) zkClient.getConnection();
  oldZookeeper = connection.getZookeeper();
  LOG.info("After session expiry sessionId = " + oldZookeeper.getSessionId());
}
 
Example 13
Source File: ZkTestHelper.java    From helix with Apache License 2.0 4 votes vote down vote up
/**
 * Expire current zk session and wait for {@link IZkStateListener#handleNewSession(String)} invoked
 * @param client
 * @throws Exception
 */
public static void disconnectSession(HelixZkClient client)
    throws Exception {
  final ZkClient zkClient = (ZkClient) client;
  IZkStateListener listener = new IZkStateListener() {
    @Override
    public void handleStateChanged(KeeperState state)
        throws Exception {
      // System.err.println("disconnectSession handleStateChanged. state: " + state);
    }

    @Override
    public void handleNewSession(final String sessionId)
        throws Exception {
      // make sure zkclient is connected again
      zkClient.waitUntilConnected(HelixZkClient.DEFAULT_CONNECTION_TIMEOUT, TimeUnit.SECONDS);

      LOG.info("handleNewSession. sessionId: {}.", sessionId);
    }

    @Override
    public void handleSessionEstablishmentError(Throwable var1)
        throws Exception {
    }
  };

  zkClient.subscribeStateChanges(listener);
  ZkConnection connection = (ZkConnection) zkClient.getConnection();
  ZooKeeper curZookeeper = connection.getZookeeper();
  LOG.info("Before expiry. sessionId: " + Long.toHexString(curZookeeper.getSessionId()));

  Watcher watcher = new Watcher() {
    @Override
    public void process(WatchedEvent event) {
      LOG.info("Process watchEvent: " + event);
    }
  };

  final ZooKeeper dupZookeeper =
      new ZooKeeper(connection.getServers(), curZookeeper.getSessionTimeout(), watcher,
          curZookeeper.getSessionId(), curZookeeper.getSessionPasswd());
  // wait until connected, then close
  while (dupZookeeper.getState() != States.CONNECTED) {
    Thread.sleep(10);
  }
  dupZookeeper.close();

  connection = (ZkConnection) zkClient.getConnection();
  curZookeeper = connection.getZookeeper();
  zkClient.unsubscribeStateChanges(listener);

  // System.err.println("zk: " + oldZookeeper);
  LOG.info("After expiry. sessionId: " + Long.toHexString(curZookeeper.getSessionId()));
}
 
Example 14
Source File: ZkTestHelper.java    From helix with Apache License 2.0 4 votes vote down vote up
public static void expireSession(RealmAwareZkClient client)
    throws Exception {
  final CountDownLatch waitNewSession = new CountDownLatch(1);
  final ZkClient zkClient = (ZkClient) client;

  IZkStateListener listener = new IZkStateListener() {
    @Override
    public void handleStateChanged(KeeperState state)
        throws Exception {
      LOG.info("IZkStateListener#handleStateChanged, state: " + state);
    }

    @Override
    public void handleNewSession(final String sessionId)
        throws Exception {
      // make sure zkclient is connected again
      zkClient.waitUntilConnected(HelixZkClient.DEFAULT_CONNECTION_TIMEOUT, TimeUnit.SECONDS);

      LOG.info("handleNewSession. sessionId: {}.", sessionId);
      waitNewSession.countDown();
    }

    @Override
    public void handleSessionEstablishmentError(Throwable var1)
        throws Exception {
    }
  };

  zkClient.subscribeStateChanges(listener);

  ZkConnection connection = ((ZkConnection) zkClient.getConnection());
  ZooKeeper curZookeeper = connection.getZookeeper();
  String oldSessionId = Long.toHexString(curZookeeper.getSessionId());
  LOG.info("Before session expiry. sessionId: " + oldSessionId + ", zk: " + curZookeeper);

  Watcher watcher = new Watcher() {
    @Override
    public void process(WatchedEvent event) {
      LOG.info("Watcher#process, event: " + event);
    }
  };

  final ZooKeeper dupZookeeper =
      new ZooKeeper(connection.getServers(), curZookeeper.getSessionTimeout(), watcher,
          curZookeeper.getSessionId(), curZookeeper.getSessionPasswd());
  // wait until connected, then close
  while (dupZookeeper.getState() != States.CONNECTED) {
    Thread.sleep(10);
  }
  Assert.assertEquals(dupZookeeper.getState(), States.CONNECTED,
      "Fail to connect to zk using current session info");
  dupZookeeper.close();

  // make sure session expiry really happens
  waitNewSession.await();
  zkClient.unsubscribeStateChanges(listener);

  connection = (ZkConnection) zkClient.getConnection();
  curZookeeper = connection.getZookeeper();

  String newSessionId = Long.toHexString(curZookeeper.getSessionId());
  LOG.info("After session expiry. sessionId: " + newSessionId + ", zk: " + curZookeeper);
  Assert.assertFalse(newSessionId.equals(oldSessionId),
      "Fail to expire current session, zk: " + curZookeeper);
}