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

[WIP] Set tidb_gc_life_time by sysvar #3922

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
29 changes: 24 additions & 5 deletions charts/tidb-backup/templates/scripts/_start_backup.sh.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,38 @@ then
password_str="-p${TIDB_PASSWORD}"
fi

gc_life_time=`/usr/bin/mysql -h${host} -P4000 -u${TIDB_USER} ${password_str} -Nse "select variable_value from mysql.tidb where variable_name='tikv_gc_life_time';"`
has_tidb_gc_life_time=`/usr/bin/mysql -h${host} -P4000 -u${TIDB_USER} ${password_str} -Nse "SHOW GLOBAL VARIABLES LIKE 'tidb_gc_life_time';" | wc -l`

if [ ${has_tidb_gc_life_time} -ge 1 ];
then
gc_life_time=`/usr/bin/mysql -h${host} -P4000 -u${TIDB_USER} ${password_str} -Nse "select @@tidb_gc_life_time;"`
else
gc_life_time=`/usr/bin/mysql -h${host} -P4000 -u${TIDB_USER} ${password_str} -Nse "select variable_value from mysql.tidb where variable_name='tikv_gc_life_time';"`
fi
echo "Old TiKV GC life time is ${gc_life_time}"

function reset_gc_lifetime() {
echo "Reset TiKV GC life time to ${gc_life_time}"
/usr/bin/mysql -h${host} -P4000 -u${TIDB_USER} ${password_str} -Nse "update mysql.tidb set variable_value='${gc_life_time}' where variable_name='tikv_gc_life_time';"
/usr/bin/mysql -h${host} -P4000 -u${TIDB_USER} ${password_str} -Nse "select variable_name,variable_value from mysql.tidb where variable_name='tikv_gc_life_time';"
if [ ${has_tidb_gc_life_time} -ge 1 ];
then
/usr/bin/mysql -h${host} -P4000 -u${TIDB_USER} ${password_str} -Nse "SET GLOBAL tidb_gc_life_time = '${gc_life_time}';"
/usr/bin/mysql -h${host} -P4000 -u${TIDB_USER} ${password_str} -Nse "SHOW GLOBAL VARIABLES LIKE 'tidb_gc_life_time';"
else
/usr/bin/mysql -h${host} -P4000 -u${TIDB_USER} ${password_str} -Nse "update mysql.tidb set variable_value='${gc_life_time}' where variable_name='tikv_gc_life_time';"
/usr/bin/mysql -h${host} -P4000 -u${TIDB_USER} ${password_str} -Nse "select variable_name,variable_value from mysql.tidb where variable_name='tikv_gc_life_time';"
fi
}
trap "reset_gc_lifetime" EXIT

echo "Increase TiKV GC life time to {{ .Values.tikvGCLifeTime | default "720h" }}"
/usr/bin/mysql -h${host} -P4000 -u${TIDB_USER} ${password_str} -Nse "update mysql.tidb set variable_value='{{ .Values.tikvGCLifeTime | default "720h" }}' where variable_name='tikv_gc_life_time';"
/usr/bin/mysql -h${host} -P4000 -u${TIDB_USER} ${password_str} -Nse "select variable_name,variable_value from mysql.tidb where variable_name='tikv_gc_life_time';"
if [ ${has_tidb_gc_life_time} -ge 1 ];
then
/usr/bin/mysql -h${host} -P4000 -u${TIDB_USER} ${password_str} -Nse "SET GLOBAL tidb_gc_life_time = '{{ .Values.tikvGCLifeTime | default "720h" }}';"
/usr/bin/mysql -h${host} -P4000 -u${TIDB_USER} ${password_str} -Nse "SHOW GLOBAL VARIABLES LIKE 'tidb_gc_life_time';"
else
/usr/bin/mysql -h${host} -P4000 -u${TIDB_USER} ${password_str} -Nse "update mysql.tidb set variable_value='{{ .Values.tikvGCLifeTime | default "720h" }}' where variable_name='tikv_gc_life_time';"
/usr/bin/mysql -h${host} -P4000 -u${TIDB_USER} ${password_str} -Nse "select variable_name,variable_value from mysql.tidb where variable_name='tikv_gc_life_time';"
fi

if [ -n "{{ .Values.initialCommitTs }}" ];
then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,25 @@ then
password_str="-p${TIDB_PASSWORD}"
fi

gc_life_time=`/usr/bin/mysql -h${host} -P4000 -u${TIDB_USER} ${password_str} -Nse "select variable_value from mysql.tidb where variable_name='tikv_gc_life_time';"`
has_tidb_gc_life_time=`/usr/bin/mysql -h${host} -P4000 -u${TIDB_USER} ${password_str} -Nse "SHOW GLOBAL VARIABLES LIKE 'tidb_gc_life_time';" | wc -l`

if [ ${has_tidb_gc_life_time} -ge 1 ];
then
gc_life_time=`/usr/bin/mysql -h${host} -P4000 -u${TIDB_USER} ${password_str} -Nse "select @@tidb_gc_life_time;"`
else
gc_life_time=`/usr/bin/mysql -h${host} -P4000 -u${TIDB_USER} ${password_str} -Nse "select variable_value from mysql.tidb where variable_name='tikv_gc_life_time';"`
fi
echo "Old TiKV GC life time is ${gc_life_time}"

echo "Increase TiKV GC life time to {{ .Values.scheduledBackup.tikvGCLifeTime | default "720h" }}"
/usr/bin/mysql -h${host} -P4000 -u${TIDB_USER} ${password_str} -Nse "update mysql.tidb set variable_value='{{ .Values.scheduledBackup.tikvGCLifeTime | default "720h" }}' where variable_name='tikv_gc_life_time';"
/usr/bin/mysql -h${host} -P4000 -u${TIDB_USER} ${password_str} -Nse "select variable_name,variable_value from mysql.tidb where variable_name='tikv_gc_life_time';"
if [ ${has_tidb_gc_life_time} -ge 1 ];
then
/usr/bin/mysql -h${host} -P4000 -u${TIDB_USER} ${password_str} -Nse "SET GLOBAL tidb_gc_life_time = '{{ .Values.scheduledBackup.tikvGCLifeTime | default "720h" }}';"
/usr/bin/mysql -h${host} -P4000 -u${TIDB_USER} ${password_str} -Nse "SHOW GLOBAL VARIABLES LIKE 'tidb_gc_life_time';"
else
/usr/bin/mysql -h${host} -P4000 -u${TIDB_USER} ${password_str} -Nse "update mysql.tidb set variable_value='{{ .Values.scheduledBackup.tikvGCLifeTime | default "720h" }}' where variable_name='tikv_gc_life_time';"
/usr/bin/mysql -h${host} -P4000 -u${TIDB_USER} ${password_str} -Nse "select variable_name,variable_value from mysql.tidb where variable_name='tikv_gc_life_time';"
fi

/mydumper \
--outputdir=${backupPath} \
Expand All @@ -34,8 +47,14 @@ echo "Increase TiKV GC life time to {{ .Values.scheduledBackup.tikvGCLifeTime |
{{ .Values.scheduledBackup.options }}

echo "Reset TiKV GC life time to ${gc_life_time}"
/usr/bin/mysql -h${host} -P4000 -u${TIDB_USER} ${password_str} -Nse "update mysql.tidb set variable_value='${gc_life_time}' where variable_name='tikv_gc_life_time';"
/usr/bin/mysql -h${host} -P4000 -u${TIDB_USER} ${password_str} -Nse "select variable_name,variable_value from mysql.tidb where variable_name='tikv_gc_life_time';"
if [ ${has_tidb_gc_life_time} -ge 1 ];
then
/usr/bin/mysql -h${host} -P4000 -u${TIDB_USER} ${password_str} -Nse "SET GLOBAL tidb_gc_life_time = '${gc_life_time}';"
/usr/bin/mysql -h${host} -P4000 -u${TIDB_USER} ${password_str} -Nse "SHOW GLOBAL VARIABLES LIKE 'tidb_gc_life_time';"
else
/usr/bin/mysql -h${host} -P4000 -u${TIDB_USER} ${password_str} -Nse "update mysql.tidb set variable_value='${gc_life_time}' where variable_name='tikv_gc_life_time';"
/usr/bin/mysql -h${host} -P4000 -u${TIDB_USER} ${password_str} -Nse "select variable_name,variable_value from mysql.tidb where variable_name='tikv_gc_life_time';"
fi

{{- if .Values.scheduledBackup.gcp }}
# Once we know there are no more credentials that will be logged we can run with -x
Expand Down
14 changes: 7 additions & 7 deletions cmd/backup-manager/app/backup/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func (bm *Manager) performBackup(ctx context.Context, backup *v1alpha1.Backup, d
oldTikvGCTime, err = bm.GetTikvGCLifeTime(ctx, db)
if err != nil {
errs = append(errs, err)
klog.Errorf("cluster %s get %s failed, err: %s", bm, constants.TikvGCVariable, err)
klog.Errorf("cluster %s get %s failed, err: %s", bm, constants.TidbGCVariable, err)
uerr := bm.StatusUpdater.Update(backup, &v1alpha1.BackupCondition{
Type: v1alpha1.BackupFailed,
Status: corev1.ConditionTrue,
Expand All @@ -186,12 +186,12 @@ func (bm *Manager) performBackup(ctx context.Context, backup *v1alpha1.Backup, d
errs = append(errs, uerr)
return errorutils.NewAggregate(errs)
}
klog.Infof("cluster %s %s is %s", bm, constants.TikvGCVariable, oldTikvGCTime)
klog.Infof("cluster %s %s is %s", bm, constants.TidbGCVariable, oldTikvGCTime)

oldTikvGCTimeDuration, err = time.ParseDuration(oldTikvGCTime)
if err != nil {
errs = append(errs, err)
klog.Errorf("cluster %s parse old %s failed, err: %s", bm, constants.TikvGCVariable, err)
klog.Errorf("cluster %s parse old %s failed, err: %s", bm, constants.TidbGCVariable, err)
uerr := bm.StatusUpdater.Update(backup, &v1alpha1.BackupCondition{
Type: v1alpha1.BackupFailed,
Status: corev1.ConditionTrue,
Expand All @@ -207,7 +207,7 @@ func (bm *Manager) performBackup(ctx context.Context, backup *v1alpha1.Backup, d
tikvGCTimeDuration, err = time.ParseDuration(tikvGCLifeTime)
if err != nil {
errs = append(errs, err)
klog.Errorf("cluster %s parse configured %s failed, err: %s", bm, constants.TikvGCVariable, err)
klog.Errorf("cluster %s parse configured %s failed, err: %s", bm, constants.TidbGCVariable, err)
uerr := bm.StatusUpdater.Update(backup, &v1alpha1.BackupCondition{
Type: v1alpha1.BackupFailed,
Status: corev1.ConditionTrue,
Expand All @@ -222,7 +222,7 @@ func (bm *Manager) performBackup(ctx context.Context, backup *v1alpha1.Backup, d
tikvGCTimeDuration, err = time.ParseDuration(tikvGCLifeTime)
if err != nil {
errs = append(errs, err)
klog.Errorf("cluster %s parse default %s failed, err: %s", bm, constants.TikvGCVariable, err)
klog.Errorf("cluster %s parse default %s failed, err: %s", bm, constants.TidbGCVariable, err)
uerr := bm.StatusUpdater.Update(backup, &v1alpha1.BackupCondition{
Type: v1alpha1.BackupFailed,
Status: corev1.ConditionTrue,
Expand All @@ -248,7 +248,7 @@ func (bm *Manager) performBackup(ctx context.Context, backup *v1alpha1.Backup, d
errs = append(errs, uerr)
return errorutils.NewAggregate(errs)
}
klog.Infof("set cluster %s %s to %s success", bm, constants.TikvGCVariable, tikvGCLifeTime)
klog.Infof("set cluster %s %s to %s success", bm, constants.TidbGCVariable, tikvGCLifeTime)
}
}

Expand Down Expand Up @@ -284,7 +284,7 @@ func (bm *Manager) performBackup(ctx context.Context, backup *v1alpha1.Backup, d
errs = append(errs, uerr)
return errorutils.NewAggregate(errs)
}
klog.Infof("reset cluster %s %s to %s success", bm, constants.TikvGCVariable, oldTikvGCTime)
klog.Infof("reset cluster %s %s to %s success", bm, constants.TidbGCVariable, oldTikvGCTime)
}

if backupErr != nil {
Expand Down
9 changes: 7 additions & 2 deletions cmd/backup-manager/app/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,13 @@ const (
// TikvGCLifeTime is the safe gc life time for dump tidb cluster data
TikvGCLifeTime = "72h"

// TikvGCVariable is the tikv gc life time variable name
TikvGCVariable = "tikv_gc_life_time"
// LegacyTikvGCVariable is the legacy tikv gc life time variable name before v5.0
// Move tikv gc configuration to sysvars
// Ref: https://github.com/pingcap/tidb/pull/21988
LegacyTikvGCVariable = "tikv_gc_life_time"

// TidbGCVariable is the tidb gc life time variable name
TidbGCVariable = "tidb_gc_life_time"

// TidbMetaDB is the database name for store meta info
TidbMetaDB = "mysql"
Expand Down
14 changes: 7 additions & 7 deletions cmd/backup-manager/app/export/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func (bm *BackupManager) performBackup(ctx context.Context, backup *v1alpha1.Bac
oldTikvGCTime, err := bm.GetTikvGCLifeTime(ctx, db)
if err != nil {
errs = append(errs, err)
klog.Errorf("cluster %s get %s failed, err: %s", bm, constants.TikvGCVariable, err)
klog.Errorf("cluster %s get %s failed, err: %s", bm, constants.TidbGCVariable, err)
uerr := bm.StatusUpdater.Update(backup, &v1alpha1.BackupCondition{
Type: v1alpha1.BackupFailed,
Status: corev1.ConditionTrue,
Expand All @@ -175,12 +175,12 @@ func (bm *BackupManager) performBackup(ctx context.Context, backup *v1alpha1.Bac
errs = append(errs, uerr)
return errorutils.NewAggregate(errs)
}
klog.Infof("cluster %s %s is %s", bm, constants.TikvGCVariable, oldTikvGCTime)
klog.Infof("cluster %s %s is %s", bm, constants.TidbGCVariable, oldTikvGCTime)

oldTikvGCTimeDuration, err := time.ParseDuration(oldTikvGCTime)
if err != nil {
errs = append(errs, err)
klog.Errorf("cluster %s parse old %s failed, err: %s", bm, constants.TikvGCVariable, err)
klog.Errorf("cluster %s parse old %s failed, err: %s", bm, constants.TidbGCVariable, err)
uerr := bm.StatusUpdater.Update(backup, &v1alpha1.BackupCondition{
Type: v1alpha1.BackupFailed,
Status: corev1.ConditionTrue,
Expand All @@ -198,7 +198,7 @@ func (bm *BackupManager) performBackup(ctx context.Context, backup *v1alpha1.Bac
tikvGCTimeDuration, err = time.ParseDuration(tikvGCLifeTime)
if err != nil {
errs = append(errs, err)
klog.Errorf("cluster %s parse configured %s failed, err: %s", bm, constants.TikvGCVariable, err)
klog.Errorf("cluster %s parse configured %s failed, err: %s", bm, constants.TidbGCVariable, err)
uerr := bm.StatusUpdater.Update(backup, &v1alpha1.BackupCondition{
Type: v1alpha1.BackupFailed,
Status: corev1.ConditionTrue,
Expand All @@ -213,7 +213,7 @@ func (bm *BackupManager) performBackup(ctx context.Context, backup *v1alpha1.Bac
tikvGCTimeDuration, err = time.ParseDuration(tikvGCLifeTime)
if err != nil {
errs = append(errs, err)
klog.Errorf("cluster %s parse default %s failed, err: %s", bm, constants.TikvGCVariable, err)
klog.Errorf("cluster %s parse default %s failed, err: %s", bm, constants.TidbGCVariable, err)
uerr := bm.StatusUpdater.Update(backup, &v1alpha1.BackupCondition{
Type: v1alpha1.BackupFailed,
Status: corev1.ConditionTrue,
Expand All @@ -239,7 +239,7 @@ func (bm *BackupManager) performBackup(ctx context.Context, backup *v1alpha1.Bac
errs = append(errs, uerr)
return errorutils.NewAggregate(errs)
}
klog.Infof("set cluster %s %s to %s success", bm, constants.TikvGCVariable, constants.TikvGCLifeTime)
klog.Infof("set cluster %s %s to %s success", bm, constants.TidbGCVariable, constants.TikvGCLifeTime)
}

backupFullPath := bm.getBackupFullPath()
Expand Down Expand Up @@ -280,7 +280,7 @@ func (bm *BackupManager) performBackup(ctx context.Context, backup *v1alpha1.Bac
errs = append(errs, uerr)
return errorutils.NewAggregate(errs)
}
klog.Infof("reset cluster %s %s to %s success", bm, constants.TikvGCVariable, oldTikvGCTime)
klog.Infof("reset cluster %s %s to %s success", bm, constants.TidbGCVariable, oldTikvGCTime)
}

if backupErr != nil {
Expand Down
14 changes: 7 additions & 7 deletions cmd/backup-manager/app/restore/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (rm *Manager) performRestore(ctx context.Context, restore *v1alpha1.Restore
oldTikvGCTime, err = rm.GetTikvGCLifeTime(ctx, db)
if err != nil {
errs = append(errs, err)
klog.Errorf("cluster %s get %s failed, err: %s", rm, constants.TikvGCVariable, err)
klog.Errorf("cluster %s get %s failed, err: %s", rm, constants.TidbGCVariable, err)
uerr := rm.StatusUpdater.Update(restore, &v1alpha1.RestoreCondition{
Type: v1alpha1.RestoreFailed,
Status: corev1.ConditionTrue,
Expand All @@ -182,12 +182,12 @@ func (rm *Manager) performRestore(ctx context.Context, restore *v1alpha1.Restore
errs = append(errs, uerr)
return errorutils.NewAggregate(errs)
}
klog.Infof("cluster %s %s is %s", rm, constants.TikvGCVariable, oldTikvGCTime)
klog.Infof("cluster %s %s is %s", rm, constants.TidbGCVariable, oldTikvGCTime)

oldTikvGCTimeDuration, err = time.ParseDuration(oldTikvGCTime)
if err != nil {
errs = append(errs, err)
klog.Errorf("cluster %s parse old %s failed, err: %s", rm, constants.TikvGCVariable, err)
klog.Errorf("cluster %s parse old %s failed, err: %s", rm, constants.TidbGCVariable, err)
uerr := rm.StatusUpdater.Update(restore, &v1alpha1.RestoreCondition{
Type: v1alpha1.RestoreFailed,
Status: corev1.ConditionTrue,
Expand All @@ -203,7 +203,7 @@ func (rm *Manager) performRestore(ctx context.Context, restore *v1alpha1.Restore
tikvGCTimeDuration, err = time.ParseDuration(tikvGCLifeTime)
if err != nil {
errs = append(errs, err)
klog.Errorf("cluster %s parse configured %s failed, err: %s", rm, constants.TikvGCVariable, err)
klog.Errorf("cluster %s parse configured %s failed, err: %s", rm, constants.TidbGCVariable, err)
uerr := rm.StatusUpdater.Update(restore, &v1alpha1.RestoreCondition{
Type: v1alpha1.RestoreFailed,
Status: corev1.ConditionTrue,
Expand All @@ -218,7 +218,7 @@ func (rm *Manager) performRestore(ctx context.Context, restore *v1alpha1.Restore
tikvGCTimeDuration, err = time.ParseDuration(tikvGCLifeTime)
if err != nil {
errs = append(errs, err)
klog.Errorf("cluster %s parse default %s failed, err: %s", rm, constants.TikvGCVariable, err)
klog.Errorf("cluster %s parse default %s failed, err: %s", rm, constants.TidbGCVariable, err)
uerr := rm.StatusUpdater.Update(restore, &v1alpha1.RestoreCondition{
Type: v1alpha1.RestoreFailed,
Status: corev1.ConditionTrue,
Expand All @@ -244,7 +244,7 @@ func (rm *Manager) performRestore(ctx context.Context, restore *v1alpha1.Restore
errs = append(errs, uerr)
return errorutils.NewAggregate(errs)
}
klog.Infof("set cluster %s %s to %s success", rm, constants.TikvGCVariable, tikvGCLifeTime)
klog.Infof("set cluster %s %s to %s success", rm, constants.TidbGCVariable, tikvGCLifeTime)
}
}

Expand All @@ -271,7 +271,7 @@ func (rm *Manager) performRestore(ctx context.Context, restore *v1alpha1.Restore
errs = append(errs, uerr)
return errorutils.NewAggregate(errs)
}
klog.Infof("reset cluster %s %s to %s success", rm, constants.TikvGCVariable, oldTikvGCTime)
klog.Infof("reset cluster %s %s to %s success", rm, constants.TidbGCVariable, oldTikvGCTime)
}

if restoreErr != nil {
Expand Down
53 changes: 45 additions & 8 deletions cmd/backup-manager/app/util/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,58 @@ func (bo *GenericOptions) GetDSN(enabledTLSClient bool) (string, error) {
}

shonge marked this conversation as resolved.
Show resolved Hide resolved
func (bo *GenericOptions) GetTikvGCLifeTime(ctx context.Context, db *sql.DB) (string, error) {
var tikvGCTime string
sql := fmt.Sprintf("select variable_value from %s where variable_name= ?", constants.TidbMetaTable)
row := db.QueryRowContext(ctx, sql, constants.TikvGCVariable)
err := row.Scan(&tikvGCTime)
var tikvGCTime, sql, gcVariable string
has, err := bo.HasTiDBGCLifeTime(ctx, db)
if err != nil {
return "", err
}
if has {
gcVariable = constants.TidbGCVariable
sql = fmt.Sprintf("select @@%s", gcVariable)
} else {
gcVariable = constants.LegacyTikvGCVariable
sql = fmt.Sprintf("select variable_value from %s where variable_name= %s", constants.TidbMetaTable, gcVariable)
}
row := db.QueryRowContext(ctx, sql)
err = row.Scan(&tikvGCTime)
if err != nil {
return tikvGCTime, fmt.Errorf("query cluster %s %s failed, sql: %s, err: %v", bo, constants.TikvGCVariable, sql, err)
return tikvGCTime, fmt.Errorf("query cluster %s %s failed, sql: %s, err: %v", bo, constants.TidbGCVariable, sql, err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return tikvGCTime, fmt.Errorf("query cluster %s %s failed, sql: %s, err: %v", bo, constants.TidbGCVariable, sql, err)
return tikvGCTime, fmt.Errorf("query cluster %s %s failed, sql: %s, err: %v", bo, gcVariable, sql, err)

}
return tikvGCTime, nil
}

func (bo *GenericOptions) SetTikvGCLifeTime(ctx context.Context, db *sql.DB, gcTime string) error {
sql := fmt.Sprintf("update %s set variable_value = ? where variable_name = ?", constants.TidbMetaTable)
_, err := db.ExecContext(ctx, sql, gcTime, constants.TikvGCVariable)
var sql, gcVariable string
has, err := bo.HasTiDBGCLifeTime(ctx, db)
if err != nil {
return err
}
// Move tikv gc configuration to sysvars
// Ref: https://github.com/pingcap/tidb/pull/21988
if has {
gcVariable = constants.TidbGCVariable
sql = fmt.Sprintf("SET GLOBAL %s = %s", gcVariable, gcTime)
} else {
gcVariable = constants.LegacyTikvGCVariable
sql = fmt.Sprintf("update %s set variable_value = %s where variable_name = %s", constants.TidbMetaTable, gcTime, gcVariable)
}
_, err = db.ExecContext(ctx, sql)
if err != nil {
return fmt.Errorf("set cluster %s %s failed, sql: %s, err: %v", bo, constants.TikvGCVariable, sql, err)
return fmt.Errorf("set cluster %s %s failed, sql: %s, err: %v", bo, constants.TidbGCVariable, sql, err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return fmt.Errorf("set cluster %s %s failed, sql: %s, err: %v", bo, constants.TidbGCVariable, sql, err)
return fmt.Errorf("set cluster %s %s failed, sql: %s, err: %v", bo, gcVariable, sql, err)

}
return nil
}

func (bo *GenericOptions) HasTiDBGCLifeTime(ctx context.Context, db *sql.DB) (bool, error) {
var tikvGCTime string
sql := fmt.Sprintln("SHOW GLOBAL VARIABLES LIKE ?")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to use fmt.Sprintln here, right?

row := db.QueryRowContext(ctx, sql, constants.TidbGCVariable)
err := row.Scan(&tikvGCTime)
if err != nil {
return false, fmt.Errorf("query cluster %s %s failed, sql: %s, err: %v", bo, constants.TidbGCVariable, sql, err)
}
if len(tikvGCTime) > 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you may use sql.NullString for judgment here?

return true, nil
}
return false, nil
}