Beispiel #1
0
        async Task <bool> ChangeLeaseAsync(string currentLeaseId)
        {
            this.settings.Logger.PartitionManagerInfo(
                this.storageAccountName,
                this.taskHub,
                this.workerName,
                this.appLeaseContainerName,
                $"Attempting to change lease from current owner {currentLeaseId} to {this.appLeaseId}.");

            bool leaseAcquired;

            try
            {
                this.settings.Logger.LeaseAcquisitionStarted(
                    this.storageAccountName,
                    this.taskHub,
                    this.workerName,
                    this.appLeaseContainerName,
                    LeaseType);

                await this.appLeaseContainer.ChangeLeaseAsync(this.appLeaseId, currentLeaseId);

                var appLeaseInfo = new AppLeaseInfo()
                {
                    OwnerId = this.appLeaseId,
                };

                await this.UpdateAppLeaseInfoBlob(appLeaseInfo);

                leaseAcquired = true;

                this.settings.Logger.LeaseAcquisitionSucceeded(
                    this.storageAccountName,
                    this.taskHub,
                    this.workerName,
                    this.appLeaseContainerName,
                    LeaseType);

                // When changing the lease over to another app, the paritions will still be listened to on the first app until the AppLeaseManager
                // renew task fails to renew the lease. To avoid potential split brain we must delay before the new lease holder can start
                // listening to the partitions.
                if (this.settings.UseLegacyPartitionManagement == true)
                {
                    await Task.Delay(this.settings.AppLeaseOptions.RenewInterval);
                }
            }
            catch (DurableTaskStorageException e)
            {
                leaseAcquired = false;

                this.settings.Logger.PartitionManagerWarning(
                    this.storageAccountName,
                    this.taskHub,
                    this.workerName,
                    this.appLeaseContainerName,
                    $"Failed to change app lease from currentLeaseId {currentLeaseId} to {this.appLeaseId}. Exception: {e.Message}");
            }

            return(leaseAcquired);
        }
Beispiel #2
0
        async Task <bool> TryAquireAppLeaseAsync()
        {
            AppLeaseInfo appLeaseInfo = await this.GetAppLeaseInfoAsync();

            bool leaseAcquired;

            if (appLeaseInfo.DesiredSwapId == this.appLeaseId)
            {
                leaseAcquired = await this.ChangeLeaseAsync(appLeaseInfo.OwnerId);
            }
            else
            {
                leaseAcquired = await this.TryAquireLeaseAsync();
            }

            this.isLeaseOwner = leaseAcquired;

            return(leaseAcquired);
        }