Java Code Examples for org.apache.zookeeper.Watcher.Event.KeeperState#SyncConnected

The following examples show how to use org.apache.zookeeper.Watcher.Event.KeeperState#SyncConnected . 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: ZKConnection.java    From kafka-topology-builder with MIT License 6 votes vote down vote up
public ZooKeeper connect(String host) throws IOException, InterruptedException {
  zoo =
      new ZooKeeper(
          host,
          2000,
          new Watcher() {
            public void process(WatchedEvent we) {
              if (we.getState() == KeeperState.SyncConnected) {
                connectionLatch.countDown();
              }
            }
          });

  connectionLatch.await();
  return zoo;
}
 
Example 2
Source File: RpcInvokeHandler.java    From Distributed-KV with Apache License 2.0 6 votes vote down vote up
@Override
public void process(WatchedEvent event) {
	//如果ZooKeeper连接处于正常连接状态
	if (event.getState() == KeeperState.SyncConnected) {
		//对于事件类型
		switch (event.getType()) {
		// 服务器节点更新
		case NodeChildrenChanged:
			try {
				//如果服务节点更新,则主动拉取服务列表
				getServerList();
			} catch (Exception e) {
				e.printStackTrace();
			}
			break;
		// 服务被删除了,则将整个监视器关闭,使得其失活
		case NodeDeleted:
			close();
			break;
		default:
			break;
		}
	}

}
 
Example 3
Source File: ZKTestEnv.java    From herddb with Apache License 2.0 6 votes vote down vote up
public ZKTestEnv(Path path) throws Exception {
    zkServer = new TestingServer(1282, path.toFile(), true);
    // waiting for ZK to be reachable
    CountDownLatch latch = new CountDownLatch(1);
    ZooKeeper zk = new ZooKeeper(zkServer.getConnectString(),
            herddb.server.ServerConfiguration.PROPERTY_ZOOKEEPER_SESSIONTIMEOUT_DEFAULT, (WatchedEvent event) -> {
                LOG.log(Level.INFO, "ZK EVENT {0}", event);
                if (event.getState() == KeeperState.SyncConnected) {
                    latch.countDown();
                }
            });
    try {
        if (!latch.await(herddb.server.ServerConfiguration.PROPERTY_ZOOKEEPER_SESSIONTIMEOUT_DEFAULT, TimeUnit.MILLISECONDS)) {
            LOG.log(Level.INFO, "ZK client did not connect withing {0} seconds, maybe the server did not start up",
                    herddb.server.ServerConfiguration.PROPERTY_ZOOKEEPER_SESSIONTIMEOUT_DEFAULT);
        }
    } finally {
        zk.close(1000);
    }
    this.path = path;
}
 
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: ZKLeaderSelector.java    From zkclient with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isLeader(){
    if (client.getCurrentState() == KeeperState.SyncConnected ){
        if(lock.getParticipantNodes().size()>0){
            return id.equals(client.getData(leaderPath+"/"+lock.getParticipantNodes().get(0)));
        }
    }
    
    return false;
}
 
Example 6
Source File: ZKManager.java    From uncode-schedule with Apache License 2.0 5 votes vote down vote up
private void sessionEvent(CountDownLatch connectionLatch, WatchedEvent event) {
  if (event.getState() == KeeperState.SyncConnected) {
    LOG.info("收到ZK连接成功事件!");
    connectionLatch.countDown();
  } else if (event.getState() == KeeperState.Expired) {
    LOG.error("会话超时,等待重新建立ZK连接...");
    try {
      reConnection();
    } catch (Exception e) {
      LOG.error(e.getMessage(), e);
    }
  } // Disconnected:Zookeeper会自动处理Disconnected状态重连
}
 
Example 7
Source File: ClientBaseWithFixes.java    From big-c with Apache License 2.0 5 votes vote down vote up
@Override
synchronized public void process(WatchedEvent event) {
    if (event.getState() == KeeperState.SyncConnected ||
        event.getState() == KeeperState.ConnectedReadOnly) {
        connected = true;
        notifyAll();
        clientConnected.countDown();
    } else {
        connected = false;
        notifyAll();
    }
}
 
Example 8
Source File: ZKManager.java    From uncode-schedule with GNU General Public License v2.0 5 votes vote down vote up
private void sessionEvent(CountDownLatch connectionLatch, WatchedEvent event) {
    if (event.getState() == KeeperState.SyncConnected) {
        log.info("收到ZK连接成功事件!");
        connectionLatch.countDown();
    } else if (event.getState() == KeeperState.Expired) {
        log.error("会话超时,等待重新建立ZK连接...");
        try {
            reConnection();
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
    } // Disconnected:Zookeeper会自动处理Disconnected状态重连
}
 
Example 9
Source File: LeaderElection.java    From hbase-indexer with Apache License 2.0 5 votes vote down vote up
@Override
public void process(WatchedEvent event) {
    if (stopped) {
        return;
    }

    if (event.getType() == EventType.None &&
            (event.getState().equals(Event.KeeperState.Disconnected) ||
            event.getState().equals(Event.KeeperState.Expired))) {

        if (elected) {
            elected = false;
            log.info("No longer leader for the position of " + position);
            leaderProvisioner.setRequiredState(LeaderState.I_AM_NOT_LEADER);
        }

        // Note that if we get a disconnected event here, our watcher is not unregistered, thus we will
        // get a connected event on this watcher once we are reconnected.

        // Since we are not owner of the ZooKeeper handle, we assume Expired states are handled
        // elsewhere in the application.

    } else if (event.getType() == EventType.None && event.getState() == KeeperState.SyncConnected) {
        // Upon reconnect, since our session was not expired, our ephemeral node will still
        // exist (it might even be the leader), therefore have a look at the leaders.
        watchLeaders();
    }
}
 
Example 10
Source File: ClientBaseWithFixes.java    From hadoop with Apache License 2.0 5 votes vote down vote up
@Override
synchronized public void process(WatchedEvent event) {
    if (event.getState() == KeeperState.SyncConnected ||
        event.getState() == KeeperState.ConnectedReadOnly) {
        connected = true;
        notifyAll();
        clientConnected.countDown();
    } else {
        connected = false;
        notifyAll();
    }
}
 
Example 11
Source File: CreateGroup.java    From disconf with Apache License 2.0 5 votes vote down vote up
@Override
public void process(WatchedEvent event) { // Watcher interface

    if (event.getState() == KeeperState.SyncConnected) {
        connectedSignal.countDown();
    }
}
 
Example 12
Source File: CreateGroup.java    From disconf with Apache License 2.0 5 votes vote down vote up
@Override
public void process(WatchedEvent event) { // Watcher interface

    if (event.getState() == KeeperState.SyncConnected) {
        connectedSignal.countDown();
    }
}
 
Example 13
Source File: ZkTestHelper.java    From helix with Apache License 2.0 5 votes vote down vote up
/**
 * Simulate a zk state change by calling {@link ZkClient#process(WatchedEvent)} directly
 */
public static void simulateZkStateReconnected(RealmAwareZkClient client) {
  ZkClient zkClient = (ZkClient) client;
  WatchedEvent event = new WatchedEvent(EventType.None, KeeperState.Disconnected, null);
  zkClient.process(event);
  event = new WatchedEvent(EventType.None, KeeperState.SyncConnected, null);
  zkClient.process(event);
}
 
Example 14
Source File: ConnectionWatcher.java    From disconf with Apache License 2.0 5 votes vote down vote up
/**
 * 当连接成功时调用的
 */
@Override
public void process(WatchedEvent event) {
    if (event.getState() == KeeperState.SyncConnected) {

        LOGGER.info("zk SyncConnected");
        connectedSignal.countDown();

    } else if (event.getState().equals(KeeperState.Disconnected)) {

        // 这时收到断开连接的消息,这里其实无能为力,因为这时已经和ZK断开连接了,只能等ZK再次开启了
        LOGGER.warn("zk Disconnected");

    } else if (event.getState().equals(KeeperState.Expired)) {

        if (!debug) {

            // 这时收到这个信息,表示,ZK已经重新连接上了,但是会话丢失了,这时需要重新建立会话。
            LOGGER.error("zk Expired");

            // just reconnect forever
            reconnect();
        } else {
            LOGGER.info("zk Expired");
        }

    } else if (event.getState().equals(KeeperState.AuthFailed)) {

        LOGGER.error("zk AuthFailed");
    }
}
 
Example 15
Source File: ZkConnect.java    From bidder with Apache License 2.0 5 votes vote down vote up
public ZooKeeper connect(String host) throws Exception {
	zk = new ZooKeeper(host, 2101, new Watcher() {
		public void process(WatchedEvent event) {
			if (event.getState() == KeeperState.SyncConnected) {
				connSignal.countDown();
			}
		}
	});
	connSignal.await();
	return zk;
}
 
Example 16
Source File: ZKConnection.java    From tutorials with MIT License 5 votes vote down vote up
public ZooKeeper connect(String host) throws IOException, InterruptedException {
    zoo = new ZooKeeper(host, 2000, new Watcher() {
        public void process(WatchedEvent we) {
            if (we.getState() == KeeperState.SyncConnected) {
                connectionLatch.countDown();
            }
        }
    });
    connectionLatch.await();
    return zoo;
}
 
Example 17
Source File: SwiftWatcher.java    From recsys-offline with Apache License 2.0 5 votes vote down vote up
@Override
public void process(WatchedEvent event) {
	if (event.getState() == KeeperState.SyncConnected) {
		conSignal.countDown();
	}
	synchronized (this) {
		if (event.getPath() != null
				&& event.getPath().equals(Constants.zk_start_flag)
				&& event.getType() == Event.EventType.NodeCreated) {
			try {
				if (zk.exists(Constants.zk_znode, false) != null) {
					List<String> list = zk.getChildren(Constants.zk_znode,	true);
					if (list != null && list.size() > 0) {
						for (String str : list) {
							zk.delete(Constants.zk_znode + "/" + str, -1);
						}
					}
					zk.delete(Constants.zk_znode, -1);
					logger.info("delete zookeeper dir successful. It begin to start hadoop task...");
					Recsys.runAllTask();
					
				} 
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
 
Example 18
Source File: ZkclientZookeeperClient.java    From JobX with Apache License 2.0 4 votes vote down vote up
public boolean isConnected() {
    return state == KeeperState.SyncConnected;
}
 
Example 19
Source File: ZkclientZookeeperClient.java    From dubbox-hystrix with Apache License 2.0 4 votes vote down vote up
public boolean isConnected() {
	return state == KeeperState.SyncConnected;
}
 
Example 20
Source File: ZkClientZkClient.java    From light-task-scheduler with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isConnected() {
    return state == KeeperState.SyncConnected;
}