Skip to content

Commit

Permalink
Improve RoutingNodes API
Browse files Browse the repository at this point in the history
Currently the RoutingNodes API allows modification of it's internal state outside of the class.
This commit improves the APIs of `RoutingNode` and `RoutingNode` to change internal state
only within the classes itself.

Closes #4458
  • Loading branch information
s1monw committed Dec 16, 2013
1 parent 6af80d5 commit 30c6f2f
Show file tree
Hide file tree
Showing 39 changed files with 845 additions and 710 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@
import org.elasticsearch.cluster.ClusterService;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.routing.MutableShardRouting;
import org.elasticsearch.cluster.routing.RoutingNode;
import org.elasticsearch.cluster.routing.ShardIterator;
import org.elasticsearch.cluster.routing.ShardRouting;
import org.elasticsearch.cluster.routing.*;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.engine.DocumentMissingException;
Expand Down Expand Up @@ -96,13 +93,13 @@ protected void doExecute(final MoreLikeThisRequest request, final ActionListener
// update to the concrete index
final String concreteIndex = clusterState.metaData().concreteIndex(request.index());

RoutingNode routingNode = clusterState.getRoutingNodes().nodesToShards().get(clusterService.localNode().getId());
Iterable<MutableShardRouting> routingNode = clusterState.getRoutingNodes().routingNodeIter(clusterService.localNode().getId());
if (routingNode == null) {
redirect(request, concreteIndex, listener, clusterState);
return;
}
boolean hasIndexLocally = false;
for (MutableShardRouting shardRouting : routingNode.shards()) {
for (MutableShardRouting shardRouting : routingNode) {
if (concreteIndex.equals(shardRouting.index())) {
hasIndexLocally = true;
break;
Expand Down
41 changes: 20 additions & 21 deletions src/main/java/org/elasticsearch/cluster/routing/RoutingNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@

package org.elasticsearch.cluster.routing;

import com.google.common.collect.Iterators;
import org.elasticsearch.ElasticSearchIllegalStateException;
import org.elasticsearch.cluster.node.DiscoveryNode;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

Expand Down Expand Up @@ -52,6 +54,10 @@ public RoutingNode(String nodeId, DiscoveryNode node, List<MutableShardRouting>

@Override
public Iterator<MutableShardRouting> iterator() {
return Iterators.unmodifiableIterator(shards.iterator());
}

Iterator<MutableShardRouting> mutableIterator() {
return shards.iterator();
}

Expand All @@ -72,12 +78,8 @@ public String nodeId() {
return this.nodeId;
}

/**
* Get a list of shards hosted on this node
* @return list of shards
*/
public List<MutableShardRouting> shards() {
return this.shards;
public int size() {
return shards.size();
}

/**
Expand Down Expand Up @@ -150,21 +152,6 @@ public List<MutableShardRouting> shardsWithState(String index, ShardRoutingState
return shards;
}

/**
* Get the number of shard that not match the given states
* @param state set states to exclude
* @return number of shards which state is listed
*/
public int numberOfShardsNotWithState(ShardRoutingState state) {
int count = 0;
for (MutableShardRouting shardEntry : this) {
if (shardEntry.state() != state) {
count++;
}
}
return count;
}

/**
* The number of shards on this node that will not be eventually relocated.
*/
Expand All @@ -187,4 +174,16 @@ public String prettyPrint() {
}
return sb.toString();
}

public MutableShardRouting get(int i) {
return shards.get(i) ;
}

public Collection<MutableShardRouting> copyShards() {
return new ArrayList<MutableShardRouting>(shards);
}

public boolean isEmpty() {
return shards.isEmpty();
}
}

0 comments on commit 30c6f2f

Please sign in to comment.