in src/DurableTask.AzureStorage/Partitioning/LeaseCollectionBalancer.cs [675:744]
async Task RemoveLeaseAsync(T lease, bool hasOwnership)
{
CloseReason reason = hasOwnership ? CloseReason.Shutdown : CloseReason.LeaseLost;
if (lease != null && this.currentlyOwnedShards != null && this.currentlyOwnedShards.TryRemove(lease.PartitionId, out lease))
{
this.settings.Logger.PartitionRemoved(
this.accountName,
this.taskHub,
this.workerName,
lease.PartitionId,
lease.Token);
try
{
if (hasOwnership)
{
this.keepRenewingDuringClose.TryAdd(lease.PartitionId, lease);
}
// Notify the host that we lost shard so shutdown can be triggered on the host
await this.leaseObserverManager.NotifyShardReleasedAsync(lease, reason);
}
catch (Exception ex)
{
// Eat any exceptions during notification of observers
this.settings.Logger.PartitionManagerError(
this.accountName,
this.taskHub,
this.workerName,
lease.PartitionId,
$"Encountered exception while notifying observers of {this.leaseType} lease release: {ex}");
}
finally
{
if (hasOwnership)
{
this.keepRenewingDuringClose.TryRemove(lease.PartitionId, out lease);
}
}
if (hasOwnership)
{
try
{
await this.leaseManager.ReleaseAsync(lease);
this.settings.Logger.LeaseRemoved(
this.accountName,
this.taskHub,
this.workerName,
lease.PartitionId,
lease.Token,
this.leaseType);
}
catch (LeaseLostException)
{
// We have already shutdown the processor so we can ignore any LeaseLost at this point
}
catch (Exception ex)
{
this.settings.Logger.PartitionManagerError(
this.accountName,
this.taskHub,
this.workerName,
lease.PartitionId,
$"Encountered failure while releasing owned {this.leaseType}: {ex}");
}
}
}
}