public RoutingNodes(ClusterState clusterState, boolean readOnly) {
    this.readOnly = readOnly;
    this.metaData = clusterState.metaData();
    this.blocks = clusterState.blocks();
    this.routingTable = clusterState.routingTable();
    this.customs = clusterState.customs();

    Map<String, List<ShardRouting>> nodesToShards = new HashMap<>();
    // fill in the nodeToShards with the "live" nodes
    for (ObjectCursor<DiscoveryNode> cursor : clusterState.nodes().dataNodes().values()) {
      nodesToShards.put(cursor.value.id(), new ArrayList<ShardRouting>());
    }

    // fill in the inverse of node -> shards allocated
    // also fill replicaSet information
    for (IndexRoutingTable indexRoutingTable : routingTable.indicesRouting().values()) {
      for (IndexShardRoutingTable indexShard : indexRoutingTable) {
        for (ShardRouting shard : indexShard) {
          // to get all the shards belonging to an index, including the replicas,
          // we define a replica set and keep track of it. A replica set is identified
          // by the ShardId, as this is common for primary and replicas.
          // A replica Set might have one (and not more) replicas with the state of RELOCATING.
          if (shard.assignedToNode()) {
            List<ShardRouting> entries = nodesToShards.get(shard.currentNodeId());
            if (entries == null) {
              entries = new ArrayList<>();
              nodesToShards.put(shard.currentNodeId(), entries);
            }
            final ShardRouting sr = getRouting(shard, readOnly);
            entries.add(sr);
            assignedShardsAdd(sr);
            if (shard.relocating()) {
              entries = nodesToShards.get(shard.relocatingNodeId());
              relocatingShards++;
              if (entries == null) {
                entries = new ArrayList<>();
                nodesToShards.put(shard.relocatingNodeId(), entries);
              }
              // add the counterpart shard with relocatingNodeId reflecting the source from which
              // it's relocating from.
              ShardRouting targetShardRouting = shard.buildTargetRelocatingShard();
              if (readOnly) {
                targetShardRouting.freeze();
              }
              entries.add(targetShardRouting);
              assignedShardsAdd(targetShardRouting);
            } else if (!shard.active()) { // shards that are initializing without being relocated
              if (shard.primary()) {
                inactivePrimaryCount++;
              }
              inactiveShardCount++;
            }
          } else {
            final ShardRouting sr = getRouting(shard, readOnly);
            assignedShardsAdd(sr);
            unassignedShards.add(sr);
          }
        }
      }
    }
    for (Map.Entry<String, List<ShardRouting>> entry : nodesToShards.entrySet()) {
      String nodeId = entry.getKey();
      this.nodesToShards.put(
          nodeId, new RoutingNode(nodeId, clusterState.nodes().get(nodeId), entry.getValue()));
    }
  }
 public boolean hasUnassignedShards() {
   return !unassignedShards.isEmpty();
 }
Exemple #3
0
 /**
  * Returns <code>true</code> iff this {@link RoutingNodes} instance has any unassigned shards even
  * if the shards are marked as temporarily ignored.
  *
  * @see UnassignedShards#isEmpty()
  * @see UnassignedShards#isIgnoredEmpty()
  */
 public boolean hasUnassignedShards() {
   return unassignedShards.isEmpty() == false || unassignedShards.isIgnoredEmpty() == false;
 }
 public boolean hasUnassignedPrimaries() {
   return unassignedShards.numPrimaries() > 0;
 }
Exemple #5
0
 /**
  * Returns <code>true</code> iff this {@link RoutingNodes} instance has any unassigned primaries
  * even if the primaries are marked as temporarily ignored.
  */
 public boolean hasUnassignedPrimaries() {
   return unassignedShards.getNumPrimaries() + unassignedShards.getNumIgnoredPrimaries() > 0;
 }