org.elasticsearch.cluster.routing.RoutingNodes Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of elasticsearch Show documentation
Show all versions of elasticsearch Show documentation
Elasticsearch subproject :server
/*
* Licensed to Elastic Search and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Elastic Search licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.cluster.routing;
import org.elasticsearch.cluster.block.ClusterBlocks;
import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.common.util.concurrent.NotThreadSafe;
import java.util.*;
import static org.elasticsearch.common.collect.Lists.*;
import static org.elasticsearch.common.collect.Maps.*;
/**
* @author kimchy (shay.banon)
*/
@NotThreadSafe
public class RoutingNodes implements Iterable {
private final MetaData metaData;
private final ClusterBlocks blocks;
private final RoutingTable routingTable;
private final Map nodesToShards = newHashMap();
private final List unassigned = newArrayList();
private final List ignoredUnassigned = newArrayList();
public RoutingNodes(MetaData metaData, ClusterBlocks blocks, RoutingTable routingTable) {
this.metaData = metaData;
this.blocks = blocks;
this.routingTable = routingTable;
Map> nodesToShards = newHashMap();
for (IndexRoutingTable indexRoutingTable : routingTable.indicesRouting().values()) {
for (IndexShardRoutingTable indexShard : indexRoutingTable) {
for (ShardRouting shard : indexShard) {
if (shard.assignedToNode()) {
List entries = nodesToShards.get(shard.currentNodeId());
if (entries == null) {
entries = newArrayList();
nodesToShards.put(shard.currentNodeId(), entries);
}
entries.add(new MutableShardRouting(shard));
if (shard.relocating()) {
entries = nodesToShards.get(shard.relocatingNodeId());
if (entries == null) {
entries = newArrayList();
nodesToShards.put(shard.relocatingNodeId(), entries);
}
// add the counterpart shard with relocatingNodeId reflecting the source from which
// it's relocating from.
entries.add(new MutableShardRouting(shard.index(), shard.id(), shard.relocatingNodeId(),
shard.currentNodeId(), shard.primary(), ShardRoutingState.INITIALIZING));
}
} else {
unassigned.add(new MutableShardRouting(shard));
}
}
}
}
for (Map.Entry> entry : nodesToShards.entrySet()) {
String nodeId = entry.getKey();
this.nodesToShards.put(nodeId, new RoutingNode(nodeId, entry.getValue()));
}
}
@Override public Iterator iterator() {
return nodesToShards.values().iterator();
}
public RoutingTable routingTable() {
return routingTable;
}
public RoutingTable getRoutingTable() {
return routingTable();
}
public MetaData metaData() {
return this.metaData;
}
public MetaData getMetaData() {
return metaData();
}
public ClusterBlocks blocks() {
return this.blocks;
}
public ClusterBlocks getBlocks() {
return this.blocks;
}
public int requiredAverageNumberOfShardsPerNode() {
return metaData.totalNumberOfShards() / nodesToShards.size();
}
public boolean hasUnassigned() {
return !unassigned.isEmpty();
}
public List ignoredUnassigned() {
return this.ignoredUnassigned;
}
public List unassigned() {
return this.unassigned;
}
public List getUnassigned() {
return unassigned();
}
public Map nodesToShards() {
return nodesToShards;
}
public Map getNodesToShards() {
return nodesToShards();
}
public RoutingNode node(String nodeId) {
return nodesToShards.get(nodeId);
}
public MutableShardRouting findPrimaryForReplica(ShardRouting shard) {
assert !shard.primary();
for (RoutingNode routingNode : nodesToShards.values()) {
for (MutableShardRouting shardRouting : routingNode) {
if (shardRouting.shardId().equals(shard.shardId()) && shardRouting.primary()) {
return shardRouting;
}
}
}
return null;
}
public List shardsRoutingFor(ShardRouting shardRouting) {
return shardsRoutingFor(shardRouting.index(), shardRouting.id());
}
public List shardsRoutingFor(String index, int shardId) {
List shards = newArrayList();
for (RoutingNode routingNode : this) {
for (MutableShardRouting shardRouting : routingNode) {
if (shardRouting.index().equals(index) && shardRouting.id() == shardId) {
shards.add(shardRouting);
}
}
}
for (MutableShardRouting shardRouting : unassigned) {
if (shardRouting.index().equals(index) && shardRouting.id() == shardId) {
shards.add(shardRouting);
}
}
return shards;
}
public int numberOfShardsOfType(ShardRoutingState state) {
int count = 0;
for (RoutingNode routingNode : this) {
count += routingNode.numberOfShardsWithState(state);
}
return count;
}
public List shardsWithState(ShardRoutingState... state) {
List shards = newArrayList();
for (RoutingNode routingNode : this) {
shards.addAll(routingNode.shardsWithState(state));
}
return shards;
}
public List sortedNodesLeastToHigh() {
return nodesToShardsSorted(new Comparator() {
@Override public int compare(RoutingNode o1, RoutingNode o2) {
return o1.shards().size() - o2.shards().size();
}
});
}
public List nodesToShardsSorted(Comparator comparator) {
List nodes = new ArrayList(nodesToShards.values());
if (comparator != null) {
Collections.sort(nodes, comparator);
}
return nodes;
}
public String prettyPrint() {
StringBuilder sb = new StringBuilder("routing_nodes:\n");
for (RoutingNode routingNode : this) {
sb.append(routingNode.prettyPrint());
}
sb.append("---- unassigned\n");
for (MutableShardRouting shardEntry : unassigned) {
sb.append("--------").append(shardEntry.shortSummary()).append('\n');
}
return sb.toString();
}
}