Soru

Zorluk: ZorImplement Durable Functions

You are developing a C# (.NET Isolated process) Durable Function orchestrator named InventoryAuditOrchestrator to coordinate a nightly inventory synchronization workflow. The orchestrator must retrieve a list of store locations, generate a unique audit tracking identifier (GUID) for each location, call an activity function to perform the audit, and record the completion timestamp.

You write the following orchestrator function code:

csharp
[Function("InventoryAuditOrchestrator")]
public static async Task RunOrchestrator(
[OrchestrationTrigger] TaskOrchestrationContext context)
{
var stores = await context.CallActivityAsync<List<string>>("GetStoreLocations", null);
foreach (var store in stores)
{
Guid auditId = Guid.NewGuid();
DateTime auditTime = DateTime.UtcNow;

var auditData = new AuditPayload(store, auditId, auditTime);
await context.CallActivityAsync("RunStoreAudit", auditData);
}
}

During testing, you notice that the workflow fails during execution replay, resulting in mismatched audit identifiers and timestamps across replays.

Which modification must you apply to the orchestrator code to resolve the replay errors and guarantee deterministic execution?

  1. Replace the Guid.NewGuid() call with context.NewGuid() and replace the DateTime.UtcNow call with context.CurrentUtcDateTime.Cevap
  2. B
    Configure the function app to use an Azure Functions Premium hosting plan to support multi-threaded and non-deterministic operations.
  3. C
    Run the Guid.NewGuid() and DateTime.UtcNow calls inside a Task.Run() block to offload them to a separate thread pool thread.
  4. D
    Decorate the orchestrator function with the [NoReplay] attribute to disable state replay and allow direct system calls.

Cevap

Replace the Guid.NewGuid() call with context.NewGuid() and replace the DateTime.UtcNow call with context.CurrentUtcDateTime.
The correct answer is to replace Guid.NewGuid() with context.NewGuid() and replace DateTime.UtcNow with context.CurrentUtcDateTime. In Azure Durable Functions, the orchestrator function must be deterministic because its state is rebuilt by replaying execution logs. Standard system calls for generating GUIDs or fetching the current time return different values upon each invocation, which causes the orchestration engine to detect a mismatch between the current execution and the recorded history. By using the methods provided on TaskOrchestrationContext, the framework records the generated values in the history database on the first run and replays them consistently on subsequent runs.

Adım Adım Çözüm

1
Analyze the orchestrator's code for non-deterministic behavior.
Identify Guid.NewGuid() and DateTime.UtcNow as non-deterministic API calls.
Durable orchestrator functions must be deterministic because they replay the execution history to rebuild state. Standard system calls like Guid.NewGuid() and DateTime.UtcNow yield different values on every execution, violating this constraint.
2
Evaluate the TaskOrchestrationContext API capabilities for deterministic alternatives.
Identify context.NewGuid() and context.CurrentUtcDateTime.
The Durable Functions SDK provides built-in API replacements for GUID generation and time retrieval that coordinate with the execution history to return the same values during replays.
3
Substitute the non-deterministic system calls with their deterministic SDK equivalents.
The code generates identical tracking identifiers and timestamps during orchestrator replays, preventing execution mismatch errors.
This maintains the integrity of the replay state machine while satisfying the workflow logic.

Anahtar Kavram

Orchestrator determinism requirements and C# isolated worker SDK APIs
Tahmini Süre:2m 0s
Bu soruyu puanla