This is an automated email from the ASF dual-hosted git repository.
lahirujayathilake pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/airavata-custos.git
The following commit(s) were added to refs/heads/master by this push:
new ac04977a6 Lock merged users FOR NO KEY UPDATE (#683)
ac04977a6 is described below
commit ac04977a6b223a9fe71b37af4101a53fe4da806c
Author: Yasith Jayawardana <[email protected]>
AuthorDate: Wed Nov 4 17:10:43 2026 -0500
Lock merged users FOR NO KEY UPDATE (#683)
---
internal/store/store.go | 3 ++-
internal/store/user_store.go | 2 +-
pkg/service/user_merge_integration_test.go | 43 ++++++++++++++++++++++++++++++
3 files changed, 46 insertions(+), 2 deletions(-)
diff --git a/internal/store/store.go b/internal/store/store.go
index 75e8c7fd6..19b40565c 100644
--- a/internal/store/store.go
+++ b/internal/store/store.go
@@ -29,7 +29,8 @@ import (
type UserStore interface {
// FindByID returns the user with the given ID, or nil if not found.
FindByID(ctx context.Context, id string) (*models.User, error)
- // FindByIDTx returns the user locked FOR UPDATE in tx, or nil if not
found.
+ // FindByIDTx returns the user locked FOR NO KEY UPDATE in tx, or nil
if not found.
+ // That still lets other transactions insert rows that reference the
user.
FindByIDTx(ctx context.Context, tx *sql.Tx, id string) (*models.User,
error)
// FindByEmail returns the user with the given email, or nil if not
found.
FindByEmail(ctx context.Context, email string) (*models.User, error)
diff --git a/internal/store/user_store.go b/internal/store/user_store.go
index d36e0c643..7d689293b 100644
--- a/internal/store/user_store.go
+++ b/internal/store/user_store.go
@@ -55,7 +55,7 @@ func (s *pgUserStore) FindByID(ctx context.Context, id
string) (*models.User, er
func (s *pgUserStore) FindByIDTx(ctx context.Context, tx *sql.Tx, id string)
(*models.User, error) {
var r models.User
err := (&sqlx.Tx{Tx: tx, Mapper: s.db.Mapper}).GetContext(ctx, &r,
- `SELECT `+userColumns+` FROM users WHERE id = $1 FOR UPDATE`,
id)
+ `SELECT `+userColumns+` FROM users WHERE id = $1 FOR NO KEY
UPDATE`, id)
if errors.Is(err, sql.ErrNoRows) {
return nil, nil
}
diff --git a/pkg/service/user_merge_integration_test.go
b/pkg/service/user_merge_integration_test.go
index a5438f583..2aaf8fd90 100644
--- a/pkg/service/user_merge_integration_test.go
+++ b/pkg/service/user_merge_integration_test.go
@@ -25,6 +25,7 @@ import (
"fmt"
"strings"
"testing"
+ "time"
"github.com/google/uuid"
@@ -136,6 +137,48 @@ func TestMergeUsers_KeepsHigherTagAndPIHoldsNoTag(t
*testing.T) {
}
}
+// Make sure a merge waiting on a cluster account another transaction holds
+// does not block that transaction from inserting a row that references the
user.
+func TestMergeUsers_LetsALockHolderReferenceTheUser(t *testing.T) {
+ database := setupTestDB(t)
+ svc := newTestService(database)
+ survivor := seedUser(t, database,
fmt.Sprintf("[email protected]", uuid.NewString()))
+ cu := newClusterUser(t, svc, database)
+
+ hold, err := database.Begin()
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer func() { _ = hold.Rollback() }()
+ if _, err := hold.Exec("SELECT id FROM compute_cluster_users WHERE id =
$1 FOR UPDATE", cu.ID); err != nil {
+ t.Fatal(err)
+ }
+ merged := make(chan error, 1)
+ go func() {
+ _, err := svc.MergeUsers(ctx(), survivor, cu.UserID)
+ merged <- err
+ }()
+ for n, deadline := 0, time.Now().Add(5*time.Second); n < 1;
time.Sleep(10 * time.Millisecond) {
+ if time.Now().After(deadline) {
+ t.Fatal("merge never blocked on the cluster account
lock")
+ }
+ if err := database.Get(&n, "SELECT count(*) FROM
pg_stat_activity WHERE wait_event_type = 'Lock'"); err != nil {
+ t.Fatal(err)
+ }
+ }
+
+ if _, err := hold.Exec("INSERT INTO user_identities (id, user_id,
source, external_id) VALUES ($1, $2, 'test', $3)",
+ uuid.NewString(), cu.UserID, uuid.NewString()); err != nil {
+ t.Fatalf("insert identity while the merge waits: %v", err)
+ }
+ if err := hold.Commit(); err != nil {
+ t.Fatal(err)
+ }
+ if err := <-merged; err != nil {
+ t.Fatalf("merge: %v", err)
+ }
+}
+
func TestDeleteUser_RemovesTheirAllocationMemberships(t *testing.T) {
database := setupTestDB(t)
svc := newTestService(database)