Skip to content

Commit 14e523a

Browse files
authored
Clean up workflow query handling (#582)
* Allow user to set QueryRejectCondition through WorkflowClientOptions * Introduce WorkflowQueryRejectedException and deprecate QueryResult
1 parent 024b14c commit 14e523a

12 files changed

+240
-129
lines changed

src/main/java/com/uber/cadence/client/WorkflowClientOptions.java

+77-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package com.uber.cadence.client;
1919

20+
import com.uber.cadence.QueryRejectCondition;
2021
import com.uber.cadence.context.ContextPropagator;
2122
import com.uber.cadence.converter.DataConverter;
2223
import com.uber.cadence.converter.JsonDataConverter;
@@ -60,6 +61,7 @@ public static final class Builder {
6061
private Scope metricsScope = NoopScope.getInstance();
6162
private String identity = ManagementFactory.getRuntimeMXBean().getName();;
6263
private List<ContextPropagator> contextPropagators = EMPTY_CONTEXT_PROPAGATORS;
64+
private QueryRejectCondition queryRejectCondition;
6365

6466
private Builder() {}
6567

@@ -69,6 +71,7 @@ private Builder(WorkflowClientOptions options) {
6971
interceptors = options.getInterceptors();
7072
metricsScope = options.getMetricsScope();
7173
identity = options.getIdentity();
74+
queryRejectCondition = options.getQueryRejectCondition();
7275
}
7376

7477
public Builder setDomain(String domain) {
@@ -123,33 +126,55 @@ public Builder setContextPropagators(List<ContextPropagator> contextPropagators)
123126
return this;
124127
}
125128

129+
/**
130+
* QueryRejectCondition is an optional field used to reject queries based on workflow state.
131+
* QueryRejectConditionNotOpen will reject queries to workflows that are not open.
132+
* QueryRejectConditionNotCompletedCleanly will reject queries to workflows that are not
133+
* completed successfully. (e.g. terminated, canceled timeout etc...)
134+
*
135+
* <p>Default is null, which means that queries will not be rejected based on workflow state.
136+
*/
137+
public Builder setQueryRejectCondition(QueryRejectCondition queryRejectCondition) {
138+
this.queryRejectCondition = queryRejectCondition;
139+
return this;
140+
}
141+
126142
public WorkflowClientOptions build() {
127143
metricsScope = metricsScope.tagged(ImmutableMap.of(MetricsTag.DOMAIN, domain));
128144
return new WorkflowClientOptions(
129-
domain, dataConverter, interceptors, metricsScope, identity, contextPropagators);
145+
domain,
146+
dataConverter,
147+
interceptors,
148+
metricsScope,
149+
identity,
150+
contextPropagators,
151+
queryRejectCondition);
130152
}
131153
}
132154

133155
private final String domain;
134156
private final DataConverter dataConverter;
135157
private final WorkflowClientInterceptor[] interceptors;
136158
private final Scope metricsScope;
137-
private String identity;
138-
private List<ContextPropagator> contextPropagators;
159+
private final String identity;
160+
private final List<ContextPropagator> contextPropagators;
161+
private final QueryRejectCondition queryRejectCondition;
139162

140163
private WorkflowClientOptions(
141164
String domain,
142165
DataConverter dataConverter,
143166
WorkflowClientInterceptor[] interceptors,
144167
Scope metricsScope,
145168
String identity,
146-
List<ContextPropagator> contextPropagators) {
169+
List<ContextPropagator> contextPropagators,
170+
QueryRejectCondition queryRejectCondition) {
147171
this.domain = domain;
148172
this.dataConverter = dataConverter;
149173
this.interceptors = interceptors;
150174
this.metricsScope = metricsScope;
151175
this.identity = identity;
152176
this.contextPropagators = contextPropagators;
177+
this.queryRejectCondition = queryRejectCondition;
153178
}
154179

155180
public String getDomain() {
@@ -175,4 +200,52 @@ public String getIdentity() {
175200
public List<ContextPropagator> getContextPropagators() {
176201
return contextPropagators;
177202
}
203+
204+
public QueryRejectCondition getQueryRejectCondition() {
205+
return queryRejectCondition;
206+
}
207+
208+
@Override
209+
public String toString() {
210+
return "WorkflowClientOptions{"
211+
+ "domain='"
212+
+ domain
213+
+ '\''
214+
+ ", dataConverter="
215+
+ dataConverter
216+
+ ", interceptors="
217+
+ Arrays.toString(interceptors)
218+
+ ", identity='"
219+
+ identity
220+
+ '\''
221+
+ ", contextPropagators="
222+
+ contextPropagators
223+
+ ", queryRejectCondition="
224+
+ queryRejectCondition
225+
+ '}';
226+
}
227+
228+
@Override
229+
public boolean equals(Object o) {
230+
if (this == o) return true;
231+
if (o == null || getClass() != o.getClass()) return false;
232+
WorkflowClientOptions that = (WorkflowClientOptions) o;
233+
return com.google.common.base.Objects.equal(domain, that.domain)
234+
&& com.google.common.base.Objects.equal(dataConverter, that.dataConverter)
235+
&& Arrays.equals(interceptors, that.interceptors)
236+
&& com.google.common.base.Objects.equal(identity, that.identity)
237+
&& com.google.common.base.Objects.equal(contextPropagators, that.contextPropagators)
238+
&& queryRejectCondition == that.queryRejectCondition;
239+
}
240+
241+
@Override
242+
public int hashCode() {
243+
return com.google.common.base.Objects.hashCode(
244+
domain,
245+
dataConverter,
246+
Arrays.hashCode(interceptors),
247+
identity,
248+
contextPropagators,
249+
queryRejectCondition);
250+
}
178251
}

src/main/java/com/uber/cadence/client/WorkflowQueryException.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import com.uber.cadence.WorkflowExecution;
2121
import java.util.Optional;
2222

23-
public final class WorkflowQueryException extends WorkflowException {
23+
public class WorkflowQueryException extends WorkflowException {
2424

2525
public WorkflowQueryException(WorkflowExecution execution, String message) {
2626
super(message, execution, Optional.empty(), null);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Modifications Copyright (c) 2017-2020 Uber Technologies Inc.
3+
* Portions of the Software are attributed to Copyright (c) 2020 Temporal Technologies Inc.
4+
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
7+
* use this file except in compliance with the License. A copy of the License is
8+
* located at
9+
*
10+
* http://aws.amazon.com/apache2.0
11+
*
12+
* or in the "license" file accompanying this file. This file is distributed on
13+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14+
* express or implied. See the License for the specific language governing
15+
* permissions and limitations under the License.
16+
*/
17+
18+
package com.uber.cadence.client;
19+
20+
import com.uber.cadence.QueryRejectCondition;
21+
import com.uber.cadence.WorkflowExecution;
22+
import com.uber.cadence.WorkflowExecutionCloseStatus;
23+
24+
public final class WorkflowQueryRejectedException extends WorkflowQueryException {
25+
26+
private final QueryRejectCondition queryRejectCondition;
27+
private final WorkflowExecutionCloseStatus workflowExecutionStatus;
28+
29+
public WorkflowQueryRejectedException(
30+
WorkflowExecution execution,
31+
QueryRejectCondition queryRejectCondition,
32+
WorkflowExecutionCloseStatus workflowExecutionStatus) {
33+
super(
34+
execution,
35+
"Query invoked with "
36+
+ queryRejectCondition
37+
+ " reject condition. The workflow execution status is "
38+
+ workflowExecutionStatus);
39+
this.queryRejectCondition = queryRejectCondition;
40+
this.workflowExecutionStatus = workflowExecutionStatus;
41+
}
42+
43+
public QueryRejectCondition getQueryRejectCondition() {
44+
return queryRejectCondition;
45+
}
46+
47+
public WorkflowExecutionCloseStatus getWorkflowExecutionStatus() {
48+
return workflowExecutionStatus;
49+
}
50+
}

src/main/java/com/uber/cadence/client/WorkflowStub.java

+16-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*
2+
* Modifications Copyright (c) 2017-2021 Uber Technologies Inc.
3+
* Portions of the Software are attributed to Copyright (c) 2020 Temporal Technologies Inc.
24
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
35
*
4-
* Modifications copyright (C) 2017 Uber Technologies, Inc.
5-
*
66
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
77
* use this file except in compliance with the License. A copy of the License is
88
* located at
@@ -19,7 +19,6 @@
1919

2020
import com.uber.cadence.QueryRejectCondition;
2121
import com.uber.cadence.WorkflowExecution;
22-
import com.uber.cadence.internal.common.QueryResponse;
2322
import java.lang.reflect.InvocationHandler;
2423
import java.lang.reflect.Proxy;
2524
import java.lang.reflect.Type;
@@ -142,17 +141,29 @@ <R> CompletableFuture<R> getResultAsync(
142141

143142
<R> CompletableFuture<R> getResultAsync(long timeout, TimeUnit unit, Class<R> resultClass);
144143

144+
/**
145+
* Query workflow by invoking its query handler. A query handler is a method annotated with {@link
146+
* com.uber.cadence.workflow.QueryMethod}.
147+
*
148+
* @see WorkflowClientOptions.Builder#setQueryRejectCondition(QueryRejectCondition)
149+
* @param queryType name of the query handler. Usually it is a method name.
150+
* @param resultClass class of the query result type
151+
* @param args optional query arguments
152+
* @param <R> type of the query result
153+
* @return query result
154+
* @throws WorkflowQueryException if query failed for any reason.
155+
*/
145156
<R> R query(String queryType, Class<R> resultClass, Object... args);
146157

147158
<R> R query(String queryType, Class<R> resultClass, Type resultType, Object... args);
148159

149-
<R> QueryResponse<R> query(
160+
<R> R query(
150161
String queryType,
151162
Class<R> resultClass,
152163
QueryRejectCondition queryRejectCondition,
153164
Object... args);
154165

155-
<R> QueryResponse<R> query(
166+
<R> R query(
156167
String queryType,
157168
Class<R> resultClass,
158169
Type resultType,

src/main/java/com/uber/cadence/internal/common/QueryResponse.java

-48
This file was deleted.

src/main/java/com/uber/cadence/internal/sync/TestWorkflowEnvironmentInternal.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@
8989
import com.uber.cadence.client.WorkflowClientOptions;
9090
import com.uber.cadence.client.WorkflowOptions;
9191
import com.uber.cadence.client.WorkflowStub;
92-
import com.uber.cadence.internal.common.QueryResponse;
9392
import com.uber.cadence.internal.testservice.TestWorkflowService;
9493
import com.uber.cadence.serviceclient.IWorkflowService;
9594
import com.uber.cadence.testing.TestEnvironmentOptions;
@@ -965,7 +964,7 @@ public <R> R query(String queryType, Class<R> resultClass, Type resultType, Obje
965964
}
966965

967966
@Override
968-
public <R> QueryResponse<R> query(
967+
public <R> R query(
969968
String queryType,
970969
Class<R> resultClass,
971970
QueryRejectCondition queryRejectCondition,
@@ -974,7 +973,7 @@ public <R> QueryResponse<R> query(
974973
}
975974

976975
@Override
977-
public <R> QueryResponse<R> query(
976+
public <R> R query(
978977
String queryType,
979978
Class<R> resultClass,
980979
Type resultType,

0 commit comments

Comments
 (0)