|
| 1 | +// Copyright 2024 PingCAP, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package utils_test |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "crypto/sha256" |
| 20 | + "fmt" |
| 21 | + "math/rand" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/gogo/protobuf/proto" |
| 25 | + backuppb "github.com/pingcap/kvproto/pkg/brpb" |
| 26 | + "github.com/pingcap/tidb/br/pkg/metautil" |
| 27 | + "github.com/pingcap/tidb/br/pkg/storage" |
| 28 | + "github.com/pingcap/tidb/br/pkg/utils" |
| 29 | + "github.com/stretchr/testify/require" |
| 30 | +) |
| 31 | + |
| 32 | +func flushMetaFile( |
| 33 | + ctx context.Context, |
| 34 | + t *testing.T, |
| 35 | + fname string, |
| 36 | + metaFile *backuppb.MetaFile, |
| 37 | + storage storage.ExternalStorage, |
| 38 | + cipher *backuppb.CipherInfo, |
| 39 | +) *backuppb.File { |
| 40 | + content, err := metaFile.Marshal() |
| 41 | + require.NoError(t, err) |
| 42 | + |
| 43 | + encyptedContent, iv, err := metautil.Encrypt(content, cipher) |
| 44 | + require.NoError(t, err) |
| 45 | + |
| 46 | + err = storage.WriteFile(ctx, fname, encyptedContent) |
| 47 | + require.NoError(t, err) |
| 48 | + |
| 49 | + checksum := sha256.Sum256(content) |
| 50 | + file := &backuppb.File{ |
| 51 | + Name: fname, |
| 52 | + Sha256: checksum[:], |
| 53 | + Size_: uint64(len(content)), |
| 54 | + CipherIv: iv, |
| 55 | + } |
| 56 | + |
| 57 | + return file |
| 58 | +} |
| 59 | + |
| 60 | +func flushStatsFile( |
| 61 | + ctx context.Context, |
| 62 | + t *testing.T, |
| 63 | + fname string, |
| 64 | + statsFile *backuppb.StatsFile, |
| 65 | + storage storage.ExternalStorage, |
| 66 | + cipher *backuppb.CipherInfo, |
| 67 | +) *backuppb.StatsFileIndex { |
| 68 | + content, err := proto.Marshal(statsFile) |
| 69 | + require.NoError(t, err) |
| 70 | + |
| 71 | + checksum := sha256.Sum256(content) |
| 72 | + sizeOri := uint64(len(content)) |
| 73 | + encryptedContent, iv, err := metautil.Encrypt(content, cipher) |
| 74 | + require.NoError(t, err) |
| 75 | + |
| 76 | + err = storage.WriteFile(ctx, fname, encryptedContent) |
| 77 | + require.NoError(t, err) |
| 78 | + |
| 79 | + return &backuppb.StatsFileIndex{ |
| 80 | + Name: fname, |
| 81 | + Sha256: checksum[:], |
| 82 | + SizeEnc: uint64(len(encryptedContent)), |
| 83 | + SizeOri: sizeOri, |
| 84 | + CipherIv: iv, |
| 85 | + InlineData: []byte(fmt.Sprintf("%d", rand.Int())), |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func TestDecodeMetaFile(t *testing.T) { |
| 90 | + ctx := context.Background() |
| 91 | + base := t.TempDir() |
| 92 | + s, err := storage.NewLocalStorage(base) |
| 93 | + require.NoError(t, err) |
| 94 | + cipher := &backuppb.CipherInfo{CipherType: 1} |
| 95 | + file1 := flushMetaFile(ctx, t, "data", &backuppb.MetaFile{ |
| 96 | + DataFiles: []*backuppb.File{ |
| 97 | + { |
| 98 | + Name: "1.sst", |
| 99 | + Sha256: []byte("1.sst"), |
| 100 | + StartKey: []byte("start"), |
| 101 | + EndKey: []byte("end"), |
| 102 | + EndVersion: 1, |
| 103 | + Crc64Xor: 1, |
| 104 | + TotalKvs: 2, |
| 105 | + TotalBytes: 3, |
| 106 | + Cf: "write", |
| 107 | + CipherIv: []byte("1.sst"), |
| 108 | + }, |
| 109 | + }, |
| 110 | + }, s, cipher) |
| 111 | + stats := flushStatsFile(ctx, t, "stats", &backuppb.StatsFile{Blocks: []*backuppb.StatsBlock{ |
| 112 | + { |
| 113 | + PhysicalId: 1, |
| 114 | + JsonTable: []byte("1"), |
| 115 | + }, |
| 116 | + { |
| 117 | + PhysicalId: 2, |
| 118 | + JsonTable: []byte("2"), |
| 119 | + }, |
| 120 | + }}, s, cipher) |
| 121 | + metaFile2 := &backuppb.MetaFile{ |
| 122 | + Schemas: []*backuppb.Schema{ |
| 123 | + { |
| 124 | + Db: []byte(`{"db_name":{"L":"test","O":"test"},"id":1,"state":5}`), |
| 125 | + Table: []byte(`{"id":2,"state":5}`), |
| 126 | + Crc64Xor: 1, |
| 127 | + TotalKvs: 2, |
| 128 | + TotalBytes: 3, |
| 129 | + TiflashReplicas: 4, |
| 130 | + Stats: []byte(`{"a":1}`), |
| 131 | + StatsIndex: []*backuppb.StatsFileIndex{stats}, |
| 132 | + }, |
| 133 | + }, |
| 134 | + } |
| 135 | + file2 := flushMetaFile(ctx, t, "schema", metaFile2, s, cipher) |
| 136 | + |
| 137 | + { |
| 138 | + err = utils.DecodeMetaFile(ctx, s, cipher, &backuppb.MetaFile{MetaFiles: []*backuppb.File{file1}}) |
| 139 | + require.NoError(t, err) |
| 140 | + content, err := s.ReadFile(ctx, "jsons/data.json") |
| 141 | + require.NoError(t, err) |
| 142 | + metaFile, err := utils.UnmarshalMetaFile(content) |
| 143 | + require.NoError(t, err) |
| 144 | + require.Equal(t, 1, len(metaFile.DataFiles)) |
| 145 | + require.Equal(t, "1.sst", metaFile.DataFiles[0].Name) |
| 146 | + require.Equal(t, []byte("1.sst"), metaFile.DataFiles[0].Sha256) |
| 147 | + require.Equal(t, []byte("start"), metaFile.DataFiles[0].StartKey) |
| 148 | + require.Equal(t, []byte("end"), metaFile.DataFiles[0].EndKey) |
| 149 | + require.Equal(t, uint64(1), metaFile.DataFiles[0].EndVersion) |
| 150 | + require.Equal(t, uint64(1), metaFile.DataFiles[0].Crc64Xor) |
| 151 | + require.Equal(t, uint64(2), metaFile.DataFiles[0].TotalKvs) |
| 152 | + require.Equal(t, uint64(3), metaFile.DataFiles[0].TotalBytes) |
| 153 | + require.Equal(t, "write", metaFile.DataFiles[0].Cf) |
| 154 | + require.Equal(t, []byte("1.sst"), metaFile.DataFiles[0].CipherIv) |
| 155 | + } |
| 156 | + |
| 157 | + { |
| 158 | + err = utils.DecodeMetaFile(ctx, s, cipher, &backuppb.MetaFile{MetaFiles: []*backuppb.File{file2}}) |
| 159 | + require.NoError(t, err) |
| 160 | + { |
| 161 | + content, err := s.ReadFile(ctx, "jsons/schema.json") |
| 162 | + require.NoError(t, err) |
| 163 | + metaFile, err := utils.UnmarshalMetaFile(content) |
| 164 | + require.NoError(t, err) |
| 165 | + require.Equal(t, 1, len(metaFile.Schemas)) |
| 166 | + require.Equal(t, metaFile2.Schemas[0].Db, metaFile.Schemas[0].Db) |
| 167 | + require.Equal(t, metaFile2.Schemas[0].Table, metaFile.Schemas[0].Table) |
| 168 | + require.Equal(t, uint64(1), metaFile.Schemas[0].Crc64Xor) |
| 169 | + require.Equal(t, uint64(2), metaFile.Schemas[0].TotalKvs) |
| 170 | + require.Equal(t, uint64(3), metaFile.Schemas[0].TotalBytes) |
| 171 | + require.Equal(t, uint32(4), metaFile.Schemas[0].TiflashReplicas) |
| 172 | + require.Equal(t, metaFile2.Schemas[0].Stats, metaFile.Schemas[0].Stats) |
| 173 | + statsIndex := metaFile.Schemas[0].StatsIndex |
| 174 | + require.Equal(t, 1, len(statsIndex)) |
| 175 | + require.Equal(t, stats.Name, statsIndex[0].Name) |
| 176 | + require.Equal(t, stats.Sha256, statsIndex[0].Sha256) |
| 177 | + require.Equal(t, stats.SizeEnc, statsIndex[0].SizeEnc) |
| 178 | + require.Equal(t, stats.SizeOri, statsIndex[0].SizeOri) |
| 179 | + require.Equal(t, stats.CipherIv, statsIndex[0].CipherIv) |
| 180 | + require.Equal(t, stats.InlineData, statsIndex[0].InlineData) |
| 181 | + } |
| 182 | + } |
| 183 | +} |
0 commit comments