Skip to content

WIP #994

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

Open
wants to merge 1 commit into
base: feature/issue-985/thrift-deprecation
Choose a base branch
from
Open

WIP #994

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,17 @@ public static boolean isWorkflowExecutionCompletedEvent(HistoryEvent event) {
|| event.getEventType() == EventType.WorkflowExecutionTerminated));
}

public static boolean isWorkflowExecutionCompletedEvent(
com.uber.cadence.api.v1.HistoryEvent event) {
return ((event != null)
&& (event.hasWorkflowExecutionCompletedEventAttributes()
|| event.hasWorkflowExecutionCanceledEventAttributes()
|| event.hasWorkflowExecutionFailedEventAttributes()
|| event.hasWorkflowExecutionTimedOutEventAttributes()
|| event.hasWorkflowExecutionContinuedAsNewEventAttributes()
|| event.hasWorkflowExecutionTerminatedEventAttributes()));
}

public static boolean isWorkflowExecutionCompleteDecision(Decision decision) {
return ((decision != null)
&& (decision.getDecisionType() == DecisionType.CompleteWorkflowExecution
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
package com.uber.cadence.internal.testservice;

import com.google.common.base.Throwables;
import com.uber.cadence.InternalServiceError;
import com.uber.cadence.WorkflowExecution;
import com.uber.cadence.api.v1.WorkflowExecution;
import com.uber.cadence.serviceclient.exceptions.InternalServiceException;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
Expand All @@ -39,7 +39,10 @@ final class ActivityId {
}

ActivityId(String domain, String workflowId, String runId, String id) {
this(domain, new WorkflowExecution().setWorkflowId(workflowId).setRunId(runId), id);
this(
domain,
WorkflowExecution.newBuilder().setWorkflowId(workflowId).setRunId(runId).build(),
id);
}

public ActivityId(ExecutionId executionId, String id) {
Expand Down Expand Up @@ -85,7 +88,7 @@ public String toString() {
}

/** Used for task tokens. */
public byte[] toBytes() throws InternalServiceError {
public byte[] toBytes() throws InternalServiceException {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(bout);
try {
Expand All @@ -96,11 +99,11 @@ public byte[] toBytes() throws InternalServiceError {
out.writeUTF(id);
return bout.toByteArray();
} catch (IOException e) {
throw new InternalServiceError(Throwables.getStackTraceAsString(e));
throw new InternalServiceException(e);
}
}

static ActivityId fromBytes(byte[] serialized) throws InternalServiceError {
static ActivityId fromBytes(byte[] serialized) throws InternalServiceException {
ByteArrayInputStream bin = new ByteArrayInputStream(serialized);
DataInputStream in = new DataInputStream(bin);
try {
Expand All @@ -110,7 +113,7 @@ static ActivityId fromBytes(byte[] serialized) throws InternalServiceError {
String id = in.readUTF();
return new ActivityId(domain, workflowId, runId, id);
} catch (IOException e) {
throw new InternalServiceError(Throwables.getStackTraceAsString(e));
throw new InternalServiceException(Throwables.getStackTraceAsString(e));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@

package com.uber.cadence.internal.testservice;

import com.google.common.base.Throwables;
import com.uber.cadence.InternalServiceError;
import com.uber.cadence.serviceclient.exceptions.InternalServiceException;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
Expand All @@ -45,13 +44,13 @@ int getHistorySize() {
}

/** Used for task tokens. */
byte[] toBytes() throws InternalServiceError {
byte[] toBytes() throws InternalServiceException {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(bout);
try {
addBytes(out);
} catch (IOException e) {
throw new InternalServiceError(Throwables.getStackTraceAsString(e));
throw new InternalServiceException(e);
}
return bout.toByteArray();
}
Expand All @@ -61,15 +60,15 @@ private void addBytes(DataOutputStream out) throws IOException {
out.writeInt(historySize);
}

static DecisionTaskToken fromBytes(byte[] serialized) throws InternalServiceError {
static DecisionTaskToken fromBytes(byte[] serialized) throws InternalServiceException {
ByteArrayInputStream bin = new ByteArrayInputStream(serialized);
DataInputStream in = new DataInputStream(bin);
try {
ExecutionId executionId = ExecutionId.readFromBytes(in);
int historySize = in.readInt();
return new DecisionTaskToken(executionId, historySize);
} catch (IOException e) {
throw new InternalServiceError(Throwables.getStackTraceAsString(e));
throw new InternalServiceException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@

package com.uber.cadence.internal.testservice;

import com.google.common.base.Throwables;
import com.uber.cadence.InternalServiceError;
import com.uber.cadence.WorkflowExecution;
import com.uber.cadence.api.v1.WorkflowExecution;
import com.uber.cadence.serviceclient.exceptions.InternalServiceException;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
Expand All @@ -40,7 +39,10 @@ final class ExecutionId {
ExecutionId(String domain, String workflowId, String runId) {
this(
domain,
new WorkflowExecution().setWorkflowId(Objects.requireNonNull(workflowId)).setRunId(runId));
WorkflowExecution.newBuilder()
.setWorkflowId(Objects.requireNonNull(workflowId))
.setRunId(runId)
.build());
}

public String getDomain() {
Expand Down Expand Up @@ -81,13 +83,13 @@ public String toString() {
}

/** Used for task tokens. */
byte[] toBytes() throws InternalServiceError {
byte[] toBytes() throws InternalServiceException {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(bout);
try {
addBytes(out);
} catch (IOException e) {
throw new InternalServiceError(Throwables.getStackTraceAsString(e));
throw new InternalServiceException(e);
}
return bout.toByteArray();
}
Expand All @@ -100,13 +102,13 @@ void addBytes(DataOutputStream out) throws IOException {
}
}

static ExecutionId fromBytes(byte[] serialized) throws InternalServiceError {
static ExecutionId fromBytes(byte[] serialized) throws InternalServiceException {
ByteArrayInputStream bin = new ByteArrayInputStream(serialized);
DataInputStream in = new DataInputStream(bin);
try {
return readFromBytes(in);
} catch (IOException e) {
throw new InternalServiceError(Throwables.getStackTraceAsString(e));
throw new InternalServiceException(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@

package com.uber.cadence.internal.testservice;

import com.uber.cadence.BadRequestError;
import com.uber.cadence.EntityNotExistsError;
import com.uber.cadence.HistoryEvent;
import com.uber.cadence.InternalServiceError;
import com.uber.cadence.WorkflowExecution;
import com.uber.cadence.api.v1.HistoryEvent;
import com.uber.cadence.api.v1.WorkflowExecution;
import com.uber.cadence.internal.common.WorkflowExecutionUtils;
import com.uber.cadence.internal.testservice.TestWorkflowStore.ActivityTask;
import com.uber.cadence.internal.testservice.TestWorkflowStore.DecisionTask;
import com.uber.cadence.serviceclient.exceptions.BadRequestException;
import com.uber.cadence.serviceclient.exceptions.EntityNotExistsException;
import com.uber.cadence.serviceclient.exceptions.InternalServiceException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
Expand All @@ -36,7 +36,7 @@ final class RequestContext {
@FunctionalInterface
interface CommitCallback {

void apply(int historySize) throws InternalServiceError, BadRequestError;
void apply(int historySize) throws InternalServiceException, BadRequestException;
}

static final class Timer {
Expand Down Expand Up @@ -206,12 +206,12 @@ void onCommit(CommitCallback callback) {

/** @return nextEventId */
long commitChanges(TestWorkflowStore store)
throws InternalServiceError, EntityNotExistsError, BadRequestError {
throws InternalServiceException, EntityNotExistsException, BadRequestException {
return store.save(this);
}

/** Called by {@link TestWorkflowStore#save(RequestContext)} */
void fireCallbacks(int historySize) throws InternalServiceError, BadRequestError {
void fireCallbacks(int historySize) throws InternalServiceException, BadRequestException {
for (CommitCallback callback : commitCallbacks) {
callback.apply(historySize);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

package com.uber.cadence.internal.testservice;

import com.uber.cadence.BadRequestError;
import com.uber.cadence.RetryPolicy;
import com.uber.cadence.api.v1.RetryPolicy;
import com.uber.cadence.serviceclient.exceptions.BadRequestException;
import java.util.List;
import java.util.concurrent.TimeUnit;

Expand All @@ -28,14 +28,14 @@ final class RetryState {
private final long expirationTime;
private final int attempt;

RetryState(RetryPolicy retryPolicy, long expirationTime) throws BadRequestError {
RetryState(RetryPolicy retryPolicy, long expirationTime) throws BadRequestException {
this(validateRetryPolicy(retryPolicy), expirationTime, 0);
}

private RetryState(RetryPolicy retryPolicy, long expirationTime, int attempt) {
this.retryPolicy = retryPolicy;
this.expirationTime =
retryPolicy.getExpirationIntervalInSeconds() == 0 ? Long.MAX_VALUE : expirationTime;
retryPolicy.getExpirationInterval().getSeconds() == 0 ? Long.MAX_VALUE : expirationTime;
this.attempt = attempt;
}

Expand Down Expand Up @@ -68,10 +68,10 @@ && getAttempt() >= retryPolicy.getMaximumAttempts() - 1) {
// MaximumAttempts is the total attempts, including initial (non-retry) attempt.
return 0;
}
long initInterval = TimeUnit.SECONDS.toMillis(retryPolicy.getInitialIntervalInSeconds());
long initInterval = TimeUnit.SECONDS.toMillis(retryPolicy.getInitialInterval().getSeconds());
long nextInterval =
(long) (initInterval * Math.pow(retryPolicy.getBackoffCoefficient(), getAttempt()));
long maxInterval = TimeUnit.SECONDS.toMillis(retryPolicy.getMaximumIntervalInSeconds());
long maxInterval = TimeUnit.SECONDS.toMillis(retryPolicy.getMaximumInterval().getSeconds());
if (nextInterval <= 0) {
// math.Pow() could overflow
if (maxInterval > 0) {
Expand All @@ -93,37 +93,37 @@ && getAttempt() >= retryPolicy.getMaximumAttempts() - 1) {
}

// check if error is non-retriable
List<String> nonRetriableErrorReasons = retryPolicy.getNonRetriableErrorReasons();
if (nonRetriableErrorReasons != null) {
for (String err : nonRetriableErrorReasons) {
if (errReason.equals(err)) {
return 0;
}
List<String> nonRetriableErrorReasons = retryPolicy.getNonRetryableErrorReasonsList();
for (String err : nonRetriableErrorReasons) {
if (errReason.equals(err)) {
return 0;
}
}
return (int) TimeUnit.MILLISECONDS.toSeconds((long) Math.ceil((double) backoffInterval));
}

static RetryPolicy validateRetryPolicy(RetryPolicy policy) throws BadRequestError {
if (policy.getInitialIntervalInSeconds() <= 0) {
throw new BadRequestError("InitialIntervalInSeconds must be greater than 0 on retry policy.");
static RetryPolicy validateRetryPolicy(RetryPolicy policy) throws BadRequestException {
if (policy.getInitialInterval().getSeconds() <= 0) {
throw new BadRequestException(
"InitialIntervalInSeconds must be greater than 0 on retry policy.");
}
if (policy.getBackoffCoefficient() < 1) {
throw new BadRequestError("BackoffCoefficient cannot be less than 1 on retry policy.");
throw new BadRequestException("BackoffCoefficient cannot be less than 1 on retry policy.");
}
if (policy.getMaximumIntervalInSeconds() < 0) {
throw new BadRequestError("MaximumIntervalInSeconds cannot be less than 0 on retry policy.");
if (policy.getMaximumInterval().getSeconds() < 0) {
throw new BadRequestException(
"MaximumIntervalInSeconds cannot be less than 0 on retry policy.");
}
if (policy.getMaximumIntervalInSeconds() > 0
&& policy.getMaximumIntervalInSeconds() < policy.getInitialIntervalInSeconds()) {
throw new BadRequestError(
if (policy.getMaximumInterval().getSeconds() > 0
&& policy.getMaximumInterval().getSeconds() < policy.getInitialInterval().getSeconds()) {
throw new BadRequestException(
"MaximumIntervalInSeconds cannot be less than InitialIntervalInSeconds on retry policy.");
}
if (policy.getMaximumAttempts() < 0) {
throw new BadRequestError("MaximumAttempts cannot be less than 0 on retry policy.");
throw new BadRequestException("MaximumAttempts cannot be less than 0 on retry policy.");
}
if (policy.getMaximumAttempts() == 0 && policy.getExpirationIntervalInSeconds() == 0) {
throw new BadRequestError(
if (policy.getMaximumAttempts() == 0 && policy.getExpirationInterval().getSeconds() == 0) {
throw new BadRequestException(
"MaximumAttempts and ExpirationIntervalInSeconds are both 0. At least one of them must be specified.");
}
return policy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

package com.uber.cadence.internal.testservice;

import com.uber.cadence.BadRequestError;
import com.uber.cadence.InternalServiceError;
import com.uber.cadence.internal.testservice.StateMachines.Action;
import com.uber.cadence.internal.testservice.StateMachines.State;
import com.uber.cadence.serviceclient.exceptions.BadRequestException;
import com.uber.cadence.serviceclient.exceptions.InternalServiceException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand All @@ -45,7 +45,7 @@ final class StateMachine<Data> {
interface Callback<D, R> {

void apply(RequestContext ctx, D data, R request, long referenceId)
throws InternalServiceError, BadRequestError;
throws InternalServiceException, BadRequestException;
}

/**
Expand All @@ -57,7 +57,7 @@ interface DynamicCallback<D, R> {

/** @return state after the action */
State apply(RequestContext ctx, D data, R request, long referenceId)
throws InternalServiceError, BadRequestError;
throws InternalServiceException, BadRequestException;
}

private static class Transition {
Expand Down Expand Up @@ -110,7 +110,7 @@ public String toString() {

private interface TransitionDestination<Data, R> {
State apply(RequestContext ctx, Data data, R request, long referenceId)
throws InternalServiceError, BadRequestError;
throws InternalServiceException, BadRequestException;
}

private static class FixedTransitionDestination<Data, R>
Expand All @@ -132,7 +132,7 @@ public String toString() {

@Override
public State apply(RequestContext ctx, Data data, R request, long referenceId)
throws InternalServiceError, BadRequestError {
throws InternalServiceException, BadRequestException {
callback.apply(ctx, data, request, referenceId);
return state;
}
Expand All @@ -158,7 +158,7 @@ public String toString() {

@Override
public State apply(RequestContext ctx, Data data, R request, long referenceId)
throws InternalServiceError, BadRequestError {
throws InternalServiceException, BadRequestException {
state = callback.apply(ctx, data, request, referenceId);
for (State s : expectedStates) {
if (s == state) {
Expand Down Expand Up @@ -221,13 +221,14 @@ <V> StateMachine<Data> add(
}

<R> void action(Action action, RequestContext context, R request, long referenceId)
throws InternalServiceError, BadRequestError {
throws InternalServiceException, BadRequestException {
Transition transition = new Transition(state, action);
@SuppressWarnings("unchecked")
TransitionDestination<Data, R> destination =
(TransitionDestination<Data, R>) transitions.get(transition);
if (destination == null) {
throw new InternalServiceError("Invalid " + transition + ", history: " + transitionHistory);
throw new InternalServiceException(
"Invalid " + transition + ", history: " + transitionHistory);
}
state = destination.apply(context, data, request, referenceId);
transitionHistory.add(transition);
Expand Down
Loading
Loading