in src/DurableTask.AzureStorage/Partitioning/TablePartitionManager.cs [520:567]
void CheckOtherWorkersLeases(
TablePartitionLease partition,
Dictionary<string, List<TablePartitionLease>> partitionDistribution,
ReadTableReponse response,
ref int ownershipLeaseCount,
ref string previousOwner,
ref bool stoleLease)
{
bool isOtherWorkersLease = partition.CurrentOwner != this.workerName && partition.NextOwner == null && partition.IsDraining == false;
bool isOtherWorkerStealingLease = partition.CurrentOwner != this.workerName && partition.NextOwner != null;
bool isOwnerShuttingDown = partition.CurrentOwner != this.workerName && partition.NextOwner == null && partition.IsDraining == true;
string owner;
// If the lease is other worker's current lease, add partition to the dictionary with CurrentOwner as key.
if (isOtherWorkersLease)
{
owner = partition.CurrentOwner!;
AddToDictionary(partition, partitionDistribution, owner);
}
// If other worker's lease is stolen, assume the lease tranfer will finish successfully and add partition to the dictionary with NextOwner as key.
if (isOtherWorkerStealingLease)
{
owner = partition.NextOwner!;
// If the lease was stolen by _this_ worker, we increase its currently owned lease count.
if (owner == this.workerName)
{
ownershipLeaseCount++;
response.IsWaitingForPartitionRelease = true;
}
// If the lease is stolen by another worker, keep track of it for rebalancing purposes.
else
{
AddToDictionary(partition, partitionDistribution, owner);
}
}
// If the lease belongs to a worker that is shutting down, and it has not been stolen yet, steal it.
if (isOwnerShuttingDown)
{
previousOwner = partition.CurrentOwner!;
this.StealLease(partition);
stoleLease = true;
response.IsWaitingForPartitionRelease = true;
}
}