Repository navigation
Add migration SAS renewal and drain handling #1444
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| // ---------------------------------------------------------------------------------- | ||
| // Copyright Microsoft Corporation | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // ---------------------------------------------------------------------------------- | ||
|
|
||
| namespace DurableTask.AzureStorage | ||
| { | ||
| using System; | ||
|
|
||
| /// <summary> | ||
| /// Thrown when a client request is rejected because the Azure Storage backend is unavailable, for example while a | ||
| /// live migration is ending. | ||
| /// </summary> | ||
| public class OrchestrationServiceUnavailableException : Exception | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="OrchestrationServiceUnavailableException"/> class. | ||
| /// </summary> | ||
| /// <param name="message">The message that describes the error.</param> | ||
| public OrchestrationServiceUnavailableException(string message) | ||
| : base(message) | ||
| { | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,318 @@ | ||
| // ---------------------------------------------------------------------------------- | ||
| // Copyright Microsoft Corporation | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // ---------------------------------------------------------------------------------- | ||
| #nullable enable | ||
| namespace DurableTask.AzureStorage.Storage | ||
| { | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Azure; | ||
| using Azure.Core; | ||
| using Azure.Data.Tables; | ||
| using Azure.Data.Tables.Sas; | ||
| using Azure.Storage.Blobs; | ||
| using Azure.Storage.Sas; | ||
| using Azure.Storage.Queues; | ||
| using BlobKey = Azure.Storage.Blobs.Models.UserDelegationKey; | ||
| using QueueKey = Azure.Storage.Queues.Models.UserDelegationKey; | ||
|
|
||
| sealed class MigrationSasManager | ||
| { | ||
| internal static readonly TimeSpan SasLifetime = TimeSpan.FromSeconds(15); | ||
| static readonly TimeSpan RenewBefore = TimeSpan.FromSeconds(11); | ||
| readonly AzureStorageMigration migration; | ||
| readonly Guid migrationId; | ||
| readonly AzureStorageOrchestrationServiceSettings settings; | ||
| readonly BlobServiceClient blobService; | ||
| readonly QueueServiceClient queueService; | ||
| readonly TableServiceClient tableService; | ||
| readonly IStorageServiceClientProvider<TableServiceClient, TableClientOptions> tableProvider; | ||
| readonly Action onMigrationEnding; | ||
| readonly object sync = new(); | ||
| readonly SemaphoreSlim refreshLock = new(1, 1); | ||
| readonly Dictionary<string, AzureSasCredential> credentials = new(); | ||
| BlobKey? blobKey; | ||
| QueueKey? queueKey; | ||
| TableUserDelegationKey? tableKey; | ||
| DateTimeOffset expiresAt; | ||
| DateTimeOffset sampledServerTime; | ||
| long sampledAt; | ||
| volatile bool isMigrationEnding; | ||
| CancellationTokenSource? renewalSource; | ||
| Task renewalTask = Task.CompletedTask; | ||
|
|
||
| public MigrationSasManager( | ||
| AzureStorageOrchestrationServiceSettings settings, | ||
| Guid migrationId, | ||
| BlobServiceClient blobService, | ||
| QueueServiceClient queueService, | ||
| TableServiceClient tableService, | ||
| IStorageServiceClientProvider<TableServiceClient, TableClientOptions> tableProvider, | ||
| Action onMigrationEnding) | ||
| { | ||
| this.settings = settings; | ||
| this.migrationId = migrationId; | ||
| this.blobService = blobService; | ||
| this.queueService = queueService; | ||
| this.tableService = tableService; | ||
| this.tableProvider = tableProvider; | ||
| this.onMigrationEnding = onMigrationEnding; | ||
| this.migration = new AzureStorageMigration(tableService, settings.TaskHubName); | ||
| } | ||
|
|
||
| public bool IsMigrationEnding => this.isMigrationEnding; | ||
|
|
||
| public async Task RefreshAsync(CancellationToken cancellationToken) | ||
| { | ||
| // Serialize explicit refreshes and the renewal loop | ||
| await this.refreshLock.WaitAsync(cancellationToken).ConfigureAwait(false); | ||
| try | ||
| { | ||
| if (this.isMigrationEnding) | ||
| { | ||
| return; | ||
| } | ||
| await this.RefreshCoreAsync(cancellationToken).ConfigureAwait(false); | ||
| } | ||
| finally | ||
| { | ||
| this.refreshLock.Release(); | ||
| } | ||
| } | ||
|
|
||
| public Task StartRefreshLoop() | ||
| { | ||
| lock (this.sync) | ||
| { | ||
| if (!this.isMigrationEnding && (this.renewalSource == null || this.renewalTask.IsCompleted)) | ||
| { | ||
| this.renewalSource?.Dispose(); | ||
| var source = new CancellationTokenSource(); | ||
| this.renewalSource = source; | ||
| this.renewalTask = Task.Run(() => this.RunAsync(source.Token)); | ||
| } | ||
| return this.renewalTask; | ||
| } | ||
| } | ||
|
|
||
| public Task StopRefreshing() | ||
| { | ||
| lock (this.sync) | ||
| { | ||
| this.renewalSource?.Cancel(); | ||
| return this.renewalTask; | ||
| } | ||
| } | ||
|
|
||
| async Task RunAsync(CancellationToken cancellationToken) | ||
| { | ||
| while (!cancellationToken.IsCancellationRequested && !this.isMigrationEnding) | ||
| { | ||
| try | ||
| { | ||
| // Per-host jitter avoids synchronizing readers | ||
| await Task.Delay(TimeSpan.FromMilliseconds(1000 + (uint)this.settings.WorkerId.GetHashCode() % 1000), cancellationToken).ConfigureAwait(false); | ||
| await this.RefreshAsync(cancellationToken).ConfigureAwait(false); | ||
| } | ||
| catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) | ||
| { | ||
| break; | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| // Keep the last credentials, never extend them locally or use unrestricted authentication. | ||
| this.settings.Logger.GeneralWarning(this.queueService.AccountName, this.settings.TaskHubName, | ||
| $"Migration SAS renewal failed ({e.GetType().Name}). Source access will stop when its published credentials expire."); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| async Task RefreshCoreAsync(CancellationToken cancellationToken) | ||
| { | ||
| while (true) | ||
| { | ||
| cancellationToken.ThrowIfCancellationRequested(); | ||
|
|
||
| // Include the read time when checking whether the credentials have expired. | ||
| long sampledAt = Stopwatch.GetTimestamp(); | ||
|
|
||
| // If the gate is missing, migration has been misconfigured | ||
| AzureStorageMigration.Gate? gate = await this.migration.ReadAsync(cancellationToken, this.migrationId).ConfigureAwait(false) | ||
| ?? throw new InvalidOperationException("Migration control row is missing. Source access cannot be granted."); | ||
|
|
||
| // If the gate indicates migration is ending, exit | ||
| if (gate.IsMigrationEnding) | ||
| { | ||
| this.SetMigrationEnding(); | ||
| return; | ||
| } | ||
|
|
||
| if (await this.RefreshKeysAsync(gate.ServerTime, cancellationToken).ConfigureAwait(false)) | ||
| { | ||
| // Key acquisition may be slow, so re-read the gate | ||
| continue; | ||
| } | ||
|
|
||
| // If the gate is about to expire, extend it | ||
| if (gate.AccessExpiresAt <= gate.ServerTime + RenewBefore) | ||
| { | ||
| gate.AccessExpiresAt = gate.ServerTime + SasLifetime; | ||
| // Another host must have updated the gate in the meantime. Re-read and try again. | ||
| if (!await this.migration.TryUpdateAsync(gate, cancellationToken).ConfigureAwait(false)) | ||
| { | ||
| continue; | ||
| } | ||
| } | ||
|
|
||
| // Generate new SAS tokens with the new expiry time | ||
| lock (this.sync) | ||
| { | ||
| this.expiresAt = gate.AccessExpiresAt; | ||
| this.sampledServerTime = gate.ServerTime; | ||
| this.sampledAt = sampledAt; | ||
| foreach (KeyValuePair<string, AzureSasCredential> entry in this.credentials) | ||
| { | ||
| entry.Value.Update(this.Sign(entry.Key[0], entry.Key.Substring(2))); | ||
| } | ||
| } | ||
|
|
||
| // Some slowdown in the renewal path consumed the whole expiry interval. Re-read before returning | ||
| if (this.EstimatedServerTime >= gate.AccessExpiresAt) | ||
| { | ||
| continue; | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this have a minimum valid window? If we return right before it expires, what happens?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think it's necessary, this is meant to handle a pretty niche edge case anyway (like something in this loop taking longer than 15 seconds is quite unlikely imo), and in later PRs we have logic to retry a failed request if a SAS token does happen to be expired. But I don't feel strongly about this and can add it |
||
| return; | ||
| } | ||
| } | ||
|
|
||
| async Task<bool> RefreshKeysAsync(DateTimeOffset serverTime, CancellationToken cancellationToken) | ||
| { | ||
| // Reuse delegation keys to sign new SAS tokens; normally only the control row needs refreshing. | ||
| bool refreshed = false; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need a more granular boolean for what was refreshed?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think so - the point of this is to indicate that any of the keys were refreshed, it doesn't matter which |
||
|
|
||
| // Tables has no CanGenerateAccountSasUri property, so check the provider's identity credential. | ||
| TokenCredential? credential = (this.tableProvider as IStorageTokenCredentialProvider)?.TokenCredential; | ||
| if (credential != null && (this.tableKey == null || this.tableKey.ExpiresOn <= serverTime.AddMinutes(5))) | ||
| { | ||
| TableUserDelegationKey key = await TableUserDelegationKey.GetAsync(this.tableService.Uri, credential, | ||
| this.tableProvider.CreateOptions(), serverTime, cancellationToken).ConfigureAwait(false); | ||
| lock (this.sync) | ||
| { | ||
| this.tableKey = key; | ||
| } | ||
| refreshed = true; | ||
| } | ||
|
|
||
| // Blob and Queue clients with an account key do not need delegation keys. | ||
| if (!this.blobService.CanGenerateAccountSasUri && (this.blobKey == null || this.blobKey.SignedExpiresOn <= serverTime.AddMinutes(5))) | ||
| { | ||
| BlobKey key = await this.blobService.GetUserDelegationKeyAsync( | ||
| serverTime.AddMinutes(-15), serverTime.AddHours(24), cancellationToken).ConfigureAwait(false); | ||
| lock (this.sync) | ||
| { | ||
| this.blobKey = key; | ||
| } | ||
| refreshed = true; | ||
| } | ||
| if (!this.queueService.CanGenerateAccountSasUri && (this.queueKey == null || this.queueKey.SignedExpiresOn <= serverTime.AddMinutes(5))) | ||
| { | ||
| QueueKey key = await this.queueService.GetUserDelegationKeyAsync( | ||
| serverTime.AddMinutes(-15), serverTime.AddHours(24), cancellationToken).ConfigureAwait(false); | ||
| lock (this.sync) | ||
| { | ||
| this.queueKey = key; | ||
| } | ||
| refreshed = true; | ||
| } | ||
| return refreshed; | ||
| } | ||
|
|
||
| DateTimeOffset EstimatedServerTime => this.sampledServerTime + TimeSpan.FromSeconds((Stopwatch.GetTimestamp() - this.sampledAt) / (double)Stopwatch.Frequency); | ||
|
|
||
| public void EnsureAccess() | ||
| { | ||
| lock (this.sync) | ||
| { | ||
| if (this.isMigrationEnding) | ||
|
sophiatev marked this conversation as resolved.
Resolved
|
||
| { | ||
| throw new OrchestrationServiceUnavailableException("Migration is ending; source storage access is disabled."); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public AzureSasCredential GetCredential(char service, string resource) | ||
| { | ||
| lock (this.sync) | ||
| { | ||
| this.EnsureAccess(); | ||
| string key = service + ":" + resource; | ||
| if (!this.credentials.TryGetValue(key, out AzureSasCredential? credential)) | ||
| { | ||
| credential = new AzureSasCredential(this.Sign(service, resource)); | ||
| this.credentials.Add(key, credential); | ||
| } | ||
| return credential; | ||
| } | ||
| } | ||
|
|
||
| string Sign(char service, string resource) | ||
| { | ||
| // CanGenerateAccountSasUri means the client has an account key and can sign directly. | ||
| switch (service) | ||
| { | ||
| case 'b': | ||
| var blob = new BlobSasBuilder { BlobContainerName = resource, Resource = "c", ExpiresOn = this.expiresAt }; | ||
| blob.SetPermissions(BlobContainerSasPermissions.Read | BlobContainerSasPermissions.Write | BlobContainerSasPermissions.Create | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does migration need things like create/delete? Is this just for migration related metadata? Otherwise I would assume migration just needs read/list so it can move the data.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So to clarify these SAS tokens are used in the normal Azure Storage flow to write customer data when migration is on, they actually aren't writing any migration metadata. We switch from keys/Entra/whatever the customer was using before to authenticate to their Azure Storage account to these short-lived tokens. The point of this is that we can bound our drain time when migration is ending. We set migration to ending, and the Azure Storage hosts recognize this and don't renew their SAS tokens any longer. But this means we also know that once we set migration to ending the longest an existing token can still be valid is only 15 seconds. |
||
| | BlobContainerSasPermissions.Delete | BlobContainerSasPermissions.List); | ||
| if (this.blobService.CanGenerateAccountSasUri) | ||
| { | ||
| return this.blobService.GetBlobContainerClient(resource).GenerateSasUri(blob).Query.TrimStart('?'); | ||
| } | ||
| blob.Protocol = SasProtocol.Https; | ||
| blob.StartsOn = this.blobKey!.SignedStartsOn; | ||
| return blob.ToSasQueryParameters(this.blobKey, this.blobService.AccountName).ToString(); | ||
| case 'q': | ||
| var queue = new QueueSasBuilder { QueueName = resource, ExpiresOn = this.expiresAt }; | ||
| queue.SetPermissions(QueueSasPermissions.Read | QueueSasPermissions.Add | QueueSasPermissions.Update | QueueSasPermissions.Process); | ||
| if (this.queueService.CanGenerateAccountSasUri) | ||
| { | ||
| return this.queueService.GetQueueClient(resource).GenerateSasUri(queue).Query.TrimStart('?'); | ||
| } | ||
| queue.Protocol = SasProtocol.Https; | ||
| queue.StartsOn = this.queueKey!.SignedStartsOn; | ||
| return queue.ToSasQueryParameters(this.queueKey, this.queueService.AccountName).ToString(); | ||
| case 't': | ||
| // Tables has no capability flag; identity clients use the key loaded during refresh. | ||
| if (this.tableKey != null) | ||
| { | ||
| return this.tableKey.Sign(this.tableService.AccountName, resource, this.expiresAt); | ||
| } | ||
|
|
||
| // Account-key connections also use bounded SAS. Queries and batches require container scope; | ||
| // individual entity operations require object scope. Resource deletion is blocked by our wrappers. | ||
| return this.tableService.GenerateSasUri(TableAccountSasPermissions.Read | TableAccountSasPermissions.Add | ||
| | TableAccountSasPermissions.Update | TableAccountSasPermissions.Delete, | ||
| TableAccountSasResourceTypes.Container | TableAccountSasResourceTypes.Object, this.expiresAt).Query.TrimStart('?'); | ||
| default: throw new ArgumentOutOfRangeException(nameof(service)); | ||
| } | ||
| } | ||
|
|
||
| void SetMigrationEnding() | ||
| { | ||
| this.isMigrationEnding = true; | ||
| this.onMigrationEnding(); | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.