10
10
using System . Collections . Generic ;
11
11
using System . Collections . Immutable ;
12
12
using System . Linq ;
13
+ using System . Threading . Tasks ;
13
14
using Akka . Actor ;
14
15
using Akka . Annotations ;
15
16
using Akka . Cluster . Sharding . Internal ;
@@ -1051,17 +1052,25 @@ private void AcquireLeaseIfNeeded()
1051
1052
1052
1053
private void ReleaseLeaseIfNeeded ( )
1053
1054
{
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 ( )
1055
1070
{
1056
- _lease . Release ( ) . ContinueWith ( r =>
1071
+ try
1057
1072
{
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 ( ) )
1065
1074
{
1066
1075
Log . Info ( "{0}: Lease of shardId [{1}] released." , _typeName , _shardId ) ;
1067
1076
}
@@ -1071,7 +1080,13 @@ private void ReleaseLeaseIfNeeded()
1071
1080
"{0}: Failed to release lease of shardId [{1}]. Shard may not be able to run on another node until lease timeout occurs." ,
1072
1081
_typeName , _shardId ) ;
1073
1082
}
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
+ }
1075
1090
}
1076
1091
}
1077
1092
@@ -1085,18 +1100,12 @@ private bool AwaitingLease(object message)
1085
1100
{
1086
1101
switch ( message )
1087
1102
{
1088
- case LeaseAcquireResult lar when lar . Acquired :
1103
+ case LeaseAcquireResult { Acquired : true } :
1089
1104
Log . Debug ( "{0}: Lease acquired" , _typeName ) ;
1090
1105
TryLoadRememberedEntities ( ) ;
1091
1106
return true ;
1092
1107
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 :
1100
1109
Log . Error ( lar . Reason , "{0}: Failed to get lease for shard id [{1}]. Retry in {2}" ,
1101
1110
_typeName , _shardId , _leaseRetryInterval ) ;
1102
1111
Timers . StartSingleTimer ( LeaseRetryTimer , Shard . LeaseRetry . Instance , _leaseRetryInterval ) ;
@@ -1126,12 +1135,21 @@ private void TryGetLease(Lease lease)
1126
1135
Log . Info ( "{0}: Acquiring lease {1}" , _typeName , lease . Settings ) ;
1127
1136
1128
1137
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 ( )
1130
1142
{
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
+ }
1135
1153
}
1136
1154
1137
1155
// ===== remember entities initialization =====
@@ -1610,7 +1628,7 @@ private void HandOff(IActorRef replyTo)
1610
1628
"HandOffStopper" ) ) ;
1611
1629
1612
1630
//During hand off we only care about watching for termination of the hand off stopper
1613
- Context . Become ( ( object message ) =>
1631
+ Context . Become ( message =>
1614
1632
{
1615
1633
switch ( message )
1616
1634
{
0 commit comments