|
| 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/br/pkg/utils/consts" |
| 27 | + "github.com/pingcap/tidb/pkg/meta" |
| 28 | + "github.com/pingcap/tidb/pkg/meta/model" |
| 29 | + "go.uber.org/zap" |
| 30 | +) |
| 31 | + |
| 32 | +// BatchFileProcessor defines how to process a batch of files |
| 33 | +type BatchFileProcessor interface { |
| 34 | + // process a batch of files and with a filterTS and return what's not processed for next iteration |
| 35 | + processBatch( |
| 36 | + ctx context.Context, |
| 37 | + files []*backuppb.DataFileInfo, |
| 38 | + entries []*KvEntryWithTS, |
| 39 | + filterTS uint64, |
| 40 | + cf string, |
| 41 | + ) ([]*KvEntryWithTS, error) |
| 42 | +} |
| 43 | + |
| 44 | +// RestoreProcessor implements BatchFileProcessor for restoring files |
| 45 | +type RestoreProcessor struct { |
| 46 | + client *LogClient |
| 47 | + schemasReplace *stream.SchemasReplace |
| 48 | + updateStats func(kvCount uint64, size uint64) |
| 49 | + progressInc func() |
| 50 | +} |
| 51 | + |
| 52 | +func (rp *RestoreProcessor) processBatch( |
| 53 | + ctx context.Context, |
| 54 | + files []*backuppb.DataFileInfo, |
| 55 | + entries []*KvEntryWithTS, |
| 56 | + filterTS uint64, |
| 57 | + cf string, |
| 58 | +) ([]*KvEntryWithTS, error) { |
| 59 | + return rp.client.RestoreBatchMetaKVFiles( |
| 60 | + ctx, files, rp.schemasReplace, entries, |
| 61 | + filterTS, rp.updateStats, rp.progressInc, cf, |
| 62 | + ) |
| 63 | +} |
| 64 | + |
| 65 | +// DDLCollector implements BatchFileProcessor for collecting DDL information |
| 66 | +// 1. It collects table renaming information. The table rename operation will not change the table id, and the process |
| 67 | +// will drop the original table and create a new one with the same table id, so in DDL history there will be two events |
| 68 | +// that corresponds to the same table id. |
| 69 | +// |
| 70 | +// add more logic in future if needed |
| 71 | +type DDLCollector struct { |
| 72 | + client *LogClient |
| 73 | + tableRenameInfo *stream.LogBackupTableHistory |
| 74 | +} |
| 75 | + |
| 76 | +func (dc *DDLCollector) processBatch( |
| 77 | + ctx context.Context, |
| 78 | + files []*backuppb.DataFileInfo, |
| 79 | + entries []*KvEntryWithTS, |
| 80 | + filterTS uint64, |
| 81 | + cf string, |
| 82 | +) ([]*KvEntryWithTS, error) { |
| 83 | + // doesn't need to parse writeCF as it contains value like "p\XXXX\XXX" which is meaningless. |
| 84 | + // DefaultCF value should contain everything we want for DDL operation |
| 85 | + if cf == consts.WriteCF { |
| 86 | + return nil, nil |
| 87 | + } |
| 88 | + |
| 89 | + curSortedEntries, filteredEntries, err := dc.client.filterAndSortKvEntriesFromFiles(ctx, files, entries, filterTS) |
| 90 | + if err != nil { |
| 91 | + return nil, errors.Trace(err) |
| 92 | + } |
| 93 | + |
| 94 | + // process entries to collect table IDs |
| 95 | + for _, entry := range curSortedEntries { |
| 96 | + value := entry.E.Value |
| 97 | + |
| 98 | + if utils.IsMetaDBKey(entry.E.Key) { |
| 99 | + rawKey, err := stream.ParseTxnMetaKeyFrom(entry.E.Key) |
| 100 | + if err != nil { |
| 101 | + return nil, errors.Trace(err) |
| 102 | + } |
| 103 | + |
| 104 | + // collect db id -> name mapping during log backup, it will contain information about newly created db |
| 105 | + if meta.IsDBkey(rawKey.Field) { |
| 106 | + var dbInfo model.DBInfo |
| 107 | + if err := json.Unmarshal(value, &dbInfo); err != nil { |
| 108 | + return nil, errors.Trace(err) |
| 109 | + } |
| 110 | + dc.tableRenameInfo.RecordDBIdToName(dbInfo.ID, dbInfo.Name.O) |
| 111 | + } else if !meta.IsDBkey(rawKey.Key) { |
| 112 | + // also see RewriteMetaKvEntry |
| 113 | + continue |
| 114 | + } |
| 115 | + |
| 116 | + // collect table history indexed by table id, same id may have different table names in history |
| 117 | + if meta.IsTableKey(rawKey.Field) { |
| 118 | + var tableInfo model.TableInfo |
| 119 | + if err := json.Unmarshal(value, &tableInfo); err != nil { |
| 120 | + return nil, errors.Trace(err) |
| 121 | + } |
| 122 | + // cannot use dbib in the parsed table info cuz it might not set so default to 0 |
| 123 | + dbID, err := meta.ParseDBKey(rawKey.Key) |
| 124 | + if err != nil { |
| 125 | + return nil, errors.Trace(err) |
| 126 | + } |
| 127 | + |
| 128 | + log.Info("######################################## adding table info", zap.Int64("tableid", tableInfo.ID), zap.String("table name", tableInfo.Name.O), zap.Int64("db id", dbID)) |
| 129 | + dc.tableRenameInfo.AddTableHistory(tableInfo.ID, tableInfo.Name.String(), dbID) |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + return filteredEntries, nil |
| 134 | +} |
0 commit comments