forked from pingcap/br
-
Notifications
You must be signed in to change notification settings - Fork 0
/
restore_log.go
137 lines (115 loc) · 3.74 KB
/
restore_log.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright 2020 PingCAP, Inc. Licensed under Apache-2.0.
package task
import (
"context"
"github.com/pingcap/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/pingcap/br/pkg/glue"
"github.com/pingcap/br/pkg/restore"
"github.com/pingcap/br/pkg/storage"
)
const (
flagStartTS = "start-ts"
flagEndTS = "end-ts"
flagBatchWriteCount = "write-kvs"
flagBatchFlushCount = "flush-kvs"
// represents kv flush to storage for each table.
defaultFlushKV = 5120
// represents kv size flush to storage for each table.
defaultFlushKVSize = 5 << 20
// represents kv that write to TiKV once at at time.
defaultWriteKV = 1280
)
// LogRestoreConfig is the configuration specific for restore tasks.
type LogRestoreConfig struct {
Config
StartTS uint64
EndTS uint64
BatchFlushKVPairs int
BatchFlushKVSize int64
BatchWriteKVPairs int
}
// DefineLogRestoreFlags defines common flags for the backup command.
func DefineLogRestoreFlags(command *cobra.Command) {
command.Flags().Uint64P(flagStartTS, "", 0, "restore log start ts")
command.Flags().Uint64P(flagEndTS, "", 0, "restore log end ts")
command.Flags().Uint64P(flagBatchWriteCount, "", 0, "the kv count that write to TiKV once at a time")
command.Flags().Uint64P(flagBatchFlushCount, "", 0, "the kv count that flush from memory to TiKV")
}
// ParseFromFlags parses the restore-related flags from the flag set.
func (cfg *LogRestoreConfig) ParseFromFlags(flags *pflag.FlagSet) error {
var err error
cfg.StartTS, err = flags.GetUint64(flagStartTS)
if err != nil {
return errors.Trace(err)
}
cfg.EndTS, err = flags.GetUint64(flagEndTS)
if err != nil {
return errors.Trace(err)
}
err = cfg.Config.ParseFromFlags(flags)
if err != nil {
return errors.Trace(err)
}
return nil
}
// adjustRestoreConfig is use for BR(binary) and BR in TiDB.
// When new config was add and not included in parser.
// we should set proper value in this function.
// so that both binary and TiDB will use same default value.
func (cfg *LogRestoreConfig) adjustRestoreConfig() {
cfg.adjust()
if cfg.Config.Concurrency == 0 {
cfg.Config.Concurrency = defaultRestoreConcurrency
}
if cfg.BatchFlushKVPairs == 0 {
cfg.BatchFlushKVPairs = defaultFlushKV
}
if cfg.BatchWriteKVPairs == 0 {
cfg.BatchWriteKVPairs = defaultWriteKV
}
if cfg.BatchFlushKVSize == 0 {
cfg.BatchFlushKVSize = defaultFlushKVSize
}
// write kv count doesn't have to excceed flush kv count.
if cfg.BatchWriteKVPairs > cfg.BatchFlushKVPairs {
cfg.BatchWriteKVPairs = cfg.BatchFlushKVPairs
}
}
// RunLogRestore starts a restore task inside the current goroutine.
func RunLogRestore(c context.Context, g glue.Glue, cfg *LogRestoreConfig) error {
cfg.adjustRestoreConfig()
ctx, cancel := context.WithCancel(c)
defer cancel()
mgr, err := NewMgr(ctx, g, cfg.PD, cfg.TLS, GetKeepalive(&cfg.Config), cfg.CheckRequirements)
if err != nil {
return errors.Trace(err)
}
defer mgr.Close()
u, err := storage.ParseBackend(cfg.Storage, &cfg.BackendOptions)
if err != nil {
return errors.Trace(err)
}
keepaliveCfg := GetKeepalive(&cfg.Config)
keepaliveCfg.PermitWithoutStream = true
client, err := restore.NewRestoreClient(g, mgr.GetPDClient(), mgr.GetTiKV(), mgr.GetTLSConfig(), keepaliveCfg)
if err != nil {
return errors.Trace(err)
}
defer client.Close()
if err = client.SetStorage(ctx, u, false); err != nil {
return errors.Trace(err)
}
err = client.LoadRestoreStores(ctx)
if err != nil {
return errors.Trace(err)
}
logClient, err := restore.NewLogRestoreClient(
ctx, client, cfg.StartTS, cfg.EndTS, cfg.TableFilter, uint(cfg.Concurrency),
cfg.BatchFlushKVPairs, cfg.BatchFlushKVSize, cfg.BatchWriteKVPairs)
if err != nil {
return errors.Trace(err)
}
return logClient.RestoreLogData(ctx, mgr.GetDomain())
}