forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gc_worker.go
879 lines (799 loc) · 23.4 KB
/
gc_worker.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
// Copyright 2016 PingCAP, Inc.
//
// 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package tikv
import (
"bytes"
"fmt"
"os"
"strconv"
"sync"
"time"
log "github.com/Sirupsen/logrus"
"github.com/coreos/etcd/clientv3"
"github.com/juju/errors"
"github.com/pingcap/kvproto/pkg/kvrpcpb"
"github.com/pingcap/tidb"
"github.com/pingcap/tidb/ddl/util"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/meta"
"github.com/pingcap/tidb/privilege"
"github.com/pingcap/tidb/store/tikv/oracle"
"github.com/pingcap/tidb/store/tikv/tikvrpc"
"github.com/pingcap/tidb/terror"
goctx "golang.org/x/net/context"
)
// GCWorker periodically triggers GC process on tikv server.
type GCWorker struct {
uuid string
desc string
store *tikvStore
gcIsRunning bool
lastFinish time.Time
cancel goctx.CancelFunc
done chan error
session tidb.Session
}
// SafePointKV is used for a seamingless integration for mockTest and runtime.
type SafePointKV interface {
Put(k string, v string) error
Get(k string) (string, error)
}
// MockSafePointKV implements SafePointKV at mock test
type MockSafePointKV struct {
store map[string]string
mockLock sync.RWMutex
}
// NewMockSafePointKV creates an instance of MockSafePointKV
func NewMockSafePointKV() *MockSafePointKV {
return &MockSafePointKV{
store: make(map[string]string),
}
}
// Put implements the Put method for SafePointKV
func (w *MockSafePointKV) Put(k string, v string) error {
w.mockLock.Lock()
defer w.mockLock.Unlock()
w.store[k] = v
return nil
}
// Get implements the Get method for SafePointKV
func (w *MockSafePointKV) Get(k string) (string, error) {
w.mockLock.RLock()
defer w.mockLock.RUnlock()
elem, _ := w.store[k]
return elem, nil
}
// EtcdSafePointKV implements SafePointKV at runtime
type EtcdSafePointKV struct {
cli *clientv3.Client
}
// NewEtcdSafePointKV creates an instance of EtcdSafePointKV
func NewEtcdSafePointKV(addrs []string) (*EtcdSafePointKV, error) {
etcdCli, err := createEtcdKV(addrs)
if err != nil {
return nil, errors.Trace(err)
}
return &EtcdSafePointKV{cli: etcdCli}, nil
}
// Put implements the Put method for SafePointKV
func (w *EtcdSafePointKV) Put(k string, v string) error {
ctx, cancel := goctx.WithTimeout(goctx.Background(), time.Second*5)
_, err := w.cli.Put(ctx, k, v)
cancel()
if err != nil {
return errors.Trace(err)
}
return nil
}
// Get implements the Get method for SafePointKV
func (w *EtcdSafePointKV) Get(k string) (string, error) {
ctx, cancel := goctx.WithTimeout(goctx.Background(), time.Second*5)
resp, err := w.cli.Get(ctx, k)
cancel()
if err != nil {
return "", errors.Trace(err)
}
if len(resp.Kvs) > 0 {
return string(resp.Kvs[0].Value), nil
}
return "", nil
}
// StartSafePointChecker triggers SafePoint Checker on each tidb
func (w *GCWorker) StartSafePointChecker() {
go func() {
d := gcSafePointUpdateInterval
for {
select {
case spCachedTime := <-time.After(d):
cachedSafePoint, err := w.loadSafePoint(gcSavedSafePoint)
if err == nil {
w.store.UpdateSPCache(cachedSafePoint, spCachedTime)
d = gcSafePointUpdateInterval
} else {
log.Error("The read fails:", errors.ErrorStack(err))
d = gcSafePointQuickRepeatInterval
}
case <-w.store.spMsg:
return
}
}
}()
}
// NewGCWorker creates a GCWorker instance.
func NewGCWorker(store kv.Storage, enableGC bool) (*GCWorker, error) {
ver, err := store.CurrentVersion()
if err != nil {
return nil, errors.Trace(err)
}
hostName, err := os.Hostname()
if err != nil {
hostName = "unknown"
}
worker := &GCWorker{
uuid: strconv.FormatUint(ver.Ver, 16),
desc: fmt.Sprintf("host:%s, pid:%d, start at %s", hostName, os.Getpid(), time.Now()),
store: store.(*tikvStore),
gcIsRunning: false,
lastFinish: time.Now(),
done: make(chan error),
}
var ctx goctx.Context
ctx, worker.cancel = goctx.WithCancel(goctx.Background())
var wg sync.WaitGroup
if enableGC {
wg.Add(1)
go worker.start(ctx, &wg)
wg.Wait() // Wait create session finish in worker, some test code depend on this to avoid race.
}
worker.StartSafePointChecker()
return worker, nil
}
// Close stops background goroutines.
func (w *GCWorker) Close() {
w.cancel()
}
const (
gcTimeFormat = "20060102-15:04:05 -0700 MST"
gcWorkerTickInterval = time.Minute
gcWorkerLease = time.Minute * 2
gcLeaderUUIDKey = "tikv_gc_leader_uuid"
gcLeaderDescKey = "tikv_gc_leader_desc"
gcLeaderLeaseKey = "tikv_gc_leader_lease"
gcLastRunTimeKey = "tikv_gc_last_run_time"
gcRunIntervalKey = "tikv_gc_run_interval"
gcDefaultRunInterval = time.Minute * 10
gcWaitTime = time.Minute * 10
gcLifeTimeKey = "tikv_gc_life_time"
gcDefaultLifeTime = time.Minute * 10
gcSafePointKey = "tikv_gc_safe_point"
gcSavedSafePoint = "/tidb/store/gcworker/saved_safe_point"
gcSafePointCacheInterval = time.Second * 100
gcSafePointUpdateInterval = time.Second * 10
gcSafePointQuickRepeatInterval = time.Second
gcCPUTimeInaccuracyBound = time.Second
)
var gcVariableComments = map[string]string{
gcLeaderUUIDKey: "Current GC worker leader UUID. (DO NOT EDIT)",
gcLeaderDescKey: "Host name and pid of current GC leader. (DO NOT EDIT)",
gcLeaderLeaseKey: "Current GC worker leader lease. (DO NOT EDIT)",
gcLastRunTimeKey: "The time when last GC starts. (DO NOT EDIT)",
gcRunIntervalKey: "GC run interval, at least 10m, in Go format.",
gcLifeTimeKey: "All versions within life time will not be collected by GC, at least 10m, in Go format.",
gcSafePointKey: "All versions after safe point can be accessed. (DO NOT EDIT)",
}
func (w *GCWorker) start(ctx goctx.Context, wg *sync.WaitGroup) {
log.Infof("[gc worker] %s start.", w.uuid)
w.session = createSession(w.store)
w.tick(ctx) // Immediately tick once to initialize configs.
wg.Done()
ticker := time.NewTicker(gcWorkerTickInterval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
w.tick(ctx)
case err := <-w.done:
w.gcIsRunning = false
w.lastFinish = time.Now()
if err != nil {
log.Errorf("[gc worker] runGCJob error: %v", err)
break
}
case <-ctx.Done():
log.Infof("[gc worker] (%s) quit.", w.uuid)
return
}
}
}
func createSession(store kv.Storage) tidb.Session {
for {
session, err := tidb.CreateSession(store)
if err != nil {
log.Warnf("[gc worker] create session err: %v", err)
continue
}
// Disable privilege check for gc worker session.
privilege.BindPrivilegeManager(session, nil)
session.GetSessionVars().InRestrictedSQL = true
return session
}
}
func (w *GCWorker) tick(ctx goctx.Context) {
isLeader, err := w.checkLeader()
if err != nil {
log.Warnf("[gc worker] check leader err: %v", err)
return
}
if isLeader {
err = w.leaderTick(ctx)
if err != nil {
log.Warnf("[gc worker] leader tick err: %v", err)
}
} else {
// Config metrics should always be updated by leader, set them to 0 when current instance is not leader.
gcConfigGauge.WithLabelValues(gcRunIntervalKey).Set(0)
gcConfigGauge.WithLabelValues(gcLifeTimeKey).Set(0)
}
}
const notBootstrappedVer = 0
func (w *GCWorker) storeIsBootstrapped() bool {
var ver int64
err := kv.RunInNewTxn(w.store, false, func(txn kv.Transaction) error {
var err error
t := meta.NewMeta(txn)
ver, err = t.GetBootstrapVersion()
return errors.Trace(err)
})
if err != nil {
log.Errorf("[gc worker] check bootstrapped error %v", err)
return false
}
return ver > notBootstrappedVer
}
// Leader of GC worker checks if it should start a GC job every tick.
func (w *GCWorker) leaderTick(ctx goctx.Context) error {
if w.gcIsRunning {
return nil
}
ok, safePoint, err := w.prepare()
if err != nil || !ok {
w.gcIsRunning = false
return errors.Trace(err)
}
// When the worker is just started, or an old GC job has just finished,
// wait a while before starting a new job.
if time.Since(w.lastFinish) < gcWaitTime {
w.gcIsRunning = false
return nil
}
w.gcIsRunning = true
log.Infof("[gc worker] %s starts GC job, safePoint: %v", w.uuid, safePoint)
go w.runGCJob(ctx, safePoint)
return nil
}
// prepare checks required conditions for starting a GC job. It returns a bool
// that indicates whether the GC job should start and the new safePoint.
func (w *GCWorker) prepare() (bool, uint64, error) {
now, err := w.getOracleTime()
if err != nil {
return false, 0, errors.Trace(err)
}
ok, err := w.checkGCInterval(now)
if err != nil || !ok {
return false, 0, errors.Trace(err)
}
newSafePoint, err := w.calculateNewSafePoint(now)
if err != nil || newSafePoint == nil {
return false, 0, errors.Trace(err)
}
err = w.saveTime(gcLastRunTimeKey, now, w.session)
if err != nil {
return false, 0, errors.Trace(err)
}
err = w.saveTime(gcSafePointKey, *newSafePoint, w.session)
if err != nil {
return false, 0, errors.Trace(err)
}
return true, oracle.ComposeTS(oracle.GetPhysical(*newSafePoint), 0), nil
}
func (w *GCWorker) getOracleTime() (time.Time, error) {
currentVer, err := w.store.CurrentVersion()
if err != nil {
return time.Time{}, errors.Trace(err)
}
physical := oracle.ExtractPhysical(currentVer.Ver)
sec, nsec := physical/1e3, (physical%1e3)*1e6
return time.Unix(sec, nsec), nil
}
func (w *GCWorker) checkGCInterval(now time.Time) (bool, error) {
runInterval, err := w.loadDurationWithDefault(gcRunIntervalKey, gcDefaultRunInterval)
if err != nil {
return false, errors.Trace(err)
}
gcConfigGauge.WithLabelValues(gcRunIntervalKey).Set(runInterval.Seconds())
lastRun, err := w.loadTime(gcLastRunTimeKey, w.session)
if err != nil {
return false, errors.Trace(err)
}
if lastRun != nil && lastRun.Add(*runInterval).After(now) {
return false, nil
}
return true, nil
}
func (w *GCWorker) calculateNewSafePoint(now time.Time) (*time.Time, error) {
lifeTime, err := w.loadDurationWithDefault(gcLifeTimeKey, gcDefaultLifeTime)
if err != nil {
return nil, errors.Trace(err)
}
gcConfigGauge.WithLabelValues(gcLifeTimeKey).Set(lifeTime.Seconds())
lastSafePoint, err := w.loadTime(gcSafePointKey, w.session)
if err != nil {
return nil, errors.Trace(err)
}
safePoint := now.Add(-*lifeTime)
// We should never decrease safePoint.
if lastSafePoint != nil && safePoint.Before(*lastSafePoint) {
return nil, nil
}
return &safePoint, nil
}
// RunGCJob sends GC command to KV. it is exported for testing purpose, do not use it with GCWorker at the same time.
func RunGCJob(ctx goctx.Context, store kv.Storage, safePoint uint64, identifier string) error {
s, ok := store.(*tikvStore)
if !ok {
return errors.New("should use tikv driver")
}
err := resolveLocks(ctx, s, safePoint, identifier)
if err != nil {
return errors.Trace(err)
}
err = doGC(ctx, s, safePoint, identifier)
if err != nil {
return errors.Trace(err)
}
return nil
}
func (w *GCWorker) runGCJob(ctx goctx.Context, safePoint uint64) {
gcWorkerCounter.WithLabelValues("run_job").Inc()
err := resolveLocks(ctx, w.store, safePoint, w.uuid)
if err != nil {
w.done <- errors.Trace(err)
return
}
err = w.deleteRanges(ctx, safePoint)
if err != nil {
w.done <- errors.Trace(err)
return
}
err = doGC(ctx, w.store, safePoint, w.uuid)
if err != nil {
log.Error("do GC returns an error", err)
w.gcIsRunning = false
w.done <- errors.Trace(err)
return
}
w.done <- nil
}
func (w *GCWorker) deleteRanges(ctx goctx.Context, safePoint uint64) error {
gcWorkerCounter.WithLabelValues("delete_range").Inc()
session := createSession(w.store)
ranges, err := util.LoadDeleteRanges(session, safePoint)
session.Close()
if err != nil {
return errors.Trace(err)
}
bo := NewBackoffer(gcDeleteRangeMaxBackoff, goctx.Background())
log.Infof("[gc worker] %s start delete %v ranges", w.uuid, len(ranges))
startTime := time.Now()
regions := 0
for _, r := range ranges {
startKey, rangeEndKey := r.Range()
for {
select {
case <-ctx.Done():
return errors.New("[gc worker] gc job canceled")
default:
}
loc, err := w.store.regionCache.LocateKey(bo, startKey)
if err != nil {
return errors.Trace(err)
}
endKey := loc.EndKey
if loc.Contains(rangeEndKey) {
endKey = rangeEndKey
}
req := &tikvrpc.Request{
Type: tikvrpc.CmdDeleteRange,
DeleteRange: &kvrpcpb.DeleteRangeRequest{
StartKey: startKey,
EndKey: endKey,
},
}
resp, err := w.store.SendReq(bo, req, loc.Region, readTimeoutMedium)
if err != nil {
return errors.Trace(err)
}
regionErr, err := resp.GetRegionError()
if err != nil {
return errors.Trace(err)
}
if regionErr != nil {
err = bo.Backoff(boRegionMiss, errors.New(regionErr.String()))
if err != nil {
return errors.Trace(err)
}
continue
}
deleteRangeResp := resp.DeleteRange
if deleteRangeResp == nil {
return errors.Trace(errBodyMissing)
}
if err := deleteRangeResp.GetError(); err != "" {
return errors.Errorf("unexpected delete range err: %v", err)
}
regions++
if bytes.Equal(endKey, rangeEndKey) {
break
}
startKey = endKey
}
session := createSession(w.store)
err := util.CompleteDeleteRange(session, r)
session.Close()
if err != nil {
return errors.Trace(err)
}
}
log.Infof("[gc worker] %s finish delete %v ranges, regions: %v, cost time: %s", w.uuid, len(ranges), regions, time.Since(startTime))
gcHistogram.WithLabelValues("delete_ranges").Observe(time.Since(startTime).Seconds())
return nil
}
func resolveLocks(ctx goctx.Context, store *tikvStore, safePoint uint64, identifier string) error {
gcWorkerCounter.WithLabelValues("resolve_locks").Inc()
req := &tikvrpc.Request{
Type: tikvrpc.CmdScanLock,
ScanLock: &kvrpcpb.ScanLockRequest{
MaxVersion: safePoint,
},
}
bo := NewBackoffer(gcResolveLockMaxBackoff, goctx.Background())
log.Infof("[gc worker] %s start resolve locks, safePoint: %v.", identifier, safePoint)
startTime := time.Now()
regions, totalResolvedLocks := 0, 0
var key []byte
for {
select {
case <-ctx.Done():
return errors.New("[gc worker] gc job canceled")
default:
}
loc, err := store.regionCache.LocateKey(bo, key)
if err != nil {
return errors.Trace(err)
}
resp, err := store.SendReq(bo, req, loc.Region, readTimeoutMedium)
if err != nil {
return errors.Trace(err)
}
regionErr, err := resp.GetRegionError()
if err != nil {
return errors.Trace(err)
}
if regionErr != nil {
err = bo.Backoff(boRegionMiss, errors.New(regionErr.String()))
if err != nil {
return errors.Trace(err)
}
continue
}
locksResp := resp.ScanLock
if locksResp == nil {
return errors.Trace(errBodyMissing)
}
if locksResp.GetError() != nil {
return errors.Errorf("unexpected scanlock error: %s", locksResp)
}
locksInfo := locksResp.GetLocks()
locks := make([]*Lock, len(locksInfo))
for i := range locksInfo {
locks[i] = newLock(locksInfo[i])
}
ok, err1 := store.lockResolver.ResolveLocks(bo, locks)
if err1 != nil {
return errors.Trace(err1)
}
if !ok {
err = bo.Backoff(boTxnLock, errors.Errorf("remain locks: %d", len(locks)))
if err != nil {
return errors.Trace(err)
}
continue
}
regions++
totalResolvedLocks += len(locks)
key = loc.EndKey
if len(key) == 0 {
break
}
}
log.Infof("[gc worker] %s finish resolve locks, safePoint: %v, regions: %v, total resolved: %v, cost time: %s", identifier, safePoint, regions, totalResolvedLocks, time.Since(startTime))
gcHistogram.WithLabelValues("resolve_locks").Observe(time.Since(startTime).Seconds())
return nil
}
func doGC(ctx goctx.Context, store *tikvStore, safePoint uint64, identifier string) error {
gcWorkerCounter.WithLabelValues("do_gc").Inc()
err := store.gcWorker.saveSafePoint(gcSavedSafePoint, safePoint)
if err != nil {
return errors.Trace(err)
}
// Sleep to wait for all other tidb instances update their safepoint cache.
time.Sleep(gcSafePointCacheInterval)
req := &tikvrpc.Request{
Type: tikvrpc.CmdGC,
GC: &kvrpcpb.GCRequest{
SafePoint: safePoint,
},
}
bo := NewBackoffer(gcMaxBackoff, goctx.Background())
log.Infof("[gc worker] %s start gc, safePoint: %v.", identifier, safePoint)
startTime := time.Now()
regions := 0
var key []byte
for {
select {
case <-ctx.Done():
return errors.New("[gc worker] gc job canceled")
default:
}
loc, err := store.regionCache.LocateKey(bo, key)
if err != nil {
return errors.Trace(err)
}
resp, err := store.SendReq(bo, req, loc.Region, readTimeoutLong)
if err != nil {
return errors.Trace(err)
}
regionErr, err := resp.GetRegionError()
if err != nil {
return errors.Trace(err)
}
if regionErr != nil {
err = bo.Backoff(boRegionMiss, errors.New(regionErr.String()))
if err != nil {
return errors.Trace(err)
}
continue
}
gcResp := resp.GC
if gcResp == nil {
return errors.Trace(errBodyMissing)
}
if gcResp.GetError() != nil {
return errors.Errorf("unexpected gc error: %s", gcResp.GetError())
}
regions++
key = loc.EndKey
if len(key) == 0 {
break
}
}
log.Infof("[gc worker] %s finish gc, safePoint: %v, regions: %v, cost time: %s", identifier, safePoint, regions, time.Since(startTime))
gcHistogram.WithLabelValues("do_gc").Observe(time.Since(startTime).Seconds())
return nil
}
func (w *GCWorker) checkLeader() (bool, error) {
gcWorkerCounter.WithLabelValues("check_leader").Inc()
session := createSession(w.store)
defer session.Close()
_, err := session.Execute("BEGIN")
if err != nil {
return false, errors.Trace(err)
}
leader, err := w.loadValueFromSysTable(gcLeaderUUIDKey, session)
if err != nil {
_, err1 := session.Execute("ROLLBACK")
terror.Log(errors.Trace(err1))
return false, errors.Trace(err)
}
log.Debugf("[gc worker] got leader: %s", leader)
if leader == w.uuid {
err = w.saveTime(gcLeaderLeaseKey, time.Now().Add(gcWorkerLease), session)
if err != nil {
_, err1 := session.Execute("ROLLBACK")
terror.Log(errors.Trace(err1))
return false, errors.Trace(err)
}
_, err = session.Execute("COMMIT")
if err != nil {
return false, errors.Trace(err)
}
return true, nil
}
_, err = session.Execute("BEGIN")
if err != nil {
return false, errors.Trace(err)
}
lease, err := w.loadTime(gcLeaderLeaseKey, session)
if err != nil {
return false, errors.Trace(err)
}
if lease == nil || lease.Before(time.Now()) {
log.Debugf("[gc worker] register %s as leader", w.uuid)
gcWorkerCounter.WithLabelValues("register_leader").Inc()
err = w.saveValueToSysTable(gcLeaderUUIDKey, w.uuid, session)
if err != nil {
_, err1 := session.Execute("ROLLBACK")
terror.Log(errors.Trace(err1))
return false, errors.Trace(err)
}
err = w.saveValueToSysTable(gcLeaderDescKey, w.desc, session)
if err != nil {
_, err1 := session.Execute("ROLLBACK")
terror.Log(errors.Trace(err1))
return false, errors.Trace(err)
}
err = w.saveTime(gcLeaderLeaseKey, time.Now().Add(gcWorkerLease), session)
if err != nil {
_, err1 := session.Execute("ROLLBACK")
terror.Log(errors.Trace(err1))
return false, errors.Trace(err)
}
_, err = session.Execute("COMMIT")
if err != nil {
return false, errors.Trace(err)
}
return true, nil
}
_, err1 := session.Execute("ROLLBACK")
terror.Log(errors.Trace(err1))
return false, nil
}
func (w *GCWorker) saveSafePoint(key string, t uint64) error {
s := strconv.FormatUint(t, 10)
err := w.store.kv.Put(gcSavedSafePoint, s)
if err != nil {
log.Error("save safepoint failed:", err)
return errors.Trace(err)
}
return nil
}
func (w *GCWorker) loadSafePoint(key string) (uint64, error) {
str, err := w.store.kv.Get(gcSavedSafePoint)
if err != nil {
return 0, errors.Trace(err)
}
if str == "" {
return 0, nil
}
t, err := strconv.ParseUint(str, 10, 64)
if err != nil {
return 0, errors.Trace(err)
}
return t, nil
}
func (w *GCWorker) saveTime(key string, t time.Time, s tidb.Session) error {
err := w.saveValueToSysTable(key, t.Format(gcTimeFormat), s)
return errors.Trace(err)
}
func (w *GCWorker) loadTime(key string, s tidb.Session) (*time.Time, error) {
str, err := w.loadValueFromSysTable(key, s)
if err != nil {
return nil, errors.Trace(err)
}
if str == "" {
return nil, nil
}
t, err := time.Parse(gcTimeFormat, str)
if err != nil {
return nil, errors.Trace(err)
}
return &t, nil
}
func (w *GCWorker) saveDuration(key string, d time.Duration) error {
err := w.saveValueToSysTable(key, d.String(), w.session)
return errors.Trace(err)
}
func (w *GCWorker) loadDuration(key string) (*time.Duration, error) {
str, err := w.loadValueFromSysTable(key, w.session)
if err != nil {
return nil, errors.Trace(err)
}
if str == "" {
return nil, nil
}
d, err := time.ParseDuration(str)
if err != nil {
return nil, errors.Trace(err)
}
return &d, nil
}
func (w *GCWorker) loadDurationWithDefault(key string, def time.Duration) (*time.Duration, error) {
d, err := w.loadDuration(key)
if err != nil {
return nil, errors.Trace(err)
}
if d == nil {
err = w.saveDuration(key, def)
if err != nil {
return nil, errors.Trace(err)
}
return &def, nil
}
return d, nil
}
func (w *GCWorker) loadValueFromSysTable(key string, s tidb.Session) (string, error) {
stmt := fmt.Sprintf(`SELECT (variable_value) FROM mysql.tidb WHERE variable_name='%s' FOR UPDATE`, key)
rs, err := s.Execute(stmt)
if err != nil {
return "", errors.Trace(err)
}
row, err := rs[0].Next()
if err != nil {
return "", errors.Trace(err)
}
if row == nil {
log.Debugf("[gc worker] load kv, %s:nil", key)
return "", nil
}
value := row.Data[0].GetString()
log.Debugf("[gc worker] load kv, %s:%s", key, value)
return value, nil
}
func (w *GCWorker) saveValueToSysTable(key, value string, s tidb.Session) error {
stmt := fmt.Sprintf(`INSERT INTO mysql.tidb VALUES ('%[1]s', '%[2]s', '%[3]s')
ON DUPLICATE KEY
UPDATE variable_value = '%[2]s', comment = '%[3]s'`,
key, value, gcVariableComments[key])
if s == nil {
return errors.New("[saveValueToSysTable session is nil]")
}
_, err := s.Execute(stmt)
log.Debugf("[gc worker] save kv, %s:%s %v", key, value, err)
return errors.Trace(err)
}
// MockGCWorker is for test.
type MockGCWorker struct {
worker *GCWorker
}
// NewMockGCWorker creates a MockGCWorker instance ONLY for test.
func NewMockGCWorker(store kv.Storage) (*MockGCWorker, error) {
ver, err := store.CurrentVersion()
if err != nil {
return nil, errors.Trace(err)
}
hostName, err := os.Hostname()
if err != nil {
hostName = "unknown"
}
worker := &GCWorker{
uuid: strconv.FormatUint(ver.Ver, 16),
desc: fmt.Sprintf("host:%s, pid:%d, start at %s", hostName, os.Getpid(), time.Now()),
store: store.(*tikvStore),
gcIsRunning: false,
lastFinish: time.Now(),
done: make(chan error),
}
worker.session, err = tidb.CreateSession(worker.store)
if err != nil {
log.Errorf("initialize MockGCWorker session fail: %s", err)
return nil, errors.Trace(err)
}
privilege.BindPrivilegeManager(worker.session, nil)
worker.session.GetSessionVars().InRestrictedSQL = true
return &MockGCWorker{worker: worker}, nil
}
// DeleteRanges call deleteRanges internally, just for test.
func (w *MockGCWorker) DeleteRanges(ctx goctx.Context, safePoint uint64) error {
log.Errorf("deleteRanges is called")
return w.worker.deleteRanges(ctx, safePoint)
}