@@ -34,31 +34,29 @@ func marshalEventWithoutAttributes(event *history.Event) (string, error) {
3434 return string (data ), nil
3535}
3636
37- // KEYS[1..n] - payload keys
37+ // KEYS[1 - payload key
3838// ARGV[1..n] - payload values
3939var addPayloadsCmd = redis .NewScript (`
40- for i = 1, #ARGV do
41- redis.pcall("SET ", KEYS[i ], ARGV[i], "NX" )
40+ for i = 1, #ARGV, 2 do
41+ redis.pcall("HSETNX ", KEYS[1 ], ARGV[i], ARGV[i+1] )
4242 end
4343
4444 return 0
4545` )
4646
47- func addEventPayloads (ctx context.Context , p redis.Pipeliner , events []* history.Event ) error {
48- keys := make ([]string , 0 )
49- values := make ([]interface {}, 0 )
47+ func addEventPayloadsP (ctx context.Context , p redis.Pipeliner , instance * core.WorkflowInstance , events []* history.Event ) error {
48+ args := make ([]interface {}, 0 )
5049
5150 for _ , event := range events {
5251 payload , err := json .Marshal (event .Attributes )
5352 if err != nil {
5453 return fmt .Errorf ("marshaling event payload: %w" , err )
5554 }
5655
57- keys = append (keys , payloadKey (event .ID ))
58- values = append (values , string (payload ))
56+ args = append (args , event .ID , string (payload ))
5957 }
6058
61- return addPayloadsCmd .Run (ctx , p , keys , values ... ).Err ()
59+ return addPayloadsCmd .Run (ctx , p , [] string { payloadKey ( instance )}, args ... ).Err ()
6260}
6361
6462func addEventToStreamP (ctx context.Context , p redis.Pipeliner , streamKey string , event * history.Event ) error {
@@ -110,15 +108,16 @@ func addEventsToStreamP(ctx context.Context, p redis.Pipeliner, streamKey string
110108// Adds an event to be delivered in the future. Not cluster-safe.
111109// KEYS[1] - future event zset key
112110// KEYS[2] - future event key
113- // KEYS[3] - future event payload key
111+ // KEYS[3] - instance payload key
114112// ARGV[1] - timestamp
115113// ARGV[2] - Instance segment
116- // ARGV[3] - event data
117- // ARGV[4] - event payload
114+ // ARGV[3] - event id
115+ // ARGV[4] - event data
116+ // ARGV[5] - event payload
118117var addFutureEventCmd = redis .NewScript (`
119118 redis.call("ZADD", KEYS[1], ARGV[1], KEYS[2])
120- redis.call("HSET", KEYS[2], "instance", ARGV[2], "event ", ARGV[3], "payload ", KEYS[3 ])
121- redis.call("SET ", KEYS[3], ARGV[4 ], "NX" )
119+ redis.call("HSET", KEYS[2], "instance", ARGV[2], "id ", ARGV[3], "event ", ARGV[4 ])
120+ redis.call("HSETNX ", KEYS[3], ARGV[3 ], ARGV[5] )
122121 return 0
123122` )
124123
@@ -135,9 +134,10 @@ func addFutureEventP(ctx context.Context, p redis.Pipeliner, instance *core.Work
135134
136135 return addFutureEventCmd .Run (
137136 ctx , p ,
138- []string {futureEventsKey (), futureEventKey (instance , event .ScheduleEventID ), payloadKey (event . ID )},
137+ []string {futureEventsKey (), futureEventKey (instance , event .ScheduleEventID ), payloadKey (instance )},
139138 strconv .FormatInt (event .VisibleAt .UnixMilli (), 10 ),
140139 instanceSegment (instance ),
140+ event .ID ,
141141 string (eventData ),
142142 string (payloadEventData ),
143143 ).Err ()
@@ -146,15 +146,16 @@ func addFutureEventP(ctx context.Context, p redis.Pipeliner, instance *core.Work
146146// Remove a scheduled future event. Not cluster-safe.
147147// KEYS[1] - future event zset key
148148// KEYS[2] - future event key
149+ // KEYS[3] - instance payload key
149150var removeFutureEventCmd = redis .NewScript (`
150151 redis.call("ZREM", KEYS[1], KEYS[2])
151- local k = redis.call("HGET", KEYS[2], "payload ")
152- redis.call("DEL ", k )
152+ local eventID = redis.call("HGET", KEYS[2], "id ")
153+ redis.call("HDEL ", KEYS[3], eventID )
153154 return redis.call("DEL", KEYS[2])
154155` )
155156
156157// removeFutureEvent removes a scheduled future event for the given event. Events are associated via their ScheduleEventID
157158func removeFutureEventP (ctx context.Context , p redis.Pipeliner , instance * core.WorkflowInstance , event * history.Event ) {
158159 key := futureEventKey (instance , event .ScheduleEventID )
159- removeFutureEventCmd .Run (ctx , p , []string {futureEventsKey (), key })
160+ removeFutureEventCmd .Run (ctx , p , []string {futureEventsKey (), key , payloadKey ( instance ) })
160161}
0 commit comments