-
Notifications
You must be signed in to change notification settings - Fork 64
/
DgraphClientPool.java
156 lines (138 loc) · 4.34 KB
/
DgraphClientPool.java
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* Copyright 2016-18 DGraph Labs, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.dgraph;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import javafx.util.Pair;
/**
* Manage a pool of Dgraph clients.
*
* <p>Clients must belong to the same Dgraph cluster
*
* @author Deepak Jois
*/
public class DgraphClientPool {
List<Pair<ManagedChannel, DgraphGrpc.DgraphBlockingStub>> clients;
private int deadlineSecs;
private DgraphClientPool() {
this.clients = new ArrayList<>();
}
/**
* Create a pool of clients from a list of {@link ManagedChannel} objects.
*
* @param channels List of {@link ManagedChannel} objects to use when creating GRPC clients
*/
public DgraphClientPool(List<ManagedChannel> channels) {
this();
add(channels);
}
/**
* Create a pool of clients from a list of {@link ManagedChannel} objects, and specify a deadline
* (in seconds) for the requests to execute.
*
* @param channels List of {@link ManagedChannel} objects to use when creating GRPC clients
* @param deadlineSecs deadline in seconds for requests made to the clients.
*/
public DgraphClientPool(List<ManagedChannel> channels, int deadlineSecs) {
this(channels);
this.deadlineSecs = deadlineSecs;
}
/**
* Add a client that connects to a Dgraph server located at specified host and port.
*
* @param host
* @param port
*/
public void add(String host, int port) {
ManagedChannel channel =
ManagedChannelBuilder.forAddress(host, port).usePlaintext(true).build();
DgraphGrpc.DgraphBlockingStub stub = DgraphGrpc.newBlockingStub(channel);
clients.add(new Pair<>(channel, stub));
}
/**
* Add a client that connects to a Dgraph server, using the specified {@link ManagedChannel}.
*
* @param channel
*/
public void add(ManagedChannel channel) {
DgraphGrpc.DgraphBlockingStub stub = DgraphGrpc.newBlockingStub(channel);
clients.add(new Pair<>(channel, stub));
}
/**
* Add clients that connect to different nodes in a Dgraph cluster, using the specified {@link
* ManagedChannel} objects.
*
* @param channels
*/
public void add(List<ManagedChannel> channels) {
for (ManagedChannel channel : channels) {
DgraphGrpc.DgraphBlockingStub stub = DgraphGrpc.newBlockingStub(channel);
clients.add(new Pair<>(channel, stub));
}
}
/**
* Returns a randomly chosen client from the pool.
*
* @return A {@link io.dgraph.DgraphGrpc.DgraphBlockingStub} object.
*/
public DgraphGrpc.DgraphBlockingStub anyClient() {
Random rand = new Random();
DgraphGrpc.DgraphBlockingStub client = clients.get(rand.nextInt(clients.size())).getValue();
if (deadlineSecs > 0) {
return client.withDeadlineAfter(deadlineSecs, TimeUnit.SECONDS);
}
return client;
}
/**
* Return the deadline(in secs) for the clients in the pool.
*
* @return
*/
public int getDeadline() {
return deadlineSecs;
}
/**
* Set the deadline for the clients in the pool in secs.
*
* @param deadlineSecs
*/
public void setDeadline(int deadlineSecs) {
this.deadlineSecs = deadlineSecs;
}
/**
* Closes all the clients in the pool with the specified timeout.
*
* @param timeoutSecs timeout value in seconds
* @throws InterruptedException
*/
public void close(long timeoutSecs) throws InterruptedException {
for (Pair<ManagedChannel, DgraphGrpc.DgraphBlockingStub> client : clients) {
client.getKey().awaitTermination(timeoutSecs, TimeUnit.SECONDS);
}
}
/**
* Closes all the clients in the pool with a default timeout of 5 seconds.
*
* @throws InterruptedException
*/
public void close() throws InterruptedException {
close(5);
}
}