forked from buraksezer/olric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdmap_lock.go
255 lines (234 loc) · 6.79 KB
/
dmap_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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Copyright 2018 Burak Sezer
//
// 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 olric
import (
"context"
"time"
"github.com/buraksezer/olric/internal/protocol"
)
func (db *Olric) findLockKey(hkey uint64, name, key string) (host, error) {
part := db.getPartition(hkey)
part.RLock()
defer part.RUnlock()
if len(part.owners) == 0 {
panic("partition owners list cannot be empty")
}
if len(part.owners) == 1 {
if hostCmp(db.this, part.owners[0]) {
return db.this, nil
}
}
for i := 1; i <= len(part.owners); i++ {
// Traverse in reverse order.
idx := len(part.owners) - i
owner := part.owners[idx]
if hostCmp(db.this, owner) {
continue
}
req := &protocol.Message{
DMap: name,
Key: key,
}
_, err := db.requestTo(owner.String(), protocol.OpFindLock, req)
if err == nil {
return owner, nil
}
if err == ErrNoSuchLock {
err = nil
}
if err != nil {
return host{}, err
}
}
return db.this, nil
}
// Wait until the timeout is exceeded and background and release the key if it's still locked.
func (db *Olric) waitLockForTimeout(dm *dmap, key string, timeout time.Duration) {
defer db.wg.Done()
unlockCh := dm.locker.unlockNotifier(key)
select {
case <-time.After(timeout):
case <-db.ctx.Done():
case <-unlockCh:
// It's already unlocked
return
}
err := dm.locker.unlock(key)
if err == ErrNoSuchLock {
err = nil
}
if err != nil {
db.log.Printf("[ERROR] Failed to unlock key: %s", key)
}
}
func (db *Olric) lockKey(hkey uint64, name, key string, timeout time.Duration) error {
dm, err := db.getDMap(name, hkey)
if err != nil {
return err
}
if dm.locker.check(key) {
dm.locker.lock(key)
db.wg.Add(1)
go db.waitLockForTimeout(dm, key, timeout)
return nil
}
// Find the key or lock among previous owners, if any.
owner, err := db.findLockKey(hkey, name, key)
if err != nil {
return err
}
// One of the previous owners has the key, redirect the call.
if !hostCmp(db.this, owner) {
req := &protocol.Message{
DMap: name,
Key: key,
Extra: protocol.LockWithTimeoutExtra{TTL: timeout.Nanoseconds()},
}
_, err := db.requestTo(owner.String(), protocol.OpLockPrev, req)
return err
}
// This node owns the key/lock. Try to acquire it.
dm.locker.lock(key)
// Wait until the timeout is exceeded and background and release the key if
// it's still locked.
db.wg.Add(1)
go db.waitLockForTimeout(dm, key, timeout)
return nil
}
func (db *Olric) lockWithTimeout(name, key string, timeout time.Duration) error {
member, hkey, err := db.locateKey(name, key)
if err != nil {
return err
}
if !hostCmp(member, db.this) {
req := &protocol.Message{
DMap: name,
Key: key,
Extra: protocol.LockWithTimeoutExtra{TTL: timeout.Nanoseconds()},
}
_, err = db.requestTo(member.String(), protocol.OpExLockWithTimeout, req)
return err
}
return db.lockKey(hkey, name, key, timeout)
}
// LockWithTimeout sets a lock for the given key. If the lock is still unreleased the end of given period of time,
// it automatically releases the lock. Acquired lock is only for the key in this map.
//
// It returns immediately if it acquires the lock for the given key. Otherwise, it waits until timeout.
// The timeout is determined by http.Client which can be configured via Config structure.
//
// You should know that the locks are approximate, and only to be used for non-critical purposes.
func (dm *DMap) LockWithTimeout(key string, timeout time.Duration) error {
return dm.db.lockWithTimeout(dm.name, key, timeout)
}
func (db *Olric) unlockKey(hkey uint64, name, key string) error {
owner, err := db.findLockKey(hkey, name, key)
if err != nil {
return err
}
if !hostCmp(db.this, owner) {
req := &protocol.Message{
DMap: name,
Key: key,
}
_, err = db.requestTo(owner.String(), protocol.OpUnlockPrev, req)
return err
}
dm, err := db.getDMap(name, hkey)
if err != nil {
return err
}
return dm.locker.unlock(key)
}
func (db *Olric) unlock(name, key string) error {
<-db.bcx.Done()
if db.bcx.Err() == context.DeadlineExceeded {
return ErrOperationTimeout
}
member, hkey, err := db.locateKey(name, key)
if err != nil {
return err
}
if !hostCmp(member, db.this) {
req := &protocol.Message{
DMap: name,
Key: key,
}
_, err = db.requestTo(member.String(), protocol.OpExUnlock, req)
return err
}
return db.unlockKey(hkey, name, key)
}
// Unlock releases an acquired lock for the given key. It returns ErrNoSuchLock if there is no lock for the given key.
func (dm *DMap) Unlock(key string) error {
return dm.db.unlock(dm.name, key)
}
func (db *Olric) exLockWithTimeoutOperation(req *protocol.Message) *protocol.Message {
ttl := req.Extra.(protocol.LockWithTimeoutExtra).TTL
err := db.lockWithTimeout(req.DMap, req.Key, time.Duration(ttl))
if err != nil {
return req.Error(protocol.StatusInternalServerError, err)
}
return req.Success()
}
func (db *Olric) exUnlockOperation(req *protocol.Message) *protocol.Message {
err := db.unlock(req.DMap, req.Key)
if err == ErrNoSuchLock {
return req.Error(protocol.StatusNoSuchLock, "")
}
if err != nil {
return req.Error(protocol.StatusInternalServerError, err)
}
return req.Success()
}
func (db *Olric) findLockOperation(req *protocol.Message) *protocol.Message {
hkey := db.getHKey(req.DMap, req.Key)
dm, err := db.getDMap(req.DMap, hkey)
if err != nil {
return req.Error(protocol.StatusInternalServerError, err)
}
if dm.locker.check(req.Key) {
return req.Success()
}
return req.Error(protocol.StatusNoSuchLock, "")
}
func (db *Olric) unlockPrevOperation(req *protocol.Message) *protocol.Message {
key := req.Key
hkey := db.getHKey(req.DMap, key)
dm, err := db.getDMap(req.DMap, hkey)
if err != nil {
return req.Error(protocol.StatusInternalServerError, err)
}
err = dm.locker.unlock(key)
if err == ErrNoSuchLock {
return req.Error(protocol.StatusNoSuchLock, "")
}
if err != nil {
return req.Error(protocol.StatusInternalServerError, err)
}
return req.Success()
}
func (db *Olric) lockPrevOperation(req *protocol.Message) *protocol.Message {
key := req.Key
hkey := db.getHKey(req.DMap, key)
dm, err := db.getDMap(req.DMap, hkey)
if err != nil {
return req.Error(protocol.StatusInternalServerError, err)
}
dm.locker.lock(key)
db.wg.Add(1)
ttl := req.Extra.(protocol.LockWithTimeoutExtra).TTL
go db.waitLockForTimeout(dm, key, time.Duration(ttl))
return req.Success()
}