Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DurableTaskGrpcClient.getInstanceMetadata does not return null when no instance found. #77

Closed
YusukeTobo opened this issue Jul 28, 2022 · 0 comments · Fixed by #79
Closed

Comments

@YusukeTobo
Copy link

YusukeTobo commented Jul 28, 2022

Regarding the API, getInstanceMetadata should return null when there are no instances found.

public abstract OrchestrationMetadata getInstanceMetadata(String instanceId, boolean getInputsAndOutputs);

     * @return a metadata record that describes the orchestration instance and its execution status, or
     *         <code>null</code> if no such instance is found.
     */
    @Nullable
    public abstract OrchestrationMetadata getInstanceMetadata(String instanceId, boolean getInputsAndOutputs);

However, In that case, I found that DurableTaskGrpcClient.getInstanceMetadata returns DEFAULT_INSTANCE with orchestrationState_=0 (ORCHESTRATION_STATUS_RUNNING).

// Protobuf code.

    /**
     * <code>.OrchestrationState orchestrationState = 2;</code>
     * @return The orchestrationState.
     */
    @java.lang.Override
    public com.microsoft.durabletask.implementation.protobuf.OrchestratorService.OrchestrationState getOrchestrationState() {
      return orchestrationState_ == null ? com.microsoft.durabletask.implementation.protobuf.OrchestratorService.OrchestrationState.getDefaultInstance() : orchestrationState_;
    }
 
 
    // @@protoc_insertion_point(class_scope:OrchestrationState)
    private static final com.microsoft.durabletask.implementation.protobuf.OrchestratorService.OrchestrationState DEFAULT_INSTANCE;
    static {
      DEFAULT_INSTANCE = new com.microsoft.durabletask.implementation.protobuf.OrchestratorService.OrchestrationState();
    }
    public static com.microsoft.durabletask.implementation.protobuf.OrchestratorService.OrchestrationState getDefaultInstance() {
      return DEFAULT_INSTANCE;
    }

This issue prevents us from invoking client.scheduleNewOrchestrationInstance in Singleton pattern described the official document here. That also means the library users cannot tell the accurate state of instance.

https://docs.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-singletons?tabs=java#singleton-example

@FunctionName("HttpStartSingle")
public HttpResponseMessage runSingle(
        @HttpTrigger(name = "req") HttpRequestMessage<?> req,
        @DurableClientInput(name = "durableContext") DurableClientContext durableContext) {

    String instanceID = "StaticID";
    DurableTaskClient client = durableContext.getClient();

    // Check to see if an instance with this ID is already running
    OrchestrationMetadata metadata = client.getInstanceMetadata(instanceID, false);
    if (metadata.isRunning()) {
        return req.createResponseBuilder(HttpStatus.CONFLICT)
                .body("An instance with ID '" + instanceID + "' already exists.")
                .build();
    }

    // No such instance exists - create a new one. De-dupe is handled automatically
    // in the storage layer if another function tries to also use this instance ID.
    client.scheduleNewOrchestrationInstance("MyOrchestration", null, instanceID);
    return durableContext.createCheckStatusResponse(req, instanceID);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants