/
dlock.go
124 lines (105 loc) · 3.85 KB
/
dlock.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
// Package dlock implements a distributed lock on top of etcd.
package dlock
import (
"context"
"time"
etcd "go.etcd.io/etcd/client/v3"
"go.etcd.io/etcd/client/v3/concurrency"
"go.uber.org/zap"
"github.com/pachyderm/pachyderm/v2/src/internal/errors"
"github.com/pachyderm/pachyderm/v2/src/internal/log"
"github.com/pachyderm/pachyderm/v2/src/internal/pctx"
)
// DLock is a handle to a distributed lock.
type DLock interface {
// Lock acquries the distributed lock, blocking if necessary. If
// the lock is acquired, it returns a context that should be used
// in any subsequent blocking requests, so that if you lose the lock,
// the requests get cancelled correctly.
Lock(context.Context) (context.Context, error)
// TryLock is like Lock, but returns an error if the lock is already locked.
TryLock(context.Context) (context.Context, error)
// Unlock releases the distributed lock.
Unlock(context.Context) error
}
type etcdImpl struct {
client *etcd.Client
prefix string
session *concurrency.Session
mutex *concurrency.Mutex
}
// NewDLock attempts to acquire a distributed lock that locks a given prefix
// in the data store.
func NewDLock(client *etcd.Client, prefix string) DLock {
return &etcdImpl{
client: client,
prefix: prefix,
}
}
func (d *etcdImpl) Lock(ctx context.Context) (_ context.Context, retErr error) {
ctx = pctx.Child(ctx, "", pctx.WithFields(zap.String("withLock", d.prefix)))
defer log.Span(ctx, "DLock.Lock")(log.Errorp(&retErr))
// The default TTL is 60 secs which means that if a node dies, it
// still holds the lock for 60 secs, which is too high.
session, err := concurrency.NewSession(d.client, concurrency.WithContext(ctx), concurrency.WithTTL(15))
if err != nil {
return nil, errors.EnsureStack(err)
}
mutex := concurrency.NewMutex(session, d.prefix)
if err := mutex.Lock(ctx); err != nil {
return nil, errors.EnsureStack(err)
}
start := time.Now()
log.Debug(ctx, "acquired lock ok")
ctx, cancel := pctx.WithCancel(pctx.Child(ctx, "", pctx.WithFields(zap.Bool("locked", true))))
go func() {
select {
case <-ctx.Done():
log.Debug(ctx, "lock's context is done", zap.Error(context.Cause(ctx)), zap.Duration("lockLifetime", time.Since(start)))
case <-session.Done():
log.Debug(ctx, "lock's session is done; cancelling associated context", zap.Duration("lockLifetime", time.Since(start)))
cancel()
}
}()
d.session = session
d.mutex = mutex
return ctx, nil
}
func (d *etcdImpl) TryLock(ctx context.Context) (_ context.Context, retErr error) {
ctx = pctx.Child(ctx, "", pctx.WithFields(zap.String("withLock", d.prefix)))
defer log.Span(ctx, "DLock.TryLock")(log.Errorp(&retErr))
session, err := concurrency.NewSession(d.client, concurrency.WithContext(ctx), concurrency.WithTTL(15))
if err != nil {
return nil, errors.EnsureStack(err)
}
mutex := concurrency.NewMutex(session, d.prefix)
if err := mutex.TryLock(ctx); err != nil {
return nil, errors.EnsureStack(err)
}
start := time.Now()
log.Debug(ctx, "acquired lock ok")
ctx, cancel := pctx.WithCancel(pctx.Child(ctx, "", pctx.WithFields(zap.Bool("locked", true))))
go func() {
select {
case <-ctx.Done():
log.Debug(ctx, "lock's context is done", zap.Error(context.Cause(ctx)), zap.Duration("lockLifetime", time.Since(start)))
case <-session.Done():
log.Debug(ctx, "lock's session is done; cancelling associated context", zap.Duration("lockLifetime", time.Since(start)))
cancel()
}
}()
d.session = session
d.mutex = mutex
return ctx, nil
}
func (d *etcdImpl) Unlock(ctx context.Context) (retErr error) {
defer log.Span(ctx, "DLock.Unlock", zap.String("prefix", d.prefix))(log.Errorp(&retErr))
if err := d.mutex.Unlock(ctx); err != nil {
return errors.EnsureStack(err)
}
if err := d.session.Close(); err != nil {
return errors.EnsureStack(err)
}
log.Debug(ctx, "relinquished lock ok", zap.String("prefix", d.prefix))
return nil
}