@@ -4,17 +4,23 @@ package main
44
55import (
66 "context"
7+ "crypto/tls"
78 "path"
89 "reflect"
10+ "strings"
911
1012 "github.com/gogo/protobuf/proto"
1113 "github.com/pingcap/errors"
1214 "github.com/pingcap/log"
1315 "github.com/spf13/cobra"
16+ "github.com/tikv/migration/br/pkg/checksum"
17+ "github.com/tikv/migration/br/pkg/conn"
1418 "github.com/tikv/migration/br/pkg/metautil"
19+ "github.com/tikv/migration/br/pkg/pdutil"
1520 "github.com/tikv/migration/br/pkg/task"
1621 "github.com/tikv/migration/br/pkg/utils"
1722 "github.com/tikv/migration/br/pkg/version/build"
23+ pd "github.com/tikv/pd/client"
1824)
1925
2026// NewDebugCommand return a debug subcommand.
@@ -51,10 +57,9 @@ func newCheckSumCommand() *cobra.Command {
5157 Short : "check the backup data" ,
5258 Args : cobra .NoArgs ,
5359 RunE : func (cmd * cobra.Command , _ []string ) error {
54- return errors . Errorf ( "checksum is unsupported " )
60+ return runRawChecksumCommand ( cmd , "RawChecksum " )
5561 },
5662 }
57- command .Hidden = true
5863 return command
5964}
6065
@@ -77,6 +82,7 @@ func newBackupMetaValidateCommand() *cobra.Command {
7782 },
7883 }
7984 command .Flags ().Uint64 ("offset" , 0 , "the offset of table id alloctor" )
85+ command .Hidden = true
8086 return command
8187}
8288
@@ -223,3 +229,55 @@ func setPDConfigCommand() *cobra.Command {
223229 }
224230 return pdConfigCmd
225231}
232+
233+ func runRawChecksumCommand (command * cobra.Command , cmdName string ) error {
234+ cfg := task.Config {LogProgress : HasLogFile ()}
235+ err := cfg .ParseFromFlags (command .Flags ())
236+ if err != nil {
237+ command .SilenceUsage = false
238+ return errors .Trace (err )
239+ }
240+
241+ ctx := GetDefaultContext ()
242+ pdAddress := strings .Join (cfg .PD , "," )
243+ securityOption := pd.SecurityOption {}
244+ var tlsConf * tls.Config = nil
245+ if cfg .TLS .IsEnabled () {
246+ securityOption .CAPath = cfg .TLS .CA
247+ securityOption .CertPath = cfg .TLS .Cert
248+ securityOption .KeyPath = cfg .TLS .Key
249+ tlsConf , err = cfg .TLS .ToTLSConfig ()
250+ if err != nil {
251+ return errors .Trace (err )
252+ }
253+ }
254+ pdCtrl , err := pdutil .NewPdController (ctx , pdAddress , tlsConf , securityOption )
255+ if err != nil {
256+ return errors .Trace (err )
257+ }
258+ storageAPIVersion , err := conn .GetTiKVApiVersion (ctx , pdCtrl .GetPDClient (), tlsConf )
259+ if err != nil {
260+ return errors .Trace (err )
261+ }
262+ _ , _ , backupMeta , err := task .ReadBackupMeta (ctx , metautil .MetaFile , & cfg )
263+ if err != nil {
264+ return errors .Trace (err )
265+ }
266+ fileChecksum , keyRanges := task .CalcChecksumAndRangeFromBackupMeta (ctx , backupMeta , storageAPIVersion )
267+ if ! task .CheckBackupAPIVersion (storageAPIVersion , backupMeta .ApiVersion ) {
268+ return errors .Errorf ("Unsupported api version, storage:%s, backup meta:%s." ,
269+ storageAPIVersion .String (), backupMeta .ApiVersion .String ())
270+ }
271+ checksumMethod := checksum .StorageChecksumCommand
272+ if storageAPIVersion != backupMeta .ApiVersion {
273+ checksumMethod = checksum .StorageScanCommand
274+ }
275+
276+ executor := checksum .NewExecutor (keyRanges , cfg .PD , pdCtrl .GetPDClient (), storageAPIVersion ,
277+ cfg .ChecksumConcurrency )
278+ err = checksum .Run (ctx , cmdName , executor , checksumMethod , fileChecksum )
279+ if err != nil {
280+ return errors .Trace (err )
281+ }
282+ return nil
283+ }
0 commit comments