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

[core] OCTRL-911 Transitions should not be performed concurrently #600

Merged
merged 1 commit into from
Jul 31, 2024
Merged
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
24 changes: 19 additions & 5 deletions core/environment/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ var log = logger.New(logrus.StandardLogger(), "env")
type Environment struct {
Mu sync.RWMutex
once sync.Once
transitionMutex sync.RWMutex
Sm *fsm.FSM
name string
id uid.ID
Expand Down Expand Up @@ -955,6 +956,13 @@ func (env *Environment) runTasksAsHooks(hooksToTrigger task.Tasks) (errorMap map
}

func (env *Environment) TryTransition(t Transition) (err error) {
if !env.transitionMutex.TryLock() {
log.WithField("partition", env.id.String()).
Warnf("environment transition attempt delayed: transition '%s' in progress. waiting for completion or failure", env.currentTransition)
env.transitionMutex.Lock()
}
defer env.transitionMutex.Unlock()

the.EventWriterWithTopic(topic.Environment).WriteEvent(&pb.Ev_EnvironmentEvent{
EnvironmentId: env.id.String(),
State: env.Sm.Current(),
Expand Down Expand Up @@ -1171,11 +1179,17 @@ func (env *Environment) subscribeToWfState(taskman *task.Manager) {
Warn("one of the critical tasks went into ERROR state, transitioning the environment into ERROR")
err := env.TryTransition(NewGoErrorTransition(taskman))
if err != nil {
log.WithField("partition", env.id).
WithError(err).
WithField("level", infologger.IL_Devel).
Warn("could not transition gently to ERROR, forcing it")
env.setState(wfState.String())
if env.Sm.Current() == "ERROR" {
log.WithField("partition", env.id).
WithField("level", infologger.IL_Devel).
Info("skipped requested transition to ERROR: environment already in ERROR state")
} else {
log.WithField("partition", env.id).
WithError(err).
WithField("level", infologger.IL_Devel).
Warn("could not transition gently to ERROR, forcing it")
env.setState(wfState.String())
}
}
toStop := env.Workflow().GetTasks().Filtered(func(t *task.Task) bool {
t.SetSafeToStop(true)
Expand Down
7 changes: 7 additions & 0 deletions core/environment/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,13 @@ func (envs *Manager) TeardownEnvironment(environmentId uid.ID, force bool) error
return err
}

if !env.transitionMutex.TryLock() {
log.WithField("partition", environmentId.String()).
Warnf("environment teardown attempt delayed: transition '%s' in progress. waiting for completion or failure", env.currentTransition)
env.transitionMutex.Lock()
}
defer env.transitionMutex.Unlock()

if env.CurrentState() != "STANDBY" && env.CurrentState() != "DEPLOYED" && !force {
return errors.New(fmt.Sprintf("cannot teardown environment in state %s", env.CurrentState()))
}
Expand Down
Loading