Skip to content

Commit

Permalink
Fix consistent query interface which caused overloading ambiguity wit…
Browse files Browse the repository at this point in the history
…h variable argument (#644)
  • Loading branch information
longquanzheng authored Oct 4, 2021
1 parent c9ec678 commit f760fb5
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 24 deletions.
99 changes: 99 additions & 0 deletions src/main/java/com/uber/cadence/client/QueryOptions.java
Original file line number Diff line number Diff line change
@@ -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
+ '}';
}
}
20 changes: 15 additions & 5 deletions src/main/java/com/uber/cadence/client/WorkflowStub.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -156,27 +155,38 @@ <R> CompletableFuture<R> getResultAsync(
*/
<R> R query(String queryType, Class<R> resultClass, Object... args);

/**
* Deprecated: please use {@link #queryWithOptions(String, QueryOptions, Type, Class, Object...)}
* to avoid variable argument ambiguity with Object... args
*/
<R> R query(String queryType, Class<R> resultClass, Type resultType, Object... args);

/**
* Deprecated: please use {@link #queryWithOptions(String, QueryOptions, Type, Class, Object...)}
* to avoid variable argument ambiguity with Object... args
*/
<R> R query(
String queryType,
Class<R> resultClass,
QueryRejectCondition queryRejectCondition,
Object... args);

/**
* Deprecated: please use {@link #queryWithOptions(String, QueryOptions, Type, Class, Object...)}
* to avoid variable argument ambiguity with Object... args
*/
<R> R query(
String queryType,
Class<R> resultClass,
Type resultType,
QueryRejectCondition queryRejectCondition,
Object... args);

<R> R query(
<R> R queryWithOptions(
String queryType,
Class<R> resultClass,
QueryOptions options,
Type resultType,
QueryRejectCondition queryRejectCondition,
QueryConsistencyLevel queryConsistencyLevel,
Class<R> resultClass,
Object... args);

/** Request cancellation. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -1010,15 +1010,13 @@ public <R> R query(
}

@Override
public <R> R query(
public <R> R queryWithOptions(
String queryType,
Class<R> resultClass,
QueryOptions options,
Type resultType,
QueryRejectCondition queryRejectCondition,
QueryConsistencyLevel queryConsistencyLevel,
Class<R> resultClass,
Object... args) {
return next.query(
queryType, resultClass, resultType, queryRejectCondition, queryConsistencyLevel, args);
return next.queryWithOptions(queryType, options, resultType, resultClass, args);
}

@Override
Expand Down
28 changes: 16 additions & 12 deletions src/main/java/com/uber/cadence/internal/sync/WorkflowStubImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,8 @@ private <R> R mapToWorkflowFailureException(

@Override
public <R> R query(String queryType, Class<R> resultClass, Object... args) {
return query(queryType, resultClass, resultClass, args);
return queryWithOptions(
queryType, new QueryOptions.Builder().build(), resultClass, resultClass, args);
}

@Override
Expand All @@ -431,30 +432,31 @@ public <R> 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> R query(
public <R> R queryWithOptions(
String queryType,
Class<R> resultClass,
QueryOptions options,
Type resultType,
QueryRejectCondition queryRejectCondition,
QueryConsistencyLevel queryConsistencyLevel,
Class<R> 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 {
Expand All @@ -477,7 +479,9 @@ public <R> 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());
}
}

Expand Down

0 comments on commit f760fb5

Please sign in to comment.