Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pgdriver: preser order (do not read tx between snapshot xmin-xmax) #60

Merged
merged 3 commits into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions pgdriver/get_states_by_labels_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,21 @@ func (*queries) GetStatesByLabels(ctx context.Context, tx conntx, orLabels []map

q := `
SELECT state, rev
FROM flowstate_states
WHERE ` + where + `
ORDER BY "rev" ASC LIMIT ` + strconv.Itoa(len(ss)) + `;`
FROM
(
SELECT xmin::text::bigint, state, rev
FROM flowstate_states
WHERE ` + where + `
ORDER BY "rev" ASC LIMIT ` + strconv.Itoa(len(ss)) + `
) AS subquery
CROSS JOIN (
SELECT
split_part(pg_current_snapshot()::text, ':', 1)::bigint AS xmin,
split_part(pg_current_snapshot()::text, ':', 2)::bigint AS xmax
) AS snapshot
WHERE subquery.xmin < snapshot.xmin OR subquery.xmin > snapshot.xmax
;
`

rows, err := tx.Query(ctx, q, args...)
if err != nil {
Expand Down
59 changes: 59 additions & 0 deletions pgdriver/get_states_by_labels_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -684,4 +684,63 @@ func TestQuery_GetStatesByLabels(main *testing.T) {
{ID: `4`, Rev: 4, Labels: map[string]string{`bar`: `barVal`}},
}, ss)
})

main.Run("PreserveOrder", func(t *testing.T) {
conn := openDB(t, `postgres://postgres:postgres@localhost:5432/postgres`, ``)

q := &queries{}

// past
require.NoError(t, q.InsertState(context.Background(), conn, &flowstate.State{ID: `1`}))

// still active
tx0, err := conn.Begin(context.Background())
require.NoError(t, err)
defer tx0.Rollback(context.Background())
require.NoError(t, q.InsertState(context.Background(), tx0, &flowstate.State{ID: `2`}))

// still active
tx1, err := conn.Begin(context.Background())
require.NoError(t, err)
defer tx1.Rollback(context.Background())
require.NoError(t, q.InsertState(context.Background(), tx1, &flowstate.State{ID: `3`}))

// commited but should not be visible
tx2, err := conn.Begin(context.Background())
require.NoError(t, err)
defer tx2.Rollback(context.Background())
require.NoError(t, q.InsertState(context.Background(), tx2, &flowstate.State{ID: `4`}))
require.NoError(t, tx2.Commit(context.Background()))

ss := make([]flowstate.State, 4)
ss, err = q.GetStatesByLabels(context.Background(), conn, nil, int64(0), ss)
require.NoError(t, err)
require.Equal(t, []flowstate.State{
{ID: `1`, Rev: 1},
}, ss)

// now, we should see 2
require.NoError(t, tx0.Commit(context.Background()))

ss = make([]flowstate.State, 4)
ss, err = q.GetStatesByLabels(context.Background(), conn, nil, int64(0), ss)
require.NoError(t, err)
require.Equal(t, []flowstate.State{
{ID: `1`, Rev: 1},
{ID: `2`, Rev: 2},
}, ss)

// now, we should everything
require.NoError(t, tx1.Commit(context.Background()))

ss = make([]flowstate.State, 4)
ss, err = q.GetStatesByLabels(context.Background(), conn, nil, int64(0), ss)
require.NoError(t, err)
require.Equal(t, []flowstate.State{
{ID: `1`, Rev: 1},
{ID: `2`, Rev: 2},
{ID: `3`, Rev: 3},
{ID: `4`, Rev: 4},
}, ss)
})
}
Loading