-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathRedisConnectionManager.java
442 lines (387 loc) · 16.4 KB
/
RedisConnectionManager.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*
* 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.EventLoopContext;
import io.vertx.core.impl.VertxInternal;
import io.vertx.core.impl.logging.Logger;
import io.vertx.core.impl.logging.LoggerFactory;
import io.vertx.core.net.NetClient;
import io.vertx.core.net.NetClientOptions;
import io.vertx.core.net.NetSocket;
import io.vertx.core.net.impl.pool.ConnectResult;
import io.vertx.core.net.impl.pool.ConnectionManager;
import io.vertx.core.net.impl.pool.Endpoint;
import io.vertx.core.net.impl.pool.Lease;
import io.vertx.core.net.impl.pool.ConnectionPool;
import io.vertx.core.net.impl.pool.PoolConnector;
import io.vertx.core.spi.metrics.ClientMetrics;
import io.vertx.core.spi.metrics.PoolMetrics;
import io.vertx.core.spi.metrics.VertxMetrics;
import io.vertx.core.tracing.TracingPolicy;
import io.vertx.redis.client.*;
import io.vertx.redis.client.impl.types.ErrorType;
import java.util.Objects;
class RedisConnectionManager {
private static final Logger LOG = LoggerFactory.getLogger(RedisConnectionManager.class);
private static final Handler<Throwable> DEFAULT_EXCEPTION_HANDLER = t -> LOG.error("Unhandled Error", t);
private final VertxInternal vertx;
private final ContextInternal context;
private final NetClient netClient;
private final PoolMetrics metrics;
private final NetClientOptions tcpOptions;
private final PoolOptions poolOptions;
private final RedisConnectOptions connectOptions;
private final TracingPolicy tracingPolicy;
private final ConnectionManager<ConnectionKey, Lease<RedisConnectionInternal>> pooledConnectionManager;
private long timerID;
RedisConnectionManager(VertxInternal vertx, NetClientOptions tcpOptions, PoolOptions poolOptions, RedisConnectOptions connectOptions, TracingPolicy tracingPolicy) {
this.vertx = vertx;
this.context = vertx.getOrCreateContext();
this.tcpOptions = tcpOptions;
this.poolOptions = poolOptions;
this.connectOptions = connectOptions;
this.tracingPolicy = tracingPolicy;
VertxMetrics metricsSPI = this.vertx.metricsSPI();
metrics = metricsSPI != null ? metricsSPI.createPoolMetrics("redis", poolOptions.getName(), poolOptions.getMaxSize()) : null;
this.netClient = vertx.createNetClient(tcpOptions);
this.pooledConnectionManager = new ConnectionManager<>();
}
private Endpoint<Lease<RedisConnectionInternal>> connectionEndpointProvider(ContextInternal ctx, Runnable dispose, String connectionString, Request setup) {
return new RedisEndpoint(vertx, netClient, tcpOptions, poolOptions, connectOptions, tracingPolicy, dispose, connectionString, setup);
}
synchronized void start() {
long period = poolOptions.getCleanerInterval();
this.timerID = period > 0 ? vertx.setTimer(period, id -> checkExpired(period)) : -1;
}
private void checkExpired(long period) {
pooledConnectionManager.forEach(e ->
((RedisEndpoint) e).pool.evict(conn -> !conn.isValid(), ar -> {
if (ar.succeeded()) {
for (RedisConnectionInternal conn : ar.result()) {
// on close we reset the default handlers
conn.handler(null);
conn.endHandler(null);
conn.exceptionHandler(null);
conn.forceClose();
}
}
}));
timerID = vertx.setTimer(period, id -> checkExpired(period));
}
static class ConnectionKey {
private final String string;
private final Request setup;
ConnectionKey(String string, Request setup) {
this.string = string;
this.setup = setup;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ConnectionKey that = (ConnectionKey) o;
return Objects.equals(string, that.string) && Objects.equals(setup, that.setup);
}
@Override
public int hashCode() {
return Objects.hash(string, setup);
}
}
static class RedisConnectionProvider implements PoolConnector<RedisConnectionInternal> {
private final VertxInternal vertx;
private final NetClient netClient;
private final RedisURI redisURI;
private final Request setup;
private final NetClientOptions netClientOptions;
private final PoolOptions poolOptions;
private final RedisConnectOptions options;
private final TracingPolicy tracingPolicy;
public RedisConnectionProvider(VertxInternal vertx, NetClient netClient, NetClientOptions netClientOptions, PoolOptions poolOptions, RedisConnectOptions options, TracingPolicy tracingPolicy, String connectionString, Request setup) {
this.vertx = vertx;
this.netClient = netClient;
this.netClientOptions = netClientOptions;
this.poolOptions = poolOptions;
this.options = options;
this.tracingPolicy = tracingPolicy;
this.redisURI = new RedisURI(connectionString);
this.setup = setup;
}
@Override
public boolean isValid(RedisConnectionInternal conn) {
return conn.isValid();
}
@Override
public void connect(EventLoopContext ctx, Listener listener, Handler<AsyncResult<ConnectResult<RedisConnectionInternal>>> onConnect) {
// verify if we can make this connection
final boolean netClientSsl = netClientOptions.isSsl();
final boolean connectionStringSsl = redisURI.ssl();
final boolean connectionStringInetSocket = redisURI.socketAddress().isInetSocket();
// when dealing with sockets, ssl is only covered in case of inet sockets
// not domain sockets
if (connectionStringInetSocket) {
// net client is ssl and connection string is not ssl is not allowed
if (netClientSsl && !connectionStringSsl) {
ctx.execute(ctx.failedFuture("Pool initialized with SSL but connection requested plain socket"), onConnect);
return;
}
}
// all calls the user handler will happen in the user context (ctx)
try {
netClient
.connect(redisURI.socketAddress(), clientConnect -> {
if (clientConnect.failed()) {
// connection failed
ctx.execute(ctx.failedFuture(clientConnect.cause()), onConnect);
return;
}
// socket connection succeeded
final NetSocket netSocket = clientConnect.result();
// upgrade to ssl is only possible for inet sockets
if (connectionStringInetSocket && !netClientSsl && connectionStringSsl) {
// must upgrade protocol
netSocket.upgradeToSsl(upgradeToSsl -> {
if (upgradeToSsl.failed()) {
ctx.execute(ctx.failedFuture(upgradeToSsl.cause()), onConnect);
} else {
// complete the connection
init(ctx, netSocket, listener, onConnect);
}
});
} else {
// no need to upgrade
init(ctx, netSocket, listener, onConnect);
}
});
} catch (RuntimeException err) {
// the netClient is in a closed state?
ctx.execute(ctx.failedFuture(err), onConnect);
}
}
private void init(ContextInternal ctx, NetSocket netSocket, PoolConnector.Listener connectionListener, Handler<AsyncResult<ConnectResult<RedisConnectionInternal>>> onConnect) {
// the connection will inherit the user event loop context
VertxMetrics vertxMetrics = vertx.metricsSPI();
ClientMetrics metrics = vertxMetrics != null
? vertxMetrics.createClientMetrics(redisURI.socketAddress(), "redis", netClientOptions.getMetricsName())
: null;
final RedisStandaloneConnection connection = new RedisStandaloneConnection(vertx, ctx, connectionListener, netSocket, poolOptions, options.getMaxWaitingHandlers(), redisURI, metrics, tracingPolicy);
// initialization
connection.exceptionHandler(DEFAULT_EXCEPTION_HANDLER);
// parser utility
netSocket
.handler(new RESPParser(connection, options.getMaxNestedArrays()))
.closeHandler(connection::end)
.exceptionHandler(connection::fail);
// initial handshake
hello(ctx, connection, redisURI, hello -> {
if (hello.failed()) {
ctx.execute(ctx.failedFuture(hello.cause()), onConnect);
return;
}
// perform select
select(ctx, connection, redisURI.select(), select -> {
if (select.failed()) {
ctx.execute(ctx.failedFuture(select.cause()), onConnect);
return;
}
// perform setup
setup(ctx, connection, setup, setupResult -> {
if (setupResult.failed()) {
ctx.execute(ctx.failedFuture(setupResult.cause()), onConnect);
return;
}
// connection is valid
connection.setValid();
ctx.execute(ctx.succeededFuture(new ConnectResult<>(connection, 1, 0)), onConnect);
});
});
});
}
private void hello(ContextInternal ctx, RedisConnection connection, RedisURI redisURI, Handler<AsyncResult<Void>> handler) {
if (!options.isProtocolNegotiation()) {
ping(ctx, connection, handler);
} else {
Request hello = Request.cmd(Command.HELLO).arg(RESPParser.VERSION);
String password = redisURI.password() != null ? redisURI.password() : options.getPassword();
String user = redisURI.user();
if (password != null) {
// will perform auth at hello level
hello
.arg("AUTH")
.arg(user == null ? "default" : user)
.arg(password);
}
String client = redisURI.param("client");
if (client != null) {
hello.arg("SETNAME").arg(client);
}
connection.send(hello, onSend -> {
if (onSend.succeeded()) {
LOG.debug(onSend.result());
ctx.execute(ctx.succeededFuture(), handler);
return;
}
final Throwable err = onSend.cause();
if (err != null) {
if (err instanceof ErrorType) {
final ErrorType redisErr = (ErrorType) err;
if (redisErr.is("NOAUTH")) {
authenticate(ctx, connection, user, password, handler);
return;
}
if (redisErr.is("ERR")) {
String msg = redisErr.getMessage();
if (msg.startsWith("ERR unknown command") || msg.startsWith("ERR unknown or unsupported command")) {
// chatting to an old server
ping(ctx, connection, handler);
}
return;
}
}
}
ctx.execute(ctx.failedFuture(err), handler);
});
}
}
private void ping(ContextInternal ctx, RedisConnection connection, Handler<AsyncResult<Void>> handler) {
Request ping = Request.cmd(Command.PING);
connection.send(ping, onSend -> {
if (onSend.succeeded()) {
LOG.debug(onSend.result());
ctx.execute(ctx.succeededFuture(), handler);
return;
}
final Throwable err = onSend.cause();
if (err != null) {
if (err instanceof ErrorType) {
if (((ErrorType) err).is("NOAUTH")) {
// old authentication required
String password = redisURI.password() != null ? redisURI.password() : options.getPassword();
authenticate(ctx, connection, redisURI.user(), password, handler);
return;
}
}
}
ctx.execute(ctx.failedFuture(err), handler);
});
}
private void authenticate(ContextInternal ctx, RedisConnection connection, String user, String password, Handler<AsyncResult<Void>> handler) {
if (password == null) {
ctx.execute(ctx.succeededFuture(), handler);
return;
}
// perform authentication
final Request cmd = Request.cmd(Command.AUTH);
// when working with ACLs (Redis >= 6) we may use usernames
if (user != null) {
cmd.arg(user);
}
cmd.arg(password);
connection.send(cmd, auth -> {
if (auth.failed()) {
ctx.execute(ctx.failedFuture(auth.cause()), handler);
} else {
ctx.execute(ctx.succeededFuture(), handler);
}
});
}
private void select(ContextInternal ctx, RedisConnection connection, Integer select, Handler<AsyncResult<Void>> handler) {
if (select == null) {
ctx.execute(ctx.succeededFuture(), handler);
return;
}
// perform select
connection.send(Request.cmd(Command.SELECT).arg(select), auth -> {
if (auth.failed()) {
ctx.execute(ctx.failedFuture(auth.cause()), handler);
} else {
ctx.execute(ctx.succeededFuture(), handler);
}
});
}
private void setup(ContextInternal ctx, RedisConnection connection, Request setup, Handler<AsyncResult<Void>> handler) {
if (setup == null) {
ctx.execute(ctx.succeededFuture(), handler);
return;
}
// perform setup
connection.send(setup, req -> {
if (req.failed()) {
ctx.execute(ctx.failedFuture(req.cause()), handler);
} else {
ctx.execute(ctx.succeededFuture(), handler);
}
});
}
}
public Future<PooledRedisConnection> getConnection(String connectionString, Request setup) {
final Promise<Lease<RedisConnectionInternal>> promise = vertx.promise();
final EventLoopContext eventLoopContext;
if (context instanceof EventLoopContext) {
eventLoopContext = (EventLoopContext) context;
} else {
eventLoopContext = vertx.createEventLoopContext(context.nettyEventLoop(), context.workerPool(), context.classLoader());
}
final boolean metricsEnabled = metrics != null;
final Object queueMetric = metricsEnabled ? metrics.submitted() : null;
pooledConnectionManager.getConnection(eventLoopContext, new ConnectionKey(connectionString, setup), (ctx, dispose) -> connectionEndpointProvider(ctx, dispose, connectionString, setup), promise);
return promise.future()
.onFailure(err -> {
if (metricsEnabled) {
metrics.rejected(queueMetric);
}
})
.compose(lease -> Future.succeededFuture(new PooledRedisConnection(lease, metrics, metricsEnabled ? metrics.begin(queueMetric) : null)));
}
public void close() {
synchronized (this) {
if (timerID >= 0) {
vertx.cancelTimer(timerID);
timerID = -1;
}
}
pooledConnectionManager.close();
netClient.close();
if (metrics != null) {
metrics.close();
}
}
static class RedisEndpoint extends Endpoint<Lease<RedisConnectionInternal>> {
final ConnectionPool<RedisConnectionInternal> pool;
public RedisEndpoint(VertxInternal vertx, NetClient netClient, NetClientOptions netClientOptions, PoolOptions poolOptions, RedisConnectOptions connectOptions, TracingPolicy tracingPolicy, Runnable dispose, String connectionString, Request setup) {
super(dispose);
PoolConnector<RedisConnectionInternal> connector = new RedisConnectionProvider(vertx, netClient, netClientOptions, poolOptions, connectOptions, tracingPolicy, connectionString, setup);
pool = ConnectionPool.pool(connector, new int[]{poolOptions.getMaxSize()}, poolOptions.getMaxWaiting());
}
@Override
public void requestConnection(ContextInternal ctx, long timeout, Handler<AsyncResult<Lease<RedisConnectionInternal>>> handler) {
pool.acquire(ctx, 0, ar -> {
if (ar.succeeded()) {
// increment the reference counter to avoid the pool to be closed too soon
// once there are no more connections the pool is collected, so this counter needs
// to be as up to date as possible.
incRefCount();
// Integration between endpoint/pool and the standalone connection
((RedisStandaloneConnection) ar.result().get())
.evictHandler(this::decRefCount);
}
// proceed to user
handler.handle(ar);
});
}
}
}