Skip to content
This repository was archived by the owner on Dec 9, 2024. It is now read-only.

Commit b9b275e

Browse files
committed
revise code
Signed-off-by: yisaer <disxiaofei@163.com>
1 parent ccb97e0 commit b9b275e

File tree

5 files changed

+136
-648
lines changed

5 files changed

+136
-648
lines changed

testcase/stale-read/bench.go

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ import (
44
"bytes"
55
"database/sql"
66
"fmt"
7+
"math/rand"
8+
"time"
9+
710
"github.com/pingcap/errors"
11+
"github.com/pingcap/log"
812
"github.com/tiancaiamao/sysbench"
9-
"math/rand"
13+
"go.uber.org/zap"
1014
)
1115

1216
const createTableTemplate = `create table if not exists sbtest%d (
@@ -20,12 +24,21 @@ const splitTableTemplate = `SPLIT TABLE sbtest%d BETWEEN (0) AND (1000000000) RE
2024
type SysbenchCase struct {
2125
insertCount int
2226
rowsEachInsert int
23-
dbHost string
2427
}
2528

2629
func (c *SysbenchCase) CreateTable(db *sql.DB) error {
27-
db.Exec(fmt.Sprintf(createTableTemplate, 0))
28-
db.Exec(fmt.Sprintf(splitTableTemplate, 0))
30+
if err := c.DropTable(db); err != nil {
31+
log.Error("fail to drop table", zap.Error(err))
32+
return err
33+
}
34+
if _, err := db.Exec(fmt.Sprintf(createTableTemplate, 0)); err != nil {
35+
log.Error("fail to create table", zap.Error(err))
36+
return err
37+
}
38+
if _, err := db.Exec(fmt.Sprintf(splitTableTemplate, 0)); err != nil {
39+
log.Error("fail to split table", zap.Error(err))
40+
return err
41+
}
2942
return nil
3043
}
3144

@@ -46,33 +59,39 @@ func (c *SysbenchCase) InsertData(worker *sysbench.Worker, db *sql.DB) error {
4659

4760
_, err := db.Exec(buf.String())
4861
if err != nil {
62+
log.Info("Insert data error", zap.Error(err))
4963
return errors.WithStack(err)
5064
}
5165
}
66+
log.Info("insert data finish")
5267
return nil
5368
}
5469

70+
// TODO: fulfill workload in future
5571
func (c *SysbenchCase) Execute(worker *sysbench.Worker, db *sql.DB) error {
56-
for i := 0; i < 100; i++ {
57-
err := c.executeSET(db)
58-
if err != nil {
59-
return err
60-
}
61-
err = c.executeSTART(db)
62-
if err != nil {
63-
return err
64-
}
65-
err = c.executeSelect(db)
66-
if err != nil {
67-
return err
68-
}
72+
log.Info("worker start execute")
73+
err := c.executeSET(db)
74+
if err != nil {
75+
log.Info("execute set transaction read only as of testcase fail", zap.Error(err))
76+
return err
77+
}
78+
err = c.executeSelect(db)
79+
if err != nil {
80+
log.Info("execute select as of timestamp fail", zap.Error(err))
81+
return err
6982
}
83+
log.Info("worker start success")
7084
return nil
7185
}
7286

7387
func (c *SysbenchCase) executeSET(db *sql.DB) error {
7488
num := c.insertCount * c.rowsEachInsert
75-
_, err := db.Exec("SET TRANSACTION READ ONLY AS OF TIMESTAMP tidb_bounded_staleness(DATE_SUB(NOW(), INTERVAL 10 SECOND)")
89+
now := time.Now()
90+
previous := now.Add(-3 * time.Second)
91+
nowStr := now.Format("2006-1-2 15:04:05.000")
92+
previousStr := previous.Format("2006-1-2 15:04:05.000")
93+
setSQL := fmt.Sprintf(`SET TRANSACTION READ ONLY as of timestamp tidb_bounded_staleness('%v', '%v')`, previousStr, nowStr)
94+
_, err := db.Exec(setSQL)
7695
if err != nil {
7796
return err
7897
}
@@ -84,9 +103,15 @@ func (c *SysbenchCase) executeSET(db *sql.DB) error {
84103
return nil
85104
}
86105

106+
// TODO: don't know why this case failed
87107
func (c *SysbenchCase) executeSTART(db *sql.DB) error {
88108
num := c.insertCount * c.rowsEachInsert
89-
_, err := db.Exec("START TRANSACTION READ ONLY AS OF TIMESTAMP tidb_bounded_staleness(DATE_SUB(NOW(), INTERVAL 10 SECOND)")
109+
now := time.Now()
110+
previous := now.Add(-3 * time.Second)
111+
nowStr := now.Format("2006-1-2 15:04:05.000")
112+
previousStr := previous.Format("2006-1-2 15:04:05.000")
113+
startSQL := fmt.Sprintf(`START TRANSACTION READ ONLY AS OF TIMESTAMP tidb_bounded_staleness('%v', '%v')`, previousStr, nowStr)
114+
_, err := db.Exec(startSQL)
90115
if err != nil {
91116
return err
92117
}
@@ -104,7 +129,12 @@ func (c *SysbenchCase) executeSTART(db *sql.DB) error {
104129

105130
func (c *SysbenchCase) executeSelect(db *sql.DB) error {
106131
num := c.insertCount * c.rowsEachInsert
107-
rows, err := db.Query("select id, k, c, pad from sbtest0 as of timestamp tidb_bounded_staleness(DATE_SUB(NOW(), INTERVAL 10 SECOND) where k in (?, ?, ?)", rand.Intn(num), rand.Intn(num), rand.Intn(num))
132+
now := time.Now()
133+
previous := now.Add(-3 * time.Second)
134+
nowStr := now.Format("2006-1-2 15:04:05.000")
135+
previousStr := previous.Format("2006-1-2 15:04:05.000")
136+
selectSQL := fmt.Sprintf("select id, k, c, pad from sbtest0 as of timestamp tidb_bounded_staleness('%v','%v') where k in (%v, %v, %v)", previousStr, nowStr, rand.Intn(num), rand.Intn(num), rand.Intn(num))
137+
rows, err := db.Query(selectSQL)
108138
defer rows.Close()
109139
if err != nil {
110140
return errors.WithStack(err)

testcase/stale-read/cmd/main.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,23 @@ import (
1818

1919
func main() {
2020
flag.Parse()
21+
fixture.Context.ClusterName = "stale-read"
22+
fixture.Context.Namespace = "gaosong"
2123
cfg := control.Config{
2224
Mode: control.ModeStandard,
2325
ClientCount: 1,
2426
RunTime: fixture.Context.RunTime,
2527
}
2628
c := fixture.Context
29+
c.TiDBClusterConfig.PDReplicas = 1
2730
c.TiDBClusterConfig.TiDBReplicas = 1
28-
c.TiDBClusterConfig.TiKVReplicas = 5
31+
c.TiDBClusterConfig.TiKVReplicas = 1
32+
c.TiDBClusterConfig.LogStorageClassName = "shared-sas-disks"
33+
c.TiDBClusterConfig.PDStorageClassName = "shared-nvme-disks"
34+
c.TiDBClusterConfig.TiKVStorageClassName = "nvme-disks"
35+
c.TiDBClusterConfig.PDImage = "hub.pingcap.net/gaosong/pd:newly-master"
36+
c.TiDBClusterConfig.TiDBImage = "hub.pingcap.net/gaosong/tidb:newly-master"
37+
c.TiDBClusterConfig.TiKVImage = "hub.pingcap.net/gaosong/tikv:newly-master"
2938
suit := util.Suit{
3039
Config: &cfg,
3140
Provider: cluster.NewDefaultClusterProvider(),

testcase/stale-read/go.mod

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ go 1.16
55
require (
66
github.com/go-sql-driver/mysql v1.5.0
77
github.com/pingcap/errors v0.11.5-0.20201126102027-b0a155152ca3
8-
github.com/pingcap/log v0.0.0-20201112100606-8f1e84a3abc8 // indirect
8+
github.com/pingcap/kvproto v0.0.0-20210219064844-c1844a4775d6 // indirect
9+
github.com/pingcap/log v0.0.0-20201112100606-8f1e84a3abc8
910
github.com/pingcap/tipocket v1.0.0
1011
github.com/pingcap/tipocket/logsearch v0.0.0-20210602095541-45d321986652
1112
github.com/prometheus/client_golang v1.5.0
13+
github.com/prometheus/common v0.9.1
14+
github.com/stretchr/testify v1.6.0 // indirect
1215
github.com/tiancaiamao/sysbench v0.0.0-20200214034607-ee9d97eabd23
13-
github.com/tikv/pd v1.1.0-beta.0.20210225143804-1f200cbcd647 // indirect
14-
go.uber.org/zap v1.15.0 // indirect
16+
go.uber.org/zap v1.15.0
1517
)
1618

1719
replace google.golang.org/grpc => google.golang.org/grpc v1.26.0

0 commit comments

Comments
 (0)