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

log-backup: set gc disable when restore log #39729

Merged
merged 6 commits into from
Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
27 changes: 27 additions & 0 deletions br/pkg/task/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,23 @@ func (s *streamMgr) checkStreamStartEnable(g glue.Glue) error {
return nil
}

// SetGcStatus sets status of GC enabled or disabled.
func SetGcStatus(g glue.Glue, store kv.Storage, enable bool) error {
se, err := g.CreateSession(store)
if err != nil {
return errors.Trace(err)
}

execCtx := se.GetSessionCtx().(sqlexec.RestrictedSQLExecutor)
if err = utils.SetGcEnableStatus(execCtx, enable); err != nil {
log.Warn("failed to set gc status", zap.Bool("target-gc-status", enable), zap.Error(err))
return errors.Trace(err)
}

log.Info("set gc status", zap.Bool("enabled", enable))
return nil
}

// RunStreamCommand run all kinds of `stream task`
func RunStreamCommand(
ctx context.Context,
Expand Down Expand Up @@ -1142,6 +1159,16 @@ func restoreStream(
// Always run the post-work even on error, so we don't stuck in the import
// mode or emptied schedulers
defer restorePostWork(ctx, client, restoreSchedulers)
// It need disable GC in TiKV when piTR.
// because the process of PITR is concurrent and kv events isn't sorted by tso.
if err = SetGcStatus(g, mgr.GetStorage(), false); err != nil {
return errors.Trace(err)
}
defer func() {
if err = SetGcStatus(g, mgr.GetStorage(), true); err != nil {
log.Info("failed to set gc enabled", zap.Error(err))
joccau marked this conversation as resolved.
Show resolved Hide resolved
}
}()

err = client.InstallLogFileManager(ctx, cfg.StartTS, cfg.RestoreTS)
if err != nil {
Expand Down
15 changes: 15 additions & 0 deletions br/pkg/utils/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"sync"

"github.com/pingcap/errors"
"github.com/pingcap/log"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/sessionctx"
Expand Down Expand Up @@ -98,6 +99,20 @@ func IsLogBackupEnabled(ctx sqlexec.RestrictedSQLExecutor) (bool, error) {
return true, nil
}

// SetGcEnableStatus sets the status of GC.
// gc.ratio-threshold = -1.0, which represents disable gc in TiKV.
// gc.ratio-threshold = 1.1 is the default value in TiKV.
func SetGcEnableStatus(ctx sqlexec.RestrictedSQLExecutor, enable bool) error {
ratio := 1.1
joccau marked this conversation as resolved.
Show resolved Hide resolved
if !enable {
ratio = -1.0
}

internalCtx := kv.WithInternalSourceType(context.Background(), kv.InternalTxnBR)
_, _, err := ctx.ExecRestrictedSQL(internalCtx, nil, "set config tikv `gc.ratio-threshold`=%?", ratio)
return errors.Trace(err)
}

// LogBackupTaskCountInc increases the count of log backup task.
func LogBackupTaskCountInc() {
LogBackupTaskMutex.Lock()
Expand Down