Search in sources :

Example 1 with ZooKeeperConnectionException

use of com.twitter.common.zookeeper.ZooKeeperClient.ZooKeeperConnectionException in project commons by twitter.

the class ZooKeeperNode method init.

/**
   * Initialize zookeeper tracking for this {@link Supplier}.  Once this call returns, this object
   * will be tracking data in zookeeper.
   *
   * @throws InterruptedException if the underlying zookeeper server transaction is interrupted
   * @throws KeeperException if the server signals an error
   * @throws ZooKeeperConnectionException if there was a problem connecting to the zookeeper
   *     cluster
   */
@VisibleForTesting
void init() throws InterruptedException, KeeperException, ZooKeeperConnectionException {
    Watcher watcher = zkClient.registerExpirationHandler(new Command() {

        @Override
        public void execute() {
            try {
                synchronized (safeToRewatchLock) {
                    if (safeToRewatch) {
                        tryWatchDataNode();
                    }
                }
            } catch (InterruptedException e) {
                LOG.log(Level.WARNING, "Interrupted while trying to re-establish watch.", e);
                Thread.currentThread().interrupt();
            }
        }
    });
    try {
        /*
       * Synchronize to prevent the race of watchDataNode completing and then the session expiring
       * before we update safeToRewatch.
       */
        synchronized (safeToRewatchLock) {
            watchDataNode();
            safeToRewatch = true;
        }
    } catch (InterruptedException e) {
        zkClient.unregister(watcher);
        throw e;
    } catch (KeeperException e) {
        zkClient.unregister(watcher);
        throw e;
    } catch (ZooKeeperConnectionException e) {
        zkClient.unregister(watcher);
        throw e;
    }
}
Also used : Command(com.twitter.common.base.Command) Watcher(org.apache.zookeeper.Watcher) ZooKeeperConnectionException(com.twitter.common.zookeeper.ZooKeeperClient.ZooKeeperConnectionException) KeeperException(org.apache.zookeeper.KeeperException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with ZooKeeperConnectionException

use of com.twitter.common.zookeeper.ZooKeeperClient.ZooKeeperConnectionException in project commons by twitter.

the class ZooKeeperClientTest method testGet.

@Test
public void testGet() throws Exception {
    final ZooKeeperClient zkClient = createZkClient();
    shutdownNetwork();
    try {
        zkClient.get(Amount.of(50L, Time.MILLISECONDS));
        fail("Expected client connection to timeout while network down");
    } catch (TimeoutException e) {
        assertTrue(zkClient.isClosed());
    }
    assertNull(zkClient.getZooKeeperClientForTests());
    final CountDownLatch blockingGetComplete = new CountDownLatch(1);
    final AtomicReference<ZooKeeper> client = new AtomicReference<ZooKeeper>();
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                client.set(zkClient.get());
            } catch (ZooKeeperConnectionException e) {
                throw new RuntimeException(e);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } finally {
                blockingGetComplete.countDown();
            }
        }
    }).start();
    restartNetwork();
    // Hung blocking connects should succeed when server connection comes up
    blockingGetComplete.await();
    assertNotNull(client.get());
    // New connections should succeed now that network is back up
    long sessionId = zkClient.get().getSessionId();
    // While connected the same client should be reused (no new connections while healthy)
    assertSame(client.get(), zkClient.get());
    shutdownNetwork();
    // Our client doesn't know the network is down yet so we should be able to get()
    ZooKeeper zooKeeper = zkClient.get();
    try {
        zooKeeper.exists("/", false);
        fail("Expected client operation to fail while network down");
    } catch (ConnectionLossException e) {
    // expected
    }
    restartNetwork();
    assertEquals("Expected connection to be re-established with existing session", sessionId, zkClient.get().getSessionId());
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) ZooKeeperConnectionException(com.twitter.common.zookeeper.ZooKeeperClient.ZooKeeperConnectionException) CountDownLatch(java.util.concurrent.CountDownLatch) ConnectionLossException(org.apache.zookeeper.KeeperException.ConnectionLossException) ZooKeeper(org.apache.zookeeper.ZooKeeper) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test) BaseZooKeeperTest(com.twitter.common.zookeeper.testing.BaseZooKeeperTest)

Example 3 with ZooKeeperConnectionException

use of com.twitter.common.zookeeper.ZooKeeperClient.ZooKeeperConnectionException in project commons by twitter.

the class ZooKeeperClientTest method testGetInterrupted.

/**
   * Test that if a blocking get() call gets interrupted, after a connection has been created
   * but before it's connected, the zk connection gets closed.
   */
@Test
public void testGetInterrupted() throws Exception {
    final ZooKeeperClient zkClient = createZkClient();
    shutdownNetwork();
    final CountDownLatch blockingGetComplete = new CountDownLatch(1);
    final AtomicBoolean interrupted = new AtomicBoolean();
    final AtomicReference<ZooKeeper> client = new AtomicReference<ZooKeeper>();
    Thread getThread = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                client.set(zkClient.get());
            } catch (ZooKeeperConnectionException e) {
                throw new RuntimeException(e);
            } catch (InterruptedException e) {
                interrupted.set(true);
                throw new RuntimeException(e);
            } finally {
                blockingGetComplete.countDown();
            }
        }
    });
    getThread.start();
    while (zkClient.getZooKeeperClientForTests() == null) {
        Thread.sleep(100);
    }
    getThread.interrupt();
    blockingGetComplete.await();
    assertNull("The zk connection should have been closed", zkClient.getZooKeeperClientForTests());
    assertTrue("The waiter thread should have been interrupted", interrupted.get());
    assertTrue(zkClient.isClosed());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ZooKeeper(org.apache.zookeeper.ZooKeeper) AtomicReference(java.util.concurrent.atomic.AtomicReference) ZooKeeperConnectionException(com.twitter.common.zookeeper.ZooKeeperClient.ZooKeeperConnectionException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test) BaseZooKeeperTest(com.twitter.common.zookeeper.testing.BaseZooKeeperTest)

Example 4 with ZooKeeperConnectionException

use of com.twitter.common.zookeeper.ZooKeeperClient.ZooKeeperConnectionException in project commons by twitter.

the class ZooKeeperMap method init.

/**
   * Initialize zookeeper tracking for this {@link Map}.  Once this call returns, this object
   * will be tracking data in zookeeper.
   *
   * @throws InterruptedException if the underlying zookeeper server transaction is interrupted
   * @throws KeeperException if the server signals an error
   * @throws ZooKeeperConnectionException if there was a problem connecting to the zookeeper
   *     cluster
   */
@VisibleForTesting
void init() throws InterruptedException, KeeperException, ZooKeeperConnectionException {
    Watcher watcher = zkClient.registerExpirationHandler(new Command() {

        @Override
        public void execute() {
            /*
         * First rewatch all of our locally cached children.  Some of them may not exist anymore,
         * which will lead to caught KeeperException.NoNode whereafter we'll remove that child
         * from the cached map.
         *
         * Next, we'll establish our top level child watch and add any new nodes that might exist.
         */
            try {
                synchronized (safeToRewatchLock) {
                    if (safeToRewatch) {
                        rewatchDataNodes();
                        tryWatchChildren();
                    }
                }
            } catch (InterruptedException e) {
                LOG.log(Level.WARNING, "Interrupted while trying to re-establish watch.", e);
                Thread.currentThread().interrupt();
            }
        }
    });
    try {
        // before we update safeToRewatch.
        synchronized (safeToRewatchLock) {
            watchChildren();
            safeToRewatch = true;
        }
    } catch (InterruptedException e) {
        zkClient.unregister(watcher);
        throw e;
    } catch (KeeperException e) {
        zkClient.unregister(watcher);
        throw e;
    } catch (ZooKeeperConnectionException e) {
        zkClient.unregister(watcher);
        throw e;
    }
}
Also used : Command(com.twitter.common.base.Command) Watcher(org.apache.zookeeper.Watcher) ZooKeeperConnectionException(com.twitter.common.zookeeper.ZooKeeperClient.ZooKeeperConnectionException) KeeperException(org.apache.zookeeper.KeeperException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

ZooKeeperConnectionException (com.twitter.common.zookeeper.ZooKeeperClient.ZooKeeperConnectionException)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 Command (com.twitter.common.base.Command)2 BaseZooKeeperTest (com.twitter.common.zookeeper.testing.BaseZooKeeperTest)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 KeeperException (org.apache.zookeeper.KeeperException)2 Watcher (org.apache.zookeeper.Watcher)2 ZooKeeper (org.apache.zookeeper.ZooKeeper)2 Test (org.junit.Test)2 TimeoutException (java.util.concurrent.TimeoutException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 ConnectionLossException (org.apache.zookeeper.KeeperException.ConnectionLossException)1