Search in sources :

Example 46 with WatchedEvent

use of org.apache.zookeeper.WatchedEvent in project bboxdb by jnidzwetzki.

the class ZookeeperClient method init.

/**
 * Connect to zookeeper
 */
@Override
public void init() {
    try {
        serviceState.reset();
        serviceState.dipatchToStarting();
        usage = new Phaser(1);
        final CountDownLatch connectLatch = new CountDownLatch(1);
        zookeeper = new ZooKeeper(connectionString, ZOOKEEPER_SESSION_TIMEOUT, new Watcher() {

            @Override
            public void process(final WatchedEvent event) {
                if (event.getState() == Watcher.Event.KeeperState.SyncConnected) {
                    connectLatch.countDown();
                }
            }
        });
        final boolean waitResult = connectLatch.await(ZOOKEEPER_CONNECT_TIMEOUT_IN_SEC, TimeUnit.SECONDS);
        if (waitResult == false) {
            throw new ZookeeperException("Unable to connect in " + ZOOKEEPER_CONNECT_TIMEOUT_IN_SEC + " seconds. Connect string is: " + connectionString);
        }
        createDirectoryStructureIfNeeded();
        serviceState.dispatchToRunning();
    } catch (Exception e) {
        logger.warn("Got exception while connecting to zookeeper", e);
        closeZookeeperConnectionNE();
        serviceState.dispatchToFailed(e);
    }
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) ZooKeeper(org.apache.zookeeper.ZooKeeper) Watcher(org.apache.zookeeper.Watcher) Phaser(java.util.concurrent.Phaser) CountDownLatch(java.util.concurrent.CountDownLatch) KeeperException(org.apache.zookeeper.KeeperException)

Example 47 with WatchedEvent

use of org.apache.zookeeper.WatchedEvent in project xian by happyyangyuan.

the class BasicTests method testExpiredSession.

@Test
public void testExpiredSession() throws Exception {
    // see http://wiki.apache.org/hadoop/ZooKeeper/FAQ#A4
    final Timing timing = new Timing();
    final CountDownLatch latch = new CountDownLatch(1);
    Watcher watcher = new Watcher() {

        @Override
        public void process(WatchedEvent event) {
            if (event.getState() == Event.KeeperState.Expired) {
                latch.countDown();
            }
        }
    };
    final CuratorZookeeperClient client = new CuratorZookeeperClient(server.getConnectString(), timing.session(), timing.connection(), watcher, new RetryOneTime(2));
    client.start();
    try {
        final AtomicBoolean firstTime = new AtomicBoolean(true);
        RetryLoop.callWithRetry(client, new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                if (firstTime.compareAndSet(true, false)) {
                    try {
                        client.getZooKeeper().create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
                    } catch (KeeperException.NodeExistsException ignore) {
                    // ignore
                    }
                    KillSession.kill(client.getZooKeeper(), server.getConnectString());
                    Assert.assertTrue(timing.awaitLatch(latch));
                }
                ZooKeeper zooKeeper = client.getZooKeeper();
                client.blockUntilConnectedOrTimedOut();
                Assert.assertNotNull(zooKeeper.exists("/foo", false));
                return null;
            }
        });
    } finally {
        client.close();
    }
}
Also used : RetryOneTime(org.apache.curator.retry.RetryOneTime) Watcher(org.apache.zookeeper.Watcher) CountDownLatch(java.util.concurrent.CountDownLatch) KeeperException(org.apache.zookeeper.KeeperException) WatchedEvent(org.apache.zookeeper.WatchedEvent) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ZooKeeper(org.apache.zookeeper.ZooKeeper) Timing(org.apache.curator.test.Timing) Test(org.testng.annotations.Test)

Example 48 with WatchedEvent

use of org.apache.zookeeper.WatchedEvent in project xian by happyyangyuan.

the class CuratorZookeeperClient method internalBlockUntilConnectedOrTimedOut.

void internalBlockUntilConnectedOrTimedOut() throws InterruptedException {
    long waitTimeMs = connectionTimeoutMs;
    while (!state.isConnected() && (waitTimeMs > 0)) {
        final CountDownLatch latch = new CountDownLatch(1);
        Watcher tempWatcher = new Watcher() {

            @Override
            public void process(WatchedEvent event) {
                latch.countDown();
            }
        };
        state.addParentWatcher(tempWatcher);
        long startTimeMs = System.currentTimeMillis();
        try {
            latch.await(1, TimeUnit.SECONDS);
        } finally {
            state.removeParentWatcher(tempWatcher);
        }
        long elapsed = Math.max(1, System.currentTimeMillis() - startTimeMs);
        waitTimeMs -= elapsed;
    }
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) Watcher(org.apache.zookeeper.Watcher) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 49 with WatchedEvent

use of org.apache.zookeeper.WatchedEvent in project accumulo by apache.

the class ZooLock method lockAsync.

public synchronized void lockAsync(final AsyncLockWatcher lw, byte[] data) {
    if (lockWatcher != null || lock != null || asyncLock != null) {
        throw new IllegalStateException();
    }
    lockWasAcquired = false;
    try {
        final String asyncLockPath = zooKeeper.putEphemeralSequential(path + "/" + LOCK_PREFIX, data);
        log.trace("Ephemeral node {} created", asyncLockPath);
        Stat stat = zooKeeper.getStatus(asyncLockPath, new Watcher() {

            private void failedToAcquireLock() {
                lw.failedToAcquireLock(new Exception("Lock deleted before acquired"));
                asyncLock = null;
            }

            @Override
            public void process(WatchedEvent event) {
                synchronized (ZooLock.this) {
                    if (lock != null && event.getType() == EventType.NodeDeleted && event.getPath().equals(path + "/" + lock)) {
                        lostLock(LockLossReason.LOCK_DELETED);
                    } else if (asyncLock != null && event.getType() == EventType.NodeDeleted && event.getPath().equals(path + "/" + asyncLock)) {
                        failedToAcquireLock();
                    } else if (event.getState() != KeeperState.Disconnected && event.getState() != KeeperState.Expired && (lock != null || asyncLock != null)) {
                        log.debug("Unexpected event watching lock node {} {}", event, asyncLockPath);
                        try {
                            Stat stat2 = zooKeeper.getStatus(asyncLockPath, this);
                            if (stat2 == null) {
                                if (lock != null)
                                    lostLock(LockLossReason.LOCK_DELETED);
                                else if (asyncLock != null)
                                    failedToAcquireLock();
                            }
                        } catch (Throwable e) {
                            lockWatcher.unableToMonitorLockNode(e);
                            log.error("Failed to stat lock node " + asyncLockPath, e);
                        }
                    }
                }
            }
        });
        if (stat == null) {
            lw.failedToAcquireLock(new Exception("Lock does not exist after create"));
            return;
        }
        asyncLock = asyncLockPath.substring(path.length() + 1);
        lockAsync(asyncLock, lw);
    } catch (KeeperException | InterruptedException e) {
        lw.failedToAcquireLock(e);
    }
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) ZcStat(org.apache.accumulo.fate.zookeeper.ZooCache.ZcStat) Stat(org.apache.zookeeper.data.Stat) Watcher(org.apache.zookeeper.Watcher) KeeperException(org.apache.zookeeper.KeeperException) KeeperException(org.apache.zookeeper.KeeperException)

Example 50 with WatchedEvent

use of org.apache.zookeeper.WatchedEvent in project accumulo by apache.

the class ZooLock method lockAsync.

private synchronized void lockAsync(final String myLock, final AsyncLockWatcher lw) throws KeeperException, InterruptedException {
    if (asyncLock == null) {
        throw new IllegalStateException("Called lockAsync() when asyncLock == null");
    }
    List<String> children = zooKeeper.getChildren(path);
    if (!children.contains(myLock)) {
        throw new RuntimeException("Lock attempt ephemeral node no longer exist " + myLock);
    }
    Collections.sort(children);
    if (log.isTraceEnabled()) {
        log.trace("Candidate lock nodes");
        for (String child : children) {
            log.trace("- {}", child);
        }
    }
    if (children.get(0).equals(myLock)) {
        log.trace("First candidate is my lock, acquiring");
        if (!watchingParent) {
            throw new IllegalStateException("Can not acquire lock, no longer watching parent : " + path);
        }
        this.lockWatcher = lw;
        this.lock = myLock;
        asyncLock = null;
        lockWasAcquired = true;
        lw.acquiredLock();
        return;
    }
    String prev = null;
    for (String child : children) {
        if (child.equals(myLock)) {
            break;
        }
        prev = child;
    }
    final String lockToWatch = path + "/" + prev;
    log.trace("Establishing watch on {}", lockToWatch);
    Stat stat = zooKeeper.getStatus(lockToWatch, new Watcher() {

        @Override
        public void process(WatchedEvent event) {
            if (log.isTraceEnabled()) {
                log.trace("Processing event:");
                log.trace("- type  {}", event.getType());
                log.trace("- path  {}", event.getPath());
                log.trace("- state {}", event.getState());
            }
            boolean renew = true;
            if (event.getType() == EventType.NodeDeleted && event.getPath().equals(lockToWatch)) {
                log.trace("Detected deletion of {}, attempting to acquire lock", lockToWatch);
                synchronized (ZooLock.this) {
                    try {
                        if (asyncLock != null) {
                            lockAsync(myLock, lw);
                        } else if (log.isTraceEnabled()) {
                            log.trace("While waiting for another lock {} {} was deleted", lockToWatch, myLock);
                        }
                    } catch (Exception e) {
                        if (lock == null) {
                            // have not acquired lock yet
                            lw.failedToAcquireLock(e);
                        }
                    }
                }
                renew = false;
            }
            if (event.getState() == KeeperState.Expired || event.getState() == KeeperState.Disconnected) {
                synchronized (ZooLock.this) {
                    if (lock == null) {
                        lw.failedToAcquireLock(new Exception("Zookeeper Session expired / disconnected"));
                    }
                }
                renew = false;
            }
            if (renew) {
                log.trace("Renewing watch on {}", lockToWatch);
                try {
                    Stat restat = zooKeeper.getStatus(lockToWatch, this);
                    if (restat == null) {
                        lockAsync(myLock, lw);
                    }
                } catch (KeeperException | InterruptedException e) {
                    lw.failedToAcquireLock(new Exception("Failed to renew watch on other master node"));
                }
            }
        }
    });
    if (stat == null)
        lockAsync(myLock, lw);
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) ZcStat(org.apache.accumulo.fate.zookeeper.ZooCache.ZcStat) Stat(org.apache.zookeeper.data.Stat) Watcher(org.apache.zookeeper.Watcher) KeeperException(org.apache.zookeeper.KeeperException)

Aggregations

WatchedEvent (org.apache.zookeeper.WatchedEvent)194 Watcher (org.apache.zookeeper.Watcher)123 ZooKeeper (org.apache.zookeeper.ZooKeeper)67 CountDownLatch (java.util.concurrent.CountDownLatch)64 KeeperException (org.apache.zookeeper.KeeperException)56 Test (org.junit.Test)38 Stat (org.apache.zookeeper.data.Stat)35 IOException (java.io.IOException)31 Test (org.testng.annotations.Test)27 Test (org.junit.jupiter.api.Test)18 CuratorFramework (org.apache.curator.framework.CuratorFramework)14 AsyncCallback (org.apache.zookeeper.AsyncCallback)14 List (java.util.List)10 KeeperState (org.apache.zookeeper.Watcher.Event.KeeperState)10 Set (java.util.Set)7 TimeoutException (java.util.concurrent.TimeoutException)7 HashSet (java.util.HashSet)6 CompletableFuture (java.util.concurrent.CompletableFuture)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 RetryOneTime (org.apache.curator.retry.RetryOneTime)6