From 1f37eb4256464e73ae494f4dab34d831c2d073ca Mon Sep 17 00:00:00 2001 From: Aliaksandr Skurydzin Date: Fri, 2 Aug 2024 21:29:55 +0200 Subject: [PATCH] feat: sort state client cache keys before flush in some concurrent scenarios several cache keys can be updated by several routines, thus, in order to avoid the deadlocks (in postgresql for example) we should update keys in sorted order. --- internal/clients/state/v3/state.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/internal/clients/state/v3/state.go b/internal/clients/state/v3/state.go index 28985daae6..dd2d256d17 100644 --- a/internal/clients/state/v3/state.go +++ b/internal/clients/state/v3/state.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "io" + "sort" "sync" "github.com/apache/arrow/go/v17/arrow" @@ -168,7 +169,14 @@ func (c *Client) Flush(ctx context.Context) error { if c.versionedMode { version = bldr.Field(2).(*array.Uint64Builder) } + + changes := make([]string, 0, len(c.changes)) for k := range c.changes { + changes = append(changes, k) + } + sort.Strings(changes) + + for _, k := range changes { val := c.mem[k] keys.Append(k) values.Append(val.value)