Skip to content

Commit

Permalink
Add determining invariant from invariant manager (cadence-workflow#3320)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewjdawson2016 authored Jun 6, 2020
1 parent 754870f commit 4aa034a
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 119 deletions.
12 changes: 6 additions & 6 deletions service/worker/scanner/executions/common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ const (

// HistoryExistsInvariantType asserts that history must exist if concrete execution exists
HistoryExistsInvariantType InvariantType = "history_exists"
// ValidFirstEventInvariantType asserts that the first event in a history must be of a specific form
ValidFirstEventInvariantType InvariantType = "valid_first_event"
// OpenCurrentExecutionInvariantType asserts that an open concrete execution must have a valid current execution
OpenCurrentExecutionInvariantType InvariantType = "open_current_execution"

Expand Down Expand Up @@ -116,14 +114,16 @@ type (

// ManagerCheckResult is the result of running a list of checks
ManagerCheckResult struct {
CheckResultType CheckResultType
CheckResults []CheckResult
CheckResultType CheckResultType
DeterminingInvariantType *InvariantType
CheckResults []CheckResult
}

// ManagerFixResult is the result of running a list of fixes
ManagerFixResult struct {
FixResultType FixResultType
FixResults []FixResult
FixResultType FixResultType
DeterminingInvariantType *InvariantType
FixResults []FixResult
}
)

Expand Down
5 changes: 5 additions & 0 deletions service/worker/scanner/executions/common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,8 @@ func DeleteExecution(
FixResultType: FixResultTypeFixed,
}
}

// InvariantTypePtr returns a pointer to InvariantType
func InvariantTypePtr(t InvariantType) *InvariantType {
return &t
}
60 changes: 34 additions & 26 deletions service/worker/scanner/executions/invariants/invariantManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,32 +45,40 @@ func NewInvariantManager(

// RunChecks runs all enabled checks.
func (i *invariantManager) RunChecks(execution common.Execution) common.ManagerCheckResult {
var checkResults []common.CheckResult
checkResultType := common.CheckResultTypeHealthy
result := common.ManagerCheckResult{
CheckResultType: common.CheckResultTypeHealthy,
DeterminingInvariantType: nil,
CheckResults: nil,
}
for _, iv := range i.invariants {
checkResult := iv.Check(execution)
checkResults = append(checkResults, checkResult)
checkResultType = i.nextCheckResultType(checkResultType, checkResult.CheckResultType)
}
return common.ManagerCheckResult{
CheckResultType: checkResultType,
CheckResults: checkResults,
result.CheckResults = append(result.CheckResults, checkResult)
checkResultType, updated := i.nextCheckResultType(result.CheckResultType, checkResult.CheckResultType)
result.CheckResultType = checkResultType
if updated {
result.DeterminingInvariantType = &checkResult.InvariantType
}
}
return result
}

// RunFixes runs all enabled fixes.
func (i *invariantManager) RunFixes(execution common.Execution) common.ManagerFixResult {
var fixResults []common.FixResult
fixResultType := common.FixResultTypeSkipped
result := common.ManagerFixResult{
FixResultType: common.FixResultTypeSkipped,
DeterminingInvariantType: nil,
FixResults: nil,
}
for _, iv := range i.invariants {
fixResult := iv.Fix(execution)
fixResults = append(fixResults, fixResult)
fixResultType = i.nextFixResultType(fixResultType, fixResult.FixResultType)
}
return common.ManagerFixResult{
FixResultType: fixResultType,
FixResults: fixResults,
result.FixResults = append(result.FixResults, fixResult)
fixResultType, updated := i.nextFixResultType(result.FixResultType, fixResult.FixResultType)
result.FixResultType = fixResultType
if updated {
result.DeterminingInvariantType = &fixResult.InvariantType
}
}
return result
}

// InvariantTypes returns sorted list of all invariants that manager will run.
Expand All @@ -81,17 +89,17 @@ func (i *invariantManager) InvariantTypes() []common.InvariantType {
func (i *invariantManager) nextFixResultType(
currentState common.FixResultType,
event common.FixResultType,
) common.FixResultType {
) (common.FixResultType, bool) {
switch currentState {
case common.FixResultTypeSkipped:
return event
return event, event != common.FixResultTypeSkipped
case common.FixResultTypeFixed:
if event == common.FixResultTypeFailed {
return event
return event, true
}
return currentState
return currentState, false
case common.FixResultTypeFailed:
return currentState
return currentState, false
default:
panic("unknown FixResultType")
}
Expand All @@ -100,17 +108,17 @@ func (i *invariantManager) nextFixResultType(
func (i *invariantManager) nextCheckResultType(
currentState common.CheckResultType,
event common.CheckResultType,
) common.CheckResultType {
) (common.CheckResultType, bool) {
switch currentState {
case common.CheckResultTypeHealthy:
return event
return event, event != common.CheckResultTypeHealthy
case common.CheckResultTypeCorrupted:
if event == common.CheckResultTypeFailed {
return event
return event, true
}
return currentState
return currentState, false
case common.CheckResultTypeFailed:
return currentState
return currentState, false
default:
panic("unknown CheckResultType")
}
Expand Down
Loading

0 comments on commit 4aa034a

Please sign in to comment.