-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathRedisClusterClient.java
278 lines (239 loc) · 9.5 KB
/
RedisClusterClient.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
* Copyright 2019 Red Hat, Inc.
* <p>
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* <p>
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* <p>
* The Apache License v2.0 is available at
* http://www.opensource.org/licenses/apache2.0.php
* <p>
* You may elect to redistribute this code under either of these licenses.
*/
package io.vertx.redis.client.impl;
import io.vertx.core.*;
import io.vertx.core.impl.ContextInternal;
import io.vertx.core.impl.logging.Logger;
import io.vertx.core.impl.logging.LoggerFactory;
import io.vertx.core.net.NetClientOptions;
import io.vertx.core.tracing.TracingPolicy;
import io.vertx.redis.client.*;
import io.vertx.redis.client.impl.types.MultiType;
import io.vertx.redis.client.impl.types.NumberType;
import io.vertx.redis.client.impl.types.SimpleStringType;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import static io.vertx.redis.client.Command.*;
import static io.vertx.redis.client.Request.cmd;
public class RedisClusterClient extends BaseRedisClient implements Redis {
private static final Logger LOG = LoggerFactory.getLogger(RedisClusterClient.class);
public static void addReducer(Command command, Function<List<Response>, Response> fn) {
RedisClusterConnection.addReducer(command, fn);
}
public static void addMasterOnlyCommand(Command command) {
RedisClusterConnection.addMasterOnlyCommand(command);
}
static {
// provided reducers
addReducer(MSET, list ->
// Simple string reply: always OK since MSET can't fail.
SimpleStringType.OK);
addReducer(DEL, list ->
NumberType.create(list.stream()
.mapToLong(el -> {
Long l = el.toLong();
if (l == null) {
return 0L;
} else {
return l;
}
}).sum()));
addReducer(MGET, list -> {
int total = 0;
for (Response resp : list) {
total += resp.size();
}
MultiType multi = MultiType.create(total, false);
for (Response resp : list) {
for (Response child : resp) {
multi.add(child);
}
}
return multi;
});
addReducer(KEYS, list -> {
int total = 0;
for (Response resp : list) {
total += resp.size();
}
MultiType multi = MultiType.create(total, false);
for (Response resp : list) {
for (Response child : resp) {
multi.add(child);
}
}
return multi;
});
addReducer(FLUSHDB, list ->
// Simple string reply: always OK since FLUSHDB can't fail.
SimpleStringType.OK);
addReducer(DBSIZE, list ->
// Sum of key numbers on all Key Slots
NumberType.create(list.stream()
.mapToLong(el -> {
Long l = el.toLong();
if (l == null) {
return 0L;
} else {
return l;
}
}).sum()));
addMasterOnlyCommand(WAIT);
addMasterOnlyCommand(SUBSCRIBE);
addMasterOnlyCommand(PSUBSCRIBE);
addReducer(UNSUBSCRIBE, list -> SimpleStringType.OK);
addReducer(PUNSUBSCRIBE, list -> SimpleStringType.OK);
}
private final RedisClusterConnectOptions connectOptions;
private final PoolOptions poolOptions;
private final AtomicReference<Future<Slots>> slots = new AtomicReference<>();
public RedisClusterClient(Vertx vertx, NetClientOptions tcpOptions, PoolOptions poolOptions, RedisClusterConnectOptions connectOptions, TracingPolicy tracingPolicy) {
super(vertx, tcpOptions, poolOptions, connectOptions, tracingPolicy);
this.connectOptions = connectOptions;
this.poolOptions = poolOptions;
// validate options
if (poolOptions.getMaxWaiting() < poolOptions.getMaxSize()) {
throw new IllegalStateException("Invalid options: maxWaiting < maxSize");
// we can't validate the max pool size yet as we need to know the slots first
// the remaining validation will happen at the connect time
}
}
@Override
public Future<RedisConnection> connect() {
final Promise<RedisConnection> promise = vertx.promise();
getSlots(vertx.getOrCreateContext())
.onSuccess(slots -> connect(slots, promise))
.onFailure(promise::fail);
return promise.future();
}
private void connect(Slots slots, Handler<AsyncResult<RedisConnection>> onConnected) {
// validate if the pool config is valid
final int totalUniqueEndpoints = slots.endpoints().length;
if (poolOptions.getMaxSize() < totalUniqueEndpoints) {
// this isn't a valid setup, the connection pool will not accommodate all the required connections
onConnected.handle(Future.failedFuture("RedisOptions maxPoolSize < Cluster size(" + totalUniqueEndpoints + "): The pool is not able to hold all required connections!"));
return;
}
// create a cluster connection
final AtomicBoolean failed = new AtomicBoolean(false);
final AtomicInteger counter = new AtomicInteger();
final Map<String, PooledRedisConnection> connections = new HashMap<>();
for (String endpoint : slots.endpoints()) {
connectionManager.getConnection(endpoint, RedisReplicas.NEVER != connectOptions.getUseReplicas() ? cmd(READONLY) : null)
.onFailure(err -> {
// failed try with the next endpoint
failed.set(true);
connectionComplete(counter, slots, connections, failed, onConnected);
})
.onSuccess(cconn -> {
// there can be concurrent access to the connection map
// since this is a one time operation we can pay the penalty of
// synchronizing on each write (hopefully is only a few writes)
synchronized (connections) {
connections.put(endpoint, cconn);
}
connectionComplete(counter, slots, connections, failed, onConnected);
});
}
}
private void connectionComplete(AtomicInteger counter, Slots slots, Map<String, PooledRedisConnection> connections,
AtomicBoolean failed, Handler<AsyncResult<RedisConnection>> onConnected) {
if (counter.incrementAndGet() == slots.endpoints().length) {
// end condition
if (failed.get()) {
// cleanup
// during an error we lock the map because we will change it
// probably this isn't an issue as no more write should happen anyway
synchronized (connections) {
for (RedisConnection value : connections.values()) {
if (value != null) {
value.close().onFailure(LOG::warn);
}
}
}
// return
onConnected.handle(Future.failedFuture("Failed to connect to all nodes of the cluster"));
} else {
onConnected.handle(Future.succeededFuture(new RedisClusterConnection(vertx, connectOptions, slots,
() -> this.slots.set(null), connections)));
}
}
}
private Future<Slots> getSlots(ContextInternal context) {
while (true) {
Future<Slots> slots = this.slots.get();
if (slots != null) {
return slots;
}
Promise<Slots> promise = context.promise();
Future<Slots> future = promise.future();
if (this.slots.compareAndSet(null, future)) {
LOG.debug("Obtaining hash slot assignment");
// attempt to load the slots from the first good endpoint
getSlots(connectOptions.getEndpoints(), 0, promise);
return future;
}
}
}
private void getSlots(List<String> endpoints, int index, Handler<AsyncResult<Slots>> onGotSlots) {
if (index >= endpoints.size()) {
// stop condition
onGotSlots.handle(Future.failedFuture("Cannot connect to any of the provided endpoints"));
return;
}
connectionManager.getConnection(endpoints.get(index), RedisReplicas.NEVER != connectOptions.getUseReplicas() ? cmd(READONLY) : null)
.onFailure(err -> {
// try with the next endpoint
getSlots(endpoints, index + 1, onGotSlots);
})
.onSuccess(conn -> {
getSlots(endpoints.get(index), conn, result -> {
// the connection is not needed anymore, regardless of success or failure
// (on success, we just finish, on failure, we'll try another endpoint)
conn.close().onFailure(LOG::warn);
if (result.failed()) {
// the slots command failed, try with next endpoint
getSlots(endpoints, index + 1, onGotSlots);
} else {
Slots slots = result.result();
onGotSlots.handle(Future.succeededFuture(slots));
vertx.setTimer(connectOptions.getHashSlotCacheTTL(), ignored -> this.slots.set(null));
}
});
});
}
private void getSlots(String endpoint, RedisConnection conn, Handler<AsyncResult<Slots>> onGetSlots) {
conn.send(cmd(CLUSTER).arg("SLOTS"), send -> {
if (send.failed()) {
// failed to load the slots from this connection
onGetSlots.handle(Future.failedFuture(send.cause()));
return;
}
final Response reply = send.result();
if (reply == null || reply.size() == 0) {
// no slots available we can't really proceed
onGetSlots.handle(Future.failedFuture("SLOTS No slots available in the cluster."));
return;
}
onGetSlots.handle(Future.succeededFuture(new Slots(endpoint, reply)));
});
}
}