-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathRedisStandaloneConnection.java
534 lines (475 loc) · 16.6 KB
/
RedisStandaloneConnection.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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
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.buffer.Buffer;
import io.vertx.core.eventbus.EventBus;
import io.vertx.core.impl.ContextInternal;
import io.vertx.core.impl.VertxInternal;
import io.vertx.core.impl.future.PromiseInternal;
import io.vertx.core.impl.logging.Logger;
import io.vertx.core.impl.logging.LoggerFactory;
import io.vertx.core.json.JsonObject;
import io.vertx.core.net.NetSocket;
import io.vertx.core.net.impl.pool.PoolConnector;
import io.vertx.core.spi.metrics.ClientMetrics;
import io.vertx.core.tracing.TracingPolicy;
import io.vertx.redis.client.*;
import io.vertx.redis.client.impl.types.ErrorType;
import io.vertx.redis.client.impl.types.Multi;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
public class RedisStandaloneConnection implements RedisConnectionInternal, ParserHandler {
private static final String BASE_ADDRESS = "io.vertx.redis";
private static final Logger LOG = LoggerFactory.getLogger(RedisStandaloneConnection.class);
private static final ErrorType CONNECTION_CLOSED = ErrorType.create("CONNECTION_CLOSED");
private final PoolConnector.Listener listener;
// to be used for callbacks
private final VertxInternal vertx;
// to be used for callbacks
private final ContextInternal context;
private final EventBus eventBus;
private final NetSocket netSocket;
private final long expiresAt;
// waiting: commands that have been sent but not answered
// the queue is only accessed from the event loop
private final ArrayQueue waiting;
private final RedisURI uri;
private final ClientMetrics metrics;
private final TracingPolicy tracingPolicy;
// state
private Handler<Throwable> onException;
private Handler<Void> onEnd;
private Handler<Response> onMessage;
private Runnable onEvict;
private boolean closed = false;
private boolean tainted = false;
public RedisStandaloneConnection(VertxInternal vertx, ContextInternal context, PoolConnector.Listener connectionListener, NetSocket netSocket, PoolOptions options, int maxWaitingHandlers, RedisURI uri, ClientMetrics metrics, TracingPolicy tracingPolicy) {
//System.out.println("<ctor>#" + this.hashCode());
this.vertx = vertx;
this.context = context;
this.listener = connectionListener;
this.eventBus = vertx.eventBus();
this.netSocket = netSocket;
this.waiting = new ArrayQueue(maxWaitingHandlers);
this.expiresAt = options.getRecycleTimeout() == -1 ? -1 : System.currentTimeMillis() + options.getRecycleTimeout();
this.uri = uri;
this.metrics = metrics;
this.tracingPolicy = tracingPolicy;
}
synchronized void setValid() {
//System.out.println("setValid()#" + this.hashCode());
closed = false;
// tainted will be reset, as a select during the handshake could have
// changed the state
tainted = false;
}
@Override
public void forceClose() {
//System.out.println("forceClose()#" + this.hashCode());
// The socket will call the endHandler which in turn will flip the closed flag
netSocket.close();
}
@Override
public boolean isValid() {
//System.out.println("isValid()#" + this.hashCode());
return !closed && (expiresAt <= 0 || System.currentTimeMillis() < expiresAt);
}
@Override
public Future<Void> close() {
//System.out.println("close()#" + this.hashCode());
if (listener == null) {
// no pool is being used. The socket will call the endHandler which in turn will flip the closed flag
return netSocket.close();
} else {
// Pooled mode, we don't really close the socket, just mark the connection as closed.
synchronized (this) {
closed = true;
}
return Future.succeededFuture();
}
}
@Override
public boolean pendingQueueFull() {
//System.out.println("pendingQueueFull()#" + this.hashCode());
synchronized (waiting) {
return waiting.isFull();
}
}
@Override
public RedisConnection exceptionHandler(Handler<Throwable> handler) {
//System.out.println("exceptionHandler()#" + this.hashCode());
this.onException = handler;
return this;
}
@Override
public RedisConnection endHandler(Handler<Void> handler) {
//System.out.println("endHandler()#" + this.hashCode());
this.onEnd = handler;
return this;
}
RedisConnection evictHandler(Runnable handler) {
//System.out.println("evictHandler()#" + this.hashCode());
this.onEvict = handler;
return this;
}
@Override
public RedisConnection handler(Handler<Response> handler) {
//System.out.println("handler()#" + this.hashCode());
this.onMessage = handler;
return this;
}
@Override
public RedisConnection pause() {
//System.out.println("pause()#" + this.hashCode());
netSocket.pause();
return this;
}
@Override
public RedisConnection resume() {
//System.out.println("resume()#" + this.hashCode());
netSocket.resume();
return this;
}
@Override
public RedisConnection fetch(long size) {
//System.out.println("fetch()#" + this.hashCode());
netSocket.fetch(size);
return this;
}
/**
* Checks if an executed command has tainted the connection. A connection is tainted if it changes the default state,
* for example, when a connection enters pub sub mode, or specific features are activated such as changing a database
* or different authentication is used.
* <p>
* This is only relevant for pooled connections
*/
private void taintCheck(CommandImpl cmd) {
//System.out.println("taintCheck()#" + this.hashCode());
if (listener != null) {
if (cmd.isPubSub() || Command.SELECT.equals(cmd) || Command.AUTH.equals(cmd)) {
tainted = true;
}
}
}
@Override
public Future<Response> send(final Request request) {
//System.out.println("send()#" + this.hashCode());
final Promise<Response> promise;
if (closed) {
throw new IllegalStateException("Connection is closed");
}
if (!((RequestImpl) request).valid()) {
return Future.failedFuture("Redis command is not valid, check https://redis.io/commands");
}
final CommandImpl cmd = (CommandImpl) request.command();
// tag this connection as tainted if needed
context.execute(cmd, this::taintCheck);
final boolean voidCmd = cmd.isPubSub();
// encode the message to a buffer
final Buffer message = ((RequestImpl) request).encode();
// offer the handler to the waiting queue if not void command
if (!voidCmd) {
// we might have switch thread/context
synchronized (waiting) {
if (waiting.isFull()) {
return Future.failedFuture("Redis waiting Queue is full");
}
// create a new promise bound to the caller not
// the instance of this object (a.k.a. "context")
promise = vertx.promise();
waiting.offer(promise);
}
} else {
// create a new promise bound to the caller not
// the instance of this object (a.k.a. "context")
promise = vertx.promise();
}
// write to the socket
try {
netSocket.write(message)
// if the write fails, this connection enters a unknown state
// which means it should be terminated
.onFailure(this::fail)
.onSuccess(ok -> {
if (voidCmd) {
// only on this case notify the promise
if (!promise.tryComplete()) {
// if the promise fail (e.g.: an client error forced a cleanup)
// call the exception handler if any
if (onException != null) {
context.execute(new IllegalStateException("Result is already complete: [" + promise + "]"), onException);
}
}
}
});
} catch (RuntimeException err) {
// is the socket in a broken state?
context.execute(err, this::fail);
promise.fail(err);
}
return promise.future();
}
@Override
public Future<List<Response>> batch(List<Request> commands) {
//System.out.println("batch()#" + this.hashCode());
if (closed) {
throw new IllegalStateException("Connection is closed");
}
if (commands.isEmpty()) {
LOG.debug("Empty batch");
return Future.succeededFuture(Collections.emptyList());
} else {
// create a new promise bound to the caller not
// the instance of this object (a.k.a. "context")
final Promise<List<Response>> promise = vertx.promise();
// will re-encode the handler into a list of promises
final List<Promise<Response>> callbacks = new ArrayList<>(commands.size());
final Response[] replies = new Response[commands.size()];
final AtomicInteger count = new AtomicInteger(commands.size());
final StringBuilder errorMsg = new StringBuilder();
// encode the message to a single buffer
final Buffer messages = Buffer.buffer();
for (int i = 0; i < commands.size(); i++) {
final int index = i;
final RequestImpl req = (RequestImpl) commands.get(index);
final CommandImpl cmd = (CommandImpl) req.command();
if (!req.valid()) {
return Future.failedFuture("Redis command is not valid, check https://redis.io/commands");
}
if (cmd.isPubSub()) {
// mixing pubSub cannot be used on a one-shot operation
return Future.failedFuture("PubSub command in batch not allowed");
}
// encode to the single buffer
req.encode(messages);
// tag this connection as tainted if needed
taintCheck(cmd);
// unwrap the handler into a single handler
callbacks.add(index, vertx.promise(command -> {
if (command.failed()) {
if (errorMsg.length() > 0) {
errorMsg.append(System.lineSeparator());
}
String cause = null;
if (command.cause() != null) {
cause = command.cause().toString();
if (cause != null) {
if (cause.startsWith("ERR ")) {
// strip the ERR prefix
cause = cause.substring(4);
}
}
}
// the message rewrite is just to comply to the redis error message contract
errorMsg.append("ERR [").append(index).append("] ").append(cause);
} else {
// set the reply
replies[index] = command.result();
}
if (count.decrementAndGet() == 0) {
// all results have arrived
boolean resolved = errorMsg.length() > 0 ?
promise.tryFail(ErrorType.create(errorMsg.toString())) :
promise.tryComplete(Arrays.asList(replies));
if (!resolved) {
// if the promise fail (e.g.: an client error forced a cleanup)
// call the exception handler if any
if (onException != null) {
context.execute(new IllegalStateException("Result is already complete: [" + promise + "]"), onException);
}
}
}
}));
}
synchronized (waiting) {
// we might have switch thread/context
// this means the check needs to be performed again
if (waiting.freeSlots() < callbacks.size()) {
return Future.failedFuture("Redis waiting Queue is full");
}
// offer all handlers to the waiting queue
for (Promise<Response> callback : callbacks) {
waiting.offer(callback);
}
}
// write to the socket
try {
netSocket.write(messages)
// if the write fails, this connection enters an unknown state
// which means it should be terminated
.onFailure(this::fail);
} catch (RuntimeException err) {
// is the socket in a broken state?
context.execute(err, this::fail);
promise.fail(err);
}
return promise.future();
}
}
@Override
public void handle(Response reply) {
//System.out.println("handle()#" + this.hashCode());
final boolean empty;
final Promise<Response> req;
synchronized (waiting) {
empty = waiting.isEmpty();
if (!empty) {
req = waiting.poll();
} else {
req = null;
}
}
// pub/sub mode
if ((reply != null && reply.type() == ResponseType.PUSH) || empty) {
if (onMessage != null) {
context.execute(reply, onMessage);
} else {
// pub/sub messages are arrays
if (reply instanceof Multi) {
// Detect valid published messages according to https://redis.io/topics/pubsub
if (reply.size() == 3 && "message".equals(reply.get(0).toString())) {
// channel
eventBus.send(
BASE_ADDRESS + "." + reply.get(1).toString(),
new JsonObject()
.put("status", "OK")
.put("value", new JsonObject()
.put("channel", reply.get(1).toString())
.put("message", reply.get(2).toString())));
return;
}
if (reply.size() == 4 && "pmessage".equals(reply.get(0).toString())) {
// pattern
eventBus.send(
BASE_ADDRESS + "." + reply.get(1).toString(),
new JsonObject()
.put("status", "OK")
.put("value", new JsonObject()
.put("pattern", reply.get(1).toString())
.put("channel", reply.get(2).toString())
.put("message", reply.get(3).toString())));
return;
}
// fallback will just go to the log
}
LOG.warn("No handler waiting for message: " + reply);
}
return;
}
if (req != null) {
final boolean resolved;
if (reply == null) {
// special case (nulls are always a success)
// the reason is that nil is only a valid value for
// bulk or multi
resolved = req.tryComplete();
} else {
resolved = reply.type() == ResponseType.ERROR ?
req.tryFail((ErrorType) reply) :
req.tryComplete(reply);
}
if (!resolved) {
// call the exception handler if any
if (onException != null) {
context.execute(new IllegalStateException("Result is already complete: [" + req + "]"), onException);
}
}
} else {
LOG.error("No handler waiting for message: " + reply);
}
}
public synchronized void end(Void v) {
//System.out.println("end()#" + this.hashCode());
assert !closed;
closed = true;
// evict this connection from the pool
evict();
// clean up the pending queue
cleanupQueue(CONNECTION_CLOSED);
// call the end handler if any
if (onEnd != null) {
context.execute(v, onEnd);
}
}
@Override
public synchronized void fail(Throwable t) {
//System.out.println("fail()#" + this.hashCode());
assert !closed;
closed = true;
// evict this connection from the pool
evict();
// if there are still "on going" requests
// these are all cancelled with the given
// throwable
cleanupQueue(t);
// call the exception handler if any
if (onException != null) {
context.execute(t, onException);
}
}
@Override
public synchronized boolean reset() {
//System.out.println("reset()#" + this.hashCode());
// cannot reset if connection is already closed
if (closed) {
return false;
}
// cannot reset if connection is tainted (custom DB/AUTH/PUBSUB)
if (tainted) {
evict();
forceClose();
return false;
}
return true;
}
private void evict() {
//System.out.println("evict()#" + this.hashCode());
// evict this connection from the pool
if (listener != null) {
listener.onRemove();
}
if (onEvict != null) {
onEvict.run();
}
}
private synchronized void cleanupQueue(Throwable t) {
//System.out.println("cleanupQueue()#" + this.hashCode());
Promise<Response> req;
synchronized (waiting) {
while ((req = waiting.poll()) != null) {
if (req instanceof PromiseInternal) {
if (((PromiseInternal<?>) req).isComplete()) {
// skip if already resolved
continue;
}
}
try {
req.tryFail(t);
} catch (RuntimeException err) {
LOG.warn("Exception while running cleanup", err);
}
}
}
}
@Override
public boolean isTainted() {
return tainted;
}
@Override
public VertxInternal vertx() {
return vertx;
}
@Override
public RedisURI uri() {
return uri;
}
@Override
public ClientMetrics metrics() {
return metrics;
}
@Override
public TracingPolicy tracingPolicy() {
return tracingPolicy;
}
}