@@ -83,6 +83,7 @@ import (
8383
8484const MetaKVBatchSize = 64 * 1024 * 1024
8585const maxSplitKeysOnce = 10240
86+ const maxReadMetaKVFilesConcurrency uint = 128
8687
8788// rawKVBatchCount specifies the count of entries that the rawkv client puts into TiKV.
8889const rawKVBatchCount = 64
@@ -1121,15 +1122,15 @@ func SeparateAndSortFilesByCF(files []*backuppb.DataFileInfo) ([]*backuppb.DataF
11211122 // The error of transactions of meta could happen if restore write CF events successfully,
11221123 // but failed to restore default CF events.
11231124 for _ , f := range files {
1124- if f .Cf == consts .WriteCF {
1125- filesInWriteCF = append (filesInWriteCF , f )
1126- continue
1127- }
1128- if f .Type == backuppb .FileType_Delete {
1129- log .Warn ("internal error: detected delete file of meta key, skip it" , zap .Any ("file" , f ))
1125+ if ! shouldReadMetaKVFile (f ) {
1126+ if f .Type == backuppb .FileType_Delete {
1127+ log .Warn ("internal error: detected delete file of meta key, skip it" , zap .Any ("file" , f ))
1128+ }
11301129 continue
11311130 }
1132- if f .Cf == consts .DefaultCF {
1131+ if f .Cf == consts .WriteCF {
1132+ filesInWriteCF = append (filesInWriteCF , f )
1133+ } else {
11331134 filesInDefaultCF = append (filesInDefaultCF , f )
11341135 }
11351136 }
@@ -1140,6 +1141,26 @@ func SeparateAndSortFilesByCF(files []*backuppb.DataFileInfo) ([]*backuppb.DataF
11401141 return filesInDefaultCF , filesInWriteCF
11411142}
11421143
1144+ func shouldReadMetaKVFile (file * backuppb.DataFileInfo ) bool {
1145+ if file .Cf == consts .WriteCF {
1146+ return true
1147+ }
1148+ if file .Type == backuppb .FileType_Delete {
1149+ return false
1150+ }
1151+ return file .Cf == consts .DefaultCF
1152+ }
1153+
1154+ func countReadableMetaKVFiles (files []* backuppb.DataFileInfo ) int {
1155+ count := 0
1156+ for _ , file := range files {
1157+ if shouldReadMetaKVFile (file ) {
1158+ count ++
1159+ }
1160+ }
1161+ return count
1162+ }
1163+
11431164// LoadAndProcessMetaKVFilesInBatch restores meta kv files to TiKV in strict TS order. It does so in batch and after
11441165// success it triggers an update so every TiDB node can pick up the restored content.
11451166func LoadAndProcessMetaKVFilesInBatch (
@@ -1290,14 +1311,28 @@ func (rc *LogClient) filterAndSortKvEntriesFromFiles(
12901311 }
12911312
12921313 // read all entries from files.
1293- for _ , f := range files {
1294- es , filteredOutEs , err := rc .ReadFilteredEntriesFromFiles (ctx , f , filterTS )
1295- if err != nil {
1314+ if len (files ) > 0 {
1315+ eg , egCtx := errgroup .WithContext (ctx )
1316+ workerPool := tidbutil .NewWorkerPool (min (uint (len (files )), maxReadMetaKVFilesConcurrency ), "read meta kv files" )
1317+ var entriesLock sync.Mutex
1318+ for _ , f := range files {
1319+ file := f
1320+ workerPool .ApplyOnErrorGroup (eg , func () error {
1321+ es , filteredOutEs , err := rc .ReadFilteredEntriesFromFiles (egCtx , file , filterTS )
1322+ if err != nil {
1323+ return errors .Trace (err )
1324+ }
1325+
1326+ entriesLock .Lock ()
1327+ curKvEntries = append (curKvEntries , es ... )
1328+ filteredOutKvEntries = append (filteredOutKvEntries , filteredOutEs ... )
1329+ entriesLock .Unlock ()
1330+ return nil
1331+ })
1332+ }
1333+ if err := eg .Wait (); err != nil {
12961334 return nil , nil , errors .Trace (err )
12971335 }
1298-
1299- curKvEntries = append (curKvEntries , es ... )
1300- filteredOutKvEntries = append (filteredOutKvEntries , filteredOutEs ... )
13011336 }
13021337
13031338 // sort these entries.
0 commit comments