forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lease.go
1398 lines (1281 loc) · 47 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.
package sql
import (
"bytes"
"fmt"
"math/rand"
"sort"
"sync/atomic"
"time"
"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"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
)
// 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
)
// tableVersionState holds the state for a table version. This includes
// the lease information for a table version.
// TODO(vivek): A node only needs to manage lease information on what it
// thinks is the latest version for a table descriptor.
type tableVersionState struct {
// This descriptor is immutable and can be shared by many goroutines.
// Care must be taken to not modify it.
sqlbase.TableDescriptor
// The expiration time for the table version. A transaction with
// timestamp T can use this table descriptor version iff
// TableDescriptor.ModificationTime <= T < expiration
//
// The expiration time is either the expiration time of the lease
// when a lease is associated with the table version, or the
// ModificationTime of the next version when the table version
// isn't associated with a lease.
expiration hlc.Timestamp
// mu protects refcount and leased.
mu syncutil.Mutex
refcount int
// Set if the node has a lease on this descriptor version.
// Leases can only be held for the two latest versions of
// a table descriptor. The latest version known to a node
// (can be different than the current latest version in the store)
// is always associated with a lease. The previous version known to
// a node might not necessarily be associated with a lease.
leased bool
}
func (s *tableVersionState) String() string {
return fmt.Sprintf("%d(%q) ver=%d:%s, refcount=%d", s.ID, s.Name, s.Version, s.expiration, s.refcount)
}
// hasExpired checks if the table is too old to be used (by a txn operating)
// at the given timestamp
func (s *tableVersionState) hasExpired(timestamp hlc.Timestamp) bool {
return !timestamp.Less(s.expiration)
}
func (s *tableVersionState) incRefcount() {
s.mu.Lock()
s.incRefcountLocked()
s.mu.Unlock()
}
func (s *tableVersionState) incRefcountLocked() {
s.refcount++
log.VEventf(context.TODO(), 2, "tableVersionState.incRef: %s", s)
}
// The lease expiration stored in the database is of a different type.
// We've decided that it's too much work to change the type to
// hlc.Timestamp, so we're using this method to give us the stored
// type: parser.DTimestamp.
func (s *tableVersionState) leaseExpiration() parser.DTimestamp {
return parser.DTimestamp{Time: timeutil.Unix(0, s.expiration.WallTime).Round(time.Microsecond)}
}
// 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, tableID sqlbase.ID, minExpirationTime hlc.Timestamp,
) (*tableVersionState, error) {
var table *tableVersionState
err := s.db.Txn(ctx, func(ctx context.Context, txn *client.Txn) error {
expiration := txn.OrigTimestamp()
expiration.WallTime += int64(jitteredLeaseDuration())
if expiration.Less(minExpirationTime) {
expiration = minExpirationTime
}
tableDesc, err := sqlbase.GetTableDescFromID(ctx, txn, tableID)
if err != nil {
return err
}
if err := filterTableState(tableDesc); err != nil {
return err
}
tableDesc.MaybeUpgradeFormatVersion()
// Once the descriptor is set it is immutable and care must be taken
// to not modify it.
table = &tableVersionState{
TableDescriptor: *tableDesc,
expiration: expiration,
leased: true,
}
// ValidateTable instead of Validate, even though we have a txn available,
// so we don't block reads waiting for this table version.
if err := table.ValidateTable(); err != nil {
return err
}
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)`
leaseExpiration := table.leaseExpiration()
count, err := p.exec(
ctx, insertLease, table.ID, int(table.Version), nodeID, &leaseExpiration,
)
if err != nil {
return err
}
if count != 1 {
return errors.Errorf("%s: expected 1 result, found %d", insertLease, count)
}
return nil
})
if err == nil && s.testingKnobs.LeaseAcquiredEvent != nil {
s.testingKnobs.LeaseAcquiredEvent(table.TableDescriptor, nil)
}
return table, err
}
// Release a previously acquired table descriptor.
func (s LeaseStore) release(ctx context.Context, stopper *stop.Stopper, table *tableVersionState) {
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", table)
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)`
leaseExpiration := table.leaseExpiration()
count, err := p.exec(
ctx, deleteLease, table.ID, int(table.Version), nodeID, &leaseExpiration)
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", table, count)
}
return nil
})
if s.testingKnobs.LeaseReleasedEvent != nil {
s.testingKnobs.LeaseReleasedEvent(table.TableDescriptor, err)
}
if err == nil {
break
}
log.Warningf(ctx, "error releasing lease %q: %s", table, 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++
// We need to set ModificationTime to the transaction's commit
// timestamp. Since this is a SERIALZIABLE transaction, that will
// be OrigTimestamp.
modTime := txn.OrigTimestamp()
tableDesc.ModificationTime = modTime
log.Infof(ctx, "publish: descID=%d (%s) version=%d mtime=%s",
tableDesc.ID, tableDesc.Name, tableDesc.Version, modTime.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
}
// Get the table descriptor valid for the expiration time from the store.
// We use a timestamp that is just less than the expiration time to read
// a version of the table descriptor. A tableVersionState with the
// expiration time set to expiration is returned.
//
// This returns an error when Replica.requestCanProceed() returns an
// error when the expiration timestamp is less than the storage layer
// GC threshold.
func (s LeaseStore) getForExpiration(
ctx context.Context, expiration hlc.Timestamp, id sqlbase.ID,
) (*tableVersionState, error) {
var table *tableVersionState
err := s.db.Txn(ctx, func(ctx context.Context, txn *client.Txn) error {
descKey := sqlbase.MakeDescMetadataKey(id)
prevTimestamp := expiration
prevTimestamp.WallTime--
txn.SetFixedTimestamp(ctx, prevTimestamp)
var desc sqlbase.Descriptor
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", id)
}
if !tableDesc.ModificationTime.Less(prevTimestamp) {
return errors.Errorf("internal error: unable to read table= (%d, %s)", id, expiration)
}
// Create a tableVersionState with the table and without a lease.
table = &tableVersionState{
TableDescriptor: *tableDesc,
expiration: expiration,
}
return nil
})
return table, err
}
// tableSet maintains an ordered set of tableVersionState objects sorted
// by version. It supports addition and removal of elements, finding the
// table for a particular version, or finding the most recent table version.
// The order is maintained by insert and remove and there can only be a
// unique entry for a version. Only the last two versions can be leased,
// with the last one being the latest one which is always leased.
//
// Each entry represents a time span [ModificationTime, expiration)
// and can be used by a transaction iif:
// ModificationTime <= transaction.Timestamp < expiration.
type tableSet struct {
data []*tableVersionState
}
func (l *tableSet) 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.WallTime))
}
return buf.String()
}
func (l *tableSet) insert(s *tableVersionState) {
i, match := l.findIndex(s.Version)
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 *tableSet) remove(s *tableVersionState) {
i, match := l.findIndex(s.Version)
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 *tableSet) find(version sqlbase.DescriptorVersion) *tableVersionState {
if i, match := l.findIndex(version); match {
return l.data[i]
}
return nil
}
func (l *tableSet) findIndex(version sqlbase.DescriptorVersion) (int, bool) {
i := sort.Search(len(l.data), func(i int) bool {
s := l.data[i]
return s.Version >= version
})
if i < len(l.data) {
s := l.data[i]
if s.Version == version {
return i, true
}
}
return i, false
}
func (l *tableSet) findNewest() *tableVersionState {
if len(l.data) == 0 {
return nil
}
return l.data[len(l.data)-1]
}
func (l *tableSet) findVersion(version sqlbase.DescriptorVersion) *tableVersionState {
if len(l.data) == 0 {
return nil
}
// 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 table.
tableNameCache *tableNameCache
stopper *stop.Stopper
mu struct {
syncutil.Mutex
// table descriptors sorted by increasing version. This set always
// contains a table descriptor version with a lease as the latest
// entry. 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. In the latter case, a new
// entry is created with the expiration time of the new lease and
// the older entry is removed.
active tableSet
// 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 dropped, or is being dropped.
// If set, leases are released from the store as soon as their
// refcount drops to 0, as opposed to waiting until they expire.
dropped bool
}
}
// acquire returns a version of the table appropriate for the timestamp
// The table will have its refcount incremented, so the caller is
// responsible for calling release() on it.
func (t *tableState) acquire(
ctx context.Context, timestamp hlc.Timestamp, m *LeaseManager,
) (*tableVersionState, error) {
t.mu.Lock()
defer t.mu.Unlock()
// Wait for any existing lease acquisition.
t.acquireWait()
// Acquire a lease if no lease exists or if the latest lease is
// about to expire.
if s := t.mu.active.findNewest(); s == nil || s.hasExpired(timestamp) {
if err := t.acquireNodeLease(ctx, m, hlc.Timestamp{}); err != nil {
return nil, err
}
}
return t.findForTimestamp(ctx, timestamp, m)
}
// ensureVersion ensures that the latest version >= minVersion. It will
// check if the latest known version meets the criterion, or attempt to
// acquire a lease at the latest version with the hope that it meets
// the criterion.
func (t *tableState) ensureVersion(
ctx context.Context, minVersion sqlbase.DescriptorVersion, m *LeaseManager,
) error {
t.mu.Lock()
defer t.mu.Unlock()
if s := t.mu.active.findNewest(); s != nil && minVersion <= s.Version {
return nil
}
if err := t.acquireFreshestFromStoreLocked(ctx, m); err != nil {
return err
}
if s := t.mu.active.findNewest(); s != nil && s.Version < minVersion {
return errors.Errorf("version %d for table %s does not exist yet", minVersion, s.Name)
}
return nil
}
// Find the table descriptor valid for the particular timestamp. This
// function is called after ensuring that there is a lease for the latest
// version of the table descriptor and the lease is far from expiring.
// Normally the latest version of a table descriptor if valid is returned.
// If the valid version doesn't exist it is read from the store. The refcount
// for the returned tableVersionState is incremented.
func (t *tableState) findForTimestamp(
ctx context.Context, timestamp hlc.Timestamp, m *LeaseManager,
) (*tableVersionState, error) {
afterIdx := 0
// Walk back the versions to find one that is valid for the timestamp.
for i := len(t.mu.active.data) - 1; i >= 0; i-- {
// Check to see if the ModififcationTime is valid.
if table := t.mu.active.data[i]; !timestamp.Less(table.ModificationTime) {
if timestamp.Less(table.expiration) {
// Existing valid table version.
table.incRefcount()
return table, nil
}
// We need a version after data[i], but before data[i+1].
// We could very well use the timestamp to read the table
// descriptor, but unfortunately we will not be able to assign
// it a proper expiration time. Therefore, we read table
// descriptors versions one by one from afterIdx back into the
// past until we find a valid one.
afterIdx = i + 1
if afterIdx == len(t.mu.active.data) {
return nil, fmt.Errorf("requesting a table version ahead of latest version")
}
break
}
}
// Read table descriptor versions one by one into the past until we
// find a valid one. Every version is assigned an expiration time that
// is the ModificationTime of the previous one read.
expiration := t.mu.active.data[afterIdx].ModificationTime
var versions []*tableVersionState
// We're called with mu locked, but need to unlock it while reading
// the descriptors from the store.
t.mu.Unlock()
for {
table, err := m.LeaseStore.getForExpiration(ctx, expiration, t.id)
if err != nil {
t.mu.Lock()
return nil, err
}
versions = append(versions, table)
if !timestamp.Less(table.ModificationTime) {
break
}
// Set the expiration time for the next table.
expiration = table.ModificationTime
}
t.mu.Lock()
// Insert all the table versions and return the last one.
var table *tableVersionState
for _, tableVersion := range versions {
// Since we gave up the lock while reading the versions from
// the store we have to ensure that no one else inserted the
// same table version.
table = t.mu.active.findVersion(tableVersion.Version)
if table == nil {
table = tableVersion
t.mu.active.insert(tableVersion)
}
}
table.incRefcount()
return table, 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.mu.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, m *LeaseManager) error {
// Ensure there is no lease acquisition in progress.
t.acquireWait()
// Move forward to acquire a fresh table lease.
// Set the min expiration time to guarantee that the lease acquired is the
// last lease in t.mu.active .
minExpirationTime := hlc.Timestamp{}
newestTable := t.mu.active.findNewest()
if newestTable != nil {
minExpirationTime = newestTable.expiration.Add(int64(time.Millisecond), 0)
}
return t.acquireNodeLease(ctx, m, minExpirationTime)
}
// upsertLocked inserts a lease for a particular table version.
// If an existing lease exists for the table version, it releases
// the older lease and replaces it.
func (t *tableState) upsertLocked(ctx context.Context, table *tableVersionState, m *LeaseManager) {
s := t.mu.active.find(table.Version)
if s == nil {
t.mu.active.insert(table)
return
}
s.mu.Lock()
table.mu.Lock()
// subsume the refcount of the older lease.
table.refcount += s.refcount
s.refcount = 0
s.leased = false
table.mu.Unlock()
s.mu.Unlock()
log.VEventf(ctx, 2, "replaced lease: %s with %s", s, table)
t.mu.active.remove(s)
t.mu.active.insert(table)
t.releaseLease(s, m)
}
// removeInactiveVersions removes inactive versions in t.mu.active.data with refcount 0.
// t.mu must be locked.
func (t *tableState) removeInactiveVersions(m *LeaseManager) {
// A copy of t.mu.active.data must be made since t.mu.active.data will be changed
// within the loop.
for _, table := range append([]*tableVersionState(nil), t.mu.active.data...) {
func() {
table.mu.Lock()
defer table.mu.Unlock()
if table.refcount == 0 {
t.mu.active.remove(table)
if table.leased {
table.leased = false
t.releaseLease(table, m)
}
}
}()
}
}
// acquireWait waits until no lease acquisition is in progress.
func (t *tableState) acquireWait() {
// Spin until no lease acquisition is in progress.
for acquiring := t.mu.acquiring; acquiring != nil; acquiring = t.mu.acquiring {
// We're called with mu locked, but need to unlock it while we wait
// for the in-progress lease acquisition to finish.
t.mu.Unlock()
<-acquiring
t.mu.Lock()
}
}
// If the lease cannot be obtained because the descriptor is in the process of
// being dropped, the error will be errTableDropped.
// minExpirationTime, if not set to the zero value, will be used as a lower
// bound on the expiration of the new table. 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, m *LeaseManager, minExpirationTime hlc.Timestamp,
) error {
if m.isDraining() {
return errors.New("cannot acquire lease when draining")
}
// Notify when lease has been acquired.
t.mu.acquiring = make(chan struct{})
defer func() {
close(t.mu.acquiring)
t.mu.acquiring = nil
}()
// We're called with mu locked, but need to unlock it during lease
// acquisition.
t.mu.Unlock()
table, err := m.LeaseStore.acquire(ctx, t.id, minExpirationTime)
t.mu.Lock()
if err != nil {
return err
}
t.upsertLocked(ctx, table, m)
t.tableNameCache.insert(table)
return nil
}
func (t *tableState) release(table *sqlbase.TableDescriptor, m *LeaseManager) error {
t.mu.Lock()
defer t.mu.Unlock()
s := t.mu.active.find(table.Version)
if s == nil {
return errors.Errorf("table %d version %d not found", table.ID, table.Version)
}
// Decrements the refcount and returns true if the lease has to be removed
// from the store.
decRefcount := func(s *tableVersionState) 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
// invalid.
removeOnceDereferenced := m.LeaseStore.testingKnobs.RemoveOnceDereferenced ||
// Release from the store if the table has been dropped; no leases
// can be acquired any more.
t.mu.dropped ||
// 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.mu.active.findNewest()
s.mu.Lock()
defer s.mu.Unlock()
s.refcount--
log.VEventf(context.TODO(), 2, "release: %s", s)
if s.refcount < 0 {
panic(fmt.Sprintf("negative ref count: %s", s))
}
if s.refcount == 0 && s.leased && removeOnceDereferenced {
s.leased = false
return true
}
return false
}
if decRefcount(s) {
t.mu.active.remove(s)
t.releaseLease(s, m)
}
return nil
}
// release the lease associated with the table version.
// t.mu needs to be locked.
func (t *tableState) releaseLease(table *tableVersionState, m *LeaseManager) {
t.tableNameCache.remove(table)
ctx := context.TODO()
if m.isDraining() {
// Release synchronously to guarantee release before exiting.
m.LeaseStore.release(ctx, t.stopper, table)
return
}
// Release to the store asynchronously, without the tableState lock.
if err := t.stopper.RunAsyncTask(
ctx, "sql.tableState: releasing descriptor lease",
func(ctx context.Context) {
m.LeaseStore.release(ctx, t.stopper, table)
}); err != nil {
log.Warningf(ctx, "error: %s, not releasing lease: %q", err, table)
}
}
// purgeOldVersions removes old unused table descriptor versions older than
// minVersion and releases any associated leases.
// If dropped is set, minVersion is ignored; no lease is acquired and all
// existing unused versions are removed. The table is further marked dropped,
// 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) purgeOldVersions(
ctx context.Context,
db *client.DB,
dropped bool,
minVersion sqlbase.DescriptorVersion,
m *LeaseManager,
) error {
t.mu.Lock()
empty := len(t.mu.active.data) == 0
t.mu.Unlock()
if empty {
// We don't currently have a version on this table, so no need to refresh
// anything.
return nil
}
removeInactives := func(drop bool) {
t.mu.Lock()
defer t.mu.Unlock()
t.mu.dropped = drop
t.removeInactiveVersions(m)
}
if dropped {
removeInactives(dropped)
return nil
}
if err := t.ensureVersion(ctx, minVersion, m); err != nil {
return err
}
// Acquire a lease on the table on the latest version to maintain an
// active lease, so that it doesn't get released when removeInactives()
// is called below. Release this lease after calling removeInactives().
table, err := t.acquire(ctx, m.clock.Now(), m)
if dropped := err == errTableDropped; dropped || err == nil {
removeInactives(dropped)
if table != nil {
return t.release(&table.TableDescriptor, m)
}
return nil
}
return err
}
// LeaseStoreTestingKnobs contains testing knobs.
type LeaseStoreTestingKnobs struct {
// Called after a lease is removed from the store, with any operation error.
// See LeaseRemovalTracker.
LeaseReleasedEvent func(table sqlbase.TableDescriptor, err error)
// Called after a lease is acquired, with any operation error.
LeaseAcquiredEvent func(table sqlbase.TableDescriptor, err error)
// 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 is a cache of table name -> latest table version mappings.
// The LeaseManager updates the cache every time a lease is acquired or released
// from the store. The cache maintains the latest version for each table name.
// All methods are thread-safe.
type tableNameCache struct {
mu syncutil.Mutex
tables map[tableNameCacheKey]*tableVersionState
}
// Resolves a (database ID, table name) to the table descriptor's ID.
// Returns a valid tableVersionState for the table with that name,
// if the name had been previously cached and the cache has a table
// version that has not expired. Returns nil otherwise.
// This method handles normalizing the table name.
// The table'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, timestamp hlc.Timestamp,
) *tableVersionState {
c.mu.Lock()
table, ok := c.tables[makeTableNameCacheKey(dbID, tableName)]
c.mu.Unlock()
if !ok {
return nil
}
table.mu.Lock()
defer table.mu.Unlock()
if !nameMatchesTable(&table.TableDescriptor, dbID, tableName) {
panic(fmt.Sprintf("Out of sync entry in the name cache. "+
"Cache entry: %d.%q -> %d. Lease: %d.%q.",
dbID, tableName, table.ID, table.ParentID, table.Name))
}
if !table.leased {