github.com/pingcap/chaos@v0.0.0-20190710112158-c86faf4b3719/pkg/core/model.go (about)

     1  package core
     2  
     3  // Model specifies the behavior of a data object.
     4  type Model interface {
     5  	// Prepare the initial state of the data object.
     6  	Prepare(state interface{})
     7  
     8  	// Initial state of the data object.
     9  	Init() interface{}
    10  
    11  	// Step function for the data object. Returns whether or not the system
    12  	// could take this step with the given inputs and outputs and also
    13  	// returns the new state. This should not mutate the existing state.
    14  	//
    15  	// state must support encoding to and decoding from json.
    16  	Step(state interface{}, input interface{}, output interface{}) (bool, interface{})
    17  
    18  	// Equality on states.
    19  	Equal(state1, state2 interface{}) bool
    20  
    21  	// Name returns the unique name for the model.
    22  	Name() string
    23  }
    24  
    25  // Operation action
    26  const (
    27  	InvokeOperation = "call"
    28  	ReturnOperation = "return"
    29  )
    30  
    31  // Operation of a data object.
    32  type Operation struct {
    33  	Action string      `json:"action"`
    34  	Proc   int64       `json:"proc"`
    35  	Data   interface{} `json:"data"`
    36  }
    37  
    38  // NoopModel is noop model.
    39  type NoopModel struct {
    40  	perparedState interface{}
    41  }
    42  
    43  // Prepare the initial state of the data object.
    44  func (n *NoopModel) Prepare(state interface{}) {
    45  	n.perparedState = state
    46  }
    47  
    48  // Init initials state of the data object.
    49  func (n *NoopModel) Init() interface{} {
    50  	return n.perparedState
    51  }
    52  
    53  // Step function for the data object.
    54  func (*NoopModel) Step(state interface{}, input interface{}, output interface{}) (bool, interface{}) {
    55  	return true, state
    56  }
    57  
    58  // Equal returns equality on states.
    59  func (*NoopModel) Equal(state1, state2 interface{}) bool {
    60  	return true
    61  }
    62  
    63  // Name returns the unique name for the model.
    64  func (*NoopModel) Name() string {
    65  	return "NoopModel"
    66  }