Created
December 2, 2009 04:07
-
-
Save afeinberg/246929 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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