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

The following examples show how to use org.apache.solr.common.cloud.SolrZkClient#exists() . 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: CdcrProcessStateManager.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void createStateNode() {
  SolrZkClient zkClient = core.getCoreContainer().getZkController().getZkClient();
  try {
    if (!zkClient.exists(this.getZnodePath(), true)) {
      if (!zkClient.exists(this.getZnodeBase(), true)) { // Should be a no-op if the node exists
        zkClient.makePath(this.getZnodeBase(), null, CreateMode.PERSISTENT, null, false, true);
      }
      zkClient.create(this.getZnodePath(), DEFAULT_STATE.getBytes(), CreateMode.PERSISTENT, true);
      if (log.isInfoEnabled()) {
        log.info("Created znode {}", this.getZnodePath());
      }
    }
  } catch (KeeperException.NodeExistsException ne) {
    // Someone got in first and created the node.
  } catch (KeeperException | InterruptedException e) {
    log.warn("Failed to create CDCR process state node", e);
  }
}
 
Example 2
Source File: CdcrBufferStateManager.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void createStateNode() {
  SolrZkClient zkClient = core.getCoreContainer().getZkController().getZkClient();
  try {
    if (!zkClient.exists(this.getZnodePath(), true)) {
      if (!zkClient.exists(this.getZnodeBase(), true)) {
        zkClient.makePath(this.getZnodeBase(), null, CreateMode.PERSISTENT, null, false, true); // Should be a no-op if node exists
      }
      zkClient.create(this.getZnodePath(), DEFAULT_STATE.getBytes(), CreateMode.PERSISTENT, true);
      if (log.isInfoEnabled()) {
        log.info("Created znode {}", this.getZnodePath());
      }
    }
  } catch (KeeperException.NodeExistsException ne) {
    // Someone got in first and created the node.
  }  catch (KeeperException | InterruptedException e) {
    log.warn("Failed to create CDCR buffer state node", e);
  }
}
 
Example 3
Source File: ZkController.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Validates if the chroot exists in zk (or if it is successfully created).
 * Optionally, if create is set to true this method will create the path in
 * case it doesn't exist
 *
 * @return true if the path exists or is created false if the path doesn't
 * exist and 'create' = false
 */
public static boolean checkChrootPath(String zkHost, boolean create)
    throws KeeperException, InterruptedException {
  if (!SolrZkClient.containsChroot(zkHost)) {
    return true;
  }
  log.trace("zkHost includes chroot");
  String chrootPath = zkHost.substring(zkHost.indexOf("/"), zkHost.length());

  SolrZkClient tmpClient = new SolrZkClient(zkHost.substring(0,
      zkHost.indexOf("/")), 60000, 30000, null, null, null);
  boolean exists = tmpClient.exists(chrootPath, true);
  if (!exists && create) {
    tmpClient.makePath(chrootPath, false, true);
    exists = true;
  }
  tmpClient.close();
  return exists;
}
 
Example 4
Source File: UploadConfigurationHandler.java    From ambari-logsearch with Apache License 2.0 5 votes vote down vote up
private void uploadFileToZk(SolrZkClient zkClient, String filePath, String configsPath) throws FileNotFoundException {
  InputStream is = new FileInputStream(filePath);
  try {
    if (zkClient.exists(configsPath, true)) {
      zkClient.setData(configsPath, IOUtils.toByteArray(is), true);
    } else {
      zkClient.create(configsPath, IOUtils.toByteArray(is), CreateMode.PERSISTENT, true);
    }
  } catch (Exception e) {
    throw new IllegalStateException(e);
  } finally {
    IOUtils.closeQuietly(is);
  }
}
 
Example 5
Source File: ShowFileRequestHandler.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public static String getAdminFileFromZooKeeper(SolrQueryRequest req, SolrQueryResponse rsp, SolrZkClient zkClient,
                                               Set<String> hiddenFiles)
    throws KeeperException, InterruptedException {
  String adminFile = null;
  SolrCore core = req.getCore();

  final ZkSolrResourceLoader loader = (ZkSolrResourceLoader) core
      .getResourceLoader();
  String confPath = loader.getConfigSetZkPath();

  String fname = req.getParams().get("file", null);
  if (fname == null) {
    adminFile = confPath;
  } else {
    fname = fname.replace('\\', '/'); // normalize slashes
    if (isHiddenFile(req, rsp, fname, true, hiddenFiles)) {
      return null;
    }
    if (fname.startsWith("/")) { // Only files relative to conf are valid
      fname = fname.substring(1);
    }
    adminFile = confPath + "/" + fname;
  }

  // Make sure the file exists, is readable and is not a hidden file
  if (!zkClient.exists(adminFile, true)) {
    log.error("Can not find: {}", adminFile);
    rsp.setException(new SolrException(SolrException.ErrorCode.NOT_FOUND, "Can not find: "
        + adminFile));
    return null;
  }

  return adminFile;
}
 
Example 6
Source File: ConfigSetsHandler.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void createZkNodeIfNotExistsAndSetData(SolrZkClient zkClient,
                                               String filePathInZk, byte[] data) throws Exception {
  if (!zkClient.exists(filePathInZk, true)) {
    zkClient.create(filePathInZk, data, CreateMode.PERSISTENT, true);
  } else {
    zkClient.setData(filePathInZk, data, true);
  }
}
 
Example 7
Source File: ZkController.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * <p>Verifies if /clusterstate.json exists in Zookeepeer, and if it does and is not empty, refuses to start and outputs
 * a helpful message regarding collection migration.</p>
 *
 * <p>If /clusterstate.json exists and is empty, it is removed.</p>
 */
private void checkNoOldClusterstate(final SolrZkClient zkClient) throws InterruptedException {
  try {
    if (!zkClient.exists(ZkStateReader.UNSUPPORTED_CLUSTER_STATE, true)) {
      return;
    }

    final byte[] data = zkClient.getData(ZkStateReader.UNSUPPORTED_CLUSTER_STATE, null, null, true);

    if (Arrays.equals("{}".getBytes(StandardCharsets.UTF_8), data)) {
      // Empty json. This log will only occur once.
      log.warn("{} no longer supported starting with Solr 9. Found empty file on Zookeeper, deleting it.", ZkStateReader.UNSUPPORTED_CLUSTER_STATE);
      zkClient.delete(ZkStateReader.UNSUPPORTED_CLUSTER_STATE, -1, true);
    } else {
      // /clusterstate.json not empty: refuse to start but do not automatically delete. A bit of a pain but user shouldn't
      // have older collections at this stage anyway.
      String message = ZkStateReader.UNSUPPORTED_CLUSTER_STATE + " no longer supported starting with Solr 9. "
          + "It is present and not empty. Cannot start Solr. Please first migrate collections to stateFormat=2 using an "
          + "older version of Solr or if you don't care about the data then delete the file from "
          + "Zookeeper using a command line tool, for example: bin/solr zk rm /clusterstate.json -z host:port";
      log.error(message);
      throw new SolrException(SolrException.ErrorCode.INVALID_STATE, message);
    }
  } catch (KeeperException e) {
    // Convert checked exception to one acceptable by the caller (see also init() further down)
    log.error("", e);
    throw new ZooKeeperException(SolrException.ErrorCode.SERVER_ERROR, "", e);
  }
}
 
Example 8
Source File: ZkController.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private static void bootstrapDefaultConfigSet(SolrZkClient zkClient) throws KeeperException, InterruptedException, IOException {
  if (zkClient.exists("/configs/_default", true) == false) {
    String configDirPath = getDefaultConfigDirPath();
    if (configDirPath == null) {
      log.warn("The _default configset could not be uploaded. Please provide 'solr.default.confdir' parameter that points to a configset {} {}"
          , "intended to be the default. Current 'solr.default.confdir' value:"
          , System.getProperty(SolrDispatchFilter.SOLR_DEFAULT_CONFDIR_ATTRIBUTE));
    } else {
      ZkMaintenanceUtils.upConfig(zkClient, Paths.get(configDirPath), ConfigSetsHandlerApi.DEFAULT_CONFIGSET_NAME);
    }
  }
}
 
Example 9
Source File: SchemaManager.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private ManagedIndexSchema getFreshManagedSchema(SolrCore core) throws IOException,
    KeeperException, InterruptedException {

  SolrResourceLoader resourceLoader = core.getResourceLoader();
  String name = core.getLatestSchema().getResourceName();
  if (resourceLoader instanceof ZkSolrResourceLoader) {
    final ZkSolrResourceLoader zkLoader = (ZkSolrResourceLoader)resourceLoader;
    SolrZkClient zkClient = zkLoader.getZkController().getZkClient();
    try {
      if (!zkClient.exists(zkLoader.getConfigSetZkPath() + "/" + name, true)) {
        String backupName = name + ManagedIndexSchemaFactory.UPGRADED_SCHEMA_EXTENSION;
        if (!zkClient.exists(zkLoader.getConfigSetZkPath() + "/" + backupName, true)) {
          log.warn("Unable to retrieve fresh managed schema, neither {} nor {} exist.", name, backupName);
          // use current schema
          return (ManagedIndexSchema) core.getLatestSchema();
        } else {
          name = backupName;
        }
      }
    } catch (Exception e) {
      log.warn("Unable to retrieve fresh managed schema {}", name, e);
      // use current schema
      return (ManagedIndexSchema) core.getLatestSchema();
    }
    InputStream in = resourceLoader.openResource(name);
    if (in instanceof ZkSolrResourceLoader.ZkByteArrayInputStream) {
      int version = ((ZkSolrResourceLoader.ZkByteArrayInputStream) in).getStat().getVersion();
      log.info("managed schema loaded . version : {} ", version);
      return new ManagedIndexSchema(core.getSolrConfig(), name, new InputSource(in), true, name, version,
          core.getLatestSchema().getSchemaUpdateLock());
    } else {
      return (ManagedIndexSchema) core.getLatestSchema();
    }
  } else {
    return (ManagedIndexSchema) core.getLatestSchema();
  }
}
 
Example 10
Source File: ZooKeeperInspector.java    From examples with Apache License 2.0 5 votes vote down vote up
/**
 * Returns config value given collection name Borrowed heavily from Solr's ZKController.
 */
public String readConfigName(SolrZkClient zkClient, String collection) throws KeeperException,
    InterruptedException {
  if (collection == null) {
    throw new IllegalArgumentException("collection must not be null");
  }
  String configName = null;

  // first check for alias
  collection = checkForAlias(zkClient, collection);

  String path = ZkStateReader.COLLECTIONS_ZKNODE + "/" + collection;
  if (LOG.isInfoEnabled()) {
    LOG.info("Load collection config from:" + path);
  }
  byte[] data = zkClient.getData(path, null, null, true);

  if (data != null) {
    ZkNodeProps props = ZkNodeProps.load(data);
    configName = props.getStr(ZkController.CONFIGNAME_PROP);
  }

  if (configName != null
      && !zkClient.exists(ZkController.CONFIGS_ZKNODE + "/" + configName, true)) {
    LOG.error("Specified config does not exist in ZooKeeper:" + configName);
    throw new IllegalArgumentException("Specified config does not exist in ZooKeeper:"
        + configName);
  }

  return configName;
}
 
Example 11
Source File: ZooKeeperDownloader.java    From kite with Apache License 2.0 5 votes vote down vote up
/**
 * Returns config value given collection name
 * Borrowed heavily from Solr's ZKController.
 */
public String readConfigName(SolrZkClient zkClient, String collection)
throws KeeperException, InterruptedException {
  if (collection == null) {
    throw new IllegalArgumentException("collection must not be null");
  }
  String configName = null;

  // first check for alias
  byte[] aliasData = zkClient.getData(ZkStateReader.ALIASES, null, null, true);
  Aliases aliases = ClusterState.load(aliasData);
  String alias = aliases.getCollectionAlias(collection);
  if (alias != null) {
    List<String> aliasList = StrUtils.splitSmart(alias, ",", true);
    if (aliasList.size() > 1) {
      throw new IllegalArgumentException("collection cannot be an alias that maps to multiple collections");
    }
    collection = aliasList.get(0);
  }
  
  String path = ZkStateReader.COLLECTIONS_ZKNODE + "/" + collection;
  if (LOG.isInfoEnabled()) {
    LOG.info("Load collection config from:" + path);
  }
  byte[] data = zkClient.getData(path, null, null, true);
  
  if(data != null) {
    ZkNodeProps props = ZkNodeProps.load(data);
    configName = props.getStr(ZkController.CONFIGNAME_PROP);
  }
  
  if (configName != null && !zkClient.exists(ZkConfigManager.CONFIGS_ZKNODE + "/" + configName, true)) {
    LOG.error("Specified config does not exist in ZooKeeper:" + configName);
    throw new IllegalArgumentException("Specified config does not exist in ZooKeeper:"
      + configName);
  }

  return configName;
}
 
Example 12
Source File: ConfigSetsHandler.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private void handleConfigUploadRequest(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {
  if (!"true".equals(System.getProperty("configset.upload.enabled", "true"))) {
    throw new SolrException(ErrorCode.BAD_REQUEST,
        "Configset upload feature is disabled. To enable this, start Solr with '-Dconfigset.upload.enabled=true'.");
  }

  String configSetName = req.getParams().get(NAME);
  if (StringUtils.isBlank(configSetName)) {
    throw new SolrException(ErrorCode.BAD_REQUEST,
        "The configuration name should be provided in the \"name\" parameter");
  }

  SolrZkClient zkClient = coreContainer.getZkController().getZkClient();
  String configPathInZk = ZkConfigManager.CONFIGS_ZKNODE + Path.SEPARATOR + configSetName;

  if (zkClient.exists(configPathInZk, true)) {
    throw new SolrException(ErrorCode.BAD_REQUEST,
        "The configuration " + configSetName + " already exists in zookeeper");
  }

  Iterator<ContentStream> contentStreamsIterator = req.getContentStreams().iterator();

  if (!contentStreamsIterator.hasNext()) {
    throw new SolrException(ErrorCode.BAD_REQUEST,
        "No stream found for the config data to be uploaded");
  }

  InputStream inputStream = contentStreamsIterator.next().getStream();

  // Create a node for the configuration in zookeeper
  boolean trusted = getTrusted(req);
  zkClient.makePath(configPathInZk, ("{\"trusted\": " + Boolean.toString(trusted) + "}").
      getBytes(StandardCharsets.UTF_8), true);

  ZipInputStream zis = new ZipInputStream(inputStream, StandardCharsets.UTF_8);
  ZipEntry zipEntry = null;
  while ((zipEntry = zis.getNextEntry()) != null) {
    String filePathInZk = configPathInZk + "/" + zipEntry.getName();
    if (zipEntry.isDirectory()) {
      zkClient.makePath(filePathInZk, true);
    } else {
      createZkNodeIfNotExistsAndSetData(zkClient, filePathInZk,
          IOUtils.toByteArray(zis));
    }
  }
  zis.close();
}
 
Example 13
Source File: CdcrLeaderStateManager.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/**
 * Checks if the leader is registered. If it is not registered, we are probably at the
 * initialisation phase of the cluster. In this case, we must attach a watcher to
 * be notified when the leader is registered.
 */
private boolean isLeaderRegistered(SolrZkClient zkClient, ClusterState clusterState)
    throws KeeperException, InterruptedException {
  // First check if the znode exists, and register the watcher at the same time
  return zkClient.exists(this.getZnodePath(), watcher, true) != null;
}
 
Example 14
Source File: OverseerNodePrioritizer.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public synchronized void prioritizeOverseerNodes(String overseerId) throws Exception {
  SolrZkClient zk = zkStateReader.getZkClient();
  if(!zk.exists(ZkStateReader.ROLES,true))return;
  @SuppressWarnings({"rawtypes"})
  Map m = (Map) Utils.fromJSON(zk.getData(ZkStateReader.ROLES, null, new Stat(), true));

  @SuppressWarnings({"rawtypes"})
  List overseerDesignates = (List) m.get("overseer");
  if(overseerDesignates==null || overseerDesignates.isEmpty()) return;
  String ldr = OverseerTaskProcessor.getLeaderNode(zk);
  if(overseerDesignates.contains(ldr)) return;
  log.info("prioritizing overseer nodes at {} overseer designates are {}", overseerId, overseerDesignates);
  List<String> electionNodes = OverseerTaskProcessor.getSortedElectionNodes(zk, Overseer.OVERSEER_ELECT + LeaderElector.ELECTION_NODE);
  if(electionNodes.size()<2) return;
  log.info("sorted nodes {}", electionNodes);

  String designateNodeId = null;
  for (String electionNode : electionNodes) {
    if(overseerDesignates.contains( LeaderElector.getNodeName(electionNode))){
      designateNodeId = electionNode;
      break;
    }
  }

  if(designateNodeId == null){
    log.warn("No live overseer designate ");
    return;
  }
  if(!designateNodeId.equals( electionNodes.get(1))) { //checking if it is already at no:1
    log.info("asking node {} to come join election at head", designateNodeId);
    invokeOverseerOp(designateNodeId, "rejoinAtHead"); //ask designate to come first
    if (log.isInfoEnabled()) {
      log.info("asking the old first in line {} to rejoin election  ", electionNodes.get(1));
    }
    invokeOverseerOp(electionNodes.get(1), "rejoin");//ask second inline to go behind
  }
  //now ask the current leader to QUIT , so that the designate can takeover
  stateUpdateQueue.offer(
      Utils.toJSON(new ZkNodeProps(Overseer.QUEUE_OPERATION, OverseerAction.QUIT.toLower(),
          ID, OverseerTaskProcessor.getLeaderId(zkStateReader.getZkClient()))));

}
 
Example 15
Source File: RepositoryManager.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private String getRepositoriesJson(SolrZkClient zkClient) throws UnsupportedEncodingException, KeeperException, InterruptedException {
  if (zkClient.exists(PackageUtils.REPOSITORIES_ZK_PATH, true)) {
    return new String(zkClient.getData(PackageUtils.REPOSITORIES_ZK_PATH, null, null, true), "UTF-8");
  }
  return "[]";
}
 
Example 16
Source File: SolrSnapshotManager.java    From lucene-solr with Apache License 2.0 2 votes vote down vote up
/**
 * This method returns if a named snapshot exists for the specified collection.
 *
 * @param zkClient Zookeeper client
 * @param collectionName The name of the collection
 * @param commitName The name of the snapshot
 * @return true if the named snapshot exists
 *         false Otherwise
 * @throws KeeperException In case of Zookeeper error
 * @throws InterruptedException In case of thread interruption.
 */
public static boolean snapshotExists(SolrZkClient zkClient, String collectionName, String commitName)
    throws KeeperException, InterruptedException {
  String zkPath = getSnapshotMetaDataZkPath(collectionName, Optional.ofNullable(commitName));
  return zkClient.exists(zkPath, true);
}