This repository has been archived by the owner on Jan 10, 2023. It is now read-only.
/
long_lived_lock.go
361 lines (309 loc) · 9.58 KB
/
long_lived_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
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
package service
import (
"context"
"database/sql"
"fmt"
"os"
"time"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/Netflix/titus-executor/logger"
vpcapi "github.com/Netflix/titus-executor/vpc/api"
"github.com/Netflix/titus-executor/vpc/service/data"
"github.com/Netflix/titus-executor/vpc/tracehelpers"
"github.com/golang/protobuf/ptypes/empty"
"github.com/pkg/errors"
"go.opencensus.io/trace"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"k8s.io/apimachinery/pkg/util/sets"
)
// TODO: Break this out into its own package
const (
lockTime = 30 * time.Second
timeBetweenTryingToAcquireLocks = 15 * time.Second
)
type scanner interface {
Scan(dest ...interface{}) error
}
func (vpcService *vpcService) scanLock(rowScanner scanner) (*vpcapi.Lock, error) {
lock := vpcapi.Lock{}
var heldUntil time.Time
err := rowScanner.Scan(&lock.Id, &lock.LockName, &lock.HeldBy, &heldUntil)
if err != nil {
return nil, err
}
lock.HeldUntil = timestamppb.New(heldUntil)
return &lock, nil
}
func (vpcService *vpcService) GetLocks(ctx context.Context, req *vpcapi.GetLocksRequest) (*vpcapi.GetLocksResponse, error) {
ctx, span := trace.StartSpan(ctx, "GetLocks")
defer span.End()
tx, err := vpcService.db.BeginTx(ctx, &sql.TxOptions{
ReadOnly: true,
})
if err != nil {
tracehelpers.SetStatus(err, span)
return nil, errors.Wrap(err, "Could not start database transaction")
}
defer func() {
_ = tx.Rollback()
}()
rows, err := tx.QueryContext(ctx, "SELECT id, lock_name, held_by, held_until FROM long_lived_locks LIMIT 1000")
if err != nil {
tracehelpers.SetStatus(err, span)
return nil, err
}
defer rows.Close()
locks := []*vpcapi.Lock{}
for rows.Next() {
lock, err := vpcService.scanLock(rows)
if err != nil {
tracehelpers.SetStatus(err, span)
return nil, err
}
locks = append(locks, lock)
}
return &vpcapi.GetLocksResponse{Locks: locks}, nil
}
func (vpcService *vpcService) GetLock(ctx context.Context, id *vpcapi.LockId) (*vpcapi.Lock, error) {
ctx, span := trace.StartSpan(ctx, "GetLock")
defer span.End()
row := vpcService.db.QueryRowContext(ctx, "SELECT id, lock_name, held_by, held_until FROM long_lived_locks WHERE id = $1", id.GetId())
lock, err := vpcService.scanLock(row)
if err == sql.ErrNoRows {
err = status.Error(codes.NotFound, "lock not found")
tracehelpers.SetStatus(err, span)
return nil, err
} else if err != nil {
tracehelpers.SetStatus(err, span)
return nil, err
}
return lock, nil
}
func (vpcService *vpcService) DeleteLock(ctx context.Context, id *vpcapi.LockId) (*empty.Empty, error) {
ctx, span := trace.StartSpan(ctx, "DeleteLock")
defer span.End()
res, err := vpcService.db.ExecContext(ctx, "DELETE FROM long_lived_locks WHERE id = $1", id.GetId())
if err != nil {
tracehelpers.SetStatus(err, span)
return nil, err
}
affected, err := res.RowsAffected()
if err != nil {
tracehelpers.SetStatus(err, span)
return nil, err
}
if affected == 0 {
err = status.Error(codes.NotFound, "lock not found")
tracehelpers.SetStatus(err, span)
return nil, err
}
return &empty.Empty{}, nil
}
func (vpcService *vpcService) PreemptLock(ctx context.Context, req *vpcapi.PreemptLockRequest) (*empty.Empty, error) {
ctx, span := trace.StartSpan(ctx, "PreemptLock")
defer span.End()
// in order to preempt a lock the holder has to be removed from the lock AND held_until has to be set to sometime in the past
// by setting held_by to null, we force the current holder to drop the lock
// by setting held_until to sometime in the past, we allow a new holder to acquire the lock
res, err := vpcService.db.ExecContext(ctx, "UPDATE long_lived_locks SET held_by = null, held_until = now() - (30 * interval '1 sec') WHERE lock_name = $1", req.GetLockName())
if err != nil {
tracehelpers.SetStatus(err, span)
return nil, err
}
affected, err := res.RowsAffected()
if err != nil {
tracehelpers.SetStatus(err, span)
return nil, err
}
if affected == 0 {
err = status.Error(codes.NotFound, "lock not found")
tracehelpers.SetStatus(err, span)
return nil, err
}
return &empty.Empty{}, nil
}
type itemLister func(context.Context) ([]data.KeyedItem, error)
type workFunc func(context.Context, data.KeyedItem) error
func generateLockName(taskName string, item data.KeyedItem) string {
return fmt.Sprintf("%s_%s", taskName, item.Key())
}
func (vpcService *vpcService) runFunctionUnderLongLivedLock(ctx context.Context, taskName string, lister itemLister, wf workFunc) error {
startedLockers := sets.NewString()
hostname, err := os.Hostname()
if err != nil {
return err
}
t := time.NewTimer(timeBetweenTryingToAcquireLocks)
defer t.Stop()
for {
items, err := lister(ctx)
if err != nil {
logger.G(ctx).WithError(err).Error("Cannot list items")
} else {
for idx := range items {
item := items[idx]
if !startedLockers.Has(item.Key()) {
startedLockers.Insert(item.Key())
ctx2 := logger.WithFields(ctx, map[string]interface{}{
"taskName": taskName,
})
lockName := generateLockName(taskName, item)
logger.G(ctx2).Info("Starting new long running function under lock")
go vpcService.waitToAcquireLongLivedLock(ctx2, hostname, lockName, func(ctx3 context.Context) error {
logger.G(ctx3).Info("Work fun starting")
err2 := wf(ctx3, item)
logger.G(ctx3).WithError(err2).Info("Work fun ending")
return err2
})
}
}
}
t.Reset(timeBetweenTryingToAcquireLocks)
select {
case <-t.C:
case <-ctx.Done():
return nil
}
}
}
func (vpcService *vpcService) waitToAcquireLongLivedLock(ctx context.Context, hostname, lockName string, workFun func(context.Context) error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
timer := time.NewTimer(lockTime / 2)
if !timer.Stop() {
<-timer.C
}
defer timer.Stop()
for {
lockAcquired, id, err := vpcService.tryToAcquireLock(ctx, hostname, lockName)
if err != nil {
logger.G(ctx).WithError(err).Error("Error while trying to acquire lock")
} else if lockAcquired {
logger.G(ctx).Debug("Lock acquired")
err = vpcService.holdLock(ctx, hostname, id, workFun)
if err != nil {
logger.G(ctx).WithError(err).Error("Error while holding lock")
}
}
timer.Reset(lockTime / 2)
select {
case <-ctx.Done():
return
case <-timer.C:
}
}
}
func (vpcService *vpcService) tryToAcquireLock(ctx context.Context, hostname, lockName string) (bool, int, error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
ctx, span := trace.StartSpan(ctx, "tryToAcquireLock")
defer span.End()
tx, err := vpcService.db.BeginTx(ctx, &sql.TxOptions{})
if err != nil {
err = errors.Wrap(err, "Could not start database transaction")
tracehelpers.SetStatus(err, span)
return false, 0, err
}
defer func() {
_ = tx.Rollback()
}()
row := tx.QueryRowContext(ctx, `
INSERT INTO long_lived_locks(lock_name, held_by, held_until)
VALUES ($1, $2, now() + ($3 * interval '1 sec')) ON CONFLICT (lock_name) DO
UPDATE
SET held_by = $2,
held_until = now() + ($3 * interval '1 sec')
WHERE long_lived_locks.held_until < now() RETURNING id
`, lockName, hostname, lockTime.Seconds())
var id int
err = row.Scan(&id)
if err == sql.ErrNoRows {
return false, 0, nil
} else if err != nil {
err = errors.Wrap(err, "Could not insert into long lived locks")
tracehelpers.SetStatus(err, span)
return false, 0, err
}
err = tx.Commit()
if err != nil {
err = errors.Wrap(err, "Could not commit transaction")
tracehelpers.SetStatus(err, span)
return false, 0, err
}
return true, id, nil
}
func (vpcService *vpcService) tryToHoldLock(ctx context.Context, hostname string, id int) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
ctx, span := trace.StartSpan(ctx, "tryToHoldLock")
defer span.End()
logger.G(ctx).WithField("hostname", hostname).WithField("id", id).Debug("Trying to hold lock")
tx, err := vpcService.db.BeginTx(ctx, &sql.TxOptions{})
if err != nil {
err = errors.Wrap(err, "Could not start database transaction")
tracehelpers.SetStatus(err, span)
return err
}
defer func() {
_ = tx.Rollback()
}()
result, err := tx.ExecContext(ctx, "UPDATE long_lived_locks SET held_until = now() + ($1 * interval '1 sec') WHERE id = $2 AND held_by = $3", lockTime.Seconds(), id, hostname)
if err != nil {
err = errors.Wrap(err, "Could update lock time")
tracehelpers.SetStatus(err, span)
return err
}
n, err := result.RowsAffected()
if err != nil {
err = errors.Wrap(err, "Could not get rows affected")
tracehelpers.SetStatus(err, span)
return err
}
if n != 1 {
err = fmt.Errorf("Unexpected number of rows updated: %d", n)
tracehelpers.SetStatus(err, span)
return err
}
err = tx.Commit()
if err != nil {
err = errors.Wrap(err, "Could not commit transaction")
tracehelpers.SetStatus(err, span)
return err
}
return nil
}
func (vpcService *vpcService) holdLock(ctx context.Context, hostname string, id int, workFun func(context.Context) error) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
errCh := make(chan error)
go func() {
errCh <- workFun(ctx)
}()
ticker := time.NewTicker(lockTime / 4)
defer ticker.Stop()
for {
err := vpcService.tryToHoldLock(ctx, hostname, id)
if err != nil {
return err
}
select {
case err = <-errCh:
logger.G(ctx).WithError(err).Error("Worker encountered error, releasing lock")
return errors.Wrap(err, "Worker encountered error")
case <-ctx.Done():
return ctx.Err()
case <-ticker.C:
}
}
}
func waitFor(ctx context.Context, duration time.Duration) error {
t := time.NewTimer(duration)
defer t.Stop()
select {
case <-t.C:
return nil
case <-ctx.Done():
return ctx.Err()
}
}