1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
15 "git.arvados.org/arvados.git/sdk/go/ctxlog"
16 "github.com/jmoiron/sqlx"
20 TrashSweep = &DBLocker{key: 10001}
21 ContainerLogSweep = &DBLocker{key: 10002}
22 KeepBalanceService = &DBLocker{key: 10003} // keep-balance service in periodic-sweep loop
23 KeepBalanceActive = &DBLocker{key: 10004} // keep-balance sweep in progress (either -once=true or service loop)
24 Dispatch = &DBLocker{key: 10005} // any dispatcher running
25 retryDelay = 5 * time.Second
28 // DBLocker uses pg_advisory_lock to maintain a cluster-wide lock for
29 // a long-running task like "do X every N seconds".
30 type DBLocker struct {
34 getdb func(context.Context) (*sqlx.DB, error)
35 conn *sql.Conn // != nil if advisory lock has been acquired
38 // Lock acquires the advisory lock, waiting/reconnecting if needed.
40 // Returns false if ctx is canceled before the lock is acquired.
41 func (dbl *DBLocker) Lock(ctx context.Context, getdb func(context.Context) (*sqlx.DB, error)) bool {
42 logger := ctxlog.FromContext(ctx).WithField("ID", dbl.key)
44 for ; ; time.Sleep(retryDelay) {
47 // Another goroutine is already locked/waiting
48 // on this lock. Wait for them to release.
57 if err == context.Canceled {
60 } else if err != nil {
61 logger.WithError(err).Info("error getting database pool")
65 conn, err := db.Conn(ctx)
66 if err == context.Canceled {
69 } else if err != nil {
70 logger.WithError(err).Info("error getting database connection")
75 err = conn.QueryRowContext(ctx, `SELECT pg_try_advisory_lock($1)`, dbl.key).Scan(&locked)
76 if err == context.Canceled {
78 } else if err != nil {
79 logger.WithError(err).Info("error getting pg_try_advisory_lock")
87 err = conn.QueryRowContext(ctx, `SELECT client_addr, client_port FROM pg_stat_activity WHERE pid IN
88 (SELECT pid FROM pg_locks
89 WHERE locktype = $1 AND objid = $2)`, "advisory", dbl.key).Scan(&host, &port)
91 logger.WithError(err).Info("error getting other client info")
93 heldBy := net.JoinHostPort(host, fmt.Sprintf("%d", port))
94 if lastHeldBy != heldBy {
95 logger.WithField("DBClient", heldBy).Info("waiting for other process to release lock")
103 logger.Debug("acquired pg_advisory_lock")
104 dbl.ctx, dbl.getdb, dbl.conn = ctx, getdb, conn
110 // Check confirms that the lock is still active (i.e., the session is
111 // still alive), and re-acquires if needed. Panics if Lock is not
114 // Returns false if the context passed to Lock() is canceled before
115 // the lock is confirmed or reacquired.
116 func (dbl *DBLocker) Check() bool {
118 err := dbl.conn.PingContext(dbl.ctx)
119 if err == context.Canceled {
122 } else if err == nil {
123 ctxlog.FromContext(dbl.ctx).WithField("ID", dbl.key).Debug("connection still alive")
127 ctxlog.FromContext(dbl.ctx).WithError(err).Info("database connection ping failed")
130 ctx, getdb := dbl.ctx, dbl.getdb
132 return dbl.Lock(ctx, getdb)
135 func (dbl *DBLocker) Unlock() {
137 defer dbl.mtx.Unlock()
139 _, err := dbl.conn.ExecContext(context.Background(), `SELECT pg_advisory_unlock($1)`, dbl.key)
141 ctxlog.FromContext(dbl.ctx).WithError(err).WithField("ID", dbl.key).Info("error releasing pg_advisory_lock")
143 ctxlog.FromContext(dbl.ctx).WithField("ID", dbl.key).Debug("released pg_advisory_lock")