|
| 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 logclient |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "encoding/json" |
| 20 | + |
| 21 | + "github.com/pingcap/errors" |
| 22 | + backuppb "github.com/pingcap/kvproto/pkg/brpb" |
| 23 | + "github.com/pingcap/log" |
| 24 | + "github.com/pingcap/tidb/br/pkg/stream" |
| 25 | + "github.com/pingcap/tidb/br/pkg/utils" |
| 26 | + "github.com/pingcap/tidb/pkg/meta" |
| 27 | + "github.com/pingcap/tidb/pkg/meta/model" |
| 28 | + "go.uber.org/zap" |
| 29 | +) |
| 30 | + |
| 31 | +// BatchMetaKVProcessor defines how to process a batch of files |
| 32 | +type BatchMetaKVProcessor interface { |
| 33 | + // ProcessBatch processes a batch of files and with a filterTS and return what's not processed for next iteration |
| 34 | + ProcessBatch( |
| 35 | + ctx context.Context, |
| 36 | + files []*backuppb.DataFileInfo, |
| 37 | + entries []*KvEntryWithTS, |
| 38 | + filterTS uint64, |
| 39 | + cf string, |
| 40 | + ) ([]*KvEntryWithTS, error) |
| 41 | +} |
| 42 | + |
| 43 | +// RestoreMetaKVProcessor implements BatchMetaKVProcessor for restoring files in batches |
| 44 | +type RestoreMetaKVProcessor struct { |
| 45 | + client *LogClient |
| 46 | + schemasReplace *stream.SchemasReplace |
| 47 | + updateStats func(kvCount uint64, size uint64) |
| 48 | + progressInc func() |
| 49 | +} |
| 50 | + |
| 51 | +func NewRestoreMetaKVProcessor(client *LogClient, schemasReplace *stream.SchemasReplace, |
| 52 | + updateStats func(kvCount uint64, size uint64), |
| 53 | + progressInc func()) *RestoreMetaKVProcessor { |
| 54 | + return &RestoreMetaKVProcessor{ |
| 55 | + client: client, |
| 56 | + schemasReplace: schemasReplace, |
| 57 | + updateStats: updateStats, |
| 58 | + progressInc: progressInc, |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +// RestoreAndRewriteMetaKVFiles tries to restore files about meta kv-event from stream-backup. |
| 63 | +func (rp *RestoreMetaKVProcessor) RestoreAndRewriteMetaKVFiles( |
| 64 | + ctx context.Context, |
| 65 | + files []*backuppb.DataFileInfo, |
| 66 | +) error { |
| 67 | + // starts gc row collector |
| 68 | + rp.client.RunGCRowsLoader(ctx) |
| 69 | + |
| 70 | + // separate the files by CF and sort each group by TS |
| 71 | + filesInDefaultCF, filesInWriteCF := SeparateAndSortFilesByCF(files) |
| 72 | + |
| 73 | + log.Info("start to restore meta files", |
| 74 | + zap.Int("total files", len(files)), |
| 75 | + zap.Int("default files", len(filesInDefaultCF)), |
| 76 | + zap.Int("write files", len(filesInWriteCF))) |
| 77 | + |
| 78 | + if err := LoadAndProcessMetaKVFilesInBatch( |
| 79 | + ctx, |
| 80 | + filesInDefaultCF, |
| 81 | + filesInWriteCF, |
| 82 | + rp, |
| 83 | + ); err != nil { |
| 84 | + return errors.Trace(err) |
| 85 | + } |
| 86 | + |
| 87 | + // UpdateTable global schema version to trigger a full reload so every TiDB node in the cluster will get synced with |
| 88 | + // the latest schema update. |
| 89 | + if err := rp.client.UpdateSchemaVersionFullReload(ctx); err != nil { |
| 90 | + return errors.Trace(err) |
| 91 | + } |
| 92 | + return nil |
| 93 | +} |
| 94 | + |
| 95 | +func (rp *RestoreMetaKVProcessor) ProcessBatch( |
| 96 | + ctx context.Context, |
| 97 | + files []*backuppb.DataFileInfo, |
| 98 | + entries []*KvEntryWithTS, |
| 99 | + filterTS uint64, |
| 100 | + cf string, |
| 101 | +) ([]*KvEntryWithTS, error) { |
| 102 | + return rp.client.RestoreBatchMetaKVFiles( |
| 103 | + ctx, files, rp.schemasReplace, entries, |
| 104 | + filterTS, rp.updateStats, rp.progressInc, cf, |
| 105 | + ) |
| 106 | +} |
| 107 | + |
| 108 | +// MetaKVInfoProcessor implements BatchMetaKVProcessor to iterate meta kv and collect information. |
| 109 | +// |
| 110 | +// 1. It collects table renaming information. The table rename operation will not change the table id, and the process |
| 111 | +// will drop the original table and create a new one with the same table id, so in DDL history there will be two events |
| 112 | +// that corresponds to the same table id. |
| 113 | +// |
| 114 | +// 2. It builds the id mapping from upstream to downstream. This logic was nested into table rewrite previously and now |
| 115 | +// separated out to its own component. |
| 116 | +type MetaKVInfoProcessor struct { |
| 117 | + client *LogClient |
| 118 | + tableHistoryManager *stream.LogBackupTableHistoryManager |
| 119 | + tableMappingManager *stream.TableMappingManager |
| 120 | +} |
| 121 | + |
| 122 | +func NewMetaKVInfoProcessor(client *LogClient) *MetaKVInfoProcessor { |
| 123 | + return &MetaKVInfoProcessor{ |
| 124 | + client: client, |
| 125 | + tableHistoryManager: stream.NewTableHistoryManager(), |
| 126 | + tableMappingManager: stream.NewTableMappingManager(), |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +func (mp *MetaKVInfoProcessor) ReadMetaKVFilesAndBuildInfo( |
| 131 | + ctx context.Context, |
| 132 | + files []*backuppb.DataFileInfo, |
| 133 | +) error { |
| 134 | + // separate the files by CF and sort each group by TS |
| 135 | + filesInDefaultCF, filesInWriteCF := SeparateAndSortFilesByCF(files) |
| 136 | + |
| 137 | + if err := LoadAndProcessMetaKVFilesInBatch( |
| 138 | + ctx, |
| 139 | + filesInDefaultCF, |
| 140 | + filesInWriteCF, |
| 141 | + mp, |
| 142 | + ); err != nil { |
| 143 | + return errors.Trace(err) |
| 144 | + } |
| 145 | + return nil |
| 146 | +} |
| 147 | + |
| 148 | +func (mp *MetaKVInfoProcessor) ProcessBatch( |
| 149 | + ctx context.Context, |
| 150 | + files []*backuppb.DataFileInfo, |
| 151 | + entries []*KvEntryWithTS, |
| 152 | + filterTS uint64, |
| 153 | + cf string, |
| 154 | +) ([]*KvEntryWithTS, error) { |
| 155 | + curSortedEntries, filteredEntries, err := mp.client.filterAndSortKvEntriesFromFiles(ctx, files, entries, filterTS) |
| 156 | + if err != nil { |
| 157 | + return nil, errors.Trace(err) |
| 158 | + } |
| 159 | + |
| 160 | + // process entries to collect table IDs |
| 161 | + for _, entry := range curSortedEntries { |
| 162 | + // get value from default cf and get the short value if possible from write cf |
| 163 | + value, err := stream.ExtractValue(&entry.E, cf) |
| 164 | + |
| 165 | + // write cf doesn't have short value in it |
| 166 | + if value == nil { |
| 167 | + continue |
| 168 | + } |
| 169 | + if err != nil { |
| 170 | + return nil, errors.Trace(err) |
| 171 | + } |
| 172 | + |
| 173 | + if utils.IsMetaDBKey(entry.E.Key) { |
| 174 | + rawKey, err := stream.ParseTxnMetaKeyFrom(entry.E.Key) |
| 175 | + if err != nil { |
| 176 | + return nil, errors.Trace(err) |
| 177 | + } |
| 178 | + |
| 179 | + if meta.IsDBkey(rawKey.Field) { |
| 180 | + var dbInfo model.DBInfo |
| 181 | + if err := json.Unmarshal(value, &dbInfo); err != nil { |
| 182 | + return nil, errors.Trace(err) |
| 183 | + } |
| 184 | + |
| 185 | + // collect db id -> name mapping during log backup, it will contain information about newly created db |
| 186 | + mp.tableHistoryManager.RecordDBIdToName(dbInfo.ID, dbInfo.Name.O) |
| 187 | + |
| 188 | + // update the id map |
| 189 | + if err = mp.tableMappingManager.ProcessDBValueAndUpdateIdMapping(dbInfo); err != nil { |
| 190 | + return nil, errors.Trace(err) |
| 191 | + } |
| 192 | + } else if !meta.IsDBkey(rawKey.Key) { |
| 193 | + // also see RewriteMetaKvEntry |
| 194 | + continue |
| 195 | + } |
| 196 | + |
| 197 | + // collect table history indexed by table id, same id may have different table names in history |
| 198 | + if meta.IsTableKey(rawKey.Field) { |
| 199 | + var tableInfo model.TableInfo |
| 200 | + if err := json.Unmarshal(value, &tableInfo); err != nil { |
| 201 | + return nil, errors.Trace(err) |
| 202 | + } |
| 203 | + // cannot use dbib in the parsed table info cuz it might not set so default to 0 |
| 204 | + dbID, err := meta.ParseDBKey(rawKey.Key) |
| 205 | + if err != nil { |
| 206 | + return nil, errors.Trace(err) |
| 207 | + } |
| 208 | + |
| 209 | + // add to table rename history |
| 210 | + mp.tableHistoryManager.AddTableHistory(tableInfo.ID, tableInfo.Name.String(), dbID) |
| 211 | + |
| 212 | + // update the id map |
| 213 | + if err = mp.tableMappingManager.ProcessTableValueAndUpdateIdMapping(dbID, tableInfo); err != nil { |
| 214 | + return nil, errors.Trace(err) |
| 215 | + } |
| 216 | + } |
| 217 | + } |
| 218 | + } |
| 219 | + return filteredEntries, nil |
| 220 | +} |
| 221 | + |
| 222 | +func (mp *MetaKVInfoProcessor) GetTableMappingManager() *stream.TableMappingManager { |
| 223 | + return mp.tableMappingManager |
| 224 | +} |
| 225 | + |
| 226 | +func (mp *MetaKVInfoProcessor) GetTableHistoryManager() *stream.LogBackupTableHistoryManager { |
| 227 | + return mp.tableHistoryManager |
| 228 | +} |
0 commit comments