Java Code Examples for org.apache.solr.common.cloud.SolrZkClient#checkInterrupted()

The following examples show how to use org.apache.solr.common.cloud.SolrZkClient#checkInterrupted() . 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: AbstractSolrConfigHandler.java    From ambari-logsearch with Apache License 2.0 6 votes vote down vote up
public boolean doIfConfigExists(SolrPropsConfig solrPropsConfig, SolrZkClient zkClient, String separator) throws IOException {
  logger.info("Config set exists for '{}' collection. Refreshing it if needed...", solrPropsConfig.getCollection());
  try {
    File[] listOfFiles = getConfigSetFolder().listFiles();
    if (listOfFiles == null)
      return false;
    byte[] data = zkClient.getData(String.format("%s/%s/%s", CONFIGS_ZKNODE, solrPropsConfig.getConfigName(), getConfigFileName()), null, null, true);

    for (File file : listOfFiles) {
      if (file.getName().equals(getConfigFileName()) && updateConfigIfNeeded(solrPropsConfig, zkClient, file, separator, data)) {
        return true;
      }
    }
    return false;
  } catch (KeeperException | InterruptedException e) {
    throw new IOException("Error downloading files from zookeeper path " + solrPropsConfig.getConfigName(),
            SolrZkClient.checkInterrupted(e));
  }
}
 
Example 2
Source File: BackupManager.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void uploadCollectionProperties(URI backupLoc, String backupId, String collectionName) throws IOException {
  URI sourceDir = repository.resolve(backupLoc, backupId, ZK_STATE_DIR);
  URI source = repository.resolve(sourceDir, ZkStateReader.COLLECTION_PROPS_ZKNODE);
  if (!repository.exists(source)) {
    // No collection properties to restore
    return;
  }
  String zkPath = ZkStateReader.COLLECTIONS_ZKNODE + '/' + collectionName + '/' + ZkStateReader.COLLECTION_PROPS_ZKNODE;

  try (IndexInput is = repository.openInput(sourceDir, ZkStateReader.COLLECTION_PROPS_ZKNODE, IOContext.DEFAULT)) {
    byte[] arr = new byte[(int) is.length()];
    is.readBytes(arr, 0, (int) is.length());
    zkStateReader.getZkClient().create(zkPath, arr, CreateMode.PERSISTENT, true);
  } catch (KeeperException | InterruptedException e) {
    throw new IOException("Error uploading file to zookeeper path " + source.toString() + " to " + zkPath,
        SolrZkClient.checkInterrupted(e));
  }
}
 
Example 3
Source File: BackupManager.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void downloadCollectionProperties(URI backupLoc, String backupId, String collectionName) throws IOException {
  URI dest = repository.resolve(backupLoc, backupId, ZK_STATE_DIR, ZkStateReader.COLLECTION_PROPS_ZKNODE);
  String zkPath = ZkStateReader.COLLECTIONS_ZKNODE + '/' + collectionName + '/' + ZkStateReader.COLLECTION_PROPS_ZKNODE;


  try {
    if (!zkStateReader.getZkClient().exists(zkPath, true)) {
      // Nothing to back up
      return;
    }

    try (OutputStream os = repository.createOutput(dest)) {
      byte[] data = zkStateReader.getZkClient().getData(zkPath, null, null, true);
      os.write(data);
    }
  } catch (KeeperException | InterruptedException e) {
    throw new IOException("Error downloading file from zookeeper path " + zkPath + " to " + dest.toString(),
        SolrZkClient.checkInterrupted(e));
  }
}
 
Example 4
Source File: BackupManager.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void downloadFromZK(SolrZkClient zkClient, String zkPath, URI dir) throws IOException {
  try {
    if (!repository.exists(dir)) {
      repository.createDirectory(dir);
    }
    List<String> files = zkClient.getChildren(zkPath, null, true);
    for (String file : files) {
      List<String> children = zkClient.getChildren(zkPath + "/" + file, null, true);
      if (children.size() == 0) {
        log.debug("Writing file {}", file);
        byte[] data = zkClient.getData(zkPath + "/" + file, null, null, true);
        try (OutputStream os = repository.createOutput(repository.resolve(dir, file))) {
          os.write(data);
        }
      } else {
        downloadFromZK(zkClient, zkPath + "/" + file, repository.resolve(dir, file));
      }
    }
  } catch (KeeperException | InterruptedException e) {
    throw new IOException("Error downloading files from zookeeper path " + zkPath + " to " + dir.toString(),
        SolrZkClient.checkInterrupted(e));
  }
}
 
Example 5
Source File: ZkTestServer.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Check that a path exists in this server
 * @param path the path to check
 * @throws IOException if an IO exception occurs
 */
public void ensurePathExists(String path) throws IOException {
  try (SolrZkClient client = new SolrZkClient(getZkHost(), 10000)) {
    client.makePath(path, null, CreateMode.PERSISTENT, null, false, true, 0);
  } catch (InterruptedException | KeeperException e) {
    e.printStackTrace();
    throw new IOException("Error checking path " + path, SolrZkClient.checkInterrupted(e));
  }
}
 
Example 6
Source File: ZKPropertiesWriter.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, Object> readIndexerProperties() {
  Properties props = new Properties();
  try {
    byte[] data = zkClient.getData(path, null, null, true);
    if (data != null) {
      props.load(new StringReader(new String(data, StandardCharsets.UTF_8)));
    }
  } catch (Exception e) {
    SolrZkClient.checkInterrupted(e);
    log.warn("Could not read DIH properties from {} : {}", path, e.getClass(), e);
  }
  return propertiesToMap(props);
}
 
Example 7
Source File: ContainerPluginsApi.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"rawtypes", "unchecked"})
private void persistPlugins(Function<Map<String,Object>, Map<String,Object>> modifier) throws IOException {
  try {
    zkClientSupplier.get().atomicUpdate(ZkStateReader.CLUSTER_PROPS, bytes -> {
      Map rawJson = bytes == null ? new LinkedHashMap() :
          (Map) Utils.fromJSON(bytes);
      Map pluginsModified = modifier.apply((Map) rawJson.computeIfAbsent(PLUGIN, Utils.NEW_LINKED_HASHMAP_FUN));
      if (pluginsModified == null) return null;
      rawJson.put(PLUGIN, pluginsModified);
      return Utils.toJSON(rawJson);
    });
  } catch (KeeperException | InterruptedException e) {
    throw new IOException("Error reading cluster property", SolrZkClient.checkInterrupted(e));
  }
}
 
Example 8
Source File: BackupManager.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void uploadToZk(SolrZkClient zkClient, URI sourceDir, String destZkPath) throws IOException {
  Preconditions.checkArgument(repository.exists(sourceDir), "Path {} does not exist", sourceDir);
  Preconditions.checkArgument(repository.getPathType(sourceDir) == PathType.DIRECTORY,
      "Path {} is not a directory", sourceDir);

  for (String file : repository.listAll(sourceDir)) {
    String zkNodePath = destZkPath + "/" + file;
    URI path = repository.resolve(sourceDir, file);
    PathType t = repository.getPathType(path);
    switch (t) {
      case FILE: {
        try (IndexInput is = repository.openInput(sourceDir, file, IOContext.DEFAULT)) {
          byte[] arr = new byte[(int) is.length()]; // probably ok since the config file should be small.
          is.readBytes(arr, 0, (int) is.length());
          zkClient.makePath(zkNodePath, arr, true);
        } catch (KeeperException | InterruptedException e) {
          throw new IOException(SolrZkClient.checkInterrupted(e));
        }
        break;
      }

      case DIRECTORY: {
        if (!file.startsWith(".")) {
          uploadToZk(zkClient, path, zkNodePath);
        }
        break;
      }
      default:
        throw new IllegalStateException("Unknown path type " + t);
    }
  }
}
 
Example 9
Source File: CollectionsHandler.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public SolrResponse sendToOCPQueue(ZkNodeProps m, long timeout) throws KeeperException, InterruptedException {
  String operation = m.getStr(QUEUE_OPERATION);
  if (operation == null) {
    throw new SolrException(ErrorCode.BAD_REQUEST, "missing key " + QUEUE_OPERATION);
  }
  if (m.get(ASYNC) != null) {

    String asyncId = m.getStr(ASYNC);

    if (asyncId.equals("-1")) {
      throw new SolrException(ErrorCode.BAD_REQUEST, "requestid can not be -1. It is reserved for cleanup purposes.");
    }

    NamedList<String> r = new NamedList<>();


    if (coreContainer.getZkController().claimAsyncId(asyncId)) {
      boolean success = false;
      try {
        coreContainer.getZkController().getOverseerCollectionQueue()
            .offer(Utils.toJSON(m));
        success = true;
      } finally {
        if (!success) {
          try {
            coreContainer.getZkController().clearAsyncId(asyncId);
          } catch (Exception e) {
            // let the original exception bubble up
            log.error("Unable to release async ID={}", asyncId, e);
            SolrZkClient.checkInterrupted(e);
          }
        }
      }
    } else {
      r.add("error", "Task with the same requestid already exists.");
    }
    r.add(CoreAdminParams.REQUESTID, (String) m.get(ASYNC));

    return new OverseerSolrResponse(r);
  }

  long time = System.nanoTime();
  QueueEvent event = coreContainer.getZkController()
      .getOverseerCollectionQueue()
      .offer(Utils.toJSON(m), timeout);
  if (event.getBytes() != null) {
    return OverseerSolrResponseSerializer.deserialize(event.getBytes());
  } else {
    if (System.nanoTime() - time >= TimeUnit.NANOSECONDS.convert(timeout, TimeUnit.MILLISECONDS)) {
      throw new SolrException(ErrorCode.SERVER_ERROR, operation
          + " the collection time out:" + timeout / 1000 + "s");
    } else if (event.getWatchedEvent() != null) {
      throw new SolrException(ErrorCode.SERVER_ERROR, operation
          + " the collection error [Watcher fired on path: "
          + event.getWatchedEvent().getPath() + " state: "
          + event.getWatchedEvent().getState() + " type "
          + event.getWatchedEvent().getType() + "]");
    } else {
      throw new SolrException(ErrorCode.SERVER_ERROR, operation
          + " the collection unknown case");
    }
  }
}