blob: 2bd0d24b14f6fc50300b3f9ea74a6def54a6d73a (
plain) (
blame)
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
|
// Copyright Vespa.ai. 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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
abstract class LoadBalancer {
static class NodeMetrics {
private AtomicLong sent = new AtomicLong(0);
private AtomicLong received = new AtomicLong(0);
private AtomicLong busy = new AtomicLong(0);
long pending() { return sent.get() - received.get(); }
void incSend() { sent.incrementAndGet(); }
void incReceived() { received.incrementAndGet(); }
void incBusy() { busy.incrementAndGet(); }
long sent() { return sent.get(); }
void reset() {
sent.set(0);
received.set(0);
busy.set(0);
}
}
static class Node {
Node(Mirror.Entry e, NodeMetrics m) { entry = e; metrics = m; }
Mirror.Entry entry;
NodeMetrics metrics;
}
private final Map<String, Integer> cachedIndex = new HashMap<>();
/** Statistics on each node we are load balancing over. Populated lazily. */
private final List<NodeMetrics> nodeWeights = new ArrayList<>();
private final String cluster;
public LoadBalancer(String cluster) {
this.cluster = cluster;
}
List<NodeMetrics> getNodeWeights() {
return nodeWeights;
}
/** Returns the index from a node name string */
int getIndex(String nodeName) {
try {
String s = nodeName.substring(cluster.length() + 1);
s = s.substring(0, s.indexOf("/"));
s = s.substring(s.lastIndexOf(".") + 1);
return Integer.parseInt(s);
} catch (IndexOutOfBoundsException | NumberFormatException e) {
String err = "Expected recipient on the form '" + cluster + "/x/[y.]number/z', got '" + nodeName + "'.";
throw new IllegalArgumentException(err, e);
}
}
int getCachedIndex(String nodeName) {
return cachedIndex.computeIfAbsent(nodeName, key -> getIndex(key));
}
/**
* Returns the node metrics at a given index.
* If there is no entry at the given index it is created by this call.
*/
protected final synchronized NodeMetrics getNodeMetrics(Mirror.Entry entry) {
int index = getCachedIndex(entry.getName());
// expand node array as needed
while (nodeWeights.size() < (index + 1))
nodeWeights.add(null);
NodeMetrics nodeMetrics = nodeWeights.get(index);
if (nodeMetrics == null) { // initialize statistics for this node
nodeMetrics = createNodeMetrics();
nodeWeights.set(index, nodeMetrics);
}
return nodeMetrics;
}
protected NodeMetrics createNodeMetrics() {
return new NodeMetrics();
}
abstract Node getRecipient(List<Mirror.Entry> choices);
abstract void received(Node node, boolean busy);
}
|