-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Wenqi Mou <[email protected]>
- Loading branch information
1 parent
6e22b8c
commit e9dd4d4
Showing
53 changed files
with
1,760 additions
and
879 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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,131 @@ | ||
// 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 logclient | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
|
||
"github.com/pingcap/errors" | ||
backuppb "github.com/pingcap/kvproto/pkg/brpb" | ||
"github.com/pingcap/tidb/br/pkg/stream" | ||
"github.com/pingcap/tidb/br/pkg/utils" | ||
"github.com/pingcap/tidb/br/pkg/utils/consts" | ||
"github.com/pingcap/tidb/pkg/meta" | ||
"github.com/pingcap/tidb/pkg/meta/model" | ||
) | ||
|
||
// BatchFileProcessor defines how to process a batch of files | ||
type BatchFileProcessor interface { | ||
// process a batch of files and with a filterTS and return what's not processed for next iteration | ||
processBatch( | ||
ctx context.Context, | ||
files []*backuppb.DataFileInfo, | ||
entries []*KvEntryWithTS, | ||
filterTS uint64, | ||
cf string, | ||
) ([]*KvEntryWithTS, error) | ||
} | ||
|
||
// RestoreProcessor implements BatchFileProcessor for restoring files | ||
type RestoreProcessor struct { | ||
client *LogClient | ||
schemasReplace *stream.SchemasReplace | ||
updateStats func(kvCount uint64, size uint64) | ||
progressInc func() | ||
} | ||
|
||
func (rp *RestoreProcessor) processBatch( | ||
ctx context.Context, | ||
files []*backuppb.DataFileInfo, | ||
entries []*KvEntryWithTS, | ||
filterTS uint64, | ||
cf string, | ||
) ([]*KvEntryWithTS, error) { | ||
return rp.client.RestoreBatchMetaKVFiles( | ||
ctx, files, rp.schemasReplace, entries, | ||
filterTS, rp.updateStats, rp.progressInc, cf, | ||
) | ||
} | ||
|
||
// DDLCollector implements BatchFileProcessor for collecting DDL information | ||
// 1. It collects table renaming information. The table rename operation will not change the table id, and the process | ||
// will drop the original table and create a new one with the same table id, so in DDL history there will be two events | ||
// that corresponds to the same table id. | ||
// | ||
// add more logic in future if needed | ||
type DDLCollector struct { | ||
client *LogClient | ||
tableRenameInfo *stream.LogBackupTableHistory | ||
} | ||
|
||
func (dc *DDLCollector) processBatch( | ||
ctx context.Context, | ||
files []*backuppb.DataFileInfo, | ||
entries []*KvEntryWithTS, | ||
filterTS uint64, | ||
cf string, | ||
) ([]*KvEntryWithTS, error) { | ||
// doesn't need to parse writeCF as it contains value like "p\XXXX\XXX" which is meaningless. | ||
// DefaultCF value should contain everything we want for DDL operation | ||
if cf == consts.WriteCF { | ||
return nil, nil | ||
} | ||
|
||
curSortedEntries, filteredEntries, err := dc.client.filterAndSortKvEntriesFromFiles(ctx, files, entries, filterTS) | ||
if err != nil { | ||
return nil, errors.Trace(err) | ||
} | ||
|
||
// process entries to collect table IDs | ||
for _, entry := range curSortedEntries { | ||
value := entry.E.Value | ||
|
||
if utils.IsMetaDBKey(entry.E.Key) { | ||
rawKey, err := stream.ParseTxnMetaKeyFrom(entry.E.Key) | ||
if err != nil { | ||
return nil, errors.Trace(err) | ||
} | ||
|
||
// collect db id -> name mapping during log backup, it will contain information about newly created db | ||
if meta.IsDBkey(rawKey.Field) { | ||
var dbInfo model.DBInfo | ||
if err := json.Unmarshal(value, &dbInfo); err != nil { | ||
return nil, errors.Trace(err) | ||
} | ||
dc.tableRenameInfo.RecordDBIdToName(dbInfo.ID, dbInfo.Name.O) | ||
} else if !meta.IsDBkey(rawKey.Key) { | ||
// also see RewriteMetaKvEntry | ||
continue | ||
} | ||
|
||
// collect table history indexed by table id, same id may have different table names in history | ||
if meta.IsTableKey(rawKey.Field) { | ||
var tableInfo model.TableInfo | ||
if err := json.Unmarshal(value, &tableInfo); err != nil { | ||
return nil, errors.Trace(err) | ||
} | ||
// cannot use dbib in the parsed table info cuz it might not set so default to 0 | ||
dbID, err := meta.ParseDBKey(rawKey.Key) | ||
if err != nil { | ||
return nil, errors.Trace(err) | ||
} | ||
|
||
dc.tableRenameInfo.AddTableHistory(tableInfo.ID, tableInfo.Name.String(), dbID) | ||
} | ||
} | ||
} | ||
return filteredEntries, nil | ||
} |
Oops, something went wrong.