Skip to content

Commit

Permalink
tests, executor, br: fix time_zone related bugs in UT (pingcap#49588)
Browse files Browse the repository at this point in the history
  • Loading branch information
Defined2014 authored Dec 20, 2023
1 parent 97acf71 commit a09facc
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 23 deletions.
1 change: 0 additions & 1 deletion .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ build:release --workspace_status_command=./build/print-workspace-status.sh --sta
build:release --config=ci
build:race --config=ci
build:race --@io_bazel_rules_go//go/config:race --test_env=GORACE=halt_on_error=1 --test_sharding_strategy=disabled
test --test_env=TZ=Asia/Shanghai
test --test_output=errors --test_summary=detailed
test:ci --color=yes
test:ci --verbose_failures --test_verbose_timeout_warnings
Expand Down
17 changes: 17 additions & 0 deletions br/pkg/task/show/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ func TestShowViaSQL(t *testing.T) {
err := os.WriteFile(metaPath, FullMeta, 0o444)
req.NoError(err)

tk.MustExec("set @@time_zone='+08:00'")
res := tk.MustQuery(fmt.Sprintf("SHOW BACKUP METADATA FROM 'local://%s'", tempBackup))
fmt.Printf("%#v", res.Sort().Rows())
res.Sort().Check([][]interface{}{
Expand All @@ -191,4 +192,20 @@ func TestShowViaSQL(t *testing.T) {
{"tpcc", "stock", "0", "0", "<nil>", "2023-04-10 11:18:21"},
{"tpcc", "warehouse", "0", "0", "<nil>", "2023-04-10 11:18:21"},
})

// Test result in different time_zone
tk.MustExec("set @@time_zone='-08:00'")
res = tk.MustQuery(fmt.Sprintf("SHOW BACKUP METADATA FROM 'local://%s'", tempBackup))
fmt.Printf("%#v", res.Sort().Rows())
res.Sort().Check([][]interface{}{
{"tpcc", "customer", "0", "0", "<nil>", "2023-04-09 19:18:21"},
{"tpcc", "district", "0", "0", "<nil>", "2023-04-09 19:18:21"},
{"tpcc", "history", "0", "0", "<nil>", "2023-04-09 19:18:21"},
{"tpcc", "item", "0", "0", "<nil>", "2023-04-09 19:18:21"},
{"tpcc", "new_order", "0", "0", "<nil>", "2023-04-09 19:18:21"},
{"tpcc", "order_line", "0", "0", "<nil>", "2023-04-09 19:18:21"},
{"tpcc", "orders", "0", "0", "<nil>", "2023-04-09 19:18:21"},
{"tpcc", "stock", "0", "0", "<nil>", "2023-04-09 19:18:21"},
{"tpcc", "warehouse", "0", "0", "<nil>", "2023-04-09 19:18:21"},
})
}
1 change: 1 addition & 0 deletions pkg/ddl/column_type_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ func TestChangingColOriginDefaultValueAfterAddColAndCastSucc(t *testing.T) {

tk1 := testkit.NewTestKit(t, store)
tk1.MustExec("use test")
tk1.MustExec("set time_zone = 'UTC'")

tk.MustExec("set time_zone = 'UTC'")
tk.MustExec("drop table if exists t")
Expand Down
4 changes: 2 additions & 2 deletions pkg/executor/brie.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,11 +510,11 @@ func (e *showMetaExec) Next(ctx context.Context, req *chunk.Chunk) error {
req.AppendInt64(2, int64(table.KVCount))
req.AppendInt64(3, int64(table.KVSize))
if res.StartVersion > 0 {
req.AppendTime(4, types.NewTime(types.FromGoTime(startTime), mysql.TypeDatetime, 0))
req.AppendTime(4, types.NewTime(types.FromGoTime(startTime.In(e.Ctx().GetSessionVars().Location())), mysql.TypeDatetime, 0))
} else {
req.AppendNull(4)
}
req.AppendTime(5, types.NewTime(types.FromGoTime(endTime), mysql.TypeDatetime, 0))
req.AppendTime(5, types.NewTime(types.FromGoTime(endTime.In(e.Ctx().GetSessionVars().Location())), mysql.TypeDatetime, 0))
}
return nil
}
Expand Down
10 changes: 3 additions & 7 deletions pkg/executor/slow_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ func (e *slowQueryRetriever) initialize(ctx context.Context, sctx sessionctx.Con
if e.extractor != nil {
e.checker.enableTimeCheck = e.extractor.Enable
for _, tr := range e.extractor.TimeRanges {
startTime := types.NewTime(types.FromGoTime(tr.StartTime), mysql.TypeDatetime, types.MaxFsp)
endTime := types.NewTime(types.FromGoTime(tr.EndTime), mysql.TypeDatetime, types.MaxFsp)
startTime := types.NewTime(types.FromGoTime(tr.StartTime.In(sctx.GetSessionVars().Location())), mysql.TypeDatetime, types.MaxFsp)
endTime := types.NewTime(types.FromGoTime(tr.EndTime.In(sctx.GetSessionVars().Location())), mysql.TypeDatetime, types.MaxFsp)
timeRange := &timeRange{
startTime: startTime,
endTime: endTime,
Expand Down Expand Up @@ -689,17 +689,13 @@ func getColumnValueFactoryByName(colName string, columnIdx int) (slowQueryColumn
if err != nil {
return false, err
}
timeValue := types.NewTime(types.FromGoTime(t), mysql.TypeTimestamp, types.MaxFsp)
timeValue := types.NewTime(types.FromGoTime(t.In(tz)), mysql.TypeTimestamp, types.MaxFsp)
if checker != nil {
valid := checker.isTimeValid(timeValue)
if !valid {
return valid, nil
}
}
if t.Location() != tz {
t = t.In(tz)
timeValue = types.NewTime(types.FromGoTime(t), mysql.TypeTimestamp, types.MaxFsp)
}
row[columnIdx] = types.NewTimeDatum(timeValue)
return true, nil
}, nil
Expand Down
1 change: 1 addition & 0 deletions pkg/executor/slow_query_sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ SELECT original_sql, bind_sql, default_db, status, create_time, update_time, cha
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)

tk.MustExec("set @@time_zone='+08:00'")
tk.MustExec(fmt.Sprintf("set @@tidb_slow_query_file='%v'", f.Name()))
tk.MustQuery("select count(*) from `information_schema`.`slow_query` where time > '2020-10-16 20:08:13' and time < '2020-10-16 21:08:13'").Check(testkit.Rows("1"))
tk.MustQuery("select count(*) from `information_schema`.`slow_query` where time > '2019-10-13 20:08:13' and time < '2020-10-16 21:08:13'").Check(testkit.Rows("2"))
Expand Down
4 changes: 2 additions & 2 deletions pkg/executor/stale_txn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestExactStalenessTransaction(t *testing.T) {
preSQL: "begin",
sql: `START TRANSACTION READ ONLY AS OF TIMESTAMP '2020-09-06 00:00:00';`,
IsStaleness: true,
expectPhysicalTS: 1599321600000,
expectPhysicalTS: time.Date(2020, 9, 6, 0, 0, 0, 0, time.Local).UnixMilli(),
zone: "sh",
},
{
Expand All @@ -61,7 +61,7 @@ func TestExactStalenessTransaction(t *testing.T) {
preSQL: "begin",
sql: `START TRANSACTION READ ONLY AS OF TIMESTAMP tidb_bounded_staleness('2015-09-21 00:07:01', NOW());`,
IsStaleness: true,
expectPhysicalTS: 1442765221000,
expectPhysicalTS: time.Date(2015, 9, 21, 0, 7, 1, 0, time.Local).UnixMilli(),
zone: "bj",
},
{
Expand Down
2 changes: 1 addition & 1 deletion pkg/expression/integration_test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestGetLock(t *testing.T) {
// Increase pessimistic txn max retry count to make test more stable.
originCfg := config.GetGlobalConfig()
newCfg := *originCfg
newCfg.PessimisticTxn.MaxRetryCount = 2048
newCfg.PessimisticTxn.MaxRetryCount = 10240
config.StoreGlobalConfig(&newCfg)
defer func() {
config.StoreGlobalConfig(originCfg)
Expand Down
22 changes: 12 additions & 10 deletions pkg/util/stmtsummary/v2/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package stmtsummary
import (
"bufio"
"context"
"fmt"
"os"
"testing"
"time"
Expand Down Expand Up @@ -59,7 +60,7 @@ func TestStmtFile(t *testing.T) {
require.NoError(t, f.file.Close())
}()
require.Equal(t, int64(1), f.begin)
require.Equal(t, int64(1672129280), f.end) // 2022-12-27T16-21-20.245 == 1672129280
require.Equal(t, time.Date(2022, 12, 27, 16, 21, 20, 245000000, time.Local).Unix(), f.end)

// Check if seek 0.
firstLine, err := util.ReadLine(bufio.NewReader(f.file), maxLineSize)
Expand Down Expand Up @@ -89,10 +90,11 @@ func TestStmtFileInvalidLine(t *testing.T) {
require.NoError(t, f.file.Close())
}()
require.Equal(t, int64(1), f.begin)
require.Equal(t, int64(1672129280), f.end) // 2022-12-27T16-21-20.245 == 1672129280
require.Equal(t, time.Date(2022, 12, 27, 16, 21, 20, 245000000, time.Local).Unix(), f.end)
}

func TestStmtFiles(t *testing.T) {
t1 := time.Date(2022, 12, 27, 16, 21, 20, 245000000, time.Local)
filename1 := "tidb-statements-2022-12-27T16-21-20.245.log"
filename2 := "tidb-statements.log"

Expand All @@ -101,9 +103,9 @@ func TestStmtFiles(t *testing.T) {
defer func() {
require.NoError(t, os.Remove(filename1))
}()
_, err = file.WriteString("{\"begin\":1672128520,\"end\":1672128530}\n")
_, err = file.WriteString(fmt.Sprintf("{\"begin\":%d,\"end\":%d}\n", t1.Unix()-760, t1.Unix()-750))
require.NoError(t, err)
_, err = file.WriteString("{\"begin\":1672129270,\"end\":1672129280}\n")
_, err = file.WriteString(fmt.Sprintf("{\"begin\":%d,\"end\":%d}\n", t1.Unix()-10, t1.Unix()))
require.NoError(t, err)
require.NoError(t, file.Close())

Expand All @@ -112,9 +114,9 @@ func TestStmtFiles(t *testing.T) {
defer func() {
require.NoError(t, os.Remove(filename2))
}()
_, err = file.WriteString("{\"begin\":1672129270,\"end\":1672129280}\n")
_, err = file.WriteString(fmt.Sprintf("{\"begin\":%d,\"end\":%d}\n", t1.Unix()-10, t1.Unix()))
require.NoError(t, err)
_, err = file.WriteString("{\"begin\":1672129380,\"end\":1672129390}\n")
_, err = file.WriteString(fmt.Sprintf("{\"begin\":%d,\"end\":%d}\n", t1.Unix()+100, t1.Unix()+110))
require.NoError(t, err)
require.NoError(t, file.Close())

Expand All @@ -129,7 +131,7 @@ func TestStmtFiles(t *testing.T) {

func() {
files, err := newStmtFiles(context.Background(), []*StmtTimeRange{
{Begin: 1672129270, End: 1672129271},
{Begin: t1.Unix() - 10, End: t1.Unix() - 9},
})
require.NoError(t, err)
defer files.close()
Expand All @@ -140,7 +142,7 @@ func TestStmtFiles(t *testing.T) {

func() {
files, err := newStmtFiles(context.Background(), []*StmtTimeRange{
{Begin: 0, End: 1672129270},
{Begin: 0, End: t1.Unix() - 10},
})
require.NoError(t, err)
defer files.close()
Expand All @@ -151,7 +153,7 @@ func TestStmtFiles(t *testing.T) {

func() {
files, err := newStmtFiles(context.Background(), []*StmtTimeRange{
{Begin: 0, End: 1672129269},
{Begin: 0, End: t1.Unix() - 11},
})
require.NoError(t, err)
defer files.close()
Expand All @@ -170,7 +172,7 @@ func TestStmtFiles(t *testing.T) {

func() {
files, err := newStmtFiles(context.Background(), []*StmtTimeRange{
{Begin: 1672129281, End: 0},
{Begin: t1.Unix() + 1, End: 0},
})
require.NoError(t, err)
defer files.close()
Expand Down

0 comments on commit a09facc

Please sign in to comment.