-
Notifications
You must be signed in to change notification settings - Fork 833
Expand file tree
/
Copy pathConnectionManager.cs
More file actions
587 lines (507 loc) · 22.7 KB
/
ConnectionManager.cs
File metadata and controls
587 lines (507 loc) · 22.7 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
#region Copyright notice and license
// Copyright 2019 The gRPC Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion
#if SUPPORT_LOAD_BALANCING
using System.Diagnostics;
using Grpc.Core;
using Grpc.Net.Client.Configuration;
using Grpc.Net.Client.Internal;
using Microsoft.Extensions.Logging;
namespace Grpc.Net.Client.Balancer.Internal;
internal sealed class ConnectionManager : IDisposable, IChannelControlHelper
{
public static readonly BalancerAttributesKey<string> HostOverrideKey = new BalancerAttributesKey<string>("HostOverride");
private static readonly ChannelIdProvider _channelIdProvider = new ChannelIdProvider();
private readonly Lock _lock;
internal readonly Resolver _resolver;
private readonly ISubchannelTransportFactory _subchannelTransportFactory;
private readonly List<Subchannel> _subchannels;
private readonly List<StateWatcher> _stateWatchers;
private readonly TaskCompletionSource<object?> _resolverStartedTcs;
private readonly long _channelId;
// Internal for testing
internal LoadBalancer? _balancer;
internal SubchannelPicker? _picker;
// Cache picker wrapped in task once and reuse.
private Task<SubchannelPicker>? _pickerTask;
private bool _resolverStarted;
private TaskCompletionSource<SubchannelPicker> _nextPickerTcs;
private int _currentSubchannelId;
private ServiceConfig? _previousServiceConfig;
internal ConnectionManager(
Resolver resolver,
bool disableResolverServiceConfig,
ILoggerFactory loggerFactory,
IBackoffPolicyFactory backoffPolicyFactory,
ISubchannelTransportFactory subchannelTransportFactory,
LoadBalancerFactory[] loadBalancerFactories)
{
_lock = new Lock();
_nextPickerTcs = new TaskCompletionSource<SubchannelPicker>(TaskCreationOptions.RunContinuationsAsynchronously);
_resolverStartedTcs = new TaskCompletionSource<object?>(TaskCreationOptions.RunContinuationsAsynchronously);
_channelId = _channelIdProvider.GetNextChannelId();
Logger = loggerFactory.CreateLogger(typeof(ConnectionManager));
LoggerFactory = loggerFactory;
BackoffPolicyFactory = backoffPolicyFactory;
_subchannels = new List<Subchannel>();
_stateWatchers = new List<StateWatcher>();
_resolver = resolver;
DisableResolverServiceConfig = disableResolverServiceConfig;
_subchannelTransportFactory = subchannelTransportFactory;
LoadBalancerFactories = loadBalancerFactories;
}
public ConnectivityState State { get; private set; }
public ILogger Logger { get; }
public ILoggerFactory LoggerFactory { get; }
public IBackoffPolicyFactory BackoffPolicyFactory { get; }
public bool DisableResolverServiceConfig { get; }
public LoadBalancerFactory[] LoadBalancerFactories { get; }
// For unit tests.
internal IReadOnlyList<Subchannel> GetSubchannels()
{
lock (_subchannels)
{
return _subchannels.ToArray();
}
}
internal string GetNextId()
{
var nextSubchannelId = Interlocked.Increment(ref _currentSubchannelId);
return $"{_channelId}-{nextSubchannelId}";
}
public void ConfigureBalancer(Func<IChannelControlHelper, LoadBalancer> configure)
{
_balancer = configure(this);
}
Subchannel IChannelControlHelper.CreateSubchannel(SubchannelOptions options)
{
var subchannel = new Subchannel(this, options.Addresses);
subchannel.SetTransport(_subchannelTransportFactory.Create(subchannel));
lock (_subchannels)
{
_subchannels.Add(subchannel);
}
return subchannel;
}
void IChannelControlHelper.RefreshResolver()
{
_resolver.Refresh();
}
private void OnResolverResult(ResolverResult result)
{
if (_balancer == null)
{
throw new InvalidOperationException($"Load balancer not configured.");
}
var channelStatus = result.Status;
// https://github.com/grpc/proposal/blob/master/A21-service-config-error-handling.md
// Additionally, only use resolved service config if not disabled.
LoadBalancingConfig? loadBalancingConfig = null;
if (!DisableResolverServiceConfig)
{
ServiceConfig? workingServiceConfig = null;
if (result.ServiceConfig == null)
{
// Step 4 and 5
if (result.ServiceConfigStatus == null)
{
// Step 5: Use default service config if none is provided.
workingServiceConfig = new ServiceConfig();
_previousServiceConfig = workingServiceConfig;
}
else
{
// Step 4
if (_previousServiceConfig == null)
{
// Step 4.ii: If no config was provided or set previously, then treat resolution as a failure.
channelStatus = result.ServiceConfigStatus.Value;
}
else
{
// Step 4.i: Continue using previous service config if it was set and a new one is not provided.
workingServiceConfig = _previousServiceConfig;
ConnectionManagerLog.ResolverServiceConfigFallback(Logger, result.ServiceConfigStatus.Value);
}
}
}
else
{
// Step 3: Use provided service config if it is set.
workingServiceConfig = result.ServiceConfig;
_previousServiceConfig = result.ServiceConfig;
}
if (workingServiceConfig?.LoadBalancingConfigs.Count > 0)
{
if (!ChildHandlerLoadBalancer.TryGetValidServiceConfigFactory(workingServiceConfig.LoadBalancingConfigs, LoadBalancerFactories, out loadBalancingConfig, out var _))
{
ConnectionManagerLog.ResolverUnsupportedLoadBalancingConfig(Logger, workingServiceConfig.LoadBalancingConfigs);
}
}
}
else
{
if (result.ServiceConfig != null)
{
ConnectionManagerLog.ResolverServiceConfigNotUsed(Logger);
}
}
var state = new ChannelState(
channelStatus,
result.Addresses,
loadBalancingConfig,
BalancerAttributes.Empty);
lock (_lock)
{
_balancer.UpdateChannelState(state);
_resolverStartedTcs.TrySetResult(null);
}
}
internal void OnSubchannelStateChange(Subchannel subchannel, ConnectivityState state, Status status)
{
if (state == ConnectivityState.Shutdown)
{
lock (_subchannels)
{
var removed = _subchannels.Remove(subchannel);
Debug.Assert(removed);
}
}
lock (_lock)
{
subchannel.RaiseStateChanged(state, status);
}
}
public async Task ConnectAsync(bool waitForReady, CancellationToken cancellationToken)
{
await EnsureResolverStartedAsync().ConfigureAwait(false);
if (!waitForReady || State == ConnectivityState.Ready)
{
return;
}
else
{
Task waitForReadyTask;
_lock.Enter();
try
{
var state = State;
if (state == ConnectivityState.Ready)
{
return;
}
waitForReadyTask = WaitForStateChangedAsync(state, waitForState: ConnectivityState.Ready, cancellationToken);
_balancer?.RequestConnection();
}
finally
{
_lock.Exit();
}
await waitForReadyTask.ConfigureAwait(false);
}
}
private Task EnsureResolverStartedAsync()
{
// Ensure that the resolver has started and has resolved at least once.
// This ensures an inner load balancer has been created and is running.
if (!_resolverStarted)
{
lock (_lock)
{
if (!_resolverStarted)
{
_resolver.Start(OnResolverResult);
_resolver.Refresh();
_resolverStarted = true;
}
}
}
return _resolverStartedTcs.Task;
}
public void UpdateState(BalancerState state)
{
lock (_lock)
{
if (State != state.ConnectivityState)
{
ConnectionManagerLog.ChannelStateUpdated(Logger, state.ConnectivityState);
State = state.ConnectivityState;
// Iterate in reverse to reduce shifting items in the list as watchers are removed.
for (var i = _stateWatchers.Count - 1; i >= 0; i--)
{
var stateWatcher = _stateWatchers[i];
// Trigger watcher if either:
// 1. Watcher is waiting for any state change.
// 2. The state change matches the watcher's.
if (stateWatcher.WaitForState == null || stateWatcher.WaitForState == State)
{
_stateWatchers.RemoveAt(i);
stateWatcher.Tcs.SetResult(null);
}
}
}
if (!Equals(_picker, state.Picker))
{
ConnectionManagerLog.ChannelPickerUpdated(Logger);
_picker = state.Picker;
_pickerTask = Task.FromResult(state.Picker);
_nextPickerTcs.SetResult(state.Picker);
_nextPickerTcs = new TaskCompletionSource<SubchannelPicker>(TaskCreationOptions.RunContinuationsAsynchronously);
}
}
}
public async ValueTask<(Subchannel Subchannel, BalancerAddress Address, ISubchannelCallTracker? SubchannelCallTracker)> PickAsync(PickContext context, bool waitForReady, CancellationToken cancellationToken)
{
SubchannelPicker? previousPicker = null;
// Wait for a valid picker. When the client state changes a new picker will be returned.
// Cancellation will break out of the loop. Typically cancellation will come from a
// deadline specified for a call being exceeded.
while (true)
{
var currentPicker = await GetPickerAsync(previousPicker, cancellationToken).ConfigureAwait(false);
ConnectionManagerLog.PickStarted(Logger);
var result = currentPicker.Pick(context);
switch (result.Type)
{
case PickResultType.Complete:
var subchannel = result.Subchannel!;
var (address, state) = subchannel.GetAddressAndState();
if (address != null)
{
if (state == ConnectivityState.Ready)
{
ConnectionManagerLog.PickResultSuccessful(Logger, subchannel.Id, address, subchannel.Transport.TransportStatus);
return (subchannel, address, result.SubchannelCallTracker);
}
else
{
ConnectionManagerLog.PickResultSubchannelNotReady(Logger, subchannel.Id, address, state);
previousPicker = currentPicker;
}
}
else
{
ConnectionManagerLog.PickResultSubchannelNoCurrentAddress(Logger, subchannel.Id);
previousPicker = currentPicker;
}
break;
case PickResultType.Queue:
ConnectionManagerLog.PickResultQueued(Logger);
previousPicker = currentPicker;
break;
case PickResultType.Fail:
if (waitForReady)
{
ConnectionManagerLog.PickResultFailureWithWaitForReady(Logger, result.Status);
previousPicker = currentPicker;
}
else
{
ConnectionManagerLog.PickResultFailure(Logger, result.Status);
throw new RpcException(result.Status);
}
break;
case PickResultType.Drop:
// Use metadata on the exception to signal the request was dropped.
// Metadata is checked by retry. If request was dropped then it isn't retried.
var metadata = new Metadata { new Metadata.Entry(GrpcProtocolConstants.DropRequestTrailer, bool.TrueString) };
throw new RpcException(result.Status, metadata);
default:
throw new InvalidOperationException($"Unexpected pick result type: {result.Type}");
}
}
}
private Task<SubchannelPicker> GetPickerAsync(SubchannelPicker? currentPicker, CancellationToken cancellationToken)
{
lock (_lock)
{
if (_picker != null && _picker != currentPicker)
{
Debug.Assert(_pickerTask != null);
return _pickerTask;
}
else
{
ConnectionManagerLog.PickWaiting(Logger);
return _nextPickerTcs.Task.WaitAsync(cancellationToken);
}
}
}
internal Task WaitForStateChangedAsync(ConnectivityState lastObservedState, ConnectivityState? waitForState, CancellationToken cancellationToken)
{
StateWatcher? watcher;
lock (_lock)
{
if (State != lastObservedState)
{
return Task.CompletedTask;
}
else
{
// Minor optimization to check if we're already waiting for state change
// using the specified cancellation token.
foreach (var stateWatcher in _stateWatchers)
{
if (stateWatcher.CancellationToken == cancellationToken &&
stateWatcher.WaitForState == waitForState)
{
return stateWatcher.Tcs.Task;
}
}
watcher = new StateWatcher(
cancellationToken,
waitForState,
new TaskCompletionSource<object?>(TaskCreationOptions.RunContinuationsAsynchronously));
_stateWatchers.Add(watcher);
}
}
return WaitForStateChangedAsyncCore(watcher);
}
private async Task WaitForStateChangedAsyncCore(StateWatcher watcher)
{
using (watcher.CancellationToken.Register(OnCancellation, watcher))
{
await watcher.Tcs.Task.ConfigureAwait(false);
}
}
private void OnCancellation(object? s)
{
lock (_lock)
{
StateWatcher watcher = (StateWatcher)s!;
if (_stateWatchers.Remove(watcher))
{
watcher.Tcs.SetCanceled(watcher.CancellationToken);
}
}
}
// Use a standard class for the watcher because:
// 1. On cancellation, a watcher is removed from collection. Should use default Equals implementation. Record overrides Equals.
// 2. This type is cast to object. A struct will box.
private sealed class StateWatcher
{
public StateWatcher(CancellationToken cancellationToken, ConnectivityState? waitForState, TaskCompletionSource<object?> tcs)
{
CancellationToken = cancellationToken;
WaitForState = waitForState;
Tcs = tcs;
}
public CancellationToken CancellationToken { get; }
public ConnectivityState? WaitForState { get; }
public TaskCompletionSource<object?> Tcs { get; }
}
public void Dispose()
{
_resolver.Dispose();
lock (_lock)
{
_balancer?.Dispose();
// Cancel pending state watchers.
// Iterate in reverse to reduce shifting items in the list as watchers are removed.
for (var i = _stateWatchers.Count - 1; i >= 0; i--)
{
var stateWatcher = _stateWatchers[i];
stateWatcher.Tcs.SetCanceled();
_stateWatchers.RemoveAt(i);
}
}
}
}
internal static class ConnectionManagerLog
{
private static readonly Action<ILogger, string, Exception?> _resolverUnsupportedLoadBalancingConfig =
LoggerMessage.Define<string>(LogLevel.Warning, new EventId(1, "ResolverUnsupportedLoadBalancingConfig"), "Service config returned by the resolver contains unsupported load balancer policies: {LoadBalancingConfigs}. Load balancer unchanged.");
private static readonly Action<ILogger, Exception?> _resolverServiceConfigNotUsed =
LoggerMessage.Define(LogLevel.Debug, new EventId(2, "ResolverServiceConfigNotUsed"), "Service config returned by the resolver not used.");
private static readonly Action<ILogger, ConnectivityState, Exception?> _channelStateUpdated =
LoggerMessage.Define<ConnectivityState>(LogLevel.Debug, new EventId(3, "ChannelStateUpdated"), "Channel state updated to {State}.");
private static readonly Action<ILogger, Exception?> _channelPickerUpdated =
LoggerMessage.Define(LogLevel.Debug, new EventId(4, "ChannelPickerUpdated"), "Channel picker updated.");
private static readonly Action<ILogger, Exception?> _pickStarted =
LoggerMessage.Define(LogLevel.Trace, new EventId(5, "PickStarted"), "Pick started.");
private static readonly Action<ILogger, string, BalancerAddress, TransportStatus, Exception?> _pickResultSuccessful =
LoggerMessage.Define<string, BalancerAddress, TransportStatus>(LogLevel.Debug, new EventId(6, "PickResultSuccessful"), "Successfully picked subchannel id '{SubchannelId}' with address {CurrentAddress}. Transport status: {TransportStatus}");
private static readonly Action<ILogger, string, Exception?> _pickResultSubchannelNoCurrentAddress =
LoggerMessage.Define<string>(LogLevel.Debug, new EventId(7, "PickResultSubchannelNoCurrentAddress"), "Picked subchannel id '{SubchannelId}' doesn't have a current address.");
private static readonly Action<ILogger, Exception?> _pickResultQueued =
LoggerMessage.Define(LogLevel.Debug, new EventId(8, "PickResultQueued"), "Picked queued.");
private static readonly Action<ILogger, Status, Exception?> _pickResultFailure =
LoggerMessage.Define<Status>(LogLevel.Debug, new EventId(9, "PickResultFailure"), "Picked failure with status: {Status}");
private static readonly Action<ILogger, Status, Exception?> _pickResultFailureWithWaitForReady =
LoggerMessage.Define<Status>(LogLevel.Debug, new EventId(10, "PickResultFailureWithWaitForReady"), "Picked failure with status: {Status}. Retrying because wait for ready is enabled.");
private static readonly Action<ILogger, Exception?> _pickWaiting =
LoggerMessage.Define(LogLevel.Trace, new EventId(11, "PickWaiting"), "Waiting for a new picker.");
private static readonly Action<ILogger, Status, Exception?> _resolverServiceConfigFallback =
LoggerMessage.Define<Status>(LogLevel.Debug, new EventId(12, "ResolverServiceConfigFallback"), "Falling back to previously loaded service config. Resolver failure when retreiving or parsing service config with status: {Status}");
private static readonly Action<ILogger, string, BalancerAddress, ConnectivityState, Exception?> _pickResultSubchannelNotReady =
LoggerMessage.Define<string, BalancerAddress, ConnectivityState>(LogLevel.Debug, new EventId(13, "PickResultSubchannelNotReady"), "Picked subchannel id '{SubchannelId}' with address {CurrentAddress} doesn't have a ready state. Subchannel state: {State}");
public static void ResolverUnsupportedLoadBalancingConfig(ILogger logger, IList<LoadBalancingConfig> loadBalancingConfigs)
{
if (logger.IsEnabled(LogLevel.Warning))
{
var loadBalancingConfigText = string.Join(", ", loadBalancingConfigs.Select(c => $"'{c.PolicyName}'"));
_resolverUnsupportedLoadBalancingConfig(logger, loadBalancingConfigText, null);
}
}
public static void ResolverServiceConfigNotUsed(ILogger logger)
{
_resolverServiceConfigNotUsed(logger, null);
}
public static void ChannelStateUpdated(ILogger logger, ConnectivityState connectivityState)
{
_channelStateUpdated(logger, connectivityState, null);
}
public static void ChannelPickerUpdated(ILogger logger)
{
_channelPickerUpdated(logger, null);
}
public static void PickStarted(ILogger logger)
{
_pickStarted(logger, null);
}
public static void PickResultSuccessful(ILogger logger, string subchannelId, BalancerAddress currentAddress, TransportStatus transportStatus)
{
_pickResultSuccessful(logger, subchannelId, currentAddress, transportStatus, null);
}
public static void PickResultSubchannelNoCurrentAddress(ILogger logger, string subchannelId)
{
_pickResultSubchannelNoCurrentAddress(logger, subchannelId, null);
}
public static void PickResultQueued(ILogger logger)
{
_pickResultQueued(logger, null);
}
public static void PickResultFailure(ILogger logger, Status status)
{
_pickResultFailure(logger, status, null);
}
public static void PickResultFailureWithWaitForReady(ILogger logger, Status status)
{
_pickResultFailureWithWaitForReady(logger, status, null);
}
public static void PickWaiting(ILogger logger)
{
_pickWaiting(logger, null);
}
public static void ResolverServiceConfigFallback(ILogger logger, Status status)
{
_resolverServiceConfigFallback(logger, status, null);
}
public static void PickResultSubchannelNotReady(ILogger logger, string subchannelId, BalancerAddress currentAddress, ConnectivityState state)
{
_pickResultSubchannelNotReady(logger, subchannelId, currentAddress, state, null);
}
}
#endif