Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
2 changes: 1 addition & 1 deletion pkg/domain/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ go_library(
"//pkg/util/sqlexec",
"//pkg/util/sqlkiller",
"//pkg/util/syncutil",
"//pkg/workloadbasedlearning",
"//pkg/workloadlearning",
"@com_github_burntsushi_toml//:toml",
"@com_github_ngaut_pools//:pools",
"@com_github_pingcap_errors//:errors",
Expand Down
8 changes: 4 additions & 4 deletions pkg/domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ import (
"github.com/pingcap/tidb/pkg/util/size"
"github.com/pingcap/tidb/pkg/util/sqlkiller"
"github.com/pingcap/tidb/pkg/util/syncutil"
"github.com/pingcap/tidb/pkg/workloadbasedlearning"
"github.com/pingcap/tidb/pkg/workloadlearning"
"github.com/tikv/client-go/v2/tikv"
"github.com/tikv/client-go/v2/txnkv/transaction"
pd "github.com/tikv/pd/client"
Expand Down Expand Up @@ -3394,7 +3394,7 @@ func (do *Domain) planCacheEvictTrigger() {

// SetupWorkloadBasedLearningWorker sets up all of the workload based learning workers.
func (do *Domain) SetupWorkloadBasedLearningWorker() {
wbLearningHandle := workloadbasedlearning.NewWorkloadBasedLearningHandle()
wbLearningHandle := workloadlearning.NewWorkloadLearningHandle(do.sysSessionPool)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as discussed to short the pkg name as wbl as well?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Has been discuss in our group. We will use the workloadlearning uniform

// Start the workload based learning worker to analyze the read workload by statement_summary.
do.wg.Run(
func() {
Expand All @@ -3406,7 +3406,7 @@ func (do *Domain) SetupWorkloadBasedLearningWorker() {
}

// readTableCostWorker is a background worker that periodically analyze the read path table cost by statement_summary.
func (do *Domain) readTableCostWorker(wbLearningHandle *workloadbasedlearning.Handle) {
func (do *Domain) readTableCostWorker(wbLearningHandle *workloadlearning.Handle) {
// Recover the panic and log the error when worker exit.
defer util.Recover(metrics.LabelDomain, "readTableCostWorker", nil, false)
readTableCostTicker := time.NewTicker(vardef.WorkloadBasedLearningInterval.Load())
Expand All @@ -3418,7 +3418,7 @@ func (do *Domain) readTableCostWorker(wbLearningHandle *workloadbasedlearning.Ha
select {
case <-readTableCostTicker.C:
if vardef.EnableWorkloadBasedLearning.Load() && do.statsOwner.IsOwner() {
wbLearningHandle.HandleReadTableCost()
wbLearningHandle.HandleReadTableCost(do.InfoSchema())
}
case <-do.exit:
return
Expand Down
2 changes: 1 addition & 1 deletion pkg/executor/importer/importer_testkit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ func TestProcessChunkWith(t *testing.T) {
require.NoError(t, err)
checksumMap := checksum.GetInnerChecksums()
require.Len(t, checksumMap, 1)
require.Equal(t, verify.MakeKVChecksum(111, 3, 14585065391351463171), *checksumMap[verify.DataKVGroupID])
require.Equal(t, verify.MakeKVChecksum(111, 3, 17951921359894607752), *checksumMap[verify.DataKVGroupID])
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this is changed

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as following

})
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/executor/infoschema_cluster_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ func TestTableStorageStats(t *testing.T) {
"test 2",
))
rows := tk.MustQuery("select TABLE_NAME from information_schema.TABLE_STORAGE_STATS where TABLE_SCHEMA = 'mysql';").Rows()
result := 58
result := 59
require.Len(t, rows, result)

// More tests about the privileges.
Expand Down
12 changes: 6 additions & 6 deletions pkg/expression/integration_test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1752,18 +1752,18 @@ func TestTiDBEncodeKey(t *testing.T) {
err := tk.QueryToErr("select tidb_encode_record_key('test', 't1', 0);")
require.ErrorContains(t, err, "doesn't exist")
tk.MustQuery("select tidb_encode_record_key('test', 't', 1);").
Check(testkit.Rows("74800000000000006e5f728000000000000001"))
Check(testkit.Rows("7480000000000000705f728000000000000001"))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why EncodeKeyTest is changed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because I added a new system table.


tk.MustExec("alter table t add index i(b);")
err = tk.QueryToErr("select tidb_encode_index_key('test', 't', 'i1', 1);")
require.ErrorContains(t, err, "index not found")
tk.MustQuery("select tidb_encode_index_key('test', 't', 'i', 1, 1);").
Check(testkit.Rows("74800000000000006e5f698000000000000001038000000000000001038000000000000001"))
Check(testkit.Rows("7480000000000000705f698000000000000001038000000000000001038000000000000001"))

tk.MustExec("create table t1 (a int primary key, b int) partition by hash(a) partitions 4;")
tk.MustExec("insert into t1 values (1, 1);")
tk.MustQuery("select tidb_encode_record_key('test', 't1(p1)', 1);").Check(testkit.Rows("7480000000000000735f728000000000000001"))
rs := tk.MustQuery("select tidb_mvcc_info('74800000000000006f5f728000000000000001');")
tk.MustQuery("select tidb_encode_record_key('test', 't1(p1)', 1);").Check(testkit.Rows("7480000000000000755f728000000000000001"))
rs := tk.MustQuery("select tidb_mvcc_info('74800000000000007f5f728000000000000001');")
mvccInfo := rs.Rows()[0][0].(string)
require.NotEqual(t, mvccInfo, `{"info":{}}`)

Expand All @@ -1772,14 +1772,14 @@ func TestTiDBEncodeKey(t *testing.T) {
tk2 := testkit.NewTestKit(t, store)
err = tk2.Session().Auth(&auth.UserIdentity{Username: "alice", Hostname: "localhost"}, nil, nil, nil)
require.NoError(t, err)
err = tk2.QueryToErr("select tidb_mvcc_info('74800000000000006f5f728000000000000001');")
err = tk2.QueryToErr("select tidb_mvcc_info('74800000000000007f5f728000000000000001');")
require.ErrorContains(t, err, "Access denied")
err = tk2.QueryToErr("select tidb_encode_record_key('test', 't1(p1)', 1);")
require.ErrorContains(t, err, "SELECT command denied")
err = tk2.QueryToErr("select tidb_encode_index_key('test', 't', 'i1', 1);")
require.ErrorContains(t, err, "SELECT command denied")
tk.MustExec("grant select on test.t1 to 'alice'@'%';")
tk2.MustQuery("select tidb_encode_record_key('test', 't1(p1)', 1);").Check(testkit.Rows("7480000000000000735f728000000000000001"))
tk2.MustQuery("select tidb_encode_record_key('test', 't1(p1)', 1);").Check(testkit.Rows("7480000000000000755f728000000000000001"))
}

func TestIssue9710(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions pkg/kv/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ const (
InternalTxnStats = "stats"
// InternalTxnBindInfo is the type of bind info txn.
InternalTxnBindInfo = InternalTxnOthers
// InternalTxnWorkloadLearning is the type of workload-based learning txn.
InternalTxnWorkloadLearning = "WorkloadLearning"
// InternalTxnSysVar is the type of sys var txn.
InternalTxnSysVar = InternalTxnOthers
// InternalTxnAdmin is the type of admin operations.
Expand Down
20 changes: 17 additions & 3 deletions pkg/session/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ const (
extra json, -- for the cloud env to save more info like RU, cost_saving, ...
index idx_create(created_at),
index idx_update(updated_at),
unique index idx(schema_name, table_name, index_columns))`
unique index idx(schema_name, table_name, index_columns));`

// CreateKernelOptionsTable is a table to store kernel options for tidb.
CreateKernelOptionsTable = `CREATE TABLE IF NOT EXISTS mysql.tidb_kernel_options (
Expand All @@ -769,7 +769,18 @@ const (
updated_at datetime,
status varchar(128),
description text,
primary key(module, name))`
primary key(module, name));`

// CreateWorkloadValuesTable is a table to store workload-based learning values for tidb.
CreateWorkloadValuesTable = `CREATE TABLE IF NOT EXISTS mysql.workload_values (
Copy link
Copy Markdown

@benmeadowcroft benmeadowcroft Feb 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a standard naming convention that we use for these new tables that are TiDB extensions? I see many other cases of these, non-MySQL, tables where we prefix the table name with tidb_, pd_, tikv_ or similar across the mysql and performance_schema schemas.

I am concerned that this kind of unprefixed table name could lead to future conflicts and compatibility issues if MySQL introduces this table name in the future.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Has been changed to tidb_workload_values

id bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
version bigint(20) NOT NULL,
category varchar(64) NOT NULL,
type varchar(64) NOT NULL,
table_id bigint(20) NOT NULL,
value json NOT NULL,
index idx_version_category_type (version, category, type),
index idx_table_id (table_id));`
)

// CreateTimers is a table to store all timers for tidb
Expand Down Expand Up @@ -1247,6 +1258,7 @@ const (

// version 242
// insert `cluster_id` into the `mysql.tidb` table.
// Add workload-based learning system tables
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

v242 is introduced by #59511 recently, better to add a temporary comment which will be reduced when WBL is ready to go. In case of the author will curious about the change

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid unnecessary version growth, I will share this version before releasing it.

version242 = 242
)

Expand Down Expand Up @@ -3345,8 +3357,8 @@ func upgradeToVer242(s sessiontypes.Session, ver int64) {
if ver >= version242 {
return
}

writeClusterID(s)
mustExecute(s, CreateWorkloadValuesTable)
}

// initGlobalVariableIfNotExists initialize a global variable with specific val if it does not exist.
Expand Down Expand Up @@ -3503,6 +3515,8 @@ func doDDLWorks(s sessiontypes.Session) {
mustExecute(s, CreateIndexAdvisorTable)
// create mysql.tidb_kernel_options
mustExecute(s, CreateKernelOptionsTable)
// create mysql.workload_values
mustExecute(s, CreateWorkloadValuesTable)
}

// doBootstrapSQLFile executes SQL commands in a file as the last stage of bootstrap.
Expand Down
2 changes: 2 additions & 0 deletions pkg/session/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ func TestBootstrapWithError(t *testing.T) {

// Check tidb_ttl_table_status table
MustExec(t, se, "SELECT * from mysql.tidb_ttl_table_status")
// Check mysql.workload_values table
MustExec(t, se, "SELECT * from mysql.workload_values")
}

func TestDDLTableCreateBackfillTable(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,14 @@ func TestValidateAndPrepareForDynamicPartitionedTable(t *testing.T) {
tk.MustExec("create table example_table (a int, b int, index idx(a)) partition by range (a) (partition p0 values less than (2), partition p1 values less than (4))")
tableInfo, err := dom.InfoSchema().TableByName(context.Background(), ast.NewCIStr("example_schema"), ast.NewCIStr("example_table"))
require.NoError(t, err)
partitionInfo := tableInfo.Meta().GetPartitionInfo()
require.NotNil(t, partitionInfo)
job := &priorityqueue.DynamicPartitionedTableAnalysisJob{
SchemaName: "example_schema",
GlobalTableID: tableInfo.Meta().ID,
PartitionIDs: map[int64]struct{}{
113: {},
114: {},
partitionInfo.Definitions[0].ID: {},
partitionInfo.Definitions[1].ID: {},
},
Weight: 2,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func testConcurrentlyInitStats(t *testing.T) {
require.False(t, col.IsAllEvicted())
}
}
require.Equal(t, int64(126), handle.GetMaxTidRecordForTest())
require.Equal(t, int64(128), handle.GetMaxTidRecordForTest())
}

func TestDropTableBeforeConcurrentlyInitStats(t *testing.T) {
Expand Down
11 changes: 0 additions & 11 deletions pkg/workloadbasedlearning/BUILD.bazel

This file was deleted.

91 changes: 0 additions & 91 deletions pkg/workloadbasedlearning/handle.go

This file was deleted.

35 changes: 35 additions & 0 deletions pkg/workloadlearning/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "workloadlearning",
srcs = [
"handle.go",
"metrics.go",
],
importpath = "github.com/pingcap/tidb/pkg/workloadlearning",
visibility = ["//visibility:public"],
deps = [
"//pkg/infoschema",
"//pkg/kv",
"//pkg/parser/ast",
"//pkg/sessionctx",
"//pkg/sessiontxn",
"//pkg/util",
"//pkg/util/logutil",
"//pkg/util/sqlescape",
"@org_uber_go_zap//:zap",
],
)

go_test(
name = "workloadlearning_test",
timeout = "short",
srcs = ["handle_test.go"],
flaky = True,
deps = [
":workloadlearning",
"//pkg/parser/ast",
"//pkg/testkit",
"@com_github_stretchr_testify//require",
],
)
Loading