/
lock.go
170 lines (155 loc) · 5.31 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
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
// Copyright 2019 Google LLC
//
// 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 dsstore
import (
"context"
"time"
"cloud.google.com/go/datastore" /* copybara-comment: datastore */
"google.golang.org/grpc/codes" /* copybara-comment */
"google.golang.org/grpc/status" /* copybara-comment */
"github.com/pborman/uuid" /* copybara-comment */
)
// Lock provides a distributed lock mechanism using datastore.
// Datastore doesn't provide a transaction commit time, the times used are local
// process times. Be aware of clock skew and add a small buffer to avoid
// problems.
//
// Example:
// l := dsstore.NewLock(c, "lockname")
// d := 10*time.Second
//
// for {
// // Respect the calling context.
// select {
// case <-ctx.Done():
// return ctx.Err()
// default:
// }
// // Try to acquire the lock.
// if err := l.Acquire(ctx, time.Now().Add(d)); err == nil {
// // Lock acuqired.
// break
// }
// // Lock could not be acquired. Sleep for a while and retry.
// time.Sleep(time.Second)
// }
//
// // [optional] Try to release the lock as soon as work is done.
// // Note that explicit releasing of the lock is best-effort and
// // cannot be gauranteed because of failures (e.g. crashes),
// // if a process does not or fails to explicitly release the the lock,
// // the lock will be automatically released after lock duration expires and
// // will not cause a deadlock.
// defer l.Release(ctx)
//
// // We only hold the lock for 10s, so we have a bit less than 10s remaining.
// ctx, cancel := context.WithTimeout(ctx, 9*time.Second)
// defer cancel()
//
// // Critical section that requires mutual exclusion.
// ...
// ...
// Lock is the data for a lock.
type Lock struct {
// client for accessing Datastore.
client *datastore.Client
// name is the name of the datastore lock object.
name string
// holder is the uuid for the holder of the lock.
holder string
}
// NewLock creates a new lock object.
func NewLock(client *datastore.Client, name string) *Lock {
return &Lock{
client: client,
name: name,
holder: uuid.New(),
}
}
// lockKind is the datastore kind for locks.
const lockKind = "lock"
// lockData is the datastore lock entity.
type lockData struct {
// holder is the uuid for the holder of the lock.
Holder string
// End is the time until which the lock is held.
End time.Time
}
// Acquire attempts to acquire the lock until the specified time.
// Warning: The times used are local times, be careful with cross process clock skew,
// stop assuming that you hold the lock slightly before you
func (l *Lock) Acquire(ctx context.Context, end time.Time) error {
key := datastore.NameKey(lockKind, l.name, nil)
f := func(tx *datastore.Transaction) error {
now := time.Now()
lock := &lockData{}
err := tx.Get(key, lock)
if err != nil && err != datastore.ErrNoSuchEntity {
return status.Errorf(codes.Internal, "reading %q failed: %v", l.name, err)
}
if lock.End.After(now) {
return status.Errorf(codes.FailedPrecondition, "lock %q is reserved till %v > now = %v", l.name, lock.End, now)
}
lock = &lockData{Holder: l.holder, End: end}
if _, err := tx.Put(key, lock); err != nil {
return status.Errorf(codes.Internal, "writing lock %q failed: %v", l.name, err)
}
return nil
}
if _, err := l.client.RunInTransaction(ctx, f, datastore.MaxAttempts(1)); err != nil {
return err
}
return nil
}
// Release attempts to release the holding of the named lock.
func (l *Lock) Release(ctx context.Context) error {
key := datastore.NameKey(lockKind, l.name, nil)
f := func(tx *datastore.Transaction) error {
now := time.Now()
lock := &lockData{}
err := tx.Get(key, lock)
if err != nil && err != datastore.ErrNoSuchEntity {
return status.Errorf(codes.Internal, "reading lock %q failed: %v", l.name, err)
}
if lock.Holder != l.holder {
return status.Errorf(codes.FailedPrecondition, "lock %q is hold by another process", l.name)
}
if lock.End.Before(now) {
return status.Errorf(codes.FailedPrecondition, "lock %q hold ended at %v", l.name, lock.End)
}
lock = &lockData{Holder: l.holder, End: now}
if _, err := tx.Put(key, lock); err != nil {
return status.Errorf(codes.Internal, "writing lock %q failed: %v", l.name, err)
}
return nil
}
if _, err := l.client.RunInTransaction(ctx, f, datastore.MaxAttempts(1)); err != nil {
return err
}
return nil
}
// Reset clears the information about the lock from datastore.
func (l *Lock) Reset(ctx context.Context) error {
key := datastore.NameKey(lockKind, l.name, nil)
f := func(tx *datastore.Transaction) error {
if err := tx.Delete(key); err != nil {
return status.Errorf(codes.Internal, "deleting lock %q failed: %v", l.name, err)
}
return nil
}
if _, err := l.client.RunInTransaction(ctx, f, datastore.MaxAttempts(1)); err != nil {
return err
}
return nil
}