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

Commit

Permalink
add stale-read test
Browse files Browse the repository at this point in the history
Signed-off-by: yisaer <[email protected]>

add stale-read test

Signed-off-by: yisaer <[email protected]>

add stale-read test

Signed-off-by: yisaer <[email protected]>
  • Loading branch information
Yisaer committed Jun 3, 2021
1 parent 45d3219 commit ccb97e0
Show file tree
Hide file tree
Showing 8 changed files with 2,071 additions and 0 deletions.
5 changes: 5 additions & 0 deletions testcase/stale-read/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM alpine:3.9

RUN apk update && apk add --no-cache wget bash sed
COPY ./bin/stale-read /bin/stale-read
ENTRYPOINT [ "/bin/stale-read" ]
56 changes: 56 additions & 0 deletions testcase/stale-read/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
GOARCH := $(if $(GOARCH),$(GOARCH),amd64)
GO=GO15VENDOREXPERIMENT="1" CGO_ENABLED=0 GOOS=$(GOOS) GOARCH=$(GOARCH) GO111MODULE=on go
GOTEST=GO15VENDOREXPERIMENT="1" CGO_ENABLED=1 GO111MODULE=on go test # go race detector requires cgo
VERSION := $(if $(VERSION),$(VERSION),latest)

PACKAGES := go list ./...| grep -vE 'vendor'

LDFLAGS += -s -w
LDFLAGS += -X "github.com/pingcap/tipocket/pkg/test-infra/fixture.BuildTS=$(shell date -u '+%Y-%m-%d %I:%M:%S')"
LDFLAGS += -X "github.com/pingcap/tipocket/pkg/test-infra/fixture.BuildHash=$(shell git rev-parse HEAD)"

GOBUILD=$(GO) build -ldflags '$(LDFLAGS)'

DOCKER_REGISTRY_PREFIX := $(if $(DOCKER_REGISTRY),$(DOCKER_REGISTRY)/,)

default: tidy fmt lint build

build: stale-read

stale-read:
$(GOBUILD) $(GOMOD) -o bin/stale-read cmd/*.go

fmt: groupimports
go fmt ./...

tidy:
@echo "go mod tidy"
GO111MODULE=on go mod tidy
@git diff --exit-code -- go.mod

lint: revive
@echo "linting"
revive -formatter friendly -config revive.toml $$($(PACKAGES))

revive:
ifeq (,$(shell which revive))
@echo "installing revive"
$(GO) get github.com/mgechev/[email protected]
endif

groupimports: install-goimports
goimports -w -l -local github.com/pingcap/tipocket .

install-goimports:
ifeq (,$(shell which goimports))
@echo "installing goimports"
go get golang.org/x/tools/cmd/goimports
endif

clean:
@rm -rf bin/*

test:
$(GOTEST) ./...

.PHONY: all clean build
133 changes: 133 additions & 0 deletions testcase/stale-read/bench.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package staleread

import (
"bytes"
"database/sql"
"fmt"
"github.com/pingcap/errors"
"github.com/tiancaiamao/sysbench"
"math/rand"
)

const createTableTemplate = `create table if not exists sbtest%d (
id int(11) not null primary key,
k int(11) not null,
c char(120) not null default '',
pad char(255) not null default '')`

const splitTableTemplate = `SPLIT TABLE sbtest%d BETWEEN (0) AND (1000000000) REGIONS 100;`

type SysbenchCase struct {
insertCount int
rowsEachInsert int
dbHost string
}

func (c *SysbenchCase) CreateTable(db *sql.DB) error {
db.Exec(fmt.Sprintf(createTableTemplate, 0))
db.Exec(fmt.Sprintf(splitTableTemplate, 0))
return nil
}

func (c *SysbenchCase) InsertData(worker *sysbench.Worker, db *sql.DB) error {
var buf bytes.Buffer
pkID := worker.ID
for i := 0; i < c.insertCount; i++ {
buf.Reset()
fmt.Fprintf(&buf, "insert into sbtest%d (id, k, c, pad) values ", 0)
for i := 0; i < c.rowsEachInsert; i++ {
pkID = nextPrimaryID(worker.Count, pkID)
dot := ""
if i > 0 {
dot = ", "
}
fmt.Fprintf(&buf, "%s(%d, %d, '%s', '%s')", dot, pkID, rand.Intn(1<<11), randString(32), randString(32))
}

_, err := db.Exec(buf.String())
if err != nil {
return errors.WithStack(err)
}
}
return nil
}

func (c *SysbenchCase) Execute(worker *sysbench.Worker, db *sql.DB) error {
for i := 0; i < 100; i++ {
err := c.executeSET(db)
if err != nil {
return err
}
err = c.executeSTART(db)
if err != nil {
return err
}
err = c.executeSelect(db)
if err != nil {
return err
}
}
return nil
}

func (c *SysbenchCase) executeSET(db *sql.DB) error {
num := c.insertCount * c.rowsEachInsert
_, err := db.Exec("SET TRANSACTION READ ONLY AS OF TIMESTAMP tidb_bounded_staleness(DATE_SUB(NOW(), INTERVAL 10 SECOND)")
if err != nil {
return err
}
rows, err := db.Query("select id, k, c, pad from sbtest0 where k in (?, ?, ?)", rand.Intn(num), rand.Intn(num), rand.Intn(num))
defer rows.Close()
if err != nil {
return errors.WithStack(err)
}
return nil
}

func (c *SysbenchCase) executeSTART(db *sql.DB) error {
num := c.insertCount * c.rowsEachInsert
_, err := db.Exec("START TRANSACTION READ ONLY AS OF TIMESTAMP tidb_bounded_staleness(DATE_SUB(NOW(), INTERVAL 10 SECOND)")
if err != nil {
return err
}
rows, err := db.Query("select id, k, c, pad from sbtest0 where k in (?, ?, ?)", rand.Intn(num), rand.Intn(num), rand.Intn(num))
defer rows.Close()
if err != nil {
return errors.WithStack(err)
}
_, err = db.Exec("COMMIT")
if err != nil {
return err
}
return nil
}

func (c *SysbenchCase) executeSelect(db *sql.DB) error {
num := c.insertCount * c.rowsEachInsert
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))
defer rows.Close()
if err != nil {
return errors.WithStack(err)
}
return nil
}

func (c *SysbenchCase) DropTable(db *sql.DB) error {
_, err := db.Exec("drop table if exists sbtest0")
return err
}

const ascii = "abcdefghijklmnopqrstuvwxyz1234567890"

func randString(n int) string {
var buf bytes.Buffer
for i := 0; i < n; i++ {
pos := rand.Intn(len(ascii))
buf.WriteByte(ascii[pos])
}
return buf.String()
}

func nextPrimaryID(workerCount int, current int) int {
return current + workerCount
}
38 changes: 38 additions & 0 deletions testcase/stale-read/cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"context"
"flag"
staleread "github.com/pingcap/tipocket/testcase/stale-read"

// use mysql
_ "github.com/go-sql-driver/mysql"

"github.com/pingcap/tipocket/cmd/util"
logs "github.com/pingcap/tipocket/logsearch/pkg/logs"
"github.com/pingcap/tipocket/pkg/cluster"
"github.com/pingcap/tipocket/pkg/control"
test_infra "github.com/pingcap/tipocket/pkg/test-infra"
"github.com/pingcap/tipocket/pkg/test-infra/fixture"
)

func main() {
flag.Parse()
cfg := control.Config{
Mode: control.ModeStandard,
ClientCount: 1,
RunTime: fixture.Context.RunTime,
}
c := fixture.Context
c.TiDBClusterConfig.TiDBReplicas = 1
c.TiDBClusterConfig.TiKVReplicas = 5
suit := util.Suit{
Config: &cfg,
Provider: cluster.NewDefaultClusterProvider(),
ClientCreator: staleread.ClientCreator{},
NemesisGens: util.ParseNemesisGenerators(fixture.Context.Nemesis),
ClusterDefs: test_infra.NewDefaultCluster(c.Namespace, c.ClusterName, c.TiDBClusterConfig),
LogsClient: logs.NewDiagnosticLogClient(),
}
suit.Run(context.Background())
}
51 changes: 51 additions & 0 deletions testcase/stale-read/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module github.com/pingcap/tipocket/testcase/stale-read

go 1.16

require (
github.com/go-sql-driver/mysql v1.5.0
github.com/pingcap/errors v0.11.5-0.20201126102027-b0a155152ca3
github.com/pingcap/log v0.0.0-20201112100606-8f1e84a3abc8 // indirect
github.com/pingcap/tipocket v1.0.0
github.com/pingcap/tipocket/logsearch v0.0.0-20210602095541-45d321986652
github.com/prometheus/client_golang v1.5.0
github.com/tiancaiamao/sysbench v0.0.0-20200214034607-ee9d97eabd23
github.com/tikv/pd v1.1.0-beta.0.20210225143804-1f200cbcd647 // indirect
go.uber.org/zap v1.15.0 // indirect
)

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

replace github.com/prometheus/prometheus => github.com/prometheus/prometheus v1.8.2-0.20200213233353-b90be6f32a33

replace github.com/uber-go/atomic => go.uber.org/atomic v1.5.0

replace (
k8s.io/api => k8s.io/api v0.17.0
k8s.io/apiextensions-apiserver => k8s.io/apiextensions-apiserver v0.17.0
k8s.io/apimachinery => k8s.io/apimachinery v0.17.0
k8s.io/apiserver => k8s.io/apiserver v0.17.0
k8s.io/cli-runtime => k8s.io/cli-runtime v0.17.0
k8s.io/client-go => k8s.io/client-go v0.17.0
k8s.io/cloud-provider => k8s.io/cloud-provider v0.17.0
k8s.io/cluster-bootstrap => k8s.io/cluster-bootstrap v0.17.0
k8s.io/code-generator => k8s.io/code-generator v0.17.0
k8s.io/component-base => k8s.io/component-base v0.17.0
k8s.io/cri-api => k8s.io/cri-api v0.17.0
k8s.io/csi-translation-lib => k8s.io/csi-translation-lib v0.17.0
k8s.io/kube-aggregator => k8s.io/kube-aggregator v0.17.0
k8s.io/kube-controller-manager => k8s.io/kube-controller-manager v0.17.0
k8s.io/kube-proxy => k8s.io/kube-proxy v0.17.0
k8s.io/kube-scheduler => k8s.io/kube-scheduler v0.17.0
k8s.io/kubectl => k8s.io/kubectl v0.17.0
k8s.io/kubelet => k8s.io/kubelet v0.17.0
k8s.io/legacy-cloud-providers => k8s.io/legacy-cloud-providers v0.17.0
k8s.io/metrics => k8s.io/metrics v0.17.0
k8s.io/sample-apiserver => k8s.io/sample-apiserver v0.17.0
)

replace github.com/Azure/go-autorest => github.com/Azure/go-autorest v12.2.0+incompatible

replace golang.org/x/net v0.0.0-20190813000000-74dc4d7220e7 => golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7

replace github.com/pingcap/tipocket => ../../.
Loading

0 comments on commit ccb97e0

Please sign in to comment.