-
Notifications
You must be signed in to change notification settings - Fork 0
/
lease.go
1308 lines (1206 loc) · 43.8 KB
/
lease.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2015 The Cockroach Authors.
//
// 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.
//
// Author: Peter Mattis (peter@cockroachlabs.com)
// Author: Andrei Matei (andreimatei1@gmail.com)
package sql
import (
"bytes"
"fmt"
"math/rand"
"sort"
"sync/atomic"
"time"
"github.com/gogo/protobuf/proto"
"github.com/pkg/errors"
"golang.org/x/net/context"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/config"
"github.com/cockroachdb/cockroach/pkg/gossip"
"github.com/cockroachdb/cockroach/pkg/internal/client"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/security"
"github.com/cockroachdb/cockroach/pkg/sql/parser"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/retry"
"github.com/cockroachdb/cockroach/pkg/util/stop"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
)
// TODO(pmattis): Periodically renew leases for tables that were used recently and
// for which the lease will expire soon.
var (
// LeaseDuration is the mean duration a lease will be acquired for. The
// actual duration is jittered in the range
// [0.75,1.25]*LeaseDuration. Exported for testing purposes only.
LeaseDuration = 5 * time.Minute
// MinLeaseDuration is the minimum duration a lease will have remaining upon
// acquisition. Exported for testing purposes only.
MinLeaseDuration = time.Minute
)
// LeaseState holds the state for a lease. Exported only for testing.
type LeaseState struct {
// This descriptor is immutable and can be shared by many goroutines.
// Care must be taken to not modify it.
sqlbase.TableDescriptor
expiration parser.DTimestamp
// mu protects refcount and released
mu syncutil.Mutex
refcount int
// Set if the lease has been released and cannot be handed out any more. The
// table name cache can have references to such leases since releasing a lease
// and updating the cache is not atomic.
released bool
testingKnobs LeaseStoreTestingKnobs
}
func (s *LeaseState) String() string {
return fmt.Sprintf("%d(%q) ver=%d:%d", s.ID, s.Name, s.Version, s.expiration.UnixNano())
}
// Expiration returns the expiration time of the lease.
func (s *LeaseState) Expiration() time.Time {
return s.expiration.Time
}
// hasSomeLifeLeft returns true if the lease has at least a minimum of
// lifetime left until expiration, and thus can be used.
func (s *LeaseState) hasSomeLifeLeft(clock *hlc.Clock) bool {
if s.testingKnobs.CanUseExpiredLeases {
return true
}
minDesiredExpiration := clock.Now().GoTime().Add(MinLeaseDuration)
return s.expiration.After(minDesiredExpiration)
}
// Refcount returns the reference count of the lease.
func (s *LeaseState) Refcount() int {
s.mu.Lock()
defer s.mu.Unlock()
return s.refcount
}
func (s *LeaseState) incRefcount() {
s.mu.Lock()
s.incRefcountLocked()
s.mu.Unlock()
}
func (s *LeaseState) incRefcountLocked() {
if s.released {
panic(fmt.Sprintf("trying to incRefcount on released lease: %+v", s))
}
s.refcount++
if log.V(3) {
log.Infof(context.TODO(), "LeaseState.incRef: descID=%d name=%q version=%d refcount=%d",
s.ID, s.Name, s.Version, s.refcount)
}
}
// LeaseStore implements the operations for acquiring and releasing leases and
// publishing a new version of a descriptor. Exported only for testing.
type LeaseStore struct {
db client.DB
clock *hlc.Clock
nodeID *base.NodeIDContainer
testingKnobs LeaseStoreTestingKnobs
memMetrics *MemoryMetrics
}
// jitteredLeaseDuration returns a randomly jittered duration from the interval
// [0.75 * leaseDuration, 1.25 * leaseDuration].
func jitteredLeaseDuration() time.Duration {
return time.Duration(float64(LeaseDuration) * (0.75 + 0.5*rand.Float64()))
}
// Acquire a lease on the most recent version of a table descriptor.
// If the lease cannot be obtained because the descriptor is in the process of
// being dropped, the error will be errTableDropped.
func (s LeaseStore) Acquire(
ctx context.Context,
txn *client.Txn,
tableID sqlbase.ID,
minVersion sqlbase.DescriptorVersion,
minExpirationTime parser.DTimestamp,
) (*LeaseState, error) {
lease := &LeaseState{testingKnobs: s.testingKnobs}
expiration := time.Unix(0, s.clock.Now().WallTime).Add(jitteredLeaseDuration())
expiration = expiration.Round(time.Microsecond)
if !minExpirationTime.IsZero() && expiration.Before(minExpirationTime.Time) {
expiration = minExpirationTime.Time
}
lease.expiration = parser.DTimestamp{Time: expiration}
// Use the supplied (user) transaction to look up the descriptor because the
// descriptor might have been created within the transaction.
p := makeInternalPlanner("lease-acquire", txn, security.RootUser, s.memMetrics)
defer finishInternalPlanner(p)
const getDescriptor = `SELECT descriptor FROM system.descriptor WHERE id = $1`
values, err := p.QueryRow(ctx, getDescriptor, int(tableID))
if err != nil {
return nil, err
}
if values == nil {
return nil, sqlbase.ErrDescriptorNotFound
}
desc := &sqlbase.Descriptor{}
if err := proto.Unmarshal([]byte(*values[0].(*parser.DBytes)), desc); err != nil {
return nil, err
}
tableDesc := desc.GetTable()
if tableDesc == nil {
return nil, errors.Errorf("ID %d is not a table", tableID)
}
if err := filterTableState(tableDesc); err != nil {
return nil, err
}
tableDesc.MaybeUpgradeFormatVersion()
// Once the descriptor is set it is immutable and care must be taken
// to not modify it.
lease.TableDescriptor = *tableDesc
// ValidateTable instead of Validate, even though we have a txn available,
// so we don't block reads waiting for this lease.
if err := lease.ValidateTable(); err != nil {
return nil, err
}
if lease.Version < minVersion {
return nil, errors.Errorf("version %d of table %d does not exist yet", minVersion, tableID)
}
// Insert the entry in the lease table in a separate transaction. This is
// necessary because we want to ensure that the lease entry is added and the
// transaction passed to Acquire() might be aborted. The lease entry needs to
// be added because we store the returned LeaseState in local in-memory maps
// and cannot handle the entry being reverted. This is safe because either
// the descriptor we're acquiring the lease on existed prior to the acquire
// transaction in which case acquiring the lease is kosher, or the descriptor
// was created within the acquire transaction. The second case is more
// subtle. We might create a lease entry for a table that doesn't exist, but
// there is no harm in that as no other transaction will be attempting to
// modify the descriptor and even if the descriptor is never created we'll
// just have a dangling lease entry which will eventually get GC'd.
err = s.db.Txn(ctx, func(ctx context.Context, txn *client.Txn) error {
nodeID := s.nodeID.Get()
if nodeID == 0 {
panic("zero nodeID")
}
p := makeInternalPlanner("lease-insert", txn, security.RootUser, s.memMetrics)
defer finishInternalPlanner(p)
const insertLease = `INSERT INTO system.lease (descID, version, nodeID, expiration) ` +
`VALUES ($1, $2, $3, $4)`
count, err := p.exec(ctx, insertLease, lease.ID, int(lease.Version), nodeID, &lease.expiration)
if err != nil {
return err
}
if count != 1 {
return errors.Errorf("%s: expected 1 result, found %d", insertLease, count)
}
return nil
})
return lease, err
}
// Release a previously acquired table descriptor lease.
func (s LeaseStore) Release(ctx context.Context, stopper *stop.Stopper, lease *LeaseState) {
retryOptions := base.DefaultRetryOptions()
retryOptions.Closer = stopper.ShouldQuiesce()
firstAttempt := true
for r := retry.Start(retryOptions); r.Next(); {
// This transaction is idempotent.
err := s.db.Txn(ctx, func(ctx context.Context, txn *client.Txn) error {
log.VEventf(ctx, 2, "LeaseStore releasing lease %s", lease)
nodeID := s.nodeID.Get()
if nodeID == 0 {
panic("zero nodeID")
}
p := makeInternalPlanner("lease-release", txn, security.RootUser, s.memMetrics)
defer finishInternalPlanner(p)
const deleteLease = `DELETE FROM system.lease ` +
`WHERE (descID, version, nodeID, expiration) = ($1, $2, $3, $4)`
count, err := p.exec(
ctx, deleteLease, lease.ID, int(lease.Version), nodeID, &lease.expiration)
if err != nil {
return err
}
// We allow count == 0 after the first attempt.
if count > 1 || (count == 0 && firstAttempt) {
log.Warningf(ctx, "unexpected results while deleting lease %s: "+
"expected 1 result, found %d", lease, count)
}
return nil
})
if s.testingKnobs.LeaseReleasedEvent != nil {
s.testingKnobs.LeaseReleasedEvent(lease, err)
}
if err == nil {
break
}
log.Warningf(ctx, "error releasing lease %q: %s", lease, err)
firstAttempt = false
}
}
// WaitForOneVersion returns once there are no unexpired leases on the
// previous version of the table descriptor. It returns the current version.
// After returning there can only be versions of the descriptor >= to the
// returned version. Lease acquisition (see acquire()) maintains the
// invariant that no new leases for desc.Version-1 will be granted once
// desc.Version exists.
func (s LeaseStore) WaitForOneVersion(
ctx context.Context, tableID sqlbase.ID, retryOpts retry.Options,
) (sqlbase.DescriptorVersion, error) {
desc := &sqlbase.Descriptor{}
descKey := sqlbase.MakeDescMetadataKey(tableID)
var tableDesc *sqlbase.TableDescriptor
for r := retry.Start(retryOpts); r.Next(); {
// Get the current version of the table descriptor non-transactionally.
//
// TODO(pmattis): Do an inconsistent read here?
if err := s.db.GetProto(context.TODO(), descKey, desc); err != nil {
return 0, err
}
tableDesc = desc.GetTable()
if tableDesc == nil {
return 0, errors.Errorf("ID %d is not a table", tableID)
}
// Check to see if there are any leases that still exist on the previous
// version of the descriptor.
now := s.clock.Now()
count, err := s.countLeases(ctx, tableDesc.ID, tableDesc.Version-1, now.GoTime())
if err != nil {
return 0, err
}
if count == 0 {
break
}
log.Infof(context.TODO(), "publish (count leases): descID=%d name=%s version=%d count=%d",
tableDesc.ID, tableDesc.Name, tableDesc.Version-1, count)
}
return tableDesc.Version, nil
}
var errDidntUpdateDescriptor = errors.New("didn't update the table descriptor")
// Publish updates a table descriptor. It also maintains the invariant that
// there are at most two versions of the descriptor out in the wild at any time
// by first waiting for all nodes to be on the current (pre-update) version of
// the table desc.
// The update closure is called after the wait, and it provides the new version
// of the descriptor to be written. In a multi-step schema operation, this
// update should perform a single step.
// The closure may be called multiple times if retries occur; make sure it does
// not have side effects.
// Returns the updated version of the descriptor.
func (s LeaseStore) Publish(
ctx context.Context,
tableID sqlbase.ID,
update func(*sqlbase.TableDescriptor) error,
logEvent func(*client.Txn) error,
) (*sqlbase.Descriptor, error) {
errLeaseVersionChanged := errors.New("lease version changed")
// Retry while getting errLeaseVersionChanged.
for r := retry.Start(base.DefaultRetryOptions()); r.Next(); {
// Wait until there are no unexpired leases on the previous version
// of the table.
expectedVersion, err := s.WaitForOneVersion(ctx, tableID, base.DefaultRetryOptions())
if err != nil {
return nil, err
}
desc := &sqlbase.Descriptor{}
// There should be only one version of the descriptor, but it's
// a race now to update to the next version.
err = s.db.Txn(ctx, func(ctx context.Context, txn *client.Txn) error {
descKey := sqlbase.MakeDescMetadataKey(tableID)
// Re-read the current version of the table descriptor, this time
// transactionally.
if err := txn.GetProto(ctx, descKey, desc); err != nil {
return err
}
tableDesc := desc.GetTable()
if tableDesc == nil {
return errors.Errorf("ID %d is not a table", tableID)
}
if expectedVersion != tableDesc.Version {
// The version changed out from under us. Someone else must be
// performing a schema change operation.
if log.V(3) {
log.Infof(ctx, "publish (version changed): %d != %d", expectedVersion, tableDesc.Version)
}
return errLeaseVersionChanged
}
// Run the update closure.
version := tableDesc.Version
if err := update(tableDesc); err != nil {
return err
}
if version != tableDesc.Version {
return errors.Errorf("updated version to: %d, expected: %d",
tableDesc.Version, version)
}
tableDesc.Version++
now := s.clock.Now()
tableDesc.ModificationTime = now
log.Infof(ctx, "publish: descID=%d (%s) version=%d mtime=%s",
tableDesc.ID, tableDesc.Name, tableDesc.Version, now.GoTime())
if err := tableDesc.ValidateTable(); err != nil {
return err
}
// Write the updated descriptor.
if err := txn.SetSystemConfigTrigger(); err != nil {
return err
}
b := txn.NewBatch()
b.Put(descKey, desc)
if logEvent != nil {
// If an event log is required for this update, ensure that the
// descriptor change occurs first in the transaction. This is
// necessary to ensure that the System configuration change is
// gossiped. See the documentation for
// transaction.SetSystemConfigTrigger() for more information.
if err := txn.Run(ctx, b); err != nil {
return err
}
if err := logEvent(txn); err != nil {
return err
}
return txn.Commit(ctx)
}
// More efficient batching can be used if no event log message
// is required.
return txn.CommitInBatch(ctx, b)
})
switch err {
case nil, errDidntUpdateDescriptor:
return desc, nil
case errLeaseVersionChanged:
// will loop around to retry
default:
return nil, err
}
}
panic("not reached")
}
// countLeases returns the number of unexpired leases for a particular version
// of a descriptor.
func (s LeaseStore) countLeases(
ctx context.Context, descID sqlbase.ID, version sqlbase.DescriptorVersion, expiration time.Time,
) (int, error) {
var count int
err := s.db.Txn(ctx, func(ctx context.Context, txn *client.Txn) error {
p := makeInternalPlanner("leases-count", txn, security.RootUser, s.memMetrics)
defer finishInternalPlanner(p)
const countLeases = `SELECT COUNT(version) FROM system.lease ` +
`WHERE descID = $1 AND version = $2 AND expiration > $3`
values, err := p.QueryRow(ctx, countLeases, descID, int(version), expiration)
if err != nil {
return err
}
count = int(parser.MustBeDInt(values[0]))
return nil
})
return count, err
}
// leaseSet maintains an ordered set of LeaseState objects. It supports
// addition and removal of elements, finding a specific lease, finding the
// newest lease for a particular version and finding the newest lease for the
// most recent version.
type leaseSet struct {
// The lease state data is stored in a sorted slice ordered by <version,
// expiration>. Ordering is maintained by insert and remove.
data []*LeaseState
}
func (l *leaseSet) String() string {
var buf bytes.Buffer
for i, s := range l.data {
if i > 0 {
buf.WriteString(" ")
}
buf.WriteString(fmt.Sprintf("%d:%d", s.Version, s.Expiration().UnixNano()))
}
return buf.String()
}
func (l *leaseSet) insert(s *LeaseState) {
i, match := l.findIndex(s.Version, s.expiration)
if match {
panic("unable to insert duplicate lease")
}
if i == len(l.data) {
l.data = append(l.data, s)
return
}
l.data = append(l.data, nil)
copy(l.data[i+1:], l.data[i:])
l.data[i] = s
}
func (l *leaseSet) remove(s *LeaseState) {
i, match := l.findIndex(s.Version, s.expiration)
if !match {
panic(fmt.Sprintf("can't find lease to remove: %s", s))
}
l.data = append(l.data[:i], l.data[i+1:]...)
}
func (l *leaseSet) find(
version sqlbase.DescriptorVersion, expiration parser.DTimestamp,
) *LeaseState {
if i, match := l.findIndex(version, expiration); match {
return l.data[i]
}
return nil
}
func (l *leaseSet) findIndex(
version sqlbase.DescriptorVersion, expiration parser.DTimestamp,
) (int, bool) {
i := sort.Search(len(l.data), func(i int) bool {
s := l.data[i]
if s.Version == version {
// a >= b -> !a.Before(b)
return !s.expiration.Before(expiration.Time)
}
return s.Version > version
})
if i < len(l.data) {
s := l.data[i]
if s.Version == version && s.expiration.Equal(expiration.Time) {
return i, true
}
}
return i, false
}
func (l *leaseSet) findNewest(version sqlbase.DescriptorVersion) *LeaseState {
if len(l.data) == 0 {
return nil
}
if version == 0 {
// No explicitly version, return the newest lease of the latest version.
return l.data[len(l.data)-1]
}
// Find the index of the first lease with version > targetVersion.
i := sort.Search(len(l.data), func(i int) bool {
return l.data[i].Version > version
})
if i == 0 {
return nil
}
// i-1 is the index of the newest lease for the previous version (the version
// we're looking for).
s := l.data[i-1]
if s.Version == version {
return s
}
return nil
}
type tableState struct {
id sqlbase.ID
// The cache is updated every time we acquire or release a lease.
tableNameCache *tableNameCache
stopper *stop.Stopper
// Protects both active and acquiring.
mu syncutil.Mutex
// The active leases for the table: sorted by their version and expiration
// time. There may be more than one active lease when the system is
// transitioning from one version of the descriptor to another or when the
// node preemptively acquires a new lease for a version when the old lease
// has not yet expired.
active leaseSet
// A channel used to indicate whether a lease is actively being acquired.
// nil if there is no lease acquisition in progress for the table. If
// non-nil, the channel will be closed when lease acquisition completes.
acquiring chan struct{}
// Indicates that the table has been deleted, or has an outstanding deletion.
// If set, leases are released from the store as soon as their refcount drops
// to 0, as opposed to waiting until they expire.
deleted bool
}
// acquire returns a lease at the specified version. The lease will have its
// refcount incremented, so the caller is responsible to call release() on it.
func (t *tableState) acquire(
ctx context.Context, txn *client.Txn, version sqlbase.DescriptorVersion, m *LeaseManager,
) (*LeaseState, error) {
t.mu.Lock()
defer t.mu.Unlock()
for {
s := t.active.findNewest(version)
if s != nil {
if checkedLease := t.checkLease(s, version, m.clock); checkedLease != nil {
return checkedLease, nil
}
} else if version != 0 {
n := t.active.findNewest(0)
if n != nil && version < n.Version {
return nil, errors.Errorf("table %d unable to acquire lease on old version: %d < %d",
t.id, version, n.Version)
}
}
if err := t.acquireFromStoreLocked(ctx, txn, version, m); err != nil {
return nil, err
}
// A new lease was added, so loop and perform the lookup again.
}
}
// checkLease checks whether lease is eligible to be returned to a client which
// requested a lease at a specified version (version can also be 0).
// Returns the lease after having incremented its refcount if it's OK to give it
// to the client. Returns nil otherwise.
//
// t.mu needs to be locked
func (t *tableState) checkLease(
lease *LeaseState, version sqlbase.DescriptorVersion, clock *hlc.Clock,
) *LeaseState {
// If a lease was requested for an old version of the descriptor,
// return it even if there is only a short time left before it
// expires, or even if it's expired. We can't renew this lease as doing so
// would violate the invariant that we only get leases on the newest
// version. The transaction will either finish before the lease expires or
// it will abort, which is what will happen if we returned an error here.
skipLifeCheck := version != 0 && lease != t.active.findNewest(0)
if !skipLifeCheck && !lease.hasSomeLifeLeft(clock) {
return nil
}
lease.incRefcount()
return lease
}
// acquireFromStoreLocked acquires a new lease from the store and inserts it
// into the active set. t.mu must be locked.
func (t *tableState) acquireFromStoreLocked(
ctx context.Context, txn *client.Txn, version sqlbase.DescriptorVersion, m *LeaseManager,
) error {
// Ensure there is no lease acquisition in progress.
if t.acquireWait() {
// There was a lease acquisition in progress; accept the lease just
// acquired.
return nil
}
event := m.testingKnobs.LeaseStoreTestingKnobs.LeaseAcquiringEvent
if event != nil {
event(t.id, txn)
}
s, err := t.acquireNodeLease(ctx, txn, version, m, parser.DTimestamp{})
if err != nil {
return err
}
t.active.insert(s)
return nil
}
// acquireFreshestFromStoreLocked acquires a new lease from the store and
// inserts it into the active set. It guarantees that the lease returned is
// the one acquired after the call is made. Use this if the lease we want to
// get needs to see some descriptor updates that we know happened recently
// (but that didn't cause the version to be incremented). E.g. if we suspect
// there's a new name for a table, the caller can insist on getting a lease
// reflecting this new name. Moreover, upon returning, the new lease is
// guaranteed to be the last lease in t.active (note that this is not
// generally guaranteed, as leases are assigned random expiration times).
//
// t.mu must be locked.
func (t *tableState) acquireFreshestFromStoreLocked(
ctx context.Context, txn *client.Txn, version sqlbase.DescriptorVersion, m *LeaseManager,
) error {
// Ensure there is no lease acquisition in progress.
t.acquireWait()
// Move forward to acquire a fresh lease.
// Set the min expiration time to guarantee that the lease acquired is the
// last lease in t.active .
minExpirationTime := parser.DTimestamp{}
newestLease := t.active.findNewest(0)
if newestLease != nil {
minExpirationTime = parser.DTimestamp{
Time: newestLease.expiration.Add(time.Millisecond)}
}
s, err := t.acquireNodeLease(ctx, txn, version, m, minExpirationTime)
if err != nil {
return err
}
t.active.insert(s)
return nil
}
// releaseInactiveLeases releases the leases in t.active.data with refcount 0.
// t.mu must be locked.
func (t *tableState) releaseInactiveLeases(m *LeaseManager) {
// A copy of t.active.data must be made since t.active.data will be changed
// by `removeLease`.
for _, lease := range append([]*LeaseState(nil), t.active.data...) {
func() {
lease.mu.Lock()
defer lease.mu.Unlock()
if lease.refcount == 0 {
t.removeLease(lease, m)
}
}()
}
}
// acquireWait waits until no lease acquisition is in progress. It returns
// true if it needed to wait.
func (t *tableState) acquireWait() bool {
wait := t.acquiring != nil
// Spin until no lease acquisition is in progress.
for t.acquiring != nil {
// We're called with mu locked, but need to unlock it while we wait
// for the in-progress lease acquisition to finish.
acquiring := t.acquiring
t.mu.Unlock()
<-acquiring
t.mu.Lock()
}
return wait
}
// If the lease cannot be obtained because the descriptor is in the process of
// being deleted, the error will be errDescriptorDeleted.
// minExpirationTime, if not set to the zero value, will be used as a lower
// bound on the expiration of the new lease. This can be used to eliminate the
// jitter in the expiration time, and guarantee that we get a lease that will be
// inserted at the end of the lease set (i.e. it will be returned by
// findNewest() from now on).
//
// t.mu needs to be locked.
func (t *tableState) acquireNodeLease(
ctx context.Context,
txn *client.Txn,
minVersion sqlbase.DescriptorVersion,
m *LeaseManager,
minExpirationTime parser.DTimestamp,
) (*LeaseState, error) {
if m.isDraining() {
return nil, errors.New("cannot acquire lease when draining")
}
// Notify when lease has been acquired.
t.acquiring = make(chan struct{})
defer func() {
close(t.acquiring)
t.acquiring = nil
}()
// We're called with mu locked, but need to unlock it during lease
// acquisition.
t.mu.Unlock()
defer t.mu.Lock()
lease, err := m.LeaseStore.Acquire(ctx, txn, t.id, minVersion, minExpirationTime)
if err != nil {
return nil, err
}
t.tableNameCache.insert(lease)
return lease, nil
}
func (t *tableState) release(lease *LeaseState, m *LeaseManager) error {
t.mu.Lock()
defer t.mu.Unlock()
s := t.active.find(lease.Version, lease.expiration)
if s == nil {
return errors.Errorf("table %d version %d not found", lease.ID, lease.Version)
}
// Decrements the refcount and returns true if the lease has to be removed
// from the store.
decRefcount := func(s *LeaseState) bool {
// Figure out if we'd like to remove the lease from the store asap (i.e.
// when the refcount drops to 0). If so, we'll need to mark the lease as
// released.
removeOnceDereferenced := m.LeaseStore.testingKnobs.RemoveOnceDereferenced ||
// Release from the store if the table has been deleted; no leases
// can be acquired any more.
t.deleted ||
// Release from the store if the LeaseManager is draining.
m.isDraining() ||
// Release from the store if the lease is not for the latest
// version; only leases for the latest version can be acquired.
s != t.active.findNewest(0)
s.mu.Lock()
defer s.mu.Unlock()
s.refcount--
if log.V(3) {
log.Infof(context.TODO(), "release: descID=%d name:%q version=%d refcount=%d", s.ID, s.Name, s.Version, s.refcount)
}
if s.refcount < 0 {
panic(fmt.Sprintf("negative ref count: descID=%d(%q) version=%d refcount=%d", s.ID, s.Name, s.Version, s.refcount))
}
if s.refcount == 0 && removeOnceDereferenced {
s.released = true
}
return s.released
}
if decRefcount(s) {
t.removeLease(s, m)
}
return nil
}
// t.mu needs to be locked.
func (t *tableState) removeLease(lease *LeaseState, m *LeaseManager) {
t.active.remove(lease)
t.tableNameCache.remove(lease)
ctx := context.TODO()
if m.isDraining() {
// Release synchronously to guarantee release before exiting.
m.LeaseStore.Release(ctx, t.stopper, lease)
return
}
// Release to the store asynchronously, without the tableState lock.
if err := t.stopper.RunAsyncTask(ctx, func(ctx context.Context) {
m.LeaseStore.Release(ctx, t.stopper, lease)
}); err != nil {
log.Warningf(ctx, "error: %s, not releasing lease: %q", err, lease)
}
}
// purgeOldLeases refreshes the leases on a table. Unused leases older than
// minVersion will be released.
// If deleted is set, minVersion is ignored; no lease is acquired and all
// existing unused leases are released. The table is further marked for
// deletion, which will cause existing in-use leases to be eagerly released once
// they're not in use any more.
// If t has no active leases, nothing is done.
func (t *tableState) purgeOldLeases(
ctx context.Context,
db *client.DB,
deleted bool,
minVersion sqlbase.DescriptorVersion,
m *LeaseManager,
) error {
t.mu.Lock()
empty := len(t.active.data) == 0
t.mu.Unlock()
if empty {
// We don't currently have a lease on this table, so no need to refresh
// anything.
return nil
}
// Acquire and release a lease on the table at a version >= minVersion.
var lease *LeaseState
err := db.Txn(ctx, func(ctx context.Context, txn *client.Txn) error {
var err error
if !deleted {
lease, err = t.acquire(ctx, txn, minVersion, m)
if err == errTableDropped {
deleted = true
}
}
if err == nil || deleted {
t.mu.Lock()
defer t.mu.Unlock()
if deleted {
t.deleted = true
}
t.releaseInactiveLeases(m)
return nil
}
return err
})
if err != nil {
return err
}
if lease == nil {
return nil
}
return t.release(lease, m)
}
// LeaseStoreTestingKnobs contains testing knobs.
type LeaseStoreTestingKnobs struct {
// Called after a lease is removed from the store, with any operation error.
// See LeaseRemovalTracker.
LeaseReleasedEvent func(lease *LeaseState, err error)
// Called just before a lease is about to be acquired by the store. Gives
// access to the txn doing the acquiring.
LeaseAcquiringEvent func(tableID sqlbase.ID, txn *client.Txn)
// Called after a lease is acquired, with any operation error.
LeaseAcquiredEvent func(lease *LeaseState, err error)
// Allow the use of expired leases.
CanUseExpiredLeases bool
// RemoveOnceDereferenced forces leases to be removed
// as soon as they are dereferenced.
RemoveOnceDereferenced bool
}
// ModuleTestingKnobs is part of the base.ModuleTestingKnobs interface.
func (*LeaseStoreTestingKnobs) ModuleTestingKnobs() {}
var _ base.ModuleTestingKnobs = &LeaseStoreTestingKnobs{}
// LeaseManagerTestingKnobs contains test knobs.
type LeaseManagerTestingKnobs struct {
// A callback called when a gossip update is received, before the leases are
// refreshed. Careful when using this to block for too long - you can block
// all the gossip users in the system.
GossipUpdateEvent func(config.SystemConfig)
// A callback called after the leases are refreshed as a result of a gossip update.
TestingLeasesRefreshedEvent func(config.SystemConfig)
LeaseStoreTestingKnobs LeaseStoreTestingKnobs
}
var _ base.ModuleTestingKnobs = &LeaseManagerTestingKnobs{}
// ModuleTestingKnobs is part of the base.ModuleTestingKnobs interface.
func (*LeaseManagerTestingKnobs) ModuleTestingKnobs() {}
type tableNameCacheKey struct {
dbID sqlbase.ID
normalizeTabledName string
}
// tableNameCache represents a cache of table name -> lease mappings.
// The LeaseManager updates the cache every time a lease is acquired or released
// from the store. The cache maintains the newest lease for each table name.
// All methods are thread-safe.
type tableNameCache struct {
mu syncutil.Mutex
tables map[tableNameCacheKey]*LeaseState
}
// Resolves a (database ID, table name) to the table descriptor's ID. Returns
// a valid lease for the table with that name, if the name had been previously
// cached and the cache has a lease with at least some amount of life
// left in it. Returns nil otherwise.
// This method handles normalizing the table name.
// The lease's refcount is incremented before returning, so the caller is
// responsible for releasing it to the leaseManager.
func (c *tableNameCache) get(dbID sqlbase.ID, tableName string, clock *hlc.Clock) *LeaseState {
c.mu.Lock()
lease, ok := c.tables[makeTableNameCacheKey(dbID, tableName)]
c.mu.Unlock()
if !ok {
return nil
}
lease.mu.Lock()
defer lease.mu.Unlock()
if !nameMatchesLease(lease, dbID, tableName) {
panic(fmt.Sprintf("Out of sync entry in the name cache. "+
"Cache entry: %d.%q -> %d. Lease: %d.%q.",
dbID, tableName, lease.ID, lease.ParentID, lease.Name))
}
if !lease.hasSomeLifeLeft(clock) {
// Expired, or almost expired, lease. Don't hand it out.
return nil
}
if lease.released {
// This get() raced with a release operation. The leaseManager should remove
// this cache entry soon.
return nil
}
lease.incRefcountLocked()
return lease
}
func (c *tableNameCache) insert(lease *LeaseState) {
c.mu.Lock()
defer c.mu.Unlock()
key := makeTableNameCacheKey(lease.ParentID, lease.Name)
existing, ok := c.tables[key]
if !ok {
c.tables[key] = lease
return
}
// If we already have a lease in the cache for this name, see if this one is
// better (higher version or later expiration).
if lease.Version > existing.Version ||
(lease.Version == existing.Version && lease.Expiration().After(existing.Expiration())) {
// Overwrite the old lease. The new one is better. From now on, we want
// clients to use the new one.
c.tables[key] = lease
}
}
func (c *tableNameCache) remove(lease *LeaseState) {
c.mu.Lock()
defer c.mu.Unlock()
key := makeTableNameCacheKey(lease.ParentID, lease.Name)
existing, ok := c.tables[key]
if !ok {
// Table for lease not found in table name cache. This can happen if we had
// a more recent lease on the table in the tableNameCache, then the table
// gets deleted, then the more recent lease is remove()d - which clears the
// cache.
return
}
// If this was the lease that the cache had for the table name, remove it.
// If the cache had some other lease, this remove is a no-op.
if existing == lease {
delete(c.tables, key)
}
}
func makeTableNameCacheKey(dbID sqlbase.ID, tableName string) tableNameCacheKey {
return tableNameCacheKey{dbID, parser.ReNormalizeName(tableName)}
}
// LeaseManager manages acquiring and releasing per-table leases. It also
// handles resolving table names to descriptor IDs.
//
// Exported only for testing.
//
// The locking order is:
// LeaseManager.mu > tableState.mu > tableNameCache.mu > LeaseState.mu
type LeaseManager struct {
LeaseStore
mu struct {
syncutil.Mutex
tables map[sqlbase.ID]*tableState
}
draining atomic.Value