Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions src/DurableTask.AzureStorage/OrchestrationSessionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ public OrchestrationSessionManager(

internal IEnumerable<ControlQueue> Queues => this.ownedControlQueues.Values;

// The time to wait before a failed orchestration state prefetch is tried again. Tests can set a shorter time.
internal TimeSpan PrefetchRetryDelay { get; set; } = TimeSpan.FromSeconds(5);

public void AddQueue(string partitionId, ControlQueue controlQueue, CancellationToken cancellationToken)
{
this.shutdownToken = cancellationToken;
Expand Down Expand Up @@ -538,17 +541,19 @@ async Task ScheduleOrchestrationStatePrefetch(
batch.ETags.HistoryETag = history.ETag;
batch.LastCheckpointTime = history.LastCheckpointTime;
batch.TrackingStoreContext = history.TrackingStoreContext;
}

// Try to get the instance ETag from the tracking store if it wasn't already provided
if (this.settings.UseInstanceTableEtag && batch.ETags.InstanceETag == null)
{
InstanceStatus? instanceStatus = await this.trackingStore.FetchInstanceStatusAsync(
batch.OrchestrationInstanceId,
cancellationToken);
// The instance could not exist in the case that these messages are for the first execution of a suborchestration,
// or an entity-started orchestration, for example
batch.ETags.InstanceETag = instanceStatus?.ETag;
}
// Try to get the instance ETag from the tracking store if it wasn't already provided.
// This is not in the history check above, so that a retry loads the instance ETag again
// when an earlier attempt loaded the history but failed to load the instance ETag.
if (this.settings.UseInstanceTableEtag && batch.ETags.InstanceETag == null)
{
InstanceStatus? instanceStatus = await this.trackingStore.FetchInstanceStatusAsync(
batch.OrchestrationInstanceId,
cancellationToken);
// The instance could not exist in the case that these messages are for the first execution of a suborchestration,
// or an entity-started orchestration, for example
batch.ETags.InstanceETag = instanceStatus?.ETag;
}

if (this.settings.UseSeparateQueueForEntityWorkItems
Expand All @@ -575,11 +580,15 @@ async Task ScheduleOrchestrationStatePrefetch(
e.ToString());

// Sleep briefly to avoid a tight failure loop.
await Task.Delay(TimeSpan.FromSeconds(5));
// If the token is cancelled during the delay (shutdown), this background task stops here.
await Task.Delay(this.PrefetchRetryDelay, cancellationToken);

// This is a background operation so failure is not an option. All exceptions must be handled.
// To avoid starvation, we need to re-enqueue this async operation instead of retrying in a loop.
await Task.Run(() => this.ScheduleOrchestrationStatePrefetch(node, traceActivityId, cancellationToken));
// Do not await the new operation. This method keeps the caught exception until the method completes.
// If it waits for the next attempt, each failed attempt stays in memory until an attempt is successful.
// If the failure is permanent, memory then increases without a limit.
_ = Task.Run(() => this.ScheduleOrchestrationStatePrefetch(node, traceActivityId, cancellationToken));
}
}

Expand Down
Loading
Loading