-
Notifications
You must be signed in to change notification settings - Fork 2
/
memory.go
407 lines (344 loc) · 8.25 KB
/
memory.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
// MIT License
//
// Copyright (c) 2020 Lack
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// Package memory provides a sync.Mutex implementation of the lock for local use
package memory
import (
"context"
"errors"
gosync "sync"
"time"
"github.com/google/uuid"
"github.com/vine-io/vine/lib/sync"
)
var sendEventTime = 10 * time.Millisecond
type memorySync struct {
options sync.Options
leaderStore map[string]*memoryLeader
leaderMtx gosync.RWMutex
mtx gosync.RWMutex
locks map[string]*memoryLock
chMtx gosync.RWMutex
watchers map[string]map[string]*memoryElectWatcher
}
type memoryLock struct {
id string
time time.Time
ttl time.Duration
release chan bool
isLeader bool
}
func (m *memorySync) Init(opts ...sync.Option) error {
for _, o := range opts {
o(&m.options)
}
return nil
}
func (m *memorySync) Options() sync.Options {
return m.options
}
func (m *memorySync) Leader(ctx context.Context, name string, opts ...sync.LeaderOption) (sync.Leader, error) {
var once gosync.Once
var options sync.LeaderOptions
for _, o := range opts {
o(&options)
}
if options.Id == "" {
options.Id = uuid.New().String()
}
leader := &memoryLeader{
s: m,
opts: options,
name: name,
ns: options.Namespace,
id: options.Id,
role: sync.Follow,
resign: func(id string) error {
once.Do(func() {
m.Unlock(context.TODO(), id)
})
m.leaderMtx.Lock()
close(m.leaderStore[id].status)
delete(m.leaderStore, options.Id)
m.leaderMtx.Unlock()
return nil
},
// TODO: signal when Unlock is called
observe: make(chan sync.ObserveResult, 3),
status: make(chan bool, 1),
}
m.leaderMtx.Lock()
m.leaderStore[leader.id] = leader
m.leaderMtx.Unlock()
// acquire a lock for the id
if err := m.lock(name, true); err != nil {
return nil, err
}
m.leaderMtx.Lock()
leader.role = sync.Primary
m.leaderStore[leader.id] = leader
m.leaderMtx.Unlock()
m.leaderMtx.Lock()
for _, l := range m.leaderStore {
l.observe <- sync.ObserveResult{Namespace: options.Namespace, Id: options.Id}
}
m.leaderMtx.Unlock()
// return the leader
return leader, nil
}
func (m *memorySync) sendEvent(ns string, r *sync.Member) {
m.leaderMtx.RLock()
watchers := make([]*memoryElectWatcher, 0, len(m.watchers[ns]))
for _, w := range m.watchers[ns] {
watchers = append(watchers, w)
}
m.leaderMtx.RUnlock()
for _, w := range watchers {
select {
case <-w.exit:
m.leaderMtx.Lock()
delete(m.watchers[ns], w.wo.Id)
m.leaderMtx.Unlock()
default:
select {
case w.res <- r:
case <-time.After(sendEventTime):
}
}
}
}
func (m *memorySync) ListMembers(ctx context.Context, opts ...sync.ListMembersOption) ([]*sync.Member, error) {
var options sync.ListMembersOptions
for _, opt := range opts {
opt(&options)
}
m.leaderMtx.RLock()
defer m.leaderMtx.RUnlock()
members := make([]*sync.Member, 0)
for _, l := range m.leaderStore {
member := &sync.Member{
Leader: l.name,
Id: l.id,
Namespace: l.ns,
Role: l.role,
}
members = append(members, member)
}
return members, nil
}
func (m *memorySync) WatchElect(ctx context.Context, opts ...sync.WatchElectOption) (sync.ElectWatcher, error) {
var wo sync.WatchElectOptions
for _, o := range opts {
o(&wo)
}
if wo.Id == "" {
wo.Id = uuid.New().String()
}
w := &memoryElectWatcher{
wo: wo,
res: make(chan *sync.Member, 1),
exit: make(chan struct{}, 1),
}
m.leaderMtx.Lock()
nsWatchers, ok := m.watchers[wo.Namespace]
if !ok {
m.watchers[wo.Namespace] = map[string]*memoryElectWatcher{}
nsWatchers = m.watchers[wo.Namespace]
}
nsWatchers[wo.Id] = w
m.leaderMtx.Unlock()
return w, nil
}
func (m *memorySync) Lock(ctx context.Context, id string, opts ...sync.LockOption) error {
return m.lock(id, false, opts...)
}
func (m *memorySync) lock(id string, isLeader bool, opts ...sync.LockOption) error {
var options sync.LockOptions
for _, o := range opts {
o(&options)
}
// lock our access
m.mtx.Lock()
lk, ok := m.locks[id]
if !ok {
m.locks[id] = &memoryLock{
id: id,
time: time.Now(),
ttl: options.TTL,
release: make(chan bool),
isLeader: isLeader,
}
// unlock
m.mtx.Unlock()
return nil
}
m.mtx.Unlock()
// set wait time
var wait <-chan time.Time
var ttl <-chan time.Time
// decide if we should wait
if options.Wait > time.Duration(0) {
wait = time.After(options.Wait)
}
// check the ttl of the lock
if lk.ttl > time.Duration(0) {
// time lived for the lock
live := time.Since(lk.time)
// set a timer for the leftover ttl
if live > lk.ttl {
// release the lock if it expired
_ = m.Unlock(context.TODO(), id)
} else {
ttl = time.After(live)
}
}
lockLoop:
for {
// wait for the lock to be released
select {
case <-lk.release:
m.mtx.Lock()
// someone locked before us
lk, ok = m.locks[id]
if ok {
m.mtx.Unlock()
continue
}
// got chance to lock
m.locks[id] = &memoryLock{
id: id,
time: time.Now(),
ttl: options.TTL,
release: make(chan bool),
isLeader: isLeader,
}
m.mtx.Unlock()
break lockLoop
case <-ttl:
// ttl exceeded
_ = m.Unlock(context.TODO(), id)
// TODO: check the ttl again above
ttl = nil
// try acquire
continue
case <-wait:
return sync.ErrLockTimeout
}
}
return nil
}
func (m *memorySync) Unlock(ctx context.Context, id string) error {
m.mtx.Lock()
defer m.mtx.Unlock()
lk, ok := m.locks[id]
// no lock exists
if !ok {
return nil
}
// delete the lock
delete(m.locks, id)
select {
case <-lk.release:
return nil
default:
close(lk.release)
}
return nil
}
func (m *memorySync) String() string {
return "memory"
}
type memoryLeader struct {
s *memorySync
opts sync.LeaderOptions
name string
ns string
id string
role sync.Role
resign func(id string) error
observe chan sync.ObserveResult
status chan bool
}
func (m *memoryLeader) Id() string {
return m.id
}
func (m *memoryLeader) Resign() error {
return m.resign(m.id)
}
func (m *memoryLeader) Observe() chan sync.ObserveResult {
return m.observe
}
func (m *memoryLeader) Primary() (*sync.Member, error) {
var member *sync.Member
m.s.mtx.RLock()
for _, leader := range m.s.leaderStore {
if leader.role == sync.Primary {
member = &sync.Member{
Leader: leader.name,
Id: leader.id,
Namespace: leader.ns,
Role: leader.role,
}
}
}
m.s.mtx.RUnlock()
return member, nil
}
func (m *memoryLeader) Status() chan bool {
return m.status
}
type memoryElectWatcher struct {
wo sync.WatchElectOptions
res chan *sync.Member
exit chan struct{}
}
func (m *memoryElectWatcher) Next() (*sync.Member, error) {
for {
select {
case r := <-m.res:
return r, nil
case <-m.exit:
return nil, errors.New("watcher closed")
}
}
}
func (m *memoryElectWatcher) Close() {
select {
case <-m.exit:
return
default:
close(m.exit)
}
}
func NewSync(opts ...sync.Option) sync.Sync {
var options sync.Options
for _, o := range opts {
o(&options)
}
return &memorySync{
options: options,
leaderStore: map[string]*memoryLeader{},
locks: make(map[string]*memoryLock),
mtx: gosync.RWMutex{},
leaderMtx: gosync.RWMutex{},
watchers: map[string]map[string]*memoryElectWatcher{},
}
}