Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 2 additions & 0 deletions pkg/util/workloadrepo/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ go_test(
"//pkg/parser/ast",
"//pkg/parser/auth",
"//pkg/parser/mysql",
"//pkg/session",
"//pkg/session/sessionapi",
"//pkg/sessionctx",
"//pkg/sessionctx/variable",
"//pkg/testkit",
Expand Down
44 changes: 30 additions & 14 deletions pkg/util/workloadrepo/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import (
"github.com/pingcap/tidb/pkg/parser/ast"
"github.com/pingcap/tidb/pkg/parser/auth"
"github.com/pingcap/tidb/pkg/parser/mysql"
"github.com/pingcap/tidb/pkg/session"
"github.com/pingcap/tidb/pkg/session/sessionapi"
"github.com/pingcap/tidb/pkg/sessionctx"
"github.com/pingcap/tidb/pkg/sessionctx/variable"
"github.com/pingcap/tidb/pkg/testkit"
Expand Down Expand Up @@ -146,6 +148,10 @@ func waitForTables(ctx context.Context, t *testing.T, wrk *worker, now time.Time
}, time.Minute, time.Second)
}

func anchorPartitionTestTime(now time.Time) time.Time {
return time.Date(now.Year(), now.Month(), now.Day(), 12, 0, 0, 0, now.Location())
}
Comment thread
mjonss marked this conversation as resolved.
Outdated

func TestRaceToCreateTablesWorker(t *testing.T) {
ctx, store, dom, addr := setupDomainAndContext(t)

Expand Down Expand Up @@ -641,11 +647,20 @@ func createTableWithParts(ctx context.Context, t *testing.T, tk *testkit.TestKit
require.True(t, validatePartitionsMatchExpected(ctx, t, sess, tbl, partitions))
}

func createTableWithPartsForSession(ctx context.Context, t *testing.T, se sessionapi.Session, tbl *repositoryTable,
sess sessionctx.Context, partitions []time.Time) {
createSQL, err := buildCreateQuery(ctx, sess, tbl)
require.NoError(t, err)
createSQL += buildPartitionString(partitions)
session.MustExec(t, se, createSQL)
require.True(t, validatePartitionsMatchExpected(ctx, t, sess, tbl, partitions))
}
Comment thread
mjonss marked this conversation as resolved.
Outdated

func validatePartitionCreation(ctx context.Context, now time.Time, t *testing.T,
sess sessionctx.Context, tk *testkit.TestKit, wrk *worker,
sess sessionctx.Context, se sessionapi.Session, wrk *worker,
firstTestFails bool, tableName string, partitions []time.Time, expectedParts []time.Time) {
tbl := getTable(t, tableName, wrk)
createTableWithParts(ctx, t, tk, tbl, sess, partitions)
createTableWithPartsForSession(ctx, t, se, tbl, sess, partitions)

is := sess.GetLatestInfoSchema().(infoschema.InfoSchema)
require.False(t, firstTestFails == checkTableExistsByIS(ctx, is, tbl.destTable, now))
Expand All @@ -662,56 +677,57 @@ func validatePartitionCreation(ctx context.Context, now time.Time, t *testing.T,
func TestCreatePartition(t *testing.T) {
ctx, store, dom, addr := setupDomainAndContext(t)
wrk := setupWorker(ctx, t, addr, dom, "worker", true)
tk := testkit.NewTestKit(t, store)
se, err := session.CreateSession4Test(store)
require.NoError(t, err)
t.Cleanup(se.Close)

tk.MustExec("create database WORKLOAD_SCHEMA")
tk.MustExec("use WORKLOAD_SCHEMA")
session.MustExec(t, se, "create database WORKLOAD_SCHEMA")
session.MustExec(t, se, "use WORKLOAD_SCHEMA")

_sessctx := wrk.getSessionWithRetry()
sess := _sessctx.(sessionctx.Context)

wrk.fillInTableNames()

now := time.Now()
now := anchorPartitionTestTime(time.Now())

/* Tables without partitions are not currently supported. */

// Should create one partition for today and tomorrow on a table with only old partitions before today.
partitions := []time.Time{now.AddDate(0, 0, -1)}
expectedParts := []time.Time{now.AddDate(0, 0, -1), now, now.AddDate(0, 0, 1)}
validatePartitionCreation(ctx, now, t, sess, tk, wrk, true, "PROCESSLIST", partitions, expectedParts)
validatePartitionCreation(ctx, now, t, sess, se, wrk, true, "PROCESSLIST", partitions, expectedParts)

// Should create one partition for tomorrow on a table with only a partition for today.
partitions = []time.Time{now}
expectedParts = []time.Time{now, now.AddDate(0, 0, 1)}
validatePartitionCreation(ctx, now, t, sess, tk, wrk, true, "DATA_LOCK_WAITS", partitions, expectedParts)
validatePartitionCreation(ctx, now, t, sess, se, wrk, true, "DATA_LOCK_WAITS", partitions, expectedParts)

// Should not create any partitions on a table with only a partition for tomorrow.
partitions = []time.Time{now.AddDate(0, 0, 1)}
expectedParts = []time.Time{now.AddDate(0, 0, 1)}
validatePartitionCreation(ctx, now, t, sess, tk, wrk, false, "TIDB_TRX", partitions, expectedParts)
validatePartitionCreation(ctx, now, t, sess, se, wrk, false, "TIDB_TRX", partitions, expectedParts)

// Should not create any partitions on a table with only partitions for both today and tomorrow.
partitions = []time.Time{now, now.AddDate(0, 0, 1)}
expectedParts = []time.Time{now, now.AddDate(0, 0, 1)}
validatePartitionCreation(ctx, now, t, sess, tk, wrk, false, "MEMORY_USAGE", partitions, expectedParts)
validatePartitionCreation(ctx, now, t, sess, se, wrk, false, "MEMORY_USAGE", partitions, expectedParts)

// Should not create any partitions on a table with a partition for the day after tomorrow.
partitions = []time.Time{now.AddDate(0, 0, 2)}
expectedParts = []time.Time{now.AddDate(0, 0, 2)}
validatePartitionCreation(ctx, now, t, sess, tk, wrk, false, "DEADLOCKS", partitions, expectedParts)
validatePartitionCreation(ctx, now, t, sess, se, wrk, false, "DEADLOCKS", partitions, expectedParts)

// Should not fill in missing partitions on a table with a partition for dates beyond tomorrow.
partitions = []time.Time{now, now.AddDate(0, 0, 3)}
expectedParts = []time.Time{now, now.AddDate(0, 0, 3)}
validatePartitionCreation(ctx, now, t, sess, tk, wrk, false, "TIDB_INDEX_USAGE", partitions, expectedParts)
validatePartitionCreation(ctx, now, t, sess, se, wrk, false, "TIDB_INDEX_USAGE", partitions, expectedParts)

// this table should be updated when the repository is enabled
partitions = []time.Time{now}
createTableWithParts(ctx, t, tk, getTable(t, "TIDB_STATEMENTS_STATS", wrk), sess, partitions)
createTableWithPartsForSession(ctx, t, se, getTable(t, "TIDB_STATEMENTS_STATS", wrk), sess, partitions)

// turn on the repository and see if it creates the remaining tables
now = time.Now()
wrk.setRepositoryDest(ctx, "table")
waitForTables(ctx, t, wrk, now)
}
Expand Down
Loading