Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decouple generator from client #31

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/rawkv/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ func main() {
}

var creator core.ClientCreator
var gen control.Generator
switch *clientCase {
case "register":
creator = rawkv.RegisterClientCreator{}
gen = rawkv.RegisterGenRequest
default:
log.Fatalf("invalid client test case %s", *clientCase)
}
Expand All @@ -51,6 +53,7 @@ func main() {
suit := util.Suit{
Config: &cfg,
ClientCreator: creator,
Generator: gen,
Nemesises: *nemesises,
VerifySuit: verifySuit,
}
Expand Down
4 changes: 4 additions & 0 deletions cmd/tidb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,14 @@ func main() {
}

var creator core.ClientCreator
var gen control.Generator
switch *clientCase {
case "bank":
creator = tidb.BankClientCreator{}
gen = tidb.BankGenRequest
case "multi_bank":
creator = tidb.MultiBankClientCreator{}
gen = tidb.BankGenRequest
default:
log.Fatalf("invalid client test case %s", *clientCase)
}
Expand All @@ -63,6 +66,7 @@ func main() {
suit := util.Suit{
Config: &cfg,
ClientCreator: creator,
Generator: gen,
Nemesises: *nemesises,
VerifySuit: verifySuit,
}
Expand Down
3 changes: 3 additions & 0 deletions cmd/txnkv/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ func main() {
}

var creator core.ClientCreator
var gen control.Generator
switch *clientCase {
case "register":
creator = txnkv.RegisterClientCreator{}
gen = txnkv.RegisterGenRequest
default:
log.Fatalf("invalid client test case %s", *clientCase)
}
Expand All @@ -51,6 +53,7 @@ func main() {
suit := util.Suit{
Config: &cfg,
ClientCreator: creator,
Generator: gen,
Nemesises: *nemesises,
VerifySuit: verifySuit,
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/util/suit.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
type Suit struct {
*control.Config
core.ClientCreator
control.Generator
// nemesis, seperated by comma.
Nemesises string

Expand Down Expand Up @@ -63,6 +64,7 @@ func (suit *Suit) Run(ctx context.Context, nodes []string) {
sctx,
suit.Config,
suit.ClientCreator,
suit.Generator,
nemesisGens,
suit.VerifySuit,
)
Expand Down
25 changes: 13 additions & 12 deletions db/rawkv/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/pingcap/tidb/store/tikv"
"github.com/pingcap/chaos/pkg/core"
"github.com/pingcap/chaos/pkg/model"
"github.com/pingcap/chaos/pkg/control"
)

var (
Expand Down Expand Up @@ -76,18 +77,6 @@ func (c *registerClient) Invoke(ctx context.Context, node string, r interface{})
return model.RegisterResponse{}
}

func (c *registerClient) NextRequest() interface{} {
r := model.RegisterRequest{
Op: c.r.Intn(2) == 1,
}
if r.Op == model.RegisterRead {
return r
}

r.Value = int(c.r.Int63())
return r
}

// DumpState the database state(also the model's state)
func (c *registerClient) DumpState(ctx context.Context) (interface{}, error) {
val, err := c.db.Get(register)
Expand Down Expand Up @@ -117,3 +106,15 @@ type RegisterClientCreator struct {
func (RegisterClientCreator) Create(node string) core.Client {
return &registerClient{}
}

func RegisterGenRequest(*control.Config, int64) interface{} {
r := model.RegisterRequest{
Op: rand.Intn(2) == 1,
}
if r.Op == model.RegisterRead {
return r
}

r.Value = int(rand.Int63())
return r
}
39 changes: 20 additions & 19 deletions db/tidb/bank.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/anishathalye/porcupine"
pchecker "github.com/pingcap/chaos/pkg/check/porcupine"
"github.com/pingcap/chaos/pkg/core"
"github.com/pingcap/chaos/pkg/control"
"github.com/pingcap/chaos/pkg/history"

// use mysql
Expand Down Expand Up @@ -148,25 +149,6 @@ func (c *bankClient) Invoke(ctx context.Context, node string, r interface{}) int
return bankResponse{Ok: true, Tso: tso, FromBalance: fromBalance, ToBalance: toBalance}
}

func (c *bankClient) NextRequest() interface{} {
r := bankRequest{
Op: c.r.Int() % 2,
}
if r.Op == 0 {
return r
}

r.From = c.r.Intn(c.accountNum)

r.To = c.r.Intn(c.accountNum)
if r.From == r.To {
r.To = (r.To + 1) % c.accountNum
}

r.Amount = 5
return r
}

func (c *bankClient) DumpState(ctx context.Context) (interface{}, error) {
txn, err := c.db.Begin()

Expand Down Expand Up @@ -212,6 +194,25 @@ type bankRequest struct {
Amount int64
}

func BankGenRequest(*control.Config, int64) interface{} {
r := bankRequest{
Op: rand.Int() % 2,
}
if r.Op == 0 {
return r
}

r.From = rand.Intn(accountNum)

r.To = rand.Intn(accountNum)
if r.From == r.To {
r.To = (r.To + 1) % accountNum
}

r.Amount = 5
return r
}

type bankResponse struct {
// Transaction start timestamp
Tso uint64
Expand Down
19 changes: 0 additions & 19 deletions db/tidb/multi_bank.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,25 +133,6 @@ func (c *multiBankClient) Invoke(ctx context.Context, node string, r interface{}
return bankResponse{Ok: true, Tso: tso, FromBalance: fromBalance, ToBalance: toBalance}
}

func (c *multiBankClient) NextRequest() interface{} {
r := bankRequest{
Op: c.r.Int() % 2,
}
if r.Op == 0 {
return r
}

r.From = c.r.Intn(c.accountNum)

r.To = c.r.Intn(c.accountNum)
if r.From == r.To {
r.To = (r.To + 1) % c.accountNum
}

r.Amount = 5
return r
}

// DumpState the database state(also the model's state)
func (c *multiBankClient) DumpState(ctx context.Context) (interface{}, error) {
txn, err := c.db.Begin()
Expand Down
25 changes: 13 additions & 12 deletions db/txnkv/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/pingcap/tidb/store/tikv"
"github.com/pingcap/chaos/pkg/core"
"github.com/pingcap/chaos/pkg/model"
"github.com/pingcap/chaos/pkg/control"
)

var (
Expand Down Expand Up @@ -122,18 +123,6 @@ func (c *registerClient) Invoke(ctx context.Context, node string, r interface{})
return c.invokeWrite(ctx, arg)
}

func (c *registerClient) NextRequest() interface{} {
r := model.RegisterRequest{
Op: c.r.Intn(2) == 1,
}
if r.Op == model.RegisterRead {
return r
}

r.Value = int(c.r.Int63())
return r
}

// DumpState the database state(also the model's state)
func (c *registerClient) DumpState(ctx context.Context) (interface{}, error) {
tx, err := c.db.Begin()
Expand Down Expand Up @@ -174,3 +163,15 @@ type RegisterClientCreator struct {
func (RegisterClientCreator) Create(node string) core.Client {
return &registerClient{}
}

func RegisterGenRequest(*control.Config, int64) interface{} {
r := model.RegisterRequest{
Op: rand.Intn(2) == 1,
}
if r.Op == model.RegisterRead {
return r
}

r.Value = int(rand.Int63())
return r
}
10 changes: 5 additions & 5 deletions pkg/control/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ import (

// register nemesis
_ "github.com/pingcap/chaos/pkg/nemesis"

// register tidb
_ "github.com/pingcap/chaos/db/tidb"
)

// Controller controls the whole cluster. It sends request to the database,
Expand All @@ -25,7 +22,8 @@ import (
type Controller struct {
cfg *Config

clients []core.Client
clients []core.Client
clientGenerator Generator

nemesisGenerators []core.NemesisGenerator

Expand All @@ -43,6 +41,7 @@ func NewController(
ctx context.Context,
cfg *Config,
clientCreator core.ClientCreator,
clientGenerator Generator,
nemesisGenerators []core.NemesisGenerator,
verifySuit verify.Suit,
) *Controller {
Expand All @@ -59,6 +58,7 @@ func NewController(
c := new(Controller)
c.cfg = cfg
c.ctx, c.cancel = context.WithCancel(ctx)
c.clientGenerator = clientGenerator
c.nemesisGenerators = nemesisGenerators
c.suit = verifySuit

Expand Down Expand Up @@ -235,7 +235,7 @@ func (c *Controller) onClientLoop(

procID := atomic.AddInt64(&c.proc, 1)
for atomic.AddInt64(requestCount, -1) >= 0 {
request := client.NextRequest()
request := c.clientGenerator(c.cfg, procID)

if err := recorder.RecordRequest(procID, request); err != nil {
log.Fatalf("record request %v failed %v", request, err)
Expand Down
4 changes: 4 additions & 0 deletions pkg/control/generator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package control

// Generator generates a series of operations
type Generator = func(*Config, int64) interface{}
7 changes: 0 additions & 7 deletions pkg/core/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ type Client interface {
// Invoke invokes a request to the database.
// Mostly, the return Response should implement UnknownResponse interface
Invoke(ctx context.Context, node string, r interface{}) interface{}
// NextRequest generates a request for latter Invoke.
NextRequest() interface{}
// DumpState the database state(also the model's state)
DumpState(ctx context.Context) (interface{}, error)
}
Expand Down Expand Up @@ -58,11 +56,6 @@ func (noopClient) Invoke(ctx context.Context, node string, r interface{}) interf
return nil
}

// NextRequest generates a request for latter Invoke.
func (noopClient) NextRequest() interface{} {
return nil
}

// DumpState the database state(also the model's state)
func (noopClient) DumpState(ctx context.Context) (interface{}, error) {
return nil, nil
Expand Down
47 changes: 47 additions & 0 deletions pkg/generator/gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package generator

import (
"math/rand"
"time"

"github.com/pingcap/chaos/pkg/control"
)

type PairedGenerator struct {
key interface{}
gen control.Generator
}

// Reserve takes a series of count, generator pairs, and a final default
// generator.
// (reserve 5 write 10 cas read)
// The first 5 threads will call the `write` generator, the next 10 will emit
// CAS operations, and the remaining threads will perform reads. This is
// particularly useful when you want to ensure that two classes of operations
// have a chance to proceed concurrently--for instance, if writes begin
// blocking, you might like reads to proceed concurrently without every thread
// getting tied up in a write.
func Reserve(final control.Generator, gens ...PairedGenerator) control.Generator {
return func(cfg *control.Config, proc int64) interface{} {
thread := (proc - 1) % len(cfg.Nodes)
cnt := 0
for _, pair := range gens {
n := pair.key.(int)
if thread >= cnt && thread < cnt + n {
return pair.gen(cfg, proc)
}
cnt += n
}
return final(cfg, proc)
}
}

// Stagger introduces uniform random timing noise with a mean delay of
// dt duration for every operation. Delays range from 0 to 2 * dt."
func Stagger(dt time.Duration, gen control.Generator) control.Generator {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
return func(cfg *control.Config, proc int64) interface{} {
time.Sleep(time.Duration(r.Int63n(2 * int64(dt))))
return gen(cfg, proc)
}
}