Repository navigation
Expand file tree
/
Copy pathEventStoreConnectionLogicHandler.cs
More file actions
705 lines (611 loc) · 32.3 KB
/
Copy pathEventStoreConnectionLogicHandler.cs
File metadata and controls
705 lines (611 loc) · 32.3 KB
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
using System;
using System.Diagnostics;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using EventStore.ClientAPI.ClientOperations;
using EventStore.ClientAPI.Common.Utils;
using EventStore.ClientAPI.Exceptions;
using EventStore.ClientAPI.SystemData;
using EventStore.ClientAPI.Transport.Tcp;
using EventStore.ClientAPI.Messages;
namespace EventStore.ClientAPI.Internal
{
internal class EventStoreConnectionLogicHandler : IEventStoreConnectionLogicHandler
{
private static readonly TimerTickMessage TimerTickMessage = new TimerTickMessage();
public int TotalOperationCount { get { return _operations.TotalOperationCount; } }
private readonly IEventStoreConnection _esConnection;
private readonly ConnectionSettings _settings;
private readonly byte ClientVersion = 1;
private readonly SimpleQueuedHandler _queue = new SimpleQueuedHandler();
private readonly Timer _timer;
private IEndPointDiscoverer _endPointDiscoverer;
private readonly Stopwatch _stopwatch = Stopwatch.StartNew();
private ReconnectionInfo _reconnInfo;
private HeartbeatInfo _heartbeatInfo;
private AuthInfo _authInfo;
private IdentifyInfo _identifyInfo;
private TimeSpan _lastTimeoutsTimeStamp;
private readonly OperationsManager _operations;
private readonly SubscriptionsManager _subscriptions;
private ConnectionState _state = ConnectionState.Init;
private ConnectingPhase _connectingPhase = ConnectingPhase.Invalid;
private int _wasConnected;
private int _packageNumber;
private TcpPackageConnection _connection;
public EventStoreConnectionLogicHandler(IEventStoreConnection esConnection, ConnectionSettings settings)
{
Ensure.NotNull(esConnection, "esConnection");
Ensure.NotNull(settings, "settings");
_esConnection = esConnection;
_settings = settings;
_operations = new OperationsManager(_esConnection.ConnectionName, settings);
_subscriptions = new SubscriptionsManager(_esConnection.ConnectionName, settings);
_queue.RegisterHandler<StartConnectionMessage>(msg => StartConnection(msg.Task, msg.EndPointDiscoverer));
_queue.RegisterHandler<CloseConnectionMessage>(msg => CloseConnection(msg.Reason, msg.Exception));
_queue.RegisterHandler<StartOperationMessage>(msg => StartOperation(msg.Operation, msg.MaxRetries, msg.Timeout));
_queue.RegisterHandler<StartSubscriptionMessage>(StartSubscription);
_queue.RegisterHandler<StartPersistentSubscriptionMessage>(StartSubscription);
_queue.RegisterHandler<EstablishTcpConnectionMessage>(msg => EstablishTcpConnection(msg.EndPoints));
_queue.RegisterHandler<TcpConnectionEstablishedMessage>(msg => TcpConnectionEstablished(msg.Connection));
_queue.RegisterHandler<TcpConnectionErrorMessage>(msg => TcpConnectionError(msg.Connection, msg.Exception));
_queue.RegisterHandler<TcpConnectionClosedMessage>(msg => TcpConnectionClosed(msg.Connection));
_queue.RegisterHandler<HandleTcpPackageMessage>(msg => HandleTcpPackage(msg.Connection, msg.Package));
_queue.RegisterHandler<TimerTickMessage>(msg => TimerTick());
_timer = new Timer(_ => EnqueueMessage(TimerTickMessage), null, Consts.TimerPeriod, Consts.TimerPeriod);
}
public void EnqueueMessage(Message message)
{
if (_settings.VerboseLogging && message != TimerTickMessage) LogDebug("enqueueing message {0}.", message);
_queue.EnqueueMessage(message);
}
private void StartConnection(TaskCompletionSource<object> task, IEndPointDiscoverer endPointDiscoverer)
{
Ensure.NotNull(task, "task");
Ensure.NotNull(endPointDiscoverer, "endPointDiscoverer");
LogDebug("StartConnection");
switch (_state)
{
case ConnectionState.Init:
{
_endPointDiscoverer = endPointDiscoverer;
_state = ConnectionState.Connecting;
_connectingPhase = ConnectingPhase.Reconnecting;
DiscoverEndPoint(task);
break;
}
case ConnectionState.Connecting:
case ConnectionState.Connected:
{
task.SetException(new InvalidOperationException(
string.Format("EventStoreConnection '{0}' is already active.", _esConnection.ConnectionName)));
break;
}
case ConnectionState.Closed:
task.SetException(new ObjectDisposedException(_esConnection.ConnectionName));
break;
default: throw new Exception(string.Format("Unknown state: {0}", _state));
}
}
private void DiscoverEndPoint(TaskCompletionSource<object> completionTask)
{
LogDebug("DiscoverEndPoint");
if (_state != ConnectionState.Connecting) return;
if (_connectingPhase != ConnectingPhase.Reconnecting) return;
_connectingPhase = ConnectingPhase.EndPointDiscovery;
_endPointDiscoverer.DiscoverAsync(_connection != null ? _connection.RemoteEndPoint : null).ContinueWith(t =>
{
if (t.IsFaulted)
{
EnqueueMessage(new CloseConnectionMessage("Failed to resolve TCP end point to which to connect.", t.Exception));
completionTask?.SetException(new CannotEstablishConnectionException("Cannot resolve target end point.", t.Exception));
}
else
{
EnqueueMessage(new EstablishTcpConnectionMessage(t.Result));
completionTask?.SetResult(null);
}
});
}
private void EstablishTcpConnection(NodeEndPoints endPoints)
{
var endPoint = _settings.UseSslConnection ? endPoints.SecureTcpEndPoint ?? endPoints.TcpEndPoint : endPoints.TcpEndPoint;
if (endPoint == null)
{
CloseConnection("No end point to node specified.");
return;
}
LogDebug("EstablishTcpConnection to [{0}]", endPoint);
if (_state != ConnectionState.Connecting) return;
if (_connectingPhase != ConnectingPhase.EndPointDiscovery) return;
_connectingPhase = ConnectingPhase.ConnectionEstablishing;
_connection = new TcpPackageConnection(
_settings.Log,
endPoint,
Guid.NewGuid(),
_settings.UseSslConnection,
_settings.TargetHost,
_settings.ValidateServer,
_settings.ClientConnectionTimeout,
(connection, package) => EnqueueMessage(new HandleTcpPackageMessage(connection, package)),
(connection, exc) => EnqueueMessage(new TcpConnectionErrorMessage(connection, exc)),
connection => EnqueueMessage(new TcpConnectionEstablishedMessage(connection)),
(connection, error) => EnqueueMessage(new TcpConnectionClosedMessage(connection, error)));
_connection.StartReceiving();
}
private void TcpConnectionError(TcpPackageConnection connection, Exception exception)
{
if (_connection != connection) return;
if (_state == ConnectionState.Closed) return;
LogDebug("TcpConnectionError connId {0:B}, exc {1}.", connection.ConnectionId, exception);
CloseConnection("TCP connection error occurred.", exception);
}
private void CloseConnection(string reason, Exception exception = null)
{
if (_state == ConnectionState.Closed)
{
LogDebug("CloseConnection IGNORED because is ESConnection is CLOSED, reason {0}, exception {1}.", reason, exception);
return;
}
LogDebug("CloseConnection, reason {0}, exception {1}.", reason, exception);
_state = ConnectionState.Closed;
_timer.Dispose();
_operations.CleanUp();
_subscriptions.CleanUp();
CloseTcpConnection(reason);
LogInfo("Closed. Reason: {0}.", reason);
if (exception != null)
RaiseErrorOccurred(exception);
RaiseClosed(reason);
}
private void CloseTcpConnection(string reason)
{
if (_connection == null)
{
LogDebug("CloseTcpConnection IGNORED because _connection == null");
return;
}
LogDebug("CloseTcpConnection");
_connection.Close(reason);
TcpConnectionClosed(_connection);
_connection = null;
}
private void TcpConnectionClosed(TcpPackageConnection connection)
{
if (_state == ConnectionState.Init) throw new Exception();
if (_state == ConnectionState.Closed || _connection != connection)
{
LogDebug("IGNORED (_state: {0}, _conn.ID: {1:B}, conn.ID: {2:B}): TCP connection to [{3}, L{4}] closed.",
_state, _connection == null ? Guid.Empty : _connection.ConnectionId, connection.ConnectionId,
connection.RemoteEndPoint, connection.LocalEndPoint);
return;
}
_state = ConnectionState.Connecting;
_connectingPhase = ConnectingPhase.Reconnecting;
LogDebug("TCP connection to [{0}, L{1}, {2:B}] closed.", connection.RemoteEndPoint, connection.LocalEndPoint, connection.ConnectionId);
_subscriptions.PurgeSubscribedAndDroppedSubscriptions(_connection.ConnectionId);
_reconnInfo = new ReconnectionInfo(_reconnInfo.ReconnectionAttempt, _stopwatch.Elapsed);
if (Interlocked.CompareExchange(ref _wasConnected, 0, 1) == 1)
{
RaiseDisconnected(connection.RemoteEndPoint);
}
}
private void TcpConnectionEstablished(TcpPackageConnection connection)
{
if (_state != ConnectionState.Connecting || _connection != connection || connection.IsClosed)
{
LogDebug("IGNORED (_state {0}, _conn.Id {1:B}, conn.Id {2:B}, conn.closed {3}): TCP connection to [{4}, L{5}] established.",
_state, _connection == null ? Guid.Empty : _connection.ConnectionId, connection.ConnectionId,
connection.IsClosed, connection.RemoteEndPoint, connection.LocalEndPoint);
return;
}
LogDebug("TCP connection to [{0}, L{1}, {2:B}] established.", connection.RemoteEndPoint, connection.LocalEndPoint, connection.ConnectionId);
_heartbeatInfo = new HeartbeatInfo(_packageNumber, true, _stopwatch.Elapsed);
if (_settings.DefaultUserCredentials != null)
{
_connectingPhase = ConnectingPhase.Authentication;
_authInfo = new AuthInfo(Guid.NewGuid(), _stopwatch.Elapsed);
_connection.EnqueueSend(new TcpPackage(TcpCommand.Authenticate,
TcpFlags.Authenticated,
_authInfo.CorrelationId,
_settings.DefaultUserCredentials.Username,
_settings.DefaultUserCredentials.Password,
null));
}
else
{
GoToIdentifyState();
}
}
private void GoToIdentifyState()
{
Ensure.NotNull(_connection, "_connection");
_connectingPhase = ConnectingPhase.Identification;
_identifyInfo = new IdentifyInfo(Guid.NewGuid(), _stopwatch.Elapsed);
var dto = new ClientMessage.IdentifyClient(ClientVersion, _esConnection.ConnectionName);
_connection.EnqueueSend(new TcpPackage(TcpCommand.IdentifyClient, _identifyInfo.CorrelationId, dto.Serialize()));
}
private void GoToConnectedState()
{
Ensure.NotNull(_connection, "_connection");
_state = ConnectionState.Connected;
_connectingPhase = ConnectingPhase.Connected;
Interlocked.CompareExchange(ref _wasConnected, 1, 0);
RaiseConnectedEvent(_connection.RemoteEndPoint);
if (_stopwatch.Elapsed - _lastTimeoutsTimeStamp >= _settings.OperationTimeoutCheckPeriod)
{
_operations.CheckTimeoutsAndRetry(_connection);
_subscriptions.CheckTimeoutsAndRetry(_connection);
_lastTimeoutsTimeStamp = _stopwatch.Elapsed;
}
}
private void TimerTick()
{
switch (_state)
{
case ConnectionState.Init: break;
case ConnectionState.Connecting:
{
if (_connectingPhase == ConnectingPhase.Reconnecting && _stopwatch.Elapsed - _reconnInfo.TimeStamp >= _settings.ReconnectionDelay)
{
LogDebug("TimerTick checking reconnection...");
_reconnInfo = new ReconnectionInfo(_reconnInfo.ReconnectionAttempt + 1, _stopwatch.Elapsed);
if (_settings.MaxReconnections >= 0 && _reconnInfo.ReconnectionAttempt > _settings.MaxReconnections)
CloseConnection("Reconnection limit reached.");
else
{
RaiseReconnecting();
DiscoverEndPoint(null);
}
}
if (_connectingPhase == ConnectingPhase.Authentication && _stopwatch.Elapsed - _authInfo.TimeStamp >= _settings.OperationTimeout)
{
RaiseAuthenticationFailed("Authentication timed out.");
GoToIdentifyState();
}
if (_connectingPhase == ConnectingPhase.Identification && _stopwatch.Elapsed - _identifyInfo.TimeStamp >= _settings.OperationTimeout)
{
const string msg = "Timed out waiting for client to be identified";
LogDebug(msg);
CloseTcpConnection(msg);
}
if (_connectingPhase > ConnectingPhase.ConnectionEstablishing)
ManageHeartbeats();
break;
}
case ConnectionState.Connected:
{
// operations timeouts are checked only if connection is established and check period time passed
if (_stopwatch.Elapsed - _lastTimeoutsTimeStamp >= _settings.OperationTimeoutCheckPeriod)
{
// On mono even impossible connection first says that it is established
// so clearing of reconnection count on ConnectionEstablished event causes infinite reconnections.
// So we reset reconnection count to zero on each timeout check period when connection is established
_reconnInfo = new ReconnectionInfo(0, _stopwatch.Elapsed);
_operations.CheckTimeoutsAndRetry(_connection);
_subscriptions.CheckTimeoutsAndRetry(_connection);
_lastTimeoutsTimeStamp = _stopwatch.Elapsed;
}
ManageHeartbeats();
break;
}
case ConnectionState.Closed: break;
default: throw new Exception(string.Format("Unknown state: {0}.", _state));
}
}
private void ManageHeartbeats()
{
if (_connection == null) throw new Exception();
var timeout = _heartbeatInfo.IsIntervalStage ? _settings.HeartbeatInterval : _settings.HeartbeatTimeout;
if (_stopwatch.Elapsed - _heartbeatInfo.TimeStamp < timeout)
return;
var packageNumber = _packageNumber;
if (_heartbeatInfo.LastPackageNumber != packageNumber)
{
_heartbeatInfo = new HeartbeatInfo(packageNumber, true, _stopwatch.Elapsed);
return;
}
if (_heartbeatInfo.IsIntervalStage)
{
// TcpMessage.Heartbeat analog
_connection.EnqueueSend(new TcpPackage(TcpCommand.HeartbeatRequestCommand, Guid.NewGuid(), null));
_heartbeatInfo = new HeartbeatInfo(_heartbeatInfo.LastPackageNumber, false, _stopwatch.Elapsed);
}
else
{
// TcpMessage.HeartbeatTimeout analog
var msg = string.Format("EventStoreConnection '{0}': closing TCP connection [{1}, {2}, {3}] due to HEARTBEAT TIMEOUT at pkgNum {4}.",
_esConnection.ConnectionName, _connection.RemoteEndPoint, _connection.LocalEndPoint,
_connection.ConnectionId, packageNumber);
_settings.Log.Info(msg);
CloseTcpConnection(msg);
}
}
private void StartOperation(IClientOperation operation, int maxRetries, TimeSpan timeout)
{
switch (_state)
{
case ConnectionState.Init:
operation.Fail(new InvalidOperationException(string.Format("EventStoreConnection '{0}' is not active.", _esConnection.ConnectionName)));
break;
case ConnectionState.Connecting:
LogDebug("StartOperation enqueue {0}, {1}, {2}, {3}.", operation.GetType().Name, operation, maxRetries, timeout);
_operations.EnqueueOperation(new OperationItem(operation, maxRetries, timeout));
break;
case ConnectionState.Connected:
LogDebug("StartOperation schedule {0}, {1}, {2}, {3}.", operation.GetType().Name, operation, maxRetries, timeout);
_operations.ScheduleOperation(new OperationItem(operation, maxRetries, timeout), _connection);
break;
case ConnectionState.Closed:
operation.Fail(new ObjectDisposedException(_esConnection.ConnectionName));
break;
default: throw new Exception(string.Format("Unknown state: {0}.", _state));
}
}
private void StartSubscription(StartSubscriptionMessage msg)
{
switch (_state)
{
case ConnectionState.Init:
msg.Source.SetException(new InvalidOperationException(string.Format("EventStoreConnection '{0}' is not active.", _esConnection.ConnectionName)));
break;
case ConnectionState.Connecting:
case ConnectionState.Connected:
var operation = new VolatileSubscriptionOperation(_settings.Log, msg.Source, msg.StreamId, msg.ResolveLinkTos,
msg.UserCredentials, msg.EventAppeared, msg.SubscriptionDropped,
_settings.VerboseLogging, () => _connection);
LogDebug("StartSubscription {4} {0}, {1}, {2}, {3}.", operation.GetType().Name, operation, msg.MaxRetries, msg.Timeout, _state == ConnectionState.Connected ? "fire" : "enqueue");
var subscription = new SubscriptionItem(operation, msg.MaxRetries, msg.Timeout);
if (_state == ConnectionState.Connecting)
_subscriptions.EnqueueSubscription(subscription);
else
_subscriptions.StartSubscription(subscription, _connection);
break;
case ConnectionState.Closed:
msg.Source.SetException(new ObjectDisposedException(_esConnection.ConnectionName));
break;
default: throw new Exception(string.Format("Unknown state: {0}.", _state));
}
}
private void StartSubscription(StartPersistentSubscriptionMessage msg)
{
switch (_state)
{
case ConnectionState.Init:
msg.Source.SetException(new InvalidOperationException(string.Format("EventStoreConnection '{0}' is not active.", _esConnection.ConnectionName)));
break;
case ConnectionState.Connecting:
case ConnectionState.Connected:
var operation = new ConnectToPersistentSubscriptionOperation(_settings.Log, msg.Source, msg.SubscriptionId, msg.BufferSize, msg.StreamId,
msg.UserCredentials, msg.EventAppeared, msg.SubscriptionDropped,
_settings.VerboseLogging, () => _connection);
LogDebug("StartSubscription {4} {0}, {1}, {2}, {3}.", operation.GetType().Name, operation, msg.MaxRetries, msg.Timeout, _state == ConnectionState.Connected ? "fire" : "enqueue");
var subscription = new SubscriptionItem(operation, msg.MaxRetries, msg.Timeout);
if (_state == ConnectionState.Connecting)
_subscriptions.EnqueueSubscription(subscription);
else
_subscriptions.StartSubscription(subscription, _connection);
break;
case ConnectionState.Closed:
msg.Source.SetException(new ObjectDisposedException(_esConnection.ConnectionName));
break;
default: throw new Exception(string.Format("Unknown state: {0}.", _state));
}
}
private void HandleTcpPackage(TcpPackageConnection connection, TcpPackage package)
{
if (_connection != connection || _state == ConnectionState.Closed || _state == ConnectionState.Init)
{
LogDebug("IGNORED: HandleTcpPackage connId {0}, package {1}, {2}.", connection.ConnectionId, package.Command, package.CorrelationId);
return;
}
LogDebug("HandleTcpPackage connId {0}, package {1}, {2}.", _connection.ConnectionId, package.Command, package.CorrelationId);
_packageNumber += 1;
if (package.Command == TcpCommand.HeartbeatResponseCommand)
return;
if (package.Command == TcpCommand.HeartbeatRequestCommand)
{
_connection.EnqueueSend(new TcpPackage(TcpCommand.HeartbeatResponseCommand, package.CorrelationId, null));
return;
}
if (package.Command == TcpCommand.Authenticated || package.Command == TcpCommand.NotAuthenticated)
{
if (_state == ConnectionState.Connecting
&& _connectingPhase == ConnectingPhase.Authentication
&& _authInfo.CorrelationId == package.CorrelationId)
{
if (package.Command == TcpCommand.NotAuthenticated)
RaiseAuthenticationFailed("Not authenticated");
GoToIdentifyState();
return;
}
}
if (package.Command == TcpCommand.ClientIdentified)
{
if (_state == ConnectionState.Connecting
&& _identifyInfo.CorrelationId == package.CorrelationId)
{
GoToConnectedState();
return;
}
}
if (package.Command == TcpCommand.BadRequest && package.CorrelationId == Guid.Empty)
{
string message = Helper.EatException(() => Helper.UTF8NoBom.GetString(package.Data.Array, package.Data.Offset, package.Data.Count));
var exc = new EventStoreConnectionException(
string.Format("Bad request received from server. Error: {0}", string.IsNullOrEmpty(message) ? "<no message>" : message));
CloseConnection("Connection-wide BadRequest received. Too dangerous to continue.", exc);
return;
}
OperationItem operation;
SubscriptionItem subscription;
if (_operations.TryGetActiveOperation(package.CorrelationId, out operation))
{
var result = operation.Operation.InspectPackage(package);
LogDebug("HandleTcpPackage OPERATION DECISION {0} ({1}), {2}", result.Decision, result.Description, operation);
switch (result.Decision)
{
case InspectionDecision.DoNothing: break;
case InspectionDecision.EndOperation:
_operations.RemoveOperation(operation);
break;
case InspectionDecision.Retry:
_operations.ScheduleOperationRetry(operation);
break;
case InspectionDecision.Reconnect:
ReconnectTo(new NodeEndPoints(result.TcpEndPoint, result.SecureTcpEndPoint));
_operations.ScheduleOperationRetry(operation);
break;
default: throw new Exception(string.Format("Unknown InspectionDecision: {0}", result.Decision));
}
if (_state == ConnectionState.Connected)
_operations.TryScheduleWaitingOperations(connection);
}
else if (_subscriptions.TryGetActiveSubscription(package.CorrelationId, out subscription))
{
var result = subscription.Operation.InspectPackage(package);
LogDebug("HandleTcpPackage SUBSCRIPTION DECISION {0} ({1}), {2}", result.Decision, result.Description, subscription);
switch (result.Decision)
{
case InspectionDecision.DoNothing: break;
case InspectionDecision.EndOperation:
_subscriptions.RemoveSubscription(subscription);
break;
case InspectionDecision.Retry:
_subscriptions.ScheduleSubscriptionRetry(subscription);
break;
case InspectionDecision.Reconnect:
ReconnectTo(new NodeEndPoints(result.TcpEndPoint, result.SecureTcpEndPoint));
_subscriptions.ScheduleSubscriptionRetry(subscription);
break;
case InspectionDecision.Subscribed:
subscription.IsSubscribed = true;
break;
default: throw new Exception(string.Format("Unknown InspectionDecision: {0}", result.Decision));
}
}
else
{
LogDebug("HandleTcpPackage UNMAPPED PACKAGE with CorrelationId {0:B}, Command: {1}", package.CorrelationId, package.Command);
}
}
private void ReconnectTo(NodeEndPoints endPoints)
{
IPEndPoint endPoint = _settings.UseSslConnection
? endPoints.SecureTcpEndPoint ?? endPoints.TcpEndPoint
: endPoints.TcpEndPoint;
if (endPoint == null)
{
CloseConnection("No end point is specified while trying to reconnect.");
return;
}
if (_state != ConnectionState.Connected || _connection.RemoteEndPoint.Equals(endPoint))
return;
var msg = string.Format("EventStoreConnection '{0}': going to reconnect to [{1}]. Current endpoint: [{2}, L{3}].",
_esConnection.ConnectionName, endPoint, _connection.RemoteEndPoint, _connection.LocalEndPoint);
if (_settings.VerboseLogging) _settings.Log.Info(msg);
CloseTcpConnection(msg);
_state = ConnectionState.Connecting;
_connectingPhase = ConnectingPhase.EndPointDiscovery;
EstablishTcpConnection(endPoints);
}
private void LogDebug(string message, params object[] parameters)
{
if (_settings.VerboseLogging) _settings.Log.Debug("EventStoreConnection '{0}': {1}.", _esConnection.ConnectionName, parameters.Length == 0 ? message : string.Format(message, parameters));
}
private void LogInfo(string message, params object[] parameters)
{
if (_settings.VerboseLogging) _settings.Log.Info("EventStoreConnection '{0}': {1}.", _esConnection.ConnectionName, parameters.Length == 0 ? message : string.Format(message, parameters));
}
private void RaiseConnectedEvent(IPEndPoint remoteEndPoint)
{
Connected(_esConnection, new ClientConnectionEventArgs(_esConnection, remoteEndPoint));
}
private void RaiseDisconnected(IPEndPoint remoteEndPoint)
{
Disconnected(_esConnection, new ClientConnectionEventArgs(_esConnection, remoteEndPoint));
}
private void RaiseClosed(string reason)
{
Closed(_esConnection, new ClientClosedEventArgs(_esConnection, reason));
}
private void RaiseErrorOccurred(Exception exception)
{
ErrorOccurred(_esConnection, new ClientErrorEventArgs(_esConnection, exception));
}
private void RaiseReconnecting()
{
Reconnecting(_esConnection, new ClientReconnectingEventArgs(_esConnection));
}
private void RaiseAuthenticationFailed(string reason)
{
AuthenticationFailed(_esConnection, new ClientAuthenticationFailedEventArgs(_esConnection, reason));
}
public event EventHandler<ClientConnectionEventArgs> Connected = delegate { };
public event EventHandler<ClientConnectionEventArgs> Disconnected = delegate { };
public event EventHandler<ClientReconnectingEventArgs> Reconnecting = delegate { };
public event EventHandler<ClientClosedEventArgs> Closed = delegate { };
public event EventHandler<ClientErrorEventArgs> ErrorOccurred = delegate { };
public event EventHandler<ClientAuthenticationFailedEventArgs> AuthenticationFailed = delegate { };
private struct HeartbeatInfo
{
public readonly int LastPackageNumber;
public readonly bool IsIntervalStage;
public readonly TimeSpan TimeStamp;
public HeartbeatInfo(int lastPackageNumber, bool isIntervalStage, TimeSpan timeStamp)
{
LastPackageNumber = lastPackageNumber;
IsIntervalStage = isIntervalStage;
TimeStamp = timeStamp;
}
}
private struct ReconnectionInfo
{
public readonly int ReconnectionAttempt;
public readonly TimeSpan TimeStamp;
public ReconnectionInfo(int reconnectionAttempt, TimeSpan timeStamp)
{
ReconnectionAttempt = reconnectionAttempt;
TimeStamp = timeStamp;
}
}
private struct AuthInfo
{
public readonly Guid CorrelationId;
public readonly TimeSpan TimeStamp;
public AuthInfo(Guid correlationId, TimeSpan timeStamp)
{
CorrelationId = correlationId;
TimeStamp = timeStamp;
}
}
private struct IdentifyInfo
{
public readonly Guid CorrelationId;
public readonly TimeSpan TimeStamp;
public IdentifyInfo(Guid correlationId, TimeSpan timeStamp)
{
CorrelationId = correlationId;
TimeStamp = timeStamp;
}
}
private enum ConnectionState
{
Init,
Connecting,
Connected,
Closed
}
private enum ConnectingPhase
{
Invalid,
Reconnecting,
EndPointDiscovery,
ConnectionEstablishing,
Authentication,
Identification,
Connected
}
}
}