diff --git a/Directory.Packages.props b/Directory.Packages.props index 2a74310f..2cac03e7 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -13,7 +13,7 @@ - + diff --git a/src/DurableTask.AzureStorage/OrchestrationServiceUnavailableException.cs b/src/DurableTask.AzureStorage/OrchestrationServiceUnavailableException.cs new file mode 100644 index 00000000..d08d834a --- /dev/null +++ b/src/DurableTask.AzureStorage/OrchestrationServiceUnavailableException.cs @@ -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; + + /// + /// Thrown when a client request is rejected because the Azure Storage backend is unavailable, for example while a + /// live migration is ending. + /// + public class OrchestrationServiceUnavailableException : Exception + { + /// + /// Initializes a new instance of the class. + /// + /// The message that describes the error. + public OrchestrationServiceUnavailableException(string message) + : base(message) + { + } + } +} diff --git a/src/DurableTask.AzureStorage/Storage/MigrationSasManager.cs b/src/DurableTask.AzureStorage/Storage/MigrationSasManager.cs new file mode 100644 index 00000000..379c32a9 --- /dev/null +++ b/src/DurableTask.AzureStorage/Storage/MigrationSasManager.cs @@ -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 tableProvider; + readonly Action onMigrationEnding; + readonly object sync = new(); + readonly SemaphoreSlim refreshLock = new(1, 1); + readonly Dictionary 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 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 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; + } + return; + } + } + + async Task RefreshKeysAsync(DateTimeOffset serverTime, CancellationToken cancellationToken) + { + // Reuse delegation keys to sign new SAS tokens; normally only the control row needs refreshing. + bool refreshed = false; + + // 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) + { + 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 + | 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(); + } + } +} diff --git a/test/DurableTask.AzureStorage.Tests/MigrationSasManagerTests.cs b/test/DurableTask.AzureStorage.Tests/MigrationSasManagerTests.cs new file mode 100644 index 00000000..f18bf614 --- /dev/null +++ b/test/DurableTask.AzureStorage.Tests/MigrationSasManagerTests.cs @@ -0,0 +1,590 @@ +// ---------------------------------------------------------------------------------- +// 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.Tests +{ + using System; + using System.Collections.Generic; + using System.Globalization; + using System.IO; + using System.Linq; + using System.Net; + using System.Net.Http; + using System.Text; + using System.Threading; + using System.Threading.Tasks; + using System.Xml.Linq; + using Azure; + using Azure.Core; + using Azure.Core.Pipeline; + using Azure.Data.Tables; + using Azure.Storage.Blobs; + using Azure.Storage.Queues; + using DurableTask.AzureStorage.Storage; + using Microsoft.VisualStudio.TestTools.UnitTesting; + using Newtonsoft.Json.Linq; + + [TestClass] + public class MigrationSasManagerTests + { + [DataTestMethod] + [DataRow(false)] + [DataRow(true)] + public async Task Renewal_UpdatesCachedCredentialsUsingStorageTime(bool sharedKey) + { + // 1. Issue credentials from the prepared control row. + using StorageResponses storage = new(sharedKey); + MigrationSasManager manager = storage.CreateManager(); + await manager.RefreshAsync(default); + (char Service, string Resource)[] resources = { ('b', "payloads"), ('q', "testhub-control-00"), ('t', "TestHubHistory") }; + AzureSasCredential[] credentials = resources.Select(resource => manager.GetCredential(resource.Service, resource.Resource)).ToArray(); + string[] original = credentials.Select(credential => credential.Signature).ToArray(); + Assert.AreEqual(storage.ServerTime.AddSeconds(15), storage.Expiry); + Assert.AreEqual(sharedKey ? 0 : 3, storage.KeyRequests); + Assert.AreNotSame(credentials[1], manager.GetCredential('q', "testhub-control-01")); + + // 2. Renew without replacing cached credential objects or fetching new keys. + storage.ServerTime = storage.ServerTime.AddSeconds(5); + await manager.RefreshAsync(default); + for (int index = 0; index < resources.Length; index++) + { + (char service, string resource) = resources[index]; + Assert.AreSame(credentials[index], manager.GetCredential(service, resource)); + Assert.AreNotEqual(original[index], credentials[index].Signature); + Dictionary query = Query(credentials[index].Signature); + Assert.AreEqual(storage.ServerTime.AddSeconds(15), DateTimeOffset.Parse(query["se"], CultureInfo.InvariantCulture)); + Assert.IsFalse(string.IsNullOrEmpty(query["sig"])); + if (!sharedKey) + { + Assert.AreEqual("https", query["spr"]); + Assert.AreEqual(service.ToString(), query["sks"]); + Assert.AreEqual(query["skt"], query["st"]); + } + } + Assert.AreEqual(sharedKey ? 0 : 3, storage.KeyRequests); + Dictionary tableQuery = Query(credentials[2].Signature); + if (sharedKey) + { + Assert.AreEqual("t", tableQuery["ss"]); + Assert.AreEqual("co", tableQuery["srt"]); + } + else + { + Assert.AreEqual("TestHubHistory", tableQuery["tn"]); + } + } + + [DataTestMethod] + [DataRow(3, false)] + [DataRow(4, true)] + [DataRow(9, true)] + public async Task Renewal_UsesTheElevenSecondMargin(int elapsedSeconds, bool renew) + { + // 1. Publish the first deadline, then advance the Storage clock. + using StorageResponses storage = new(); + MigrationSasManager manager = storage.CreateManager(); + await manager.RefreshAsync(default); + DateTimeOffset expiry = storage.Expiry; + int writes = storage.GateWrites; + storage.ServerTime = storage.ServerTime.AddSeconds(elapsedSeconds); + + // 2. Renew only when eleven seconds or less remain. + await manager.RefreshAsync(default); + Assert.AreEqual(writes + (renew ? 1 : 0), storage.GateWrites); + Assert.AreEqual(renew ? storage.ServerTime.AddSeconds(15) : expiry, storage.Expiry); + } + + [DataTestMethod] + [DataRow(true)] + [DataRow(false)] + public async Task Renewal_RechecksTheGateAfterLosingAnEtagRace(bool ending) + { + // 1. Another host changes the row after renewal reads it. + using StorageResponses storage = new(); + MigrationSasManager manager = storage.CreateManager(); + await manager.RefreshAsync(default); + DateTimeOffset originalExpiry = storage.Expiry; + int writes = storage.GateWrites; + storage.ServerTime = storage.ServerTime.AddSeconds(5); + storage.BeforeWrite = async () => + { + AzureStorageMigration.Gate gate = (await storage.Control.ReadAsync(default, storage.MigrationId))!; + gate.IsMigrationEnding = ending; + if (!ending) + { + gate.AccessExpiresAt = storage.ServerTime.AddSeconds(30); + } + Assert.IsTrue(await storage.Control.TryUpdateAsync(gate, default)); + }; + + // 2. The stale write loses; the latest row determines access and expiry. + await manager.RefreshAsync(default); + Assert.AreEqual(1, storage.Conflicts); + Assert.AreEqual(writes + 1, storage.GateWrites); + Assert.AreEqual(ending ? originalExpiry : storage.ServerTime.AddSeconds(30), storage.Expiry); + Assert.AreEqual(ending, manager.IsMigrationEnding); + if (ending) + { + Assert.ThrowsException(() => manager.EnsureAccess()); + Assert.ThrowsException(() => manager.GetCredential('q', "queue")); + } + else + { + Assert.AreEqual(storage.Expiry, DateTimeOffset.Parse(Query(manager.GetCredential('q', "queue").Signature)["se"], CultureInfo.InvariantCulture)); + } + } + + [DataTestMethod] + [DataRow(true)] + [DataRow(false)] + public async Task Refresh_ObservesDrainBeforeStartupOrDuringKeyAcquisition(bool beforeStartup) + { + // 1. Close the row before startup or while a key request is in flight. + using StorageResponses storage = new(); + MigrationSasManager manager = storage.CreateManager(); + if (beforeStartup) + { + await storage.EndAsync(); + } + else + { + storage.BeforeKey = _ => storage.EndAsync(); + } + + // 2. No deadline is published, and ending is reported only once. + await manager.RefreshAsync(default); + await manager.RefreshAsync(default); + Assert.IsTrue(manager.IsMigrationEnding); + Assert.AreEqual(1, storage.EndingCalls); + Assert.AreEqual(1, storage.GateWrites); + Assert.AreEqual(DateTimeOffset.FromUnixTimeSeconds(0), storage.Expiry); + Assert.AreEqual(beforeStartup ? 0 : 3, storage.KeyRequests); + Assert.ThrowsException(() => manager.EnsureAccess()); + } + + [DataTestMethod] + [DataRow(false)] + [DataRow(true)] + public async Task Refresh_MissingGateDoesNotCreateOrRenewIt(bool initialized) + { + // 1. Remove the row before startup or after credentials have been issued. + using StorageResponses storage = new(); + MigrationSasManager manager = storage.CreateManager(); + if (initialized) + { + await manager.RefreshAsync(default); + } + int writes = storage.GateWrites; + storage.Row = null; + + // 2. Renewal fails without recreating the deleted row. + await Assert.ThrowsExceptionAsync(() => manager.RefreshAsync(default)); + Assert.IsNull(storage.Row); + Assert.AreEqual(writes, storage.GateWrites); + Assert.AreEqual(initialized ? 3 : 0, storage.KeyRequests); + } + + [DataTestMethod] + [DataRow(false)] + [DataRow(true)] + public async Task Refresh_FailureDoesNotExtendCredentialsAndCanBeRetried(bool failKey) + { + // 1. Fail a renewal after the original credentials have been issued. + using StorageResponses storage = new(); + MigrationSasManager manager = storage.CreateManager(); + await manager.RefreshAsync(default); + AzureSasCredential credential = manager.GetCredential('q', "queue"); + string signature = credential.Signature; + DateTimeOffset expiry = storage.Expiry; + storage.ServerTime = failKey ? storage.ServerTime.AddHours(24) : storage.ServerTime.AddSeconds(5); + storage.FailKeys = failKey; + storage.FailReads = !failKey; + await Assert.ThrowsExceptionAsync(() => manager.RefreshAsync(default)); + Assert.AreEqual(expiry, storage.Expiry); + Assert.AreEqual(signature, credential.Signature); + + // 2. Retry with healthy storage and update the same credential. + storage.FailReads = storage.FailKeys = false; + await manager.RefreshAsync(default); + Assert.AreSame(credential, manager.GetCredential('q', "queue")); + Assert.AreEqual(storage.ServerTime.AddSeconds(15), storage.Expiry); + Assert.AreNotEqual(signature, credential.Signature); + } + + [TestMethod] + public async Task Refresh_RereadsStorageTimeAfterSlowKeyAcquisition() + { + // 1. Advance Storage's clock while fetching a delegation key. + using StorageResponses storage = new(); + MigrationSasManager manager = storage.CreateManager(); + storage.BeforeKey = _ => + { + storage.ServerTime = storage.ServerTime.AddMinutes(1); + return Task.CompletedTask; + }; + + // 2. Use a fresh gate read instead of publishing an already expired deadline. + await manager.RefreshAsync(default); + Assert.AreEqual(storage.ServerTime.AddSeconds(15), storage.Expiry); + Assert.AreEqual(1, storage.GateWrites); + Assert.AreEqual(3, storage.KeyRequests); + } + + [DataTestMethod] + [DataRow(false)] + [DataRow(true)] + [Timeout(60000)] + public async Task Refresh_RereadsStorageTimeAfterDelayedGateOperation(bool delayWrite) + { + // 1. Delay a gate read or write beyond the SAS lifetime. + using StorageResponses storage = new(sharedKey: true); + MigrationSasManager manager = storage.CreateManager(); + TimeSpan delay = MigrationSasManager.SasLifetime + TimeSpan.FromSeconds(1); + async Task DelayResponseAsync(CancellationToken token) + { + await Task.Delay(delay, token); + storage.ServerTime += delay; + } + if (delayWrite) + { + storage.BeforeWrite = () => DelayResponseAsync(default); + } + else + { + storage.AfterRead = DelayResponseAsync; + } + + // 2. Retry before returning credentials with an expired deadline. + await manager.RefreshAsync(default); + Assert.AreEqual(2, storage.GateWrites); + Assert.AreEqual(storage.ServerTime + MigrationSasManager.SasLifetime, storage.Expiry); + AzureSasCredential credential = manager.GetCredential('q', "queue"); + Assert.AreEqual(storage.Expiry, DateTimeOffset.Parse(Query(credential.Signature)["se"], CultureInfo.InvariantCulture)); + } + + [TestMethod] + [Timeout(10000)] + public async Task Refresh_CanceledWaiterDoesNotCancelTheCurrentRefresh() + { + // 1. Hold the first refresh in key acquisition. + using StorageResponses storage = new(); + MigrationSasManager manager = storage.CreateManager(); + TaskCompletionSource started = new(TaskCreationOptions.RunContinuationsAsynchronously); + TaskCompletionSource release = new(TaskCreationOptions.RunContinuationsAsynchronously); + storage.BeforeKey = async _ => { started.SetResult(true); await release.Task; }; + Task first = manager.RefreshAsync(default); + await started.Task; + try + { + // 2. Cancel a second caller while it waits for the first refresh. + using CancellationTokenSource cancellation = new(); + Task second = manager.RefreshAsync(cancellation.Token); + cancellation.Cancel(); + await AssertCanceledAsync(second); + Assert.IsFalse(first.IsCompleted); + } + finally + { + release.TrySetResult(true); + await first; + } + Assert.AreEqual(1, storage.GateWrites); + Assert.AreEqual(3, storage.KeyRequests); + } + + [TestMethod] + [Timeout(10000)] + public async Task Refresh_CancellationDuringKeyAcquisitionAllowsAnotherRefresh() + { + // 1. Block key acquisition until its caller cancels. + using StorageResponses storage = new(); + MigrationSasManager manager = storage.CreateManager(); + TaskCompletionSource started = new(TaskCreationOptions.RunContinuationsAsynchronously); + storage.BeforeKey = async token => { started.SetResult(true); await Task.Delay(Timeout.Infinite, token); }; + using CancellationTokenSource cancellation = new(); + Task pending = manager.RefreshAsync(cancellation.Token); + await started.Task; + cancellation.Cancel(); + await AssertCanceledAsync(pending); + Assert.AreEqual(0, storage.GateWrites); + + // 2. Cancellation releases the refresh lock. + await manager.RefreshAsync(default); + Assert.AreEqual(storage.ServerTime.AddSeconds(15), storage.Expiry); + } + + [TestMethod] + [Timeout(10000)] + public async Task RefreshLoop_CanStopAndRestartAndStopsWhenMigrationEnds() + { + // 1. Repeated starts share one loop, which can be stopped and restarted. + using StorageResponses storage = new(); + MigrationSasManager manager = storage.CreateManager(); + await manager.RefreshAsync(default); + Task first = manager.StartRefreshLoop(); + try + { + Assert.AreSame(first, manager.StartRefreshLoop()); + await manager.StopRefreshing(); + Task second = manager.StartRefreshLoop(); + Assert.AreNotSame(first, second); + await manager.StopRefreshing(); + + // 2. The running loop notices drain and ends without another start. + await storage.EndAsync(); + await manager.StartRefreshLoop(); + Assert.IsTrue(manager.IsMigrationEnding); + Assert.AreEqual(1, storage.EndingCalls); + Assert.IsTrue(manager.StartRefreshLoop().IsCompleted); + } + finally + { + await manager.StopRefreshing(); + } + } + + static async Task AssertCanceledAsync(Task task) + { + try + { + await task; + Assert.Fail("The caller should observe cancellation."); + } + catch (OperationCanceledException) { } + } + + static Dictionary Query(string signature) => signature.TrimStart('?').Split('&') + .Select(part => part.Split(new[] { '=' }, 2)) + .ToDictionary(parts => parts[0], parts => Uri.UnescapeDataString(parts[1])); + + // Real SDK clients use this local handler; no requests reach Azure. + sealed class StorageResponses : HttpPipelineTransport, IDisposable + { + readonly StorageAccountClientProvider provider; + int revision; + + public StorageResponses(bool sharedKey = false) + { + TestCredential credential = new TestCredential(); + string connection = "DefaultEndpointsProtocol=https;AccountName=account;AccountKey=" + Convert.ToBase64String(new byte[32]) + ";EndpointSuffix=core.windows.net"; + this.provider = sharedKey + ? new StorageAccountClientProvider( + StorageServiceClientProvider.ForBlob(connection, this.Options(new BlobClientOptions())), + StorageServiceClientProvider.ForQueue(connection, this.Options(new QueueClientOptions())), + StorageServiceClientProvider.ForTable(connection, this.Options(new TableClientOptions()))) + : new StorageAccountClientProvider( + StorageServiceClientProvider.ForBlob("account", credential, this.Options(new BlobClientOptions())), + StorageServiceClientProvider.ForQueue("account", credential, this.Options(new QueueClientOptions())), + StorageServiceClientProvider.ForTable("account", credential, this.Options(new TableClientOptions()))); + this.Control = new AzureStorageMigration(this.provider.Table.CreateClient(this.provider.Table.CreateOptions()), "TestHub"); + this.Row = new JObject + { + ["PartitionKey"] = string.Empty, + ["RowKey"] = this.MigrationId.ToString("D"), + ["MigrationId@odata.type"] = "Edm.Guid", + ["MigrationId"] = this.MigrationId, + ["IsDraining"] = false, + ["AccessExpiresAt@odata.type"] = "Edm.DateTime", + ["AccessExpiresAt"] = DateTimeOffset.FromUnixTimeSeconds(0), + ["odata.etag"] = "v0", + }; + } + + public Guid MigrationId { get; } = Guid.NewGuid(); + public DateTimeOffset ServerTime { get; set; } = new DateTimeOffset(2030, 1, 1, 0, 0, 0, TimeSpan.Zero); + public JObject? Row { get; set; } + public DateTimeOffset Expiry => this.Row!["AccessExpiresAt"]!.ToObject(); + public int GateWrites { get; private set; } + public int KeyRequests { get; private set; } + public int Conflicts { get; private set; } + public int EndingCalls { get; private set; } + public bool FailReads { get; set; } + public bool FailKeys { get; set; } + public Func? AfterRead { get; set; } + public Func? BeforeWrite { get; set; } + public Func? BeforeKey { get; set; } + public AzureStorageMigration Control { get; } + + public MigrationSasManager CreateManager() => new MigrationSasManager( + new AzureStorageOrchestrationServiceSettings { TaskHubName = "TestHub", StorageAccountClientProvider = this.provider }, + this.MigrationId, + this.provider.Blob.CreateClient(this.provider.Blob.CreateOptions()), + this.provider.Queue.CreateClient(this.provider.Queue.CreateOptions()), + this.provider.Table.CreateClient(this.provider.Table.CreateOptions()), + this.provider.Table, + () => this.EndingCalls++); + + public async Task EndAsync() + { + AzureStorageMigration.Gate gate = (await this.Control.ReadAsync(default, this.MigrationId))!; + gate.IsMigrationEnding = true; + Assert.IsTrue(await this.Control.TryUpdateAsync(gate, default)); + } + + T Options(T options) where T : ClientOptions + { + options.Transport = this; + options.Retry.MaxRetries = 0; + return options; + } + + async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) + { + Uri uri = request.RequestUri!; + if (uri.Query.Contains("comp=userdelegationkey")) + { + this.KeyRequests++; + Func? hook = this.BeforeKey; + this.BeforeKey = null; + if (hook is not null) await hook(cancellationToken); + if (this.FailKeys) return this.Error(HttpStatusCode.ServiceUnavailable, "ServerBusy"); + string service = uri.Host.Contains(".table.") ? "t" : uri.Host.Contains(".queue.") ? "q" : "b"; + XElement body = new XElement("UserDelegationKey", + new XElement("SignedOid", "11111111-1111-1111-1111-111111111111"), + new XElement("SignedTid", "22222222-2222-2222-2222-222222222222"), + new XElement("SignedStart", this.ServerTime.AddMinutes(-15).ToString("yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture)), + new XElement("SignedExpiry", this.ServerTime.AddHours(24).ToString("yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture)), + new XElement("SignedService", service), + new XElement("SignedVersion", "2025-07-05"), + new XElement("Value", Convert.ToBase64String(new byte[32]))); + return this.Response(HttpStatusCode.OK, body.ToString(), "application/xml"); + } + + Assert.AreEqual($"/TestHubMigrationCtrl(PartitionKey='',RowKey='{this.MigrationId:D}')", Uri.UnescapeDataString(uri.AbsolutePath)); + if (request.Method == HttpMethod.Get) + { + if (this.FailReads) return this.Error(HttpStatusCode.ServiceUnavailable, "ServerBusy"); + HttpResponseMessage response = this.Row is null ? this.Error(HttpStatusCode.NotFound, "ResourceNotFound") + : this.Response(HttpStatusCode.OK, this.Row.ToString()); + Func? afterRead = this.AfterRead; + this.AfterRead = null; + if (afterRead is not null) await afterRead(cancellationToken); + return response; + } + + Assert.AreEqual(HttpMethod.Put, request.Method, "The manager must not create the control row."); + Func? beforeWrite = this.BeforeWrite; + this.BeforeWrite = null; + if (beforeWrite is not null) await beforeWrite(); + if (this.Row is null) return this.Error(HttpStatusCode.NotFound, "ResourceNotFound"); + if (request.Headers.GetValues("If-Match").Single() != this.Row.Value("odata.etag")) + { + this.Conflicts++; + return this.Error(HttpStatusCode.PreconditionFailed, "UpdateConditionNotSatisfied"); + } + this.Row = JObject.Parse(await request.Content!.ReadAsStringAsync()); + Assert.AreEqual(this.MigrationId, this.Row["MigrationId"]!.ToObject()); + this.Row["odata.etag"] = "v" + (++this.revision).ToString(CultureInfo.InvariantCulture); + this.GateWrites++; + return this.Response(HttpStatusCode.NoContent); + } + + HttpResponseMessage Response(HttpStatusCode status, string body = "", string type = "application/json") + { + HttpResponseMessage response = new HttpResponseMessage(status) + { + Content = new StreamContent(new ResponseStream(Encoding.UTF8.GetBytes(body))), + }; + response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(type); + response.Headers.Date = this.ServerTime; + response.Headers.TryAddWithoutValidation("ETag", this.Row?.Value("odata.etag") ?? "v0"); + return response; + } + + HttpResponseMessage Error(HttpStatusCode status, string code) + { + HttpResponseMessage response = this.Response(status, new JObject + { + ["odata.error"] = new JObject { ["code"] = code, ["message"] = new JObject { ["lang"] = "en-US", ["value"] = code } }, + }.ToString()); + response.Headers.Add("x-ms-error-code", code); + return response; + } + + public override Request CreateRequest() => HttpClientTransport.Shared.CreateRequest(); + + public override void Process(HttpMessage message) => this.ProcessAsync(message).AsTask().GetAwaiter().GetResult(); + + public override async ValueTask ProcessAsync(HttpMessage message) + { + message.CancellationToken.ThrowIfCancellationRequested(); + using HttpRequestMessage request = new HttpRequestMessage(new HttpMethod(message.Request.Method.Method), message.Request.Uri.ToUri()); + if (message.Request.Content is not null) + { + using MemoryStream body = new MemoryStream(); + await message.Request.Content.WriteToAsync(body, message.CancellationToken); + request.Content = new ByteArrayContent(body.ToArray()); + } + foreach (HttpHeader header in message.Request.Headers) + { + if (!request.Headers.TryAddWithoutValidation(header.Name, header.Value)) + { + request.Content?.Headers.TryAddWithoutValidation(header.Name, header.Value); + } + } + using HttpResponseMessage response = await this.SendAsync(request, message.CancellationToken); + message.Response = new StorageResponse(response, await response.Content.ReadAsByteArrayAsync()); + } + + public void Dispose() { } + } + + sealed class StorageResponse : Response + { + readonly Dictionary headers; + + public StorageResponse(HttpResponseMessage response, byte[] body) + { + this.Status = (int)response.StatusCode; + this.ReasonPhrase = response.ReasonPhrase ?? string.Empty; + this.ContentStream = new ResponseStream(body); + this.headers = response.Headers.Concat(response.Content.Headers) + .ToDictionary(header => header.Key, header => string.Join(",", header.Value), StringComparer.OrdinalIgnoreCase); + } + + public override int Status { get; } + public override string ReasonPhrase { get; } + public override Stream? ContentStream { get; set; } + public override string ClientRequestId { get; set; } = string.Empty; + public override void Dispose() { } + protected override bool ContainsHeader(string name) => this.headers.ContainsKey(name); + protected override bool TryGetHeader(string name, out string value) => this.headers.TryGetValue(name, out value!); + protected override bool TryGetHeaderValues(string name, out IEnumerable values) + { + bool found = this.headers.TryGetValue(name, out string? value); + values = found ? new[] { value! } : Array.Empty(); + return found; + } + protected override IEnumerable EnumerateHeaders() => this.headers.Select(pair => new HttpHeader(pair.Key, pair.Value)); + } + + // Keep the response body available until the SDK has finished reading it. + sealed class ResponseStream : MemoryStream + { + public ResponseStream(byte[] bytes) : base(bytes) { } + protected override void Dispose(bool disposing) { } + } + + sealed class TestCredential : TokenCredential + { + public override AccessToken GetToken(TokenRequestContext requestContext, CancellationToken cancellationToken) + { + cancellationToken.ThrowIfCancellationRequested(); + return new AccessToken("test-token", DateTimeOffset.UtcNow.AddHours(1)); + } + + public override ValueTask GetTokenAsync(TokenRequestContext requestContext, CancellationToken cancellationToken) => + new ValueTask(this.GetToken(requestContext, cancellationToken)); + } + } +}