-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathAutoRecoveringConnection.cs
More file actions
379 lines (329 loc) · 15.8 KB
/
AutoRecoveringConnection.cs
File metadata and controls
379 lines (329 loc) · 15.8 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using ActiveMQ.Artemis.Client.AutoRecovering.RecoveryPolicy;
using ActiveMQ.Artemis.Client.Builders;
using ActiveMQ.Artemis.Client.Exceptions;
using ActiveMQ.Artemis.Client.InternalUtilities;
using ActiveMQ.Artemis.Client.MessageIdPolicy;
using Amqp;
using Microsoft.Extensions.Logging;
using Polly;
using Polly.Retry;
namespace ActiveMQ.Artemis.Client.AutoRecovering
{
internal class AutoRecoveringConnection : IConnection
{
private IConnection _connection;
private readonly ILoggerFactory _loggerFactory;
private readonly Func<IMessageIdPolicy> _messageIdPolicyFactory;
private readonly Func<string> _clientIdFactory;
private readonly SslSettings _sslSettings;
private readonly TcpSettings _tcpSettings;
private readonly ILogger<AutoRecoveringConnection> _logger;
private readonly Endpoint[] _endpoints;
private readonly ChannelReader<ConnectCommand> _reader;
private readonly ChannelWriter<ConnectCommand> _writer;
private readonly ConcurrentHashSet<IRecoverable> _recoverables = new();
private readonly CancellationTokenSource _recoveryCancellationToken = new();
private readonly AsyncRetryPolicy<IConnection> _connectionRetryPolicy;
private readonly Task _recoveryLoopTask;
public AutoRecoveringConnection(ILoggerFactory loggerFactory,
IEnumerable<Endpoint> endpoints,
IRecoveryPolicy recoveryPolicy,
Func<IMessageIdPolicy> messageIdPolicyFactory,
Func<string> clientIdFactory,
SslSettings sslSettings,
TcpSettings tcpSettings)
{
_logger = loggerFactory.CreateLogger<AutoRecoveringConnection>();
_loggerFactory = loggerFactory;
_messageIdPolicyFactory = messageIdPolicyFactory;
_clientIdFactory = clientIdFactory;
_sslSettings = sslSettings;
_tcpSettings = tcpSettings;
_endpoints = endpoints.ToArray();
var channel = Channel.CreateUnbounded<ConnectCommand>();
_reader = channel.Reader;
_writer = channel.Writer;
_connectionRetryPolicy = CreateConnectionRetryPolicy(recoveryPolicy);
_recoveryLoopTask = StartRecoveryLoop();
}
public Endpoint Endpoint => _connection.Endpoint;
// TODO: Probably should return false only when connection was explicitly closed.
public bool IsOpened => _connection != null && _connection.IsOpened;
private AsyncRetryPolicy<IConnection> CreateConnectionRetryPolicy(IRecoveryPolicy recoveryPolicy)
{
return Policy<IConnection>
.Handle<Exception>(exception => exception switch
{
CreateConnectionException { ErrorCode: ErrorCode.UnauthorizedAccess } => false,
_ => true
})
.WaitAndRetryAsync(recoveryPolicy.RetryCount, (retryAttempt, context) =>
{
context.SetRetryCount(retryAttempt);
return recoveryPolicy.GetDelay(retryAttempt);
}, (result, _, context) =>
{
var retryCount = context.GetRetryCount();
var endpoint = context.GetEndpoint();
if (result.Exception != null)
{
Log.FailedToEstablishConnection(_logger, endpoint, retryCount, result.Exception);
}
});
}
private Task StartRecoveryLoop()
{
return Task.Run(async () =>
{
try
{
while (true)
{
await _reader.ReadAsync(_recoveryCancellationToken.Token).ConfigureAwait(false);
if (!IsOpened)
{
await DisposeInnerConnection().ConfigureAwait(false);
foreach (var recoverable in _recoverables.Values)
{
recoverable.Suspend();
}
_connection = await CreateConnection(_recoveryCancellationToken.Token).ConfigureAwait(false);
foreach (var recoverable in _recoverables.Values)
{
try
{
await recoverable.RecoverAsync(_connection, _recoveryCancellationToken.Token).ConfigureAwait(false);
recoverable.Resume();
}
catch (Exception e)
{
await recoverable.TerminateAsync(e).ConfigureAwait(false);
}
}
_connection.ConnectionClosed += OnConnectionClosed;
Log.ConnectionRecovered(_logger);
ConnectionRecovered?.Invoke(this, new ConnectionRecoveredEventArgs(_connection.Endpoint));
}
else
{
// If the connection is already opened it means that there may be some suspended recoverables that need to be resumed
foreach (var recoverable in _recoverables.Values)
{
recoverable.Resume();
}
}
}
}
catch (OperationCanceledException)
{
// expected when recovery cancellation token is set.
}
catch (Exception e)
{
Log.MainRecoveryLoopException(_logger, e);
foreach (var recoverable in _recoverables.Values)
{
await recoverable.TerminateAsync(e).ConfigureAwait(false);
}
ConnectionRecoveryError?.Invoke(this, new ConnectionRecoveryErrorEventArgs(e));
}
});
}
private void OnConnectionClosed(object sender, ConnectionClosedEventArgs args)
{
if (args.ClosedByPeer)
{
Log.ConnectionClosedByPeer(_logger, args.Error);
_writer.TryWrite(ConnectCommand.Instance);
}
else
{
Log.ConnectionClosed(_logger);
}
ConnectionClosed?.Invoke(sender, args);
}
public async Task InitAsync(CancellationToken cancellationToken)
{
_connection = await CreateConnection(cancellationToken).ConfigureAwait(false);
_connection.ConnectionClosed += OnConnectionClosed;
}
private Task<IConnection> CreateConnection(CancellationToken cancellationToken)
{
var ctx = new Context();
ctx.SetRetryCount(0);
return _connectionRetryPolicy.ExecuteAsync(async (context, ct) =>
{
int retryCount = context.GetRetryCount();
var endpoint = GetNextEndpoint(retryCount);
context.SetEndpoint(endpoint);
var connectionBuilder = new ConnectionBuilder(_loggerFactory, _messageIdPolicyFactory, _clientIdFactory, _sslSettings, _tcpSettings);
Log.TryingToEstablishedConnection(_logger, endpoint, retryCount + 1);
var connection = await connectionBuilder.CreateAsync(endpoint, ct).ConfigureAwait(false);
Log.ConnectionEstablished(_logger, endpoint, retryCount + 1);
return connection;
}, ctx, cancellationToken);
}
public async Task<ITopologyManager> CreateTopologyManagerAsync(CancellationToken cancellationToken = default)
{
var configuration = new RequestReplyClientConfiguration
{
Address = "activemq.management"
};
var rpcClient = await CreateRequestReplyClientAsync(configuration, cancellationToken).ConfigureAwait(false);
return new TopologyManager(configuration.Address, rpcClient);
}
public async Task<IConsumer> CreateConsumerAsync(ConsumerConfiguration configuration, CancellationToken cancellationToken)
{
var autoRecoveringConsumer = new AutoRecoveringConsumer(_loggerFactory, configuration);
await PrepareRecoverable(autoRecoveringConsumer, cancellationToken).ConfigureAwait(false);
return autoRecoveringConsumer;
}
public async Task<IProducer> CreateProducerAsync(ProducerConfiguration configuration, CancellationToken cancellationToken)
{
var autoRecoveringProducer = new AutoRecoveringProducer(_loggerFactory, configuration);
await PrepareRecoverable(autoRecoveringProducer, cancellationToken).ConfigureAwait(false);
return autoRecoveringProducer;
}
public async Task<IAnonymousProducer> CreateAnonymousProducerAsync(AnonymousProducerConfiguration configuration, CancellationToken cancellationToken = default)
{
var autoRecoveringAnonymousProducer = new AutoRecoveringAnonymousProducer(_loggerFactory, configuration);
await PrepareRecoverable(autoRecoveringAnonymousProducer, cancellationToken).ConfigureAwait(false);
return autoRecoveringAnonymousProducer;
}
public async Task<IRequestReplyClient> CreateRequestReplyClientAsync(RequestReplyClientConfiguration configuration, CancellationToken cancellationToken = default)
{
var autoRecoveringRpcClient = new AutoRecoveringRequestReplyClient(_loggerFactory, configuration);
await PrepareRecoverable(autoRecoveringRpcClient, cancellationToken).ConfigureAwait(false);
return autoRecoveringRpcClient;
}
public event EventHandler<ConnectionClosedEventArgs> ConnectionClosed;
public event EventHandler<ConnectionRecoveredEventArgs> ConnectionRecovered;
public event EventHandler<ConnectionRecoveryErrorEventArgs> ConnectionRecoveryError;
private async Task PrepareRecoverable(IRecoverable recoverable, CancellationToken cancellationToken)
{
await recoverable.RecoverAsync(_connection, cancellationToken).ConfigureAwait(false);
_recoverables.Add(recoverable);
recoverable.Closed += OnRecoverableClosed;
recoverable.RecoveryRequested += OnRecoveryRequested;
}
private void OnRecoverableClosed(IRecoverable recoverable)
{
recoverable.Closed -= OnRecoverableClosed;
recoverable.RecoveryRequested -= OnRecoveryRequested;
_recoverables.Remove(recoverable);
}
private void OnRecoveryRequested()
{
_writer.TryWrite(ConnectCommand.Instance);
}
public async ValueTask DisposeAsync()
{
_recoveryCancellationToken.Cancel();
await _recoveryLoopTask.ConfigureAwait(false);
await Task.WhenAll(_recoverables.Values.Select(async x => await x.DisposeAsync().ConfigureAwait(false))).ConfigureAwait(false);
await DisposeInnerConnection().ConfigureAwait(false);
}
private async Task DisposeInnerConnection()
{
_connection.ConnectionClosed -= OnConnectionClosed;
try
{
await _connection.DisposeAsync().ConfigureAwait(false);
}
catch (Exception)
{
// ignored
}
}
private Endpoint GetNextEndpoint(int retryCount)
{
return _endpoints[retryCount % _endpoints.Length];
}
private static class Log
{
private static readonly Action<ILogger, string, int, Exception> _connectionEstablished = LoggerMessage.Define<string, int>(
LogLevel.Information,
0,
"Connection to {endpoint} established. Attempt: {attempt}.");
private static readonly Action<ILogger, string, int, Exception> _failedToEstablishConnection = LoggerMessage.Define<string, int>(
LogLevel.Warning,
0,
"Failed to establish connection to {endpoint}. Attempt: {attempt}.");
private static readonly Action<ILogger, Exception> _mainRecoveryLoopException = LoggerMessage.Define(
LogLevel.Error,
0,
"Main recovery loop threw unexpected exception.");
private static readonly Action<ILogger, string, Exception> _connectionClosedByPeer = LoggerMessage.Define<string>(
LogLevel.Warning,
0,
"Connection closed due to {error}. Reconnect scheduled.");
private static readonly Action<ILogger, Exception> _connectionClosed = LoggerMessage.Define(
LogLevel.Information,
0,
"Connection closed. Reconnect won't be scheduled.");
private static readonly Action<ILogger, Exception> _connectionRecovered = LoggerMessage.Define(
LogLevel.Information,
0,
"Connection recovered.");
private static readonly Action<ILogger, string, int, Exception> _tryingToEstablishedConnection = LoggerMessage.Define<string, int>(
LogLevel.Information,
0,
"Trying to establish connection to {endpoint}. Attempt: {attempt}.");
public static void ConnectionEstablished(ILogger logger, Endpoint endpoint, int attempt)
{
if (logger.IsEnabled(LogLevel.Information))
{
_connectionEstablished(logger, endpoint.ToString(), attempt, null);
}
}
public static void FailedToEstablishConnection(ILogger logger, Endpoint endpoint, int attempt, Exception e)
{
if (logger.IsEnabled(LogLevel.Warning))
{
_failedToEstablishConnection(logger, endpoint.ToString(), attempt, e);
}
}
public static void MainRecoveryLoopException(ILogger logger, Exception e)
{
if (logger.IsEnabled(LogLevel.Error))
{
_mainRecoveryLoopException(logger, e);
}
}
public static void ConnectionClosedByPeer(ILogger logger, string error)
{
if (logger.IsEnabled(LogLevel.Warning))
{
_connectionClosedByPeer(logger, error, null);
}
}
public static void ConnectionClosed(ILogger logger)
{
if (logger.IsEnabled(LogLevel.Information))
{
_connectionClosed(logger, null);
}
}
public static void ConnectionRecovered(ILogger logger)
{
if (logger.IsEnabled(LogLevel.Information))
{
_connectionRecovered(logger, null);
}
}
public static void TryingToEstablishedConnection(ILogger logger, Endpoint endpoint, int attempt)
{
if (logger.IsEnabled(LogLevel.Information))
{
_tryingToEstablishedConnection(logger, endpoint.ToString(), attempt, null);
}
}
}
}
}