Skip to content
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

snapshot (ticdc): reduce list tables time consumption (#11095) #11126

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 22 additions & 15 deletions cdc/entry/schema/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"math"
"strings"
"sync"
"time"

"github.com/google/btree"
"github.com/pingcap/errors"
Expand Down Expand Up @@ -146,15 +147,14 @@
forceReplicate bool,
filter filter.Filter,
) (*Snapshot, error) {
start := time.Now()

Check warning on line 150 in cdc/entry/schema/snapshot.go

View check run for this annotation

Codecov / codecov/patch

cdc/entry/schema/snapshot.go#L150

Added line #L150 was not covered by tests
snap := NewEmptySnapshot(forceReplicate)
dbinfos, err := meta.ListDatabases()
if err != nil {
return nil, cerror.WrapError(cerror.ErrMetaListDatabases, err)
}
// `tag` is used to reverse sort all versions in the generated snapshot.
tag := negative(currentTs)
// record all tables to be replicated for logging use
tables := make([]*model.TableInfo, 0, 1024)
for _, dbinfo := range dbinfos {
if filter.ShouldIgnoreSchema(dbinfo.Name.O) {
log.Debug("ignore database", zap.String("db", dbinfo.Name.O))
Expand All @@ -167,16 +167,30 @@
vname := newVersionedEntityName(-1, dbinfo.Name.O, tag) // -1 means the entity is a schema.
vname.target = dbinfo.ID
snap.inner.schemaNameToID.ReplaceOrInsert(vname)

tableInfos, err := meta.ListTables(dbinfo.ID)
// get all tables Name
tableNames, err := meta.ListSimpleTables(dbinfo.ID)

Check warning on line 171 in cdc/entry/schema/snapshot.go

View check run for this annotation

Codecov / codecov/patch

cdc/entry/schema/snapshot.go#L170-L171

Added lines #L170 - L171 were not covered by tests
if err != nil {
return nil, cerror.WrapError(cerror.ErrMetaListDatabases, err)
}
for _, tableInfo := range tableInfos {
if filter.ShouldIgnoreTable(dbinfo.Name.O, tableInfo.Name.O) {
log.Debug("ignore table", zap.String("table", tableInfo.Name.O))
tableNeeded := make([]*timodel.TableNameInfo, 0, len(tableNames))
// filter tables
for _, table := range tableNames {
if filter.ShouldIgnoreTable(dbinfo.Name.O, table.Name.O) {
log.Debug("ignore table", zap.String("table", table.Name.O))

Check warning on line 179 in cdc/entry/schema/snapshot.go

View check run for this annotation

Codecov / codecov/patch

cdc/entry/schema/snapshot.go#L175-L179

Added lines #L175 - L179 were not covered by tests
continue
}
tableNeeded = append(tableNeeded, table)

Check warning on line 182 in cdc/entry/schema/snapshot.go

View check run for this annotation

Codecov / codecov/patch

cdc/entry/schema/snapshot.go#L182

Added line #L182 was not covered by tests
}
tableInfos := make([]*timodel.TableInfo, 0, len(tableNeeded))
for _, table := range tableNeeded {
tableInfo, err := meta.GetTable(dbinfo.ID, table.ID)
if err != nil {
return nil, errors.Trace(err)
}
tableInfos = append(tableInfos, tableInfo)

Check warning on line 190 in cdc/entry/schema/snapshot.go

View check run for this annotation

Codecov / codecov/patch

cdc/entry/schema/snapshot.go#L184-L190

Added lines #L184 - L190 were not covered by tests
}

for _, tableInfo := range tableInfos {

Check warning on line 193 in cdc/entry/schema/snapshot.go

View check run for this annotation

Codecov / codecov/patch

cdc/entry/schema/snapshot.go#L193

Added line #L193 was not covered by tests
tableInfo := model.WrapTableInfo(dbinfo.ID, dbinfo.Name.O, currentTs, tableInfo)
snap.inner.tables.ReplaceOrInsert(versionedID{
id: tableInfo.ID,
Expand All @@ -193,8 +207,6 @@
ineligible := !tableInfo.IsEligible(forceReplicate)
if ineligible {
snap.inner.ineligibleTables.ReplaceOrInsert(versionedID{id: tableInfo.ID, tag: tag})
} else {
tables = append(tables, tableInfo)
}
if pi := tableInfo.GetPartitionInfo(); pi != nil {
for _, partition := range pi.Definitions {
Expand All @@ -209,15 +221,10 @@
}
}
snap.inner.currentTs = currentTs
var sb strings.Builder
sb.WriteString(fmt.Sprintf("%d tables to be replicated: ", len(tables)))
for _, table := range tables {
sb.WriteString(fmt.Sprintf("%s.%s, ", table.TableName.Schema, table.TableName.Table))
}
log.Info("schema snapshot created",
zap.Stringer("changefeed", id),
zap.Uint64("currentTs", currentTs),
zap.String("tables", sb.String()))
zap.Any("duration", time.Since(start).Seconds()))

Check warning on line 227 in cdc/entry/schema/snapshot.go

View check run for this annotation

Codecov / codecov/patch

cdc/entry/schema/snapshot.go#L227

Added line #L227 was not covered by tests
return snap, nil
}

Expand Down
Loading