Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gc_worker: add gc enable variable to enable/disable gc #8282

Merged
merged 20 commits into from
Dec 3, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
a619b49
gc_worker: add gc enable variable to enable/disable gc
crazycs520 Nov 12, 2018
bcf2317
address comment
crazycs520 Nov 13, 2018
fd9e8b9
Merge branch 'master' of https://github.com/pingcap/tidb into gc_disable
crazycs520 Nov 13, 2018
52a37f1
address comment
crazycs520 Nov 13, 2018
5d31e75
address comment
crazycs520 Nov 15, 2018
7ab65cf
add transaction for check gc enable and update safepoint
crazycs520 Nov 15, 2018
735825c
add comment
crazycs520 Nov 15, 2018
eec5db0
Merge branch 'master' of https://github.com/pingcap/tidb into gc_disable
crazycs520 Nov 15, 2018
999625c
address comment
crazycs520 Nov 19, 2018
5f712f4
address comment
crazycs520 Nov 19, 2018
2189a01
address comment
crazycs520 Nov 19, 2018
d3a42b8
refine code
crazycs520 Nov 19, 2018
0210279
Merge branch 'master' of https://github.com/pingcap/tidb into gc_disable
crazycs520 Nov 20, 2018
1a7c891
Merge branch 'master' of https://github.com/pingcap/tidb into gc_disable
crazycs520 Nov 21, 2018
6ea790b
return false when load gc enable variable failed
crazycs520 Dec 3, 2018
a675aac
Merge branch 'master' of https://github.com/pingcap/tidb into gc_disable
crazycs520 Dec 3, 2018
9e98156
Merge branch 'master' of https://github.com/pingcap/tidb into gc_disable
crazycs520 Dec 3, 2018
7715d9a
Merge branch 'master' into gc_disable
crazycs520 Dec 3, 2018
f3811f5
Merge branch 'master' into gc_disable
crazycs520 Dec 3, 2018
9fd9593
Merge branch 'master' into gc_disable
crazycs520 Dec 3, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions store/tikv/gcworker/gc_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"
"os"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -117,6 +118,11 @@ const (
gcMaxConcurrency = 128
// We don't want gc to sweep out the cached info belong to other processes, like coprocessor.
gcScanLockLimit = tikv.ResolvedCacheSize / 2

gcEnableKey = "tikv_gc_enable"
gcEnableValue = "true"
gcDisableValue = "false"
gcDefaultEnablevalue = true
crazycs520 marked this conversation as resolved.
Show resolved Hide resolved
)

var gcSafePointCacheInterval = tikv.GcSafePointCacheInterval
Expand All @@ -130,6 +136,7 @@ var gcVariableComments = map[string]string{
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)",
gcConcurrencyKey: "How many go routines used to do GC parallel, [1, 128], default 2",
gcEnableKey: "Current GC enable status",
}

func (w *GCWorker) start(ctx context.Context, wg *sync.WaitGroup) {
Expand Down Expand Up @@ -250,6 +257,14 @@ func (w *GCWorker) leaderTick(ctx context.Context) error {
// prepare checks preconditions 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) {
enable, err := w.checkGCEnable()
if !enable {
log.Warn("[gc worker] gc status is disabled.")
return false, 0, nil
}
if err != nil {
crazycs520 marked this conversation as resolved.
Show resolved Hide resolved
return false, 0, errors.Trace(err)
}
now, err := w.getOracleTime()
if err != nil {
return false, 0, errors.Trace(err)
Expand Down Expand Up @@ -283,6 +298,10 @@ func (w *GCWorker) getOracleTime() (time.Time, error) {
return time.Unix(sec, nsec), nil
}

func (w *GCWorker) checkGCEnable() (bool, error) {
return w.loadGCEnableStatus()
}

func (w *GCWorker) checkGCInterval(now time.Time) (bool, error) {
runInterval, err := w.loadDurationWithDefault(gcRunIntervalKey, gcDefaultRunInterval)
if err != nil {
Expand Down Expand Up @@ -495,6 +514,25 @@ func (w *GCWorker) loadGCConcurrencyWithDefault() (int, error) {
return jobConcurrency, nil
}

func (w *GCWorker) loadGCEnableStatus() (bool, error) {
str, err := w.loadValueFromSysTable(gcEnableKey)
zimulala marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return gcDefaultEnablevalue, errors.Trace(err)
}
if str == "" {
defaultValue := gcDisableValue
if gcDefaultEnablevalue {
crazycs520 marked this conversation as resolved.
Show resolved Hide resolved
defaultValue = gcEnableValue
}
err = w.saveValueToSysTable(gcEnableKey, defaultValue)
if err != nil {
return gcDefaultEnablevalue, errors.Trace(err)
}
return gcDefaultEnablevalue, nil
}
return strings.ToLower(str) == gcEnableValue, nil
crazycs520 marked this conversation as resolved.
Show resolved Hide resolved
}

func (w *GCWorker) resolveLocks(ctx context.Context, safePoint uint64) error {
metrics.GCWorkerCounter.WithLabelValues("resolve_locks").Inc()

Expand Down
13 changes: 13 additions & 0 deletions store/tikv/gcworker/gc_worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,19 @@ func (s *testGCWorkerSuite) TestPrepareGC(c *C) {
concurrency, err = s.gcWorker.loadGCConcurrencyWithDefault()
c.Assert(err, IsNil)
c.Assert(concurrency, Equals, gcMaxConcurrency)

// Change GC enable status.
s.oracle.AddOffset(time.Minute * 40)
err = s.gcWorker.saveValueToSysTable(gcEnableKey, gcDisableValue)
c.Assert(err, IsNil)
ok, _, err = s.gcWorker.prepare()
c.Assert(err, IsNil)
c.Assert(ok, IsFalse)
err = s.gcWorker.saveValueToSysTable(gcEnableKey, gcEnableValue)
crazycs520 marked this conversation as resolved.
Show resolved Hide resolved
c.Assert(err, IsNil)
ok, _, err = s.gcWorker.prepare()
c.Assert(err, IsNil)
c.Assert(ok, IsTrue)
}

func (s *testGCWorkerSuite) TestDoGCForOneRegion(c *C) {
Expand Down