Skip to content

Commit

Permalink
use same process ID expect meeting unknown response (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
siddontang authored Sep 6, 2018
1 parent 8f08827 commit 9d94d7d
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 8 deletions.
9 changes: 8 additions & 1 deletion db/tidb/bank.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,13 @@ type bankResponse struct {
Unknown bool
}

var _ core.UnknownResponse = (*bankResponse)(nil)

// IsUnknown implements UnknownResponse interface
func (r bankResponse) IsUnknown() bool {
return r.Unknown
}

func newBankEvent(v interface{}, id uint) porcupine.Event {
if _, ok := v.(bankRequest); ok {
return porcupine.Event{Kind: porcupine.CallEvent, Value: v, Id: id}
Expand Down Expand Up @@ -408,7 +415,7 @@ func mergeTransferEvents(index int, events tsoEvents, e *tsoEvent) (tsoEvents, e
curBalance, _ := e.GetBalances(index)

if !checkBalance(index, events, curBalance) {
return nil, fmt.Errorf("invalid event %s", e)
return nil, fmt.Errorf("%d %v invalid event %s", index, events, e)
}

events = append(events, e)
Expand Down
17 changes: 13 additions & 4 deletions pkg/control/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ type Controller struct {
ctx context.Context
cancel context.CancelFunc

proc int64
proc int64
requestCount int64

recorder *history.Recorder
}
Expand Down Expand Up @@ -180,21 +181,29 @@ func (c *Controller) onClientLoop(i int) {
ctx, cancel := context.WithTimeout(c.ctx, c.cfg.RunTime)
defer cancel()

for i := 0; i < c.cfg.RequestCount; i++ {
procID := atomic.AddInt64(&c.proc, 1)

procID := atomic.AddInt64(&c.proc, 1)
for atomic.AddInt64(&c.requestCount, 1) <= int64(c.cfg.RequestCount) {
request := client.NextRequest()

if err := c.recorder.RecordRequest(procID, request); err != nil {
log.Fatalf("record request %v failed %v", request, err)
}

response := client.Invoke(ctx, node, request)
isUnknown := true
if v, ok := response.(core.UnknownResponse); ok {
isUnknown = v.IsUnknown()
}

if err := c.recorder.RecordResponse(procID, response); err != nil {
log.Fatalf("record response %v failed %v", response, err)
}

// If Unknown, we need to use another process ID.
if isUnknown {
procID = atomic.AddInt64(&c.proc, 1)
}

select {
case <-ctx.Done():
return
Expand Down
8 changes: 5 additions & 3 deletions pkg/core/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"context"
)

// RequestGenerator generates a request.
type RequestGenerator interface {
Generate() interface{}
// UnknownResponse means we don't know wether this operation
// succeeds or not.
type UnknownResponse interface {
IsUnknown() bool
}

// Client applies the request to the database.
Expand All @@ -18,6 +19,7 @@ type Client interface {
// TearDown tears down the client.
TearDown(ctx context.Context, nodes []string, node string) error
// 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{}
Expand Down
8 changes: 8 additions & 0 deletions pkg/model/cas_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"

"github.com/anishathalye/porcupine"
"github.com/siddontang/chaos/pkg/core"
"github.com/siddontang/chaos/pkg/history"
)

Expand Down Expand Up @@ -32,6 +33,13 @@ type CasRegisterResponse struct {
Unknown bool // used when operation times out
}

var _ core.UnknownResponse = (*CasRegisterResponse)(nil)

// IsUnknown implements UnknownResponse interface
func (r CasRegisterResponse) IsUnknown() bool {
return r.Unknown
}

// CasRegisterModel returns a cas register model
func CasRegisterModel() porcupine.Model {
return porcupine.Model{
Expand Down
8 changes: 8 additions & 0 deletions pkg/model/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"

"github.com/anishathalye/porcupine"
"github.com/siddontang/chaos/pkg/core"
"github.com/siddontang/chaos/pkg/history"
)

Expand All @@ -29,6 +30,13 @@ type RegisterResponse struct {
Value int
}

var _ core.UnknownResponse = (*RegisterResponse)(nil)

// IsUnknown implements UnknownResponse interface
func (r RegisterResponse) IsUnknown() bool {
return r.Unknown
}

// RegisterModel returns a read/write register model
func RegisterModel() porcupine.Model {
return porcupine.Model{
Expand Down

0 comments on commit 9d94d7d

Please sign in to comment.