Esempio n. 1
0
  @Override
  public void close() throws IOException {
    zk.removeDefaultWatcher(watcher);

    schemaCache.close();

    for (ServerNode node : servers) {
      Closer.close(node.repository);
    }

    if (managedZk && zk != null) {
      zk.close();
    }

    // Close pools related to the Configuration objects managed by this LilyClient instance
    for (Configuration config : hbaseConnections.getConfigurations()) {
      LocalHTable.closePool(config);
    }

    // Close HBase connections created by [only] this LilyClient instance.
    // This will almost always contain only one connection, if not we would need a more
    // advanced connection mgmt so that these connections don't stay open for the lifetime
    // of LilyClient.
    Closer.close(hbaseConnections);

    this.isClosed = true;
  }
 public void init() throws KeeperException, InterruptedException {
   ZkUtil.createPath(zk, REPOSITORY_COLLECTION_PATH);
   assureDefaultRepositoryExists();
   zkWatcher = new RepositoryZkWatcher();
   zk.addDefaultWatcher(zkWatcher);
   refresh();
 }
 @Override
 public void create(String repositoryName)
     throws RepositoryExistsException, InterruptedException, RepositoryModelException {
   if (!RepoDefUtil.isValidRepositoryName(repositoryName)) {
     throw new IllegalArgumentException(
         String.format(
             "'%s' is not a valid repository name. " + RepoDefUtil.VALID_NAME_EXPLANATION,
             repositoryName));
   }
   RepositoryDefinition repoDef =
       new RepositoryDefinition(repositoryName, RepositoryLifecycleState.CREATE_REQUESTED);
   byte[] repoBytes = RepositoryDefinitionJsonSerDeser.INSTANCE.toJsonBytes(repoDef);
   try {
     zk.create(
         REPOSITORY_COLLECTION_PATH + "/" + repositoryName,
         repoBytes,
         ZooDefs.Ids.OPEN_ACL_UNSAFE,
         CreateMode.PERSISTENT);
   } catch (KeeperException.NodeExistsException e) {
     throw new RepositoryExistsException(
         "Can't create repository, a repository with this name already exists: " + repositoryName);
   } catch (KeeperException e) {
     throw new RepositoryModelException(e);
   }
 }
Esempio n. 4
0
 private void init()
     throws InterruptedException, KeeperException, NoServersException, RepositoryException {
   zk.addDefaultWatcher(watcher);
   refreshServers();
   schemaCache.start();
   this.isClosed = false;
 }
Esempio n. 5
0
 private static URI getDfsUri(ZooKeeperItf zk) {
   try {
     return new URI(new String(zk.getData(blobDfsUriPath, false, new Stat())));
   } catch (Exception e) {
     throw new RuntimeException(
         "Blob stores config lookup: failed to get DFS URI from ZooKeeper", e);
   }
 }
Esempio n. 6
0
 private static BlobStoreAccessConfig getBlobStoreAccessConfig(ZooKeeperItf zk) {
   try {
     return new BlobStoreAccessConfig(zk.getData(blobStoreAccessConfigPath, false, new Stat()));
   } catch (Exception e) {
     throw new RuntimeException(
         "Blob stores config lookup: failed to get blob store access config from ZooKeeper", e);
   }
 }
 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;
 }
 private void assureDefaultRepositoryExists() throws KeeperException, InterruptedException {
   byte[] repoBytes = RepositoryDefinitionJsonSerDeser.INSTANCE.toJsonBytes(DEFAULT_REPOSITORY);
   try {
     zk.create(
         REPOSITORY_COLLECTION_PATH + "/" + DEFAULT_REPOSITORY.getName(),
         repoBytes,
         ZooDefs.Ids.OPEN_ACL_UNSAFE,
         CreateMode.PERSISTENT);
   } catch (KeeperException.NodeExistsException e) {
     // it already exists, fine
   }
 }
 @Override
 public void deleteDirect(String repositoryName)
     throws InterruptedException, RepositoryModelException, RepositoryNotFoundException {
   disallowDefaultRepository(repositoryName);
   try {
     zk.delete(REPOSITORY_COLLECTION_PATH + "/" + repositoryName, -1);
   } catch (KeeperException.NoNodeException e) {
     throw new RepositoryNotFoundException(
         "Can't delete repository, a repository with this name doesn't exist: " + repositoryName);
   } catch (KeeperException e) {
     throw new RepositoryModelException("Error deleting repository.", e);
   }
 }
Esempio n. 10
0
 public static Configuration getHBaseConfiguration(ZooKeeperItf zk) {
   try {
     Configuration configuration = HBaseConfiguration.create();
     byte[] data = zk.getData(hbaseConfigPath, false, new Stat());
     ObjectNode propertiesNode =
         (ObjectNode) JsonFormat.deserializeSoft(data, "HBase configuration");
     Iterator<Map.Entry<String, JsonNode>> it = propertiesNode.getFields();
     while (it.hasNext()) {
       Map.Entry<String, JsonNode> entry = it.next();
       configuration.set(entry.getKey(), entry.getValue().getTextValue());
     }
     return configuration;
   } catch (Exception e) {
     throw new RuntimeException("Failed to get HBase configuration from ZooKeeper", e);
   }
 }
 private RepositoryDefinition loadRepository(String name, boolean watch)
     throws KeeperException, InterruptedException {
   byte[] repoJson =
       zk.getData(REPOSITORY_COLLECTION_PATH + "/" + name, watch ? zkWatcher : null, new Stat());
   return RepositoryDefinitionJsonSerDeser.INSTANCE.fromJsonBytes(name, repoJson);
 }
 public void storeRepository(RepositoryDefinition repoDef)
     throws InterruptedException, KeeperException {
   byte[] repoBytes = RepositoryDefinitionJsonSerDeser.INSTANCE.toJsonBytes(repoDef);
   zk.setData(REPOSITORY_COLLECTION_PATH + "/" + repoDef.getName(), repoBytes, -1);
 }
 public void close() {
   zk.removeDefaultWatcher(zkWatcher);
   closed = true;
 }
Esempio n. 14
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());
    }
  }