Example usage for org.apache.zookeeper Watcher Watcher

List of usage examples for org.apache.zookeeper Watcher Watcher

Introduction

In this page you can find the example usage for org.apache.zookeeper Watcher Watcher.

Prototype

Watcher

Source Link

Usage

From source file:com.twitter.distributedlog.ZooKeeperClientUtils.java

License:Apache License

/**
 * Expire given zookeeper client's session.
 *
 * @param zkc/*  w  ww  .  j  a  va  2s  . com*/
 *          zookeeper client
 * @param zkServers
 *          zookeeper servers
 * @param timeout
 *          timeout
 * @throws Exception
 */
public static void expireSession(ZooKeeperClient zkc, String zkServers, int timeout) throws Exception {
    final CountDownLatch expireLatch = new CountDownLatch(1);
    final CountDownLatch latch = new CountDownLatch(1);
    ZooKeeper oldZk = zkc.get();
    oldZk.exists("/", new Watcher() {
        @Override
        public void process(WatchedEvent event) {
            logger.debug("Receive event : {}", event);
            if (event.getType() == Event.EventType.None && event.getState() == Event.KeeperState.Expired) {
                expireLatch.countDown();
            }
        }
    });
    ZooKeeper newZk = new ZooKeeper(zkServers, timeout, new Watcher() {
        @Override
        public void process(WatchedEvent event) {
            if (Event.EventType.None == event.getType()
                    && Event.KeeperState.SyncConnected == event.getState()) {
                latch.countDown();
            }
        }
    }, oldZk.getSessionId(), oldZk.getSessionPasswd());
    if (!latch.await(timeout, TimeUnit.MILLISECONDS)) {
        throw KeeperException.create(KeeperException.Code.CONNECTIONLOSS);
    }
    newZk.close();

    boolean done = false;
    Stopwatch expireWait = Stopwatch.createStarted();
    while (!done && expireWait.elapsed(TimeUnit.MILLISECONDS) < timeout * 2) {
        try {
            zkc.get().exists("/", false);
            done = true;
        } catch (KeeperException ke) {
            done = (ke.code() == KeeperException.Code.SESSIONEXPIRED);
        }
    }

    assertTrue("Client should receive session expired event.",
            expireLatch.await(timeout, TimeUnit.MILLISECONDS));
}

From source file:com.twitter.finagle.common.zookeeper.ZooKeeperClient.java

License:Apache License

/**
 * Returns the current active ZK connection or establishes a new one if none has yet been
 * established or a previous connection was disconnected or had its session time out.  This
 * method will attempt to re-use sessions when possible.
 *
 * @param connectionTimeout the maximum amount of time to wait for the connection to the ZK
 *     cluster to be established; 0 to wait forever
 * @return a connected ZooKeeper client/*  www.  ja  v a 2 s.co  m*/
 * @throws ZooKeeperConnectionException if there was a problem connecting to the ZK cluster
 * @throws InterruptedException if interrupted while waiting for a connection to be established
 * @throws TimeoutException if a connection could not be established within the configured
 *     session timeout
 */
public synchronized ZooKeeper get(Duration connectionTimeout)
        throws ZooKeeperConnectionException, InterruptedException, TimeoutException {

    if (zooKeeper == null) {
        final CountDownLatch connected = new CountDownLatch(1);
        Watcher watcher = new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                switch (event.getType()) {
                // Guard the None type since this watch may be used as the default watch on calls by
                // the client outside our control.
                case None:
                    switch (event.getState()) {
                    case Expired:
                        LOG.info("Zookeeper session expired. Event: " + event);
                        close();
                        break;
                    case SyncConnected:
                        connected.countDown();
                        break;
                    default:
                        break; // nop
                    }
                    break;
                default:
                    break; // nop
                }

                eventQueue.offer(event);
            }
        };

        try {
            zooKeeper = (sessionState != null)
                    ? new ZooKeeper(connectString, sessionTimeoutMs, watcher, sessionState.sessionId,
                            sessionState.sessionPasswd)
                    : new ZooKeeper(connectString, sessionTimeoutMs, watcher);
        } catch (IOException e) {
            throw new ZooKeeperConnectionException("Problem connecting to servers: " + zooKeeperServers, e);
        }

        if (connectionTimeout.inMillis() > 0) {
            if (!connected.await(connectionTimeout.inMillis(), TimeUnit.MILLISECONDS)) {
                close();
                throw new TimeoutException("Timed out waiting for a ZK connection after " + connectionTimeout);
            }
        } else {
            try {
                connected.await();
            } catch (InterruptedException ex) {
                LOG.info("Interrupted while waiting to connect to zooKeeper");
                close();
                throw ex;
            }
        }
        credentials.authenticate(zooKeeper);

        sessionState = new SessionState(zooKeeper.getSessionId(), zooKeeper.getSessionPasswd());
    }
    return zooKeeper;
}

From source file:com.twitter.finagle.common.zookeeper.ZooKeeperClient.java

License:Apache License

/**
 * Clients that need to re-establish state after session expiration can register an
 * {@code onExpired} command to execute.
 *
 * @param onExpired the {@code Runnable} to register
 * @return the new {@link Watcher} which can later be passed to {@link #unregister} for
 *     removal.//from ww w . j a  v  a 2s  . c  om
 */
public Watcher registerExpirationHandler(final Runnable onExpired) {
    Watcher watcher = new Watcher() {
        @Override
        public void process(WatchedEvent event) {
            if (event.getType() == EventType.None && event.getState() == KeeperState.Expired) {
                onExpired.run();
            }
        }
    };
    register(watcher);
    return watcher;
}

From source file:com.twitter.heron.statemgr.zookeeper.ZkWatcherCallback.java

License:Open Source License

public static Watcher makeZkWatcher(final WatchCallback watcher) {
    return watcher == null ? null : new Watcher() {
        @Override/*from   ww  w .j a  v  a2s  . c o m*/
        public void process(WatchedEvent watchedEvent) {
            WatchCallback.WatchEventType watchEventType;
            switch (watchedEvent.getType()) {
            case None:
                watchEventType = WatchCallback.WatchEventType.None;
                break;
            case NodeCreated:
                watchEventType = WatchCallback.WatchEventType.NodeCreated;
                break;
            case NodeDeleted:
                watchEventType = WatchCallback.WatchEventType.NodeDeleted;
                break;
            case NodeDataChanged:
                watchEventType = WatchCallback.WatchEventType.NodeDataChanged;
                break;
            case NodeChildrenChanged:
                watchEventType = WatchCallback.WatchEventType.NodeChildrenChanged;
                break;

            default:
                throw new RuntimeException("Invalid integer value for conversion to EventType");
            }
            watcher.processWatch(watchedEvent.getPath(), watchEventType);
        }
    };
}

From source file:com.wms.studio.cache.lock.SyncLockCacheManagerImpl.java

License:Apache License

public void initZooKeeper() {
    try {/*from   ww w  . ja  va  2s . co m*/
        zooKeeper = new ZooKeeper(zookeeperAddress, timeout, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
            }
        });
    } catch (IOException e) {
    }
}

From source file:com.yahoo.pasc.paxos.server.tcp.TcpServer.java

License:Open Source License

public TcpServer(PascRuntime<PaxosState> runtime, StateMachine sm, ControlHandler controlHandler, String zk,
        String servers[], int port, int threads, final int id, boolean twoStages)
        throws IOException, KeeperException {
    this.bossExecutor = Executors.newCachedThreadPool();
    this.workerExecutor = Executors.newCachedThreadPool();
    this.executionHandler = new ExecutionHandler(new MemoryAwareThreadPoolExecutor(1, 1024 * 1024,
            1024 * 1024 * 1024, 10, TimeUnit.SECONDS, new ObjectSizeEstimator() {
                @Override/* w ww  .j  a  v a2 s  .  co  m*/
                public int estimateSize(Object o) {
                    return 1024;
                }
            }, new ThreadFactory() {
                private int count = 0;

                @Override
                public Thread newThread(Runnable r) {
                    return new Thread(r, id + "-" + count++);
                }
            }));
    this.channelHandler = new ServerHandler(runtime, sm, controlHandler, this);
    this.channelPipelineFactory = new PipelineFactory(channelHandler, executionHandler, twoStages,
            runtime.isProtected());
    final CountDownLatch latch = new CountDownLatch(1);
    ZooKeeper zookeeper = new ZooKeeper(zk, 2000, new Watcher() {
        @Override
        public void process(WatchedEvent event) {
            latch.countDown();
        }
    });
    try {
        latch.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    this.leaderElection = new LeaderElection(zookeeper, id, this.channelHandler);
    this.barrier = new Barrier(zookeeper, "/paxos_srv_barrier", "" + id, servers.length);
    this.servers = servers;
    this.port = port;
    this.threads = threads;
    this.id = id;
}

From source file:com.yahoo.pulsar.broker.loadbalance.LeaderElectionService.java

License:Apache License

/**
 * We try to get the data in the ELECTION_ROOT node. If the node is present (i.e. leader is present), we store it in
 * the currentLeader and keep a watch on the election node. If we lose the leader, then watch gets triggered and we
 * do the election again. If the node does not exist while getting the data, we get NoNodeException. This means,
 * there is no leader and we create the node at ELECTION_ROOT and write the leader broker's service URL in the node.
 * Once the leader is known, we call the listener method so that leader can take further actions.
 *//*from ww  w  .  ja v  a  2  s . c om*/
private void elect() {
    try {
        byte[] data = zkClient.getData(ELECTION_ROOT, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                log.warn("Type of the event is [{}] and path is [{}]", event.getType(), event.getPath());
                switch (event.getType()) {
                case NodeDeleted:
                    log.warn("Election node {} is deleted, attempting re-election...", event.getPath());
                    if (event.getPath().equals(ELECTION_ROOT)) {
                        log.info("This should call elect again...");
                        executor.execute(new Runnable() {
                            @Override
                            public void run() {
                                // If the node is deleted, attempt the re-election
                                log.info("Broker [{}] is calling re-election from the thread",
                                        pulsar.getWebServiceAddress());
                                elect();
                            }
                        });
                    }
                    break;

                default:
                    log.warn("Got something wrong on watch: {}", event);
                    break;
                }
            }
        }, null);

        LeaderBroker leaderBroker = jsonMapper.readValue(data, LeaderBroker.class);
        currentLeader.set(leaderBroker);
        isLeader.set(false);
        leaderListener.brokerIsAFollowerNow();

        // If broker comes here it is a follower. Do nothing, wait for the watch to trigger
        log.info("Broker [{}] is the follower now. Waiting for the watch to trigger...",
                pulsar.getWebServiceAddress());

    } catch (NoNodeException nne) {
        // There's no leader yet... try to become the leader
        try {
            // Create the root node and add current broker's URL as its contents
            LeaderBroker leaderBroker = new LeaderBroker(pulsar.getWebServiceAddress());
            ZkUtils.createFullPathOptimistic(pulsar.getLocalZkCache().getZooKeeper(), ELECTION_ROOT,
                    jsonMapper.writeValueAsBytes(leaderBroker), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);

            // Update the current leader and set the flag to true
            currentLeader.set(new LeaderBroker(leaderBroker.getServiceUrl()));
            isLeader.set(true);

            // Notify the listener that this broker is now the leader so that it can collect usage and start load
            // manager.
            log.info("Broker [{}] is the leader now, notifying the listener...", pulsar.getWebServiceAddress());
            leaderListener.brokerIsTheLeaderNow();
        } catch (NodeExistsException nee) {
            // Re-elect the new leader
            log.warn(
                    "Got exception [{}] while creating election node because it already exists. Attempting re-election...",
                    nee.getMessage());
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    elect();
                }
            });
        } catch (Exception e) {
            // Kill the broker because this broker's session with zookeeper might be stale. Killing the broker will
            // make sure that we get the fresh zookeeper session.
            log.error("Got exception [{}] while creating the election node", e.getMessage());
            pulsar.getShutdownService().shutdown(-1);
        }

    } catch (Exception e) {
        // Kill the broker
        log.error("Could not get the content of [{}], got exception [{}]. Shutting down the broker...",
                ELECTION_ROOT, e);
        pulsar.getShutdownService().shutdown(-1);
    }
}

From source file:com.zookeeper.web.inspector.manager.ZooInspectorManagerImpl.java

License:Apache License

@Override
public boolean connect(Properties connectionProps) {
    connected = false;//from w  w w. ja  v  a2  s  . c o  m
    try {
        if (this.zooKeeper == null) {
            String connectString = connectionProps.getProperty(CONNECT_STRING);
            String sessionTimeout = connectionProps.getProperty(SESSION_TIMEOUT);
            String encryptionManager = connectionProps.getProperty(DATA_ENCRYPTION_MANAGER);
            if (connectString == null || sessionTimeout == null) {
                throw new IllegalArgumentException("Both connect string and session timeout are required.");
            }
            if (encryptionManager == null) {
                this.encryptionManager = new BasicDataEncryptionManager();
            } else {
                Class<?> clazz = Class.forName(encryptionManager);

                if (Arrays.asList(clazz.getInterfaces()).contains(DataEncryptionManager.class)) {
                    this.encryptionManager = (DataEncryptionManager) Class.forName(encryptionManager)
                            .newInstance();
                } else {
                    throw new IllegalArgumentException(
                            "Data encryption manager must implement DataEncryptionManager interface");
                }
            }
            this.connectString = connectString;
            this.sessionTimeout = Integer.valueOf(sessionTimeout);

            //        long start = System.currentTimeMillis();
            //        System.out.println("[START] connecting...");
            this.zooKeeper = new ZooKeeperRetry(connectString, Integer.valueOf(sessionTimeout), new Watcher() {

                @Override
                public void process(WatchedEvent event) {
                    if (event.getState() == KeeperState.Expired) {
                        connected = false;
                    }
                }
            });
            ((ZooKeeperRetry) this.zooKeeper).setRetryLimit(10);
            //        System.out.println("[START] connected took: " + (System.currentTimeMillis() - start));

            connected = ((ZooKeeperRetry) this.zooKeeper).testConnection();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    // connected = false;

    // do initial cache refresh on all childs of "/"
    if (connected == true) {
        cache = new ZooInspectorManagerCache(this);
        try {
            cache.refresh(Arrays.asList("/"), 1);
        } catch (KeeperException e) {
            // TODO Auto-generated catch block
            disconnect();
            e.printStackTrace();
        }
    } else {
        disconnect();
    }

    return connected;
}

From source file:fr.ens.biologie.genomique.eoulsan.util.locker.ZooKeeperLocker.java

License:LGPL

@Override
public void lock() throws IOException {

    // Test if the connection is alive
    if (!this.response) {
        throw new IOException("Connection to Zookeeper is not alive");
    }/*from ww w .j  a  v a2 s  . c  o m*/

    try {

        if (this.zk.exists(this.lockBasePath, false) == null) {

            this.zk.create(this.lockBasePath, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        }

        // lockPath will be different than (lockBasePath + "/" + lockName) because
        // of the sequence number ZooKeeper appends

        this.lockPath = this.zk.create(this.lockBasePath + "/" + this.lockName, null, Ids.OPEN_ACL_UNSAFE,
                this.sequentialLockName ? CreateMode.EPHEMERAL_SEQUENTIAL : CreateMode.EPHEMERAL);
        final Object lock = new Object();

        synchronized (lock) {

            while (true) {

                List<String> nodes = this.zk.getChildren(this.lockBasePath, new Watcher() {

                    @Override
                    public void process(final WatchedEvent event) {

                        synchronized (lock) {
                            lock.notifyAll();
                        }
                    }
                });

                Collections.sort(nodes);

                if (this.lockPath.endsWith(nodes.get(0))) {
                    return;
                }

                lock.wait();
            }
        }
    } catch (KeeperException | InterruptedException e) {
        throw new IOException(e);
    }
}

From source file:herddb.client.ZookeeperClientSideMetadataProvider.java

License:Apache License

public ZookeeperClientSideMetadataProvider(String zkAddress, int zkSessionTimeout, String basePath) {
    this(basePath, () -> {
        try {/*from  w  w w  .  j a va  2s .c  o  m*/
            CountDownLatch waitForConnection = new CountDownLatch(1);
            ZooKeeper zk = new ZooKeeper(zkAddress, zkSessionTimeout, new Watcher() {
                @Override
                @SuppressFBWarnings(value = "SF_SWITCH_NO_DEFAULT")
                public void process(WatchedEvent event) {
                    LOG.log(Level.SEVERE, "zk client event " + event);
                    switch (event.getState()) {
                    case SyncConnected:
                    case ConnectedReadOnly:
                        waitForConnection.countDown();
                        break;
                    }
                }
            });
            boolean waitResult = waitForConnection.await(zkSessionTimeout * 2L, TimeUnit.SECONDS);
            return zk;
        } catch (Exception err) {
            LOG.log(Level.SEVERE, "zk client error " + err, err);
            return null;
        }
    });
    this.ownZooKeeper = true;
}