-
Notifications
You must be signed in to change notification settings - Fork 496
/
verify_lock.go
38 lines (33 loc) · 1.28 KB
/
verify_lock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package history
import (
"context"
"github.com/stellar/go/support/db"
"github.com/stellar/go/support/errors"
)
// stateVerificationLockId is the objid for the advisory lock acquired during
// state verification. The value is arbitrary. The only requirement is that
// all ingesting nodes use the same value which is why it's hard coded here.
const stateVerificationLockId = 73897213
// TryStateVerificationLock attempts to acquire the state verification lock
// which gives the ingesting node exclusive access to perform state verification.
// TryStateVerificationLock returns true if the lock was acquired or false if the
// lock could not be acquired because it is held by another node.
func (q *Q) TryStateVerificationLock(ctx context.Context) (bool, error) {
if tx := q.GetTx(); tx == nil {
return false, errors.New("cannot be called outside of a transaction")
}
var acquired []bool
err := q.SelectRaw(
context.WithValue(ctx, &db.QueryTypeContextKey, db.AdvisoryLockQueryType),
&acquired,
"SELECT pg_try_advisory_xact_lock(?)",
stateVerificationLockId,
)
if err != nil {
return false, errors.Wrap(err, "error acquiring advisory lock for state verification")
}
if len(acquired) != 1 {
return false, errors.New("invalid response from advisory lock")
}
return acquired[0], nil
}