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

The following examples show how to use org.apache.solr.common.cloud.ZkNodeProps#getInt() . 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: CreateCollectionCmd.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public static List<String> populateShardNames(ZkNodeProps message, String router) {
  List<String> shardNames = new ArrayList<>();
  Integer numSlices = message.getInt(OverseerCollectionMessageHandler.NUM_SLICES, null);
  if (ImplicitDocRouter.NAME.equals(router)) {
    ClusterStateMutator.getShardNames(shardNames, message.getStr("shards", null));
    numSlices = shardNames.size();
  } else {
    if (numSlices == null) {
      throw new SolrException(ErrorCode.BAD_REQUEST, OverseerCollectionMessageHandler.NUM_SLICES + " is a required param (when using CompositeId router).");
    }
    if (numSlices <= 0) {
      throw new SolrException(ErrorCode.BAD_REQUEST, OverseerCollectionMessageHandler.NUM_SLICES + " must be > 0");
    }
    ClusterStateMutator.getShardNames(numSlices, shardNames);
  }
  return shardNames;
}
 
Example 2
Source File: ReplicaMutator.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
protected ZkWriteCommand updateState(final ClusterState prevState, ZkNodeProps message) {
  final String cName = message.getStr(ZkStateReader.COLLECTION_PROP);
  if (!checkCollectionKeyExistence(message)) return ZkStateWriter.NO_OP;
  Integer numShards = message.getInt(ZkStateReader.NUM_SHARDS_PROP, null);
  log.debug("Update state numShards={} message={}", numShards, message);

  List<String> shardNames = new ArrayList<>();

  ZkWriteCommand writeCommand = null;
  ClusterState newState = null;

  //collection does not yet exist, create placeholders if num shards is specified
  boolean collectionExists = prevState.hasCollection(cName);
  if (!collectionExists && numShards != null) {
    ClusterStateMutator.getShardNames(numShards, shardNames);
    Map<String, Object> createMsg = Utils.makeMap(NAME, cName);
    createMsg.putAll(message.getProperties());
    writeCommand = new ClusterStateMutator(cloudManager).createCollection(prevState, new ZkNodeProps(createMsg));
    DocCollection collection = writeCommand.collection;
    newState = ClusterStateMutator.newState(prevState, cName, collection);
  }
  return updateState(newState != null ? newState : prevState,
      message, cName, numShards, collectionExists);
}
 
Example 3
Source File: CreateCollectionCmd.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public static void checkReplicaTypes(ZkNodeProps message) {
  int numTlogReplicas = message.getInt(TLOG_REPLICAS, 0);
  int numNrtReplicas = message.getInt(NRT_REPLICAS, message.getInt(REPLICATION_FACTOR, numTlogReplicas > 0 ? 0 : 1));

  if (numNrtReplicas + numTlogReplicas <= 0) {
    throw new SolrException(ErrorCode.BAD_REQUEST, NRT_REPLICAS + " + " + TLOG_REPLICAS + " must be greater than 0");
  }
}
 
Example 4
Source File: RestoreCmd.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private int getInt(ZkNodeProps message, String propertyName, Integer count, int defaultValue) {
  Integer value = message.getInt(propertyName, count);
  return value!=null ? value:defaultValue;
}
 
Example 5
Source File: CreateCollectionCmd.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public static List<ReplicaPosition> buildReplicaPositions(SolrCloudManager cloudManager, ClusterState clusterState,
                                                          DocCollection docCollection,
                                                          ZkNodeProps message,
                                                          List<String> shardNames,
                                                          AtomicReference<PolicyHelper.SessionWrapper> sessionWrapper) throws IOException, InterruptedException, Assign.AssignmentException {
  final String collectionName = message.getStr(NAME);
  // look at the replication factor and see if it matches reality
  // if it does not, find best nodes to create more cores
  int numTlogReplicas = message.getInt(TLOG_REPLICAS, 0);
  int numNrtReplicas = message.getInt(NRT_REPLICAS, message.getInt(REPLICATION_FACTOR, numTlogReplicas>0?0:1));
  int numPullReplicas = message.getInt(PULL_REPLICAS, 0);

  int numSlices = shardNames.size();
  int maxShardsPerNode = message.getInt(MAX_SHARDS_PER_NODE, 1);
  if (maxShardsPerNode == -1) maxShardsPerNode = Integer.MAX_VALUE;

  // we need to look at every node and see how many cores it serves
  // add our new cores to existing nodes serving the least number of cores
  // but (for now) require that each core goes on a distinct node.

  List<ReplicaPosition> replicaPositions;
  List<String> nodeList = Assign.getLiveOrLiveAndCreateNodeSetList(clusterState.getLiveNodes(), message, OverseerCollectionMessageHandler.RANDOM);
  if (nodeList.isEmpty()) {
    log.warn("It is unusual to create a collection ({}) without cores.", collectionName);

    replicaPositions = new ArrayList<>();
  } else {
    int totalNumReplicas = numNrtReplicas + numTlogReplicas + numPullReplicas;
    if (totalNumReplicas > nodeList.size()) {
      log.warn("Specified number of replicas of {} on collection {} is higher than the number of Solr instances currently live or live and part of your {}({}). {}"
          , totalNumReplicas
          , collectionName
          , OverseerCollectionMessageHandler.CREATE_NODE_SET
          , nodeList.size()
          , "It's unusual to run two replica of the same slice on the same Solr-instance.");
    }

    int maxShardsAllowedToCreate = maxShardsPerNode == Integer.MAX_VALUE ?
        Integer.MAX_VALUE :
        maxShardsPerNode * nodeList.size();
    int requestedShardsToCreate = numSlices * totalNumReplicas;
    if (maxShardsAllowedToCreate < requestedShardsToCreate) {
      throw new Assign.AssignmentException("Cannot create collection " + collectionName + ". Value of "
          + MAX_SHARDS_PER_NODE + " is " + maxShardsPerNode
          + ", and the number of nodes currently live or live and part of your "+OverseerCollectionMessageHandler.CREATE_NODE_SET+" is " + nodeList.size()
          + ". This allows a maximum of " + maxShardsAllowedToCreate
          + " to be created. Value of " + OverseerCollectionMessageHandler.NUM_SLICES + " is " + numSlices
          + ", value of " + NRT_REPLICAS + " is " + numNrtReplicas
          + ", value of " + TLOG_REPLICAS + " is " + numTlogReplicas
          + " and value of " + PULL_REPLICAS + " is " + numPullReplicas
          + ". This requires " + requestedShardsToCreate
          + " shards to be created (higher than the allowed number)");
    }
    Assign.AssignRequest assignRequest = new Assign.AssignRequestBuilder()
        .forCollection(collectionName)
        .forShard(shardNames)
        .assignNrtReplicas(numNrtReplicas)
        .assignTlogReplicas(numTlogReplicas)
        .assignPullReplicas(numPullReplicas)
        .onNodes(nodeList)
        .build();
    Assign.AssignStrategyFactory assignStrategyFactory = new Assign.AssignStrategyFactory(cloudManager);
    Assign.AssignStrategy assignStrategy = assignStrategyFactory.create(clusterState, docCollection);
    replicaPositions = assignStrategy.assign(cloudManager, assignRequest);
    sessionWrapper.set(PolicyHelper.getLastSessionWrapper(true));
  }
  return replicaPositions;
}
 
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);
    }
  }
}
 
Example 7
Source File: ClusterStateMutator.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({"unchecked"})
public ZkWriteCommand createCollection(ClusterState clusterState, ZkNodeProps message) {
  String cName = message.getStr(NAME);
  log.debug("building a new cName: {}", cName);
  if (clusterState.hasCollection(cName)) {
    log.warn("Collection {} already exists. exit", cName);
    return ZkStateWriter.NO_OP;
  }

  Map<String, Object> routerSpec = DocRouter.getRouterSpec(message);
  String routerName = routerSpec.get(NAME) == null ? DocRouter.DEFAULT_NAME : (String) routerSpec.get(NAME);
  DocRouter router = DocRouter.getDocRouter(routerName);

  Object messageShardsObj = message.get("shards");

  Map<String, Slice> slices;
  if (messageShardsObj instanceof Map) { // we are being explicitly told the slice data (e.g. coll restore)
    slices = Slice.loadAllFromMap(cName, (Map<String, Object>)messageShardsObj);
  } else {
    List<String> shardNames = new ArrayList<>();

    if (router instanceof ImplicitDocRouter) {
      getShardNames(shardNames, message.getStr("shards", DocRouter.DEFAULT_NAME));
    } else {
      int numShards = message.getInt(ZkStateReader.NUM_SHARDS_PROP, -1);
      if (numShards < 1)
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "numShards is a required parameter for 'compositeId' router");
      getShardNames(numShards, shardNames);
    }
    List<DocRouter.Range> ranges = router.partitionRange(shardNames.size(), router.fullRange());//maybe null

    slices = new LinkedHashMap<>();
    for (int i = 0; i < shardNames.size(); i++) {
      String sliceName = shardNames.get(i);

      Map<String, Object> sliceProps = new LinkedHashMap<>(1);
      sliceProps.put(Slice.RANGE, ranges == null ? null : ranges.get(i));

      slices.put(sliceName, new Slice(sliceName, null, sliceProps,cName));
    }
  }

  Map<String, Object> collectionProps = new HashMap<>();

  for (Map.Entry<String, Object> e : OverseerCollectionMessageHandler.COLLECTION_PROPS_AND_DEFAULTS.entrySet()) {
    Object val = message.get(e.getKey());
    if (val == null) {
      val = OverseerCollectionMessageHandler.COLLECTION_PROPS_AND_DEFAULTS.get(e.getKey());
    }
    if (val != null) collectionProps.put(e.getKey(), val);
  }
  collectionProps.put(DocCollection.DOC_ROUTER, routerSpec);

  if (message.getStr("fromApi") == null) {
    collectionProps.put("autoCreated", "true");
  }

  DocCollection newCollection = new DocCollection(cName, slices, collectionProps, router, -1);

  return new ZkWriteCommand(cName, newCollection);
}