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
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.dispatch.rpc;
import com.google.common.collect.ImmutableMap;
import com.google.inject.Inject;
import com.yahoo.component.AbstractComponent;
import com.yahoo.compress.CompressionType;
import com.yahoo.compress.Compressor;
import com.yahoo.compress.Compressor.Compression;
import com.yahoo.processing.request.CompoundName;
import com.yahoo.search.Query;
import com.yahoo.search.dispatch.FillInvoker;
import com.yahoo.search.dispatch.rpc.Client.NodeConnection;
import com.yahoo.vespa.config.search.DispatchConfig;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Random;
/**
* RpcResourcePool constructs {@link FillInvoker} objects that communicate with content nodes over RPC. It also contains
* the RPC connection pool.
*
* @author ollivir
*/
public class RpcResourcePool extends AbstractComponent {
/** The compression method which will be used with rpc dispatch. "lz4" (default) and "none" is supported. */
public final static CompoundName dispatchCompression = new CompoundName("dispatch.compression");
private final Compressor compressor = new Compressor(CompressionType.LZ4, 5, 0.95, 32);
private final Random random = new Random();
/** Connections to the search nodes this talks to, indexed by node id ("partid") */
private final ImmutableMap<Integer, NodeConnectionPool> nodeConnectionPools;
private final RpcClient client;
RpcResourcePool(Map<Integer, NodeConnection> nodeConnections) {
var builder = new ImmutableMap.Builder<Integer, NodeConnectionPool>();
nodeConnections.forEach((key, connection) -> builder.put(key, new NodeConnectionPool(Collections.singletonList(connection))));
this.nodeConnectionPools = builder.build();
client = null;
}
@Inject
public RpcResourcePool(DispatchConfig dispatchConfig) {
super();
client = new RpcClient("dispatch-client", dispatchConfig.numJrtTransportThreads());
// Create rpc node connection pools indexed by the node distribution key
var builder = new ImmutableMap.Builder<Integer, NodeConnectionPool>();
var numConnections = dispatchConfig.numJrtConnectionsPerNode();
for (var node : dispatchConfig.node()) {
var connections = new ArrayList<NodeConnection>(numConnections);
for (int i = 0; i < numConnections; i++) {
connections.add(client.createConnection(node.host(), node.port()));
}
builder.put(node.key(), new NodeConnectionPool(connections));
}
this.nodeConnectionPools = builder.build();
}
public Compressor compressor() {
return compressor;
}
public Compression compress(Query query, byte[] payload) {
CompressionType compression = CompressionType.valueOf(query.properties().getString(dispatchCompression, "LZ4").toUpperCase());
return compressor.compress(compression, payload);
}
public NodeConnection getConnection(int nodeId) {
var pool = nodeConnectionPools.get(nodeId);
if (pool == null) {
return null;
} else {
return pool.nextConnection();
}
}
@Override
public void deconstruct() {
super.deconstruct();
nodeConnectionPools.values().forEach(NodeConnectionPool::release);
if (client != null) {
client.close();
}
}
private class NodeConnectionPool {
private final List<Client.NodeConnection> connections;
NodeConnectionPool(List<NodeConnection> connections) {
this.connections = connections;
}
Client.NodeConnection nextConnection() {
int slot = random.nextInt(connections.size());
return connections.get(slot);
}
void release() {
connections.forEach(Client.NodeConnection::close);
}
}
}
|