Skip to content

Commit c21dc26

Browse files
authored
log-backup: set gc disable when restore log (#39729)
close #39602
1 parent c65a93a commit c21dc26

File tree

4 files changed

+129
-3
lines changed

4 files changed

+129
-3
lines changed

br/pkg/restore/client.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -2064,9 +2064,9 @@ func (rc *Client) RestoreKVFiles(
20642064
}
20652065

20662066
if supportBatch {
2067-
err = ApplyKVFilesWithBatchMethod(ctx, iter, int(pitrBatchCount), uint64(pitrBatchSize), applyFunc)
2067+
err = ApplyKVFilesWithBatchMethod(ectx, iter, int(pitrBatchCount), uint64(pitrBatchSize), applyFunc)
20682068
} else {
2069-
err = ApplyKVFilesWithSingelMethod(ctx, iter, applyFunc)
2069+
err = ApplyKVFilesWithSingelMethod(ectx, iter, applyFunc)
20702070
}
20712071
if err != nil {
20722072
return errors.Trace(err)

br/pkg/task/stream.go

+39
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,33 @@ func (s *streamMgr) checkStreamStartEnable(g glue.Glue) error {
458458
return nil
459459
}
460460

461+
type RestoreFunc func() error
462+
463+
// KeepGcDisabled keeps GC disabled and return a function that used to gc enabled.
464+
// gc.ratio-threshold = "-1.0", which represents disable gc in TiKV.
465+
func KeepGcDisabled(g glue.Glue, store kv.Storage) (RestoreFunc, error) {
466+
se, err := g.CreateSession(store)
467+
if err != nil {
468+
return nil, errors.Trace(err)
469+
}
470+
471+
execCtx := se.GetSessionCtx().(sqlexec.RestrictedSQLExecutor)
472+
oldRatio, err := utils.GetGcRatio(execCtx)
473+
if err != nil {
474+
return nil, errors.Trace(err)
475+
}
476+
477+
newRatio := "-1.0"
478+
err = utils.SetGcRatio(execCtx, newRatio)
479+
if err != nil {
480+
return nil, errors.Trace(err)
481+
}
482+
483+
return func() error {
484+
return utils.SetGcRatio(execCtx, oldRatio)
485+
}, nil
486+
}
487+
461488
// RunStreamCommand run all kinds of `stream task`
462489
func RunStreamCommand(
463490
ctx context.Context,
@@ -1143,6 +1170,18 @@ func restoreStream(
11431170
// mode or emptied schedulers
11441171
defer restorePostWork(ctx, client, restoreSchedulers)
11451172

1173+
// It need disable GC in TiKV when PiTR.
1174+
// because the process of PITR is concurrent and kv events isn't sorted by tso.
1175+
restoreGc, err := KeepGcDisabled(g, mgr.GetStorage())
1176+
if err != nil {
1177+
return errors.Trace(err)
1178+
}
1179+
defer func() {
1180+
if err = restoreGc(); err != nil {
1181+
log.Error("failed to set gc enabled", zap.Error(err))
1182+
}
1183+
}()
1184+
11461185
err = client.InstallLogFileManager(ctx, cfg.StartTS, cfg.RestoreTS)
11471186
if err != nil {
11481187
return err

br/pkg/utils/db.go

+34
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99
"sync"
1010

11+
"github.com/pingcap/errors"
1112
"github.com/pingcap/log"
1213
"github.com/pingcap/tidb/kv"
1314
"github.com/pingcap/tidb/sessionctx"
@@ -98,6 +99,39 @@ func IsLogBackupEnabled(ctx sqlexec.RestrictedSQLExecutor) (bool, error) {
9899
return true, nil
99100
}
100101

102+
func GetGcRatio(ctx sqlexec.RestrictedSQLExecutor) (string, error) {
103+
valStr := "show config where name = 'gc.ratio-threshold' and type = 'tikv'"
104+
rows, fields, errSQL := ctx.ExecRestrictedSQL(
105+
kv.WithInternalSourceType(context.Background(), kv.InternalTxnBR),
106+
nil,
107+
valStr,
108+
)
109+
if errSQL != nil {
110+
return "", errSQL
111+
}
112+
if len(rows) == 0 {
113+
// no rows mean not support log backup.
114+
return "", nil
115+
}
116+
117+
d := rows[0].GetDatum(3, &fields[3].Column.FieldType)
118+
return d.ToString()
119+
}
120+
121+
func SetGcRatio(ctx sqlexec.RestrictedSQLExecutor, ratio string) error {
122+
_, _, err := ctx.ExecRestrictedSQL(
123+
kv.WithInternalSourceType(context.Background(), kv.InternalTxnBR),
124+
nil,
125+
"set config tikv `gc.ratio-threshold`=%?",
126+
ratio,
127+
)
128+
if err != nil {
129+
return errors.Annotatef(err, "failed to set config `gc.ratio-threshold`=%s", ratio)
130+
}
131+
log.Warn("set config tikv gc.ratio-threshold", zap.String("ratio", ratio))
132+
return nil
133+
}
134+
101135
// LogBackupTaskCountInc increases the count of log backup task.
102136
func LogBackupTaskCountInc() {
103137
LogBackupTaskMutex.Lock()

br/pkg/utils/db_test.go

+54-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package utils_test
44

55
import (
66
"context"
7+
"strings"
78
"testing"
89

910
"github.com/pingcap/errors"
@@ -35,7 +36,19 @@ func (m *mockRestrictedSQLExecutor) ExecRestrictedSQL(ctx context.Context, opts
3536
if m.errHappen {
3637
return nil, nil, errors.New("injected error")
3738
}
38-
return m.rows, m.fields, nil
39+
40+
if strings.Contains(sql, "show config") {
41+
return m.rows, m.fields, nil
42+
} else if strings.Contains(sql, "set config") && strings.Contains(sql, "gc.ratio-threshold") {
43+
value := args[0].(string)
44+
45+
for _, r := range m.rows {
46+
d := types.Datum{}
47+
d.SetString(value, "")
48+
chunk.MutRow(r).SetDatum(3, d)
49+
}
50+
}
51+
return nil, nil, nil
3952
}
4053

4154
func TestIsLogBackupEnabled(t *testing.T) {
@@ -115,3 +128,43 @@ func TestCheckLogBackupTaskExist(t *testing.T) {
115128
utils.LogBackupTaskCountDec()
116129
require.False(t, utils.CheckLogBackupTaskExist())
117130
}
131+
132+
func TestGc(t *testing.T) {
133+
// config format:
134+
// MySQL [(none)]> show config where name = 'gc.ratio-threshold';
135+
// +------+-------------------+--------------------+-------+
136+
// | Type | Instance | Name | Value |
137+
// +------+-------------------+--------------------+-------+
138+
// | tikv | 172.16.6.46:3460 | gc.ratio-threshold | 1.1 |
139+
// | tikv | 172.16.6.47:3460 | gc.ratio-threshold | 1.1 |
140+
// +------+-------------------+--------------------+-------+
141+
fields := make([]*ast.ResultField, 4)
142+
tps := []*types.FieldType{
143+
types.NewFieldType(mysql.TypeString),
144+
types.NewFieldType(mysql.TypeString),
145+
types.NewFieldType(mysql.TypeString),
146+
types.NewFieldType(mysql.TypeString),
147+
}
148+
for i := 0; i < len(tps); i++ {
149+
rf := new(ast.ResultField)
150+
rf.Column = new(model.ColumnInfo)
151+
rf.Column.FieldType = *tps[i]
152+
fields[i] = rf
153+
}
154+
rows := make([]chunk.Row, 0, 2)
155+
row := chunk.MutRowFromValues("tikv", " 127.0.0.1:20161", "log-backup.enable", "1.1").ToRow()
156+
rows = append(rows, row)
157+
row = chunk.MutRowFromValues("tikv", " 127.0.0.1:20162", "log-backup.enable", "1.1").ToRow()
158+
rows = append(rows, row)
159+
160+
s := &mockRestrictedSQLExecutor{rows: rows, fields: fields}
161+
ratio, err := utils.GetGcRatio(s)
162+
require.Nil(t, err)
163+
require.Equal(t, ratio, "1.1")
164+
165+
err = utils.SetGcRatio(s, "-1.0")
166+
require.Nil(t, err)
167+
ratio, err = utils.GetGcRatio(s)
168+
require.Nil(t, err)
169+
require.Equal(t, ratio, "-1.0")
170+
}

0 commit comments

Comments
 (0)