|
| 1 | +package dbrollup |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "time" |
| 6 | + |
| 7 | + "golang.org/x/sync/errgroup" |
| 8 | + |
| 9 | + "cdr.dev/slog" |
| 10 | + |
| 11 | + "github.com/coder/coder/v2/coderd/database" |
| 12 | + "github.com/coder/coder/v2/coderd/database/dbauthz" |
| 13 | +) |
| 14 | + |
| 15 | +const ( |
| 16 | + // DefaultInterval is the default time between rollups. |
| 17 | + // Rollups will be synchronized with the clock so that |
| 18 | + // they happen 13:00, 13:05, 13:10, etc. |
| 19 | + DefaultInterval = 5 * time.Minute |
| 20 | +) |
| 21 | + |
| 22 | +type Rolluper struct { |
| 23 | + cancel context.CancelFunc |
| 24 | + closed chan struct{} |
| 25 | + db database.Store |
| 26 | + logger slog.Logger |
| 27 | +} |
| 28 | + |
| 29 | +// New creates a new DB rollup service that periodically runs rollup queries. |
| 30 | +// It is the caller's responsibility to call Close on the returned instance. |
| 31 | +// |
| 32 | +// This is for e.g. generating insights data (template_usage_stats) from |
| 33 | +// raw data (workspace_agent_stats, workspace_app_stats). |
| 34 | +func New(logger slog.Logger, db database.Store, interval time.Duration) *Rolluper { |
| 35 | + ctx, cancel := context.WithCancel(context.Background()) |
| 36 | + |
| 37 | + r := &Rolluper{ |
| 38 | + cancel: cancel, |
| 39 | + closed: make(chan struct{}), |
| 40 | + db: db, |
| 41 | + logger: logger.Named("dbrollup"), |
| 42 | + } |
| 43 | + |
| 44 | + //nolint:gocritic // The system rolls up database tables without user input. |
| 45 | + ctx = dbauthz.AsSystemRestricted(ctx) |
| 46 | + go r.start(ctx, interval) |
| 47 | + |
| 48 | + return r |
| 49 | +} |
| 50 | + |
| 51 | +func (r *Rolluper) start(ctx context.Context, interval time.Duration) { |
| 52 | + defer close(r.closed) |
| 53 | + |
| 54 | + do := func() { |
| 55 | + var eg errgroup.Group |
| 56 | + |
| 57 | + r.logger.Debug(ctx, "rolling up data") |
| 58 | + now := time.Now() |
| 59 | + |
| 60 | + // Track whether or not we performed a rollup (we got the advisory lock). |
| 61 | + templateUsageStats := false |
| 62 | + |
| 63 | + eg.Go(func() error { |
| 64 | + return r.db.InTx(func(tx database.Store) error { |
| 65 | + // Acquire a lock to ensure that only one instance of |
| 66 | + // the rollup is running at a time. |
| 67 | + ok, err := tx.TryAcquireLock(ctx, database.LockIDDBRollup) |
| 68 | + if err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + if !ok { |
| 72 | + return nil |
| 73 | + } |
| 74 | + |
| 75 | + templateUsageStats = true |
| 76 | + return tx.UpsertTemplateUsageStats(ctx) |
| 77 | + }, nil) |
| 78 | + }) |
| 79 | + |
| 80 | + err := eg.Wait() |
| 81 | + if err != nil { |
| 82 | + if database.IsQueryCanceledError(err) { |
| 83 | + return |
| 84 | + } |
| 85 | + r.logger.Error(ctx, "failed to rollup data", slog.Error(err)) |
| 86 | + } else { |
| 87 | + r.logger.Debug(ctx, |
| 88 | + "rolled up data", |
| 89 | + slog.F("took", time.Since(now)), |
| 90 | + slog.F("template_usage_stats", templateUsageStats), |
| 91 | + ) |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + // Perform do immediately and on every tick of the ticker, |
| 96 | + // disregarding the execution time of do. This ensure that |
| 97 | + // the rollup is performed every interval assuming do does |
| 98 | + // not take longer than the interval to execute. |
| 99 | + t := time.NewTicker(time.Microsecond) |
| 100 | + defer t.Stop() |
| 101 | + for { |
| 102 | + select { |
| 103 | + case <-ctx.Done(): |
| 104 | + return |
| 105 | + case <-t.C: |
| 106 | + // Ensure we're on the interval. |
| 107 | + now := time.Now() |
| 108 | + next := now.Add(interval).Truncate(interval) // Ensure we're on the interval and synced with the clock. |
| 109 | + d := next.Sub(now) |
| 110 | + // Safety check. |
| 111 | + if d == 0 { |
| 112 | + d = interval |
| 113 | + } |
| 114 | + t.Reset(d) |
| 115 | + |
| 116 | + do() |
| 117 | + |
| 118 | + r.logger.Debug(ctx, "next rollup at", slog.F("next", next)) |
| 119 | + } |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +func (r *Rolluper) Close() error { |
| 124 | + r.cancel() |
| 125 | + <-r.closed |
| 126 | + return nil |
| 127 | +} |
0 commit comments