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

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

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

ZooKeeper.getSessionId介绍

[英]The session id for this ZooKeeper client instance. The value returned is not valid until the client connects to a server and may change after a re-connect. This method is NOT thread safe
[中]此ZooKeeper客户端实例的会话id。在客户端连接到服务器之前,返回的值无效,重新连接后可能会更改。此方法不是线程安全的

代码示例

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

public synchronized long getSessionId() {
 return zk == null ? -1 : zk.getSessionId();
}

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

public String getSessionId() {
  ZooKeeper current = zk;
  if (current != null) {
    return String.format("0x%x", current.getSessionId());
  }
  return "";
}

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

public String getSessionId() {
  ZooKeeper current = zk;
  if (current != null) {
    return String.format("0x%x", current.getSessionId());
  }
  return "";
}

代码示例来源:origin: org.apache.hadoop/hadoop-common

@VisibleForTesting
synchronized long getZKSessionIdForTests() {
 if (zkClient != null) {
  return zkClient.getSessionId();
 } else {
  return -1;
 }
}

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

public synchronized void reconnectAfterExpiration()
   throws IOException, KeeperException, InterruptedException {
 if (zk != null) {
  LOG.info("Closing dead ZooKeeper connection, session" +
   " was: 0x"+Long.toHexString(zk.getSessionId()));
  zk.close();
  // reset the ZooKeeper connection
  zk = null;
 }
 checkZk();
 LOG.info("Recreated a ZooKeeper, session" +
  " is: 0x"+Long.toHexString(zk.getSessionId()));
}

代码示例来源:origin: org.apache.hadoop/hadoop-common

/**
 * The callbacks and watchers pass a reference to the ZK client
 * which made the original call. We don't want to take action
 * based on any callbacks from prior clients after we quit
 * the election.
 * @param ctx the ZK client passed into the watcher
 * @return true if it matches the current client
 */
private synchronized boolean isStaleClient(Object ctx) {
 Preconditions.checkNotNull(ctx);
 if (zkClient != (ZooKeeper)ctx) {
  LOG.warn("Ignoring stale result from old client with sessionId {}",
    String.format("0x%08x", ((ZooKeeper)ctx).getSessionId()));
  return true;
 }
 return false;
}

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

/** */
  public String getZkSessionId() {
    if (rtState.zkClient != null && rtState.zkClient.zk() != null)
      return Long.toHexString(rtState.zkClient.zk().getSessionId());
    else
      return null;
  }
}

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

LOG.debug("Closing session: 0x" + Long.toHexString(getSessionId()));
LOG.info("Session: 0x" + Long.toHexString(getSessionId()) + " closed");

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

private void verifyUnexpectedBeans(Set<ObjectName> children) {
  if (allClients != null) {
    for (ZooKeeper zkc : allClients) {
      Iterator<ObjectName> childItr = children.iterator();
      while (childItr.hasNext()) {
        ObjectName clientBean = childItr.next();
        if (clientBean.toString().contains(
            getHexSessionId(zkc.getSessionId()))) {
          LOG.info("found name:" + zkc.getSessionId()
              + " client bean:" + clientBean.toString());
          childItr.remove();
        }
      }
    }
  }
  for (ObjectName bean : children) {
    LOG.info("unexpected:" + bean.toString());
  }
  Assert.assertEquals("Unexpected bean exists!", 0, children.size());
}

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

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();
}

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

private static String createLockNodeV3(ZooKeeper zk, String lockPath, String clientId) throws Exception {
  return zk.create(getLockPathPrefixV3(lockPath, clientId, zk.getSessionId()), serializeClientId(clientId),
      ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
}

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

@Test
public void directCheckUpgradeSessionTest() throws IOException, InterruptedException, KeeperException {
  final ZooKeeper zk = createClient();
  String path = "/directcheckupgradesession";
  zk.create(path, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
  QuorumZooKeeperServer server = getConnectedServer(zk.getSessionId());
  Assert.assertNotNull("unable to find server interlocutor", server);
  Request readRequest = makeGetDataRequest(path, zk.getSessionId());
  Request createRequest = makeCreateRequest(path + "/e", zk.getSessionId());
  Assert.assertNull("tried to upgrade on a read", server.checkUpgradeSession(readRequest));
  Assert.assertNotNull("failed to upgrade on a create", server.checkUpgradeSession(createRequest));
  Assert.assertNull("tried to upgrade after successful promotion",
      server.checkUpgradeSession(createRequest));
}

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

@Test
public void ephemeralCreateMultiOpTest() throws KeeperException, InterruptedException, IOException {
  final ZooKeeper zk = createClient();
  String data = "test";
  String path = "/ephemeralcreatemultiop";
  zk.create(path, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
  QuorumZooKeeperServer server = getConnectedServer(zk.getSessionId());
  Assert.assertNotNull("unable to find server interlocutor", server);
  UpgradeableSessionTracker sessionTracker = (UpgradeableSessionTracker)server.getSessionTracker();
  Assert.assertFalse("session already global", sessionTracker.isGlobalSession(zk.getSessionId()));
  List<OpResult> multi = null;
  try {
    multi = zk.multi(Arrays.asList(
        Op.setData(path, data.getBytes(), 0),
        Op.create(path + "/e", data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL),
        Op.create(path + "/p", data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT),
        Op.create(path + "/q", data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL)
    ));
  } catch (KeeperException.SessionExpiredException e) {
    // the scenario that inspired this unit test
    Assert.fail("received session expired for a session promotion in a multi-op");
  }
  Assert.assertNotNull(multi);
  Assert.assertEquals(4, multi.size());
  Assert.assertEquals(data, new String(zk.getData(path + "/e", false, null)));
  Assert.assertEquals(data, new String(zk.getData(path + "/p", false, null)));
  Assert.assertEquals(data, new String(zk.getData(path + "/q", false, null)));
  Assert.assertTrue("session not promoted", sessionTracker.isGlobalSession(zk.getSessionId()));
}

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

@Test
public void testSessionExpire() throws Exception {
 assertArrayEquals(DATA, RO_ZK.get(PATH).get());
 ZooKeeper zk = RO_ZK.zookeeper;
 long sessionId = zk.getSessionId();
 UTIL.getZkCluster().getZooKeeperServers().get(0).closeSession(sessionId);
 // should not reach keep alive so still the same instance
 assertSame(zk, RO_ZK.zookeeper);
 byte[] got = RO_ZK.get(PATH).get();
 assertArrayEquals(DATA, got);
 assertNotNull(RO_ZK.zookeeper);
 assertNotSame(zk, RO_ZK.zookeeper);
 assertNotEquals(sessionId, RO_ZK.zookeeper.getSessionId());
}

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

Assert.assertSame("should be in r/o mode", States.CONNECTEDREADONLY, zk
    .getState());
long fakeId = zk.getSessionId();
LOG.info("Connected as r/o mode with state {} and session id {}",
    zk.getState(), fakeId);
    zk.getState());
LOG.info("Connected as rw mode with state {} and session id {}",
    zk.getState(), zk.getSessionId());
zk.create("/test", "test".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,
    CreateMode.PERSISTENT);
Assert.assertFalse("fake session and real session have same id", zk
    .getSessionId() == fakeId);
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);
}

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

@Test
public void testOnlyUpgradeSessionOnce()
    throws IOException, InterruptedException, KeeperException {
  // create a client, and create an ephemeral node to trigger the
  // upgrading process
  final String node = "/node-1";
  ZooKeeper zk = new ZooKeeper("127.0.0.1:" + clientPorts[0],
        ClientBase.CONNECTION_TIMEOUT, this);
  waitForOne(zk, States.CONNECTED);
  long sessionId = zk.getSessionId();
  QuorumZooKeeperServer server =
      (QuorumZooKeeperServer) mt[0].main.quorumPeer.getActiveServer();
  Request create1 = createEphemeralRequest("/data-1", sessionId);
  Request create2 = createEphemeralRequest("/data-2", sessionId);
  Assert.assertNotNull("failed to upgrade on a ephemeral create",
      server.checkUpgradeSession(create1));
  Assert.assertNull("tried to upgrade again", server.checkUpgradeSession(create2));
  // clean al the setups and close the zk
  zk.close();
}

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

watcher.waitForConnected(CONNECTION_TIMEOUT);
long localSessionId2 = zk.getSessionId();

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

Assert.assertEquals(0, stat.getVersion());
Assert.assertEquals(0, stat.getAversion());
Assert.assertEquals(zk.getSessionId(), stat.getEphemeralOwner());
Assert.assertEquals(childname.length(), stat.getDataLength());
Assert.assertEquals(0, stat.getNumChildren());

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

Assert.assertEquals(0, stat.getVersion());
Assert.assertEquals(0, stat.getAversion());
Assert.assertEquals(zk.getSessionId(), stat.getEphemeralOwner());
Assert.assertEquals(childname.length(), stat.getDataLength());
Assert.assertEquals(0, stat.getNumChildren());

相关文章