Skip to content

Commit

Permalink
test: add paging explain analyze and total_process_keys test (pingcap…
Browse files Browse the repository at this point in the history
  • Loading branch information
wshwsh12 authored Jul 14, 2022
1 parent 9a2ed52 commit be50ebc
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 5 deletions.
1 change: 1 addition & 0 deletions tests/realtikvtest/sessiontest/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ go_test(
"session_fail_test.go",
"session_test.go",
"temporary_table_test.go",
"paging_test.go"
],
flaky = True,
shard_count = 50,
Expand Down
105 changes: 105 additions & 0 deletions tests/realtikvtest/sessiontest/paging_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Copyright 2022 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package sessiontest

import (
"fmt"
"math/rand"
"strconv"
"strings"
"testing"

"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/testkit"
"github.com/pingcap/tidb/tests/realtikvtest"
"github.com/stretchr/testify/require"
)

func TestPagingActRowsAndProcessKeys(t *testing.T) {
// Close copr-cache
defer config.RestoreFunc()
config.UpdateGlobal(func(conf *config.Config) {
conf.TiKVClient.CoprCache.CapacityMB = 0
})

store, clean := realtikvtest.CreateMockStoreAndSetup(t)
defer clean()
session := testkit.NewTestKit(t, store)
session.MustExec("use test;")
session.MustExec("drop table if exists t;")
session.MustExec(`set @@tidb_wait_split_region_finish=1`)
session.MustExec("create table t(a int,b int,c int,index idx(a,b), primary key(a));")
// prepare data, insert 10w record
// [0, 999999]
for i := 0; i < 100; i++ {
sql := "insert into t value"
for j := 0; j < 1000; j++ {
if j != 0 {
sql += ","
}
sql += "(" + strconv.Itoa(i*1000+j) + "," + strconv.Itoa(i*1000+j) + "," + strconv.Itoa(i*1000+j) + ")"
}
session.MustExec(sql)
}

testcase := []struct {
regionNumLowerBound int32
regionNumUpperBound int32
}{
{10, 100}, // [10, 99]
{100, 500}, // [100,499]
{500, 1000}, // [500,999]
{1000, 1001}, // 1000
}

openOrClosePaging := []string{
"set tidb_enable_paging = on;",
"set tidb_enable_paging = off;",
}

sqls := []string{
"desc analyze select a,b from t;", // TableScan
"desc analyze select /*+ use_index(t,idx) */ a,b from t;", // IndexScan
"desc analyze select /*+ use_index(t,idx) */ c from t;", // IndexLookUp
}

checkScanOperator := func(strs []interface{}) {
require.Equal(t, strs[2].(string), "100000")
if *realtikvtest.WithRealTiKV { // Unistore don't collect process_keys now
require.True(t, strings.Contains(strs[5].(string), "total_process_keys: 100000"), strs[5])
}
}

checkResult := func(result [][]interface{}) {
for _, strs := range result {
if strings.Contains(strs[0].(string), "Scan") {
checkScanOperator(strs)
}
}
}

for _, tc := range testcase {
regionNum := rand.Int31n(tc.regionNumUpperBound-tc.regionNumLowerBound) + tc.regionNumLowerBound
_ = session.MustQuery(fmt.Sprintf("split table t between (0) and (1000000) regions %v;", regionNum))
_ = session.MustQuery(fmt.Sprintf("split table t index idx between (0) and (1000000) regions %v;", regionNum))
for _, sql := range sqls {
for _, pagingSQL := range openOrClosePaging {
session.MustExec(pagingSQL)
rows := session.MustQuery(sql)
checkResult(rows.Rows())
}
}
}
}
11 changes: 6 additions & 5 deletions tests/realtikvtest/sessiontest/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1373,7 +1373,8 @@ func TestCoprocessorOOMAction(t *testing.T) {
store, clean := realtikvtest.CreateMockStoreAndSetup(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create database testoom")
tk.MustExec("use testoom")
tk.MustExec(`set @@tidb_wait_split_region_finish=1`)
// create table for non keep-order case
tk.MustExec("drop table if exists t5")
Expand Down Expand Up @@ -1414,7 +1415,7 @@ func TestCoprocessorOOMAction(t *testing.T) {
quota := 5*copr.MockResponseSizeForTest - 100
defer tk.MustExec("SET GLOBAL tidb_mem_oom_action = DEFAULT")
tk.MustExec("SET GLOBAL tidb_mem_oom_action='CANCEL'")
tk.MustExec("use test")
tk.MustExec("use testoom")
tk.MustExec("set @@tidb_distsql_scan_concurrency = 10")
tk.MustExec(fmt.Sprintf("set @@tidb_mem_quota_query=%v;", quota))
var expect []string
Expand All @@ -1429,7 +1430,7 @@ func TestCoprocessorOOMAction(t *testing.T) {
disableOOM := func(tk *testkit.TestKit, name, sql string) {
t.Logf("disable OOM, testcase: %v", name)
quota := 5*copr.MockResponseSizeForTest - 100
tk.MustExec("use test")
tk.MustExec("use testoom")
tk.MustExec("set @@tidb_distsql_scan_concurrency = 10")
tk.MustExec(fmt.Sprintf("set @@tidb_mem_quota_query=%v;", quota))
err := tk.QueryToErr(sql)
Expand All @@ -1451,7 +1452,7 @@ func TestCoprocessorOOMAction(t *testing.T) {
se.Close()
}
globaltk := testkit.NewTestKit(t, store)
globaltk.MustExec("use test")
globaltk.MustExec("use testoom")
globaltk.MustExec("set global tidb_enable_rate_limit_action= 0")
for _, testcase := range testcases {
se, err := session.CreateSession4Test(store)
Expand All @@ -1476,7 +1477,7 @@ func TestCoprocessorOOMAction(t *testing.T) {
se, err := session.CreateSession4Test(store)
require.NoError(t, err)
tk.SetSession(se)
tk.MustExec("use test")
tk.MustExec("use testoom")
tk.MustExec("set tidb_distsql_scan_concurrency = 1")
tk.MustExec("set @@tidb_mem_quota_query=1;")
err = tk.QueryToErr(testcase.sql)
Expand Down

0 comments on commit be50ebc

Please sign in to comment.