Skip to content

Commit

Permalink
Optimizes performance of AllocationDecider execution. Instead of usin…
Browse files Browse the repository at this point in the history
…g loops over all ShardRoutings, do accounting in RoutingNodes.

Speeds up recalculating cluster state on large clusters.
  • Loading branch information
geidies authored and s1monw committed Dec 16, 2013
1 parent 6a856c8 commit 6af80d5
Show file tree
Hide file tree
Showing 16 changed files with 973 additions and 150 deletions.
Expand Up @@ -55,8 +55,9 @@ public MutableShardRouting(String index, int shardId, String currentNodeId,
*
* @param nodeId id of the node to assign this shard to
*/
public void assignToNode(String nodeId) {
void assignToNode(String nodeId) {
version++;

if (currentNodeId == null) {
assert state == ShardRoutingState.UNASSIGNED;

Expand All @@ -76,7 +77,7 @@ public void assignToNode(String nodeId) {
*
* @param relocatingNodeId id of the node to relocate the shard
*/
public void relocate(String relocatingNodeId) {
void relocate(String relocatingNodeId) {
version++;
assert state == ShardRoutingState.STARTED;
state = ShardRoutingState.RELOCATING;
Expand All @@ -87,7 +88,7 @@ public void relocate(String relocatingNodeId) {
* Cancel relocation of a shard. The shards state must be set
* to <code>RELOCATING</code>.
*/
public void cancelRelocation() {
void cancelRelocation() {
version++;
assert state == ShardRoutingState.RELOCATING;
assert assignedToNode();
Expand All @@ -101,7 +102,7 @@ public void cancelRelocation() {
* Set the shards state to <code>UNASSIGNED</code>.
* //TODO document the state
*/
public void deassignNode() {
void deassignNode() {
version++;
assert state != ShardRoutingState.UNASSIGNED;

Expand All @@ -115,7 +116,7 @@ public void deassignNode() {
* <code>INITIALIZING</code> or <code>RELOCATING</code>. Any relocation will be
* canceled.
*/
public void moveToStarted() {
void moveToStarted() {
version++;
assert state == ShardRoutingState.INITIALIZING || state == ShardRoutingState.RELOCATING;
relocatingNodeId = null;
Expand All @@ -127,7 +128,7 @@ public void moveToStarted() {
* Make the shard primary unless it's not Primary
* //TODO: doc exception
*/
public void moveToPrimary() {
void moveToPrimary() {
version++;
if (primary) {
throw new IllegalShardRoutingStateException(this, "Already primary, can't move to primary");
Expand All @@ -138,20 +139,13 @@ public void moveToPrimary() {
/**
* Set the primary shard to non-primary
*/
public void moveFromPrimary() {
void moveFromPrimary() {
version++;
if (!primary) {
throw new IllegalShardRoutingStateException(this, "Not primary, can't move to replica");
}
primary = false;
}

public void restoreFrom(RestoreSource restoreSource) {
version++;
if (!primary) {
throw new IllegalShardRoutingStateException(this, "Not primary, can't restore from snapshot to replica");
}
this.restoreSource = restoreSource;
}
}

17 changes: 2 additions & 15 deletions src/main/java/org/elasticsearch/cluster/routing/RoutingNode.java
Expand Up @@ -84,27 +84,14 @@ public List<MutableShardRouting> shards() {
* Add a new shard to this node
* @param shard Shard to crate on this Node
*/
public void add(MutableShardRouting shard) {
void add(MutableShardRouting shard) {
// TODO use Set with ShardIds for faster lookup.
for (MutableShardRouting shardRouting : shards) {
if (shardRouting.shardId().equals(shard.shardId())) {
throw new ElasticSearchIllegalStateException("Trying to add a shard [" + shard.shardId().index().name() + "][" + shard.shardId().id() + "] to a node [" + nodeId + "] where it already exists");
}
}
shards.add(shard);
shard.assignToNode(node.id());
}

/**
* Remove a shard from this node
* @param shardId id of the shard to remove
*/
public void removeByShardId(int shardId) {
for (Iterator<MutableShardRouting> it = shards.iterator(); it.hasNext(); ) {
MutableShardRouting shard = it.next();
if (shard.id() == shardId) {
it.remove();
}
}
}

/**
Expand Down

0 comments on commit 6af80d5

Please sign in to comment.