diff --git a/docs/providers/azure-storage.md b/docs/providers/azure-storage.md index a1744472..b9a33819 100644 --- a/docs/providers/azure-storage.md +++ b/docs/providers/azure-storage.md @@ -282,6 +282,16 @@ Checkpoints are written in this order to ensure that no data is lost if there is Because the checkpoints aren't atomic, duplicates may occur. The replay model handles history duplicates gracefully. Message duplicates may result in activities being executed multiple times if an unexpected failure occurs. +### Entity-Started Orchestrations + +With the default Azure Table tracking store, orchestrations started by entities are registered as `Pending`, including their scheduled start time, input, tags, and parent entity ID. They can be queried and terminated before their start messages become visible. Once a Pending termination has been processed, the matching delayed start is discarded without running orchestration code. + +The start message is enqueued before its Pending row is registered, and registration completes before the sending entity's history checkpoint. Workers defer registered start messages while registration is in flight. Conditional inserts and ETag-checked replacements prevent a late registration from overwriting an execution that has already started or a newer execution. Retried prepared starts do not reset terminal instances or recreate purged rows. Reusing an instance ID after a terminal execution creates a new generation. + +When an instance ID is reused, the previous execution's terminal history can remain while the new execution is Pending. An execution-independent termination is reconciled against the current Pending row with its ETag before the old terminal-history batch is discarded. A termination explicitly targeting the old execution does not cancel the newer Pending execution. The conditional update preserves execution identity, generation, input, and prior history; conflicting writes and storage failures are retried through the normal worker lifecycle rather than overwriting a replacement or recreating a deleted row. + +Upgrade all workers sharing a task hub to obtain these guarantees. Older workers do not reconcile entity-generated starts with Pending registrations, and entity start messages produced by older workers without a generation retain their legacy behavior. This does not change the provider's at-least-once delivery model or make entity actions transactional with child execution. + ## Scaling ### Horizontal Scaling diff --git a/src/DurableTask.AzureStorage/AzureStorageOrchestrationService.cs b/src/DurableTask.AzureStorage/AzureStorageOrchestrationService.cs index 54228ff3..27f03f5d 100644 --- a/src/DurableTask.AzureStorage/AzureStorageOrchestrationService.cs +++ b/src/DurableTask.AzureStorage/AzureStorageOrchestrationService.cs @@ -1086,7 +1086,44 @@ async Task IsExecutableInstanceAsync( bool allowReplayingTerminalInstances, CancellationToken cancellationToken) { - if (runtimeState.ExecutionStartedEvent == null && !newMessages.Any(msg => msg.Event is ExecutionStartedEvent)) + ExecutionStartedEvent startEvent = null; + ExecutionTerminatedEvent terminationEvent = null; + ExecutionTerminatedEvent independentTermination = null; + foreach (TaskMessage message in newMessages) + { + if (message.Event is ExecutionStartedEvent started) + { + startEvent ??= started; + } + else if (message.Event is ExecutionTerminatedEvent terminated) + { + terminationEvent = terminated; + if (message.OrchestrationInstance.ExecutionId == null) + { + independentTermination = terminated; + } + } + } + + if (runtimeState.ExecutionStartedEvent == null && + startEvent != null && + startEvent.Generation.HasValue && + startEvent.ParentInstance?.OrchestrationInstance?.InstanceId is string parentInstanceId && + DurableTask.Core.Common.Entities.IsEntityInstance(parentInstanceId)) + { + // Reconciliation can precede a Pending termination while this start waits for its session. + InstanceStatus status = await this.trackingStore.FetchInstanceStatusAsync( + startEvent.OrchestrationInstance.InstanceId, + cancellationToken); + if (status != null && + status.State.OrchestrationStatus == OrchestrationStatus.Terminated && + string.Equals(status.State.OrchestrationInstance.ExecutionId, startEvent.OrchestrationInstance.ExecutionId, StringComparison.OrdinalIgnoreCase)) + { + return $"Instance is {OrchestrationStatus.Terminated}"; + } + } + + if (runtimeState.ExecutionStartedEvent == null && startEvent == null) { var instanceId = newMessages[0].OrchestrationInstance.InstanceId; @@ -1096,13 +1133,12 @@ async Task IsExecutableInstanceAsync( } else { - TaskMessage executionTerminatedEventMessage = newMessages.LastOrDefault(msg => msg.Event is ExecutionTerminatedEvent); - if (executionTerminatedEventMessage is not null) + if (terminationEvent != null) { - var executionTerminatedEvent = (ExecutionTerminatedEvent)executionTerminatedEventMessage.Event; await this.trackingStore.UpdateStatusForTerminationAsync( instanceId, - executionTerminatedEvent); + terminationEvent, + cancellationToken); return $"Instance is {OrchestrationStatus.Terminated}"; } @@ -1122,7 +1158,24 @@ await this.trackingStore.UpdateStatusForTerminationAsync( runtimeState.OrchestrationStatus != OrchestrationStatus.Pending && runtimeState.OrchestrationStatus != OrchestrationStatus.Suspended) { - InstanceStatus instanceStatus = await this.trackingStore.FetchInstanceStatusAsync(runtimeState.OrchestrationInstance.InstanceId); + InstanceStatus instanceStatus = await this.trackingStore.FetchInstanceStatusAsync( + runtimeState.OrchestrationInstance.InstanceId, + cancellationToken); + if (instanceStatus != null && + instanceStatus.State.OrchestrationStatus == OrchestrationStatus.Pending && + !string.Equals(instanceStatus.State.OrchestrationInstance.ExecutionId, runtimeState.OrchestrationInstance.ExecutionId, StringComparison.OrdinalIgnoreCase) && + independentTermination != null && + this.trackingStore is AzureTableTrackingStore tableTrackingStore) + { + // ID reuse can leave old terminal history while the latest execution is still Pending. + await tableTrackingStore.UpdateStatusForTerminationAsync( + runtimeState.OrchestrationInstance.InstanceId, + independentTermination, + instanceStatus.ETag, + cancellationToken); + return $"Instance is {OrchestrationStatus.Terminated}"; + } + if (instanceStatus == null || (instanceStatus.State.OrchestrationInstance.ExecutionId == runtimeState.OrchestrationInstance.ExecutionId && instanceStatus.State.OrchestrationStatus != runtimeState.OrchestrationStatus)) { @@ -1453,9 +1506,64 @@ async Task CommitOutboundQueueMessages( } } + // Custom instance stores need not support conditional Pending registration or start reconciliation. await enqueueOperations.ParallelForEachAsync( this.settings.MaxStorageOperationConcurrency, - op => op.Queue.AddMessageAsync(op.Message, session)); + op => this.trackingStore is AzureTableTrackingStore && + op.Message.Event is ExecutionStartedEvent && + DurableTask.Core.Common.Entities.IsEntityInstance(session.Instance.InstanceId) + ? this.SendEntityOrchestrationStartAsync(op.Queue, op.Message, session) + : op.Queue.AddMessageAsync(op.Message, session)); + } + + internal async Task SendEntityOrchestrationStartAsync( + TaskHubQueue queue, + TaskMessage message, + OrchestrationSession session) + { + var startEvent = (ExecutionStartedEvent)message.Event; + // Retrying a prepared message must not reset a progressed or purged instance. + bool registerPending = !startEvent.Generation.HasValue; + InstanceStatus existingInstance = null; + if (registerPending) + { + existingInstance = await this.trackingStore.FetchInstanceStatusAsync(message.OrchestrationInstance.InstanceId); + if (existingInstance != null && + (string.Equals(existingInstance.State.OrchestrationInstance.ExecutionId, message.OrchestrationInstance.ExecutionId, StringComparison.OrdinalIgnoreCase) || + existingInstance.State.OrchestrationStatus is OrchestrationStatus.Pending or OrchestrationStatus.Running or OrchestrationStatus.Suspended or OrchestrationStatus.ContinuedAsNew)) + { + startEvent.Generation = existingInstance.State.Generation ?? 0; + registerPending = false; + } + else + { + startEvent.Generation = (existingInstance?.State.Generation ?? -1) + 1; + } + } + + // As with client starts, enqueue before registering Pending and before checkpointing the sender. + MessageData queuedMessage = await queue.AddMessageAsync(message, session); + if (registerPending) + { + string inputPayloadOverride = queuedMessage.CompressedBlobName == null + ? null + : this.messageManager.GetBlobUrl(queuedMessage.CompressedBlobName); + try + { + await this.trackingStore.SetNewExecutionAsync(startEvent, existingInstance?.ETag, inputPayloadOverride); + } + catch (DurableTaskStorageException e) when ( + existingInstance != null && + e.HttpStatusCode == (int)HttpStatusCode.NotFound && + e.ErrorCode == "ResourceNotFound") + { + this.settings.Logger.GeneralWarning( + this.azureStorageClient.TableAccountName, + this.settings.TaskHubName, + "Skipped Pending registration because the previous instance was purged after its status was read.", + instanceId: message.OrchestrationInstance.InstanceId); + } + } } async Task DeleteMessageBatchAsync(OrchestrationSession session, IList messagesToDelete) diff --git a/src/DurableTask.AzureStorage/Messaging/TaskHubQueue.cs b/src/DurableTask.AzureStorage/Messaging/TaskHubQueue.cs index 4441f76b..4c2e34e8 100644 --- a/src/DurableTask.AzureStorage/Messaging/TaskHubQueue.cs +++ b/src/DurableTask.AzureStorage/Messaging/TaskHubQueue.cs @@ -71,8 +71,8 @@ public TaskHubQueue( /// /// Instance of /// Instance of - /// - public Task AddMessageAsync(TaskMessage message, SessionBase sourceSession) + /// The queued message, including any large-message blob reference. + public Task AddMessageAsync(TaskMessage message, SessionBase sourceSession) { return this.AddMessageAsync(message, sourceSession.Instance, sourceSession); } diff --git a/src/DurableTask.AzureStorage/OrchestrationSessionManager.cs b/src/DurableTask.AzureStorage/OrchestrationSessionManager.cs index 9f010535..6e435eb2 100644 --- a/src/DurableTask.AzureStorage/OrchestrationSessionManager.cs +++ b/src/DurableTask.AzureStorage/OrchestrationSessionManager.cs @@ -252,10 +252,12 @@ async Task> DedupeExecutionStartedMessagesAsync( foreach (MessageData message in messages) { - // We do de-duplication when creating top-level orchestrations but not for sub-orchestrations. - // De-dupe protection for sub-orchestrations is not implemented and will require a bit more work. + // Registered entity starts use the same Pending-row reconciliation as client starts. + // Awaited sub-orchestrations and older entity starts do not have Pending registration. if (message.TaskMessage.Event is ExecutionStartedEvent startEvent && - startEvent.ParentInstance == null) + (startEvent.ParentInstance == null || + (startEvent.Generation.HasValue && + DurableTask.Core.Common.Entities.IsEntityInstance(startEvent.ParentInstance.OrchestrationInstance.InstanceId)))) { executionStartedMessages ??= new List(messages.Count); executionStartedMessages.Add(message); @@ -289,9 +291,15 @@ async Task> DedupeExecutionStartedMessagesAsync( if (remoteOrchestrationsById.TryGetValue(localInstance.InstanceId, out InstanceStatus remoteInstance) && (remoteInstance.State.OrchestrationInstance.ExecutionId == null || string.Equals(localInstance.ExecutionId, remoteInstance.State.OrchestrationInstance.ExecutionId, StringComparison.OrdinalIgnoreCase))) { - // Happy path: The message matches the table status. Alternatively, if the table doesn't have an ExecutionId field (older clients, pre-v1.8.5), - // then we have no way of knowing if it's a duplicate. Either way, allow it to run. - if (this.settings.UseInstanceTableEtag) + if (remoteInstance.State.OrchestrationStatus == OrchestrationStatus.Terminated && + remoteInstance.State.OrchestrationInstance.ExecutionId != null) + { + // Pending termination has no history event. Reject its delayed start using the same status read. + messagesToDiscard ??= new List(executionStartedMessages.Count); + messagesToDiscard.Add(message); + } + // A missing execution ID is a legacy record that cannot be safely deduplicated. + else if (this.settings.UseInstanceTableEtag) { message.MessageMetadata = remoteInstance.ETag; } diff --git a/src/DurableTask.AzureStorage/Tracking/AzureTableTrackingStore.cs b/src/DurableTask.AzureStorage/Tracking/AzureTableTrackingStore.cs index 347b9b55..ad0c64bb 100644 --- a/src/DurableTask.AzureStorage/Tracking/AzureTableTrackingStore.cs +++ b/src/DurableTask.AzureStorage/Tracking/AzureTableTrackingStore.cs @@ -815,6 +815,13 @@ public override async Task SetNewExecutionAsync( // It is possible that the queue message was small enough to be written directly to a queue message, // not a blob, but is too large to be written to a table property. await this.CompressLargeMessageAsync(entity, listOfBlobs: null, cancellationToken: cancellationToken); + // Pending rows expose large inputs as URLs, just like checkpointed instance rows. + this.SetInstancesTablePropertyFromHistoryProperty( + entity, + entity, + historyPropertyName: InputProperty, + instancePropertyName: InputProperty, + data: inputPayloadOverride ?? executionStartedEvent.Input); Stopwatch stopwatch = Stopwatch.StartNew(); try @@ -881,10 +888,19 @@ public override async Task UpdateStatusForRewindAsync(string instanceId, Cancell } /// - public override async Task UpdateStatusForTerminationAsync( + public override Task UpdateStatusForTerminationAsync( string instanceId, ExecutionTerminatedEvent executionTerminatedEvent, CancellationToken cancellationToken = default) + { + return this.UpdateStatusForTerminationAsync(instanceId, executionTerminatedEvent, ETag.All, cancellationToken); + } + + internal async Task UpdateStatusForTerminationAsync( + string instanceId, + ExecutionTerminatedEvent executionTerminatedEvent, + ETag ifMatch, + CancellationToken cancellationToken = default) { string sanitizedInstanceId = KeySanitation.EscapePartitionKey(instanceId); TableEntity instanceEntity = new TableEntity(sanitizedInstanceId, "") @@ -901,7 +917,7 @@ public override async Task UpdateStatusForTerminationAsync( await this.CompressLargeMessageAsync(instanceEntity, listOfBlobs: null, cancellationToken: cancellationToken, addBlobPropertyName: false); Stopwatch stopwatch = Stopwatch.StartNew(); - await this.InstancesTable.MergeEntityAsync(instanceEntity, ETag.All, cancellationToken); + await this.InstancesTable.MergeEntityAsync(instanceEntity, ifMatch, cancellationToken); this.settings.Logger.InstanceStatusUpdate( this.storageAccountName, diff --git a/src/DurableTask.AzureStorage/Tracking/ITrackingStore.cs b/src/DurableTask.AzureStorage/Tracking/ITrackingStore.cs index 6fc2aa8b..a54f0d33 100644 --- a/src/DurableTask.AzureStorage/Tracking/ITrackingStore.cs +++ b/src/DurableTask.AzureStorage/Tracking/ITrackingStore.cs @@ -148,10 +148,10 @@ interface ITrackingStore AsyncPageable GetStateAsync(OrchestrationInstanceStatusQueryCondition condition, CancellationToken cancellationToken = default); /// - /// Used to set a state in the tracking store whenever a new execution is initiated from the client + /// Used to set a Pending state whenever a new execution is initiated by a client or entity. /// /// The Execution Started Event being queued - /// The eTag value to use for optimistic concurrency or null to overwrite any existing execution status. + /// The eTag read before enqueueing the start, or null for a new instance. /// An override value to use for the Input column. If not specified, uses . /// The token to monitor for cancellation requests. The default value is . /// Returns true if the record was created successfully; false otherwise. diff --git a/test/DurableTask.AzureStorage.Tests/EntityStartedOrchestrationTests.cs b/test/DurableTask.AzureStorage.Tests/EntityStartedOrchestrationTests.cs new file mode 100644 index 00000000..ffbfdbae --- /dev/null +++ b/test/DurableTask.AzureStorage.Tests/EntityStartedOrchestrationTests.cs @@ -0,0 +1,901 @@ +// ---------------------------------------------------------------------------------- +// 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.Tests +{ + using System; + using System.Collections.Generic; + using System.Linq; + using System.Reflection; + using System.Threading; + using System.Threading.Tasks; + using Azure; + using Azure.Core; + using Azure.Core.Pipeline; + using Azure.Data.Tables; + using DurableTask.AzureStorage.Messaging; + using DurableTask.AzureStorage.Storage; + using DurableTask.AzureStorage.Tracking; + using DurableTask.Core; + using DurableTask.Core.Entities; + using DurableTask.Core.Entities.OperationFormat; + using DurableTask.Core.History; + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public class EntityStartedOrchestrationTests + { + AzureStorageOrchestrationService service; + TaskHubWorker worker; + AzureStorageOrchestrationServiceSettings settings; + PendingWritePolicy pendingWritePolicy; + + [TestInitialize] + public async Task Initialize() + { + this.settings = TestHelpers.GetTestAzureStorageOrchestrationServiceSettings(enableExtendedSessions: false); + this.settings.TaskHubName = "EntityStart" + Guid.NewGuid().ToString("N").Substring(0, 12); + this.settings.PartitionCount = 1; + this.settings.UseAppLease = false; + this.settings.UseSeparateQueueForEntityWorkItems = true; + this.settings.MaxQueuePollingInterval = TimeSpan.FromMilliseconds(100); + this.settings.ControlQueueVisibilityTimeout = TimeSpan.FromSeconds(5); + + this.pendingWritePolicy = new PendingWritePolicy(this.settings.InstanceTableName); + var tableOptions = new TableClientOptions(); + tableOptions.AddPolicy(this.pendingWritePolicy, HttpPipelinePosition.PerCall); + string connectionString = TestHelpers.GetTestStorageAccountConnectionString(); + this.settings.StorageAccountClientProvider = new StorageAccountClientProvider( + StorageServiceClientProvider.ForBlob(connectionString), + StorageServiceClientProvider.ForQueue(connectionString), + StorageServiceClientProvider.ForTable(connectionString, tableOptions)); + + this.service = new AzureStorageOrchestrationService(this.settings); + await this.service.CreateAsync(); + } + + [TestCleanup] + public async Task Cleanup() + { + if (this.worker != null) + { + await this.worker.StopAsync(isForced: true); + this.worker.Dispose(); + } + + if (this.service != null) + { + await this.service.DeleteAsync(); + this.service.Dispose(); + } + } + + [DataTestMethod] + [DataRow(false, 16)] + [DataRow(true, 16)] + [DataRow(false, 36 * 1024)] + [DataRow(true, 60 * 1024)] + public async Task EntityStartedScheduledOrchestrationIsPendingAndCanBeTerminated(bool useInstanceTableEtag, int inputLength) + { + this.settings.UseInstanceTableEtag = useInstanceTableEtag; + var entity = new OrchestrationInstance { InstanceId = "@pendingstarter@test" }; + string childInstanceId = Guid.NewGuid().ToString("N"); + string input = "\"" + new string('x', inputLength) + "\""; + DateTime createdTime = DateTime.UtcNow; + DateTime scheduledStartTime = default; + int executions = 0; + + this.CreateEntityWorker(() => + { + scheduledStartTime = DateTime.UtcNow.AddSeconds(10); + return new StartNewOrchestrationOperationAction + { + Name = typeof(ChildOrchestration).FullName, + InstanceId = childInstanceId, + Input = input, + ScheduledStartTime = scheduledStartTime, + Tags = new Dictionary { ["source"] = "entity" }, + }; + }); + this.worker.AddOrchestrationDispatcherMiddleware((context, next) => + { + Interlocked.Increment(ref executions); + return next(); + }); + + await this.worker.StartAsync(); + await this.service.SendTaskOrchestrationMessageAsync( + ClientEntityHelpers.EmitOperationSignal(entity, Guid.NewGuid(), "start", null, null).AsTaskMessage()); + + var client = new TaskHubClient(this.service); + await new TestInstance(client, entity, createdTime, null).WaitForStart(TimeSpan.FromSeconds(30)); + + // The entity checkpoint must not complete before its child's Pending registration. + var child = new TestOrchestrationClient(client, typeof(ChildOrchestration), childInstanceId, createdTime); + OrchestrationState state = await child.GetStatusAsync(); + Assert.IsNotNull(state, "An entity-started orchestration must be visible before its scheduled start."); + Assert.AreEqual(OrchestrationStatus.Pending, state.OrchestrationStatus); + Assert.AreEqual(scheduledStartTime, state.ScheduledStartTime); + Assert.AreEqual(input, state.Input); + Assert.AreEqual("entity", state.Tags["source"]); + Assert.AreEqual(entity.InstanceId, state.ParentInstance.OrchestrationInstance.InstanceId); + OrchestrationState listed = (await this.service.GetOrchestrationStateAsync(CancellationToken.None)) + .Single(item => item.OrchestrationInstance.InstanceId == childInstanceId); + Assert.AreEqual(OrchestrationStatus.Pending, listed.OrchestrationStatus); + Assert.IsTrue(DateTime.UtcNow < scheduledStartTime, "Termination must happen while the start is still delayed."); + Assert.IsTrue(await this.service.AllControlQueues.Single().InnerQueue.GetApproximateMessagesCountAsync() > 0); + + if (inputLength >= 60 * 1024) + { + var table = new TableServiceClient(TestHelpers.GetTestStorageAccountConnectionString()).GetTableClient(this.settings.InstanceTableName); + TableEntity row = await table.GetEntityAsync(childInstanceId, string.Empty); + Assert.IsTrue(Uri.TryCreate(row.GetString("Input"), UriKind.Absolute, out _), + "Pending must reuse the compressed queue message's blob reference for large inputs."); + } + + await child.TerminateAsync("terminate before start"); + state = await child.WaitForCompletionAsync(TimeSpan.FromSeconds(5)); + Assert.AreEqual(OrchestrationStatus.Terminated, state?.OrchestrationStatus); + + // Wait for consumption of the actual delayed queue message, not an arbitrary sleep. + using (var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(30))) + { + while (await this.service.AllControlQueues.Single().InnerQueue.GetApproximateMessagesCountAsync() > 0) + { + await Task.Delay(TimeSpan.FromMilliseconds(50), timeout.Token); + } + } + + Assert.AreEqual(0, executions, "Terminating a Pending entity-started orchestration must prevent execution."); + state = await child.GetStatusAsync(); + Assert.AreEqual(OrchestrationStatus.Terminated, state.OrchestrationStatus); + Assert.AreEqual("terminate before start", state.Output); + Assert.AreEqual( + 0, + (await this.service.TrackingStore.GetHistoryEventsAsync(childInstanceId, null)).Events.Count); + } + + [DataTestMethod] + [DataRow(false)] + [DataRow(true)] + public async Task ReusedEntityStartedScheduledOrchestrationCanBeTerminated(bool useInstanceTableEtag) + { + this.settings.UseInstanceTableEtag = useInstanceTableEtag; + string childInstanceId = Guid.NewGuid().ToString("N"); + DateTime createdTime = DateTime.UtcNow; + DateTime scheduledStartTime = default; + int executions = 0; + this.CreateEntityWorker(() => + { + scheduledStartTime = DateTime.UtcNow.AddSeconds(10); + return new StartNewOrchestrationOperationAction + { + Name = typeof(ChildOrchestration).FullName, + InstanceId = childInstanceId, + Input = "\"must-not-run\"", + ScheduledStartTime = scheduledStartTime, + }; + }); + this.worker.AddOrchestrationDispatcherMiddleware((context, next) => + { + Interlocked.Increment(ref executions); + return next(); + }); + await this.worker.StartAsync(); + var client = new TaskHubClient(this.service); + OrchestrationInstance previous = await client.CreateOrchestrationInstanceAsync( + typeof(ChildOrchestration), childInstanceId, "done"); + OrchestrationState previousState = await client.WaitForOrchestrationAsync(previous, TimeSpan.FromSeconds(30)); + Assert.IsNotNull(previousState); + Assert.AreEqual(OrchestrationStatus.Completed, previousState.OrchestrationStatus); + Interlocked.Exchange(ref executions, 0); + OrchestrationHistory previousHistory = await this.service.TrackingStore.GetHistoryEventsAsync( + childInstanceId, previous.ExecutionId); + + var entity = new OrchestrationInstance { InstanceId = "@pendingstarter@test" }; + await this.service.SendTaskOrchestrationMessageAsync( + ClientEntityHelpers.EmitOperationSignal(entity, Guid.NewGuid(), "start", null, null).AsTaskMessage()); + await new TestInstance(client, entity, createdTime, null).WaitForStart(TimeSpan.FromSeconds(30)); + InstanceStatus pending = await this.service.TrackingStore.FetchInstanceStatusAsync(childInstanceId); + Assert.AreEqual(OrchestrationStatus.Pending, pending.State.OrchestrationStatus); + Assert.AreNotEqual(previous.ExecutionId, pending.State.OrchestrationInstance.ExecutionId); + Assert.AreEqual(previousState.Generation + 1, pending.State.Generation); + Assert.IsTrue(DateTime.UtcNow < scheduledStartTime); + + var child = new TestOrchestrationClient(client, typeof(ChildOrchestration), childInstanceId, createdTime); + await child.TerminateAsync("terminate the reused Pending execution"); + using (var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(30))) + { + while (await this.service.AllControlQueues.Single().InnerQueue.GetApproximateMessagesCountAsync() > 0) + { + await Task.Delay(TimeSpan.FromMilliseconds(50), timeout.Token); + } + } + + OrchestrationState terminated = await child.GetStatusAsync(); + Assert.AreEqual(OrchestrationStatus.Terminated, terminated.OrchestrationStatus, + "Old terminal history must not cause termination of the new Pending execution to be discarded."); + Assert.AreEqual(0, executions); + Assert.AreEqual(pending.State.OrchestrationInstance.ExecutionId, terminated.OrchestrationInstance.ExecutionId); + Assert.AreEqual(pending.State.Generation, terminated.Generation); + Assert.AreEqual("terminate the reused Pending execution", terminated.Output); + Assert.AreEqual( + previousHistory.Events.Count, + (await this.service.TrackingStore.GetHistoryEventsAsync(childInstanceId, previous.ExecutionId)).Events.Count); + Assert.AreEqual( + 0, + (await this.service.TrackingStore.GetHistoryEventsAsync(childInstanceId, terminated.OrchestrationInstance.ExecutionId)).Events.Count); + } + + [DataTestMethod] + [DataRow(false)] + [DataRow(true)] + public async Task RegisteredEntityStartRechecksTerminationAfterReconciliation(bool useInstanceTableEtag) + { + this.settings.UseInstanceTableEtag = useInstanceTableEtag; + OrchestrationSession session = this.CreateEntitySession(); + TaskMessage start = CreateStart(session); + ControlQueue queue = this.service.AllControlQueues.Single(); + await this.service.SendEntityOrchestrationStartAsync(queue, start, session); + using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(10)); + MessageData queued = (await queue.GetMessagesAsync(timeout.Token)).Single(); + Assert.AreEqual(1, (await this.ReconcileStartAsync(queued)).Count()); + + await this.service.TrackingStore.UpdateStatusForTerminationAsync( + start.OrchestrationInstance.InstanceId, new ExecutionTerminatedEvent(-1, "terminated after reconciliation")); + int reads = this.pendingWritePolicy.InstanceReads; + string decision = await this.EvaluateBatchAsync(new OrchestrationRuntimeState(), new[] { queued.TaskMessage }); + + Assert.AreEqual("Instance is Terminated", decision); + Assert.AreEqual(reads + 1, this.pendingWritePolicy.InstanceReads, + "Only registered entity admission needs this fresh termination check."); + } + + [DataTestMethod] + [DataRow(null, 0)] + [DataRow("awaited-parent", 0)] + [DataRow("@legacy@test", null)] + public async Task HistorylessClientAwaitedAndLegacyStartsDoNotAddStatusReads(string parentInstanceId, int? generation) + { + TaskMessage start = CreateStart(this.CreateEntitySession()); + var startEvent = (ExecutionStartedEvent)start.Event; + startEvent.Generation = generation; + startEvent.ParentInstance = parentInstanceId == null + ? null + : new ParentInstance { OrchestrationInstance = new OrchestrationInstance { InstanceId = parentInstanceId } }; + int reads = this.pendingWritePolicy.InstanceReads; + + Assert.IsNull(await this.EvaluateBatchAsync(new OrchestrationRuntimeState(), new[] { start })); + + Assert.AreEqual(reads, this.pendingWritePolicy.InstanceReads); + } + + [DataTestMethod] + [DataRow(false, OrchestrationStatus.Completed)] + [DataRow(true, OrchestrationStatus.Completed)] + [DataRow(false, OrchestrationStatus.Failed)] + [DataRow(true, OrchestrationStatus.Failed)] + [DataRow(false, OrchestrationStatus.Terminated)] + [DataRow(true, OrchestrationStatus.Terminated)] + [DataRow(false, OrchestrationStatus.Canceled)] + [DataRow(true, OrchestrationStatus.Canceled)] + public async Task ReusedPendingTerminationPreservesCurrentExecutionMetadata(bool useInstanceTableEtag, OrchestrationStatus oldStatus) + { + this.settings.UseInstanceTableEtag = useInstanceTableEtag; + var (oldRuntime, start, pending) = await this.CreateReusedPendingAsync(oldStatus); + string reason = new string('r', 36 * 1024); + TaskMessage termination = CreateTermination(start.OrchestrationInstance.InstanceId, reason); + int reads = this.pendingWritePolicy.InstanceReads; + + string decision = await this.EvaluateBatchAsync(oldRuntime, new[] { termination }); + + Assert.AreEqual("Instance is Terminated", decision); + Assert.AreEqual(reads + 1, this.pendingWritePolicy.InstanceReads, + "Reuse the terminal-history status snapshot instead of querying it twice."); + InstanceStatus after = await this.service.TrackingStore.FetchInstanceStatusAsync(start.OrchestrationInstance.InstanceId); + Assert.AreEqual(OrchestrationStatus.Terminated, after.State.OrchestrationStatus); + Assert.AreEqual(start.OrchestrationInstance.ExecutionId, after.State.OrchestrationInstance.ExecutionId); + Assert.AreEqual(pending.State.Generation, after.State.Generation); + Assert.AreEqual(pending.State.CreatedTime, after.State.CreatedTime); + Assert.AreEqual(pending.State.ParentInstance.OrchestrationInstance.InstanceId, after.State.ParentInstance.OrchestrationInstance.InstanceId); + Assert.AreEqual(reason, after.State.Output); + Assert.AreEqual(termination.Event.Timestamp, after.State.LastUpdatedTime); + Assert.AreNotEqual(pending.ETag, after.ETag); + Assert.AreEqual( + oldRuntime.Events.Count, + (await this.service.TrackingStore.GetHistoryEventsAsync(start.OrchestrationInstance.InstanceId, oldRuntime.OrchestrationInstance.ExecutionId)).Events.Count); + } + + [DataTestMethod] + [DataRow(false)] + [DataRow(true)] + public async Task OldExecutionTerminationDoesNotCancelReusedPendingExecution(bool useInstanceTableEtag) + { + this.settings.UseInstanceTableEtag = useInstanceTableEtag; + var (oldRuntime, start, pending) = await this.CreateReusedPendingAsync(); + TaskMessage termination = CreateTermination( + start.OrchestrationInstance.InstanceId, "old execution only", oldRuntime.OrchestrationInstance.ExecutionId); + + Assert.AreEqual("Instance is Completed", await this.EvaluateBatchAsync(oldRuntime, new[] { termination })); + + InstanceStatus after = await this.service.TrackingStore.FetchInstanceStatusAsync(start.OrchestrationInstance.InstanceId); + Assert.AreEqual(pending.ETag, after.ETag); + Assert.AreEqual(OrchestrationStatus.Pending, after.State.OrchestrationStatus); + Assert.AreEqual(start.OrchestrationInstance.ExecutionId, after.State.OrchestrationInstance.ExecutionId); + } + + [DataTestMethod] + [DataRow(false)] + [DataRow(true)] + public async Task ExecutionIndependentTerminationIsNotHiddenByOldTargetedTermination(bool independentLast) + { + var (oldRuntime, start, pending) = await this.CreateReusedPendingAsync(); + TaskMessage oldTermination = CreateTermination( + start.OrchestrationInstance.InstanceId, "old execution", oldRuntime.OrchestrationInstance.ExecutionId); + TaskMessage independent = CreateTermination(start.OrchestrationInstance.InstanceId, "current execution"); + TaskMessage[] messages = independentLast + ? new[] { oldTermination, independent } + : new[] { independent, oldTermination }; + + Assert.AreEqual("Instance is Terminated", await this.EvaluateBatchAsync(oldRuntime, messages)); + + InstanceStatus after = await this.service.TrackingStore.FetchInstanceStatusAsync(start.OrchestrationInstance.InstanceId); + Assert.AreEqual("current execution", after.State.Output); + Assert.AreEqual(start.OrchestrationInstance.ExecutionId, after.State.OrchestrationInstance.ExecutionId); + Assert.AreEqual(pending.State.Generation, after.State.Generation); + } + + [DataTestMethod] + [DataRow(false, "replace")] + [DataRow(true, "replace")] + [DataRow(false, "start")] + [DataRow(true, "start")] + [DataRow(false, "purge")] + [DataRow(true, "purge")] + public async Task ReusedPendingTerminationRejectsConcurrentRowChanges(bool useInstanceTableEtag, string change) + { + this.settings.UseInstanceTableEtag = useInstanceTableEtag; + var (oldRuntime, start, pending) = await this.CreateReusedPendingAsync(); + TaskMessage replacement = CreateStart(this.CreateEntitySession(), start.OrchestrationInstance.InstanceId); + ((ExecutionStartedEvent)replacement.Event).Generation = pending.State.Generation + 1; + this.pendingWritePolicy.BeforeWrite = async () => + { + if (change == "purge") + { + await this.service.TrackingStore.PurgeInstanceHistoryAsync(start.OrchestrationInstance.InstanceId); + } + else if (change == "replace") + { + Assert.IsTrue(await this.service.TrackingStore.SetNewExecutionAsync((ExecutionStartedEvent)replacement.Event, pending.ETag, null)); + } + else + { + OrchestrationHistory history = await this.service.TrackingStore.GetHistoryEventsAsync(start.OrchestrationInstance.InstanceId, null); + var running = new OrchestrationRuntimeState(); + running.AddEvent(new OrchestratorStartedEvent(-1)); + running.AddEvent(start.Event); + running.AddEvent(new OrchestratorCompletedEvent(-1)); + await this.service.TrackingStore.UpdateStateAsync( + running, new OrchestrationRuntimeState(), start.OrchestrationInstance.InstanceId, start.OrchestrationInstance.ExecutionId, + new OrchestrationETags { HistoryETag = history.ETag, InstanceETag = pending.ETag }, history.TrackingStoreContext); + } + }; + + DurableTaskStorageException error = await Assert.ThrowsExceptionAsync( + () => this.EvaluateBatchAsync(oldRuntime, new[] { CreateTermination(start.OrchestrationInstance.InstanceId, "racing termination") })); + Assert.AreEqual(change == "purge" ? 404 : 412, error.HttpStatusCode); + InstanceStatus after = await this.service.TrackingStore.FetchInstanceStatusAsync(start.OrchestrationInstance.InstanceId); + if (change == "purge") + { + Assert.IsNull(after); + Assert.AreEqual(0, (await this.service.TrackingStore.GetHistoryEventsAsync(start.OrchestrationInstance.InstanceId, null)).Events.Count); + } + else + { + Assert.IsNotNull(after); + Assert.AreEqual(change == "replace" ? replacement.OrchestrationInstance.ExecutionId : start.OrchestrationInstance.ExecutionId, after.State.OrchestrationInstance.ExecutionId); + Assert.AreEqual(change == "replace" ? OrchestrationStatus.Pending : OrchestrationStatus.Running, after.State.OrchestrationStatus); + Assert.IsNull(after.State.Output); + } + } + + [DataTestMethod] + [DataRow(false, 404, "TableNotFound")] + [DataRow(true, 404, "TableNotFound")] + [DataRow(false, 503, "ServerBusy")] + [DataRow(true, 503, "ServerBusy")] + public async Task ReusedPendingTerminationPropagatesStorageFailures(bool useInstanceTableEtag, int status, string errorCode) + { + this.settings.UseInstanceTableEtag = useInstanceTableEtag; + var (oldRuntime, start, pending) = await this.CreateReusedPendingAsync(); + this.pendingWritePolicy.BeforeWrite = () => throw new RequestFailedException(status, "Injected termination failure", errorCode, null); + + DurableTaskStorageException error = await Assert.ThrowsExceptionAsync( + () => this.EvaluateBatchAsync(oldRuntime, new[] { CreateTermination(start.OrchestrationInstance.InstanceId, "terminate") })); + + Assert.AreEqual(status, error.HttpStatusCode); + Assert.AreEqual(errorCode, error.ErrorCode); + Assert.AreEqual(pending.ETag, (await this.service.TrackingStore.FetchInstanceStatusAsync(start.OrchestrationInstance.InstanceId)).ETag); + } + + [DataTestMethod] + [DataRow(false)] + [DataRow(true)] + public async Task ReusedPendingTerminationPropagatesCancellation(bool useInstanceTableEtag) + { + this.settings.UseInstanceTableEtag = useInstanceTableEtag; + var (oldRuntime, start, pending) = await this.CreateReusedPendingAsync(); + using var cancellation = new CancellationTokenSource(); + this.pendingWritePolicy.BeforeWrite = () => + { + Assert.AreEqual(cancellation.Token, this.pendingWritePolicy.LastWriteCancellationToken); + cancellation.Cancel(); + cancellation.Token.ThrowIfCancellationRequested(); + return Task.CompletedTask; + }; + + await Assert.ThrowsExceptionAsync( + () => this.EvaluateBatchAsync( + oldRuntime, new[] { CreateTermination(start.OrchestrationInstance.InstanceId, "terminate") }, cancellation.Token)); + + Assert.IsTrue(cancellation.IsCancellationRequested); + Assert.AreEqual(pending.ETag, (await this.service.TrackingStore.FetchInstanceStatusAsync(start.OrchestrationInstance.InstanceId)).ETag); + } + + [DataTestMethod] + [DataRow(false)] + [DataRow(true)] + public async Task ImmediateEntityStartCanCompleteAndContinueAsNew(bool continueAsNew) + { + this.settings.UseInstanceTableEtag = true; + string childInstanceId = Guid.NewGuid().ToString("N"); + this.CreateEntityWorker(() => new StartNewOrchestrationOperationAction + { + Name = typeof(ChildOrchestration).FullName, + InstanceId = childInstanceId, + Input = continueAsNew ? "\"continue\"" : "\"done\"", + }); + await this.worker.StartAsync(); + var entity = new OrchestrationInstance { InstanceId = "@pendingstarter@test" }; + await this.service.SendTaskOrchestrationMessageAsync( + ClientEntityHelpers.EmitOperationSignal(entity, Guid.NewGuid(), "start", null, null).AsTaskMessage()); + + var client = new TaskHubClient(this.service); + OrchestrationState completed = await client.WaitForOrchestrationAsync( + new OrchestrationInstance { InstanceId = childInstanceId }, TimeSpan.FromSeconds(30)); + Assert.IsNotNull(completed); + Assert.AreEqual(OrchestrationStatus.Completed, completed.OrchestrationStatus); + Assert.AreEqual("\"done\"", completed.Output); + } + + [TestMethod] + public async Task EntityStartIsEnqueuedBeforePendingAndDeferredUntilRegistration() + { + OrchestrationSession session = this.CreateEntitySession(); + TaskMessage start = CreateStart(session); + ControlQueue queue = this.service.AllControlQueues.Single(); + MessageData queuedMessage = null; + this.pendingWritePolicy.BeforeWrite = async () => + { + using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(10)); + queuedMessage = (await queue.GetMessagesAsync(timeout.Token)).Single(); + Assert.AreEqual(session.Instance.InstanceId, queuedMessage.Sender.InstanceId); + Assert.AreEqual(session.Instance.ExecutionId, queuedMessage.Sender.ExecutionId); + Assert.AreEqual(session.GetCurrentEpisode(), queuedMessage.Episode); + Assert.IsNull(await this.service.TrackingStore.FetchInstanceStatusAsync(start.OrchestrationInstance.InstanceId)); + string receipt = queuedMessage.OriginalQueueMessage.PopReceipt; + Assert.AreEqual(0, (await this.ReconcileStartAsync(queuedMessage)).Count()); + await TestHelpers.WaitFor( + () => queuedMessage.OriginalQueueMessage.PopReceipt != receipt, + TimeSpan.FromSeconds(10)); + }; + + await this.service.SendEntityOrchestrationStartAsync(queue, start, session); + + Assert.IsNotNull(queuedMessage); + Assert.AreEqual(0, ((ExecutionStartedEvent)start.Event).Generation); + Assert.AreEqual(1, (await this.ReconcileStartAsync(queuedMessage)).Count()); + } + + [DataTestMethod] + [DataRow(OrchestrationStatus.Pending)] + [DataRow(OrchestrationStatus.Running)] + [DataRow(OrchestrationStatus.Suspended)] + [DataRow(OrchestrationStatus.ContinuedAsNew)] + public async Task EntityStartDoesNotReplaceActiveInstance(OrchestrationStatus status) + { + OrchestrationSession session = this.CreateEntitySession(); + TaskMessage start = CreateStart(session); + TaskMessage previousStart = CreateStart(session, start.OrchestrationInstance.InstanceId); + await this.SeedInstanceAsync((ExecutionStartedEvent)previousStart.Event, status); + InstanceStatus before = await this.service.TrackingStore.FetchInstanceStatusAsync(start.OrchestrationInstance.InstanceId); + + await this.service.SendEntityOrchestrationStartAsync(this.service.AllControlQueues.Single(), start, session); + + InstanceStatus after = await this.service.TrackingStore.FetchInstanceStatusAsync(start.OrchestrationInstance.InstanceId); + Assert.AreEqual(before.ETag, after.ETag); + Assert.AreEqual(previousStart.OrchestrationInstance.ExecutionId, after.State.OrchestrationInstance.ExecutionId); + Assert.AreEqual(status, after.State.OrchestrationStatus); + Assert.AreEqual(before.State.Generation, ((ExecutionStartedEvent)start.Event).Generation); + } + + [DataTestMethod] + [DataRow(OrchestrationStatus.Completed)] + [DataRow(OrchestrationStatus.Failed)] + [DataRow(OrchestrationStatus.Terminated)] + [DataRow(OrchestrationStatus.Canceled)] + public async Task EntityStartReusesTerminalInstanceWithNextGeneration(OrchestrationStatus status) + { + OrchestrationSession session = this.CreateEntitySession(); + TaskMessage start = CreateStart(session); + TaskMessage previousStart = CreateStart(session, start.OrchestrationInstance.InstanceId); + await this.SeedInstanceAsync((ExecutionStartedEvent)previousStart.Event, status); + + await this.service.SendEntityOrchestrationStartAsync(this.service.AllControlQueues.Single(), start, session); + + InstanceStatus after = await this.service.TrackingStore.FetchInstanceStatusAsync(start.OrchestrationInstance.InstanceId); + Assert.AreEqual(start.OrchestrationInstance.ExecutionId, after.State.OrchestrationInstance.ExecutionId); + Assert.AreEqual(OrchestrationStatus.Pending, after.State.OrchestrationStatus); + Assert.AreEqual(8, after.State.Generation); + Assert.AreEqual(8, ((ExecutionStartedEvent)start.Event).Generation); + } + + [TestMethod] + public async Task RetriedEntityStartDoesNotResetTerminatedPendingInstance() + { + OrchestrationSession session = this.CreateEntitySession(); + TaskMessage start = CreateStart(session); + ControlQueue queue = this.service.AllControlQueues.Single(); + await this.service.SendEntityOrchestrationStartAsync(queue, start, session); + InstanceStatus pending = await this.service.TrackingStore.FetchInstanceStatusAsync(start.OrchestrationInstance.InstanceId); + await this.service.SendEntityOrchestrationStartAsync(queue, start, session); + Assert.AreEqual(pending.ETag, (await this.service.TrackingStore.FetchInstanceStatusAsync(start.OrchestrationInstance.InstanceId)).ETag); + + await this.service.TrackingStore.UpdateStatusForTerminationAsync( + start.OrchestrationInstance.InstanceId, new ExecutionTerminatedEvent(-1, "terminated")); + InstanceStatus terminated = await this.service.TrackingStore.FetchInstanceStatusAsync(start.OrchestrationInstance.InstanceId); + + // Also cover a fresh envelope for the same execution, without the prepared Generation. + TaskMessage retry = CreateStart(session, start.OrchestrationInstance.InstanceId); + retry.OrchestrationInstance.ExecutionId = start.OrchestrationInstance.ExecutionId; + await this.service.SendEntityOrchestrationStartAsync(queue, retry, session); + InstanceStatus after = await this.service.TrackingStore.FetchInstanceStatusAsync(start.OrchestrationInstance.InstanceId); + Assert.AreEqual(terminated.ETag, after.ETag); + Assert.AreEqual(OrchestrationStatus.Terminated, after.State.OrchestrationStatus); + Assert.AreEqual("terminated", after.State.Output); + } + + [TestMethod] + public async Task RetriedEntityStartDoesNotReplaceNewerExecutionOrRecreatePurgedInstance() + { + OrchestrationSession session = this.CreateEntitySession(); + TaskMessage start = CreateStart(session); + ControlQueue queue = this.service.AllControlQueues.Single(); + await this.service.SendEntityOrchestrationStartAsync(queue, start, session); + InstanceStatus pending = await this.service.TrackingStore.FetchInstanceStatusAsync(start.OrchestrationInstance.InstanceId); + TaskMessage newer = CreateStart(session, start.OrchestrationInstance.InstanceId); + ((ExecutionStartedEvent)newer.Event).Generation = 1; + Assert.IsTrue(await this.service.TrackingStore.SetNewExecutionAsync((ExecutionStartedEvent)newer.Event, pending.ETag, null)); + InstanceStatus beforeRetry = await this.service.TrackingStore.FetchInstanceStatusAsync(start.OrchestrationInstance.InstanceId); + + await this.service.SendEntityOrchestrationStartAsync(queue, start, session); + InstanceStatus afterRetry = await this.service.TrackingStore.FetchInstanceStatusAsync(start.OrchestrationInstance.InstanceId); + Assert.AreEqual(beforeRetry.ETag, afterRetry.ETag); + Assert.AreEqual(newer.OrchestrationInstance.ExecutionId, afterRetry.State.OrchestrationInstance.ExecutionId); + Assert.AreEqual(1, afterRetry.State.Generation); + + await this.service.TrackingStore.PurgeInstanceHistoryAsync(start.OrchestrationInstance.InstanceId); + await this.service.SendEntityOrchestrationStartAsync(queue, start, session); + Assert.IsNull(await this.service.TrackingStore.FetchInstanceStatusAsync(start.OrchestrationInstance.InstanceId)); + } + + [TestMethod] + public async Task EntityBatchCanRetryAfterPendingRegistrationFails() + { + OrchestrationSession session = this.CreateEntitySession(); + TaskMessage start = CreateStart(session); + ControlQueue queue = this.service.AllControlQueues.Single(); + this.pendingWritePolicy.BeforeWrite = () => throw new RequestFailedException(503, "Injected storage failure", "ServerBusy", null); + await Assert.ThrowsExceptionAsync( + () => this.service.SendEntityOrchestrationStartAsync(queue, start, session)); + Assert.IsNull(await this.service.TrackingStore.FetchInstanceStatusAsync(start.OrchestrationInstance.InstanceId)); + + // Reprocessing an uncheckpointed entity batch emits a fresh start for the same instance ID. + TaskMessage retry = CreateStart(session, start.OrchestrationInstance.InstanceId); + await this.service.SendEntityOrchestrationStartAsync(queue, retry, session); + + InstanceStatus after = await this.service.TrackingStore.FetchInstanceStatusAsync(start.OrchestrationInstance.InstanceId); + Assert.AreEqual(retry.OrchestrationInstance.ExecutionId, after.State.OrchestrationInstance.ExecutionId); + Assert.AreEqual(OrchestrationStatus.Pending, after.State.OrchestrationStatus); + Assert.AreEqual(0, after.State.Generation); + } + + [DataTestMethod] + [DataRow(false)] + [DataRow(true)] + public async Task PendingRegistrationDoesNotOverwriteConcurrentCheckpoint(bool useInstanceTableEtag) + { + this.settings.UseInstanceTableEtag = useInstanceTableEtag; + OrchestrationSession session = this.CreateEntitySession(); + TaskMessage start = CreateStart(session); + this.pendingWritePolicy.BeforeWrite = async () => + { + string instanceId = start.OrchestrationInstance.InstanceId; + OrchestrationHistory history = await this.service.TrackingStore.GetHistoryEventsAsync(instanceId, null); + var runtimeState = new OrchestrationRuntimeState(); + runtimeState.AddEvent(new OrchestratorStartedEvent(-1)); + runtimeState.AddEvent(start.Event); + runtimeState.AddEvent(new OrchestratorCompletedEvent(-1)); + await this.service.TrackingStore.UpdateStateAsync( + runtimeState, new OrchestrationRuntimeState(), instanceId, start.OrchestrationInstance.ExecutionId, + new OrchestrationETags { HistoryETag = history.ETag }, history.TrackingStoreContext); + }; + + await this.service.SendEntityOrchestrationStartAsync(this.service.AllControlQueues.Single(), start, session); + + InstanceStatus after = await this.service.TrackingStore.FetchInstanceStatusAsync(start.OrchestrationInstance.InstanceId); + Assert.AreEqual(OrchestrationStatus.Running, after.State.OrchestrationStatus); + Assert.AreEqual(start.OrchestrationInstance.ExecutionId, after.State.OrchestrationInstance.ExecutionId); + } + + [DataTestMethod] + [DataRow(false)] + [DataRow(true)] + public async Task PendingRegistrationDoesNotOverwriteReplacementOrResurrectConcurrentPurge(bool purge) + { + OrchestrationSession session = this.CreateEntitySession(); + TaskMessage start = CreateStart(session); + await this.SeedInstanceAsync((ExecutionStartedEvent)CreateStart(session, start.OrchestrationInstance.InstanceId).Event, OrchestrationStatus.Completed); + TaskMessage newer = CreateStart(session, start.OrchestrationInstance.InstanceId); + ((ExecutionStartedEvent)newer.Event).Generation = 8; + this.pendingWritePolicy.BeforeWrite = async () => + { + if (purge) + { + await this.service.TrackingStore.PurgeInstanceHistoryAsync(start.OrchestrationInstance.InstanceId); + } + else + { + InstanceStatus previous = await this.service.TrackingStore.FetchInstanceStatusAsync(start.OrchestrationInstance.InstanceId); + Assert.IsTrue(await this.service.TrackingStore.SetNewExecutionAsync((ExecutionStartedEvent)newer.Event, previous.ETag, null)); + } + }; + + await this.service.SendEntityOrchestrationStartAsync(this.service.AllControlQueues.Single(), start, session); + + InstanceStatus after = await this.service.TrackingStore.FetchInstanceStatusAsync(start.OrchestrationInstance.InstanceId); + if (purge) + { + Assert.IsNull(after); + } + else + { + Assert.AreEqual(newer.OrchestrationInstance.ExecutionId, after.State.OrchestrationInstance.ExecutionId); + Assert.AreEqual(8, after.State.Generation); + } + } + + [DataTestMethod] + [DataRow(404, "TableNotFound")] + [DataRow(503, "ServerBusy")] + public async Task PendingRegistrationPropagatesOtherStorageFailures(int status, string errorCode) + { + OrchestrationSession session = this.CreateEntitySession(); + TaskMessage start = CreateStart(session); + await this.SeedInstanceAsync((ExecutionStartedEvent)CreateStart(session, start.OrchestrationInstance.InstanceId).Event, OrchestrationStatus.Completed); + this.pendingWritePolicy.BeforeWrite = () => throw new RequestFailedException(status, "Injected storage failure", errorCode, null); + + DurableTaskStorageException error = await Assert.ThrowsExceptionAsync( + () => this.service.SendEntityOrchestrationStartAsync(this.service.AllControlQueues.Single(), start, session)); + Assert.AreEqual(status, error.HttpStatusCode); + Assert.AreEqual(errorCode, error.ErrorCode); + } + + [DataTestMethod] + [DataRow("@legacy@test", null)] + [DataRow("awaited-parent", 0)] + public async Task LegacyEntityAndAwaitedSubOrchestrationStartsDoNotRequirePendingRegistration(string parentInstanceId, int? generation) + { + OrchestrationSession session = this.CreateEntitySession(); + TaskMessage start = CreateStart(session); + var startEvent = (ExecutionStartedEvent)start.Event; + startEvent.ParentInstance.OrchestrationInstance = new OrchestrationInstance { InstanceId = parentInstanceId }; + startEvent.Generation = generation; + ControlQueue queue = this.service.AllControlQueues.Single(); + await queue.AddMessageAsync(start, session); + using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(10)); + MessageData message = (await queue.GetMessagesAsync(timeout.Token)).Single(); + + Assert.AreEqual(1, (await this.ReconcileStartAsync(message)).Count()); + } + + async Task SeedInstanceAsync(ExecutionStartedEvent start, OrchestrationStatus status) + { + start.Generation = 7; + Assert.IsTrue(await this.service.TrackingStore.SetNewExecutionAsync(start, null, null)); + var table = new TableServiceClient(TestHelpers.GetTestStorageAccountConnectionString()).GetTableClient(this.settings.InstanceTableName); + await table.UpdateEntityAsync( + new TableEntity(start.OrchestrationInstance.InstanceId, string.Empty) { ["RuntimeStatus"] = status.ToString() }, + ETag.All, TableUpdateMode.Merge); + } + + async Task<(OrchestrationRuntimeState OldRuntime, TaskMessage PendingStart, InstanceStatus Pending)> CreateReusedPendingAsync( + OrchestrationStatus oldStatus = OrchestrationStatus.Completed) + { + OrchestrationSession session = this.CreateEntitySession(); + TaskMessage previousStart = CreateStart(session); + ((ExecutionStartedEvent)previousStart.Event).Generation = 7; + string instanceId = previousStart.OrchestrationInstance.InstanceId; + OrchestrationHistory history = await this.service.TrackingStore.GetHistoryEventsAsync(instanceId, null); + var oldRuntime = new OrchestrationRuntimeState(); + oldRuntime.AddEvent(new OrchestratorStartedEvent(-1)); + oldRuntime.AddEvent(previousStart.Event); + oldRuntime.AddEvent(new ExecutionCompletedEvent(0, "\"previous output\"", oldStatus)); + oldRuntime.AddEvent(new OrchestratorCompletedEvent(-1)); + await this.service.TrackingStore.UpdateStateAsync( + oldRuntime, new OrchestrationRuntimeState(), instanceId, previousStart.OrchestrationInstance.ExecutionId, + new OrchestrationETags { HistoryETag = history.ETag }, history.TrackingStoreContext); + TaskMessage start = CreateStart(session, instanceId); + await this.service.SendEntityOrchestrationStartAsync(this.service.AllControlQueues.Single(), start, session); + InstanceStatus pending = await this.service.TrackingStore.FetchInstanceStatusAsync(instanceId); + Assert.AreEqual(OrchestrationStatus.Pending, pending.State.OrchestrationStatus); + return (oldRuntime, start, pending); + } + + static TaskMessage CreateTermination(string instanceId, string reason, string executionId = null) + { + return new TaskMessage + { + OrchestrationInstance = new OrchestrationInstance { InstanceId = instanceId, ExecutionId = executionId }, + Event = new ExecutionTerminatedEvent(-1, reason), + }; + } + + void CreateEntityWorker(Func createStart) + { + this.worker = new TaskHubWorker(this.service); + this.worker.AddTaskOrchestrations(typeof(ChildOrchestration)); + this.worker.AddEntityDispatcherMiddleware((context, next) => + { + var request = context.GetProperty(); + context.SetProperty(new EntityBatchResult + { + EntityState = "\"started\"", + Results = request.Operations.Select(_ => new OperationResult()).ToList(), + Actions = new List { createStart() }, + }); + return Task.CompletedTask; + }); + } + + OrchestrationSession CreateEntitySession() + { + var instance = new OrchestrationInstance { InstanceId = "@pendingstarter@test", ExecutionId = Guid.NewGuid().ToString("N") }; + var runtimeState = new OrchestrationRuntimeState(new HistoryEvent[] + { + new OrchestratorStartedEvent(-1), + new ExecutionStartedEvent(-1, "\"entity-state\"") { OrchestrationInstance = instance, Name = "pendingstarter" }, + new OrchestratorCompletedEvent(-1), + }); + return new OrchestrationSession( + this.settings, "test-account", instance, this.service.AllControlQueues.Single(), + new List(), runtimeState, new OrchestrationETags(), DateTime.MinValue, + null, TimeSpan.Zero, CancellationToken.None, Guid.NewGuid()); + } + + static TaskMessage CreateStart(OrchestrationSession session, string instanceId = null) + { + var instance = new OrchestrationInstance { InstanceId = instanceId ?? Guid.NewGuid().ToString("N"), ExecutionId = Guid.NewGuid().ToString("N") }; + return new TaskMessage + { + OrchestrationInstance = instance, + Event = new ExecutionStartedEvent(-1, "\"child-input\"") + { + Name = typeof(ChildOrchestration).FullName, + Version = string.Empty, + OrchestrationInstance = instance, + ParentInstance = new ParentInstance { OrchestrationInstance = session.Instance, TaskScheduleId = 0 }, + }, + }; + } + + Task> ReconcileStartAsync(MessageData message) + { + var manager = (OrchestrationSessionManager)typeof(AzureStorageOrchestrationService) + .GetField("orchestrationSessionManager", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(this.service); + MethodInfo reconcile = typeof(OrchestrationSessionManager) + .GetMethod("DedupeExecutionStartedMessagesAsync", BindingFlags.Instance | BindingFlags.NonPublic); + return (Task>)reconcile.Invoke(manager, new object[] + { + this.service.AllControlQueues.Single(), new[] { message }, Guid.NewGuid(), CancellationToken.None, + }); + } + + Task EvaluateBatchAsync( + OrchestrationRuntimeState runtimeState, + TaskMessage[] messages, + CancellationToken cancellationToken = default) + { + MethodInfo evaluate = typeof(AzureStorageOrchestrationService) + .GetMethod("IsExecutableInstanceAsync", BindingFlags.Instance | BindingFlags.NonPublic); + Assert.IsNotNull(evaluate); + return (Task)evaluate.Invoke(this.service, new object[] { runtimeState, messages, false, cancellationToken }); + } + + sealed class PendingWritePolicy : HttpPipelinePolicy + { + readonly string tableName; + + public PendingWritePolicy(string tableName) => this.tableName = tableName; + + public Func BeforeWrite { get; set; } + + public int InstanceReads { get; private set; } + + public CancellationToken LastWriteCancellationToken { get; private set; } + + public override void Process(HttpMessage message, ReadOnlyMemory pipeline) + { + this.BeforeWriteAsync(message).GetAwaiter().GetResult(); + ProcessNext(message, pipeline); + } + + public override async ValueTask ProcessAsync(HttpMessage message, ReadOnlyMemory pipeline) + { + await this.BeforeWriteAsync(message); + await ProcessNextAsync(message, pipeline); + } + + Task BeforeWriteAsync(HttpMessage message) + { + if (message.Request.Uri.ToUri().AbsolutePath.IndexOf(this.tableName, StringComparison.Ordinal) < 0) + { + return Task.CompletedTask; + } + + if (message.Request.Method == RequestMethod.Get) + { + this.InstanceReads++; + } + else if (message.Request.Method == RequestMethod.Post || + message.Request.Method == RequestMethod.Put || + message.Request.Method == RequestMethod.Patch || + message.Request.Method.ToString() == "MERGE") + { + this.LastWriteCancellationToken = message.CancellationToken; + if (this.BeforeWrite != null) + { + Func beforeWrite = this.BeforeWrite; + this.BeforeWrite = null; + return beforeWrite(); + } + } + + return Task.CompletedTask; + } + } + + public class ChildOrchestration : TaskOrchestration + { + public override Task RunTask(OrchestrationContext context, string input) + { + if (input == "continue") + { + context.ContinueAsNew("done"); + } + + return Task.FromResult(input); + } + } + } +}