-
Notifications
You must be signed in to change notification settings - Fork 598
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
Bugfix/task scheduler lifecycle #1063
Open
RenanSFreitas
wants to merge
2
commits into
spring-projects:2.5.x
Choose a base branch
from
RenanSFreitas:bugfix/task-scheduler-lifecycle
base: 2.5.x
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Bugfix/task scheduler lifecycle #1063
RenanSFreitas
wants to merge
2
commits into
spring-projects:2.5.x
from
RenanSFreitas:bugfix/task-scheduler-lifecycle
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ConcurrentTaskScheduler doesn't expose any way for it's inner executor to be shutdown, when it's instance is created with the default constructor. This can lead to a scenario where a library user ends up with many unused threads with no clear cues about where they came from. This commit replaces the usage of ConcurrentTaskScheduler with ThreadPoolTaskScheduler, a type of similar semantics but better API, which includes methods for managing its internal resources lifecycle. Also it implements spring-beans interfaces which makes it more suitable to be managed by a Spring application context. - Fixes spring-projects#624
Fix a test that sets a TimerTrigger that may try to schedule a notification runnable after the internal TaskScheduler is already shutdown. See https://github.com/spring-projects/spring-statemachine/blob/2.5.1/spring-statemachine-core/src/main/java/org/springframework/statemachine/trigger/TimerTrigger.java#L117
This pull requests aims to fix the scenario exemplified in the code below, where usage of spring-statemachine package com.example.springstatemachinethreadpoolbug;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.config.StateMachineBuilder;
import org.springframework.statemachine.config.configurers.StateConfigurer;
import org.springframework.statemachine.support.DefaultStateMachineContext;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;
@SpringBootApplication
public class Application implements CommandLineRunner {
enum StateMachineEvent {
E0, E1, E2
}
enum StateMachineState {
S0, S1, S2
}
static final EnumSet<StateMachineState> ALL_STATES = EnumSet.allOf(StateMachineState.class);
public static void main(String[] args) {
final SpringApplication app = new SpringApplication(Application.class);
app.run();
}
@Override
public void run(final String... args) {
runTestCase();
}
private class Iteration {
StateMachineState iterationInitialState;
StateMachineEvent iterationInitialEvent;
StateMachineEvent iterationEventToPush;
public Iteration(final StateMachineState iterationInitialState,
final StateMachineEvent iterationInitialEvent,
final StateMachineEvent iterationEventToPush) {
this.iterationInitialState = iterationInitialState;
this.iterationInitialEvent = iterationInitialEvent;
this.iterationEventToPush = iterationEventToPush;
}
}
private void runTestCase() {
final var beforeThreadCount = Thread.activeCount();
// Do the same thing a few times, to demonstrate the increase in the thread count
for (int i = 0; i < 100; i++) {
for (final var iteration : List.of(
new Iteration(StateMachineState.S0, StateMachineEvent.E0, StateMachineEvent.E0),
new Iteration(StateMachineState.S1, StateMachineEvent.E1, StateMachineEvent.E1)
)) {
final var iterationInitialState = iteration.iterationInitialState;
final var iterationInitialEvent = iteration.iterationInitialEvent;
final var stateMachine = buildStateMachine(iterationInitialState, iterationInitialEvent);
stateMachine.start();
stateMachine.sendEvent(MessageBuilder.withPayload(iteration.iterationEventToPush)
.setHeader("HEADER_A", "HEADER_A_VALUE")
.build());
stateMachine.stop();
}
}
System.out.println("Before thread count: " + beforeThreadCount);
System.out.println("After thread count: " + Thread.activeCount());
}
public StateMachine<StateMachineState, StateMachineEvent> buildStateMachine(StateMachineState state, StateMachineEvent event) {
try {
final StateMachineBuilder.Builder<StateMachineState, StateMachineEvent> builder = buildStateMachine();
final StateConfigurer<StateMachineState, StateMachineEvent> stateConfigurer = builder.configureStates()
.withStates()
.initial(StateMachineState.S0)
.states(ALL_STATES)
.end(StateMachineState.S2);
builder.configureTransitions()
.withExternal()
.source(StateMachineState.S0)
.target(StateMachineState.S1)
.event(StateMachineEvent.E0)
.and()
.withExternal()
.source(StateMachineState.S1)
.target(StateMachineState.S2)
.event(StateMachineEvent.E1);
stateConfigurer.state(StateMachineState.S2, context -> System.out.println("State machine action at " + StateMachineState.S2));
// it happens with stateDo too
// stateConfigurer.stateDo(StateMachineState.STATE_2, context -> System.out.println("State machine action at " + StateMachineState.S2));
final var stateMachine = builder.build();
stateMachine.getStateMachineAccessor()
.doWithAllRegions((function) -> function.resetStateMachine(new DefaultStateMachineContext<>(state,
event,
Map.of(),
stateMachine.getExtendedState(),
null,
stateMachine.getId()))
);
return stateMachine;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private StateMachineBuilder.Builder<StateMachineState, StateMachineEvent> buildStateMachine()
throws Exception {
final StateMachineBuilder.Builder<StateMachineState, StateMachineEvent> result = StateMachineBuilder.builder();
result.configureConfiguration()
.withConfiguration()
.autoStartup(false)
.machineId("STATE_MACHINE_ID");
return result;
}
} |
nbrouand
added a commit
to chutney-testing/chutney-legacy
that referenced
this pull request
Nov 29, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
ConcurrentTaskScheduler
doesn't expose any way for it's inner executor to be shutdown, when it's instance is created with the default constructor. This can lead to a scenario where a library user ends up with many unused threads with no clear cues about where they came from. The unmanaged creation of those threads can result in anOutOfMemoryError
for a client application.This commit replaces the usage of
ConcurrentTaskScheduler
withThreadPoolTaskScheduler
, a type of similar semantics but better API, which includes methods for managing its internal resources lifecycle. Also it implements spring-beans interfaces which makes it more suitable to be managed by a Spring application context.