-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathlease.go
173 lines (154 loc) · 5.26 KB
/
lease.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright 2016 The Cockroach Authors.
//
// 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 client
import (
"context"
"fmt"
"time"
"github.com/pkg/errors"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/cockroach/pkg/util/uuid"
)
// DefaultLeaseDuration is the duration a lease will be acquired for if no
// duration was specified in a LeaseManager's options.
// Exported for testing purposes.
const DefaultLeaseDuration = 1 * time.Minute
// LeaseNotAvailableError indicates that the lease the caller attempted to
// acquire is currently held by a different client.
type LeaseNotAvailableError struct {
key roachpb.Key
expiration hlc.Timestamp
}
func (e *LeaseNotAvailableError) Error() string {
return fmt.Sprintf("lease %q is not available until at least %s", e.key, e.expiration)
}
// LeaseManager provides functionality for acquiring and managing leases
// via the kv api.
type LeaseManager struct {
db *DB
clock *hlc.Clock
clientID string
leaseDuration time.Duration
}
// Lease contains the state of a lease on a particular key.
// Should only be passed by pointer, not by value.
type Lease struct {
key roachpb.Key
val struct {
syncutil.Mutex
lease *LeaseVal
}
}
// LeaseManagerOptions are used to configure a new LeaseManager.
type LeaseManagerOptions struct {
// ClientID must be unique to this LeaseManager instance.
ClientID string
LeaseDuration time.Duration
}
// NewLeaseManager allocates a new LeaseManager.
func NewLeaseManager(db *DB, clock *hlc.Clock, options LeaseManagerOptions) *LeaseManager {
if options.ClientID == "" {
options.ClientID = uuid.MakeV4().String()
}
if options.LeaseDuration <= 0 {
options.LeaseDuration = DefaultLeaseDuration
}
return &LeaseManager{
db: db,
clock: clock,
clientID: options.ClientID,
leaseDuration: options.LeaseDuration,
}
}
// AcquireLease attempts to grab a lease on the provided key. Returns a non-nil
// lease object if it was successful, or an error if it failed to acquire the
// lease for any reason.
//
// NB: Acquiring a non-expired lease is allowed if this LeaseManager's clientID
// matches the lease owner's ID. This behavior allows a process to re-grab
// leases without having to wait if it restarts and uses the same ID.
func (m *LeaseManager) AcquireLease(ctx context.Context, key roachpb.Key) (*Lease, error) {
lease := &Lease{
key: key,
}
if err := m.db.Txn(ctx, func(ctx context.Context, txn *Txn) error {
var val LeaseVal
err := txn.GetProto(ctx, key, &val)
if err != nil {
return err
}
if !m.leaseAvailable(&val) {
return &LeaseNotAvailableError{key: key, expiration: val.Expiration}
}
lease.val.lease = &LeaseVal{
Owner: m.clientID,
Expiration: m.clock.Now().Add(m.leaseDuration.Nanoseconds(), 0),
}
return txn.Put(ctx, key, lease.val.lease)
}); err != nil {
return nil, err
}
return lease, nil
}
func (m *LeaseManager) leaseAvailable(val *LeaseVal) bool {
return val.Owner == m.clientID || m.timeRemaining(val) <= 0
}
// TimeRemaining returns the amount of time left on the given lease.
func (m *LeaseManager) TimeRemaining(l *Lease) time.Duration {
l.val.Lock()
defer l.val.Unlock()
return m.timeRemaining(l.val.lease)
}
func (m *LeaseManager) timeRemaining(val *LeaseVal) time.Duration {
maxOffset := m.clock.MaxOffset()
if maxOffset == timeutil.ClocklessMaxOffset {
// Clockless reads are active, so we don't need to stop using the lease
// early.
maxOffset = 0
}
return val.Expiration.GoTime().Sub(m.clock.Now().GoTime()) - maxOffset
}
// ExtendLease attempts to push the expiration time of the lease farther out
// into the future.
func (m *LeaseManager) ExtendLease(ctx context.Context, l *Lease) error {
l.val.Lock()
defer l.val.Unlock()
if m.timeRemaining(l.val.lease) < 0 {
return errors.Errorf("can't extend lease that expired at time %s", l.val.lease.Expiration)
}
newVal := &LeaseVal{
Owner: m.clientID,
Expiration: m.clock.Now().Add(m.leaseDuration.Nanoseconds(), 0),
}
if err := m.db.CPut(ctx, l.key, newVal, l.val.lease); err != nil {
if _, ok := err.(*roachpb.ConditionFailedError); ok {
// Something is wrong - immediately expire the local lease state.
l.val.lease.Expiration = hlc.Timestamp{}
return errors.Wrapf(err, "local lease state %v out of sync with DB state", l.val.lease)
}
return err
}
l.val.lease = newVal
return nil
}
// ReleaseLease attempts to release the given lease so that another process can
// grab it.
func (m *LeaseManager) ReleaseLease(ctx context.Context, l *Lease) error {
l.val.Lock()
defer l.val.Unlock()
return m.db.CPut(ctx, l.key, nil, l.val.lease)
}