Skip to content

Commit add6c4e

Browse files
Add support for upsert memo (#2202)
Add support for upsert memo
1 parent ecd26b7 commit add6c4e

File tree

18 files changed

+368
-14
lines changed

18 files changed

+368
-14
lines changed

temporal-sdk/src/main/java/io/temporal/common/interceptors/WorkflowOutboundCallsInterceptor.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,8 @@ <R> R mutableSideEffect(
617617

618618
void upsertTypedSearchAttributes(SearchAttributeUpdate<?>... searchAttributeUpdates);
619619

620+
void upsertMemo(Map<String, Object> memo);
621+
620622
/**
621623
* Intercepts creation of a workflow child thread.
622624
*

temporal-sdk/src/main/java/io/temporal/common/interceptors/WorkflowOutboundCallsInterceptorBase.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,11 @@ public void upsertTypedSearchAttributes(SearchAttributeUpdate<?>... searchAttrib
156156
next.upsertTypedSearchAttributes(searchAttributeUpdates);
157157
}
158158

159+
@Override
160+
public void upsertMemo(Map<String, Object> memo) {
161+
next.upsertMemo(memo);
162+
}
163+
159164
@Override
160165
public Object newChildThread(Runnable runnable, boolean detached, String name) {
161166
return next.newChildThread(runnable, detached, name);

temporal-sdk/src/main/java/io/temporal/internal/common/WorkflowExecutionUtils.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ public static boolean isCommandEvent(HistoryEvent event) {
299299
case EVENT_TYPE_WORKFLOW_EXECUTION_UPDATE_ACCEPTED:
300300
case EVENT_TYPE_WORKFLOW_EXECUTION_UPDATE_REJECTED:
301301
case EVENT_TYPE_WORKFLOW_EXECUTION_UPDATE_COMPLETED:
302+
case EVENT_TYPE_WORKFLOW_PROPERTIES_MODIFIED:
302303
return true;
303304
default:
304305
return false;
@@ -334,6 +335,8 @@ public static EventType getEventTypeForCommand(CommandType commandType) {
334335
return EventType.EVENT_TYPE_SIGNAL_EXTERNAL_WORKFLOW_EXECUTION_INITIATED;
335336
case COMMAND_TYPE_UPSERT_WORKFLOW_SEARCH_ATTRIBUTES:
336337
return EventType.EVENT_TYPE_UPSERT_WORKFLOW_SEARCH_ATTRIBUTES;
338+
case COMMAND_TYPE_MODIFY_WORKFLOW_PROPERTIES:
339+
return EventType.EVENT_TYPE_WORKFLOW_PROPERTIES_MODIFIED;
337340
}
338341
throw new IllegalArgumentException("Unknown commandType");
339342
}

temporal-sdk/src/main/java/io/temporal/internal/replay/BasicWorkflowContext.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,6 @@ public Map<String, Payload> getHeader() {
128128
return startedAttributes.getHeader().getFieldsMap();
129129
}
130130

131-
public Payload getMemo(String key) {
132-
return startedAttributes.getMemo().getFieldsMap().get(key);
133-
}
134-
135131
int getAttempt() {
136132
return startedAttributes.getAttempt();
137133
}

temporal-sdk/src/main/java/io/temporal/internal/replay/ReplayWorkflowContext.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,9 @@ boolean getVersion(
401401
/** Updates or inserts search attributes used to index workflows. */
402402
void upsertSearchAttributes(@Nonnull SearchAttributes searchAttributes);
403403

404+
/** Updates or inserts memos. */
405+
void upsertMemo(@Nonnull Memo memo);
406+
404407
/**
405408
* @return true if this flag may currently be used.
406409
*/

temporal-sdk/src/main/java/io/temporal/internal/replay/ReplayWorkflowContextImpl.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ public long getRunStartedTimestampMillis() {
185185

186186
@Override
187187
public Payload getMemo(String key) {
188-
return basicWorkflowContext.getMemo(key);
188+
return mutableState.getMemo(key);
189189
}
190190

191191
@Override
@@ -335,6 +335,12 @@ public void upsertSearchAttributes(@Nonnull SearchAttributes searchAttributes) {
335335
mutableState.upsertSearchAttributes(searchAttributes);
336336
}
337337

338+
@Override
339+
public void upsertMemo(@Nonnull Memo memo) {
340+
workflowStateMachines.upsertMemo(memo);
341+
mutableState.upsertMemo(memo);
342+
}
343+
338344
@Override
339345
public int getAttempt() {
340346
return basicWorkflowContext.getAttempt();

temporal-sdk/src/main/java/io/temporal/internal/replay/WorkflowMutableState.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@
2121
package io.temporal.internal.replay;
2222

2323
import io.temporal.api.command.v1.ContinueAsNewWorkflowExecutionCommandAttributes;
24+
import io.temporal.api.common.v1.Memo;
25+
import io.temporal.api.common.v1.Payload;
2426
import io.temporal.api.common.v1.SearchAttributes;
2527
import io.temporal.api.history.v1.WorkflowExecutionStartedEventAttributes;
28+
import javax.annotation.Nonnull;
2629
import javax.annotation.Nullable;
2730

2831
class WorkflowMutableState {
@@ -31,11 +34,17 @@ class WorkflowMutableState {
3134
private boolean workflowMethodCompleted;
3235
private Throwable workflowTaskFailureThrowable;
3336
private SearchAttributes.Builder searchAttributes;
37+
private Memo.Builder memo;
3438

3539
WorkflowMutableState(WorkflowExecutionStartedEventAttributes startedAttributes) {
3640
if (startedAttributes.hasSearchAttributes()) {
3741
this.searchAttributes = startedAttributes.getSearchAttributes().toBuilder();
3842
}
43+
if (startedAttributes.hasMemo()) {
44+
this.memo = startedAttributes.getMemo().toBuilder();
45+
} else {
46+
this.memo = Memo.newBuilder();
47+
}
3948
}
4049

4150
boolean isCancelRequested() {
@@ -76,6 +85,10 @@ SearchAttributes getSearchAttributes() {
7685
: searchAttributes.build();
7786
}
7887

88+
public Payload getMemo(String key) {
89+
return memo.build().getFieldsMap().get(key);
90+
}
91+
7992
void upsertSearchAttributes(@Nullable SearchAttributes searchAttributes) {
8093
if (searchAttributes == null || searchAttributes.getIndexedFieldsCount() == 0) {
8194
return;
@@ -85,4 +98,8 @@ void upsertSearchAttributes(@Nullable SearchAttributes searchAttributes) {
8598
}
8699
this.searchAttributes.putAllIndexedFields(searchAttributes.getIndexedFieldsMap());
87100
}
101+
102+
public void upsertMemo(@Nonnull Memo memo) {
103+
this.memo.putAllFields(memo.getFieldsMap());
104+
}
88105
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright (C) 2022 Temporal Technologies, Inc. All Rights Reserved.
3+
*
4+
* Copyright (C) 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5+
*
6+
* Modifications copyright (C) 2017 Uber Technologies, Inc.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this material except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
package io.temporal.internal.statemachines;
22+
23+
import io.temporal.api.command.v1.Command;
24+
import io.temporal.api.command.v1.ModifyWorkflowPropertiesCommandAttributes;
25+
import io.temporal.api.enums.v1.CommandType;
26+
import io.temporal.api.enums.v1.EventType;
27+
import io.temporal.workflow.Functions;
28+
29+
final class WorkflowPropertiesModifiedStateMachine
30+
extends EntityStateMachineInitialCommand<
31+
WorkflowPropertiesModifiedStateMachine.State,
32+
WorkflowPropertiesModifiedStateMachine.ExplicitEvent,
33+
WorkflowPropertiesModifiedStateMachine> {
34+
35+
private ModifyWorkflowPropertiesCommandAttributes modifiedPropertiesAttributes;
36+
37+
public static void newInstance(
38+
ModifyWorkflowPropertiesCommandAttributes modifiedPropertiesAttributes,
39+
Functions.Proc1<CancellableCommand> commandSink,
40+
Functions.Proc1<StateMachine> stateMachineSink) {
41+
new WorkflowPropertiesModifiedStateMachine(
42+
modifiedPropertiesAttributes, commandSink, stateMachineSink);
43+
}
44+
45+
private WorkflowPropertiesModifiedStateMachine(
46+
ModifyWorkflowPropertiesCommandAttributes modifiedPropertiesAttributes,
47+
Functions.Proc1<CancellableCommand> commandSink,
48+
Functions.Proc1<StateMachine> stateMachineSink) {
49+
super(STATE_MACHINE_DEFINITION, commandSink, stateMachineSink);
50+
this.modifiedPropertiesAttributes = modifiedPropertiesAttributes;
51+
explicitEvent(ExplicitEvent.SCHEDULE);
52+
}
53+
54+
enum ExplicitEvent {
55+
SCHEDULE
56+
}
57+
58+
enum State {
59+
CREATED,
60+
MODIFY_COMMAND_CREATED,
61+
MODIFY_COMMAND_RECORDED,
62+
}
63+
64+
public static final StateMachineDefinition<
65+
State, ExplicitEvent, WorkflowPropertiesModifiedStateMachine>
66+
STATE_MACHINE_DEFINITION =
67+
StateMachineDefinition
68+
.<State, ExplicitEvent, WorkflowPropertiesModifiedStateMachine>newInstance(
69+
"WorkflowPropertiesModified", State.CREATED, State.MODIFY_COMMAND_RECORDED)
70+
.add(
71+
State.CREATED,
72+
ExplicitEvent.SCHEDULE,
73+
State.MODIFY_COMMAND_CREATED,
74+
WorkflowPropertiesModifiedStateMachine::createModifyCommand)
75+
.add(
76+
State.MODIFY_COMMAND_CREATED,
77+
CommandType.COMMAND_TYPE_MODIFY_WORKFLOW_PROPERTIES,
78+
State.MODIFY_COMMAND_CREATED)
79+
.add(
80+
State.MODIFY_COMMAND_CREATED,
81+
EventType.EVENT_TYPE_WORKFLOW_PROPERTIES_MODIFIED,
82+
State.MODIFY_COMMAND_RECORDED);
83+
84+
private void createModifyCommand() {
85+
addCommand(
86+
Command.newBuilder()
87+
.setCommandType(CommandType.COMMAND_TYPE_MODIFY_WORKFLOW_PROPERTIES)
88+
.setModifyWorkflowPropertiesCommandAttributes(modifiedPropertiesAttributes)
89+
.build());
90+
modifiedPropertiesAttributes = null;
91+
}
92+
}

temporal-sdk/src/main/java/io/temporal/internal/statemachines/WorkflowStateMachines.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,14 @@ public void upsertSearchAttributes(SearchAttributes attributes) {
918918
UpsertSearchAttributesStateMachine.newInstance(attributes, commandSink, stateMachineSink);
919919
}
920920

921+
public void upsertMemo(Memo memo) {
922+
checkEventLoopExecuting();
923+
WorkflowPropertiesModifiedStateMachine.newInstance(
924+
ModifyWorkflowPropertiesCommandAttributes.newBuilder().setUpsertedMemo(memo).build(),
925+
commandSink,
926+
stateMachineSink);
927+
}
928+
921929
public void completeWorkflow(Optional<Payloads> workflowOutput) {
922930
checkEventLoopExecuting();
923931
CompleteWorkflowStateMachine.newInstance(workflowOutput, commandSink, stateMachineSink);
@@ -1188,6 +1196,7 @@ private void validateCommand(Command command, HistoryEvent event) {
11881196
case COMMAND_TYPE_COMPLETE_WORKFLOW_EXECUTION:
11891197
case COMMAND_TYPE_FAIL_WORKFLOW_EXECUTION:
11901198
case COMMAND_TYPE_PROTOCOL_MESSAGE:
1199+
case COMMAND_TYPE_MODIFY_WORKFLOW_PROPERTIES:
11911200
break;
11921201
case UNRECOGNIZED:
11931202
case COMMAND_TYPE_UNSPECIFIED:
@@ -1343,6 +1352,7 @@ private OptionalLong getInitialCommandEventId(HistoryEvent event) {
13431352
case EVENT_TYPE_WORKFLOW_EXECUTION_CANCEL_REQUESTED:
13441353
case EVENT_TYPE_WORKFLOW_EXECUTION_CANCELED:
13451354
case EVENT_TYPE_WORKFLOW_EXECUTION_UPDATE_ADMITTED:
1355+
case EVENT_TYPE_WORKFLOW_PROPERTIES_MODIFIED:
13461356
return OptionalLong.of(event.getEventId());
13471357

13481358
default:

temporal-sdk/src/main/java/io/temporal/internal/sync/SyncWorkflowContext.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,6 +1209,16 @@ public void upsertTypedSearchAttributes(SearchAttributeUpdate<?>... searchAttrib
12091209
replayContext.upsertSearchAttributes(attr);
12101210
}
12111211

1212+
@Override
1213+
public void upsertMemo(Map<String, Object> memo) {
1214+
Preconditions.checkArgument(memo != null, "null memo");
1215+
Preconditions.checkArgument(!memo.isEmpty(), "empty memo");
1216+
replayContext.upsertMemo(
1217+
Memo.newBuilder()
1218+
.putAllFields(intoPayloadMap(dataConverterWithCurrentWorkflowContext, memo))
1219+
.build());
1220+
}
1221+
12121222
@Nonnull
12131223
public Object newWorkflowMethodThreadIntercepted(Runnable runnable, @Nullable String name) {
12141224
return runner.newWorkflowThread(runnable, false, name);

0 commit comments

Comments
 (0)