forked from jaegertracing/jaeger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lock.go
101 lines (89 loc) · 3.09 KB
/
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cassandra
import (
"time"
"github.com/pkg/errors"
"github.com/uber/jaeger/pkg/cassandra"
)
// Lock is a distributed lock based off Cassandra.
type Lock struct {
session cassandra.Session
tenantID string
}
const (
defaultTTL = 60 * time.Second
leasesTable = `leases`
cqlInsertLock = `INSERT INTO ` + leasesTable + ` (name, owner) VALUES (?,?) IF NOT EXISTS USING TTL ?;`
cqlUpdateLock = `UPDATE ` + leasesTable + ` USING TTL ? SET owner = ? WHERE name = ? IF owner = ?;`
cqlDeleteLock = `DELETE FROM ` + leasesTable + ` WHERE name = ? IF owner = ?;`
)
var (
errLockOwnership = errors.New("This host does not own the resource lock")
)
// NewLock creates a new instance of a distributed locking mechanism based off Cassandra.
func NewLock(session cassandra.Session, tenantID string) *Lock {
return &Lock{
session: session,
tenantID: tenantID,
}
}
// Acquire acquires a lease around a given resource. NB. Cassandra only allows ttl of seconds granularity
func (l *Lock) Acquire(resource string, ttl time.Duration) (bool, error) {
if ttl == 0 {
ttl = defaultTTL
}
var name, owner string
applied, err := l.session.Query(cqlInsertLock, resource, l.tenantID, ttl.Seconds()).ScanCAS(&name, &owner)
if err != nil {
return false, errors.Wrap(err, "Failed to acquire resource lock due to cassandra error")
}
if applied {
// The lock was successfully created
return true, nil
}
if owner == l.tenantID {
// This host already owns the lock, extend the lease
if err = l.extendLease(resource, ttl); err != nil {
return false, errors.Wrap(err, "Failed to extend lease on resource lock")
}
return true, nil
}
return false, nil
}
// Forfeit forfeits an existing lease around a given resource.
func (l *Lock) Forfeit(resource string) (bool, error) {
var name, owner string
applied, err := l.session.Query(cqlDeleteLock, resource, l.tenantID).ScanCAS(&name, &owner)
if err != nil {
return false, errors.Wrap(err, "Failed to forfeit resource lock due to cassandra error")
}
if applied {
// The lock was successfully deleted
return true, nil
}
return false, errors.Wrap(errLockOwnership, "Failed to forfeit resource lock")
}
// extendLease will attempt to extend the lease of an existing lock on a given resource.
func (l *Lock) extendLease(resource string, ttl time.Duration) error {
var owner string
applied, err := l.session.Query(cqlUpdateLock, ttl.Seconds(), l.tenantID, resource, l.tenantID).ScanCAS(&owner)
if err != nil {
return err
}
if applied {
return nil
}
return errLockOwnership
}