Java Code Examples for org.apache.solr.common.cloud.ZkNodeProps#getBool()

The following examples show how to use org.apache.solr.common.cloud.ZkNodeProps#getBool() . 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: Assign.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public static List<String> getLiveOrLiveAndCreateNodeSetList(final Set<String> liveNodes, final ZkNodeProps message, final Random random) {
  List<String> nodeList;
  final String createNodeSetStr = message.getStr(CREATE_NODE_SET);
  final List<String> createNodeList = (createNodeSetStr == null) ? null :
      StrUtils.splitSmart((OverseerCollectionMessageHandler.CREATE_NODE_SET_EMPTY.equals(createNodeSetStr) ?
          "" : createNodeSetStr), ",", true);

  if (createNodeList != null) {
    nodeList = new ArrayList<>(createNodeList);
    nodeList.retainAll(liveNodes);
    if (message.getBool(OverseerCollectionMessageHandler.CREATE_NODE_SET_SHUFFLE,
        OverseerCollectionMessageHandler.CREATE_NODE_SET_SHUFFLE_DEFAULT)) {
      Collections.shuffle(nodeList, random);
    }
  } else {
    nodeList = new ArrayList<>(liveNodes);
    Collections.shuffle(nodeList, random);
  }

  return nodeList;
}
 
Example 2
Source File: RenameCmd.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public void call(ClusterState state, ZkNodeProps message, @SuppressWarnings({"rawtypes"})NamedList results) throws Exception {
  String extCollectionName = message.getStr(CoreAdminParams.NAME);
  String target = message.getStr(CollectionAdminParams.TARGET);

  if (ocmh.zkStateReader.aliasesManager != null) { // not a mock ZkStateReader
    ocmh.zkStateReader.aliasesManager.update();
  }

  if (extCollectionName == null || target == null) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "both collection 'name' and 'target' name must be specified");
  }
  Aliases aliases = ocmh.zkStateReader.getAliases();

  boolean followAliases = message.getBool(FOLLOW_ALIASES, false);
  String collectionName;
  if (followAliases) {
    collectionName = aliases.resolveSimpleAlias(extCollectionName);
  } else {
    collectionName = extCollectionName;
  }
  if (!state.hasCollection(collectionName)) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "source collection '" + collectionName + "' not found.");
  }
  if (ocmh.zkStateReader.getAliases().hasAlias(target)) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "target alias '" + target + "' exists: "
        + ocmh.zkStateReader.getAliases().getCollectionAliasListMap().get(target));
  }

  ocmh.zkStateReader.aliasesManager.applyModificationAndExportToZk(a -> a.cloneWithRename(extCollectionName, target));
}
 
Example 3
Source File: DeleteReplicaCmd.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
void deleteReplica(ClusterState clusterState, ZkNodeProps message, @SuppressWarnings({"rawtypes"})NamedList results, Runnable onComplete)
        throws KeeperException, InterruptedException {
  if (log.isDebugEnabled()) {
    log.debug("deleteReplica() : {}", Utils.toJSONString(message));
  }
  boolean parallel = message.getBool("parallel", false);

  //If a count is specified the strategy needs be different
  if (message.getStr(COUNT_PROP) != null) {
    deleteReplicaBasedOnCount(clusterState, message, results, onComplete, parallel);
    return;
  }


  ocmh.checkRequired(message, COLLECTION_PROP, SHARD_ID_PROP, REPLICA_PROP);
  String extCollectionName = message.getStr(COLLECTION_PROP);
  String shard = message.getStr(SHARD_ID_PROP);
  String replicaName = message.getStr(REPLICA_PROP);

  boolean followAliases = message.getBool(FOLLOW_ALIASES, false);
  String collectionName;
  if (followAliases) {
    collectionName = ocmh.cloudManager.getClusterStateProvider().resolveSimpleAlias(extCollectionName);
  } else {
    collectionName = extCollectionName;
  }

  DocCollection coll = clusterState.getCollection(collectionName);
  Slice slice = coll.getSlice(shard);
  if (slice == null) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
            "Invalid shard name : " +  shard + " in collection : " +  collectionName);
  }

  deleteCore(slice, collectionName, replicaName, message, shard, results, onComplete,  parallel);

}
 
Example 4
Source File: AddReplicaCmd.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public static CreateReplica assignReplicaDetails(SolrCloudManager cloudManager, ClusterState clusterState,
                                               ZkNodeProps message, ReplicaPosition replicaPosition) {
  boolean skipCreateReplicaInClusterState = message.getBool(SKIP_CREATE_REPLICA_IN_CLUSTER_STATE, false);

  String collection = message.getStr(COLLECTION_PROP);
  String node = replicaPosition.node;
  String shard = message.getStr(SHARD_ID_PROP);
  String coreName = message.getStr(CoreAdminParams.NAME);
  String coreNodeName = message.getStr(CoreAdminParams.CORE_NODE_NAME);
  Replica.Type replicaType = replicaPosition.type;

  if (StringUtils.isBlank(coreName)) {
    coreName = message.getStr(CoreAdminParams.PROPERTY_PREFIX + CoreAdminParams.NAME);
  }

  log.info("Node Identified {} for creating new replica of shard {} for collection {}", node, shard, collection);
  if (!clusterState.liveNodesContain(node)) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Node: " + node + " is not live");
  }
  DocCollection coll = clusterState.getCollection(collection);
  if (coreName == null) {
    coreName = Assign.buildSolrCoreName(cloudManager.getDistribStateManager(), coll, shard, replicaType);
  } else if (!skipCreateReplicaInClusterState) {
    //Validate that the core name is unique in that collection
    for (Slice slice : coll.getSlices()) {
      for (Replica replica : slice.getReplicas()) {
        String replicaCoreName = replica.getStr(CORE_NAME_PROP);
        if (coreName.equals(replicaCoreName)) {
          throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Another replica with the same core name already exists" +
              " for this collection");
        }
      }
    }
  }
  log.info("Returning CreateReplica command.");
  return new CreateReplica(collection, shard, node, replicaType, coreName, coreNodeName);
}
 
Example 5
Source File: BackupCmd.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public void call(ClusterState state, ZkNodeProps message, @SuppressWarnings({"rawtypes"})NamedList results) throws Exception {
  String extCollectionName = message.getStr(COLLECTION_PROP);
  boolean followAliases = message.getBool(FOLLOW_ALIASES, false);
  String collectionName;
  if (followAliases) {
    collectionName = ocmh.cloudManager.getClusterStateProvider().resolveSimpleAlias(extCollectionName);
  } else {
    collectionName = extCollectionName;
  }
  String backupName = message.getStr(NAME);
  String repo = message.getStr(CoreAdminParams.BACKUP_REPOSITORY);

  Instant startTime = Instant.now();

  CoreContainer cc = ocmh.overseer.getCoreContainer();
  BackupRepository repository = cc.newBackupRepository(Optional.ofNullable(repo));
  BackupManager backupMgr = new BackupManager(repository, ocmh.zkStateReader);

  // Backup location
  URI location = repository.createURI(message.getStr(CoreAdminParams.BACKUP_LOCATION));
  URI backupPath = repository.resolve(location, backupName);

  //Validating if the directory already exists.
  if (repository.exists(backupPath)) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "The backup directory already exists: " + backupPath);
  }

  // Create a directory to store backup details.
  repository.createDirectory(backupPath);

  String strategy = message.getStr(CollectionAdminParams.INDEX_BACKUP_STRATEGY, CollectionAdminParams.COPY_FILES_STRATEGY);
  switch (strategy) {
    case CollectionAdminParams.COPY_FILES_STRATEGY: {
      copyIndexFiles(backupPath, collectionName, message, results);
      break;
    }
    case CollectionAdminParams.NO_INDEX_BACKUP_STRATEGY: {
      break;
    }
  }

  log.info("Starting to backup ZK data for backupName={}", backupName);

  //Download the configs
  String configName = ocmh.zkStateReader.readConfigName(collectionName);
  backupMgr.downloadConfigDir(location, backupName, configName);

  //Save the collection's state (coming from the collection's state.json)
  //We extract the state and back it up as a separate json
  DocCollection collectionState = ocmh.zkStateReader.getClusterState().getCollection(collectionName);
  backupMgr.writeCollectionState(location, backupName, collectionName, collectionState);

  Properties properties = new Properties();

  properties.put(BackupManager.BACKUP_NAME_PROP, backupName);
  properties.put(BackupManager.COLLECTION_NAME_PROP, collectionName);
  properties.put(BackupManager.COLLECTION_ALIAS_PROP, extCollectionName);
  properties.put(CollectionAdminParams.COLL_CONF, configName);
  properties.put(BackupManager.START_TIME_PROP, startTime.toString());
  properties.put(BackupManager.INDEX_VERSION_PROP, Version.LATEST.toString());
  //TODO: Add MD5 of the configset. If during restore the same name configset exists then we can compare checksums to see if they are the same.
  //if they are not the same then we can throw an error or have an 'overwriteConfig' flag
  //TODO save numDocs for the shardLeader. We can use it to sanity check the restore.

  backupMgr.writeBackupProperties(location, backupName, properties);

  backupMgr.downloadCollectionProperties(location, backupName, collectionName);

  log.info("Completed backing up ZK data for backupName={}", backupName);
}
 
Example 6
Source File: MigrateCmd.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public void call(ClusterState clusterState, ZkNodeProps message, @SuppressWarnings({"rawtypes"})NamedList results) throws Exception {
  String extSourceCollectionName = message.getStr("collection");
  String splitKey = message.getStr("split.key");
  String extTargetCollectionName = message.getStr("target.collection");
  int timeout = message.getInt("forward.timeout", 10 * 60) * 1000;

  boolean followAliases = message.getBool(FOLLOW_ALIASES, false);
  String sourceCollectionName;
  String targetCollectionName;
  if (followAliases) {
    sourceCollectionName = ocmh.cloudManager.getClusterStateProvider().resolveSimpleAlias(extSourceCollectionName);
    targetCollectionName = ocmh.cloudManager.getClusterStateProvider().resolveSimpleAlias(extTargetCollectionName);
  } else {
    sourceCollectionName = extSourceCollectionName;
    targetCollectionName = extTargetCollectionName;
  }

  DocCollection sourceCollection = clusterState.getCollection(sourceCollectionName);
  if (sourceCollection == null) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Unknown source collection: " + sourceCollectionName);
  }
  DocCollection targetCollection = clusterState.getCollection(targetCollectionName);
  if (targetCollection == null) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Unknown target collection: " + sourceCollectionName);
  }
  if (!(sourceCollection.getRouter() instanceof CompositeIdRouter)) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Source collection must use a compositeId router");
  }
  if (!(targetCollection.getRouter() instanceof CompositeIdRouter)) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Target collection must use a compositeId router");
  }

  if (splitKey == null || splitKey.trim().length() == 0)  {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "The split.key cannot be null or empty");
  }

  CompositeIdRouter sourceRouter = (CompositeIdRouter) sourceCollection.getRouter();
  CompositeIdRouter targetRouter = (CompositeIdRouter) targetCollection.getRouter();
  Collection<Slice> sourceSlices = sourceRouter.getSearchSlicesSingle(splitKey, null, sourceCollection);
  if (sourceSlices.isEmpty()) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
        "No active slices available in source collection: " + sourceCollection + "for given split.key: " + splitKey);
  }
  Collection<Slice> targetSlices = targetRouter.getSearchSlicesSingle(splitKey, null, targetCollection);
  if (targetSlices.isEmpty()) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
        "No active slices available in target collection: " + targetCollection + "for given split.key: " + splitKey);
  }

  String asyncId = null;
  if (message.containsKey(ASYNC) && message.get(ASYNC) != null)
    asyncId = message.getStr(ASYNC);

  for (Slice sourceSlice : sourceSlices) {
    for (Slice targetSlice : targetSlices) {
      log.info("Migrating source shard: {} to target shard: {} for split.key = {}", sourceSlice, targetSlice, splitKey);
      migrateKey(clusterState, sourceCollection, sourceSlice, targetCollection, targetSlice, splitKey,
          timeout, results, asyncId, message);
    }
  }
}