Skip to content

Commit 48ea72a

Browse files
committed
[core] OCTRL-911 Transitions should not be performed concurrently
1 parent bf6af3b commit 48ea72a

2 files changed

Lines changed: 26 additions & 5 deletions

File tree

core/environment/environment.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ var log = logger.New(logrus.StandardLogger(), "env")
6363
type Environment struct {
6464
Mu sync.RWMutex
6565
once sync.Once
66+
transitionMutex sync.RWMutex
6667
Sm *fsm.FSM
6768
name string
6869
id uid.ID
@@ -955,6 +956,13 @@ func (env *Environment) runTasksAsHooks(hooksToTrigger task.Tasks) (errorMap map
955956
}
956957

957958
func (env *Environment) TryTransition(t Transition) (err error) {
959+
if !env.transitionMutex.TryLock() {
960+
log.WithField("partition", env.id.String()).
961+
Warnf("environment transition attempt delayed: transition '%s' in progress. waiting for completion or failure", env.currentTransition)
962+
env.transitionMutex.Lock()
963+
}
964+
defer env.transitionMutex.Unlock()
965+
958966
the.EventWriterWithTopic(topic.Environment).WriteEvent(&pb.Ev_EnvironmentEvent{
959967
EnvironmentId: env.id.String(),
960968
State: env.Sm.Current(),
@@ -1171,11 +1179,17 @@ func (env *Environment) subscribeToWfState(taskman *task.Manager) {
11711179
Warn("one of the critical tasks went into ERROR state, transitioning the environment into ERROR")
11721180
err := env.TryTransition(NewGoErrorTransition(taskman))
11731181
if err != nil {
1174-
log.WithField("partition", env.id).
1175-
WithError(err).
1176-
WithField("level", infologger.IL_Devel).
1177-
Warn("could not transition gently to ERROR, forcing it")
1178-
env.setState(wfState.String())
1182+
if env.Sm.Current() == "ERROR" {
1183+
log.WithField("partition", env.id).
1184+
WithField("level", infologger.IL_Devel).
1185+
Info("skipped requested transition to ERROR: environment already in ERROR state")
1186+
} else {
1187+
log.WithField("partition", env.id).
1188+
WithError(err).
1189+
WithField("level", infologger.IL_Devel).
1190+
Warn("could not transition gently to ERROR, forcing it")
1191+
env.setState(wfState.String())
1192+
}
11791193
}
11801194
toStop := env.Workflow().GetTasks().Filtered(func(t *task.Task) bool {
11811195
t.SetSafeToStop(true)

core/environment/manager.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,13 @@ func (envs *Manager) TeardownEnvironment(environmentId uid.ID, force bool) error
582582
return err
583583
}
584584

585+
if !env.transitionMutex.TryLock() {
586+
log.WithField("partition", environmentId.String()).
587+
Warnf("environment teardown attempt delayed: transition '%s' in progress. waiting for completion or failure", env.currentTransition)
588+
env.transitionMutex.Lock()
589+
}
590+
defer env.transitionMutex.Unlock()
591+
585592
if env.CurrentState() != "STANDBY" && env.CurrentState() != "DEPLOYED" && !force {
586593
return errors.New(fmt.Sprintf("cannot teardown environment in state %s", env.CurrentState()))
587594
}

0 commit comments

Comments
 (0)