Skip to content

Commit

Permalink
update tikv_gc_life_time if needed (pingcap#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
holys authored and huachaohuang committed Mar 30, 2018
1 parent ed356a3 commit ed5c67c
Showing 1 changed file with 47 additions and 10 deletions.
57 changes: 47 additions & 10 deletions lightning/restore/restore.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package restore

import (
"database/sql"
"fmt"
"io"
"runtime"
Expand Down Expand Up @@ -28,6 +29,10 @@ var (
concurrency = 1
)

const (
defaultGCLifeTime = 100 * time.Hour
)

func init() {
concurrency = runtime.NumCPU() // TODO ... config

Expand Down Expand Up @@ -725,19 +730,20 @@ func DoChecksum(dsn config.DBStore, tables []string) ([]*RemoteChecksum, error)
db := common.ConnectDB(dsn.Host, dsn.Port, dsn.User, dsn.Psw)
defer db.Close()

// ps : checksum command usually take long time to execute,
// so here need to expand the gcLifeTime for single transaction.
oriGCLifeTime, gcErr := ObtainGCLifeTime(db)
if gcErr == nil {
gcErr = UpdateGCLifeTime(db, "100h")
}
if gcErr != nil {
return nil, errors.Trace(gcErr)
ori, err := increaseGCLifeTime(db)
if err != nil {
return nil, errors.Trace(err)
}
defer UpdateGCLifeTime(db, oriGCLifeTime)
// set it back finally
defer func() {
err = UpdateGCLifeTime(db, ori)
if err != nil {
log.Errorf("update tikv_gc_life_time error %s", errors.ErrorStack(err))
}
}()

// ps : speed up executing checksum temporarily
_, err := db.Exec("set session tidb_checksum_table_concurrency = 32")
_, err = db.Exec("set session tidb_checksum_table_concurrency = 32")
if err != nil {
log.Warnf("failed to set variable @tidb_checksum_table_concurrency: %s", err.Error())
}
Expand Down Expand Up @@ -775,6 +781,37 @@ func DoChecksum(dsn config.DBStore, tables []string) ([]*RemoteChecksum, error)
return checksums, nil
}

func increaseGCLifeTime(db *sql.DB) (oriGCLifeTime string, err error) {
// checksum command usually takes a long time to execute,
// so here need to increase the gcLifeTime for single transaction.
oriGCLifeTime, err = ObtainGCLifeTime(db)
if err != nil {
return "", errors.Trace(err)
}

var increaseGCLifeTime bool
if oriGCLifeTime != "" {
ori, err := time.ParseDuration(oriGCLifeTime)
if err != nil {
return "", errors.Trace(err)
}
if ori < defaultGCLifeTime {
increaseGCLifeTime = true
}
} else {
increaseGCLifeTime = true
}

if increaseGCLifeTime {
err = UpdateGCLifeTime(db, defaultGCLifeTime.String())
if err != nil {
return "", errors.Trace(err)
}
}

return oriGCLifeTime, nil
}

func (tr *TableRestore) excCheckTable() error {
log.Infof("Verify by execute `admin check table` : %s", tr.tableMeta.Name)

Expand Down

0 comments on commit ed5c67c

Please sign in to comment.