首页 > 解决方案 > Azure Durable Functions:我是否正确使用了`!orchestrationContext.IsReplaying`?

问题描述

在微软的扇出示例中看到,无需担心“防御”重播

但我绝对需要if (!orchestrationContext.IsReplaying)这里:

[FunctionName(FUNC_NAME_ORCH)]
public static async Task<string> RunPlayerYouTubeOrchestration(
    [OrchestrationTrigger] DurableOrchestrationContext orchestrationContext,
    ILogger log)
{
    log?.LogInformation($"{FUNC_NAME_ORCH}: {nameof(RunPlayerYouTubeOrchestration)} invoked...");
    log?.LogInformation($"{FUNC_NAME_ORCH}: {nameof(DurableOrchestrationContextBase.InstanceId)} {orchestrationContex

    log?.LogInformation($"{FUNC_NAME_ORCH}: calling {FUNC_NAME_ORCH_FUNC0}.c ..");
    var jsonBlobs = await orchestrationContext
        .CallActivityAsync<IEnumerable<string>>(FUNC_NAME_ORCH_FUNC0, null);

    if ((jsonBlobs == null) || !jsonBlobs.Any())
    {
        var errorMessage = $"{FUNC_NAME_ORCH}: The expected JSON blobs from `{FUNC_NAME_ORCH_FUNC0}` are not here.";
        log?.LogError(errorMessage);
        throw new NullReferenceException(errorMessage);
    }

    if (!orchestrationContext.IsReplaying)
    {
        var tasks = jsonBlobs.Select(json =>
            {
                log?.LogInformation($"{FUNC_NAME_ORCH}: calling {FUNC_NAME_ORCH_FUNC1}...");
                return orchestrationContext.CallActivityAsync(FUNC_NAME_ORCH_FUNC1, json);
            });

        log?.LogInformation($"{FUNC_NAME_ORCH}: fan-out starting...");
        await Task.WhenAll(tasks);
    }

    log?.LogInformation($"{FUNC_NAME_ORCH}: fan-out complete.");

    log?.LogInformation($"{FUNC_NAME_ORCH}: calling {FUNC_NAME_ORCH_FUNC2}...");
    await orchestrationContext.CallActivityAsync(FUNC_NAME_ORCH_FUNC2, null);

    return orchestrationContext.InstanceId;
}

没有这if道门,FUNC_NAME_ORCH_FUNC1将被无休止地呼唤

我在这里做错了什么?是否有一个“正确”的时间来使用!orchestrationContext.IsReplaying,或者使用低于确定性的代码是一种黑客行为?

标签: taskazure-functionsdeterministicnon-deterministicazure-durable-functions

解决方案


我认为您的代码在没有IsReplaying. 该CallActivityAsync()方法将被多次调用,但对于给定的输入,它只会执行一次活动函数。在第一次执行之后,结果会在表存储中设置检查点,并且对该活动的连续调用将从表中返回结果,并且不会再次执行活动函数。

有关检查点和重放的更多信息:https ://docs.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-orchesstrations#reliability

我建议您将 FUNC1 的日志记录语句移到FUNC1 活动函数中这样,您只记录实际的活动函数执行,而不是记录该CallActivityAsync方法被调用的次数。


推荐阅读