Skip to content

Support Ordered and @Order for exit code generation #30457

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

Conversation

dugenkui03
Copy link
Contributor

Describing Your Changes

Different order in ExitCodeGenerators#generators will lead to different exitCode. I suggest add ‘order’ support in order to sort ExitCodeGenerators#generators by Ordered and @Order.

@dugenkui03 dugenkui03 changed the title order ExitCodeGenerator in ExitCodeGenerators by Ordered and @Order b… order generators in ExitCodeGenerators by Ordered and @Order before #getExitCode Mar 29, 2022
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 29, 2022
@wilkinsona
Copy link
Member

Thanks for the proposal. Unfortunately, I think this will cause some confusion. ExitCodeGenerators currently returns the code that's furthest from zero:

https://github.com/spring-projects/spring-boot/blob/main/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ExitCodeGenerators.java#L85-L100

We have tests for this behaviour:

@Test
void getExitCodeWhenAllNegativeShouldReturnLowestValue() {
ExitCodeGenerators generators = new ExitCodeGenerators();
generators.add(mockGenerator(-1));
generators.add(mockGenerator(-3));
generators.add(mockGenerator(-2));
assertThat(generators.getExitCode()).isEqualTo(-3);
}
@Test
void getExitCodeWhenAllPositiveShouldReturnHighestValue() {
ExitCodeGenerators generators = new ExitCodeGenerators();
generators.add(mockGenerator(1));
generators.add(mockGenerator(3));
generators.add(mockGenerator(2));
assertThat(generators.getExitCode()).isEqualTo(3);
}

Ordering these generators would make no difference to the exit code that's returned.

The only place where ordering would make a difference is if it changed the first generator to return a non-zero value. That non-zero value will decide if the returned code is positive or negative. The other generators will then be called and the value that's further from zero in the direction determined by the first non-zero value will be returned.

Given the above, I don't think we should add ordering support as you have proposed here. We could, perhaps, consider it in combination with a change to return the first non-zero value. That would be a breaking change so we'd have to make it with some care. I'll flag this for a forthcoming team meeting so that we can discuss our options.

@wilkinsona wilkinsona added the for: team-meeting An issue we'd like to discuss as a team to make progress label Mar 29, 2022
@dugenkui03
Copy link
Contributor Author

dugenkui03 commented Mar 29, 2022

Combine order with 'return the first non-zero value' will be a greate idea, it is easy to understand.

That non-zero value will decide if the returned code is positive or negative.

It seems that the 'positive or negative' of first generator value would not change the return value.

base: -1, -3, 2, output: 2

		generators.add(mockGenerator(-1));
		generators.add(mockGenerator(-3));
		generators.add(mockGenerator(2));

the 'positive or negative' would not change: 1, -3, 2, also output: 2

		generators.add(mockGenerator(1));
		generators.add(mockGenerator(-3));
		generators.add(mockGenerator(2));

order does:1, 2, -3: output: -3, not 2;

		generators.add(mockGenerator(1));
		generators.add(mockGenerator(2));
		generators.add(mockGenerator(-3));

@wilkinsona
Copy link
Member

It seems that the 'positive or negative' of first generator value would not change the return value.

You're right. I'd misread the code. Thanks for the correction.

@philwebb philwebb added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged for: team-meeting An issue we'd like to discuss as a team to make progress labels Apr 4, 2022
@philwebb philwebb added this to the 2.7.x milestone Apr 4, 2022
@philwebb
Copy link
Member

philwebb commented Apr 4, 2022

We discussed this today and we'd like to order the ExitCodeGenerator beans then return the first non-zero result. This will be a breaking change so we'll do it in 2.7.

@dugenkui03
Copy link
Contributor Author

dugenkui03 commented Apr 5, 2022

@philwebb Thanks for your response. this pr is updated, does this work for the new logic: ’order the ExitCodeGenerator beans then return the first non-zero result‘.

@dugenkui03 dugenkui03 changed the base branch from main to 2.7.x April 5, 2022 17:07
@dugenkui03 dugenkui03 changed the base branch from 2.7.x to main April 5, 2022 17:07
@wilkinsona wilkinsona changed the title order generators in ExitCodeGenerators by Ordered and @Order before #getExitCode Support Ordered and @Order for exit code generation Apr 7, 2022
@wilkinsona wilkinsona self-assigned this Apr 7, 2022
@wilkinsona wilkinsona closed this in 6d5edc4 Apr 7, 2022
@wilkinsona wilkinsona modified the milestones: 2.7.x, 2.7.0-RC1 Apr 7, 2022
@wilkinsona
Copy link
Member

Thanks very much, @dugenkui03.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants