Skip to content

Commit

Permalink
fix(coderd/database): reduce db load via dbpurge advisory locking (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
mafredri committed Apr 22, 2024
1 parent 8a12162 commit 3adcccb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
46 changes: 30 additions & 16 deletions coderd/database/dbpurge/dbpurge.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package dbpurge

import (
"context"
"errors"
"io"
"time"

"golang.org/x/sync/errgroup"
"golang.org/x/xerrors"

"cdr.dev/slog"

Expand Down Expand Up @@ -35,22 +34,37 @@ func New(ctx context.Context, logger slog.Logger, db database.Store) io.Closer {
doTick := func() {
defer ticker.Reset(delay)

var eg errgroup.Group
eg.Go(func() error {
return db.DeleteOldWorkspaceAgentLogs(ctx)
})
eg.Go(func() error {
return db.DeleteOldWorkspaceAgentStats(ctx)
})
eg.Go(func() error {
return db.DeleteOldProvisionerDaemons(ctx)
})
err := eg.Wait()
if err != nil {
if errors.Is(err, context.Canceled) {
return
start := time.Now()
// Start a transaction to grab advisory lock, we don't want to run
// multiple purges at the same time (multiple replicas).
if err := db.InTx(func(tx database.Store) error {
// Acquire a lock to ensure that only one instance of the
// purge is running at a time.
ok, err := tx.TryAcquireLock(ctx, database.LockIDDBPurge)
if err != nil {
return err
}
if !ok {
logger.Debug(ctx, "unable to acquire lock for purging old database entries, skipping")
return nil
}

if err := tx.DeleteOldWorkspaceAgentLogs(ctx); err != nil {
return xerrors.Errorf("failed to delete old workspace agent logs: %w", err)
}
if err := tx.DeleteOldWorkspaceAgentStats(ctx); err != nil {
return xerrors.Errorf("failed to delete old workspace agent stats: %w", err)
}
if err := tx.DeleteOldProvisionerDaemons(ctx); err != nil {
return xerrors.Errorf("failed to delete old provisioner daemons: %w", err)
}

logger.Info(ctx, "purged old database entries", slog.F("duration", time.Since(start)))

return nil
}, nil); err != nil {
logger.Error(ctx, "failed to purge old database entries", slog.Error(err))
return
}
}

Expand Down
1 change: 1 addition & 0 deletions coderd/database/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const (
LockIDDeploymentSetup = iota + 1
LockIDEnterpriseDeploymentSetup
LockIDDBRollup
LockIDDBPurge
)

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

0 comments on commit 3adcccb

Please sign in to comment.