Skip to content

Commit

Permalink
*: optimize the speed of full load (#54185)
Browse files Browse the repository at this point in the history
ref #50959
  • Loading branch information
wjhuang2016 authored Jul 15, 2024
1 parent e069ee7 commit 2934442
Show file tree
Hide file tree
Showing 36 changed files with 577 additions and 936 deletions.
16 changes: 6 additions & 10 deletions pkg/bindinfo/capture_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package bindinfo_test

import (
"context"
"fmt"
"strings"
"testing"
Expand Down Expand Up @@ -288,16 +289,11 @@ func TestCapturePlanBaselineIgnoreTiFlash(t *testing.T) {
// Create virtual tiflash replica info.
domSession := domain.GetDomain(tk.Session())
is := domSession.InfoSchema()
db, exists := is.SchemaByName(model.NewCIStr("test"))
require.True(t, exists)
for _, tbl := range is.SchemaTables(db.Name) {
tblInfo := tbl.Meta()
if tblInfo.Name.L == "t" {
tblInfo.TiFlashReplica = &model.TiFlashReplicaInfo{
Count: 1,
Available: true,
}
}
tbl, err := is.TableByName(context.Background(), model.NewCIStr("test"), model.NewCIStr("t"))
require.NoError(t, err)
tbl.Meta().TiFlashReplica = &model.TiFlashReplicaInfo{
Count: 1,
Available: true,
}
// Here the plan is the TiFlash plan.
rows := tk.MustQuery("explain select * from t").Rows()
Expand Down
15 changes: 5 additions & 10 deletions pkg/bindinfo/tests/bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,16 +568,11 @@ func TestBindingWithIsolationRead(t *testing.T) {
// Create virtual tiflash replica info.
dom := domain.GetDomain(tk.Session())
is := dom.InfoSchema()
db, exists := is.SchemaByName(model.NewCIStr("test"))
require.True(t, exists)
for _, tbl := range is.SchemaTables(db.Name) {
tblInfo := tbl.Meta()
if tblInfo.Name.L == "t" {
tblInfo.TiFlashReplica = &model.TiFlashReplicaInfo{
Count: 1,
Available: true,
}
}
tbl, err := is.TableByName(context.Background(), model.NewCIStr("test"), model.NewCIStr("t"))
require.NoError(t, err)
tbl.Meta().TiFlashReplica = &model.TiFlashReplicaInfo{
Count: 1,
Available: true,
}
tk.MustExec("create global binding for select * from t where a >= 1 and b >= 1 using select * from t use index(idx_a) where a >= 1 and b >= 1")
tk.MustExec("set @@tidb_use_plan_baselines = 1")
Expand Down
3 changes: 2 additions & 1 deletion pkg/ddl/tests/partition/db_partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2621,6 +2621,8 @@ func getPartitionTableRecordsNum(t *testing.T, ctx sessionctx.Context, tbl table
func TestPartitionErrorCode(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk1 := testkit.NewTestKit(t, store)

// add partition
tk.MustExec("set @@session.tidb_enable_table_partition = 1")
tk.MustExec("drop database if exists test_db_with_partition")
Expand Down Expand Up @@ -2665,7 +2667,6 @@ func TestPartitionErrorCode(t *testing.T) {
tk.MustGetErrCode("alter table t_part repair partition p1;", errno.ErrUnsupportedDDLOperation)

// Reduce the impact on DML when executing partition DDL
tk1 := testkit.NewTestKit(t, store)
tk1.MustExec("use test")
tk1.MustExec("set global tidb_enable_metadata_lock=0")
tk1.MustExec("drop table if exists t;")
Expand Down
3 changes: 2 additions & 1 deletion pkg/ddl/tiflash_replica_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,12 @@ func TestSkipSchemaChecker(t *testing.T) {

store := testkit.CreateMockStoreWithSchemaLease(t, tiflashReplicaLease)
tk := testkit.NewTestKit(t, store)
tk2 := testkit.NewTestKit(t, store)

tk.MustExec("use test")
tk.MustExec("set global tidb_enable_metadata_lock=0")
tk.MustExec("drop table if exists t1")
tk.MustExec("create table t1 (a int)")
tk2 := testkit.NewTestKit(t, store)
tk2.MustExec("use test")

// Test skip schema checker for ActionSetTiFlashReplica.
Expand Down
24 changes: 20 additions & 4 deletions pkg/domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,10 @@ const fetchSchemaConcurrency = 1

func (*Domain) splitForConcurrentFetch(schemas []*model.DBInfo) [][]*model.DBInfo {
groupSize := (len(schemas) + fetchSchemaConcurrency - 1) / fetchSchemaConcurrency
if variable.SchemaCacheSize.Load() > 0 && len(schemas) > 1000 {
// TODO: Temporary solution to speed up when too many databases, will refactor it later.
groupSize = 8
}
splitted := make([][]*model.DBInfo, 0, fetchSchemaConcurrency)
schemaCnt := len(schemas)
for i := 0; i < schemaCnt; i += groupSize {
Expand All @@ -471,10 +475,22 @@ func (*Domain) fetchSchemasWithTables(schemas []*model.DBInfo, m *meta.Meta, don
// schema is not public, can't be used outside.
continue
}
tables, err := m.ListTables(di.ID)
if err != nil {
done <- err
return
var tables []*model.TableInfo
var err error
if variable.SchemaCacheSize.Load() > 0 && !infoschema.IsSpecialDB(di.Name.L) {
name2ID, specialTableInfos, err := meta.GetAllNameToIDAndSpecialAttributeInfo(m, di.ID)
if err != nil {
done <- err
return
}
di.TableName2ID = name2ID
tables = specialTableInfos
} else {
tables, err = m.ListTables(di.ID)
if err != nil {
done <- err
return
}
}
// If TreatOldVersionUTF8AsUTF8MB4 was enable, need to convert the old version schema UTF8 charset to UTF8MB4.
if config.GetGlobalConfig().TreatOldVersionUTF8AsUTF8MB4 {
Expand Down
1 change: 1 addition & 0 deletions pkg/executor/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ go_test(
"//pkg/planner/core/base",
"//pkg/planner/property",
"//pkg/planner/util",
"//pkg/planner/util/coretestsdk",
"//pkg/server",
"//pkg/session",
"//pkg/session/types",
Expand Down
1 change: 1 addition & 0 deletions pkg/executor/internal/querywatch/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ go_test(
"//pkg/errno",
"//pkg/kv",
"//pkg/meta/autoid",
"//pkg/sessionctx/variable",
"//pkg/testkit",
"//pkg/testkit/testsetup",
"@com_github_pingcap_failpoint//:failpoint",
Expand Down
4 changes: 4 additions & 0 deletions pkg/executor/internal/querywatch/query_watch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@ import (
"github.com/pingcap/failpoint"
mysql "github.com/pingcap/tidb/pkg/errno"
"github.com/pingcap/tidb/pkg/kv"
"github.com/pingcap/tidb/pkg/sessionctx/variable"
"github.com/pingcap/tidb/pkg/testkit"
"github.com/stretchr/testify/require"
)

func TestQueryWatch(t *testing.T) {
store, dom := testkit.CreateMockStoreAndDomain(t)
tk := testkit.NewTestKit(t, store)
if variable.SchemaCacheSize.Load() != 0 {
t.Skip("skip this test because the schema cache is enabled")
}
tk.MustExec("use test")
tk.MustExec("create table t1(a int)")
tk.MustExec("insert into t1 values(1)")
Expand Down
25 changes: 13 additions & 12 deletions pkg/executor/partition_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/pingcap/tidb/pkg/domain"
"github.com/pingcap/tidb/pkg/infoschema"
"github.com/pingcap/tidb/pkg/parser/model"
"github.com/pingcap/tidb/pkg/planner/util/coretestsdk"
"github.com/pingcap/tidb/pkg/testkit"
"github.com/pingcap/tidb/pkg/testkit/external"
"github.com/pingcap/tidb/pkg/util/dbterror/exeerrors"
Expand Down Expand Up @@ -316,18 +317,18 @@ func TestOrderByAndLimit(t *testing.T) {

// Create virtual tiflash replica info.
dom := domain.GetDomain(tk.Session())
is := dom.InfoSchema()
db, exists := is.SchemaByName(model.NewCIStr("test_orderby_limit"))
require.True(t, exists)
for _, tbl := range is.SchemaTables(db.Name) {
tblInfo := tbl.Meta()
if strings.HasPrefix(tblInfo.Name.L, "tr") || strings.HasPrefix(tblInfo.Name.L, "thash") || strings.HasPrefix(tblInfo.Name.L, "tlist") {
tblInfo.TiFlashReplica = &model.TiFlashReplicaInfo{
Count: 1,
Available: true,
}
}
}
coretestsdk.SetTiFlashReplica(t, dom, "test_orderby_limit", "trange")
coretestsdk.SetTiFlashReplica(t, dom, "test_orderby_limit", "thash")
coretestsdk.SetTiFlashReplica(t, dom, "test_orderby_limit", "tlist")
coretestsdk.SetTiFlashReplica(t, dom, "test_orderby_limit", "tregular")
coretestsdk.SetTiFlashReplica(t, dom, "test_orderby_limit", "trange_intpk")
coretestsdk.SetTiFlashReplica(t, dom, "test_orderby_limit", "thash_intpk")
coretestsdk.SetTiFlashReplica(t, dom, "test_orderby_limit", "tlist_intpk")
coretestsdk.SetTiFlashReplica(t, dom, "test_orderby_limit", "tregular_intpk")
coretestsdk.SetTiFlashReplica(t, dom, "test_orderby_limit", "trange_clustered")
coretestsdk.SetTiFlashReplica(t, dom, "test_orderby_limit", "thash_clustered")
coretestsdk.SetTiFlashReplica(t, dom, "test_orderby_limit", "tlist_clustered")
coretestsdk.SetTiFlashReplica(t, dom, "test_orderby_limit", "tregular_clustered")
tk.MustExec("set @@session.tidb_isolation_read_engines=\"tikv\"")

// test indexLookUp
Expand Down
16 changes: 6 additions & 10 deletions pkg/executor/prepared_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package executor_test

import (
"context"
"fmt"
"strconv"
"strings"
Expand Down Expand Up @@ -1038,16 +1039,11 @@ func TestPrepareStmtAfterIsolationReadChange(t *testing.T) {

// create virtual tiflash replica.
is := dom.InfoSchema()
db, exists := is.SchemaByName(model.NewCIStr("test"))
require.True(t, exists)
for _, tbl := range is.SchemaTables(db.Name) {
tblInfo := tbl.Meta()
if tblInfo.Name.L == "t" {
tblInfo.TiFlashReplica = &model.TiFlashReplicaInfo{
Count: 1,
Available: true,
}
}
tbl, err := is.TableByName(context.Background(), model.NewCIStr("test"), model.NewCIStr("t"))
require.NoError(t, err)
tbl.Meta().TiFlashReplica = &model.TiFlashReplicaInfo{
Count: 1,
Available: true,
}

tk.MustExec("set @@session.tidb_isolation_read_engines='tikv'")
Expand Down
15 changes: 5 additions & 10 deletions pkg/executor/test/showtest/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,16 +288,11 @@ func TestShowWarningsForExprPushdown(t *testing.T) {
// create tiflash replica
{
is := dom.InfoSchema()
db, exists := is.SchemaByName(model.NewCIStr("test"))
require.True(t, exists)
for _, tbl := range is.SchemaTables(db.Name) {
tblInfo := tbl.Meta()
if tblInfo.Name.L == "show_warnings_expr_pushdown" {
tblInfo.TiFlashReplica = &model.TiFlashReplicaInfo{
Count: 1,
Available: true,
}
}
tblInfo, err := is.TableByName(context.Background(), model.NewCIStr("test"), model.NewCIStr("show_warnings_expr_pushdown"))
require.NoError(t, err)
tblInfo.Meta().TiFlashReplica = &model.TiFlashReplicaInfo{
Count: 1,
Available: true,
}
}
tk.MustExec("set tidb_allow_mpp=0")
Expand Down
32 changes: 11 additions & 21 deletions pkg/expression/integration_test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -649,16 +649,11 @@ func TestShardIndexOnTiFlash(t *testing.T) {
// Create virtual tiflash replica info.
dom := domain.GetDomain(tk.Session())
is := dom.InfoSchema()
db, exists := is.SchemaByName(model.NewCIStr("test"))
require.True(t, exists)
for _, tbl := range is.SchemaTables(db.Name) {
tblInfo := tbl.Meta()
if tblInfo.Name.L == "t" {
tblInfo.TiFlashReplica = &model.TiFlashReplicaInfo{
Count: 1,
Available: true,
}
}
tbl, err := is.TableByName(context.Background(), model.NewCIStr("test"), model.NewCIStr("t"))
require.NoError(t, err)
tbl.Meta().TiFlashReplica = &model.TiFlashReplicaInfo{
Count: 1,
Available: true,
}
tk.MustExec("set @@session.tidb_isolation_read_engines = 'tiflash'")
tk.MustExec("set @@session.tidb_enforce_mpp = 1")
Expand All @@ -672,7 +667,7 @@ func TestShardIndexOnTiFlash(t *testing.T) {
tk.MustExec("set @@session.tidb_enforce_mpp = 0")
tk.MustExec("set @@session.tidb_allow_mpp = 0")
// when we isolated the read engine as 'tiflash' and banned TiDB opening allow-mpp, no suitable plan is generated.
_, err := tk.Exec("explain select max(b) from t")
_, err = tk.Exec("explain select max(b) from t")
require.NotNil(t, err)
require.Equal(t, err.Error(), "[planner:1815]Internal : Can't find a proper physical plan for this query")
}
Expand All @@ -690,16 +685,11 @@ func TestExprPushdownBlacklist(t *testing.T) {
// Create virtual tiflash replica info.
dom := domain.GetDomain(tk.Session())
is := dom.InfoSchema()
db, exists := is.SchemaByName(model.NewCIStr("test"))
require.True(t, exists)
for _, tbl := range is.SchemaTables(db.Name) {
tblInfo := tbl.Meta()
if tblInfo.Name.L == "t" {
tblInfo.TiFlashReplica = &model.TiFlashReplicaInfo{
Count: 1,
Available: true,
}
}
tbl, err := is.TableByName(context.Background(), model.NewCIStr("test"), model.NewCIStr("t"))
require.NoError(t, err)
tbl.Meta().TiFlashReplica = &model.TiFlashReplicaInfo{
Count: 1,
Available: true,
}

tk.MustExec("insert into mysql.expr_pushdown_blacklist " +
Expand Down
19 changes: 18 additions & 1 deletion pkg/infoschema/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -919,19 +919,36 @@ func (b *Builder) createSchemaTablesForDB(di *model.DBInfo, tableFromMeta tableF

schTbls.tables[t.Name.L] = tbl
b.addTable(schemaVersion, di, t, tbl)
if len(di.TableName2ID) > 0 {
delete(di.TableName2ID, t.Name.L)
}

if tblInfo := tbl.Meta(); tblInfo.TempTableType != model.TempTableNone {
b.addTemporaryTable(tblInfo.ID)
}
}
// Add the rest name to ID mappings.
if b.enableV2 {
for name, id := range di.TableName2ID {
item := tableItem{
dbName: di.Name.L,
dbID: di.ID,
tableName: name,
tableID: id,
schemaVersion: schemaVersion,
}
b.infoData.byID.Set(item)
b.infoData.byName.Set(item)
}
}
b.addDB(schemaVersion, di, schTbls)

return nil
}

func (b *Builder) addDB(schemaVersion int64, di *model.DBInfo, schTbls *schemaTables) {
if b.enableV2 {
if isSpecialDB(di.Name.L) {
if IsSpecialDB(di.Name.L) {
b.infoData.addSpecialDB(di, schTbls)
} else {
b.infoData.addDB(schemaVersion, di)
Expand Down
11 changes: 6 additions & 5 deletions pkg/infoschema/infoschema_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,8 @@ func (is *infoschemaV2) tableByID(id int64, noRefill bool) (val table.Table, ok
return ret, true
}

func isSpecialDB(dbName string) bool {
// IsSpecialDB tells whether the database is a special database.
func IsSpecialDB(dbName string) bool {
return dbName == util.InformationSchemaName.L ||
dbName == util.PerformanceSchemaName.L ||
dbName == util.MetricSchemaName.L
Expand All @@ -455,7 +456,7 @@ func (is *infoschemaV2) EvictTable(schema, tbl string) {
}

func (is *infoschemaV2) TableByName(ctx context.Context, schema, tbl model.CIStr) (t table.Table, err error) {
if isSpecialDB(schema.L) {
if IsSpecialDB(schema.L) {
if raw, ok := is.specials.Load(schema.L); ok {
tbNames := raw.(*schemaTables)
if t, ok = tbNames.tables[tbl.L]; ok {
Expand Down Expand Up @@ -504,7 +505,7 @@ func (is *infoschemaV2) TableInfoByID(id int64) (*model.TableInfo, bool) {

// SchemaTableInfos implements InfoSchema.FindTableInfoByPartitionID
func (is *infoschemaV2) SchemaTableInfos(schema model.CIStr) []*model.TableInfo {
if isSpecialDB(schema.L) {
if IsSpecialDB(schema.L) {
raw, ok := is.Data.specials.Load(schema.L)
if ok {
schTbls := raw.(*schemaTables)
Expand Down Expand Up @@ -554,7 +555,7 @@ func (is *infoschemaV2) FindTableInfoByPartitionID(
}

func (is *infoschemaV2) SchemaByName(schema model.CIStr) (val *model.DBInfo, ok bool) {
if isSpecialDB(schema.L) {
if IsSpecialDB(schema.L) {
raw, ok := is.Data.specials.Load(schema.L)
if !ok {
return nil, false
Expand Down Expand Up @@ -727,7 +728,7 @@ func (is *infoschemaV2) SchemaByID(id int64) (*model.DBInfo, bool) {
}

func (is *infoschemaV2) SchemaTables(schema model.CIStr) (tables []table.Table) {
if isSpecialDB(schema.L) {
if IsSpecialDB(schema.L) {
raw, ok := is.Data.specials.Load(schema.L)
if ok {
schTbls := raw.(*schemaTables)
Expand Down
3 changes: 2 additions & 1 deletion pkg/meta/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ go_library(
"//pkg/parser/mysql",
"//pkg/structure",
"//pkg/util/dbterror",
"//pkg/util/hack",
"@com_github_pingcap_errors//:errors",
"@com_github_pingcap_kvproto//pkg/kvrpcpb",
"@com_github_pingcap_kvproto//pkg/resource_manager",
Expand All @@ -32,7 +33,7 @@ go_test(
],
embed = [":meta"],
flaky = True,
shard_count = 12,
shard_count = 14,
deps = [
"//pkg/kv",
"//pkg/parser/ast",
Expand Down
Loading

0 comments on commit 2934442

Please sign in to comment.