Skip to content

Commit 322c494

Browse files
authored
Block lease release in Shard PostStop (#7383)
1 parent 902daf5 commit 322c494

File tree

1 file changed

+42
-24
lines changed
  • src/contrib/cluster/Akka.Cluster.Sharding

1 file changed

+42
-24
lines changed

src/contrib/cluster/Akka.Cluster.Sharding/Shard.cs

+42-24
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.Collections.Generic;
1111
using System.Collections.Immutable;
1212
using System.Linq;
13+
using System.Threading.Tasks;
1314
using Akka.Actor;
1415
using Akka.Annotations;
1516
using Akka.Cluster.Sharding.Internal;
@@ -1051,17 +1052,25 @@ private void AcquireLeaseIfNeeded()
10511052

10521053
private void ReleaseLeaseIfNeeded()
10531054
{
1054-
if (_lease != null)
1055+
if (_lease is null)
1056+
return;
1057+
1058+
try
1059+
{
1060+
ReleaseLease().GetAwaiter().GetResult();
1061+
}
1062+
catch
1063+
{
1064+
// no-op, we're shutting down anyway.
1065+
}
1066+
1067+
return;
1068+
1069+
async Task ReleaseLease()
10551070
{
1056-
_lease.Release().ContinueWith(r =>
1071+
try
10571072
{
1058-
if (r.IsFaulted || r.IsCanceled)
1059-
{
1060-
Log.Error(r.Exception,
1061-
"{0}: Failed to release lease of shardId [{1}]. Shard may not be able to run on another node until lease timeout occurs.",
1062-
_typeName, _shardId);
1063-
}
1064-
else if (r.Result)
1073+
if (await _lease.Release())
10651074
{
10661075
Log.Info("{0}: Lease of shardId [{1}] released.", _typeName, _shardId);
10671076
}
@@ -1071,7 +1080,13 @@ private void ReleaseLeaseIfNeeded()
10711080
"{0}: Failed to release lease of shardId [{1}]. Shard may not be able to run on another node until lease timeout occurs.",
10721081
_typeName, _shardId);
10731082
}
1074-
});
1083+
}
1084+
catch (Exception ex)
1085+
{
1086+
Log.Error(ex,
1087+
"{0}: Failed to release lease of shardId [{1}]. Shard may not be able to run on another node until lease timeout occurs.",
1088+
_typeName, _shardId);
1089+
}
10751090
}
10761091
}
10771092

@@ -1085,18 +1100,12 @@ private bool AwaitingLease(object message)
10851100
{
10861101
switch (message)
10871102
{
1088-
case LeaseAcquireResult lar when lar.Acquired:
1103+
case LeaseAcquireResult { Acquired: true }:
10891104
Log.Debug("{0}: Lease acquired", _typeName);
10901105
TryLoadRememberedEntities();
10911106
return true;
10921107

1093-
case LeaseAcquireResult lar when !lar.Acquired && lar.Reason == null:
1094-
Log.Error("{0}: Failed to get lease for shard id [{1}]. Retry in {2}",
1095-
_typeName, _shardId, _leaseRetryInterval);
1096-
Timers.StartSingleTimer(LeaseRetryTimer, Shard.LeaseRetry.Instance, _leaseRetryInterval);
1097-
return true;
1098-
1099-
case LeaseAcquireResult lar when !lar.Acquired && lar.Reason != null:
1108+
case LeaseAcquireResult { Acquired: false } lar:
11001109
Log.Error(lar.Reason, "{0}: Failed to get lease for shard id [{1}]. Retry in {2}",
11011110
_typeName, _shardId, _leaseRetryInterval);
11021111
Timers.StartSingleTimer(LeaseRetryTimer, Shard.LeaseRetry.Instance, _leaseRetryInterval);
@@ -1126,12 +1135,21 @@ private void TryGetLease(Lease lease)
11261135
Log.Info("{0}: Acquiring lease {1}", _typeName, lease.Settings);
11271136

11281137
var self = Self;
1129-
lease.Acquire(reason => { self.Tell(new LeaseLost(reason)); }).ContinueWith(r =>
1138+
Acquire().PipeTo(self);
1139+
return;
1140+
1141+
async Task<LeaseAcquireResult> Acquire()
11301142
{
1131-
if (r.IsFaulted || r.IsCanceled)
1132-
return new LeaseAcquireResult(false, r.Exception);
1133-
return new LeaseAcquireResult(r.Result, null);
1134-
}).PipeTo(Self);
1143+
try
1144+
{
1145+
var result = await lease.Acquire(reason => { self.Tell(new LeaseLost(reason)); });
1146+
return new LeaseAcquireResult(result, null);
1147+
}
1148+
catch (Exception ex)
1149+
{
1150+
return new LeaseAcquireResult(false, ex);
1151+
}
1152+
}
11351153
}
11361154

11371155
// ===== remember entities initialization =====
@@ -1610,7 +1628,7 @@ private void HandOff(IActorRef replyTo)
16101628
"HandOffStopper"));
16111629

16121630
//During hand off we only care about watching for termination of the hand off stopper
1613-
Context.Become((object message) =>
1631+
Context.Become(message =>
16141632
{
16151633
switch (message)
16161634
{

0 commit comments

Comments
 (0)