add shards iterator that can iterate over unrelated list of shards

This commit is contained in:
kimchy 2010-11-21 14:27:22 +02:00
parent 7db5e63ab7
commit e183fbd6ad
7 changed files with 263 additions and 191 deletions

View File

@ -47,7 +47,10 @@ public class ImmutableShardRouting implements Streamable, Serializable, ShardRou
private transient ShardId shardIdentifier;
public ImmutableShardRouting() {
private final transient ImmutableList<ShardRouting> asList;
ImmutableShardRouting() {
this.asList = ImmutableList.of((ShardRouting) this);
}
public ImmutableShardRouting(ShardRouting copy) {
@ -55,18 +58,19 @@ public class ImmutableShardRouting implements Streamable, Serializable, ShardRou
this.relocatingNodeId = copy.relocatingNodeId();
}
public ImmutableShardRouting(String index, int shardId, String currentNodeId,
String relocatingNodeId, boolean primary, ShardRoutingState state) {
this(index, shardId, currentNodeId, primary, state);
this.relocatingNodeId = relocatingNodeId;
}
public ImmutableShardRouting(String index, int shardId, String currentNodeId, boolean primary, ShardRoutingState state) {
this.index = index;
this.shardId = shardId;
this.currentNodeId = currentNodeId;
this.primary = primary;
this.state = state;
}
public ImmutableShardRouting(String index, int shardId, String currentNodeId,
String relocatingNodeId, boolean primary, ShardRoutingState state) {
this(index, shardId, currentNodeId, primary, state);
this.relocatingNodeId = relocatingNodeId;
this.asList = ImmutableList.of((ShardRouting) this);
}
@Override public String index() {
@ -134,7 +138,7 @@ public class ImmutableShardRouting implements Streamable, Serializable, ShardRou
}
@Override public ShardIterator shardsIt() {
return new PlainShardIterator(shardId(), ImmutableList.of((ShardRouting) this));
return new PlainShardIterator(shardId(), asList);
}
public static ImmutableShardRouting readShardRoutingEntry(StreamInput in) throws IOException {

View File

@ -95,7 +95,7 @@ public class IndexShardRoutingTable implements Iterable<ShardRouting> {
}
public ShardIterator shardsRandomIt() {
return new IndexShardIterator(nextCounter());
return new IndexShardIterator(Math.abs(nextCounter()));
}
public ShardRouting primaryShard() {
@ -133,8 +133,8 @@ public class IndexShardRoutingTable implements Iterable<ShardRouting> {
return counter.getAndIncrement();
}
ShardRouting shardModulo(int shardId) {
return shards.get((Math.abs(shardId) % size()));
ShardRouting shardModulo(int counter) {
return shards.get((counter % size()));
}
/**

View File

@ -24,9 +24,6 @@ package org.elasticsearch.cluster.routing;
*/
public class MutableShardRouting extends ImmutableShardRouting {
public MutableShardRouting() {
}
public MutableShardRouting(ShardRouting copy) {
super(copy);
}

View File

@ -21,134 +21,29 @@ package org.elasticsearch.cluster.routing;
import org.elasticsearch.index.shard.ShardId;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
/**
* @author kimchy (shay.banon)
*/
public class PlainShardIterator implements ShardIterator {
public class PlainShardIterator extends PlainShardsIterator implements ShardIterator {
private final ShardId shardId;
private final List<ShardRouting> shards;
private volatile int counter = 0;
public PlainShardIterator(ShardId shardId, List<ShardRouting> shards) {
super(shards);
this.shardId = shardId;
this.shards = shards;
}
@Override public ShardIterator reset() {
this.counter = 0;
super.reset();
return this;
}
@Override public int size() {
return shards.size();
}
@Override public ShardId shardId() {
return this.shardId;
}
@Override public Iterator<ShardRouting> iterator() {
return this;
}
@Override public boolean hasNext() {
return counter < shards.size();
}
@Override public ShardRouting next() {
if (!hasNext()) {
throw new NoSuchElementException("No shard found");
}
return shards.get(counter++);
}
@Override public int sizeActive() {
int sizeActive = 0;
for (ShardRouting shardRouting : shards) {
if (shardRouting.active()) {
sizeActive++;
}
}
return sizeActive;
}
@Override public boolean hasNextActive() {
int counter = this.counter;
while (counter < shards.size()) {
if (shards.get(counter++).active()) {
return true;
}
}
return false;
}
@Override public ShardRouting nextActive() throws NoSuchElementException {
ShardRouting shardRouting = nextActiveOrNull();
if (shardRouting == null) {
throw new NoSuchElementException("No active shard found");
}
return shardRouting;
}
@Override public ShardRouting nextActiveOrNull() throws NoSuchElementException {
while (counter < shards.size()) {
ShardRouting shardRouting = shards.get(counter++);
if (shardRouting.active()) {
return shardRouting;
}
}
return null;
}
@Override public int sizeAssigned() {
int sizeAssigned = 0;
for (ShardRouting shardRouting : shards) {
if (shardRouting.assignedToNode()) {
sizeAssigned++;
}
}
return sizeAssigned;
}
@Override public boolean hasNextAssigned() {
int counter = this.counter;
while (counter < shards.size()) {
if (shards.get(counter++).assignedToNode()) {
return true;
}
}
return false;
}
@Override public ShardRouting nextAssigned() throws NoSuchElementException {
ShardRouting shardRouting = nextAssignedOrNull();
if (shardRouting == null) {
throw new NoSuchElementException("No assigned shard found");
}
return shardRouting;
}
@Override public ShardRouting nextAssignedOrNull() {
while (counter < shards.size()) {
ShardRouting shardRouting = shards.get(counter++);
if (shardRouting.assignedToNode()) {
return shardRouting;
}
}
return null;
}
@Override public void remove() {
throw new UnsupportedOperationException();
}
@Override public boolean equals(Object o) {
if (this == o) return true;

View File

@ -0,0 +1,142 @@
/*
* 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 java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
/**
* @author kimchy (shay.banon)
*/
public class PlainShardsIterator implements ShardsIterator {
protected final List<ShardRouting> shards;
private volatile int counter = 0;
public PlainShardsIterator(List<ShardRouting> shards) {
this.shards = shards;
}
@Override public ShardsIterator reset() {
this.counter = 0;
return this;
}
@Override public int size() {
return shards.size();
}
@Override public Iterator<ShardRouting> iterator() {
return this;
}
@Override public boolean hasNext() {
return counter < shards.size();
}
@Override public ShardRouting next() {
if (!hasNext()) {
throw new NoSuchElementException("No shard found");
}
return shards.get(counter++);
}
@Override public int sizeActive() {
int sizeActive = 0;
for (ShardRouting shardRouting : shards) {
if (shardRouting.active()) {
sizeActive++;
}
}
return sizeActive;
}
@Override public boolean hasNextActive() {
int counter = this.counter;
while (counter < shards.size()) {
if (shards.get(counter++).active()) {
return true;
}
}
return false;
}
@Override public ShardRouting nextActive() throws NoSuchElementException {
ShardRouting shardRouting = nextActiveOrNull();
if (shardRouting == null) {
throw new NoSuchElementException("No active shard found");
}
return shardRouting;
}
@Override public ShardRouting nextActiveOrNull() throws NoSuchElementException {
while (counter < shards.size()) {
ShardRouting shardRouting = shards.get(counter++);
if (shardRouting.active()) {
return shardRouting;
}
}
return null;
}
@Override public int sizeAssigned() {
int sizeAssigned = 0;
for (ShardRouting shardRouting : shards) {
if (shardRouting.assignedToNode()) {
sizeAssigned++;
}
}
return sizeAssigned;
}
@Override public boolean hasNextAssigned() {
int counter = this.counter;
while (counter < shards.size()) {
if (shards.get(counter++).assignedToNode()) {
return true;
}
}
return false;
}
@Override public ShardRouting nextAssigned() throws NoSuchElementException {
ShardRouting shardRouting = nextAssignedOrNull();
if (shardRouting == null) {
throw new NoSuchElementException("No assigned shard found");
}
return shardRouting;
}
@Override public ShardRouting nextAssignedOrNull() {
while (counter < shards.size()) {
ShardRouting shardRouting = shards.get(counter++);
if (shardRouting.assignedToNode()) {
return shardRouting;
}
}
return null;
}
@Override public void remove() {
throw new UnsupportedOperationException();
}
}

View File

@ -21,15 +21,12 @@ package org.elasticsearch.cluster.routing;
import org.elasticsearch.index.shard.ShardId;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* Allows to iterate over a set of shard instances (routing) within a shard id group.
*
* @author kimchy (shay.banon)
*/
public interface ShardIterator extends Iterable<ShardRouting>, Iterator<ShardRouting> {
public interface ShardIterator extends ShardsIterator {
/**
* The shard id this group relates to.
@ -40,69 +37,4 @@ public interface ShardIterator extends Iterable<ShardRouting>, Iterator<ShardRou
* Resets the iterator.
*/
ShardIterator reset();
/**
* The number of shard routing instances.
*/
int size();
/**
* The number of active shard routing instances.
*
* @see ShardRouting#active()
*/
int sizeActive();
/**
* Is there an active shard we can iterate to.
*
* @see ShardRouting#active()
*/
boolean hasNextActive();
/**
* Returns the next active shard, or throws {@link NoSuchElementException}.
*
* @see ShardRouting#active()
*/
ShardRouting nextActive() throws NoSuchElementException;
/**
* Returns the next active shard, or <tt>null</tt>.
*
* @see ShardRouting#active()
*/
ShardRouting nextActiveOrNull();
/**
* The number of assigned shard routing instances.
*
* @see ShardRouting#assignedToNode()
*/
int sizeAssigned();
/**
* Is there an assigned shard we can iterate to.
*
* @see ShardRouting#assignedToNode()
*/
boolean hasNextAssigned();
/**
* Returns the next assigned shard, or throws {@link NoSuchElementException}.
*
* @see ShardRouting#assignedToNode()
*/
ShardRouting nextAssigned() throws NoSuchElementException;
/**
* Returns the next assigned shard, or <tt>null</tt>.
*
* @see ShardRouting#assignedToNode()
*/
ShardRouting nextAssignedOrNull();
int hashCode();
boolean equals(Object other);
}

View File

@ -0,0 +1,102 @@
/*
* 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 java.util.Iterator;
import java.util.NoSuchElementException;
/**
* Allows to iterate over unrelated shards.
*
* @author kimchy (shay.banon)
*/
public interface ShardsIterator extends Iterable<ShardRouting>, Iterator<ShardRouting> {
/**
* Resets the iterator.
*/
ShardsIterator reset();
/**
* The number of shard routing instances.
*/
int size();
/**
* The number of active shard routing instances.
*
* @see ShardRouting#active()
*/
int sizeActive();
/**
* Is there an active shard we can iterate to.
*
* @see ShardRouting#active()
*/
boolean hasNextActive();
/**
* Returns the next active shard, or throws {@link NoSuchElementException}.
*
* @see ShardRouting#active()
*/
ShardRouting nextActive() throws NoSuchElementException;
/**
* Returns the next active shard, or <tt>null</tt>.
*
* @see ShardRouting#active()
*/
ShardRouting nextActiveOrNull();
/**
* The number of assigned shard routing instances.
*
* @see ShardRouting#assignedToNode()
*/
int sizeAssigned();
/**
* Is there an assigned shard we can iterate to.
*
* @see ShardRouting#assignedToNode()
*/
boolean hasNextAssigned();
/**
* Returns the next assigned shard, or throws {@link NoSuchElementException}.
*
* @see ShardRouting#assignedToNode()
*/
ShardRouting nextAssigned() throws NoSuchElementException;
/**
* Returns the next assigned shard, or <tt>null</tt>.
*
* @see ShardRouting#assignedToNode()
*/
ShardRouting nextAssignedOrNull();
int hashCode();
boolean equals(Object other);
}