diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2020-06-15 11:57:25 +0000 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2020-06-15 11:57:25 +0000 |
commit | 33fef34dc86908c2523df6bf18ec8138372368df (patch) | |
tree | b322320edc8fc88403fea530d5281b2514e8d27e /documentapi/src | |
parent | 37453541835fabe546a30caf554331e992649f50 (diff) |
Legacy non-balancing loadbalancer is now a thing of the past.
Diffstat (limited to 'documentapi/src')
3 files changed, 7 insertions, 166 deletions
diff --git a/documentapi/src/main/java/com/yahoo/documentapi/messagebus/protocol/LegacyLoadBalancer.java b/documentapi/src/main/java/com/yahoo/documentapi/messagebus/protocol/LegacyLoadBalancer.java deleted file mode 100644 index c1e580794b4..00000000000 --- a/documentapi/src/main/java/com/yahoo/documentapi/messagebus/protocol/LegacyLoadBalancer.java +++ /dev/null @@ -1,92 +0,0 @@ -// 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 java.util.List; - -/** - * Load balances over a set of nodes based on statistics gathered from those nodes. - * - * @author thomasg - */ -class LegacyLoadBalancer extends LoadBalancer { - - static class LegacyNodeMetrics extends NodeMetrics { - double weight = 1.0; - } - - private double position = 0.0; - - public LegacyLoadBalancer(String cluster) { - super(cluster); - } - - /** - * The load balancing operation: Returns a node choice from the given choices, - * based on previously gathered statistics on the nodes, and a running "position" - * which is increased by 1 on each call to this. - * - * @param choices the node choices, represented as Slobrok entries - * @return the chosen node, or null only if the given choices were zero - */ - @Override - Node getRecipient(List<Mirror.Entry> choices) { - if (choices.isEmpty()) return null; - - double weightSum = 0.0; - Node selectedNode = null; - synchronized (this) { - for (Mirror.Entry entry : choices) { - LegacyNodeMetrics nodeMetrics = (LegacyNodeMetrics)getNodeMetrics(entry); - - weightSum += nodeMetrics.weight; - - if (weightSum > position) { - selectedNode = new Node(entry, nodeMetrics); - break; - } - } - if (selectedNode == null) { // Position>sum of all weights: Wrap around (but keep the remainder for some reason) - position -= weightSum; - selectedNode = new Node(choices.get(0), getNodeMetrics(choices.get(0))); - } - position += 1.0; - selectedNode.metrics.incSend(); - } - return selectedNode; - } - - @Override - protected NodeMetrics createNodeMetrics() { - return new LegacyNodeMetrics(); - } - - /** Scale weights such that ratios are preserved */ - private void increaseWeights() { - for (NodeMetrics nodeMetrics : getNodeWeights()) { - LegacyNodeMetrics n = (LegacyNodeMetrics) nodeMetrics; - if (n == null) continue; - double want = n.weight * 1.01010101010101010101; - n.weight = Math.max(1.0, want); - } - } - - @Override - void received(Node node, boolean busy) { - if (busy) { - synchronized (this) { - LegacyNodeMetrics n = (LegacyNodeMetrics) node.metrics; - double wantWeight = n.weight - 0.01; - if (wantWeight < 1.0) { - increaseWeights(); - n.weight = 1.0; - } else { - n.weight = wantWeight; - } - node.metrics.incBusy(); - } - } - } - -} diff --git a/documentapi/src/main/java/com/yahoo/documentapi/messagebus/protocol/LoadBalancerPolicy.java b/documentapi/src/main/java/com/yahoo/documentapi/messagebus/protocol/LoadBalancerPolicy.java index 4f955f3649e..3c670299f3e 100644 --- a/documentapi/src/main/java/com/yahoo/documentapi/messagebus/protocol/LoadBalancerPolicy.java +++ b/documentapi/src/main/java/com/yahoo/documentapi/messagebus/protocol/LoadBalancerPolicy.java @@ -26,7 +26,6 @@ import java.util.Map; public class LoadBalancerPolicy extends SlobrokPolicy { private final String session; private final String pattern; - private final LoadBalancer loadBalancer; LoadBalancerPolicy(String param) { @@ -48,19 +47,12 @@ public class LoadBalancerPolicy extends SlobrokPolicy { } pattern = cluster + "/*/" + session; - String type = params.get("type"); - if ("adaptive".equals(type)) { - loadBalancer = new AdaptiveLoadBalancer(cluster); - } else if ("legacy".equals(type)) { - loadBalancer = new LegacyLoadBalancer(cluster); - } else { - loadBalancer = new LegacyLoadBalancer(cluster); - } + loadBalancer = new AdaptiveLoadBalancer(cluster); } @Override public void select(RoutingContext context) { - LegacyLoadBalancer.Node node = getRecipient(context); + LoadBalancer.Node node = getRecipient(context); if (node != null) { context.setContext(node); @@ -77,7 +69,7 @@ public class LoadBalancerPolicy extends SlobrokPolicy { @return Returns a hop representing the TCP address of the target, or null if none could be found. */ - private LegacyLoadBalancer.Node getRecipient(RoutingContext context) { + private LoadBalancer.Node getRecipient(RoutingContext context) { List<Mirror.Entry> lastLookup = lookup(context, pattern); return loadBalancer.getRecipient(lastLookup); } @@ -85,7 +77,7 @@ public class LoadBalancerPolicy extends SlobrokPolicy { public void merge(RoutingContext context) { RoutingNodeIterator it = context.getChildIterator(); Reply reply = it.removeReply(); - LegacyLoadBalancer.Node target = (LegacyLoadBalancer.Node)context.getContext(); + LoadBalancer.Node target = (LoadBalancer.Node)context.getContext(); boolean busy = false; for (int i = 0; i < reply.getNumErrors(); i++) { diff --git a/documentapi/src/test/java/com/yahoo/documentapi/messagebus/protocol/LoadBalancerTestCase.java b/documentapi/src/test/java/com/yahoo/documentapi/messagebus/protocol/LoadBalancerTestCase.java index 088259b74ac..582bd53d8e7 100644 --- a/documentapi/src/test/java/com/yahoo/documentapi/messagebus/protocol/LoadBalancerTestCase.java +++ b/documentapi/src/test/java/com/yahoo/documentapi/messagebus/protocol/LoadBalancerTestCase.java @@ -33,7 +33,7 @@ public class LoadBalancerTestCase { } private static void assertIllegalArgument(String clusterName, String recipient, String expectedMessage) { - LegacyLoadBalancer policy = new LegacyLoadBalancer(clusterName); + LoadBalancer policy = new AdaptiveLoadBalancer(clusterName); try { fail("Expected exception, got index " + policy.getIndex(recipient) + "."); } catch (IllegalArgumentException e) { @@ -44,9 +44,9 @@ public class LoadBalancerTestCase { @Test public void testLoadBalancerCreation() { LoadBalancerPolicy lbp = new LoadBalancerPolicy("cluster=docproc/cluster.mobile.indexing;session=chain.mobile.indexing"); - assertTrue(lbp.getLoadBalancer() instanceof LegacyLoadBalancer); + assertTrue(lbp.getLoadBalancer() instanceof AdaptiveLoadBalancer); lbp = new LoadBalancerPolicy("cluster=docproc/cluster.mobile.indexing;session=chain.mobile.indexing;type=legacy"); - assertTrue(lbp.getLoadBalancer() instanceof LegacyLoadBalancer); + assertTrue(lbp.getLoadBalancer() instanceof AdaptiveLoadBalancer); lbp = new LoadBalancerPolicy("cluster=docproc/cluster.mobile.indexing;session=chain.mobile.indexing;type=adaptive"); assertTrue(lbp.getLoadBalancer() instanceof AdaptiveLoadBalancer); } @@ -110,64 +110,6 @@ public class LoadBalancerTestCase { assertEquals(1019, weights.get(2).pending()); } - @Test - public void testLegacyLoadBalancer() { - LoadBalancer lb = new LegacyLoadBalancer("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).reset(); - weights.get(1).reset(); - weights.get(2).reset(); - } - - { - // 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 * ((LegacyLoadBalancer.LegacyNodeMetrics)weights.get(0)).weight / ((LegacyLoadBalancer.LegacyNodeMetrics)weights.get(1)).weight)); - assertEquals(100, (int)(100 * ((LegacyLoadBalancer.LegacyNodeMetrics)weights.get(1)).weight)); - assertEquals(421, (int)(100 * ((LegacyLoadBalancer.LegacyNodeMetrics)weights.get(2)).weight / ((LegacyLoadBalancer.LegacyNodeMetrics)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()); - } - private void verifyLoadBalancerOneItemOnly(LoadBalancer lb) { List<Mirror.Entry> entries = Arrays.asList(new Mirror.Entry("foo/0/default", "tcp/bar:1") ); @@ -181,7 +123,6 @@ public class LoadBalancerTestCase { } @Test public void testLoadBalancerOneItemOnly() { - verifyLoadBalancerOneItemOnly(new LegacyLoadBalancer("foo")); verifyLoadBalancerOneItemOnly(new AdaptiveLoadBalancer("foo")); } } |