Skip to content

Commit 52e33cb

Browse files
committed
[Fix #1727] Adding MetaTransient annotation
Signed-off-by: Francisco Javier Tirado Sarti <ftirados@ibm.com>
1 parent 5be295c commit 52e33cb

4 files changed

Lines changed: 79 additions & 2 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.serverlessworkflow.impl.persistence.metadata;
17+
18+
import static java.lang.annotation.ElementType.TYPE;
19+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
20+
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.Target;
23+
24+
/**
25+
* Annotation to mark an additional object POJO as transient, indicating that it should not be
26+
* persisted.
27+
*/
28+
@Retention(RUNTIME)
29+
@Target(TYPE)
30+
public @interface MetaTransient {}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.serverlessworkflow.impl.persistence.metadata;
17+
18+
import io.serverlessworkflow.impl.WorkflowInstanceData;
19+
import java.util.Map;
20+
import java.util.Map.Entry;
21+
import java.util.stream.Collectors;
22+
import java.util.stream.Stream;
23+
24+
public class PersistenceMetaUtils {
25+
26+
private PersistenceMetaUtils() {}
27+
28+
public static Stream<Entry<String, Object>> durableMetadataAsStream(
29+
WorkflowInstanceData instance) {
30+
return instance.metadata().entrySet().stream()
31+
.filter(
32+
e ->
33+
e.getValue() != null
34+
&& !e.getValue().getClass().isAnnotationPresent(MetaTransient.class));
35+
}
36+
37+
public static Map<String, Object> durableMetadata(WorkflowInstanceData instance) {
38+
return durableMetadataAsStream(instance)
39+
.collect(Collectors.toMap(Entry::getKey, Entry::getValue));
40+
}
41+
}

‎impl/persistence/bigmap/src/main/java/io/serverlessworkflow/impl/persistence/bigmap/BytesMapInstanceTransaction.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import io.serverlessworkflow.impl.persistence.hashing.HashItem;
4040
import io.serverlessworkflow.impl.persistence.hashing.HashMappingCoordinator;
4141
import io.serverlessworkflow.impl.persistence.hashing.HashMappingInfo;
42+
import io.serverlessworkflow.impl.persistence.metadata.PersistenceMetaUtils;
4243
import java.io.ByteArrayInputStream;
4344
import java.io.ByteArrayOutputStream;
4445
import java.io.IOException;
@@ -130,7 +131,7 @@ protected byte[] marshallTaskCompleted(WorkflowContextData contextData, TaskCont
130131
}
131132

132133
private void writeMetadata(WorkflowInstanceData instanceData, WorkflowOutputBuffer writer) {
133-
Map<String, Object> additionalObjects = new HashMap<>(instanceData.metadata());
134+
Map<String, Object> additionalObjects = PersistenceMetaUtils.durableMetadata(instanceData);
134135
writer.writeInt(additionalObjects.size());
135136
additionalObjects.forEach(
136137
(k, v) -> {

‎impl/persistence/tests/src/main/java/io/serverlessworkflow/impl/persistence/test/AbstractHandlerPersistenceTest.java‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import io.serverlessworkflow.impl.executors.TransitionInfo;
3838
import io.serverlessworkflow.impl.persistence.PersistenceInstanceHandlers;
3939
import io.serverlessworkflow.impl.persistence.WorkflowPersistenceInstance;
40+
import io.serverlessworkflow.impl.persistence.metadata.MetaTransient;
4041
import java.io.IOException;
4142
import java.time.Duration;
4243
import java.time.Instant;
@@ -52,6 +53,9 @@
5253

5354
public abstract class AbstractHandlerPersistenceTest {
5455

56+
@MetaTransient
57+
private record TransientMeta(String useless) {}
58+
5559
private PersistenceInstanceHandlers handlers;
5660
private static WorkflowApplication app;
5761
private static WorkflowDefinition definition;
@@ -76,7 +80,8 @@ void setup() {
7680
when(workflowContext.context()).thenReturn(context);
7781
when(workflowContext.definition()).thenReturn(definition);
7882
when(workflowContext.instanceData()).thenReturn(workflowInstance);
79-
when(workflowInstance.metadata()).thenReturn(Map.of("Javierito", "rules"));
83+
when(workflowInstance.metadata())
84+
.thenReturn(Map.of("Javierito", "rules", "ignored", new TransientMeta("ignored")));
8085
when(workflowInstance.startedAt()).thenReturn(beforeStart.plus(Duration.ofMillis(1)));
8186
when(workflowInstance.context()).thenReturn(context);
8287
when(workflowInstance.id()).thenReturn(app.idFactory().get());

0 commit comments

Comments
 (0)