sagas

package
v0.0.0-...-b22a335 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 25, 2017 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TaskStarted flag = 1 << iota
	TaskCompleted
	CompTaskStarted
	CompTaskCompleted
)

Variables

This section is empty.

Functions

func FatalErr

func FatalErr(err error) bool

Checks the error returned by updating saga state. Returns true if the error is a FatalErr. Returns false if the error is transient and a retry might succeed

func GenId

func GenId() gopter.Gen

Generator for a valid SagaId or TaskId

func GenSagaState

func GenSagaState() gopter.Gen

Generator for a Valid Saga State

func GenSagaStateAndTaskId

func GenSagaStateAndTaskId() gopter.Gen

Generator for a SagaState and TaskId, returns a StateTaskPair SagaState is always valid. TaskId may or may not be part of the saga

func NewInternalLogError

func NewInternalLogError(msg string) error

func NewInvalidRequestError

func NewInvalidRequestError(msg string) error

func NewInvalidSagaMessageError

func NewInvalidSagaMessageError(msg string) error

func NewInvalidSagaStateError

func NewInvalidSagaStateError(msg string) error

Types

type InternalLogError

type InternalLogError struct {
	// contains filtered or unexported fields
}

InternalLogError should be returned by the SagaLog when the request failed, but may succeed on retry this is equivalent to an HTTP 500

func (InternalLogError) Error

func (e InternalLogError) Error() string

type InvalidRequestError

type InvalidRequestError struct {
	// contains filtered or unexported fields
}

InvalidRequestError should be returned by the SagaLog when the request is invalid and the same request will fail on restart, equivalent to an HTTP 400

func (InvalidRequestError) Error

func (e InvalidRequestError) Error() string

type InvalidSagaMessageError

type InvalidSagaMessageError struct {
	// contains filtered or unexported fields
}

InvalidSagaMessageError signifies that the SagaMessage was not valid

func (InvalidSagaMessageError) Error

func (e InvalidSagaMessageError) Error() string

type InvalidSagaStateError

type InvalidSagaStateError struct {
	// contains filtered or unexported fields
}

InvalidSagaStateError signifies that an illegal SagaState would occur from the requested action.

func (InvalidSagaStateError) Error

func (e InvalidSagaStateError) Error() string

type MockSagaLog

type MockSagaLog struct {
	// contains filtered or unexported fields
}

Mock of SagaLog interface

func NewMockSagaLog

func NewMockSagaLog(ctrl *gomock.Controller) *MockSagaLog

func (*MockSagaLog) EXPECT

func (_m *MockSagaLog) EXPECT() *_MockSagaLogRecorder

func (*MockSagaLog) GetActiveSagas

func (_m *MockSagaLog) GetActiveSagas() ([]string, error)

func (*MockSagaLog) GetMessages

func (_m *MockSagaLog) GetMessages(sagaId string) ([]SagaMessage, error)

func (*MockSagaLog) LogMessage

func (_m *MockSagaLog) LogMessage(message SagaMessage) error

func (*MockSagaLog) StartSaga

func (_m *MockSagaLog) StartSaga(sagaId string, job []byte) error

type Saga

type Saga struct {
	// contains filtered or unexported fields
}

Concurrent Object Representing a Saga, to allow for the concurrent execution of tasks Methods update the state of the saga.

func (*Saga) AbortSaga

func (s *Saga) AbortSaga() error

Log an AbortSaga message. This indicates that the Saga has failed and all execution should be stopped and compensating transactions should be applied.

Returns an error if it fails

func (*Saga) EndCompensatingTask

func (s *Saga) EndCompensatingTask(taskId string, results []byte) error

Log an End Compensating Task Message to the log when a Compensating Task has been successfully completed. Returns an error if it fails.

EndCompTask is idempotent with respect to sagaId & taskId. If the data passed changes the last written EndCompTask message will win

Returns an error if it fails

func (*Saga) EndSaga

func (s *Saga) EndSaga() error

Log an End Saga Message to the log, returns updated SagaState Returns the resulting SagaState or an error if it fails

Once EndSaga is succesfully called, trying to log additional messages will result in a panic.

func (*Saga) EndTask

func (s *Saga) EndTask(taskId string, results []byte) error

Log an EndTask Message to the log. Indicates that this task has been successfully completed. Returns an error if it fails.

EndTask is idempotent with respect to sagaId & taskId. If the data passed changes the last written EndTask message will win

Returns an error if it fails

func (*Saga) GetEndCompTaskData

func (s *Saga) GetEndCompTaskData(taskId string) []byte

func (*Saga) GetEndTaskData

func (s *Saga) GetEndTaskData(taskId string) []byte

func (*Saga) GetStartCompTaskData

func (s *Saga) GetStartCompTaskData(taskId string) []byte

func (*Saga) GetStartTaskData

func (s *Saga) GetStartTaskData(taskId string) []byte

func (*Saga) GetTaskIds

func (s *Saga) GetTaskIds() []string

func (*Saga) IsCompTaskCompleted

func (s *Saga) IsCompTaskCompleted(taskId string) bool

func (*Saga) IsCompTaskStarted

func (s *Saga) IsCompTaskStarted(taskId string) bool

func (*Saga) IsSagaAborted

func (s *Saga) IsSagaAborted() bool

func (*Saga) IsSagaCompleted

func (s *Saga) IsSagaCompleted() bool

func (*Saga) IsTaskCompleted

func (s *Saga) IsTaskCompleted(taskId string) bool

func (*Saga) IsTaskStarted

func (s *Saga) IsTaskStarted(taskId string) bool

func (*Saga) Job

func (s *Saga) Job() []byte

func (*Saga) SagaId

func (s *Saga) SagaId() string

Getter Methods for Saga State to ensure concurrent access is safe.

func (*Saga) StartCompensatingTask

func (s *Saga) StartCompensatingTask(taskId string, data []byte) error

Log a Start Compensating Task Message to the log. Should only be logged after a Saga has been avoided and in Rollback Recovery Mode. Should not be used in ForwardRecovery Mode returns an error if it fails

StartCompTask is idempotent with respect to sagaId & taskId. If the data passed changes the last written StartCompTask message will win

Returns an error if it fails

func (*Saga) StartTask

func (s *Saga) StartTask(taskId string, data []byte) error

Log a StartTask Message to the log. Returns an error if it fails.

StartTask is idempotent with respect to sagaId & taskId. If the data passed changes the last written StartTask message will win

Returns an error if it fails

type SagaCoordinator

type SagaCoordinator struct {
	// contains filtered or unexported fields
}

SagaCoordinator organizes a set of Sagas.

func MakeSagaCoordinator

func MakeSagaCoordinator(log SagaLog) SagaCoordinator

Make a Saga which uses the specied SagaLog interface for durable storage

func (SagaCoordinator) GetActiveSagas

func (s SagaCoordinator) GetActiveSagas() ([]string, error)

Should be called at SEC Startup Time. Returns a Slice of In Progress SagaIds

func (SagaCoordinator) MakeSaga

func (s SagaCoordinator) MakeSaga(sagaId string, job []byte) (*Saga, error)

Make a Saga add it to the SagaCoordinator, if a Saga Already exists with the same id, it will overwrite the already existing one.

func (SagaCoordinator) RecoverSagaState

func (sc SagaCoordinator) RecoverSagaState(sagaId string, recoveryType SagaRecoveryType) (*Saga, error)

Recovers SagaState by reading all logged messages from the log. Utilizes the specified recoveryType to determine if Saga needs to be Aborted or can proceed safely.

Returns the current SagaState

type SagaLog

type SagaLog interface {

	//
	// Log a Start Saga Message message to the log.
	// Returns an error if it fails.
	//
	StartSaga(sagaId string, job []byte) error

	//
	// Update the State of the Saga by Logging a message.
	// Returns an error if it fails.
	//
	LogMessage(message SagaMessage) error

	//
	// Returns all of the messages logged so far for the
	// specified saga.
	//
	GetMessages(sagaId string) ([]SagaMessage, error)

	//
	// Returns a list of all in progress sagaIds.
	// This MUST include all not completed sagaIds.
	// It may also included completed sagas
	// Returns an error if it fails.
	//
	GetActiveSagas() ([]string, error)
}

SagaLog Interface, Implemented

type SagaMessage

type SagaMessage struct {
	// contains filtered or unexported fields
}

Data Structure representation of a entry in the SagaLog. Different SagaMessageTypes utilize different fields. Factory Methods are supplied for creation of Saga Messages and should be used instead of directly creatinga sagaMessage struct

func MakeAbortSagaMessage

func MakeAbortSagaMessage(sagaId string) SagaMessage

AbortSaga SagaMessageType

  • sagaId - id of the Saga

func MakeEndCompTaskMessage

func MakeEndCompTaskMessage(sagaId string, taskId string, results []byte) SagaMessage

EndCompTask SagaMessageType

  • sagaId - id of the Saga
  • taskId - id of the completed compensating task. Should be the same as the original taskId
  • data - any results from compensating task completion

func MakeEndSagaMessage

func MakeEndSagaMessage(sagaId string) SagaMessage

EndSaga SagaMessageType

  • sagaId - id of the Saga

func MakeEndTaskMessage

func MakeEndTaskMessage(sagaId string, taskId string, results []byte) SagaMessage

EndTask SagaMessageType

  • sagaId - id of the Saga
  • taskId - id of the completed Task
  • data - any results from task completion

func MakeStartCompTaskMessage

func MakeStartCompTaskMessage(sagaId string, taskId string, data []byte) SagaMessage

StartCompTask SagaMessageType

  • sagaId - id of the Saga
  • taskId - id of the started compensating task. Should be the same as the original taskId
  • data - data that is persisted to the log, useful for diagnostic information

func MakeStartSagaMessage

func MakeStartSagaMessage(sagaId string, job []byte) SagaMessage

StartSaga SagaMessageType

  • sagaId - id of the Saga
  • data - data needed to execute the saga

func MakeStartTaskMessage

func MakeStartTaskMessage(sagaId string, taskId string, data []byte) SagaMessage

StartTask SagaMessageType

  • sagaId - id of the Saga
  • taskId - id of the started Task
  • data - data that is persisted to the log, useful for diagnostic information

func (SagaMessage) GetData

func (sm SagaMessage) GetData() []byte

func (SagaMessage) GetMessageType

func (sm SagaMessage) GetMessageType() SagaMessageType

func (SagaMessage) GetSagaId

func (sm SagaMessage) GetSagaId() string

func (SagaMessage) GetTaskId

func (sm SagaMessage) GetTaskId() string

type SagaMessageType

type SagaMessageType int
const (
	StartSaga SagaMessageType = iota
	EndSaga
	AbortSaga
	StartTask
	EndTask
	StartCompTask
	EndCompTask
)

func (SagaMessageType) String

func (s SagaMessageType) String() string

type SagaRecoveryType

type SagaRecoveryType int
const (
	RollbackRecovery SagaRecoveryType = iota
	ForwardRecovery
)

Saga Recovery Types define how to interpret SagaState in RecoveryMode.

ForwardRecovery: all tasks in the saga must be executed at least once.

tasks MUST BE idempotent

RollbackRecovery: if Saga is Aborted or in unsafe state, compensating

tasks for all started tasks need to be executed.
compensating tasks MUST BE idempotent.

type SagaState

type SagaState struct {
	// contains filtered or unexported fields
}

Data Structure representation of the current state of the Saga.

func (*SagaState) String

func (state *SagaState) String() string

Custom ToString function for SagaState

type StateTaskPair

type StateTaskPair struct {
	// contains filtered or unexported fields
}

func (StateTaskPair) String

func (p StateTaskPair) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL