1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.documentapi.messagebus.protocol;
import com.yahoo.jrt.slobrok.api.Mirror;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
/**
* @author Simon Thoresen Hult
*/
public class LoadBalancerTestCase {
@Test
public void requireThatParseExceptionIsReadable() {
assertIllegalArgument("foo", "bar", "Expected recipient on the form 'foo/x/[y.]number/z', got 'bar'.");
assertIllegalArgument("foo", "foobar", "Expected recipient on the form 'foo/x/[y.]number/z', got 'foobar'.");
assertIllegalArgument("foo", "foo", "Expected recipient on the form 'foo/x/[y.]number/z', got 'foo'.");
assertIllegalArgument("foo", "foo/", "Expected recipient on the form 'foo/x/[y.]number/z', got 'foo/'.");
assertIllegalArgument("foo", "foo/0", "Expected recipient on the form 'foo/x/[y.]number/z', got 'foo/0'.");
assertIllegalArgument("foo", "foo/0.", "Expected recipient on the form 'foo/x/[y.]number/z', got 'foo/0.'.");
assertIllegalArgument("foo", "foo/0.bar", "Expected recipient on the form 'foo/x/[y.]number/z', got 'foo/0.bar'.");
assertIllegalArgument("foo", "foo/bar", "Expected recipient on the form 'foo/x/[y.]number/z', got 'foo/bar'.");
assertIllegalArgument("foo", "foo/bar.", "Expected recipient on the form 'foo/x/[y.]number/z', got 'foo/bar.'.");
assertIllegalArgument("foo", "foo/bar.0", "Expected recipient on the form 'foo/x/[y.]number/z', got 'foo/bar.0'.");
}
private static void assertIllegalArgument(String clusterName, String recipient, String expectedMessage) {
LoadBalancer policy = new LoadBalancer(clusterName);
try {
fail("Expected exception, got index " + policy.getIndex(recipient) + ".");
} catch (IllegalArgumentException e) {
assertEquals(expectedMessage, e.getMessage());
}
}
@Test
public void testLoadBalancer() {
LoadBalancer lb = new LoadBalancer("foo");
List<Mirror.Entry> entries = Arrays.asList(new Mirror.Entry("foo/0/default", "tcp/bar:1"),
new Mirror.Entry("foo/1/default", "tcp/bar:2"),
new Mirror.Entry("foo/2/default", "tcp/bar:3"));
List<LoadBalancer.NodeMetrics> weights = lb.getNodeWeights();
{
for (int i = 0; i < 99; i++) {
LoadBalancer.Node node = lb.getRecipient(entries);
assertEquals("foo/" + (i % 3) + "/default" , node.entry.getName());
}
assertEquals(33, weights.get(0).sent);
assertEquals(33, weights.get(1).sent);
assertEquals(33, weights.get(2).sent);
weights.get(0).sent = 0;
weights.get(1).sent = 0;
weights.get(2).sent = 0;
}
{
// Simulate that one node is overloaded. It returns busy twice as often as the others.
for (int i = 0; i < 100; i++) {
lb.received(new LoadBalancer.Node(new Mirror.Entry("foo/0/default", "tcp/bar:1"), weights.get(0)), true);
lb.received(new LoadBalancer.Node(new Mirror.Entry("foo/0/default", "tcp/bar:1"), weights.get(0)), false);
lb.received(new LoadBalancer.Node(new Mirror.Entry("foo/0/default", "tcp/bar:1"), weights.get(0)), false);
lb.received(new LoadBalancer.Node(new Mirror.Entry("foo/2/default", "tcp/bar:3"), weights.get(2)), true);
lb.received(new LoadBalancer.Node(new Mirror.Entry("foo/2/default", "tcp/bar:3"), weights.get(2)), false);
lb.received(new LoadBalancer.Node(new Mirror.Entry("foo/2/default", "tcp/bar:3"), weights.get(2)), false);
lb.received(new LoadBalancer.Node(new Mirror.Entry("foo/1/default", "tcp/bar:2"), weights.get(1)), true);
lb.received(new LoadBalancer.Node(new Mirror.Entry("foo/1/default", "tcp/bar:2"), weights.get(1)), true);
lb.received(new LoadBalancer.Node(new Mirror.Entry("foo/1/default", "tcp/bar:2"), weights.get(1)), false);
}
assertEquals(421, (int)(100 * weights.get(0).weight / weights.get(1).weight));
assertEquals(100, (int)(100 * weights.get(1).weight));
assertEquals(421, (int)(100 * weights.get(2).weight / weights.get(1).weight));
}
assertEquals("foo/0/default" , lb.getRecipient(entries).entry.getName());
assertEquals("foo/0/default" , lb.getRecipient(entries).entry.getName());
assertEquals("foo/1/default" , lb.getRecipient(entries).entry.getName());
assertEquals("foo/2/default" , lb.getRecipient(entries).entry.getName());
assertEquals("foo/2/default" , lb.getRecipient(entries).entry.getName());
assertEquals("foo/2/default" , lb.getRecipient(entries).entry.getName());
assertEquals("foo/2/default" , lb.getRecipient(entries).entry.getName());
assertEquals("foo/0/default" , lb.getRecipient(entries).entry.getName());
assertEquals("foo/0/default" , lb.getRecipient(entries).entry.getName());
assertEquals("foo/0/default" , lb.getRecipient(entries).entry.getName());
}
@Test
public void testLoadBalancerOneItemOnly() {
LoadBalancer lb = new LoadBalancer("foo");
List<Mirror.Entry> entries = Arrays.asList(new Mirror.Entry("foo/0/default", "tcp/bar:1") );
List<LoadBalancer.NodeMetrics> weights = lb.getNodeWeights();
assertEquals("foo/0/default" , lb.getRecipient(entries).entry.getName());
lb.received(new LoadBalancer.Node(new Mirror.Entry("foo/0/default", "tcp/bar:1"), weights.get(0)), true); // busy
assertEquals("foo/0/default" , lb.getRecipient(entries).entry.getName());
}
}
|