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

meta: support getting all table infos quickly #58808

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
43 changes: 43 additions & 0 deletions pkg/ddl/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"fmt"
"math"
"slices"
"strconv"
"strings"
"sync"
Expand All @@ -32,6 +33,7 @@ import (
ddlutil "github.com/pingcap/tidb/pkg/ddl/util"
"github.com/pingcap/tidb/pkg/domain"
"github.com/pingcap/tidb/pkg/errno"
"github.com/pingcap/tidb/pkg/infoschema"
"github.com/pingcap/tidb/pkg/kv"
"github.com/pingcap/tidb/pkg/meta"
"github.com/pingcap/tidb/pkg/meta/model"
Expand Down Expand Up @@ -1270,3 +1272,44 @@ func TestAdminAlterDDLJobCommitFailed(t *testing.T) {
require.Equal(t, j.ReorgMeta, job.ReorgMeta)
deleteJobMetaByID(tk, job.ID)
}

func TestGetAllTableInfos(t *testing.T) {
store, dom := testkit.CreateMockStoreAndDomain(t)
tk := testkit.NewTestKit(t, store)

tk.MustExec("create table test.t1 (a int)")

tblInfos1 := make([]*model.TableInfo, 0)
tblInfos2 := make([]*model.TableInfo, 0)
dbs := dom.InfoSchema().AllSchemas()
maxDBID := int64(0)
for _, db := range dbs {
if infoschema.IsSpecialDB(db.Name.L) {
continue
}
if db.ID > maxDBID {
maxDBID = db.ID
}
info, err := dom.InfoSchema().SchemaTableInfos(context.Background(), db.Name)
require.NoError(t, err)
tblInfos1 = append(tblInfos1, info...)
}

err := meta.IterAllTables(context.Background(), store, oracle.GoTimeToTS(time.Now()), 10, func(tblInfo *model.TableInfo) error {
tblInfos2 = append(tblInfos2, tblInfo)
return nil
}, maxDBID)
require.NoError(t, err)

slices.SortFunc(tblInfos1, func(i, j *model.TableInfo) int {
return int(i.ID - j.ID)
})
slices.SortFunc(tblInfos2, func(i, j *model.TableInfo) int {
return int(i.ID - j.ID)
})
require.Equal(t, len(tblInfos1), len(tblInfos2))
for i := range tblInfos1 {
require.Equal(t, tblInfos1[i].ID, tblInfos2[i].ID)
require.Equal(t, tblInfos1[i].DBID, tblInfos2[i].DBID)
}
}
1 change: 1 addition & 0 deletions pkg/meta/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ go_library(
"//pkg/resourcegroup",
"//pkg/store/helper",
"//pkg/structure",
"//pkg/util",
"//pkg/util/codec",
"//pkg/util/dbterror",
"//pkg/util/hack",
Expand Down
55 changes: 55 additions & 0 deletions pkg/meta/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"github.com/pingcap/tidb/pkg/resourcegroup"
"github.com/pingcap/tidb/pkg/store/helper"
"github.com/pingcap/tidb/pkg/structure"
"github.com/pingcap/tidb/pkg/util"
"github.com/pingcap/tidb/pkg/util/codec"
"github.com/pingcap/tidb/pkg/util/dbterror"
"github.com/pingcap/tidb/pkg/util/hack"
Expand Down Expand Up @@ -997,6 +998,60 @@ func (m *Mutator) IterTables(dbID int64, fn func(info *model.TableInfo) error) e
return errors.Trace(err)
}

// IterAllTables iterates all the table at once, in order to avoid oom.
func IterAllTables(ctx context.Context, store kv.Storage, startTs uint64, concurrency int, fn func(info *model.TableInfo) error, hintMaxDBID int64) error {
cancelCtx, cancel := context.WithCancel(ctx)
defer cancel()
workGroup, _ := util.NewErrorGroupWithRecoverWithCtx(cancelCtx)

idBatch := math.MaxInt64
if concurrency >= 15 {
concurrency = 15
}
if hintMaxDBID == 0 {
concurrency = 1
} else {
idBatch = max(int(hintMaxDBID)/concurrency+1, 1)
}

mu := sync.Mutex{}
for i := 0; i < concurrency; i++ {
snapshot := store.GetSnapshot(kv.NewVersion(startTs))
snapshot.SetOption(kv.RequestSourceInternal, true)
snapshot.SetOption(kv.RequestSourceType, kv.InternalTxnMeta)
t := structure.NewStructure(snapshot, nil, mMetaPrefix)
workGroup.Go(func() error {
startKey := DBkey(int64(i * idBatch))
endKey := DBkey(int64((i + 1) * idBatch))
return t.IterateHashWithBoundedKey(startKey, endKey, func(key []byte, field []byte, value []byte) error {
// only handle table meta
tableKey := string(field)
if !strings.HasPrefix(tableKey, mTablePrefix) {
return nil
}

tbInfo := &model.TableInfo{}
err := json.Unmarshal(value, tbInfo)
if err != nil {
return errors.Trace(err)
}
dbID, err := ParseDBKey(key)
if err != nil {
return errors.Trace(err)
}
tbInfo.DBID = dbID

mu.Lock()
err = fn(tbInfo)
mu.Unlock()
return errors.Trace(err)
})
})
}

return errors.Trace(workGroup.Wait())
}

// GetMetasByDBID return all meta information of a database.
// Note(dongmen): This method is used by TiCDC to reduce the time of changefeed initialization.
// Ref: https://github.com/pingcap/tiflow/issues/11109
Expand Down
28 changes: 28 additions & 0 deletions pkg/structure/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,34 @@ func (t *TxStructure) IterateHash(key []byte, fn func(k []byte, v []byte) error)
return nil
}

// IterateHashWithBoundedKey iterates all the fields and values in hash with a bounded key.
func (t *TxStructure) IterateHashWithBoundedKey(hashStartKey []byte, hashEndKey []byte, fn func(k []byte, f []byte, v []byte) error) error {
hashStartKey = t.hashDataKeyPrefix(hashStartKey)
hashEndKey = t.hashDataKeyPrefix(hashEndKey)
it, err := t.reader.Iter(hashStartKey, hashEndKey)
if err != nil {
return errors.Trace(err)
}

var field []byte
var key []byte
for it.Valid() {
key, field, err = t.decodeHashDataKey(it.Key())
if err != nil {
continue
}
if err = fn(key, field, it.Value()); err != nil {
return errors.Trace(err)
}
err = it.Next()
if err != nil {
return errors.Trace(err)
}
}

return nil
}

// ReverseHashIterator is the reverse hash iterator.
type ReverseHashIterator struct {
t *TxStructure
Expand Down