-
Notifications
You must be signed in to change notification settings - Fork 116
/
RedisConnectionManager.java
410 lines (364 loc) · 15.5 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
/*
* 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.Future;
import io.vertx.core.Handler;
import io.vertx.core.Promise;
import io.vertx.core.internal.ContextInternal;
import io.vertx.core.internal.VertxInternal;
import io.vertx.core.internal.logging.Logger;
import io.vertx.core.internal.logging.LoggerFactory;
import io.vertx.core.net.ConnectOptions;
import io.vertx.core.net.NetClientOptions;
import io.vertx.core.net.NetSocket;
import io.vertx.core.net.impl.NetClientInternal;
import io.vertx.core.net.impl.endpoint.EndpointManager;
import io.vertx.core.net.impl.endpoint.EndpointProvider;
import io.vertx.core.net.impl.endpoint.Endpoint;
import io.vertx.core.internal.pool.*;
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 implements EndpointProvider<RedisConnectionManager.ConnectionKey, RedisConnectionManager.RedisEndpoint> {
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 NetClientInternal netClient;
private final PoolMetrics metrics;
private final NetClientOptions tcpOptions;
private final PoolOptions poolOptions;
private final RedisConnectOptions connectOptions;
private final TracingPolicy tracingPolicy;
private final EndpointManager<ConnectionKey, RedisEndpoint> pooledConnectionManager;
private long timerID;
RedisConnectionManager(VertxInternal vertx, NetClientOptions tcpOptions, PoolOptions poolOptions, RedisConnectOptions connectOptions, TracingPolicy tracingPolicy) {
this.vertx = vertx;
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 = (NetClientInternal) vertx.createNetClient(tcpOptions);
this.pooledConnectionManager = new EndpointManager<>();
}
private RedisEndpoint connectionEndpointProvider(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 NetClientInternal 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, NetClientInternal 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 Future<ConnectResult<RedisConnectionInternal>> connect(ContextInternal ctx, Listener listener) {
// 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) {
return ctx.failedFuture("Pool initialized with SSL but connection requested plain socket");
}
}
// all calls the user handler will happen in the user context (ctx)
return connectAndSetup(ctx, listener, connectionStringInetSocket, connectionStringSsl, netClientSsl);
}
private Future<ConnectResult<RedisConnectionInternal>> connectAndSetup(
ContextInternal ctx,
Listener listener,
boolean connectionStringInetSocket,
boolean connectionStringSsl,
boolean netClientSsl) {
try {
ConnectOptions connectOptions = new ConnectOptions()
.setRemoteAddress(redisURI.socketAddress())
.setSsl(netClientOptions.isSsl())
.setSslOptions(netClientOptions.getSslOptions());
Promise<NetSocket> promise = ctx.promise();
netClient.connectInternal(connectOptions, promise, ctx);
return promise.future()
.compose(so -> {
// upgrade to ssl is only possible for inet sockets
if (connectionStringInetSocket && !netClientSsl && connectionStringSsl) {
// must upgrade protocol
return so
.upgradeToSsl()
.compose(v -> init(ctx, so, listener));
} else {
// no need to upgrade
return init(ctx, so, listener);
}
});
} catch (RuntimeException err) {
// the netClient is in a closed state?
return ctx.failedFuture(err);
}
}
private Future<ConnectResult<RedisConnectionInternal>> init(ContextInternal ctx, NetSocket netSocket, PoolConnector.Listener connectionListener) {
// 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
return hello(ctx, connection, redisURI)
.compose(hello -> {
// perform select
return select(ctx, connection, redisURI.select());
}).compose(select -> {
// perform setup
return setup(ctx, connection, setup);
}).map(setup -> {
// connection is valid
connection.setValid();
return new ConnectResult<>(connection, 1, 0);
});
}
private Future<Void> hello(ContextInternal ctx, RedisConnection connection, RedisURI redisURI) {
if (!options.isProtocolNegotiation()) {
return ping(ctx, connection);
} else {
String version = RESPParser.VERSION;
if (options.getPreferredProtocolVersion() != null) {
version = options.getPreferredProtocolVersion().getValue();
}
Request hello = Request.cmd(Command.HELLO).arg(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);
}
return connection
.send(hello)
.<Void>mapEmpty()
.transform(ar -> {
if (ar.failed()) {
Throwable err = ar.cause();
if (err instanceof ErrorType) {
final ErrorType redisErr = (ErrorType) err;
if (redisErr.is("NOAUTH")) {
return authenticate(ctx, connection, user, password);
}
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
return ping(ctx, connection);
}
}
}
} else {
LOG.debug(ar.result());
}
return (Future<Void>) ar;
});
}
}
private Future<Void> ping(ContextInternal ctx, RedisConnection connection) {
Request ping = Request.cmd(Command.PING);
return connection
.send(ping)
.onSuccess(LOG::debug)
.transform(ar -> {
if (ar.failed()) {
Throwable err = ar.cause();
if (err instanceof ErrorType) {
if (((ErrorType) err).is("NOAUTH")) {
// old authentication required
String password = redisURI.password() != null ? redisURI.password() : options.getPassword();
return this.authenticate(ctx, connection, redisURI.user(), password);
}
}
}
return ((Future<Response>) ar).mapEmpty();
});
}
private Future<Void> authenticate(ContextInternal ctx, RedisConnection connection, String user, String password) {
if (password == null) {
return ctx.succeededFuture();
}
// 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);
return connection.send(cmd).mapEmpty();
}
private Future<Void> select(ContextInternal ctx, RedisConnection connection, Integer select) {
if (select == null) {
return ctx.succeededFuture();
}
// perform select
return connection
.send(Request.cmd(Command.SELECT).arg(select))
.mapEmpty();
}
private Future<Void> setup(ContextInternal ctx, RedisConnection connection, Request setup) {
if (setup == null) {
return ctx.succeededFuture();
}
// perform setup
return connection
.send(setup)
.mapEmpty();
}
}
@Override
public RedisEndpoint create(ConnectionKey key, Runnable dispose) {
return connectionEndpointProvider(dispose, key.string, key.setup);
}
public Future<PooledRedisConnection> getConnection(String connectionString, Request setup) {
final ContextInternal context = vertx.getOrCreateContext();
final ContextInternal eventLoopContext;
if (context.isEventLoopContext()) {
eventLoopContext = context;
} else {
eventLoopContext = vertx.createEventLoopContext(context.nettyEventLoop(), context.workerPool(), context.classLoader());
}
final boolean metricsEnabled = metrics != null;
final Object queueMetric = metricsEnabled ? metrics.submitted() : null;
Future<Lease<RedisConnectionInternal>> future = pooledConnectionManager.withEndpointAsync(new ConnectionKey(connectionString, setup), this, (endpoint, created) -> {
return endpoint.requestConnection(eventLoopContext);
});
return 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 {
final ConnectionPool<RedisConnectionInternal> pool;
public RedisEndpoint(VertxInternal vertx, NetClientInternal 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());
}
public Future<Lease<RedisConnectionInternal>> requestConnection(ContextInternal ctx) {
Promise<Lease<RedisConnectionInternal>> promise = ctx.promise();
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
promise.handle(ar);
});
return promise.future();
}
}
}