private Map<String, RepositoryDefinition> loadRepositories(boolean watch)
     throws KeeperException, InterruptedException {
   Map<String, RepositoryDefinition> repositories = new HashMap<String, RepositoryDefinition>();
   List<String> children = zk.getChildren(REPOSITORY_COLLECTION_PATH, watch ? zkWatcher : null);
   for (String child : children) {
     repositories.put(child, loadRepository(child, watch));
   }
   return repositories;
 }
Beispiel #2
0
  private synchronized void refreshServers() throws InterruptedException, KeeperException {
    Set<String> currentServers = new HashSet<String>();

    boolean retry;
    do {
      retry = false;
      try {
        currentServers.addAll(zk.getChildren(nodesPath, true));
      } catch (KeeperException.NoNodeException e) {
        // The path does not exist: this can happen if the client is started before
        // any Lily server has ever been started, or when using the LilyLauncher
        // from the test framework and calling its resetLilyState JMX operation.
        // In this case, put a watcher to be notified when the path is created.
        Stat stat = zk.exists(nodesPath, true);
        if (stat == null) {
          if (log.isInfoEnabled()) {
            log.info("The path with Lily servers does not exist in ZooKeeper: " + nodesPath);
          }
          clearServers();
          return;
        } else {
          // The node was created in between the getChildren and exists calls: retry
          retry = true;
        }
      }
    } while (retry);

    Set<String> removedServers = new HashSet<String>();
    removedServers.addAll(serverAddresses);
    removedServers.removeAll(currentServers);

    Set<String> newServers = new HashSet<String>();
    newServers.addAll(currentServers);
    newServers.removeAll(serverAddresses);

    if (log.isDebugEnabled()) {
      log.debug(
          "# current servers in ZK: "
              + currentServers.size()
              + ", # added servers: "
              + newServers.size()
              + ", # removed servers: "
              + removedServers.size());
    }

    // Remove removed servers
    Iterator<ServerNode> serverIt = servers.iterator();
    while (serverIt.hasNext()) {
      ServerNode server = serverIt.next();
      if (removedServers.contains(server.lilyAddressAndPort)) {
        serverIt.remove();
        Closer.close(server.repository);
      }
    }
    serverAddresses.removeAll(removedServers);

    // Add new servers
    for (String server : newServers) {
      servers.add(new ServerNode(server));
      serverAddresses.add(server);
    }

    if (log.isInfoEnabled()) {
      log.info("Current Lily servers = " + serverAddresses.toString());
    }
  }