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 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
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
5 changes: 4 additions & 1 deletion cmd/backup-manager/app/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ const (
// TikvGCLifeTime is the safe gc life time for dump tidb cluster data
TikvGCLifeTime = "72h"

// LegacyTikvGCVariable is the legacy tikv gc life time variable name before v5.0
LegacyTikvGCVariable = "tikv_gc_life_time"

// TikvGCVariable is the tikv gc life time variable name
TikvGCVariable = "tikv_gc_life_time"
TikvGCVariable = "tidb_gc_life_time"
shonge marked this conversation as resolved.
Show resolved Hide resolved

// TidbMetaDB is the database name for store meta info
TidbMetaDB = "mysql"
Expand Down
47 changes: 41 additions & 6 deletions cmd/backup-manager/app/util/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,56 @@ 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.TikvGCVariable
sql = fmt.Sprintln("select @@?")
} else {
gcVariable = constants.LegacyTikvGCVariable
sql = fmt.Sprintf("select variable_value from %s where variable_name= ?", constants.TidbMetaTable)
}
row := db.QueryRowContext(ctx, sql, gcVariable)
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, 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
}
if has {
gcVariable = constants.TikvGCVariable
sql = fmt.Sprintln("SET GLOBAL ? = ?")
} else {
gcVariable = constants.LegacyTikvGCVariable
sql = fmt.Sprintf("update %s set variable_value = ? where variable_name = ?", constants.TidbMetaTable)
}
_, err = db.ExecContext(ctx, sql, gcTime, gcVariable)
shonge marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return fmt.Errorf("set cluster %s %s failed, sql: %s, err: %v", bo, constants.TikvGCVariable, 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.TikvGCVariable)
err := row.Scan(&tikvGCTime)
if err != nil {
return false, fmt.Errorf("query cluster %s %s failed, sql: %s, err: %v", bo, constants.TikvGCVariable, 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
}