Skip to content

Instantly share code, notes, and snippets.

@afeinberg
Created December 2, 2009 04:07
Show Gist options
  • Save afeinberg/246929 to your computer and use it in GitHub Desktop.
Save afeinberg/246929 to your computer and use it in GitHub Desktop.
/**
* @author afeinberg
*/
public class HintedHandoff {
private final Cluster cluster;
private final RoutingStrategy routingStrategy;
private int lastChosen = -1;
public HintedHandoff(Cluster cluster, RoutingStrategy routingStrategy) {
this.cluster = cluster;
this.routingStrategy = routingStrategy;
}
public synchronized Node getSlopNode(byte[] key) {
Set<Integer> prefListIds = ImmutableSet.copyOf(Lists.transform(routingStrategy.routeRequest(key),
new Function<Node, Integer>() {
public Integer apply(Node input) {
return input.getId();
}
}));
if (lastChosen == cluster.getNumberOfNodes()-1)
lastChosen = -1;
for (int i=lastChosen+1; i < cluster.getNumberOfNodes(); i++) {
if (!prefListIds.contains(i)) {
lastChosen = i;
return cluster.getNodeById(lastChosen);
}
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment