Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Add 10 second timeout while locking row for replication

Neeraj Gupta8299c029ec38
diff --git a/server/pkg/controller/replication3.go b/server/pkg/controller/replication3.go
index 99c81837a6..bacb2b4f99 100644
--- a/server/pkg/controller/replication3.go
+++ b/server/pkg/controller/replication3.go
@@ -1,6 +1,7 @@
package controller
import (
+ "context"
"database/sql"
"encoding/base64"
"errors"
@@ -222,7 +223,9 @@ func (c *ReplicationController3) replicate(i int) {
// objects left to replicate currently.
func (c *ReplicationController3) tryReplicate() error {
// Fetch an object to replicate
- copies, err := c.ObjectCopiesRepo.GetAndLockUnreplicatedObject()
+ ctxWithTimeout, cancel := context.WithTimeout(context.Background(), 10*time.Second)
+ defer cancel()
+ copies, err := c.ObjectCopiesRepo.GetAndLockUnreplicatedObject(ctxWithTimeout)
if err != nil {
if !errors.Is(err, sql.ErrNoRows) {
log.Errorf("Could not fetch an object to replicate: %s", err)
diff --git a/server/pkg/repo/object_copies.go b/server/pkg/repo/object_copies.go
index fb2c585325..7d6fe6f7e5 100644
--- a/server/pkg/repo/object_copies.go
+++ b/server/pkg/repo/object_copies.go
@@ -22,8 +22,8 @@ type ObjectCopiesRepository struct {
// to that object to be blocked for 24h before next replication attemp.
//
// ObjectCopies is guaranteed to be nil if error is not nil.
-func (repo *ObjectCopiesRepository) GetAndLockUnreplicatedObject() (*ente.ObjectCopies, error) {
- tx, err := repo.DB.Begin()
+func (repo *ObjectCopiesRepository) GetAndLockUnreplicatedObject(ctx context.Context) (*ente.ObjectCopies, error) {
+ tx, err := repo.DB.BeginTx(ctx, nil)
if err != nil {
return nil, stacktrace.Propagate(err, "")
}
@@ -35,7 +35,7 @@ func (repo *ObjectCopiesRepository) GetAndLockUnreplicatedObject() (*ente.Object
}
}
- row := tx.QueryRow(`
+ row := tx.QueryRowContext(ctx, `
SELECT object_key, want_b2, b2, want_wasabi, wasabi, want_scw, scw
FROM object_copies
WHERE (
@@ -60,7 +60,7 @@ func (repo *ObjectCopiesRepository) GetAndLockUnreplicatedObject() (*ente.Object
return nil, stacktrace.Propagate(err, "")
}
- err = repo.RegisterReplicationAttempt(tx, r.ObjectKey)
+ err = repo.RegisterReplicationAttempt(tx, ctx, r.ObjectKey)
if err != nil {
rollback()
return nil, stacktrace.Propagate(err, "failed to register replication attempt")
@@ -102,8 +102,8 @@ func (repo *ObjectCopiesRepository) CreateNewWasabiObject(ctx context.Context, t
// RegisterReplicationAttempt sets the last_attempt timestamp so that this row can
// be skipped over for the next day in case the replication was not succesful.
-func (repo *ObjectCopiesRepository) RegisterReplicationAttempt(tx *sql.Tx, objectKey string) error {
- _, err := tx.Exec(`
+func (repo *ObjectCopiesRepository) RegisterReplicationAttempt(tx *sql.Tx, ctx context.Context, objectKey string) error {
+ _, err := tx.ExecContext(ctx, `
UPDATE object_copies
SET last_attempt = now_utc_micro_seconds()
WHERE object_key = $1