Java Code Examples for org.apache.zookeeper.KeeperException#SessionExpiredException

The following examples show how to use org.apache.zookeeper.KeeperException#SessionExpiredException . 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: SpliceZooKeeperManager.java    From spliceengine with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public void abort(String why,Throwable e){
    if(e instanceof KeeperException.SessionExpiredException){
        try{
            LOG.info("Lost connection with ZooKeeper, attempting reconnect");
            watcher=null;
            initialize(HConfiguration.getConfiguration());
            LOG.info("Successfully reconnected to ZooKeeper");
        }catch(RuntimeException zce){
            LOG.error("Could not reconnect to zookeeper after session expiration, aborting");
            e=zce;
        }
    }
    if(e!=null)
        LOG.error(why,e);
    else
        LOG.error(why);
    this.isAborted=true;
}
 
Example 2
Source File: ZookeeperMetadataStorageManager.java    From herddb with Apache License 2.0 5 votes vote down vote up
private void handleSessionExpiredError(Throwable error) {
    if (!(error instanceof KeeperException.SessionExpiredException)) {
        return;
    }
    try {
        restartZooKeeper();
    } catch (IOException | InterruptedException err) {
        LOGGER.log(Level.SEVERE, "Error handling session expired", err);
    }
}
 
Example 3
Source File: ZookeeperMetadataStorageManagerTest.java    From herddb with Apache License 2.0 5 votes vote down vote up
@Test
public void testSessionExpired() throws Exception {
    try (ZookeeperMetadataStorageManager man = new ZookeeperMetadataStorageManager(testEnv.getAddress(),
            testEnv.getTimeout(), testEnv.getPath())) {
        man.start();

        TableSpace tableSpace = TableSpace
                .builder()
                .leader("test")
                .replica("test")
                .name(TableSpace.DEFAULT)
                .build();
        man.registerTableSpace(tableSpace);
        assertEquals(1, man.listTableSpaces().size());
        ZooKeeper actual = man.getZooKeeper();
        long sessionId = actual.getSessionId();
        byte[] passwd = actual.getSessionPasswd();
        expireZkSession(sessionId, passwd);
        for (int i = 0; i < 10; i++) {
            try {
                man.listTableSpaces();
                fail("session should be expired or not connected");
            } catch (MetadataStorageManagerException ok) {
                System.out.println("ok: " + ok);
                assertTrue(ok.getCause() instanceof KeeperException.ConnectionLossException
                        || ok.getCause() instanceof KeeperException.SessionExpiredException);
                if (ok.getCause() instanceof KeeperException.SessionExpiredException) {
                    break;
                }
            }
            Thread.sleep(500);
        }
        assertNotSame(actual, man.getZooKeeper());
        assertEquals(1, man.listTableSpaces().size());

        assertNotNull(tableSpace = man.describeTableSpace(TableSpace.DEFAULT));
        man.dropTableSpace(TableSpace.DEFAULT, tableSpace);
    }
}
 
Example 4
Source File: ShardLeaderElectionContext.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private boolean areAllReplicasParticipating() throws InterruptedException {
  final String shardsElectZkPath = electionPath + LeaderElector.ELECTION_NODE;
  final DocCollection docCollection = zkController.getClusterState().getCollectionOrNull(collection);

  if (docCollection != null && docCollection.getSlice(shardId) != null) {
    final Slice slices = docCollection.getSlice(shardId);
    int found = 0;
    try {
      found = zkClient.getChildren(shardsElectZkPath, null, true).size();
    } catch (KeeperException e) {
      if (e instanceof KeeperException.SessionExpiredException) {
        // if the session has expired, then another election will be launched, so
        // quit here
        throw new SolrException(ErrorCode.SERVER_ERROR,
            "ZK session expired - cancelling election for " + collection + " " + shardId);
      }
      SolrException.log(log, "Error checking for the number of election participants", e);
    }

    if (found >= slices.getReplicasMap().size()) {
      log.debug("All replicas are ready to participate in election.");
      return true;
    }
  } else {
    log.warn("Shard not found: {} for collection {}", shardId, collection);
    return false;
  }
  return false;
}
 
Example 5
Source File: SessionFailedRetryPolicy.java    From curator with Apache License 2.0 5 votes vote down vote up
@Override
public boolean allowRetry(Throwable exception)
{
    if ( exception instanceof KeeperException.SessionExpiredException )
    {
        return false;
    }
    else
    {
        return delegatePolicy.allowRetry(exception);
    }
}
 
Example 6
Source File: SpliceZooKeeperManager.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
public <T> T execute(Command<T> command) throws InterruptedException, KeeperException{
    try{
        return command.execute(getRecoverableZooKeeper());
    }catch(ZooKeeperConnectionException e){
        throw new KeeperException.SessionExpiredException();
    }
}
 
Example 7
Source File: ShardLeaderElectionContext.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private boolean waitForReplicasToComeUp(int timeoutms) throws InterruptedException {
  long timeoutAt = System.nanoTime() + TimeUnit.NANOSECONDS.convert(timeoutms, TimeUnit.MILLISECONDS);
  final String shardsElectZkPath = electionPath + LeaderElector.ELECTION_NODE;

  DocCollection docCollection = zkController.getClusterState().getCollectionOrNull(collection);
  Slice slices = (docCollection == null) ? null : docCollection.getSlice(shardId);
  int cnt = 0;
  while (!isClosed && !cc.isShutDown()) {
    // wait for everyone to be up
    if (slices != null) {
      int found = 0;
      try {
        found = zkClient.getChildren(shardsElectZkPath, null, true).size();
      } catch (KeeperException e) {
        if (e instanceof KeeperException.SessionExpiredException) {
          // if the session has expired, then another election will be launched, so
          // quit here
          throw new SolrException(ErrorCode.SERVER_ERROR,
              "ZK session expired - cancelling election for " + collection + " " + shardId);
        }
        SolrException.log(log,
            "Error checking for the number of election participants", e);
      }

      // on startup and after connection timeout, wait for all known shards
      if (found >= slices.getReplicas(EnumSet.of(Replica.Type.TLOG, Replica.Type.NRT)).size()) {
        log.info("Enough replicas found to continue.");
        return true;
      } else {
        if (cnt % 40 == 0) {
          if (log.isInfoEnabled()) {
            log.info("Waiting until we see more replicas up for shard {}: total={} found={} timeoute in={}ms"
                , shardId, slices.getReplicas(EnumSet.of(Replica.Type.TLOG, Replica.Type.NRT)).size(), found,
                TimeUnit.MILLISECONDS.convert(timeoutAt - System.nanoTime(), TimeUnit.NANOSECONDS));
          }
        }
      }

      if (System.nanoTime() > timeoutAt) {
        log.info("Was waiting for replicas to come up, but they are taking too long - assuming they won't come back till later");
        return false;
      }
    } else {
      log.warn("Shard not found: {} for collection {}", shardId, collection);

      return false;

    }

    Thread.sleep(500);
    docCollection = zkController.getClusterState().getCollectionOrNull(collection);
    slices = (docCollection == null) ? null : docCollection.getSlice(shardId);
    cnt++;
  }
  return false;
}
 
Example 8
Source File: SpliceZooKeeperManager.java    From spliceengine with GNU Affero General Public License v3.0 4 votes vote down vote up
public <T> T executeUnlessExpired(Command<T> command) throws InterruptedException, KeeperException{
    /*
     * What actually happens is that, in the event of a long network partition, ZooKeeper will throw
     * ConnectionLoss exceptions, but it will NOT throw a SessionExpired exception until it reconnects, even
     * if it's been disconnected for CLEARLY longer than the session timeout.
     *
     * To deal with this, we have to basically loop through our command repeatedly until we either
     *
     * 1. Succeed.
     * 2. Get a SessionExpired event from ZooKeeper
     * 3. Spent more than 2*sessionTimeout ms attempting the request
     * 4. Get some other kind of Zk error (NoNode, etc).
     */
    RecoverableZooKeeper rzk;
    try{
        rzk=getRecoverableZooKeeper();
    }catch(ZooKeeperConnectionException e){
        throw new KeeperException.SessionExpiredException();
    }
    //multiple by 2 to make absolutely certain we're timed out.
    int sessionTimeout=2*rzk.getZooKeeper().getSessionTimeout();
    long nextTime=System.currentTimeMillis();
    long startTime=System.currentTimeMillis();
    while((int)(nextTime-startTime)<sessionTimeout){
        try{
            return command.execute(rzk);
        }catch(KeeperException ke){
            switch(ke.code()){
                case CONNECTIONLOSS:
                case OPERATIONTIMEOUT:
                    LOG.warn("Detected a Connection issue("+ke.code()+") with ZooKeeper, retrying");
                    nextTime=System.currentTimeMillis();
                    break;
                default:
                    throw ke;
            }
        }
    }

    //we've run out of time, our session has almost certainly expired. Give up and explode
    throw new KeeperException.SessionExpiredException();
}