Search in sources :

Example 1 with LoadBalancer

use of com.yahoo.documentapi.messagebus.protocol.LoadBalancer in project vespa by vespa-engine.

the class LoadBalancerTestCase method assertIllegalArgument.

private static void assertIllegalArgument(String clusterName, String recipient, String expectedMessage) {
    LoadBalancer.Metrics metric = new LoadBalancer.Metrics("");
    LoadBalancer policy = new LoadBalancer(clusterName, "", metric);
    try {
        fail("Expected exception, got index " + policy.getIndex(recipient) + ".");
    } catch (IllegalArgumentException e) {
        assertEquals(expectedMessage, e.getMessage());
    }
}
Also used : LoadBalancer(com.yahoo.documentapi.messagebus.protocol.LoadBalancer)

Example 2 with LoadBalancer

use of com.yahoo.documentapi.messagebus.protocol.LoadBalancer in project vespa by vespa-engine.

the class LoadBalancerTestCase method testLoadBalancer.

@Test
public void testLoadBalancer() {
    LoadBalancer.Metrics m = new LoadBalancer.Metrics("");
    LoadBalancer lb = new LoadBalancer("foo", "", m);
    Mirror.Entry[] entries = new Mirror.Entry[] { 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.get().intValue());
        assertEquals(33, weights.get(1).sent.get().intValue());
        assertEquals(33, weights.get(2).sent.get().intValue());
        weights.get(0).sent.set(new AtomicLong(0));
        weights.get(1).sent.set(new AtomicLong(0));
        weights.get(2).sent.set(new AtomicLong(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);
        }
        PrintWriter writer = new PrintWriter(System.out);
        m.toXML(new XMLWriter(writer));
        writer.flush();
        assertEquals(421, (int) (100 * weights.get(0).weight.get() / weights.get(1).weight.get()));
        assertEquals(100, (int) (100 * weights.get(1).weight.get()));
        assertEquals(421, (int) (100 * weights.get(2).weight.get() / weights.get(1).weight.get()));
    }
    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());
}
Also used : LoadBalancer(com.yahoo.documentapi.messagebus.protocol.LoadBalancer) XMLWriter(com.yahoo.text.XMLWriter) AtomicLong(java.util.concurrent.atomic.AtomicLong) Mirror(com.yahoo.jrt.slobrok.api.Mirror) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 3 with LoadBalancer

use of com.yahoo.documentapi.messagebus.protocol.LoadBalancer in project vespa by vespa-engine.

the class LoadBalancerTestCase method testLoadBalancerOneItemOnly.

@Test
public void testLoadBalancerOneItemOnly() {
    LoadBalancer.Metrics m = new LoadBalancer.Metrics("");
    LoadBalancer lb = new LoadBalancer("foo", "", m);
    Mirror.Entry[] entries = new Mirror.Entry[] { new Mirror.Entry("foo/0/default", "tcp/bar:1") };
    List<LoadBalancer.NodeMetrics> weights = lb.getNodeWeights();
    assertEquals("foo/0/default", lb.getRecipient(entries).entry.getName());
    // busy
    lb.received(new LoadBalancer.Node(new Mirror.Entry("foo/0/default", "tcp/bar:1"), weights.get(0)), true);
    assertEquals("foo/0/default", lb.getRecipient(entries).entry.getName());
}
Also used : LoadBalancer(com.yahoo.documentapi.messagebus.protocol.LoadBalancer) Test(org.junit.Test)

Aggregations

LoadBalancer (com.yahoo.documentapi.messagebus.protocol.LoadBalancer)3 Test (org.junit.Test)2 Mirror (com.yahoo.jrt.slobrok.api.Mirror)1 XMLWriter (com.yahoo.text.XMLWriter)1 PrintWriter (java.io.PrintWriter)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1