-
Notifications
You must be signed in to change notification settings - Fork 13
/
SubscriptionWorker.java
762 lines (644 loc) · 33.7 KB
/
SubscriptionWorker.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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
package net.ravendb.client.documents.subscriptions;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.TreeNode;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import net.ravendb.client.documents.DocumentStore;
import net.ravendb.client.documents.commands.GetTcpInfoForRemoteTaskCommand;
import net.ravendb.client.exceptions.AllTopologyNodesDownException;
import net.ravendb.client.exceptions.ClientVersionMismatchException;
import net.ravendb.client.exceptions.InvalidNetworkTopologyException;
import net.ravendb.client.exceptions.cluster.NodeIsPassiveException;
import net.ravendb.client.exceptions.database.DatabaseDoesNotExistException;
import net.ravendb.client.exceptions.documents.subscriptions.*;
import net.ravendb.client.exceptions.security.AuthorizationException;
import net.ravendb.client.extensions.JsonExtensions;
import net.ravendb.client.http.RequestExecutor;
import net.ravendb.client.http.ServerNode;
import net.ravendb.client.primitives.*;
import net.ravendb.client.serverwide.commands.GetTcpInfoCommand;
import net.ravendb.client.serverwide.commands.TcpConnectionInfo;
import net.ravendb.client.serverwide.tcp.TcpConnectionHeaderMessage;
import net.ravendb.client.serverwide.tcp.TcpConnectionHeaderResponse;
import net.ravendb.client.serverwide.tcp.TcpNegotiateParameters;
import net.ravendb.client.serverwide.tcp.TcpNegotiation;
import net.ravendb.client.util.TcpUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.IOException;
import java.net.Socket;
import java.security.GeneralSecurityException;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import java.util.stream.Collectors;
public class SubscriptionWorker<T> implements CleanCloseable {
private final Class<T> _clazz;
private final boolean _revisions;
private final Log _logger;
private final DocumentStore _store;
private final String _dbName;
private final CancellationTokenSource _processingCts = new CancellationTokenSource();
private final SubscriptionWorkerOptions _options;
private Consumer<SubscriptionBatch<T>> _subscriber;
private Socket _tcpClient;
private JsonParser _parser;
private boolean _disposed;
private CompletableFuture<Void> _subscriptionTask;
private int _forcedTopologyUpdateAttempts = 0;
private List<Consumer<SubscriptionBatch<T>>> afterAcknowledgment;
private List<Consumer<Exception>> onSubscriptionConnectionRetry;
private List<Consumer<Exception>> onUnexpectedSubscriptionError;
public void addAfterAcknowledgmentListener(Consumer<SubscriptionBatch<T>> handler) {
afterAcknowledgment.add(handler);
}
public void removeAfterAcknowledgmentListener(Consumer<SubscriptionBatch<T>> handler) {
afterAcknowledgment.remove(handler);
}
public void addOnSubscriptionConnectionRetry(Consumer<Exception> handler) {
onSubscriptionConnectionRetry.add(handler);
}
public void removeOnSubscriptionConnectionRetry(Consumer<Exception> handler) {
onSubscriptionConnectionRetry.remove(handler);
}
public void addOnUnexpectedSubscriptionError(Consumer<Exception> handler) {
onUnexpectedSubscriptionError.add(handler);
}
public void removeOnUnexpectedSubscriptionError(Consumer<Exception> handler) {
onUnexpectedSubscriptionError.remove(handler);
}
@SuppressWarnings("unchecked")
SubscriptionWorker(Class<?> clazz, SubscriptionWorkerOptions options, boolean withRevisions, DocumentStore documentStore, String dbName) {
_clazz = (Class<T>) clazz;
_options = options;
_revisions = withRevisions;
if (StringUtils.isEmpty(options.getSubscriptionName())) {
throw new IllegalArgumentException("SubscriptionConnectionOptions must specify the subscriptionName");
}
_store = documentStore;
_dbName = documentStore.getEffectiveDatabase(dbName);
_logger = LogFactory.getLog(SubscriptionWorker.class);
afterAcknowledgment = new ArrayList<>();
onSubscriptionConnectionRetry = new ArrayList<>();
onUnexpectedSubscriptionError = new ArrayList<>();
}
@Override
public void close() {
close(true);
}
public void close(boolean waitForSubscriptionTask) {
if (_disposed) {
return;
}
try {
_disposed = true;
_processingCts.cancel();
closeTcpClient(); // we disconnect immediately
if (_subscriptionTask != null && waitForSubscriptionTask) {
try {
_subscriptionTask.get();
} catch (Exception e) {
// just need to wait for it to end
}
}
if (_subscriptionLocalRequestExecutor != null) {
_subscriptionLocalRequestExecutor.close();
}
} catch (Exception ex) {
if (_logger.isDebugEnabled()) {
_logger.debug("Error during close of subscription: " + ex.getMessage(), ex);
}
} finally {
if (onClosed != null) {
onClosed.accept(this);
}
}
}
public CompletableFuture<Void> run(Consumer<SubscriptionBatch<T>> processDocuments) {
if (processDocuments == null) {
throw new IllegalArgumentException("ProcessDocuments cannot be null");
}
_subscriber = processDocuments;
return run();
}
private CompletableFuture<Void> run() {
if (_subscriptionTask != null) {
throw new IllegalStateException("The subscription is already running");
}
return _subscriptionTask = runSubscriptionAsync();
}
private ServerNode _redirectNode;
private RequestExecutor _subscriptionLocalRequestExecutor;
public String getCurrentNodeTag() {
if (_redirectNode != null) {
return _redirectNode.getClusterTag();
}
return null;
}
public String getSubscriptionName() {
if (_options != null) {
return _options.getSubscriptionName();
}
return null;
}
private Socket connectToServer() throws IOException, GeneralSecurityException {
GetTcpInfoForRemoteTaskCommand command = new GetTcpInfoForRemoteTaskCommand(
"Subscription/" + _dbName,
_dbName,
_options != null ? _options.getSubscriptionName() : null,
true);
RequestExecutor requestExecutor = _store.getRequestExecutor(_dbName);
TcpConnectionInfo tcpInfo;
if (_redirectNode != null) {
try {
requestExecutor.execute(_redirectNode, null, command, false, null);
tcpInfo = command.getResult();
} catch (ClientVersionMismatchException e) {
tcpInfo = legacyTryGetTcpInfo(requestExecutor, _redirectNode);
} catch (Exception e) {
// if we failed to talk to a node, we'll forget about it and let the topology to
// redirect us to the current node
_redirectNode = null;
throw new RuntimeException(e);
}
} else {
try {
requestExecutor.execute(command);
tcpInfo = command.getResult();
if (tcpInfo.getNodeTag() != null) {
TcpConnectionInfo finalTcpInfo = tcpInfo;
_redirectNode = requestExecutor.getTopology().getNodes()
.stream()
.filter(x -> finalTcpInfo.getNodeTag().equals(x.getClusterTag()))
.findFirst()
.orElse(null);
}
} catch (ClientVersionMismatchException e) {
tcpInfo = legacyTryGetTcpInfo(requestExecutor);
}
}
TcpUtils.ConnectSecuredTcpSocketResult result = TcpUtils.connectSecuredTcpSocket(
tcpInfo,
command.getResult().getCertificate(),
_store.getCertificate(),
_store.getCertificatePrivateKeyPassword(),
TcpConnectionHeaderMessage.OperationTypes.SUBSCRIPTION,
this::negotiateProtocolVersionForSubscription
);
_tcpClient = result.socket;
_tcpClient.setTcpNoDelay(true);
_tcpClient.setSendBufferSize(_options.getSendBufferSize());
_tcpClient.setReceiveBufferSize(_options.getReceiveBufferSize());
_supportedFeatures = result.supportedFeatures;
if (_supportedFeatures.protocolVersion <= 0) {
throw new IllegalStateException(_options.getSubscriptionName() + " : TCP negotiation resulted with an invalid protocol version: " + _supportedFeatures.protocolVersion);
}
byte[] options = JsonExtensions.writeValueAsBytes(_options);
_tcpClient.getOutputStream().write(options);
_tcpClient.getOutputStream().flush();
if (_subscriptionLocalRequestExecutor != null) {
_subscriptionLocalRequestExecutor.close();
}
_subscriptionLocalRequestExecutor = RequestExecutor.createForSingleNodeWithoutConfigurationUpdates(
command.getRequestedNode().getUrl(),
_dbName, requestExecutor.getCertificate(), requestExecutor.getKeyPassword(), requestExecutor.getTrustStore(),
_store.getExecutorService(),
_store.getConventions());
_store.registerEvents(_subscriptionLocalRequestExecutor);
return _tcpClient;
}
private TcpConnectionHeaderMessage.SupportedFeatures negotiateProtocolVersionForSubscription(
String chosenUrl,
TcpConnectionInfo tcpInfo,
Socket socket
) throws IOException {
String databaseName = _store.getEffectiveDatabase(_dbName);
TcpNegotiateParameters parameters = new TcpNegotiateParameters();
parameters.setDatabase(databaseName);
parameters.setOperation(TcpConnectionHeaderMessage.OperationTypes.SUBSCRIPTION);
parameters.setVersion(TcpConnectionHeaderMessage.SUBSCRIPTION_TCP_VERSION);
parameters.setReadResponseAndGetVersionCallback(this::readServerResponseAndGetVersion);
parameters.setDestinationNodeTag(getCurrentNodeTag());
parameters.setDestinationUrl(chosenUrl);
parameters.setDestinationServerId(tcpInfo.getServerId());
return TcpNegotiation.negotiateProtocolVersion(socket, parameters);
}
private TcpConnectionInfo legacyTryGetTcpInfo(RequestExecutor requestExecutor) {
GetTcpInfoCommand tcpCommand = new GetTcpInfoCommand("Subscription/" + _dbName, _dbName);
try {
requestExecutor.execute(tcpCommand, null);
} catch (Exception e) {
_redirectNode = null;
throw e;
}
return tcpCommand.getResult();
}
private TcpConnectionInfo legacyTryGetTcpInfo(RequestExecutor requestExecutor, ServerNode node) {
GetTcpInfoCommand tcpCommand = new GetTcpInfoCommand("Subscription/" + _dbName, _dbName);
try {
requestExecutor.execute(node, null, tcpCommand, false, null);
} catch (Exception e) {
_redirectNode = null;
throw e;
}
return tcpCommand.getResult();
}
private void ensureParser(Socket socket) throws IOException {
if (_parser == null) {
_parser = JsonExtensions.getDefaultMapper().getFactory().createParser(socket.getInputStream());
_parser.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false);
}
}
private int readServerResponseAndGetVersion(String url, Socket socket) {
try {
//Reading reply from server
ensureParser(socket);
TreeNode response = _parser.readValueAsTree();
TcpConnectionHeaderResponse reply = JsonExtensions.getDefaultMapper().treeToValue(response, TcpConnectionHeaderResponse.class);
switch (reply.getStatus()) {
case OK:
return reply.getVersion();
case AUTHORIZATION_FAILED:
throw new AuthorizationException("Cannot access database " + _dbName + " because " + reply.getMessage());
case TCP_VERSION_MISMATCH:
if (reply.getVersion() != TcpNegotiation.OUT_OF_RANGE_STATUS) {
return reply.getVersion();
}
//Kindly request the server to drop the connection
sendDropMessage(reply);
throw new IllegalStateException("Can't connect to database " + _dbName + " because: " + reply.getMessage());
case INVALID_NETWORK_TOPOLOGY:
throw new InvalidNetworkTopologyException("Failed to connect to url " + url + " because " + reply.getMessage());
}
return reply.getVersion();
} catch (IOException e) {
throw ExceptionsUtils.unwrapException(e);
}
}
private void sendDropMessage(TcpConnectionHeaderResponse reply) throws IOException {
TcpConnectionHeaderMessage dropMsg = new TcpConnectionHeaderMessage();
dropMsg.setOperation(TcpConnectionHeaderMessage.OperationTypes.DROP);
dropMsg.setDatabaseName(_dbName);
dropMsg.setOperationVersion(TcpConnectionHeaderMessage.SUBSCRIPTION_TCP_VERSION);
dropMsg.setInfo("Couldn't agree on subscription tcp version ours: " + TcpConnectionHeaderMessage.SUBSCRIPTION_TCP_VERSION + " theirs: " + reply.getVersion());
byte[] header = JsonExtensions.writeValueAsBytes(dropMsg);
_tcpClient.getOutputStream().write(header);
_tcpClient.getOutputStream().flush();
}
private void assertConnectionState(SubscriptionConnectionServerMessage connectionStatus) {
if (connectionStatus.getType() == SubscriptionConnectionServerMessage.MessageType.ERROR) {
if (connectionStatus.getException().contains("DatabaseDoesNotExistException")) {
throw new DatabaseDoesNotExistException(_dbName + " does not exists. " + connectionStatus.getMessage());
}
}
if (connectionStatus.getType() != SubscriptionConnectionServerMessage.MessageType.CONNECTION_STATUS) {
String message = "Server returned illegal type message when expecting connection status, was:" + connectionStatus.getType();
if (connectionStatus.getType() == SubscriptionConnectionServerMessage.MessageType.ERROR) {
message += ". Exception: " + connectionStatus.getException();
}
throw new SubscriptionMessageTypeException(message);
}
switch (connectionStatus.getStatus()) {
case ACCEPTED:
break;
case IN_USE:
throw new SubscriptionInUseException("Subscription with id " + _options.getSubscriptionName() + " cannot be opened, because it's in use and the connection strategy is " + _options.getStrategy());
case CLOSED:
boolean canReconnect = false;
JsonNode canReconnectNode = connectionStatus.getData().get("CanReconnect");
if (canReconnectNode != null && canReconnectNode.isBoolean() && canReconnectNode.asBoolean()) {
canReconnect = true;
}
throw new SubscriptionClosedException("Subscription with id " + _options.getSubscriptionName() + " was closed. " + connectionStatus.getException(), canReconnect);
case INVALID:
throw new SubscriptionInvalidStateException("Subscription with id " + _options.getSubscriptionName() + " cannot be opened, because it is in invalid state. " + connectionStatus.getException());
case NOT_FOUND:
throw new SubscriptionDoesNotExistException("Subscription with id " + _options.getSubscriptionName() + " cannot be opened, because it does not exist. " + connectionStatus.getException());
case REDIRECT:
if (_options.getStrategy() == SubscriptionOpeningStrategy.WAIT_FOR_FREE) {
if (connectionStatus.getData() != null) {
JsonNode registerConnectionDurationInTicksObject = connectionStatus.getData().get("RegisterConnectionDurationInTicks");
if (registerConnectionDurationInTicksObject.isLong()) {
long registerConnectionDurationInTicks = registerConnectionDurationInTicksObject.asLong();
if (registerConnectionDurationInTicks / 10_000 >= _options.getMaxErroneousPeriod().toMillis()) {
// this worker connection Waited For Free for more than MaxErroneousPeriod
_lastConnectionFailure = null;
}
}
}
}
ObjectNode data = connectionStatus.getData();
JsonNode redirectedTag = data.get("RedirectedTag");
String appropriateNode = redirectedTag.isNull() ? null : redirectedTag.asText();
JsonNode currentTagRaw = data.get("CurrentTag");
String currentNode = currentTagRaw != null && !currentTagRaw.isNull() ? currentTagRaw.asText() : null;
JsonNode rawReasons = data.get("Reasons");
Map<String, String> reasonsDictionary = new HashMap<>();
if (rawReasons instanceof ArrayNode) {
ArrayNode rawReasonsArray = (ArrayNode) rawReasons;
for (JsonNode item : rawReasonsArray) {
if (item instanceof ObjectNode) {
ObjectNode itemAsBlittable = (ObjectNode) item;
if (itemAsBlittable.size() == 1) {
String tagName = itemAsBlittable.fieldNames().next();
reasonsDictionary.put(tagName, itemAsBlittable.get(tagName).asText());
}
}
}
}
String reasonsJoined = reasonsDictionary
.entrySet()
.stream()
.map(x -> x.getKey() + ":" + x.getValue())
.collect(Collectors.joining());
SubscriptionDoesNotBelongToNodeException notBelongToNodeException =
new SubscriptionDoesNotBelongToNodeException(
"Subscription with id '" + _options.getSubscriptionName() + "' cannot be processed by current node '" + currentNode + "', " +
"it will be redirected to " + appropriateNode + System.lineSeparator() + reasonsJoined);
notBelongToNodeException.setAppropriateNode(appropriateNode);
notBelongToNodeException.setReasons(reasonsDictionary);
throw notBelongToNodeException;
case CONCURRENCY_RECONNECT:
throw new SubscriptionChangeVectorUpdateConcurrencyException(connectionStatus.getMessage());
default:
throw new IllegalStateException("Subscription " + _options.getSubscriptionName() + " could not be opened, reason: " + connectionStatus.getStatus());
}
}
@SuppressWarnings("ConstantConditions")
private void processSubscription() throws Exception {
try {
_processingCts.getToken().throwIfCancellationRequested();
try (Socket socket = connectToServer()) {
_processingCts.getToken().throwIfCancellationRequested();
Socket tcpClientCopy = _tcpClient;
SubscriptionConnectionServerMessage connectionStatus = readNextObject(tcpClientCopy);
if (_processingCts.getToken().isCancellationRequested()) {
return;
}
if (connectionStatus.getType() != SubscriptionConnectionServerMessage.MessageType.CONNECTION_STATUS
|| connectionStatus.getStatus() != SubscriptionConnectionServerMessage.ConnectionStatus.ACCEPTED) {
assertConnectionState(connectionStatus);
}
_lastConnectionFailure = null;
if (_processingCts.getToken().isCancellationRequested()) {
return;
}
CompletableFuture<Void> notifiedSubscriber = CompletableFuture.completedFuture(null);
SubscriptionBatch<T> batch = new SubscriptionBatch<>(_clazz, _revisions, _subscriptionLocalRequestExecutor, _store, _dbName, _logger);
while (!_processingCts.getToken().isCancellationRequested()) {
// start reading next batch from server on 1'st thread (can be before client started processing)
CompletableFuture<BatchFromServer> readFromServer =
CompletableFuture.supplyAsync(() -> {
try {
return readSingleSubscriptionBatchFromServer(tcpClientCopy, batch);
} catch (IOException e) {
throw new RuntimeException(e);
}
}, _store.getExecutorService());
try {
notifiedSubscriber.get();
} catch (Exception e) {
// if the subscriber errored, we shut down
try {
closeTcpClient();
} catch (Exception ex2) {
// nothing to be done here
}
throw e;
}
BatchFromServer incomingBatch = readFromServer.get();
_processingCts.getToken().throwIfCancellationRequested();
String lastReceivedChangeVector = batch.initialize(incomingBatch);
notifiedSubscriber = CompletableFuture.runAsync(() -> {
try {
_subscriber.accept(batch);
} catch (Exception ex) {
if (_logger.isDebugEnabled()) {
_logger.debug("Subscription " + _options.getSubscriptionName() + ". Subscriber threw an exception on document batch", ex);
}
if (!_options.isIgnoreSubscriberErrors()) {
throw new SubscriberErrorException("Subscriber threw an exception in subscription " + _options.getSubscriptionName(), ex);
}
}
try {
if (tcpClientCopy != null) {
sendAck(lastReceivedChangeVector, tcpClientCopy);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}, _store.getExecutorService());
}
}
} catch (OperationCancelledException e) {
if (!_disposed) {
throw e;
}
// otherwise this is thrown when shutting down,
// it isn't an error, so we don't need to treat it as such
}
}
private BatchFromServer readSingleSubscriptionBatchFromServer(Socket socket, SubscriptionBatch<T> batch) throws IOException {
List<SubscriptionConnectionServerMessage> incomingBatch = new ArrayList<>();
List<ObjectNode> includes = new ArrayList<>();
List<BatchFromServer.CounterIncludeItem> counterIncludes = new ArrayList<>();
List<ObjectNode> timeSeriesIncludes = new ArrayList<>();
boolean endOfBatch = false;
while (!endOfBatch && !_processingCts.getToken().isCancellationRequested()) {
SubscriptionConnectionServerMessage receivedMessage = readNextObject(socket);
if (receivedMessage == null || _processingCts.getToken().isCancellationRequested()) {
break;
}
switch (receivedMessage.getType()) {
case DATA:
incomingBatch.add(receivedMessage);
break;
case INCLUDES:
includes.add(receivedMessage.getIncludes());
break;
case COUNTER_INCLUDES:
counterIncludes.add(new BatchFromServer.CounterIncludeItem(receivedMessage.getCounterIncludes(), receivedMessage.getIncludedCounterNames()));
break;
case TIME_SERIES_INCLUDES:
timeSeriesIncludes.add(receivedMessage.getTimeSeriesIncludes());
break;
case END_OF_BATCH:
endOfBatch = true;
break;
case CONFIRM:
EventHelper.invoke(afterAcknowledgment, batch);
incomingBatch.clear();
batch.getItems().clear();
break;
case CONNECTION_STATUS:
assertConnectionState(receivedMessage);
break;
case ERROR:
throwSubscriptionError(receivedMessage);
break;
default:
throwInvalidServerResponse(receivedMessage);
break;
}
}
BatchFromServer batchFromServer = new BatchFromServer();
batchFromServer.setMessages(incomingBatch);
batchFromServer.setIncludes(includes);
batchFromServer.setCounterIncludes(counterIncludes);
batchFromServer.setTimeSeriesIncludes(timeSeriesIncludes);
return batchFromServer;
}
private static void throwInvalidServerResponse(SubscriptionConnectionServerMessage receivedMessage) {
throw new IllegalArgumentException("Unrecognized message " + receivedMessage.getType() + " type received from server");
}
private static void throwSubscriptionError(SubscriptionConnectionServerMessage receivedMessage) {
throw new IllegalStateException("Connected terminated by server. Exception: " + ObjectUtils.firstNonNull(receivedMessage.getException(), "None"));
}
private SubscriptionConnectionServerMessage readNextObject(Socket socket) throws IOException {
if (_processingCts.getToken().isCancellationRequested() || !_tcpClient.isConnected()) {
return null;
}
if (_disposed) { //if we are disposed, nothing to do...
return null;
}
TreeNode response = _parser.readValueAsTree();
return JsonExtensions.getDefaultMapper().treeToValue(response, SubscriptionConnectionServerMessage.class);
}
private void sendAck(String lastReceivedChangeVector, Socket networkStream) throws IOException {
SubscriptionConnectionClientMessage msg = new SubscriptionConnectionClientMessage();
msg.setChangeVector(lastReceivedChangeVector);
msg.setType(SubscriptionConnectionClientMessage.MessageType.ACKNOWLEDGE);
byte[] ack = JsonExtensions.writeValueAsBytes(msg);
networkStream.getOutputStream().write(ack);
networkStream.getOutputStream().flush();
}
private CompletableFuture<Void> runSubscriptionAsync() {
return CompletableFuture.runAsync(() -> {
while (!_processingCts.getToken().isCancellationRequested()) {
try {
closeTcpClient();
if (_logger.isInfoEnabled()) {
_logger.info("Subscription " + _options.getSubscriptionName() + ". Connecting to server...");
}
processSubscription();
} catch (Exception ex) {
try {
if (_processingCts.getToken().isCancellationRequested()) {
if (!_disposed) {
throw ex;
}
return;
}
if (_logger.isInfoEnabled()) {
_logger.info("Subscription " + _options.getSubscriptionName() + ". Pulling task threw the following exception", ex);
}
if (shouldTryToReconnect(ex)) {
Thread.sleep(_options.getTimeToWaitBeforeConnectionRetry().toMillis());
if (_redirectNode == null) {
RequestExecutor reqEx = _store.getRequestExecutor(_dbName);
List<ServerNode> curTopology = reqEx.getTopologyNodes();
int nextNodeIndex = (_forcedTopologyUpdateAttempts++) % curTopology.size();
try {
_redirectNode = reqEx.getRequestedNode(curTopology.get(nextNodeIndex).getClusterTag(), true).currentNode;
if (_logger.isInfoEnabled()) {
_logger.info("Subscription '" + _options.getSubscriptionName() + "'. Will modify redirect node from null to " + _redirectNode.getClusterTag(), ex);
}
} catch (Exception e) {
// will let topology to decide
if (_logger.isInfoEnabled()) {
_logger.info("Subscription '" + _options.getSubscriptionName() + "'. Could not select the redirect node will keep it null.", e);
}
}
}
EventHelper.invoke(onSubscriptionConnectionRetry, ex);
} else {
if (_logger.isErrorEnabled()) {
_logger.error("Connection to subscription " + _options.getSubscriptionName() + " have been shut down because of an error", ex);
}
throw ex;
}
} catch (Exception e) {
throw ExceptionsUtils.unwrapException(e);
}
}
}
}, _store.getExecutorService());
}
private Date _lastConnectionFailure;
private TcpConnectionHeaderMessage.SupportedFeatures _supportedFeatures;
private void assertLastConnectionFailure() {
if (_lastConnectionFailure == null) {
_lastConnectionFailure = new Date();
return;
}
if (new Date().getTime() - _lastConnectionFailure.getTime() > _options.getMaxErroneousPeriod().toMillis()) {
throw new SubscriptionInvalidStateException("Subscription connection was in invalid state for more than "
+ _options.getMaxErroneousPeriod() + " and therefore will be terminated");
}
}
private boolean shouldTryToReconnect(Exception ex) {
ex = ExceptionsUtils.unwrapException(ex);
if (ex instanceof SubscriptionDoesNotBelongToNodeException) {
SubscriptionDoesNotBelongToNodeException se = (SubscriptionDoesNotBelongToNodeException) ex;
RequestExecutor requestExecutor = _store.getRequestExecutor(_dbName);
if (se.getAppropriateNode() == null) {
assertLastConnectionFailure();
_redirectNode = null;
return true;
}
ServerNode nodeToRedirectTo = requestExecutor.getTopologyNodes()
.stream()
.filter(x -> x.getClusterTag().equals(se.getAppropriateNode()))
.findFirst()
.orElse(null);
if (nodeToRedirectTo == null) {
throw new IllegalStateException("Could not redirect to " + se.getAppropriateNode() + ", because it was not found in local topology, even after retrying");
}
_redirectNode = nodeToRedirectTo;
return true;
} else if (ex instanceof NodeIsPassiveException) {
// if we failed to talk to a node, we'll forget about it and let the topology to
// redirect us to the current node
_redirectNode = null;
return true;
} else if (ex instanceof SubscriptionChangeVectorUpdateConcurrencyException) {
return true;
} else if (ex instanceof SubscriptionClosedException) {
SubscriptionClosedException sce = (SubscriptionClosedException) ex;
if (sce.isCanReconnect()) {
return true;
}
_processingCts.cancel();
return false;
}
if (ex instanceof SubscriptionInUseException
|| ex instanceof SubscriptionDoesNotExistException
|| ex instanceof SubscriptionInvalidStateException
|| ex instanceof DatabaseDoesNotExistException
|| ex instanceof AuthorizationException
|| ex instanceof AllTopologyNodesDownException
|| ex instanceof SubscriberErrorException) {
_processingCts.cancel();
return false;
}
EventHelper.invoke(onUnexpectedSubscriptionError, ex);
assertLastConnectionFailure();
return true;
}
private void closeTcpClient() {
if (_parser != null) {
IOUtils.closeQuietly(_parser, null);
_parser = null;
}
if (_tcpClient != null) {
IOUtils.closeQuietly(_tcpClient, null);
_tcpClient = null;
}
}
Consumer<SubscriptionWorker<T>> onClosed = null;
}