-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathActorCell.FaultHandling.cs
469 lines (422 loc) · 17.7 KB
/
ActorCell.FaultHandling.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
//-----------------------------------------------------------------------
// <copyright file="ActorCell.FaultHandling.cs" company="Akka.NET Project">
// Copyright (C) 2009-2016 Typesafe Inc. <http://www.typesafe.com>
// Copyright (C) 2013-2016 Akka.NET project <https://github.com/akkadotnet/akka.net>
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.ExceptionServices;
using Akka.Actor.Internal;
using Akka.Dispatch.SysMsg;
using Akka.Event;
using Akka.Util.Internal;
namespace Akka.Actor
{
public partial class ActorCell
{
private void SuspendNonRecursive()
{
Mailbox.Suspend();
}
private void ResumeNonRecursive()
{
UseThreadContext(() => Mailbox.Resume());
}
// ReSharper disable once InconsistentNaming
private IActorRef _failed_DoNotUseMeDirectly;
private bool IsFailed { get { return _failed_DoNotUseMeDirectly != null; } }
private void SetFailed(IActorRef perpetrator)
{
_failed_DoNotUseMeDirectly = perpetrator;
}
private void ClearFailed()
{
_failed_DoNotUseMeDirectly = null;
}
private IActorRef Perpetrator { get { return _failed_DoNotUseMeDirectly; } }
/// <summary>Re-create the actor in response to a failure.</summary>
private void FaultRecreate(Exception cause)
{
if (_actor == null)
{
_systemImpl.EventStream.Publish(new Error(null, _self.Path.ToString(), GetType(), "Changing Recreate into Create after " + cause));
FaultCreate();
return;
}
if (IsNormal)
{
var failedActor = _actor;
if (System.Settings.DebugLifecycle)
Publish(new Debug(_self.Path.ToString(), failedActor.GetType(), "Restarting"));
if (failedActor != null)
{
var optionalMessage = CurrentMessage;
try
{
// if the actor fails in preRestart, we can do nothing but log it: it’s best-effort
failedActor.AroundPreRestart(cause, optionalMessage);
// run actor pre-incarnation plugin pipeline
var pipeline = _systemImpl.ActorPipelineResolver.ResolvePipeline(failedActor.GetType());
pipeline.BeforeActorIncarnated(failedActor, this);
}
catch (Exception e)
{
HandleNonFatalOrInterruptedException(() =>
{
var ex = new PreRestartException(_self, e, cause, optionalMessage);
Publish(new Error(ex, _self.Path.ToString(), failedActor.GetType(), e.Message));
});
}
finally
{
ClearActor(_actor);
}
}
global::System.Diagnostics.Debug.Assert(Mailbox.IsSuspended, "Mailbox must be suspended during restart, status=" + Mailbox.Status);
if (!SetChildrenTerminationReason(new SuspendReason.Recreation(cause)))
{
FinishRecreate(cause, failedActor);
}
}
else
{
// need to keep that suspend counter balanced
FaultResume(null);
}
}
/// <summary>
/// Suspends the actor in response to a failure of a parent (i.e. the "recursive suspend" feature).
/// </summary>
private void FaultSuspend()
{
SuspendNonRecursive();
SuspendChildren();
}
/// <summary>
/// Resumes the actor in response to a failure
/// </summary>
/// <param name="causedByFailure">The exception that caused the failure. signifies if it was our own failure
/// which prompted this action.</param>
private void FaultResume(Exception causedByFailure)
{
if (_actor == null)
{
_systemImpl.EventStream.Publish(new Error(null, _self.Path.ToString(), GetType(), "Changing Resume into Create after " + causedByFailure));
FaultCreate();
}
//Akka Jvm does the following commented section as well, but we do not store the context inside the actor so it's not applicable
// else if (_actor.context == null && causedByFailure != null)
// {
// system.eventStream.publish(Error(self.path.toString, clazz(actor), "changing Resume into Restart after " + causedByFailure))
// faultRecreate(causedByFailure)
// }
else
{
var perp = Perpetrator;
// done always to keep that suspend counter balanced
// must happen "atomically"
try
{
ResumeNonRecursive();
}
finally
{
if (causedByFailure != null)
ClearFailed();
}
ResumeChildren(causedByFailure, perp);
}
}
/// <summary>
/// Create the actor in response to a failure
/// </summary>
private void FaultCreate()
{
global::System.Diagnostics.Debug.Assert(Mailbox.IsSuspended, "Mailbox must be suspended during failed creation, status=" + Mailbox.Status);
global::System.Diagnostics.Debug.Assert(_self.Equals(Perpetrator), "Perpetrator should be self");
SetReceiveTimeout(null);
CancelReceiveTimeout();
// stop all children, which will turn childrenRefs into TerminatingChildrenContainer (if there are children)
StopChildren();
if (!SetChildrenTerminationReason(new SuspendReason.Creation()))
FinishCreate();
}
private void FinishCreate()
{
try
{
ResumeNonRecursive();
}
finally
{
ClearFailed();
}
try
{
Create(failure: null);
}
catch (Exception e)
{
HandleNonFatalOrInterruptedException(() => HandleInvokeFailure(e));
}
}
/// <summary>Terminates this instance.</summary>
private void Terminate()
{
SetReceiveTimeout(null);
CancelReceiveTimeout();
// prevent Deadletter(Terminated) messages
UnwatchWatchedActors(_actor);
StopChildren();
//TODO: Implement when we have ActorSystem.Abort
// if (systemImpl.aborting) {
// // separate iteration because this is a very rare case that should not penalize normal operation
// children foreach {
// case ref: ActorRefScope if !ref.isLocal ⇒ self.sendSystemMessage(DeathWatchNotification(ref, true, false))
// case _ ⇒
// }
// }
var wasTerminating = IsTerminating;
if (SetChildrenTerminationReason(SuspendReason.Termination.Instance))
{
if (!wasTerminating)
{
// do not process normal messages while waiting for all children to terminate
SuspendNonRecursive();
// do not propagate failures during shutdown to the supervisor
SetFailed(_self);
if (System.Settings.DebugLifecycle)
Publish(new Debug(_self.Path.ToString(), ActorType, "Stopping"));
}
}
else
{
SetTerminated();
FinishTerminate();
}
}
private void HandleInvokeFailure(Exception cause, IEnumerable<IActorRef> childrenNotToSuspend = null)
{
// prevent any further messages to be processed until the actor has been restarted
if (!IsFailed)
{
try
{
SuspendNonRecursive();
if (CurrentMessage is Failed)
{
var failedChild = Sender;
childrenNotToSuspend = childrenNotToSuspend.Concat(failedChild); //Function handles childrenNotToSuspend being null
SetFailed(failedChild);
}
else
{
SetFailed(_self);
}
SuspendChildren(childrenNotToSuspend == null ? null : childrenNotToSuspend.ToList());
//Tell supervisor
Parent.Tell(new Failed(_self, cause, _self.Path.Uid));
}
catch (Exception e)
{
HandleNonFatalOrInterruptedException(() =>
{
Publish(new Error(e, _self.Path.ToString(), _actor.GetType(), "Emergency stop: exception in failure handling for " + cause));
try
{
StopChildren();
}
finally
{
FinishTerminate();
}
});
}
}
}
private void StopChildren()
{
foreach (var child in ChildrenContainer.Children)
{
child.Stop();
}
}
private void FinishTerminate()
{
// The following order is crucial for things to work properly. Only change this if you're very confident and lucky.
//
// Please note that if a parent is also a watcher then ChildTerminated and Terminated must be processed in this
// specific order.
var a = _actor;
try
{
if (a != null)
{
a.AroundPostStop();
// run actor pre-incarnation plugin pipeline
var pipeline = _systemImpl.ActorPipelineResolver.ResolvePipeline(a.GetType());
pipeline.BeforeActorIncarnated(a, this);
}
}
catch (Exception x)
{
HandleNonFatalOrInterruptedException(
() => Publish(new Error(x, _self.Path.ToString(), ActorType, x.Message)));
}
finally
{
try
//TODO: Akka Jvm: this is done in a call to dispatcher.detach()
{
//TODO: Akka Jvm: this is done in a call to MessageDispatcher.detach()
{
var mailbox = Mailbox;
var deadLetterMailbox = System.Mailboxes.DeadLetterMailbox;
SwapMailbox(deadLetterMailbox);
mailbox.BecomeClosed();
mailbox.CleanUp();
Dispatcher.Detach(this);
}
}
finally
{
try { Parent.Tell(new DeathWatchNotification(_self, existenceConfirmed: true, addressTerminated: false)); }
finally
{
try { TellWatchersWeDied(); }
finally
{
try { UnwatchWatchedActors(a); }
finally
{
if (System.Settings.DebugLifecycle)
Publish(new Debug(_self.Path.ToString(), ActorType, "Stopped"));
ClearActor(a);
ClearActorCell();
_actor = null;
}
}
}
}
}
}
private void FinishRecreate(Exception cause, ActorBase failedActor)
{
// need to keep a snapshot of the surviving children before the new actor instance creates new ones
var survivors = ChildrenContainer.Children;
try
{
try { ResumeNonRecursive(); }
finally { ClearFailed(); } // must happen in any case, so that failure is propagated
var freshActor = NewActor();
_actor = freshActor; // this must happen before postRestart has a chance to fail
if (ReferenceEquals(freshActor, failedActor))
SetActorFields(freshActor); // If the creator returns the same instance, we need to restore our nulled out fields.
UseThreadContext(() => freshActor.AroundPostRestart(cause, null));
if (System.Settings.DebugLifecycle)
Publish(new Debug(_self.Path.ToString(), freshActor.GetType(), "Restarted (" + freshActor + ")"));
// only after parent is up and running again do restart the children which were not stopped
foreach (var survivingChild in survivors)
{
try
{
survivingChild.Restart(cause);
}
catch (Exception e)
{
var child = survivingChild; //Needed since otherwise it would be access to foreach variable in closure
HandleNonFatalOrInterruptedException(() => Publish(new Error(e, _self.Path.ToString(), freshActor.GetType(), "restarting (" + child + ")")));
}
}
}
catch (Exception e)
{
ClearActor(_actor); // in order to prevent preRestart() from happening again
HandleInvokeFailure(new PostRestartException(_self, e, cause), survivors);
}
}
private void HandleFailed(Failed f) //Called handleFailure in Akka JVM
{
CurrentMessage = f;
var failedChild = f.Child;
var failedChildIsNobody = failedChild.IsNobody();
Sender = failedChildIsNobody ? _systemImpl.DeadLetters : failedChild;
//Only act upon the failure, if it comes from a currently known child;
//the UID protects against reception of a Failed from a child which was
//killed in preRestart and re-created in postRestart
ChildRestartStats childStats;
if (TryGetChildStatsByRef(failedChild, out childStats))
{
var statsUid = childStats.Child.Path.Uid;
if (statsUid == f.Uid)
{
var handled = _actor.SupervisorStrategyInternal.HandleFailure(this, f.Cause, childStats, ChildrenContainer.Stats);
if (!handled)
ExceptionDispatchInfo.Capture(f.Cause).Throw();
}
else
{
Publish(new Debug(_self.Path.ToString(), _actor.GetType(), "Dropping Failed(" + f.Cause + ") from old child " + f.Child + " (uid=" + statsUid + " != " + f.Uid + ")"));
}
}
else
{
Publish(new Debug(_self.Path.ToString(), _actor.GetType(), "Dropping Failed(" + f.Cause + ") from unknown child " + failedChild));
}
}
private void HandleChildTerminated(IActorRef child)
{
var status = RemoveChildAndGetStateChange(child);
//If this fails, we do nothing in case of terminating/restarting state,
//otherwise tell the supervisor etc. (in that second case, the match
//below will hit the empty default case, too)
if (_actor != null)
{
try
{
_actor.SupervisorStrategyInternal.HandleChildTerminated(this, child, GetChildren());
}
catch (Exception e)
{
HandleNonFatalOrInterruptedException(() =>
{
Publish(new Error(e, _self.Path.ToString(), _actor.GetType(), "HandleChildTerminated failed"));
HandleInvokeFailure(e);
});
}
}
// if the removal changed the state of the (terminating) children container,
// then we are continuing the previously suspended recreate/create/terminate action
var recreation = status as SuspendReason.Recreation;
if (recreation != null)
{
FinishRecreate(recreation.Cause,_actor);
}
else if (status is SuspendReason.Creation)
{
FinishCreate();
}
else if (status is SuspendReason.Termination)
{
FinishTerminate();
}
}
/// <summary>
/// Handles the non fatal or interrupted exception.
/// </summary>
/// <param name="action">The action.</param>
private void HandleNonFatalOrInterruptedException(Action action)
{
try
{
action();
}
catch
{
//TODO: Hmmm?
}
}
}
}