From 93cd6d28c84ac2ebf2034225ceedd18f0ebd0f29 Mon Sep 17 00:00:00 2001 From: qupeng Date: Thu, 18 Mar 2021 15:01:36 +0800 Subject: [PATCH] cherry pick #23389 to release-5.0 Signed-off-by: ti-srebot --- store/gcworker/gc_worker.go | 44 ++++++++++++-------------------- store/gcworker/gc_worker_test.go | 26 ++++++++----------- 2 files changed, 27 insertions(+), 43 deletions(-) diff --git a/store/gcworker/gc_worker.go b/store/gcworker/gc_worker.go index b85095850d183..130d4908578c2 100644 --- a/store/gcworker/gc_worker.go +++ b/store/gcworker/gc_worker.go @@ -636,16 +636,7 @@ func (w *GCWorker) runGCJob(ctx context.Context, safePoint uint64, concurrency i return errors.Trace(err) } - useDistributedGC, err := w.checkUseDistributedGC() - if err != nil { - logutil.Logger(ctx).Error("[gc worker] failed to load gc mode, fall back to central mode.", - zap.String("uuid", w.uuid), - zap.Error(err)) - metrics.GCJobFailureCounter.WithLabelValues("check_gc_mode").Inc() - useDistributedGC = false - } - - if useDistributedGC { + if w.checkUseDistributedGC() { err = w.uploadSafePointToPD(ctx, safePoint) if err != nil { logutil.Logger(ctx).Error("[gc worker] failed to upload safe point to PD", @@ -949,27 +940,24 @@ func (w *GCWorker) loadGCConcurrencyWithDefault() (int, error) { return jobConcurrency, nil } -func (w *GCWorker) checkUseDistributedGC() (bool, error) { - str, err := w.loadValueFromSysTable(gcModeKey) - if err != nil { - return false, errors.Trace(err) - } - if str == "" { +// Central mode is deprecated in v5.0. This function will always return true. +func (w *GCWorker) checkUseDistributedGC() bool { + mode, err := w.loadValueFromSysTable(gcModeKey) + if err == nil && mode == "" { err = w.saveValueToSysTable(gcModeKey, gcModeDefault) - if err != nil { - return false, errors.Trace(err) - } - str = gcModeDefault } - if strings.EqualFold(str, gcModeDistributed) { - return true, nil - } - if strings.EqualFold(str, gcModeCentral) { - return false, nil + if err != nil { + logutil.BgLogger().Error("[gc worker] failed to load gc mode, fall back to distributed mode", + zap.String("uuid", w.uuid), + zap.Error(err)) + metrics.GCJobFailureCounter.WithLabelValues("check_gc_mode").Inc() + } else if strings.EqualFold(mode, gcModeCentral) { + logutil.BgLogger().Warn("[gc worker] distributed mode will be used as central mode is deprecated") + } else if !strings.EqualFold(mode, gcModeDistributed) { + logutil.BgLogger().Warn("[gc worker] distributed mode will be used", + zap.String("invalid gc mode", mode)) } - logutil.BgLogger().Warn("[gc worker] distributed mode will be used", - zap.String("invalid gc mode", str)) - return true, nil + return true } func (w *GCWorker) checkUsePhysicalScanLock() (bool, error) { diff --git a/store/gcworker/gc_worker_test.go b/store/gcworker/gc_worker_test.go index c4006fa3f31aa..8273801102a29 100644 --- a/store/gcworker/gc_worker_test.go +++ b/store/gcworker/gc_worker_test.go @@ -487,30 +487,28 @@ func (s *testGCWorkerSuite) TestDoGC(c *C) { } func (s *testGCWorkerSuite) TestCheckGCMode(c *C) { - useDistributedGC, err := s.gcWorker.checkUseDistributedGC() - c.Assert(err, IsNil) + useDistributedGC := s.gcWorker.checkUseDistributedGC() c.Assert(useDistributedGC, Equals, true) // Now the row must be set to the default value. str, err := s.gcWorker.loadValueFromSysTable(gcModeKey) c.Assert(err, IsNil) c.Assert(str, Equals, gcModeDistributed) + // Central mode is deprecated in v5.0. err = s.gcWorker.saveValueToSysTable(gcModeKey, gcModeCentral) c.Assert(err, IsNil) - useDistributedGC, err = s.gcWorker.checkUseDistributedGC() + useDistributedGC = s.gcWorker.checkUseDistributedGC() c.Assert(err, IsNil) - c.Assert(useDistributedGC, Equals, false) + c.Assert(useDistributedGC, Equals, true) err = s.gcWorker.saveValueToSysTable(gcModeKey, gcModeDistributed) c.Assert(err, IsNil) - useDistributedGC, err = s.gcWorker.checkUseDistributedGC() - c.Assert(err, IsNil) + useDistributedGC = s.gcWorker.checkUseDistributedGC() c.Assert(useDistributedGC, Equals, true) err = s.gcWorker.saveValueToSysTable(gcModeKey, "invalid_mode") c.Assert(err, IsNil) - useDistributedGC, err = s.gcWorker.checkUseDistributedGC() - c.Assert(err, IsNil) + useDistributedGC = s.gcWorker.checkUseDistributedGC() c.Assert(useDistributedGC, Equals, true) } @@ -987,11 +985,10 @@ func (s *testGCWorkerSuite) TestRunGCJob(c *C) { gcSafePointCacheInterval = 0 // Test distributed mode - useDistributedGC, err := s.gcWorker.checkUseDistributedGC() - c.Assert(err, IsNil) + useDistributedGC := s.gcWorker.checkUseDistributedGC() c.Assert(useDistributedGC, IsTrue) safePoint := s.mustAllocTs(c) - err = s.gcWorker.runGCJob(context.Background(), safePoint, 1) + err := s.gcWorker.runGCJob(context.Background(), safePoint, 1) c.Assert(err, IsNil) pdSafePoint := s.mustGetSafePointFromPd(c) @@ -1004,12 +1001,11 @@ func (s *testGCWorkerSuite) TestRunGCJob(c *C) { err = s.gcWorker.runGCJob(context.Background(), safePoint-1, 1) c.Assert(err, NotNil) - // Test central mode + // Central mode is deprecated in v5.0, fallback to distributed mode if it's set. err = s.gcWorker.saveValueToSysTable(gcModeKey, gcModeCentral) c.Assert(err, IsNil) - useDistributedGC, err = s.gcWorker.checkUseDistributedGC() - c.Assert(err, IsNil) - c.Assert(useDistributedGC, IsFalse) + useDistributedGC = s.gcWorker.checkUseDistributedGC() + c.Assert(useDistributedGC, IsTrue) p := s.createGCProbe(c, "k1") safePoint = s.mustAllocTs(c)