org.apache.zookeeper.ZooKeeper.getState()方法的使用及代码示例

x33g5p2x  于2022-02-05 转载在 其他  
字(6.7k)|赞(0)|评价(0)|浏览(240)

本文整理了Java中org.apache.zookeeper.ZooKeeper.getState()方法的一些代码示例,展示了ZooKeeper.getState()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ZooKeeper.getState()方法的具体详情如下:
包路径:org.apache.zookeeper.ZooKeeper
类名称:ZooKeeper
方法名:getState

ZooKeeper.getState介绍

暂无

代码示例

代码示例来源:origin: apache/zookeeper

protected String getPrompt() {       
  return "[zk: " + host + "("+zk.getState()+")" + " " + commandCount + "] ";
}

代码示例来源:origin: apache/hbase

public synchronized States getState() {
 return zk == null ? null : zk.getState();
}

代码示例来源:origin: org.apache.zookeeper/zookeeper

protected String getPrompt() {       
  return "[zk: " + host + "("+zk.getState()+")" + " " + commandCount + "] ";
}

代码示例来源:origin: apache/hbase

private ZooKeeper getZk() throws IOException {
 // may be closed when session expired
 if (zookeeper == null || !zookeeper.getState().isAlive()) {
  zookeeper = new ZooKeeper(connectString, sessionTimeoutMs, e -> {});
 }
 return zookeeper;
}

代码示例来源:origin: apache/zookeeper

synchronized void ensureConnected(){
  while(zk.getState()!=ZooKeeper.States.CONNECTED){
    try {
      wait();
    } catch (InterruptedException e) {
      return;
    }
  }
}

代码示例来源:origin: apache/zookeeper

public static void waitForOne(ZooKeeper zk, ZooKeeper.States state) throws InterruptedException {
  int iterations = ClientBase.CONNECTION_TIMEOUT / 500;
  while (zk.getState() != state) {
    if (iterations-- == 0) {
      throw new RuntimeException("Waiting too long " + zk.getState() + " != " + state);
    }
    Thread.sleep(500);
  }
}

代码示例来源:origin: apache/ignite

/** {@inheritDoc} */
@Override public void run() {
  try {
    zkClient.exists(paths.clusterDir, false);
  }
  catch (Throwable t) {
    if (zkClient.getState().isAlive())
      U.warn(log, "Failed to ping Zookeeper.", t);
    else
      scheduler.cancel();
  }
}

代码示例来源:origin: apache/zookeeper

private void waitForOne(ZooKeeper zk, ArrayList<States> states) throws InterruptedException {
  int iterations = ClientBase.CONNECTION_TIMEOUT / 500;
  while (!states.contains(zk.getState())) {
    if (iterations-- == 0) {
      LOG.info("state is {}", zk.getState());
      throw new RuntimeException("Waiting too long");
    }
    Thread.sleep(500);
  }
}

代码示例来源:origin: Netflix/curator

@Override
public ZooKeeper.States getZookeeperState()
{
  try
  {
    return curator.getZookeeperClient().getZooKeeper().getState();
  }
  catch ( Exception e )
  {
    throw new RuntimeException(e);
  }
}

代码示例来源:origin: ltsopensource/light-task-scheduler

private void checkConnect() {
  if (zk == null || !zk.getState().isConnected()) {
    lock.lock();
    try {
      conditionConnected.await(10000, TimeUnit.MILLISECONDS);
    } catch (InterruptedException e) {
      LOGGER.error(e);
    } finally {
      lock.unlock();
    }
    if (zk == null || !zk.getState().isConnected()) {
      throw new ZkException("zk not connected, please wait");
    }
  }
}

代码示例来源:origin: ltsopensource/light-task-scheduler

private void checkConnect() {
  if (zk == null || !zk.getState().isConnected()) {
    lock.lock();
    try {
      conditionConnected.await(10000, TimeUnit.MILLISECONDS);
    } catch (InterruptedException e) {
      LOGGER.error(e);
    } finally {
      lock.unlock();
    }
    if (zk == null || !zk.getState().isConnected()) {
      throw new ZkException("zk not connected, please wait");
    }
  }
}

代码示例来源:origin: apache/zookeeper

public static void logStates(ZooKeeper[] zks) {
  StringBuilder sbBuilder = new StringBuilder("Connection States: {");
  for (int i = 0; i < zks.length; i++) {
    sbBuilder.append(i + " : " + zks[i].getState() + ", ");
  }
  sbBuilder.append('}');
  LOG.error(sbBuilder.toString());
}

代码示例来源:origin: apache/zookeeper

protected void connectToZK(String newHost) throws InterruptedException, IOException {
  if (zk != null && zk.getState().isAlive()) {
    zk.close();
  }
  host = newHost;
  boolean readOnly = cl.getOption("readonly") != null;
  if (cl.getOption("secure") != null) {
    System.setProperty(ZKClientConfig.SECURE_CLIENT, "true");
    System.out.println("Secure connection is enabled");
  }
  zk = new ZooKeeperAdmin(host, Integer.parseInt(cl.getOption("timeout")), new MyWatcher(), readOnly);
}

代码示例来源:origin: org.apache.zookeeper/zookeeper

protected void connectToZK(String newHost) throws InterruptedException, IOException {
  if (zk != null && zk.getState().isAlive()) {
    zk.close();
  }
  host = newHost;
  boolean readOnly = cl.getOption("readonly") != null;
  zk = new ZooKeeper(host,
       Integer.parseInt(cl.getOption("timeout")),
       new MyWatcher(), readOnly);
}

代码示例来源:origin: apache/zookeeper

/**
 * String representation of this ZooKeeper client. Suitable for things
 * like logging.
 * 
 * Do NOT count on the format of this string, it may change without
 * warning.
 * 
 * @since 3.3.0
 */
@Override
public String toString() {
  States state = getState();
  return ("State:" + state.toString()
      + (state.isConnected() ?
          " Timeout:" + getSessionTimeout() + " " :
          " ")
      + cnxn);
}

代码示例来源:origin: spotify/helios

@Override
public ZooKeeper.States getState() throws KeeperException {
 assertClusterIdFlagTrue();
 try {
  return client.getZookeeperClient().getZooKeeper().getState();
 } catch (Exception e) {
  throwIfInstanceOf(e, KeeperException.class);
  throw new RuntimeException(e);
 }
}

代码示例来源:origin: apache/zookeeper

public static void waitForAll(ZooKeeper[] zks, ZooKeeper.States state) throws InterruptedException {
  int iterations = ClientBase.CONNECTION_TIMEOUT / 1000;
  boolean someoneNotConnected = true;
  while (someoneNotConnected) {
    if (iterations-- == 0) {
      logStates(zks);
      ClientBase.logAllStackTraces();
      throw new RuntimeException("Waiting too long");
    }
    someoneNotConnected = false;
    for (ZooKeeper zk : zks) {
      if (zk.getState() != state) {
        someoneNotConnected = true;
        break;
      }
    }
    Thread.sleep(1000);
  }
}

代码示例来源:origin: apache/zookeeper

@Test
  public void testTryWithResources() throws Exception {
    ZooKeeper zooKeeper;
    try (ZooKeeper zk = createClient()) {
      zooKeeper = zk;
      Assert.assertTrue(zooKeeper.getState().isAlive());
    }

    Assert.assertFalse(zooKeeper.getState().isAlive());
  }
}

代码示例来源:origin: apache/zookeeper

@Test
  public void testClientRetry() throws IOException, InterruptedException, TimeoutException{
    CountdownWatcher cdw1 = new CountdownWatcher();
    CountdownWatcher cdw2 = new CountdownWatcher();
    ZooKeeper zk = new ZooKeeper(hostPort, 10000, cdw1);
    try {
      cdw1.waitForConnected(CONNECTION_TIMEOUT);
      ZooKeeper zk2 = new ZooKeeper(hostPort, 10000, cdw2);
      try {
        States s1 = zk.getState();
        States s2 = zk2.getState();
        Assert.assertSame(s1,States.CONNECTED);
        Assert.assertSame(s2,States.CONNECTING);
        cdw1.reset();
        zk.close();
        cdw1.waitForDisconnected(CONNECTION_TIMEOUT);
        cdw2.waitForConnected(CONNECTION_TIMEOUT);
        Assert.assertSame(zk2.getState(),States.CONNECTED);
      } finally {
        zk2.close();
      }
    } finally {
      zk.close();
    }
  }
}

代码示例来源:origin: twitter/distributedlog

@Test(timeout = 60000)
public void testZooKeeperReconnection() throws Exception {
  int sessionTimeoutMs = 100;
  ZooKeeperClient zkc = clientBuilder(sessionTimeoutMs).zkAclId(null).build();
  ZooKeeper zk = zkc.get();
  long sessionId = zk.getSessionId();
  ZooKeeperClientUtils.expireSession(zkc, zkServers, 2 * sessionTimeoutMs);
  ZooKeeper newZk = zkc.get();
  while (!ZooKeeper.States.CONNECTED.equals(newZk.getState())) {
    TimeUnit.MILLISECONDS.sleep(sessionTimeoutMs / 2);
  }
  long newSessionId = newZk.getSessionId();
  assertTrue(newZk == zk);
  assertFalse(sessionId == newSessionId);
}

相关文章