Skip to content

Commit 7b4b3d2

Browse files
mfateevLiang Mei
authored and
Liang Mei
committed
Added ActivityInterface. Removed timeout properties from ActivityMethod
1 parent 95bbaba commit 7b4b3d2

13 files changed

+249
-264
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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.activity;
19+
20+
import java.lang.annotation.ElementType;
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.RetentionPolicy;
23+
import java.lang.annotation.Target;
24+
25+
/**
26+
* Indicates that the interface is an activity interface. Only interfaces annotated with this
27+
* annotation can be used as parameters to {@link
28+
* com.uber.cadence.workflow.Workflow#newActivityStub(Class)} methods.
29+
*
30+
* <p>Each method of the interface annotated with <code>ActivityInterface</code> including inherited
31+
* from interfaces is a separate activity. By default the name of an activity type is "short
32+
* interface name"_"method name".
33+
*/
34+
@Retention(RetentionPolicy.RUNTIME)
35+
@Target(ElementType.TYPE)
36+
public @interface ActivityInterface {}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*
2+
* Modifications Copyright (c) 2017-2020 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
@@ -24,46 +24,12 @@
2424

2525
/**
2626
* Indicates that the method is an activity method. This annotation applies only to activity
27-
* interface methods. Not required. Use it to override default activity type name or other options.
28-
* When both {@link ActivityOptions} and {@link ActivityMethod} have non default value for some
29-
* parameter the {@link ActivityOptions} one takes precedence.
27+
* interface methods. Not required. Use it to override default activity type name.
3028
*/
3129
@Retention(RetentionPolicy.RUNTIME)
3230
@Target(ElementType.METHOD)
3331
public @interface ActivityMethod {
3432

3533
/** Name of the workflow type. Default is {short class name}::{method name} */
3634
String name() default "";
37-
38-
/**
39-
* Overall timeout workflow is willing to wait for activity to complete. It includes time in a
40-
* task list (use {@link #scheduleToStartTimeoutSeconds()} to limit it) plus activity execution
41-
* time (use {@link #startToCloseTimeoutSeconds()} to limit it). Either this option or both
42-
* schedule to start and start to close are required.
43-
*/
44-
int scheduleToCloseTimeoutSeconds() default 0;
45-
46-
/**
47-
* Time activity can stay in task list before it is picked up by a worker. If schedule to close is
48-
* not provided then both this and start to close are required.
49-
*/
50-
int scheduleToStartTimeoutSeconds() default 0;
51-
52-
/**
53-
* Maximum activity execution time after it was sent to a worker. If schedule to close is not
54-
* provided then both this and schedule to start are required.
55-
*/
56-
int startToCloseTimeoutSeconds() default 0;
57-
58-
/**
59-
* Heartbeat interval. Activity must heartbeat before this interval passes after a last heartbeat
60-
* or activity start.
61-
*/
62-
int heartbeatTimeoutSeconds() default 0;
63-
64-
/**
65-
* Task list to use when dispatching activity task to a worker. By default it is the same task
66-
* list name the workflow was started with.
67-
*/
68-
String taskList() default "";
6935
}

src/main/java/com/uber/cadence/activity/ActivityOptions.java

+26-43
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*
2+
* Modifications Copyright (c) 2017-2020 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
@@ -29,36 +29,22 @@
2929
/** Options used to configure how an activity is invoked. */
3030
public final class ActivityOptions {
3131

32-
/**
33-
* Used to merge annotation and options. Options takes precedence. Returns options with all
34-
* defaults filled in.
35-
*/
36-
public static ActivityOptions merge(ActivityMethod a, MethodRetry r, ActivityOptions o) {
37-
if (a == null) {
38-
if (r == null) {
39-
return new ActivityOptions.Builder(o).validateAndBuildWithDefaults();
40-
}
41-
RetryOptions mergedR = RetryOptions.merge(r, o.getRetryOptions());
42-
return new ActivityOptions.Builder().setRetryOptions(mergedR).validateAndBuildWithDefaults();
43-
}
44-
if (o == null) {
45-
o = new ActivityOptions.Builder().build();
46-
}
47-
return new ActivityOptions.Builder()
48-
.setScheduleToCloseTimeout(
49-
mergeDuration(a.scheduleToCloseTimeoutSeconds(), o.getScheduleToCloseTimeout()))
50-
.setScheduleToStartTimeout(
51-
mergeDuration(a.scheduleToStartTimeoutSeconds(), o.getScheduleToStartTimeout()))
52-
.setStartToCloseTimeout(
53-
mergeDuration(a.startToCloseTimeoutSeconds(), o.getStartToCloseTimeout()))
54-
.setHeartbeatTimeout(mergeDuration(a.heartbeatTimeoutSeconds(), o.getHeartbeatTimeout()))
55-
.setTaskList(
56-
o.getTaskList() != null
57-
? o.getTaskList()
58-
: (a.taskList().isEmpty() ? null : a.taskList()))
59-
.setRetryOptions(RetryOptions.merge(r, o.getRetryOptions()))
60-
.setContextPropagators(o.getContextPropagators())
61-
.validateAndBuildWithDefaults();
32+
public static Builder newBuilder() {
33+
return new Builder();
34+
}
35+
36+
public static Builder newBuilder(ActivityOptions options) {
37+
return new Builder(options);
38+
}
39+
40+
public static ActivityOptions getDefaultInstance() {
41+
return DEFAULT_INSTANCE;
42+
}
43+
44+
private static final ActivityOptions DEFAULT_INSTANCE;
45+
46+
static {
47+
DEFAULT_INSTANCE = ActivityOptions.newBuilder().build();
6248
}
6349

6450
public static final class Builder {
@@ -155,6 +141,14 @@ public Builder setContextPropagators(List<ContextPropagator> contextPropagators)
155141
return this;
156142
}
157143

144+
/**
145+
* Properties that are set on this builder take precedence over ones found in the annotation.
146+
*/
147+
public Builder setMethodRetry(MethodRetry r) {
148+
retryOptions = RetryOptions.merge(r, retryOptions);
149+
return this;
150+
}
151+
158152
public ActivityOptions build() {
159153
return new ActivityOptions(
160154
heartbeatTimeout,
@@ -324,15 +318,4 @@ public int hashCode() {
324318
retryOptions,
325319
contextPropagators);
326320
}
327-
328-
static Duration mergeDuration(int annotationSeconds, Duration options) {
329-
if (options == null) {
330-
if (annotationSeconds == 0) {
331-
return null;
332-
}
333-
return Duration.ofSeconds(annotationSeconds);
334-
} else {
335-
return options;
336-
}
337-
}
338321
}

src/main/java/com/uber/cadence/activity/LocalActivityOptions.java

+30-27
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*
2+
* Modifications Copyright (c) 2017-2020 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
@@ -29,31 +29,23 @@
2929
/** Options used to configure how an local activity is invoked. */
3030
public final class LocalActivityOptions {
3131

32-
/**
33-
* Used to merge annotation and options. Options takes precedence. Returns options with all
34-
* defaults filled in.
35-
*/
36-
public static LocalActivityOptions merge(
37-
ActivityMethod a, MethodRetry r, LocalActivityOptions o) {
38-
if (a == null) {
39-
if (r == null) {
40-
return new LocalActivityOptions.Builder(o).validateAndBuildWithDefaults();
41-
}
42-
RetryOptions mergedR = RetryOptions.merge(r, o.getRetryOptions());
43-
return new LocalActivityOptions.Builder()
44-
.setRetryOptions(mergedR)
45-
.validateAndBuildWithDefaults();
46-
}
47-
if (o == null) {
48-
o = new LocalActivityOptions.Builder().build();
49-
}
50-
return new LocalActivityOptions.Builder()
51-
.setScheduleToCloseTimeout(
52-
ActivityOptions.mergeDuration(
53-
a.scheduleToCloseTimeoutSeconds(), o.getScheduleToCloseTimeout()))
54-
.setRetryOptions(RetryOptions.merge(r, o.getRetryOptions()))
55-
.setContextPropagators(o.getContextPropagators())
56-
.validateAndBuildWithDefaults();
32+
public static Builder newBuilder() {
33+
return new Builder(null);
34+
}
35+
36+
/** @param o null is allowed */
37+
public static Builder newBuilder(LocalActivityOptions o) {
38+
return new Builder(o);
39+
}
40+
41+
public static LocalActivityOptions getDefaultInstance() {
42+
return DEFAULT_INSTANCE;
43+
}
44+
45+
private static final LocalActivityOptions DEFAULT_INSTANCE;
46+
47+
static {
48+
DEFAULT_INSTANCE = LocalActivityOptions.newBuilder().build();
5749
}
5850

5951
public static final class Builder {
@@ -92,6 +84,17 @@ public Builder setContextPropagators(List<ContextPropagator> contextPropagators)
9284
return this;
9385
}
9486

87+
/**
88+
* Merges MethodRetry annotation. The values of this builder take precedence over annotation
89+
* ones.
90+
*/
91+
public Builder setMethodRetry(MethodRetry r) {
92+
if (r != null) {
93+
this.retryOptions = RetryOptions.merge(r, retryOptions);
94+
}
95+
return this;
96+
}
97+
9598
public LocalActivityOptions build() {
9699
return new LocalActivityOptions(scheduleToCloseTimeout, retryOptions, contextPropagators);
97100
}

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*
2+
* Modifications Copyright (c) 2017-2020 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
@@ -17,7 +17,6 @@
1717

1818
package com.uber.cadence.internal.sync;
1919

20-
import com.uber.cadence.activity.ActivityMethod;
2120
import com.uber.cadence.activity.ActivityOptions;
2221
import com.uber.cadence.common.MethodRetry;
2322
import com.uber.cadence.workflow.ActivityStub;
@@ -42,9 +41,10 @@ private ActivityInvocationHandler(ActivityOptions options, WorkflowInterceptor a
4241

4342
@Override
4443
protected Function<Object[], Object> getActivityFunc(
45-
Method method, MethodRetry methodRetry, ActivityMethod activityMethod, String activityName) {
44+
Method method, MethodRetry methodRetry, String activityName) {
4645
Function<Object[], Object> function;
47-
ActivityOptions mergedOptions = ActivityOptions.merge(activityMethod, methodRetry, options);
46+
ActivityOptions mergedOptions =
47+
ActivityOptions.newBuilder(options).setMethodRetry(methodRetry).build();
4848
ActivityStub stub = ActivityStubImpl.newInstance(mergedOptions, activityExecutor);
4949

5050
function =

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public Object invoke(Object proxy, Method method, Object[] args) {
6767
activityName = activityMethod.name();
6868
}
6969

70-
function = getActivityFunc(method, methodRetry, activityMethod, activityName);
70+
function = getActivityFunc(method, methodRetry, activityName);
7171
methodFunctions.put(method, function);
7272
} catch (NoSuchMethodException e) {
7373
throw Workflow.wrap(e);
@@ -77,5 +77,5 @@ public Object invoke(Object proxy, Method method, Object[] args) {
7777
}
7878

7979
protected abstract Function<Object[], Object> getActivityFunc(
80-
Method method, MethodRetry methodRetry, ActivityMethod activityMethod, String activityName);
80+
Method method, MethodRetry methodRetry, String activityName);
8181
}

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

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*
2+
* Modifications Copyright (c) 2017-2020 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
@@ -17,7 +17,6 @@
1717

1818
package com.uber.cadence.internal.sync;
1919

20-
import com.uber.cadence.activity.ActivityMethod;
2120
import com.uber.cadence.activity.LocalActivityOptions;
2221
import com.uber.cadence.common.MethodRetry;
2322
import com.uber.cadence.workflow.ActivityStub;
@@ -43,10 +42,12 @@ private LocalActivityInvocationHandler(
4342

4443
@Override
4544
protected Function<Object[], Object> getActivityFunc(
46-
Method method, MethodRetry methodRetry, ActivityMethod activityMethod, String activityName) {
45+
Method method, MethodRetry methodRetry, String activityName) {
4746
Function<Object[], Object> function;
4847
LocalActivityOptions mergedOptions =
49-
LocalActivityOptions.merge(activityMethod, methodRetry, options);
48+
LocalActivityOptions.newBuilder(options)
49+
.setMethodRetry(methodRetry)
50+
.validateAndBuildWithDefaults();
5051
ActivityStub stub = LocalActivityStubImpl.newInstance(mergedOptions, activityExecutor);
5152
function =
5253
(a) -> stub.execute(activityName, method.getReturnType(), method.getGenericReturnType(), a);

0 commit comments

Comments
 (0)