Skip to content

Commit

Permalink
This is an automated cherry-pick of #54137
Browse files Browse the repository at this point in the history
Signed-off-by: ti-chi-bot <[email protected]>
  • Loading branch information
Rustin170506 authored and ti-chi-bot committed Jul 5, 2024
1 parent 660aebc commit f190090
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 0 deletions.
6 changes: 6 additions & 0 deletions tests/realtikvtest/statisticstest/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@ go_test(
flaky = True,
race = "on",
deps = [
<<<<<<< HEAD
"//statistics/handle",
"//testkit",
=======
"//pkg/parser/model",
"//pkg/statistics/asyncload",
"//pkg/testkit",
>>>>>>> ba5c6a9bc6a (statistics: add a testcase for issue 54022 (#54137))
"//tests/realtikvtest",
"@com_github_stretchr_testify//require",
],
Expand Down
125 changes: 125 additions & 0 deletions tests/realtikvtest/statisticstest/statistics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,19 @@
package statisticstest

import (
"context"
"fmt"
"testing"
"time"

<<<<<<< HEAD
"github.com/pingcap/tidb/statistics/handle"
"github.com/pingcap/tidb/testkit"
=======
"github.com/pingcap/tidb/pkg/parser/model"
"github.com/pingcap/tidb/pkg/statistics/asyncload"
"github.com/pingcap/tidb/pkg/testkit"
>>>>>>> ba5c6a9bc6a (statistics: add a testcase for issue 54022 (#54137))
"github.com/pingcap/tidb/tests/realtikvtest"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -186,3 +194,120 @@ func TestNewCollationStatsWithPrefixIndex(t *testing.T) {
"1 3 15 0 2 0",
))
}
<<<<<<< HEAD
=======

func TestBlockMergeFMSketch(t *testing.T) {
store := realtikvtest.CreateMockStoreAndSetup(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("set @@tidb_enable_async_merge_global_stats=OFF;")
defer func() {
tk.MustExec("set @@tidb_enable_async_merge_global_stats=ON;")
}()
checkFMSketch(tk)
}

func TestAsyncMergeFMSketch(t *testing.T) {
store := realtikvtest.CreateMockStoreAndSetup(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("set @@tidb_enable_async_merge_global_stats=ON;")
checkFMSketch(tk)
}

func checkFMSketch(tk *testkit.TestKit) {
tk.MustExec(`CREATE TABLE employees (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,fname VARCHAR(25) NOT NULL,lname VARCHAR(25) NOT NULL,store_id INT NOT NULL,department_id INT NOT NULL
) PARTITION BY RANGE(id) (
PARTITION p0 VALUES LESS THAN (5),
PARTITION p1 VALUES LESS THAN (10),
PARTITION p2 VALUES LESS THAN (15),
PARTITION p3 VALUES LESS THAN MAXVALUE
);`)
tk.MustExec(`INSERT INTO employees(FNAME,LNAME,STORE_ID,DEPARTMENT_ID) VALUES
('Bob', 'Taylor', 3, 2), ('Frank', 'Williams', 1, 2),
('Ellen', 'Johnson', 3, 4), ('Jim', 'Smith', 2, 4),
('Mary', 'Jones', 1, 1), ('Linda', 'Black', 2, 3),
('Ed', 'Jones', 2, 1), ('June', 'Wilson', 3, 1),
('Andy', 'Smith', 1, 3), ('Lou', 'Waters', 2, 4),
('Jill', 'Stone', 1, 4), ('Roger', 'White', 3, 2),
('Howard', 'Andrews', 1, 2), ('Fred', 'Goldberg', 3, 3),
('Barbara', 'Brown', 2, 3), ('Alice', 'Rogers', 2, 2),
('Mark', 'Morgan', 3, 3), ('Karen', 'Cole', 3, 2);`)
tk.MustExec("ANALYZE TABLE employees;")
tk.MustExec("select * from employees;")
tk.MustExec("alter table employees truncate partition p0;")
tk.MustExec("select * from employees;")
tk.MustExec("analyze table employees partition p3;")
tk.MustExec("select * from employees;")
tk.MustQuery(`SHOW STATS_HISTOGRAMS WHERE TABLE_NAME='employees' and partition_name="global" and column_name="id"`).CheckAt([]int{6}, [][]any{
{"14"}})
}

func TestNoNeedIndexStatsLoading(t *testing.T) {
store, dom := realtikvtest.CreateMockStoreAndDomainAndSetup(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test;")
tk.MustExec("drop table if exists t;")
// 1. Create a table and the statsHandle.Update(do.InfoSchema()) will load this table into the stats cache.
tk.MustExec("create table if not exists t(a int, b int, index ia(a));")
// 2. Drop the stats of the stats, it will clean up all system table records for this table.
tk.MustExec("drop stats t;")
// 3. Insert some data and wait for the modify_count and the count is not null in the mysql.stats_meta.
tk.MustExec("insert into t value(1,1), (2,2);")
h := dom.StatsHandle()
require.NoError(t, h.DumpStatsDeltaToKV(true))
require.NoError(t, h.Update(dom.InfoSchema()))
// 4. Try to select some data from this table by ID, it would trigger an async load.
tk.MustExec("set tidb_opt_objective='determinate';")
tk.MustQuery("select * from t where a = 1 and b = 1;").Check(testkit.Rows("1 1"))
table, err := dom.InfoSchema().TableByName(model.NewCIStr("test"), model.NewCIStr("t"))
require.NoError(t, err)
checkTableIDInItems(t, table.Meta().ID)
}

func checkTableIDInItems(t *testing.T, tableID int64) {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
defer cancel()

ticker := time.NewTicker(2 * time.Millisecond)
defer ticker.Stop()

done := make(chan bool)

// First, confirm that the table ID is in the items.
items := asyncload.AsyncLoadHistogramNeededItems.AllItems()
for _, item := range items {
if item.TableID == tableID {
// Then, continuously check until it no longer exists or timeout.
go func() {
for {
select {
case <-ticker.C:
items := asyncload.AsyncLoadHistogramNeededItems.AllItems()
found := false
for _, item := range items {
if item.TableID == tableID {
found = true
}
}
if !found {
done <- true
}
case <-ctx.Done():
return
}
}
}()
break
}
}

select {
case <-done:
t.Log("Table ID has been removed from items")
case <-ctx.Done():
t.Fatal("Timeout: Table ID was not removed from items within the time limit")
}
}
>>>>>>> ba5c6a9bc6a (statistics: add a testcase for issue 54022 (#54137))

0 comments on commit f190090

Please sign in to comment.