Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.
Closed
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
2 changes: 1 addition & 1 deletion cmd/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ func runBackup(flagSet *pflag.FlagSet, cmdName, db, table string) error {
// Checksum has finished
close(updateCh)

utils.TimeCollector.SummaryLog(cmdName)
return client.SaveBackupMeta(ctx)
}

Expand Down Expand Up @@ -232,7 +233,6 @@ func newTableBackupCommand() *cobra.Command {
if len(table) == 0 {
return errors.Errorf("empty table name is not allowed")
}

return runBackup(command.Flags(), "Table backup", db, table)
},
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func newFullRestoreCommand() *cobra.Command {
return err
}
close(updateCh)

utils.TimeCollector.SummaryLog("restore full")
return nil
},
}
Expand Down Expand Up @@ -255,6 +255,7 @@ func newDbRestoreCommand() *cobra.Command {
return err
}
close(updateCh)
utils.TimeCollector.SummaryLog("restore database")
return nil
},
}
Expand Down Expand Up @@ -370,6 +371,7 @@ func newTableRestoreCommand() *cobra.Command {
}
close(updateCh)

utils.TimeCollector.SummaryLog("restore table")
return nil
},
}
Expand Down
7 changes: 3 additions & 4 deletions pkg/backup/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ func (bc *Client) BackupRanges(
start := time.Now()
defer func() {
elapsed := time.Since(start)
log.Info("Backup Ranges", zap.Duration("take", elapsed))
utils.TimeCollector.CollectDuration("backup ranges", elapsed)
}()

errCh := make(chan error)
Expand Down Expand Up @@ -399,8 +399,7 @@ func (bc *Client) backupRange(
// Check if there are duplicated files.
results.checkDupFiles()

log.Info("backup range finished",
zap.Duration("take", time.Since(start)))
utils.TimeCollector.CollectDuration("backup range finished", time.Since(start))
return nil
}

Expand Down Expand Up @@ -689,7 +688,7 @@ func (bc *Client) FastChecksum() (bool, error) {
start := time.Now()
defer func() {
elapsed := time.Since(start)
log.Info("Backup Checksum", zap.Duration("take", elapsed))
utils.TimeCollector.CollectDuration("backup checksum", elapsed)
}()

dbs, err := utils.LoadBackupTables(&bc.backupMeta)
Expand Down
9 changes: 5 additions & 4 deletions pkg/restore/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package restore

import (
"context"
"fmt"
"math"
"sort"
"sync"
Expand Down Expand Up @@ -249,7 +250,7 @@ func (rc *Client) RestoreTable(
start := time.Now()
defer func() {
elapsed := time.Since(start)
log.Info("Restore Table", zap.Stringer("table", table.Schema.Name), zap.Duration("take", elapsed))
utils.TimeCollector.CollectDuration(fmt.Sprintf("restore table: %s", table.Schema.Name), elapsed)
}()
log.Debug("start to restore table",
zap.Stringer("table", table.Schema.Name),
Expand Down Expand Up @@ -310,7 +311,7 @@ func (rc *Client) RestoreDatabase(
start := time.Now()
defer func() {
elapsed := time.Since(start)
log.Info("Restore Database", zap.Stringer("db", db.Schema.Name), zap.Duration("take", elapsed))
utils.TimeCollector.CollectDuration(fmt.Sprintf("restore database: %s", db.Schema.Name), elapsed)
}()
errCh := make(chan error, len(db.Tables))
wg := new(sync.WaitGroup)
Expand Down Expand Up @@ -346,7 +347,7 @@ func (rc *Client) RestoreAll(
start := time.Now()
defer func() {
elapsed := time.Since(start)
log.Info("Restore All", zap.Duration("take", elapsed))
utils.TimeCollector.CollectDuration("restore all", elapsed)
}()
errCh := make(chan error, len(rc.databases))
wg := new(sync.WaitGroup)
Expand Down Expand Up @@ -443,7 +444,7 @@ func (rc *Client) ValidateChecksum(
start := time.Now()
defer func() {
elapsed := time.Since(start)
log.Info("Restore Checksum", zap.Duration("take", elapsed))
utils.TimeCollector.CollectDuration("restore checksum", elapsed)
}()

log.Info("Start to validate checksum")
Expand Down
4 changes: 3 additions & 1 deletion pkg/restore/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"github.com/pingcap/tidb/util/codec"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"

"github.com/pingcap/br/pkg/utils"
)

var recordPrefixSep = []byte("_r")
Expand Down Expand Up @@ -333,7 +335,7 @@ func SplitRanges(
start := time.Now()
defer func() {
elapsed := time.Since(start)
log.Info("SplitRegion", zap.Duration("costs", elapsed))
utils.TimeCollector.CollectDuration("split region", elapsed)
}()
splitter := restore_util.NewRegionSplitter(restore_util.NewClient(client.GetPDClient()))
return splitter.Split(ctx, ranges, rewriteRules, func(*restore_util.Range) {
Expand Down
37 changes: 37 additions & 0 deletions pkg/utils/unit.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
package utils

import (
"fmt"
"sync"
"time"

"github.com/pingcap/log"
"go.uber.org/zap"
)

// unit of storage
const (
B = 1 << (iota * 10)
Expand All @@ -8,3 +17,31 @@ const (
GB
TB
)

type timeCollector struct {
mu sync.Mutex
times map[string]time.Duration
}

// TimeCollector collects time into summary log
var TimeCollector *timeCollector

func init() {
TimeCollector = &timeCollector{
times: make(map[string]time.Duration),
}
}

func (tc *timeCollector) CollectDuration(name string, t time.Duration) {
tc.mu.Lock()
defer tc.mu.Unlock()
tc.times[name] = t
}

func (tc *timeCollector) SummaryLog(name string) {
z := make([]zap.Field, 0, len(tc.times))
for k, v := range tc.times {
z = append(z, zap.Duration(k, v))
}
log.Info(fmt.Sprintf("%s time summary", name), z...)
}