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

*: fix incorrect handling IterAllTables (#59894) #60040

Open
wants to merge 1 commit into
base: release-8.5
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
47 changes: 47 additions & 0 deletions pkg/ddl/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1270,3 +1270,50 @@ func TestAdminAlterDDLJobCommitFailed(t *testing.T) {
require.Equal(t, j.ReorgMeta, job.ReorgMeta)
deleteJobMetaByID(tk, job.ID)
}
<<<<<<< HEAD
=======

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

for i := 0; i < 113; i++ {
tk.MustExec(fmt.Sprintf("create database test%d", i))
tk.MustExec(fmt.Sprintf("use test%d", i))
tk.MustExec("create table t1 (a int)")
tk.MustExec("create table t2 (a int)")
tk.MustExec("create table t3 (a int)")
}

tblInfos1 := make([]*model.TableInfo, 0)
tblInfos2 := make([]*model.TableInfo, 0)
dbs := dom.InfoSchema().AllSchemas()
for _, db := range dbs {
if infoschema.IsSpecialDB(db.Name.L) {
continue
}
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()), 13, func(tblInfo *model.TableInfo) error {
tblInfos2 = append(tblInfos2, tblInfo)
return nil
})
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)
}
}
>>>>>>> 6bdacafe82d (*: fix incorrect handling IterAllTables (#59894))
79 changes: 79 additions & 0 deletions pkg/meta/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,85 @@ func (m *Mutator) IterTables(dbID int64, fn func(info *model.TableInfo) error) e
return errors.Trace(err)
}

<<<<<<< HEAD
=======
func splitRangeInt64Max(n int64) [][]string {
ranges := make([][]string, n)

// 9999999999999999999 is the max number than maxInt64 in string format.
batch := 9999999999999999999 / uint64(n)

for k := int64(0); k < n; k++ {
start := batch * uint64(k)
end := batch * uint64(k+1)

startStr := fmt.Sprintf("%019d", start)
if k == 0 {
startStr = "0"
}
endStr := fmt.Sprintf("%019d", end)

ranges[k] = []string{startStr, endStr}
}

return ranges
}

// IterAllTables iterates all the table at once, in order to avoid oom. It can use at most 15 concurrency to iterate.
// This function is optimized for 'many databases' scenario. Only 1 concurrency can work for 'many tables in one database' scenario.
func IterAllTables(ctx context.Context, store kv.Storage, startTs uint64, concurrency int, fn func(info *model.TableInfo) error) error {
cancelCtx, cancel := context.WithCancel(ctx)
defer cancel()
workGroup, _ := util.NewErrorGroupWithRecoverWithCtx(cancelCtx)

if concurrency >= 15 {
concurrency = 15
}

kvRanges := splitRangeInt64Max(int64(concurrency))

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 := []byte(fmt.Sprintf("%s:", mDBPrefix))
startKey = codec.EncodeBytes(startKey, []byte(kvRanges[i][0]))
endKey := []byte(fmt.Sprintf("%s:", mDBPrefix))
endKey = codec.EncodeBytes(endKey, []byte(kvRanges[i][1]))

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())
}

>>>>>>> 6bdacafe82d (*: fix incorrect handling IterAllTables (#59894))
// 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