From f760fb5c10fbeb287832bf7a931289269519ec78 Mon Sep 17 00:00:00 2001 From: Quanzheng Long Date: Mon, 4 Oct 2021 08:20:32 -0700 Subject: [PATCH] Fix consistent query interface which caused overloading ambiguity with variable argument (#644) --- .../com/uber/cadence/client/QueryOptions.java | 99 +++++++++++++++++++ .../com/uber/cadence/client/WorkflowStub.java | 20 +++- .../sync/TestWorkflowEnvironmentInternal.java | 12 +-- .../internal/sync/WorkflowStubImpl.java | 28 +++--- 4 files changed, 135 insertions(+), 24 deletions(-) create mode 100644 src/main/java/com/uber/cadence/client/QueryOptions.java diff --git a/src/main/java/com/uber/cadence/client/QueryOptions.java b/src/main/java/com/uber/cadence/client/QueryOptions.java new file mode 100644 index 000000000..10076f7e1 --- /dev/null +++ b/src/main/java/com/uber/cadence/client/QueryOptions.java @@ -0,0 +1,99 @@ +/* + * Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Modifications copyright (C) 2017 Uber Technologies, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"). You may not + * use this file except in compliance with the License. A copy of the License is + * located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file 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. + */ + +package com.uber.cadence.client; + +import com.uber.cadence.QueryConsistencyLevel; +import com.uber.cadence.QueryRejectCondition; +import java.util.Objects; + +public final class QueryOptions { + + public static final class Builder { + + private QueryRejectCondition queryRejectCondition = null; // default to empty condition + private QueryConsistencyLevel queryConsistencyLevel = + QueryConsistencyLevel.EVENTUAL; // default to eventual consistent query + + public Builder() {} + + public Builder(QueryOptions o) { + if (o == null) { + return; + } + this.queryConsistencyLevel = o.queryConsistencyLevel; + this.queryRejectCondition = o.queryRejectCondition; + } + + /** queryRejectCondition to decide condition to reject the query */ + public Builder setQueryRejectCondition(QueryRejectCondition queryRejectCondition) { + this.queryRejectCondition = queryRejectCondition; + return this; + } + + public Builder setQueryConsistencyLevel(QueryConsistencyLevel queryConsistencyLevel) { + this.queryConsistencyLevel = queryConsistencyLevel; + return this; + } + + public QueryOptions build() { + return new QueryOptions(queryRejectCondition, queryConsistencyLevel); + } + } + + private QueryRejectCondition queryRejectCondition; + private QueryConsistencyLevel queryConsistencyLevel; + + private QueryOptions( + QueryRejectCondition queryRejectCondition, QueryConsistencyLevel queryConsistencyLevel) { + this.queryConsistencyLevel = queryConsistencyLevel; + this.queryRejectCondition = queryRejectCondition; + } + + public QueryRejectCondition getQueryRejectCondition() { + return queryRejectCondition; + } + + public QueryConsistencyLevel getQueryConsistencyLevel() { + return queryConsistencyLevel; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + QueryOptions that = (QueryOptions) o; + return Objects.equals(queryRejectCondition, that.queryRejectCondition) + && queryConsistencyLevel == that.queryConsistencyLevel; + } + + @Override + public int hashCode() { + return Objects.hash(queryRejectCondition, queryConsistencyLevel); + } + + @Override + public String toString() { + return "QueryOptions{" + + "queryRejectCondition='" + + queryRejectCondition + + '\'' + + ", queryConsistencyLevel=" + + queryConsistencyLevel + + '}'; + } +} diff --git a/src/main/java/com/uber/cadence/client/WorkflowStub.java b/src/main/java/com/uber/cadence/client/WorkflowStub.java index 44933e714..d58c57424 100644 --- a/src/main/java/com/uber/cadence/client/WorkflowStub.java +++ b/src/main/java/com/uber/cadence/client/WorkflowStub.java @@ -17,7 +17,6 @@ package com.uber.cadence.client; -import com.uber.cadence.QueryConsistencyLevel; import com.uber.cadence.QueryRejectCondition; import com.uber.cadence.WorkflowExecution; import java.lang.reflect.InvocationHandler; @@ -156,14 +155,26 @@ CompletableFuture getResultAsync( */ R query(String queryType, Class resultClass, Object... args); + /** + * Deprecated: please use {@link #queryWithOptions(String, QueryOptions, Type, Class, Object...)} + * to avoid variable argument ambiguity with Object... args + */ R query(String queryType, Class resultClass, Type resultType, Object... args); + /** + * Deprecated: please use {@link #queryWithOptions(String, QueryOptions, Type, Class, Object...)} + * to avoid variable argument ambiguity with Object... args + */ R query( String queryType, Class resultClass, QueryRejectCondition queryRejectCondition, Object... args); + /** + * Deprecated: please use {@link #queryWithOptions(String, QueryOptions, Type, Class, Object...)} + * to avoid variable argument ambiguity with Object... args + */ R query( String queryType, Class resultClass, @@ -171,12 +182,11 @@ R query( QueryRejectCondition queryRejectCondition, Object... args); - R query( + R queryWithOptions( String queryType, - Class resultClass, + QueryOptions options, Type resultType, - QueryRejectCondition queryRejectCondition, - QueryConsistencyLevel queryConsistencyLevel, + Class resultClass, Object... args); /** Request cancellation. */ diff --git a/src/main/java/com/uber/cadence/internal/sync/TestWorkflowEnvironmentInternal.java b/src/main/java/com/uber/cadence/internal/sync/TestWorkflowEnvironmentInternal.java index 49598ca82..68c969a10 100644 --- a/src/main/java/com/uber/cadence/internal/sync/TestWorkflowEnvironmentInternal.java +++ b/src/main/java/com/uber/cadence/internal/sync/TestWorkflowEnvironmentInternal.java @@ -54,7 +54,6 @@ import com.uber.cadence.PollForActivityTaskResponse; import com.uber.cadence.PollForDecisionTaskRequest; import com.uber.cadence.PollForDecisionTaskResponse; -import com.uber.cadence.QueryConsistencyLevel; import com.uber.cadence.QueryFailedError; import com.uber.cadence.QueryRejectCondition; import com.uber.cadence.QueryWorkflowRequest; @@ -90,6 +89,7 @@ import com.uber.cadence.WorkflowExecutionAlreadyCompletedError; import com.uber.cadence.WorkflowExecutionAlreadyStartedError; import com.uber.cadence.client.ActivityCompletionClient; +import com.uber.cadence.client.QueryOptions; import com.uber.cadence.client.WorkflowClient; import com.uber.cadence.client.WorkflowClientInterceptor; import com.uber.cadence.client.WorkflowClientOptions; @@ -1010,15 +1010,13 @@ public R query( } @Override - public R query( + public R queryWithOptions( String queryType, - Class resultClass, + QueryOptions options, Type resultType, - QueryRejectCondition queryRejectCondition, - QueryConsistencyLevel queryConsistencyLevel, + Class resultClass, Object... args) { - return next.query( - queryType, resultClass, resultType, queryRejectCondition, queryConsistencyLevel, args); + return next.queryWithOptions(queryType, options, resultType, resultClass, args); } @Override diff --git a/src/main/java/com/uber/cadence/internal/sync/WorkflowStubImpl.java b/src/main/java/com/uber/cadence/internal/sync/WorkflowStubImpl.java index 5a9417a15..47e4ff720 100644 --- a/src/main/java/com/uber/cadence/internal/sync/WorkflowStubImpl.java +++ b/src/main/java/com/uber/cadence/internal/sync/WorkflowStubImpl.java @@ -407,7 +407,8 @@ private R mapToWorkflowFailureException( @Override public R query(String queryType, Class resultClass, Object... args) { - return query(queryType, resultClass, resultClass, args); + return queryWithOptions( + queryType, new QueryOptions.Builder().build(), resultClass, resultClass, args); } @Override @@ -431,30 +432,31 @@ public R query( Type resultType, QueryRejectCondition queryRejectCondition, Object... args) { - return query( + return queryWithOptions( queryType, - resultClass, + new QueryOptions.Builder() + .setQueryRejectCondition(queryRejectCondition) + .setQueryConsistencyLevel(QueryConsistencyLevel.EVENTUAL) + .build(), resultType, - queryRejectCondition, - QueryConsistencyLevel.EVENTUAL, + resultClass, args); } @Override - public R query( + public R queryWithOptions( String queryType, - Class resultClass, + QueryOptions options, Type resultType, - QueryRejectCondition queryRejectCondition, - QueryConsistencyLevel queryConsistencyLevel, + Class resultClass, Object... args) { checkStarted(); QueryWorkflowParameters p = new QueryWorkflowParameters(); p.setInput(dataConverter.toData(args)); p.setQueryType(queryType); p.setWorkflowId(execution.get().getWorkflowId()); - p.setQueryRejectCondition(queryRejectCondition); - p.setQueryConsistencyLevel(queryConsistencyLevel); + p.setQueryRejectCondition(options.getQueryRejectCondition()); + p.setQueryConsistencyLevel(options.getQueryConsistencyLevel()); QueryWorkflowResponse result; try { @@ -477,7 +479,9 @@ public R query( return dataConverter.fromData(result.getQueryResult(), resultClass, resultType); } else { throw new WorkflowQueryRejectedException( - execution.get(), queryRejectCondition, result.getQueryRejected().getCloseStatus()); + execution.get(), + options.getQueryRejectCondition(), + result.getQueryRejected().getCloseStatus()); } }