コード例 #1
0
  private ShardIterator preferenceActiveShardIterator(
      IndexShardRoutingTable indexShard,
      String localNodeId,
      DiscoveryNodes nodes,
      @Nullable String preference) {
    if (preference == null || preference.isEmpty()) {
      String[] awarenessAttributes = awarenessAllocationDecider.awarenessAttributes();
      if (awarenessAttributes.length == 0) {
        return indexShard.activeInitializingShardsRandomIt();
      } else {
        return indexShard.preferAttributesActiveInitializingShardsIt(awarenessAttributes, nodes);
      }
    }
    if (preference.charAt(0) == '_') {
      Preference preferenceType = Preference.parse(preference);
      if (preferenceType == Preference.SHARDS) {
        // starts with _shards, so execute on specific ones
        int index = preference.indexOf(';');

        String shards;
        if (index == -1) {
          shards = preference.substring(Preference.SHARDS.type().length() + 1);
        } else {
          shards = preference.substring(Preference.SHARDS.type().length() + 1, index);
        }
        String[] ids = Strings.splitStringByCommaToArray(shards);
        boolean found = false;
        for (String id : ids) {
          if (Integer.parseInt(id) == indexShard.shardId().id()) {
            found = true;
            break;
          }
        }
        if (!found) {
          return null;
        }
        // no more preference
        if (index == -1 || index == preference.length() - 1) {
          String[] awarenessAttributes = awarenessAllocationDecider.awarenessAttributes();
          if (awarenessAttributes.length == 0) {
            return indexShard.activeInitializingShardsRandomIt();
          } else {
            return indexShard.preferAttributesActiveInitializingShardsIt(
                awarenessAttributes, nodes);
          }
        } else {
          // update the preference and continue
          preference = preference.substring(index + 1);
        }
      }
      preferenceType = Preference.parse(preference);
      switch (preferenceType) {
        case PREFER_NODE:
          return indexShard.preferNodeActiveInitializingShardsIt(
              preference.substring(Preference.PREFER_NODE.type().length() + 1));
        case LOCAL:
          return indexShard.preferNodeActiveInitializingShardsIt(localNodeId);
        case PRIMARY:
          return indexShard.primaryActiveInitializingShardIt();
        case PRIMARY_FIRST:
          return indexShard.primaryFirstActiveInitializingShardsIt();
        case ONLY_LOCAL:
          return indexShard.onlyNodeActiveInitializingShardsIt(localNodeId);
        case ONLY_NODE:
          String nodeId = preference.substring(Preference.ONLY_NODE.type().length() + 1);
          ensureNodeIdExists(nodes, nodeId);
          return indexShard.onlyNodeActiveInitializingShardsIt(nodeId);
        case ONLY_NODES:
          String nodeAttribute = preference.substring(Preference.ONLY_NODES.type().length() + 1);
          return indexShard.onlyNodeSelectorActiveInitializingShardsIt(nodeAttribute, nodes);
        default:
          throw new IllegalArgumentException("unknown preference [" + preferenceType + "]");
      }
    }
    // if not, then use it as the index
    String[] awarenessAttributes = awarenessAllocationDecider.awarenessAttributes();
    if (awarenessAttributes.length == 0) {
      return indexShard.activeInitializingShardsIt(DjbHashFunction.DJB_HASH(preference));
    } else {
      return indexShard.preferAttributesActiveInitializingShardsIt(
          awarenessAttributes, nodes, DjbHashFunction.DJB_HASH(preference));
    }
  }