Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

Commit

Permalink
close domain after restoring tiflash-replica
Browse files Browse the repository at this point in the history
Signed-off-by: 5kbpers <[email protected]>
  • Loading branch information
5kbpers committed Mar 18, 2020
1 parent 9a98ffc commit 63c1d0f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 53 deletions.
56 changes: 3 additions & 53 deletions cmd/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,10 @@
package cmd

import (
"fmt"
"strings"

"github.com/pingcap/errors"
"github.com/pingcap/log"
pd "github.com/pingcap/pd/v4/client"
"github.com/pingcap/tidb/session"
"github.com/spf13/cobra"
"go.uber.org/zap"

"github.com/pingcap/br/pkg/gluetikv"
"github.com/pingcap/br/pkg/restore"
"github.com/pingcap/br/pkg/summary"
"github.com/pingcap/br/pkg/task"
"github.com/pingcap/br/pkg/utils"
Expand All @@ -38,55 +30,13 @@ func runRestoreRawCommand(command *cobra.Command, cmdName string) error {
return task.RunRestoreRaw(GetDefaultContext(), gluetikv.Glue{}, cmdName, &cfg)
}

func runRestoreTiflashReplicaCommand(command *cobra.Command) error {
func runRestoreTiflashReplicaCommand(command *cobra.Command, cmdName string) error {
cfg := task.RestoreConfig{Config: task.Config{LogProgress: HasLogFile()}}
if err := cfg.ParseFromFlags(command.Flags()); err != nil {
return err
}

_, _, backupMeta, err := task.ReadBackupMeta(GetDefaultContext(), &cfg.Config)
if err != nil {
return err
}

dbs, err := utils.LoadBackupTables(backupMeta)
if err != nil {
return errors.Trace(err)
}

securityOption := pd.SecurityOption{}
tlsConfig := cfg.TLS
if tlsConfig.IsEnabled() {
securityOption.CAPath = tlsConfig.CA
securityOption.CertPath = tlsConfig.Cert
securityOption.KeyPath = tlsConfig.Key
}

pdAddress := strings.Join(cfg.PD, ",")
if len(pdAddress) == 0 {
return errors.New("pd address can not be empty")
}
store, err := tidbGlue.Open(fmt.Sprintf("tikv://%s?disableGC=true", pdAddress), securityOption)
if err != nil {
return err
}
db, err := restore.NewDB(tidbGlue, store)
if err != nil {
return err
}

for _, d := range dbs {
for _, t := range d.Tables {
log.Info("get table", zap.Stringer("name", t.Info.Name), zap.Int("replica", t.TiFlashReplicas))
if t.TiFlashReplicas > 0 {
err := db.AlterTiflashReplica(GetDefaultContext(), t, t.TiFlashReplicas)
if err != nil {
return errors.Trace(err)
}
}
}
}
return nil
return task.RunRestoreTiflashReplica(GetDefaultContext(), tidbGlue, cmdName, &cfg)
}

// NewRestoreCommand returns a restore subcommand
Expand Down Expand Up @@ -161,7 +111,7 @@ func newTiflashReplicaRestoreCommand() *cobra.Command {
Use: "tiflash-replica",
Short: "restore the tiflash replica before the last restore, it must only be used after the last restore failed",
RunE: func(cmd *cobra.Command, _ []string) error {
return runRestoreTiflashReplicaCommand(cmd)
return runRestoreTiflashReplicaCommand(cmd, "Restore TiFlash Replica")
},
}
return command
Expand Down
48 changes: 48 additions & 0 deletions pkg/task/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,3 +369,51 @@ func splitPostWork(ctx context.Context, client *restore.Client, tables []*model.
}
return nil
}

// RunRestoreTiflashReplica restores the replica of tiflash saved in the last restore.
func RunRestoreTiflashReplica(c context.Context, g glue.Glue, cmdName string, cfg *RestoreConfig) error {
defer summary.Summary(cmdName)
ctx, cancel := context.WithCancel(c)
defer cancel()

mgr, err := newMgr(ctx, g, cfg.PD, cfg.TLS, conn.SkipTiFlash)
if err != nil {
return err
}
defer mgr.Close()

// Load backupmeta
_, _, backupMeta, err := ReadBackupMeta(c, &cfg.Config)
if err != nil {
return err
}
dbs, err := utils.LoadBackupTables(backupMeta)
if err != nil {
return err
}
se, err := restore.NewDB(g, mgr.GetTiKV())
if err != nil {
return err
}

tables := make([]*utils.Table, 0)
for _, db := range dbs {
tables = append(tables, db.Tables...)
}
updateCh := utils.StartProgress(
ctx, "Checksum", int64(len(tables)), !cfg.LogProgress)
for _, t := range tables {
log.Info("get table", zap.Stringer("name", t.Info.Name),
zap.Int("replica", t.TiFlashReplicas))
if t.TiFlashReplicas > 0 {
err := se.AlterTiflashReplica(ctx, t, t.TiFlashReplicas)
if err != nil {
return err
}
updateCh <- struct{}{}
}
}
summary.CollectInt("recover tables", len(tables))

return nil
}

0 comments on commit 63c1d0f

Please sign in to comment.