Skip to content

Commit e189af3

Browse files
committed
fix(coderd/database): reduce db load via dbpurge advisory locking
1 parent 8a12162 commit e189af3

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

coderd/database/dbpurge/dbpurge.go

+31-16
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ package dbpurge
22

33
import (
44
"context"
5-
"errors"
65
"io"
76
"time"
87

9-
"golang.org/x/sync/errgroup"
8+
"golang.org/x/xerrors"
109

1110
"cdr.dev/slog"
1211

@@ -35,22 +34,38 @@ func New(ctx context.Context, logger slog.Logger, db database.Store) io.Closer {
3534
doTick := func() {
3635
defer ticker.Reset(delay)
3736

38-
var eg errgroup.Group
39-
eg.Go(func() error {
40-
return db.DeleteOldWorkspaceAgentLogs(ctx)
41-
})
42-
eg.Go(func() error {
43-
return db.DeleteOldWorkspaceAgentStats(ctx)
44-
})
45-
eg.Go(func() error {
46-
return db.DeleteOldProvisionerDaemons(ctx)
47-
})
48-
err := eg.Wait()
49-
if err != nil {
50-
if errors.Is(err, context.Canceled) {
51-
return
37+
start := time.Now()
38+
// Start a transaction to grab advisory lock, we don't want to run
39+
// multiple purges at the same time (multiple replicas). The purge
40+
// queries themselves are run outside of the transaction.
41+
if err := db.InTx(func(tx database.Store) error {
42+
// Acquire a lock to ensure that only one instance of
43+
// the rollup is running at a time.
44+
ok, err := tx.TryAcquireLock(ctx, database.LockIDDBPurge)
45+
if err != nil {
46+
return err
47+
}
48+
if !ok {
49+
logger.Debug(ctx, "unable to acquire lock for purging old database entries, skipping")
50+
return nil
51+
}
52+
53+
if err := tx.DeleteOldWorkspaceAgentLogs(ctx); err != nil {
54+
return xerrors.Errorf("failed to delete old workspace agent logs: %w", err)
5255
}
56+
if err := tx.DeleteOldWorkspaceAgentStats(ctx); err != nil {
57+
return xerrors.Errorf("failed to delete old workspace agent stats: %w", err)
58+
}
59+
if err := tx.DeleteOldProvisionerDaemons(ctx); err != nil {
60+
return xerrors.Errorf("failed to delete old provisioner daemons: %w", err)
61+
}
62+
63+
logger.Info(ctx, "purged old database entries", slog.F("duration", time.Since(start)))
64+
65+
return nil
66+
}, nil); err != nil {
5367
logger.Error(ctx, "failed to purge old database entries", slog.Error(err))
68+
return
5469
}
5570
}
5671

coderd/database/lock.go

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const (
99
LockIDDeploymentSetup = iota + 1
1010
LockIDEnterpriseDeploymentSetup
1111
LockIDDBRollup
12+
LockIDDBPurge
1213
)
1314

1415
// GenLockID generates a unique and consistent lock ID from a given string.

0 commit comments

Comments
 (0)