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

Add activity interface support #559

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
76 changes: 76 additions & 0 deletions src/main/java/com/uber/cadence/activity/ActivityInterface.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Modifications Copyright (c) 2017-2020 Uber Technologies Inc.
* Portions of the Software are attributed to Copyright (c) 2020 Temporal Technologies Inc.
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* 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.activity;

import com.uber.cadence.workflow.Workflow;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Indicates that the interface is an activity interface. Only interfaces annotated with this
* annotation can be used as parameters to {@link Workflow#newActivityStub(Class)} methods.
*
* <p>Each method of the interface annotated with <code>ActivityInterface</code> including inherited
* from interfaces is a separate activity. By default the name of an activity type is "short
* interface name"_"method name".
*
* <p>Example:
*
* <pre><code>
* public interface A {
* a();
* }
*
* {@literal @}ActivityInterface
* public interface B extends A {
* b();
* }
*
* {@literal @}ActivityInterface
* public interface C extends B {
* c();
* }
*
* public class CImpl implements C {
* public void a() {}
* public void b() {}
* public void c() {}
* }
* </code></pre>
*
* When <code>CImpl</code> instance is registered with the {@link com.uber.cadence.worker.Worker}
* the following activities are registered:
*
* <p>
*
* <ul>
* <li>B_a
* <li>B_b
* <li>C_c
* </ul>
*
* Note that method <code>a()</code> is registered as "B_a" because interface <code>A</code> lacks
* ActivityInterface annotation. The workflow code can call activities through stubs to <code>B
* </code> and <code>C</code> interfaces. A call to crate stub to <code>A</code> interface will fail
* as <code>A</code> is not annotated with ActivityInterface.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface ActivityInterface {}
19 changes: 14 additions & 5 deletions src/main/java/com/uber/cadence/activity/ActivityMethod.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
* Modifications Copyright (c) 2017-2020 Uber Technologies Inc.
* Portions of the Software are attributed to Copyright (c) 2020 Temporal Technologies Inc.
* 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
Expand All @@ -21,12 +21,11 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.time.Duration;

/**
* Indicates that the method is an activity method. This annotation applies only to activity
* interface methods. Not required. Use it to override default activity type name or other options.
* When both {@link ActivityOptions} and {@link ActivityMethod} have non default value for some
* parameter the {@link ActivityOptions} one takes precedence.
* interface methods. Not required. Use it to override default activity type name.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
Expand All @@ -40,30 +39,40 @@
* task list (use {@link #scheduleToStartTimeoutSeconds()} to limit it) plus activity execution
* time (use {@link #startToCloseTimeoutSeconds()} to limit it). Either this option or both
* schedule to start and start to close are required.
*
* @deprecated use {@link ActivityOptions.Builder#setScheduleToCloseTimeout(Duration)} instead.
*/
int scheduleToCloseTimeoutSeconds() default 0;

/**
* Time activity can stay in task list before it is picked up by a worker. If schedule to close is
* not provided then both this and start to close are required.
*
* @deprecated use {@link ActivityOptions.Builder#setScheduleToStartTimeout(Duration)} instead.
*/
int scheduleToStartTimeoutSeconds() default 0;

/**
* Maximum activity execution time after it was sent to a worker. If schedule to close is not
* provided then both this and schedule to start are required.
*
* @deprecated use {@link ActivityOptions.Builder#setStartToCloseTimeout(Duration)} instead.
*/
int startToCloseTimeoutSeconds() default 0;

/**
* Heartbeat interval. Activity must heartbeat before this interval passes after a last heartbeat
* or activity start.
*
* @deprecated use {@link ActivityOptions.Builder#setHeartbeatTimeout(Duration)} instead.
*/
int heartbeatTimeoutSeconds() default 0;

/**
* Task list to use when dispatching activity task to a worker. By default it is the same task
* list name the workflow was started with.
*
* @deprecated use {@link ActivityOptions.Builder#setTaskList(String)} instead.
*/
String taskList() default "";
}
69 changes: 26 additions & 43 deletions src/main/java/com/uber/cadence/activity/ActivityOptions.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
* Modifications Copyright (c) 2017-2020 Uber Technologies Inc.
* Portions of the Software are attributed to Copyright (c) 2020 Temporal Technologies Inc.
* 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
Expand All @@ -29,36 +29,22 @@
/** Options used to configure how an activity is invoked. */
public final class ActivityOptions {

/**
* Used to merge annotation and options. Options takes precedence. Returns options with all
* defaults filled in.
*/
public static ActivityOptions merge(ActivityMethod a, MethodRetry r, ActivityOptions o) {
if (a == null) {
if (r == null) {
return new ActivityOptions.Builder(o).validateAndBuildWithDefaults();
}
RetryOptions mergedR = RetryOptions.merge(r, o.getRetryOptions());
return new ActivityOptions.Builder().setRetryOptions(mergedR).validateAndBuildWithDefaults();
}
if (o == null) {
o = new ActivityOptions.Builder().build();
}
return new ActivityOptions.Builder()
.setScheduleToCloseTimeout(
mergeDuration(a.scheduleToCloseTimeoutSeconds(), o.getScheduleToCloseTimeout()))
.setScheduleToStartTimeout(
mergeDuration(a.scheduleToStartTimeoutSeconds(), o.getScheduleToStartTimeout()))
.setStartToCloseTimeout(
mergeDuration(a.startToCloseTimeoutSeconds(), o.getStartToCloseTimeout()))
.setHeartbeatTimeout(mergeDuration(a.heartbeatTimeoutSeconds(), o.getHeartbeatTimeout()))
.setTaskList(
o.getTaskList() != null
? o.getTaskList()
: (a.taskList().isEmpty() ? null : a.taskList()))
.setRetryOptions(RetryOptions.merge(r, o.getRetryOptions()))
.setContextPropagators(o.getContextPropagators())
.validateAndBuildWithDefaults();
public static Builder newBuilder() {
return new Builder();
}

public static Builder newBuilder(ActivityOptions options) {
return new Builder(options);
}

public static ActivityOptions getDefaultInstance() {
return DEFAULT_INSTANCE;
}

private static final ActivityOptions DEFAULT_INSTANCE;

static {
DEFAULT_INSTANCE = ActivityOptions.newBuilder().build();
}

public static final class Builder {
Expand Down Expand Up @@ -155,6 +141,14 @@ public Builder setContextPropagators(List<ContextPropagator> contextPropagators)
return this;
}

/**
* Properties that are set on this builder take precedence over ones found in the annotation.
*/
public Builder setMethodRetry(MethodRetry r) {
retryOptions = RetryOptions.merge(r, retryOptions);
return this;
}

public ActivityOptions build() {
return new ActivityOptions(
heartbeatTimeout,
Expand Down Expand Up @@ -324,15 +318,4 @@ public int hashCode() {
retryOptions,
contextPropagators);
}

static Duration mergeDuration(int annotationSeconds, Duration options) {
if (options == null) {
if (annotationSeconds == 0) {
return null;
}
return Duration.ofSeconds(annotationSeconds);
} else {
return options;
}
}
}
57 changes: 30 additions & 27 deletions src/main/java/com/uber/cadence/activity/LocalActivityOptions.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
* Modifications Copyright (c) 2017-2020 Uber Technologies Inc.
* Portions of the Software are attributed to Copyright (c) 2020 Temporal Technologies Inc.
* 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
Expand All @@ -29,31 +29,23 @@
/** Options used to configure how an local activity is invoked. */
public final class LocalActivityOptions {

/**
* Used to merge annotation and options. Options takes precedence. Returns options with all
* defaults filled in.
*/
public static LocalActivityOptions merge(
ActivityMethod a, MethodRetry r, LocalActivityOptions o) {
if (a == null) {
if (r == null) {
return new LocalActivityOptions.Builder(o).validateAndBuildWithDefaults();
}
RetryOptions mergedR = RetryOptions.merge(r, o.getRetryOptions());
return new LocalActivityOptions.Builder()
.setRetryOptions(mergedR)
.validateAndBuildWithDefaults();
}
if (o == null) {
o = new LocalActivityOptions.Builder().build();
}
return new LocalActivityOptions.Builder()
.setScheduleToCloseTimeout(
ActivityOptions.mergeDuration(
a.scheduleToCloseTimeoutSeconds(), o.getScheduleToCloseTimeout()))
.setRetryOptions(RetryOptions.merge(r, o.getRetryOptions()))
.setContextPropagators(o.getContextPropagators())
.validateAndBuildWithDefaults();
public static Builder newBuilder() {
return new Builder(null);
}

/** @param o null is allowed */
public static Builder newBuilder(LocalActivityOptions o) {
return new Builder(o);
}

public static LocalActivityOptions getDefaultInstance() {
return DEFAULT_INSTANCE;
}

private static final LocalActivityOptions DEFAULT_INSTANCE;

static {
DEFAULT_INSTANCE = LocalActivityOptions.newBuilder().build();
}

public static final class Builder {
Expand Down Expand Up @@ -92,6 +84,17 @@ public Builder setContextPropagators(List<ContextPropagator> contextPropagators)
return this;
}

/**
* Merges MethodRetry annotation. The values of this builder take precedence over annotation
* ones.
*/
public Builder setMethodRetry(MethodRetry r) {
if (r != null) {
this.retryOptions = RetryOptions.merge(r, retryOptions);
}
return this;
}

public LocalActivityOptions build() {
return new LocalActivityOptions(scheduleToCloseTimeout, retryOptions, contextPropagators);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
* Modifications Copyright (c) 2017-2020 Uber Technologies Inc.
* Portions of the Software are attributed to Copyright (c) 2020 Temporal Technologies Inc.
* 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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
* Modifications Copyright (c) 2017-2020 Uber Technologies Inc.
* Portions of the Software are attributed to Copyright (c) 2020 Temporal Technologies Inc.
* 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
Expand Down Expand Up @@ -51,7 +51,11 @@ public final class InternalUtils {
* @return "Simple class name"::"methodName"
*/
public static String getSimpleName(Method method) {
return method.getDeclaringClass().getSimpleName() + "::" + method.getName();
return getSimpleName(method.getDeclaringClass(), method);
}

public static String getSimpleName(Class<?> type, Method method) {
return type.getSimpleName() + "::" + method.getName();
}

public static String getWorkflowType(Method method, WorkflowMethod workflowMethod) {
Expand Down
Loading