forked from dustin/java-memcached-client
-
Notifications
You must be signed in to change notification settings - Fork 114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add New Node Locator - Round Robin #27
Open
amcrn
wants to merge
1
commit into
couchbase:master
Choose a base branch
from
amcrn:enh/round-robin-locator
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package net.spy.memcached; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
import net.spy.memcached.MemcachedNode; | ||
import net.spy.memcached.MemcachedNodeROImpl; | ||
import net.spy.memcached.NodeLocator; | ||
|
||
|
||
/** | ||
* NodeLocator implementation that round-robins across active nodes. | ||
*/ | ||
public final class RoundRobinLocator implements NodeLocator { | ||
|
||
private int nodeIndex; | ||
private MemcachedNode[] nodes; | ||
|
||
public RoundRobinLocator(List<MemcachedNode> n) { | ||
super(); | ||
nodes = n.toArray(new MemcachedNode[n.size()]); | ||
} | ||
|
||
private RoundRobinLocator(MemcachedNode[] n) { | ||
super(); | ||
nodes = n; | ||
} | ||
|
||
/** | ||
* @param k Input key (which is ignored in round robin) | ||
* @return Next active node. If none of the nodes are active, the next node in | ||
* the list is returned. Never returns null. | ||
*/ | ||
@Override | ||
public synchronized MemcachedNode getPrimary(String k) { | ||
int i; | ||
for (i = nodeIndex; !nodes[i % nodes.length].isActive() | ||
&& i < nodeIndex + nodes.length; i++) {} | ||
|
||
nodeIndex = (i + 1) % nodes.length; | ||
return nodes[i % nodes.length]; | ||
} | ||
|
||
@Override | ||
public Iterator<MemcachedNode> getSequence(String k) { | ||
return new NodeIterator(nodeIndex); | ||
} | ||
|
||
@Override | ||
public Collection<MemcachedNode> getAll() { | ||
return Arrays.asList(nodes); | ||
} | ||
|
||
@Override | ||
public NodeLocator getReadonlyCopy() { | ||
MemcachedNode[] n = new MemcachedNode[nodes.length]; | ||
for (int i = 0; i < nodes.length; i++) { | ||
n[i] = new MemcachedNodeROImpl(nodes[i]); | ||
} | ||
return new RoundRobinLocator(n); | ||
} | ||
|
||
@Override | ||
public void updateLocator(List<MemcachedNode> newNodes) { | ||
this.nodes = newNodes.toArray(new MemcachedNode[newNodes.size()]); | ||
} | ||
|
||
class NodeIterator implements Iterator<MemcachedNode> { | ||
|
||
private final int start; | ||
private int next = 0; | ||
|
||
public NodeIterator(int keyStart) { | ||
start = keyStart; | ||
next = start; | ||
computeNext(); | ||
assert next >= 0 || nodes.length == 1 : "Starting sequence at " | ||
+ start + " of " + nodes.length + " next is " + next; | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return next >= 0; | ||
} | ||
|
||
private void computeNext() { | ||
if (++next >= nodes.length) { | ||
next = 0; | ||
} | ||
if (next == start) { | ||
next = -1; | ||
} | ||
} | ||
|
||
@Override | ||
public MemcachedNode next() { | ||
try { | ||
return nodes[next]; | ||
} finally { | ||
computeNext(); | ||
} | ||
} | ||
|
||
@Override | ||
public void remove() { | ||
throw new UnsupportedOperationException("Can't remove a node"); | ||
} | ||
} | ||
|
||
} |
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
121 changes: 121 additions & 0 deletions
121
src/test/java/net/spy/memcached/RoundRobinLocatorTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package net.spy.memcached; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
|
||
|
||
/** | ||
* Test the RoundRobinLocatorTest. | ||
*/ | ||
public class RoundRobinLocatorTest extends AbstractNodeLocationCase { | ||
|
||
private void setActive(boolean value, int ... nodes) { | ||
for (int n : nodes) { | ||
nodeMocks[n].expects(atLeastOnce()).method("isActive") | ||
.will(returnValue(value)); | ||
} | ||
} | ||
|
||
@Override | ||
protected void setupNodes(int n) { | ||
super.setupNodes(n); | ||
locator = new RoundRobinLocator(Arrays.asList(nodes)); | ||
} | ||
|
||
public void testPrimarySingleNodeActive() throws Exception { | ||
setupNodes(1); | ||
setActive(true, 0); | ||
assertSame(nodes[0], locator.getPrimary("a")); | ||
assertSame(nodes[0], locator.getPrimary("b")); | ||
assertSame(nodes[0], locator.getPrimary("c")); | ||
} | ||
|
||
public void testPrimarySingleNodeDown() throws Exception { | ||
setupNodes(1); | ||
setActive(false, 0); | ||
assertSame(nodes[0], locator.getPrimary("a")); | ||
assertSame(nodes[0], locator.getPrimary("b")); | ||
assertSame(nodes[0], locator.getPrimary("c")); | ||
} | ||
|
||
public void testPrimaryMultiNodeOneDown() throws Exception { | ||
setupNodes(2); | ||
setActive(false, 0); | ||
setActive(true, 1); | ||
assertSame(nodes[1], locator.getPrimary("a")); | ||
assertSame(nodes[1], locator.getPrimary("b")); | ||
assertSame(nodes[1], locator.getPrimary("c")); | ||
} | ||
|
||
public void testPrimaryMultiMixedDown() throws Exception { | ||
setupNodes(4); | ||
setActive(false, 0, 2); | ||
setActive(true, 1, 3); | ||
assertSame(nodes[1], locator.getPrimary("a")); | ||
assertSame(nodes[3], locator.getPrimary("b")); | ||
assertSame(nodes[1], locator.getPrimary("c")); | ||
assertSame(nodes[3], locator.getPrimary("a")); | ||
} | ||
|
||
public void testPrimaryMultiNodeAllActive() throws Exception { | ||
setupNodes(4); | ||
setActive(true, 0, 1, 2, 3); | ||
assertSame(nodes[0], locator.getPrimary("a")); | ||
assertSame(nodes[1], locator.getPrimary("b")); | ||
assertSame(nodes[2], locator.getPrimary("c")); | ||
assertSame(nodes[3], locator.getPrimary("d")); | ||
assertSame(nodes[0], locator.getPrimary("e")); | ||
assertSame(nodes[1], locator.getPrimary("f")); | ||
assertSame(nodes[2], locator.getPrimary("g")); | ||
assertSame(nodes[3], locator.getPrimary("h")); | ||
assertSame(nodes[0], locator.getPrimary("i")); | ||
} | ||
|
||
public void testPrimaryMultiNodeAllDown() throws Exception { | ||
setupNodes(4); | ||
setActive(false, 0, 1, 2, 3); | ||
assertSame(nodes[0], locator.getPrimary("a")); | ||
assertSame(nodes[1], locator.getPrimary("b")); | ||
assertSame(nodes[2], locator.getPrimary("c")); | ||
assertSame(nodes[3], locator.getPrimary("d")); | ||
assertSame(nodes[0], locator.getPrimary("e")); | ||
assertSame(nodes[1], locator.getPrimary("f")); | ||
assertSame(nodes[2], locator.getPrimary("g")); | ||
assertSame(nodes[3], locator.getPrimary("h")); | ||
assertSame(nodes[0], locator.getPrimary("i")); | ||
} | ||
|
||
public void testPrimaryClone() throws Exception { | ||
setupNodes(2); | ||
setActive(true, 0); | ||
assertEquals(nodes[0].toString(), | ||
locator.getReadonlyCopy().getPrimary("a").toString()); | ||
assertEquals(nodes[0].toString(), | ||
locator.getReadonlyCopy().getPrimary("b").toString()); | ||
} | ||
|
||
public void testAll() throws Exception { | ||
setupNodes(4); | ||
Collection<MemcachedNode> all = locator.getAll(); | ||
assertEquals(4, all.size()); | ||
assertTrue(all.contains(nodes[0])); | ||
assertTrue(all.contains(nodes[1])); | ||
assertTrue(all.contains(nodes[2])); | ||
assertTrue(all.contains(nodes[3])); | ||
} | ||
|
||
public void testAllClone() throws Exception { | ||
setupNodes(4); | ||
Collection<MemcachedNode> all = locator.getReadonlyCopy().getAll(); | ||
assertEquals(4, all.size()); | ||
} | ||
|
||
@Override | ||
public final void testCloningGetPrimary() { | ||
setupNodes(5); | ||
setActive(true, 0); | ||
assertTrue(locator.getReadonlyCopy().getPrimary("hi") | ||
instanceof MemcachedNodeROImpl); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@amcrn what is the reason for excluding Redistribute FailureMode ? RoundRobin seems like a good fit for Redistribute as well.