From ff72590958889186ef9dfab1889f019dc1b5ea8f Mon Sep 17 00:00:00 2001 From: Neeraj Gupta <254676+ua741@users.noreply.github.com> Date: Mon, 12 Aug 2024 21:36:49 +0530 Subject: [PATCH] Fix bug in GetAndLockUnreplicatedObject query --- server/pkg/controller/filedata/replicate.go | 2 +- server/pkg/repo/object_copies.go | 23 +++++++++------------ 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/server/pkg/controller/filedata/replicate.go b/server/pkg/controller/filedata/replicate.go index 90afa6169e..668ae844aa 100644 --- a/server/pkg/controller/filedata/replicate.go +++ b/server/pkg/controller/filedata/replicate.go @@ -63,7 +63,7 @@ func (c *Controller) tryReplicate() error { row, err := c.Repo.GetPendingSyncDataAndExtendLock(ctx, newLockTime, false) if err != nil { if !errors.Is(err, sql.ErrNoRows) { - log.Errorf("Could not fetch row for deletion: %s", err) + log.Errorf("Could not fetch row for replication: %s", err) } return err } diff --git a/server/pkg/repo/object_copies.go b/server/pkg/repo/object_copies.go index 8d7be84571..fb2c585325 100644 --- a/server/pkg/repo/object_copies.go +++ b/server/pkg/repo/object_copies.go @@ -35,13 +35,6 @@ func (repo *ObjectCopiesRepository) GetAndLockUnreplicatedObject() (*ente.Object } } - commit := func() { - cerr := tx.Commit() - if cerr != nil { - log.Errorf("Ignoring error when committing transaction: %s", cerr) - } - } - row := tx.QueryRow(` SELECT object_key, want_b2, b2, want_wasabi, wasabi, want_scw, scw FROM object_copies @@ -59,20 +52,24 @@ func (repo *ObjectCopiesRepository) GetAndLockUnreplicatedObject() (*ente.Object err = row.Scan(&r.ObjectKey, &r.WantB2, &r.B2, &r.WantWasabi, &r.Wasabi, &r.WantSCW, &r.SCW) - if err != nil && errors.Is(err, sql.ErrNoRows) { - commit() - return nil, err - } - if err != nil { - rollback() + rollback() // Rollback transaction on any error + if errors.Is(err, sql.ErrNoRows) { + return nil, err // Return sql.ErrNoRows without committing the transaction + } return nil, stacktrace.Propagate(err, "") } + err = repo.RegisterReplicationAttempt(tx, r.ObjectKey) if err != nil { rollback() return nil, stacktrace.Propagate(err, "failed to register replication attempt") } + + err = tx.Commit() + if err != nil { + return nil, stacktrace.Propagate(err, "") + } return &r, nil }