-
Notifications
You must be signed in to change notification settings - Fork 24
/
rawkv_config.go
181 lines (162 loc) · 5.31 KB
/
rawkv_config.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright 2022 TiKV Project Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package task
import (
"bytes"
"strings"
"time"
"github.com/pingcap/errors"
backuppb "github.com/pingcap/kvproto/pkg/brpb"
"github.com/pingcap/kvproto/pkg/kvrpcpb"
"github.com/spf13/pflag"
berrors "github.com/tikv/migration/br/pkg/errors"
"github.com/tikv/migration/br/pkg/utils"
)
// RawKvConfig is the common config for rawkv backup and restore.
type RawKvConfig struct {
Config
StartKey []byte `json:"start-key" toml:"start-key"`
EndKey []byte `json:"end-key" toml:"end-key"`
DstAPIVersion string `json:"dst-api-version" toml:"dst-api-version"`
CompressionConfig
RemoveSchedulers bool `json:"remove-schedulers" toml:"remove-schedulers"`
SafeInterval time.Duration `json:"safe-interval" toml:"safe-interval"`
GCTTL time.Duration `json:"gc-ttl" toml:"gc-ttl"`
}
// ParseBackupConfigFromFlags parses the backup-related flags from the flag set.
func (cfg *RawKvConfig) ParseBackupConfigFromFlags(flags *pflag.FlagSet) error {
err := cfg.ParseFromFlags(flags)
if err != nil {
return errors.Trace(err)
}
if err = cfg.parseDstAPIVersion(flags); err != nil {
return errors.Trace(err)
}
safeInterval, err := flags.GetDuration(flagSafeInterval)
if err != nil {
return errors.Trace(err)
}
cfg.SafeInterval = safeInterval
gcTTL, err := flags.GetDuration(flagGCTTL)
if err != nil {
return errors.Trace(err)
}
cfg.GCTTL = gcTTL
compressionCfg, err := cfg.parseCompressionFlags(flags)
if err != nil {
return errors.Trace(err)
}
cfg.CompressionConfig = *compressionCfg
cfg.RemoveSchedulers, err = flags.GetBool(flagRemoveSchedulers)
if err != nil {
return errors.Trace(err)
}
level, err := flags.GetInt32(flagCompressionLevel)
if err != nil {
return errors.Trace(err)
}
cfg.CompressionLevel = level
return nil
}
// ParseFromFlags parses the raw kv backup&restore common flags from the flag set.
func (cfg *RawKvConfig) ParseFromFlags(flags *pflag.FlagSet) error {
// parse key format.
format, err := flags.GetString(flagKeyFormat)
if err != nil {
return errors.Trace(err)
}
// parse start key.
start, err := flags.GetString(flagStartKey)
if err != nil {
return errors.Trace(err)
}
cfg.StartKey, err = utils.ParseKey(format, start)
if err != nil {
return errors.Trace(err)
}
// parse end key.
end, err := flags.GetString(flagEndKey)
if err != nil {
return errors.Trace(err)
}
cfg.EndKey, err = utils.ParseKey(format, end)
if err != nil {
return errors.Trace(err)
}
// verify whether start key < end key.
if len(cfg.StartKey) > 0 && len(cfg.EndKey) > 0 && bytes.Compare(cfg.StartKey, cfg.EndKey) >= 0 {
return errors.Annotate(berrors.ErrBackupInvalidRange, "endKey must be greater than startKey")
}
// parse other configs.
if err = cfg.Config.ParseFromFlags(flags); err != nil {
return errors.Trace(err)
}
return nil
}
func (cfg *RawKvConfig) parseDstAPIVersion(flags *pflag.FlagSet) error {
originalValue, err := flags.GetString(flagDstAPIVersion)
if err != nil {
return errors.Trace(err)
}
if len(originalValue) == 0 {
return nil // if dst api version is empty, use cur api version as dst api version.
}
cfg.DstAPIVersion = strings.ToUpper(originalValue)
if _, ok := kvrpcpb.APIVersion_value[cfg.DstAPIVersion]; !ok {
supportedValues := kvrpcpb.APIVersion_V1.String() +
", " + kvrpcpb.APIVersion_V1TTL.String() +
", " + kvrpcpb.APIVersion_V2.String()
return errors.Errorf("unsupported dst-api-version: %v. supported values are: %v", originalValue, supportedValues)
}
return nil
}
// parseCompressionFlags parses the backup-related flags from the flag set.
func (cfg *RawKvConfig) parseCompressionFlags(flags *pflag.FlagSet) (*CompressionConfig, error) {
compressionStr, err := flags.GetString(flagCompressionType)
if err != nil {
return nil, errors.Trace(err)
}
compressionType, err := cfg.parseCompressionType(compressionStr)
if err != nil {
return nil, errors.Trace(err)
}
level, err := flags.GetInt32(flagCompressionLevel)
if err != nil {
return nil, errors.Trace(err)
}
return &CompressionConfig{
CompressionLevel: level,
CompressionType: compressionType,
}, nil
}
func (cfg *RawKvConfig) parseCompressionType(s string) (backuppb.CompressionType, error) {
var ct backuppb.CompressionType
switch s {
case "lz4":
ct = backuppb.CompressionType_LZ4
case "snappy":
ct = backuppb.CompressionType_SNAPPY
case "zstd":
ct = backuppb.CompressionType_ZSTD
default:
return backuppb.CompressionType_UNKNOWN, errors.Annotatef(berrors.ErrInvalidArgument, "invalid compression type '%s'", s)
}
return ct, nil
}
func (cfg *RawKvConfig) adjustBackupRange(curAPIVersion kvrpcpb.APIVersion) {
if curAPIVersion == kvrpcpb.APIVersion_V2 {
keyRange := utils.FormatAPIV2KeyRange(cfg.StartKey, cfg.EndKey)
cfg.StartKey, cfg.EndKey = keyRange.Start, keyRange.End
}
}