2424import io .serverlessworkflow .impl .WorkflowMutableInstance ;
2525import io .serverlessworkflow .impl .WorkflowStatus ;
2626import io .serverlessworkflow .impl .executors .TransitionInfo ;
27+ import java .util .HashMap ;
28+ import java .util .HashSet ;
2729import java .util .Optional ;
30+ import java .util .Set ;
2831import java .util .concurrent .CompletableFuture ;
32+ import java .util .function .Supplier ;
2933
3034public class WorkflowPersistenceInstance extends WorkflowMutableInstance {
3135
3236 private final PersistenceWorkflowInfo info ;
37+ private Set <String > userMetaKeys = new HashSet <>();
3338
3439 public static WorkflowInstance of (WorkflowDefinition definition , PersistenceWorkflowInfo info ) {
3540 return definition
@@ -48,7 +53,7 @@ private WorkflowPersistenceInstance(WorkflowDefinition definition, PersistenceWo
4853 }
4954 });
5055 this .startedAt = info .startedAt ();
51- additionalObjects = info .metadata ();
56+ additionalObjects = new HashMap <>( info .metadata () );
5257 }
5358
5459 @ Override
@@ -100,6 +105,43 @@ public void restoreContext(WorkflowContext workflow, TaskContext context) {
100105 searchContext = tryContext .parent ();
101106 }
102107 }
103- taskInfo .additionalObjects ().forEach (additionalObjects ::put );
108+ Set <String > retainKeys ;
109+ synchronized (userMetaKeys ) {
110+ retainKeys = new HashSet <>(userMetaKeys );
111+ taskInfo
112+ .additionalObjects ()
113+ .forEach (
114+ (k , v ) -> {
115+ if (!retainKeys .contains (k )) {
116+ additionalObjects .put (k , v );
117+ retainKeys .add (k );
118+ }
119+ });
120+ additionalObjects .keySet ().retainAll (retainKeys );
121+ }
122+ }
123+
124+ @ Override
125+ public <T > T addMetadataIfAbsent (String key , Supplier <T > supplier ) {
126+ synchronized (userMetaKeys ) {
127+ userMetaKeys .add (key );
128+ return super .addMetadataIfAbsent (key , supplier );
129+ }
130+ }
131+
132+ @ Override
133+ public void removeMetadata (String key ) {
134+ synchronized (userMetaKeys ) {
135+ userMetaKeys .add (key );
136+ super .removeMetadata (key );
137+ }
138+ }
139+
140+ @ Override
141+ public <T > Optional <T > removeMetadata (String key , Class <T > clazz ) {
142+ synchronized (userMetaKeys ) {
143+ userMetaKeys .add (key );
144+ return super .removeMetadata (key , clazz );
145+ }
104146 }
105147}
0 commit comments