-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathPubSubRendezvousGrain.cs
507 lines (446 loc) · 20.1 KB
/
PubSubRendezvousGrain.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Orleans.Core;
using Orleans.Providers;
using Orleans.Runtime;
using Orleans.Serialization.Serializers;
using Orleans.Storage;
using Orleans.Streams.Core;
#nullable enable
namespace Orleans.Streams
{
internal sealed class PubSubGrainStateStorageFactory
{
private readonly IServiceProvider _serviceProvider;
private readonly ILogger<PubSubGrainStateStorageFactory> _logger;
public PubSubGrainStateStorageFactory(IServiceProvider serviceProvider, ILoggerFactory loggerFactory)
{
_serviceProvider = serviceProvider;
_logger = loggerFactory.CreateLogger<PubSubGrainStateStorageFactory>();
}
public StateStorageBridge<PubSubGrainState> GetStorage(PubSubRendezvousGrain grain)
{
var span = grain.GrainId.Key.AsSpan();
var i = span.IndexOf((byte)'/');
if (i < 0)
{
throw new ArgumentException($"Unable to parse \"{grain.GrainId.Key}\" as a stream id");
}
var providerName = Encoding.UTF8.GetString(span[..i]);
if (_logger.IsEnabled(LogLevel.Debug))
{
_logger.LogDebug("Trying to find storage provider {ProviderName}", providerName);
}
var storage = _serviceProvider.GetKeyedService<IGrainStorage>(providerName);
if (storage == null)
{
if (_logger.IsEnabled(LogLevel.Debug))
{
_logger.LogDebug("Fallback to storage provider {ProviderName}", ProviderConstants.DEFAULT_PUBSUB_PROVIDER_NAME);
}
storage = _serviceProvider.GetRequiredKeyedService<IGrainStorage>(ProviderConstants.DEFAULT_PUBSUB_PROVIDER_NAME);
}
return new(nameof(PubSubRendezvousGrain), grain.GrainContext, storage);
}
}
[Serializable]
[GenerateSerializer]
internal sealed class PubSubGrainState
{
[Id(0)]
public HashSet<PubSubPublisherState> Producers { get; set; } = new HashSet<PubSubPublisherState>();
[Id(1)]
public HashSet<PubSubSubscriptionState> Consumers { get; set; } = new HashSet<PubSubSubscriptionState>();
}
[GrainType("pubsubrendezvous")]
internal sealed class PubSubRendezvousGrain : Grain, IPubSubRendezvousGrain, IGrainMigrationParticipant
{
private readonly ILogger _logger;
private const bool DEBUG_PUB_SUB = false;
private readonly PubSubGrainStateStorageFactory _storageFactory;
private readonly StateStorageBridge<PubSubGrainState> _storage;
private PubSubGrainState State => _storage.State;
public PubSubRendezvousGrain(PubSubGrainStateStorageFactory storageFactory, ILogger<PubSubRendezvousGrain> logger)
{
_storageFactory = storageFactory;
_logger = logger;
_storage = _storageFactory.GetStorage(this);
}
public override async Task OnActivateAsync(CancellationToken cancellationToken)
{
await ReadStateAsync();
LogPubSubCounts("OnActivateAsync");
}
public override Task OnDeactivateAsync(DeactivationReason reason, CancellationToken cancellationToken)
{
LogPubSubCounts("OnDeactivateAsync");
return Task.CompletedTask;
}
public async Task<ISet<PubSubSubscriptionState>> RegisterProducer(QualifiedStreamId streamId, GrainId streamProducer)
{
StreamInstruments.PubSubProducersAdded.Add(1);
try
{
var publisherState = new PubSubPublisherState(streamId, streamProducer);
State.Producers.Add(publisherState);
LogPubSubCounts("RegisterProducer {0}", streamProducer);
await WriteStateAsync();
StreamInstruments.PubSubProducersTotal.Add(1);
}
catch (Exception exc)
{
_logger.LogError(
(int)ErrorCode.Stream_RegisterProducerFailed,
exc,
"Failed to register a stream producer. Stream: {StreamId}, Producer: {StreamProducer}",
streamId,
streamProducer);
// Corrupted state, deactivate grain.
DeactivateOnIdle();
throw;
}
return State.Consumers.Where(c => !c.IsFaulted).ToSet();
}
public async Task UnregisterProducer(QualifiedStreamId streamId, GrainId streamProducer)
{
StreamInstruments.PubSubProducersRemoved.Add(1);
try
{
int numRemoved = State.Producers.RemoveWhere(s => s.Equals(streamId, streamProducer));
LogPubSubCounts("UnregisterProducer {0} NumRemoved={1}", streamProducer, numRemoved);
if (numRemoved > 0)
{
Task updateStorageTask = State.Producers.Count == 0 && State.Consumers.Count == 0
? ClearStateAsync() //State contains no producers or consumers, remove it from storage
: WriteStateAsync();
await updateStorageTask;
}
StreamInstruments.PubSubProducersTotal.Add(-numRemoved);
}
catch (Exception exc)
{
_logger.LogError(
(int)ErrorCode.Stream_UnegisterProducerFailed,
exc,
"Failed to unregister a stream producer. Stream: {StreamId}, Producer: {StreamProducer}",
streamId,
streamProducer);
// Corrupted state, deactivate grain.
DeactivateOnIdle();
throw;
}
if (State.Producers.Count == 0 && State.Consumers.Count == 0)
{
DeactivateOnIdle(); // No producers or consumers left now, so flag ourselves to expedite Deactivation
}
}
public async Task RegisterConsumer(
GuidId subscriptionId,
QualifiedStreamId streamId,
GrainId streamConsumer,
string filterData)
{
StreamInstruments.PubSubConsumersAdded.Add(1);
var pubSubState = State.Consumers.FirstOrDefault(s => s.Equals(subscriptionId));
if (pubSubState != null && pubSubState.IsFaulted)
throw new FaultedSubscriptionException(subscriptionId, streamId);
try
{
if (pubSubState == null)
{
pubSubState = new PubSubSubscriptionState(subscriptionId, streamId, streamConsumer);
State.Consumers.Add(pubSubState);
}
if (!string.IsNullOrWhiteSpace(filterData))
pubSubState.AddFilter(filterData);
LogPubSubCounts("RegisterConsumer {0}", streamConsumer);
await WriteStateAsync();
StreamInstruments.PubSubConsumersTotal.Add(1);
}
catch (Exception exc)
{
_logger.LogError(
(int)ErrorCode.Stream_RegisterConsumerFailed,
exc,
"Failed to register a stream consumer. Stream: {StreamId}, SubscriptionId {SubscriptionId}, Consumer: {StreamConsumer}",
streamId,
subscriptionId,
streamConsumer);
// Corrupted state, deactivate grain.
DeactivateOnIdle();
throw;
}
int numProducers = State.Producers.Count;
if (numProducers <= 0)
return;
if (_logger.IsEnabled(LogLevel.Debug))
_logger.LogDebug("Notifying {ProducerCount} existing producer(s) about new consumer {Consumer}. Producers={Producers}",
numProducers, streamConsumer, Utils.EnumerableToString(State.Producers));
// Notify producers about a new streamConsumer.
var tasks = new List<Task>();
var producers = State.Producers.ToList();
int initialProducerCount = producers.Count;
try
{
foreach (PubSubPublisherState producerState in producers)
{
tasks.Add(ExecuteProducerTask(producerState, p => p.AddSubscriber(subscriptionId, streamId, streamConsumer, filterData)));
}
Exception? exception = null;
try
{
await Task.WhenAll(tasks);
}
catch (Exception exc)
{
exception = exc;
}
// if the number of producers has been changed, resave state.
if (State.Producers.Count != initialProducerCount)
{
await WriteStateAsync();
StreamInstruments.PubSubConsumersTotal.Add(-(initialProducerCount - State.Producers.Count));
}
if (exception != null)
{
ExceptionDispatchInfo.Capture(exception).Throw();
}
}
catch (Exception exc)
{
_logger.LogError(
(int)ErrorCode.Stream_RegisterConsumerFailed,
exc,
"Failed to update producers while register a stream consumer. Stream: {StreamId}, SubscriptionId {SubscriptionId}, Consumer: {StreamConsumer}",
streamId,
subscriptionId,
streamConsumer);
// Corrupted state, deactivate grain.
DeactivateOnIdle();
throw;
}
}
private void RemoveProducer(PubSubPublisherState producer)
{
_logger.LogWarning(
(int)ErrorCode.Stream_ProducerIsDead,
"Producer {Producer} on stream {StreamId} is no longer active - permanently removing producer.",
producer, producer.Stream);
State.Producers.Remove(producer);
}
public async Task UnregisterConsumer(GuidId subscriptionId, QualifiedStreamId streamId)
{
StreamInstruments.PubSubConsumersRemoved.Add(1);
try
{
int numRemoved = State.Consumers.RemoveWhere(c => c.Equals(subscriptionId));
LogPubSubCounts("UnregisterSubscription {0} NumRemoved={1}", subscriptionId, numRemoved);
if (await TryClearState())
{
// If state was cleared expedite Deactivation
DeactivateOnIdle();
}
else
{
if (numRemoved != 0)
{
await WriteStateAsync();
}
await NotifyProducersOfRemovedSubscription(subscriptionId, streamId);
}
StreamInstruments.PubSubConsumersTotal.Add(-numRemoved);
}
catch (Exception exc)
{
_logger.LogError(
(int)ErrorCode.Stream_UnregisterConsumerFailed,
exc,
"Failed to unregister a stream consumer. Stream: {StreamId}, SubscriptionId {SubscriptionId}",
streamId,
subscriptionId);
// Corrupted state, deactivate grain.
DeactivateOnIdle();
throw;
}
}
public Task<int> ProducerCount(QualifiedStreamId streamId)
{
return Task.FromResult(State.Producers.Count);
}
public Task<int> ConsumerCount(QualifiedStreamId streamId)
{
return Task.FromResult(GetConsumersForStream(streamId).Length);
}
public Task<PubSubSubscriptionState[]> DiagGetConsumers(QualifiedStreamId streamId)
{
return Task.FromResult(GetConsumersForStream(streamId));
}
private PubSubSubscriptionState[] GetConsumersForStream(QualifiedStreamId streamId)
{
return State.Consumers.Where(c => !c.IsFaulted && c.Stream.Equals(streamId)).ToArray();
}
private void LogPubSubCounts(string fmt, params object[] args)
{
if (_logger.IsEnabled(LogLevel.Debug) || DEBUG_PUB_SUB)
{
int numProducers = 0;
int numConsumers = 0;
if (State?.Producers != null)
numProducers = State.Producers.Count;
if (State?.Consumers != null)
numConsumers = State.Consumers.Count;
string when = args != null && args.Length != 0 ? string.Format(fmt, args) : fmt;
_logger.LogDebug("{When}. Now have total of {ProducerCount} producers and {ConsumerCount} consumers. All Consumers = {Consumers}, All Producers = {Producers}",
when, numProducers, numConsumers, Utils.EnumerableToString(State?.Consumers), Utils.EnumerableToString(State?.Producers));
}
}
// Check that what we have cached locally matches what is in the persistent table.
public async Task Validate()
{
var captureProducers = State.Producers;
var captureConsumers = State.Consumers;
await ReadStateAsync();
if (captureProducers.Count != State.Producers.Count)
{
throw new OrleansException(
$"State mismatch between PubSubRendezvousGrain and its persistent state. captureProducers.Count={captureProducers.Count}, State.Producers.Count={State.Producers.Count}");
}
if (captureProducers.Any(producer => !State.Producers.Contains(producer)))
{
throw new OrleansException(
$"State mismatch between PubSubRendezvousGrain and its persistent state. captureProducers={Utils.EnumerableToString(captureProducers)}, State.Producers={Utils.EnumerableToString(State.Producers)}");
}
if (captureConsumers.Count != State.Consumers.Count)
{
LogPubSubCounts("Validate: Consumer count mismatch");
throw new OrleansException(
$"State mismatch between PubSubRendezvousGrain and its persistent state. captureConsumers.Count={captureConsumers.Count}, State.Consumers.Count={State.Consumers.Count}");
}
if (captureConsumers.Any(consumer => !State.Consumers.Contains(consumer)))
{
throw new OrleansException(
$"State mismatch between PubSubRendezvousGrain and its persistent state. captureConsumers={Utils.EnumerableToString(captureConsumers)}, State.Consumers={Utils.EnumerableToString(State.Consumers)}");
}
}
public Task<List<StreamSubscription>> GetAllSubscriptions(QualifiedStreamId streamId, GrainId streamConsumer)
{
if (streamConsumer != default)
{
List<StreamSubscription> subscriptions =
State.Consumers.Where(c => !c.IsFaulted && c.Consumer.Equals(streamConsumer))
.Select(
c =>
new StreamSubscription(c.SubscriptionId.Guid, streamId.ProviderName, streamId,
streamConsumer)).ToList();
return Task.FromResult(subscriptions);
}
else
{
List<StreamSubscription> subscriptions =
State.Consumers.Where(c => !c.IsFaulted)
.Select(
c =>
new StreamSubscription(c.SubscriptionId.Guid, streamId.ProviderName, streamId,
c.Consumer)).ToList();
return Task.FromResult(subscriptions);
}
}
public async Task FaultSubscription(GuidId subscriptionId)
{
var pubSubState = State.Consumers.FirstOrDefault(s => s.Equals(subscriptionId));
if (pubSubState == null)
{
return;
}
try
{
pubSubState.Fault();
if (_logger.IsEnabled(LogLevel.Debug)) _logger.LogDebug("Setting subscription {SubscriptionId} to a faulted state.", subscriptionId);
await WriteStateAsync();
await NotifyProducersOfRemovedSubscription(pubSubState.SubscriptionId, pubSubState.Stream);
}
catch (Exception exc)
{
_logger.LogError(
(int)ErrorCode.Stream_SetSubscriptionToFaultedFailed,
exc,
"Failed to set subscription state to faulted. SubscriptionId {SubscriptionId}",
subscriptionId);
// Corrupted state, deactivate grain.
DeactivateOnIdle();
throw;
}
}
private async Task NotifyProducersOfRemovedSubscription(GuidId subscriptionId, QualifiedStreamId streamId)
{
int numProducersBeforeNotify = State.Producers.Count;
if (numProducersBeforeNotify > 0)
{
if (_logger.IsEnabled(LogLevel.Debug)) _logger.LogDebug("Notifying {ProducerCountBeforeNotify} existing producers about unregistered consumer.", numProducersBeforeNotify);
// Notify producers about unregistered consumer.
List<Task> tasks = State.Producers
.Select(producerState => ExecuteProducerTask(producerState, p => p.RemoveSubscriber(subscriptionId, streamId)))
.ToList();
await Task.WhenAll(tasks);
//if producers got removed
if (State.Producers.Count < numProducersBeforeNotify)
await WriteStateAsync();
}
}
/// <summary>
/// Try clear state will only clear the state if there are no producers or consumers.
/// </summary>
/// <returns></returns>
private async Task<bool> TryClearState()
{
if (State.Producers.Count == 0 && State.Consumers.Count == 0) // + we already know that numProducers == 0 from previous if-clause
{
await ClearStateAsync(); //State contains no producers or consumers, remove it from storage
return true;
}
return false;
}
private async Task ExecuteProducerTask(PubSubPublisherState producer, Func<IStreamProducerExtension, Task> producerTask)
{
try
{
var extension = GrainFactory
.GetGrain(producer.Producer)
.AsReference<IStreamProducerExtension>();
await producerTask(extension);
}
catch (GrainExtensionNotInstalledException)
{
RemoveProducer(producer);
}
catch (ClientNotAvailableException)
{
RemoveProducer(producer);
}
catch (OrleansMessageRejectionException)
{
// if producer is a system target on and unavailable silo, remove it.
if (producer.Producer.IsSystemTarget())
{
RemoveProducer(producer);
}
else // otherwise, throw
{
throw;
}
}
}
private Task ReadStateAsync() => _storage.ReadStateAsync();
private Task WriteStateAsync() => _storage.WriteStateAsync();
private Task ClearStateAsync() => _storage.ClearStateAsync();
void IGrainMigrationParticipant.OnDehydrate(IDehydrationContext dehydrationContext) => _storage.OnDehydrate(dehydrationContext);
void IGrainMigrationParticipant.OnRehydrate(IRehydrationContext rehydrationContext) => _storage.OnRehydrate(rehydrationContext);
}
}