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

The following examples show how to use org.apache.zookeeper.ZooKeeper#close() . 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: SafeModeTest.java    From incubator-retired-blur with Apache License 2.0 6 votes vote down vote up
@Test
public void testSecondNodeStartup() throws IOException, InterruptedException, KeeperException {
  ZooKeeper zk = new ZooKeeper(miniCluster.getZkConnectionString(), 20000, new Watcher() {
    @Override
    public void process(WatchedEvent event) {

    }
  });

  SafeMode setupSafeMode = new SafeMode(zk, "/testing/safemode", "/testing/nodepath", TimeUnit.SECONDS, 5,
      TimeUnit.SECONDS, 60, 0);
  setupSafeMode.registerNode("node10", null);

  SafeMode safeMode = new SafeMode(zk, "/testing/safemode", "/testing/nodepath", TimeUnit.SECONDS, 5,
      TimeUnit.SECONDS, 15, 0);
  try {
    safeMode.registerNode("node10", null);
    fail("should throw exception.");
  } catch (Exception e) {
  }
  zk.close();
}
 
Example 2
Source File: HandleHolder.java    From xian with Apache License 2.0 6 votes vote down vote up
private void internalClose() throws Exception
{
    try
    {
        ZooKeeper zooKeeper = (helper != null) ? helper.getZooKeeper() : null;
        if ( zooKeeper != null )
        {
            Watcher dummyWatcher = new Watcher()
            {
                @Override
                public void process(WatchedEvent event)
                {
                }
            };
            zooKeeper.register(dummyWatcher);   // clear the default watcher so that no new events get processed by mistake
            zooKeeper.close();
        }
    }
    catch ( InterruptedException dummy )
    {
        Thread.currentThread().interrupt();
    }
}
 
Example 3
Source File: SafeModeTest.java    From incubator-retired-blur with Apache License 2.0 6 votes vote down vote up
@Test
public void testExtraNodeStartup() throws IOException, InterruptedException, KeeperException {
  ZooKeeper zk = new ZooKeeper(miniCluster.getZkConnectionString(), 20000, new Watcher() {
    @Override
    public void process(WatchedEvent event) {

    }
  });

  SafeMode setupSafeMode = new SafeMode(zk, "/testing/safemode", "/testing/nodepath", TimeUnit.SECONDS, 5,
      TimeUnit.SECONDS, 60, 0);
  setupSafeMode.registerNode("node1", null);

  SafeMode safeMode = new SafeMode(zk, "/testing/safemode", "/testing/nodepath", TimeUnit.SECONDS, 5,
      TimeUnit.SECONDS, 60, 0);
  long s = System.nanoTime();
  safeMode.registerNode("node101", null);
  long e = System.nanoTime();

  assertTrue((e - s) < TimeUnit.SECONDS.toNanos(1));
  zk.close();
}
 
Example 4
Source File: ThriftServerTest.java    From incubator-retired-blur with Apache License 2.0 6 votes vote down vote up
@Test
public void testNonRootedZkPathWithSlash() throws IOException, KeeperException, InterruptedException {
  ZooKeeper zk = null;
  ZooKeeper nonRooted = null;
  try {
    BlurConfiguration conf = new BlurConfiguration();
    conf.set(BlurConstants.BLUR_ZOOKEEPER_CONNECTION, getZkConnString() + "/");
    zk = ThriftServer.setupZookeeper(conf, "default");

    nonRooted = ZkUtils.newZooKeeper(getZkConnString(), 10000);
    assertTrue("Should really be rooted.",
        ZkUtils.exists(nonRooted,  ZookeeperPathConstants.getClusterPath("default")));
  } finally {
    if (zk != null) {
      zk.close();
    }
    if(nonRooted != null) {
      nonRooted.close();
    }
  }
}
 
Example 5
Source File: ZookeeperTestServerTest.java    From kafka-junit with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Attempts to validate a Zookeeper server by the following criteria.
 *      - connecting
 *      - create a new node
 *      - reading that node
 *      - closing connection
 *
 * @param zkConnectString Zookeeper host.
 */
private void testZookeeperConnection(final String zkConnectString) throws Exception {
    final String pathStr = "/zkTest" + System.currentTimeMillis();
    final String dataStr = "zkData" + System.currentTimeMillis();

    // Attempt to connect using a zookeeper client.
    ZooKeeper zkClient = null;
    try {
        zkClient = createZkClient(zkConnectString);

        // Create a new node storing dataBytes.
        writeZkString(zkClient, pathStr, dataStr);

        // Read back out of Zookeeper
        final String resultStr = readZkString(zkClient, pathStr);

        // Validate we got what we expected.
        Assertions.assertEquals(dataStr, resultStr);
    } finally {
        if (zkClient != null) {
            zkClient.close();
        }
    }
}
 
Example 6
Source File: TestReadOnlyZKClient.java    From hbase with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setUp() throws Exception {
  final int port = UTIL.startMiniZKCluster().getClientPort();
  String hostPort = UTIL.getZkCluster().getAddress().toString();

  ZooKeeper zk = ZooKeeperHelper.getConnectedZooKeeper(hostPort, 10000);
  DATA = new byte[10];
  ThreadLocalRandom.current().nextBytes(DATA);
  zk.create(PATH, DATA, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
  for (int i = 0; i < CHILDREN; i++) {
    zk.create(PATH + "/c" + i, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
  }
  zk.close();
  Configuration conf = UTIL.getConfiguration();
  conf.set(HConstants.ZOOKEEPER_QUORUM, hostPort);
  conf.setInt(ReadOnlyZKClient.RECOVERY_RETRY, 3);
  conf.setInt(ReadOnlyZKClient.RECOVERY_RETRY_INTERVAL_MILLIS, 100);
  conf.setInt(ReadOnlyZKClient.KEEPALIVE_MILLIS, 3000);
  RO_ZK = new ReadOnlyZKClient(conf);
  // only connect when necessary
  assertNull(RO_ZK.zookeeper);
}
 
Example 7
Source File: ConnectionTest.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
@Test
public void zookeeperReconnectTest() throws Exception {
    ZooKeeper zookeeper = new ZooKeeper(ts.getConnectString(), 5000, null);

    try {
        boolean pass = awaitState(zookeeper, ZooKeeper.States.CONNECTED);
        Assert.assertTrue(pass);

        ts.restart();
        pass = awaitState(zookeeper, ZooKeeper.States.CONNECTED);
        Assert.assertTrue(pass);
    } finally {
        if (zookeeper != null) {
            zookeeper.close();
        }
    }
}
 
Example 8
Source File: Util.java    From kafka-service-broker with Apache License 2.0 6 votes vote down vote up
List<String> getBootstrapServers() throws Exception {
    List<String> ret = new ArrayList<>();
    ZooKeeper zk = null;
    try {
        zk = getZooKeeper();

        List<String> ids = zk.getChildren("/brokers/ids", false);
        for (String id : ids) {
            String brokerInfo = new String(zk.getData("/brokers/ids/" + id, false, null));
            Map m = toMap(brokerInfo);
            ret.add(m.get("host") + ":" + m.get("port"));
        }
        return ret;
    } finally {
        if(zk != null) {
            zk.close();
        }
    }
}
 
Example 9
Source File: ClusterTest.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
@Test
public void clusterTest1() throws Exception {
    ZooKeeper zookeeper = new ZooKeeper(zookeeperAddress, 5000, null);
    awaitZookeeperConnected(zookeeper);

    if (zookeeper != null) {
        zookeeper.close();
    }
}
 
Example 10
Source File: HandleHolder.java    From curator with Apache License 2.0 5 votes vote down vote up
private void internalClose(int waitForShutdownTimeoutMs) throws Exception
{
    try
    {
        ZooKeeper zooKeeper = (helper != null) ? helper.getZooKeeper() : null;
        if ( zooKeeper != null )
        {
            Watcher dummyWatcher = new Watcher()
            {
                @Override
                public void process(WatchedEvent event)
                {
                }
            };
            zooKeeper.register(dummyWatcher);   // clear the default watcher so that no new events get processed by mistake
            if ( waitForShutdownTimeoutMs == 0 )
            {
                zooKeeper.close();  // coming from closeAndReset() which is executed in ZK's event thread. Cannot use zooKeeper.close(n) otherwise we'd get a dead lock
            }
            else
            {
                zooKeeper.close(waitForShutdownTimeoutMs);
            }
        }
    }
    catch ( InterruptedException dummy )
    {
        Thread.currentThread().interrupt();
    }
}
 
Example 11
Source File: ZookeeperDiscoverySegmentationAndConnectionRestoreTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @param spi Spi instance.
 */
private static void closeZkClient(ZookeeperDiscoverySpi spi) {
    ZooKeeper zk = ZookeeperDiscoverySpiTestHelper.zkClient(spi);

    try {
        zk.close();
    }
    catch (Exception e) {
        fail("Unexpected error: " + e);
    }
}
 
Example 12
Source File: UtilTest.java    From kafka-service-broker with Apache License 2.0 5 votes vote down vote up
@Test
public void testZoo() throws Exception {
    ZooKeeper z = null;
    try {
        z = util.getZooKeeper();
        assertNotNull(z);
        assertNotNull(z.getSessionId());
    } finally {
        if (z != null) {
            z.close();
        }
    }
}
 
Example 13
Source File: ConnectionTest.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
@Test
public void zookeeperExpiredTest() throws Exception {
    ZooKeeper zookeeper = new ZooKeeper(ts.getConnectString(), 5000, null);

    try {
        boolean pass = awaitState(zookeeper, ZooKeeper.States.CONNECTED);
        Assert.assertTrue(pass);

        ts.restart();
        pass = awaitState(zookeeper, ZooKeeper.States.CONNECTED);
        Assert.assertTrue(pass);

        ts.stop();
        ts.close();

        pass = awaitState(zookeeper, ZooKeeper.States.CONNECTING);
        Assert.assertTrue(pass);

        ts = createTestingServer();

        pass = awaitState(zookeeper, ZooKeeper.States.CONNECTED);
        Assert.assertFalse(pass);
    } finally {
        if (zookeeper != null) {
            zookeeper.close();
        }
    }
}
 
Example 14
Source File: ZooKeeperMigrator.java    From nifi with Apache License 2.0 5 votes vote down vote up
private void closeZooKeeper(ZooKeeper zooKeeper) {
    try {
        zooKeeper.close();
    } catch (InterruptedException e) {
        LOGGER.warn("could not close ZooKeeper client due to interrupt", e);
        Thread.currentThread().interrupt();
    }
}
 
Example 15
Source File: h2oworker.java    From h2o-2 with Apache License 2.0 5 votes vote down vote up
@Override
public void notifyAboutEmbeddedWebServerIpPort (InetAddress ip, int port) {
  _embeddedWebServerIp = ip.getHostAddress();
  _embeddedWebServerPort = port;

  try {
    ZooKeeper z = ZooKeeperFactory.makeZk(_zk);
    byte[] payload;
    payload = z.getData(_zkroot, null, null);
    ClusterPayload cp = ClusterPayload.fromPayload(payload, ClusterPayload.class);
    _numNodes = cp.numNodes;
    if (_numNodes <= 0) {
      System.out.println("ERROR: numNodes must be > 0 (" + _numNodes + ")");
      System.exit(1);
    }

    WorkerPayload wp = new WorkerPayload();
    wp.ip = ip.getHostAddress();
    wp.port = port;
    wp.pid = getProcessId();
    payload = wp.toPayload();
    z.create(_zkroot + "/nodes/", payload, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
    z.close();
  }
  catch (Exception e) {
    e.printStackTrace();
    System.exit(1);
  }
}
 
Example 16
Source File: ZooKeeperConnection.java    From javatech with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
public static void main(String[] args) throws IOException, InterruptedException {
    ZooKeeperConnection zooKeeperConnection = new ZooKeeperConnection();
    ZooKeeper zk = zooKeeperConnection.connect(HOST);
    States state = zk.getState();
    System.out.println("ZooKeeper isAlive:" + state.isAlive());
    zk.close();
}
 
Example 17
Source File: ZookeeperTestServerTest.java    From kafka-junit with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Test restarting preserves data stored.
 */
@Test
void testRestartPreservesData() throws Exception {
    final String pathStr = "/preservedData" + System.currentTimeMillis();
    final String dataStr = "MySpecialData" + System.currentTimeMillis();

    // Create instance.
    try (final ZookeeperTestServer zookeeperTestServer = new ZookeeperTestServer()) {
        // Start service
        zookeeperTestServer.start();

        ZooKeeper zkClient = null;
        try {
            // Create client
            zkClient = createZkClient(zookeeperTestServer.getConnectString());

            // Write data
            writeZkString(zkClient, pathStr, dataStr);
        } finally {
            // Close client.
            if (zkClient != null) {
                zkClient.close();
            }
        }

        // Call restart on server.
        zookeeperTestServer.restart();

        // Attempt to read original data out.
        try {
            // Create client
            zkClient = createZkClient(zookeeperTestServer.getConnectString());

            // Write data
            final String result = readZkString(zkClient, pathStr);
            Assertions.assertEquals(dataStr, result);
        } finally {
            // Close client.
            if (zkClient != null) {
                zkClient.close();
            }
        }
    }
}
 
Example 18
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 19
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 20
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()));
}