-
Notifications
You must be signed in to change notification settings - Fork 6k
br: fix debug decode backupmeta #56627
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3f10e70
fix debug decode backupmeta
Leavrth 62ee5cf
add unit test
Leavrth 63c9152
make bazel
Leavrth 46d5dc4
pase the []byte into string
Leavrth 7ca55c8
create a json directory
Leavrth 59cf8f2
use jsonvalue for stats field
Leavrth b3d97a6
fix integration test
Leavrth 06999de
error if encode backupmeta v2
Leavrth 2971ded
fix br integration test
Leavrth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
// Copyright 2024 PingCAP, Inc. | ||
// | ||
// 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 metautil | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"crypto/sha256" | ||
"fmt" | ||
|
||
"github.com/gogo/protobuf/proto" | ||
"github.com/pingcap/errors" | ||
backuppb "github.com/pingcap/kvproto/pkg/brpb" | ||
berrors "github.com/pingcap/tidb/br/pkg/errors" | ||
"github.com/pingcap/tidb/br/pkg/storage" | ||
"github.com/pingcap/tidb/br/pkg/utils" | ||
tidbutil "github.com/pingcap/tidb/pkg/util" | ||
"golang.org/x/sync/errgroup" | ||
) | ||
|
||
const ( | ||
// JSONFileFormat represents json file name format | ||
JSONFileFormat = "jsons/%s.json" | ||
) | ||
|
||
// DecodeStatsFile decodes the stats file to json format, it is called by br debug | ||
func DecodeStatsFile( | ||
ctx context.Context, | ||
s storage.ExternalStorage, | ||
cipher *backuppb.CipherInfo, | ||
schemas []*backuppb.Schema, | ||
) error { | ||
for _, schema := range schemas { | ||
for _, statsIndex := range schema.StatsIndex { | ||
if len(statsIndex.Name) == 0 { | ||
continue | ||
} | ||
content, err := s.ReadFile(ctx, statsIndex.Name) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
decryptContent, err := utils.Decrypt(content, cipher, statsIndex.CipherIv) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
checksum := sha256.Sum256(decryptContent) | ||
if !bytes.Equal(statsIndex.Sha256, checksum[:]) { | ||
return berrors.ErrInvalidMetaFile.GenWithStackByArgs(fmt.Sprintf( | ||
"checksum mismatch expect %x, got %x", statsIndex.Sha256, checksum[:])) | ||
} | ||
statsFileBlocks := &backuppb.StatsFile{} | ||
if err := proto.Unmarshal(decryptContent, statsFileBlocks); err != nil { | ||
return errors.Trace(err) | ||
} | ||
jsonContent, err := utils.MarshalStatsFile(statsFileBlocks) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
if err := s.WriteFile(ctx, fmt.Sprintf(JSONFileFormat, statsIndex.Name), jsonContent); err != nil { | ||
return errors.Trace(err) | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// DecodeMetaFile decodes the meta file to json format, it is called by br debug | ||
func DecodeMetaFile( | ||
ctx context.Context, | ||
s storage.ExternalStorage, | ||
cipher *backuppb.CipherInfo, | ||
metaIndex *backuppb.MetaFile, | ||
) error { | ||
if metaIndex == nil { | ||
return nil | ||
} | ||
eg, ectx := errgroup.WithContext(ctx) | ||
workers := tidbutil.NewWorkerPool(8, "download files workers") | ||
for _, node := range metaIndex.MetaFiles { | ||
workers.ApplyOnErrorGroup(eg, func() error { | ||
content, err := s.ReadFile(ectx, node.Name) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
|
||
decryptContent, err := utils.Decrypt(content, cipher, node.CipherIv) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
|
||
checksum := sha256.Sum256(decryptContent) | ||
if !bytes.Equal(node.Sha256, checksum[:]) { | ||
return berrors.ErrInvalidMetaFile.GenWithStackByArgs(fmt.Sprintf( | ||
"checksum mismatch expect %x, got %x", node.Sha256, checksum[:])) | ||
} | ||
|
||
child := &backuppb.MetaFile{} | ||
if err = proto.Unmarshal(decryptContent, child); err != nil { | ||
return errors.Trace(err) | ||
} | ||
|
||
// the max depth of the root metafile is only 1. | ||
// ASSERT: len(child.MetaFiles) == 0 | ||
if len(child.MetaFiles) > 0 { | ||
return errors.Errorf("the metafile has unexpected level: %v", child) | ||
} | ||
|
||
jsonContent, err := utils.MarshalMetaFile(child) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
|
||
if err := s.WriteFile(ctx, fmt.Sprintf(JSONFileFormat, node.Name), jsonContent); err != nil { | ||
return errors.Trace(err) | ||
} | ||
|
||
err = DecodeStatsFile(ctx, s, cipher, child.Schemas) | ||
return errors.Trace(err) | ||
}) | ||
} | ||
return eg.Wait() | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it possible to make the pool size configurable or at least move 8 to constant definition section in l33.