LoadBalancer { public Optional<Group> takeGroup(Set<Integer> rejectedGroups) { synchronized (this) { Optional<GroupStatus> best = scheduler.takeNextGroup(rejectedGroups); if (best.isPresent()) { GroupStatus gs = best.get(); gs.allocate(); Group ret = gs.group; log.fine(() -> "Offering <" + ret + "> for query connection"); return Optional.of(ret); } else { return Optional.empty(); } } } LoadBalancer(SearchCluster searchCluster, boolean roundRobin); Optional<Group> takeGroup(Set<Integer> rejectedGroups); void releaseGroup(Group group, boolean success, double searchTimeMs);  }
@Test public void requireThatLoadBalancerServesSingleNodeSetups() { Node n1 = new Node(0, "test-node1", 0); SearchCluster cluster = new SearchCluster("a", createDispatchConfig(n1), null, null); LoadBalancer lb = new LoadBalancer(cluster, true); Optional<Group> grp = lb.takeGroup(null); Group group = grp.orElseGet(() -> { throw new AssertionFailedError("Expected a SearchCluster.Group"); }); assertThat(group.nodes().size(), equalTo(1)); }
@Test public void requireThatLoadBalancerServesMultiGroupSetups() { Node n1 = new Node(0, "test-node1", 0); Node n2 = new Node(1, "test-node2", 1); SearchCluster cluster = new SearchCluster("a", createDispatchConfig(n1, n2), null, null); LoadBalancer lb = new LoadBalancer(cluster, true); Optional<Group> grp = lb.takeGroup(null); Group group = grp.orElseGet(() -> { throw new AssertionFailedError("Expected a SearchCluster.Group"); }); assertThat(group.nodes().size(), equalTo(1)); }
@Test public void requireThatLoadBalancerServesClusteredGroups() { Node n1 = new Node(0, "test-node1", 0); Node n2 = new Node(1, "test-node2", 0); Node n3 = new Node(0, "test-node3", 1); Node n4 = new Node(1, "test-node4", 1); SearchCluster cluster = new SearchCluster("a", createDispatchConfig(n1, n2, n3, n4), null, null); LoadBalancer lb = new LoadBalancer(cluster, true); Optional<Group> grp = lb.takeGroup(null); assertThat(grp.isPresent(), is(true)); }