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

Add AbstractTaskletStepBuilder copy constructor #4471

Closed
wants to merge 2 commits into from

Conversation

ilpyoyang
Copy link
Contributor

Description

This PR adds a copy constructor to the AbstractTaskletStepBuilder to address an issue arising during the property setting process with taskExecutor.

Changes

  • Add AbstractTaskletStepBuilder copy constructor
  • Remove unnecessary transactionManager in SimpleStepBuilder copy constructor
  • Add testcode

Resolve #4438

* copied, so it can be re-used.
* @param parent a parent helper containing common step properties
*
* @author Ilpyo Yang
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe @author should be located on L51, not here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change to L51 completed!


@Test
void faultTolerantMethodTest() throws ReflectiveOperationException {
simpleStepBuilder.taskExecutor(taskExecutor); // The task executor is set before faultTolerant()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assertEquals(taskExecutor, afterTaskExecutor);

Can we check taskExecutor is copied well after faultTolerant()?

Copy link
Contributor Author

@ilpyoyang ilpyoyang Oct 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added it, thank you for the review.

@ilpyoyang ilpyoyang force-pushed the main branch 2 times, most recently from e281868 to a22d354 Compare October 23, 2023 16:04
Comment on lines +78 to +93
/**
* Create a new builder initialized with any properties in the parent. The parent is
* copied, so it can be re-used.
* @param parent a parent helper containing common step properties
*/
public AbstractTaskletStepBuilder(AbstractTaskletStepBuilder<?> parent) {
super(parent);
this.chunkListeners = parent.chunkListeners;
this.stepOperations = parent.stepOperations;
this.transactionManager = parent.transactionManager;
this.transactionAttribute = parent.transactionAttribute;
this.streams.addAll(parent.streams);
this.exceptionHandler = parent.exceptionHandler;
this.throttleLimit = parent.throttleLimit;
this.taskExecutor = parent.taskExecutor;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

        TaskletStep step2 = new StepBuilder("step-name", jobRepository)
                .chunk(10, transactionManager)
                .taskExecutor(taskExecutor)// The task executor is set before faultTolerant()
                .reader(itemReader)
                .processor(itemProcessor)
                .writer(itemWriter)
                .faultTolerant() // after faultTolerant(), taskExecutor is not copied
                // cause there's no copy constructor of AbstractTaskletStepBuilder!
                .build();

Hi @fmbenhassine , me and @Ilpyo-Yang checked that there's no copy constructor of AbstractTaskletStepBuilder, so SimpleStepBuilder.faultTolerant() doesn't copy member variables of AbstractTaskletStepBuilder well now.

All users who use SimpleStepBuilder.faultTolerant() affected by this bug, so I think it's good to release this bug fix on next release.

So can you review this PR? thanks a lot! 🙇

this.stepOperations = parent.stepOperations;
this.transactionManager = parent.transactionManager;
this.transactionAttribute = parent.transactionAttribute;
this.streams.addAll(parent.streams);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
this.streams.addAll(parent.streams);
this.streams = parent.streams;

Hmm maybe we can just use parent.streams instead of addAll()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm maybe we can just use parent.streams instead of addAll()?

To creating a copy constructor, it seems like the second option, this.streams = parents.streams, is appropriate. Thank you for your comment @injae-kim!

But just curious.. was there any specific reason for initially declaring streams as a final variable?

* copied, so it can be re-used.
* @param parent a parent helper containing common step properties
*/
public AbstractTaskletStepBuilder(AbstractTaskletStepBuilder<?> parent) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about making it protected?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh I will try thanks!

@@ -0,0 +1,114 @@
package org.springframework.batch.test;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think package line should be followed by copyright comments. (see also)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't recognize it.. I will updated it ;)

*/
@SpringBatchTest
@SpringJUnitConfig
public class AbstractTaskletStepBuilderTests {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this test code should be placed somewhere in spring-batch-core/src/test/java....

spring-batch-test module is for providing test utils for spring batch users.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

umm that is why this not merged.. I missed few things in contribute regulation. Thank you for kindness review. I will update all in this holiday

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@acktsap thanks for the detailed review, and I've incorporated the suggested changes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still see this class in the spring-batch-test module. No problem, I will move it to the core module on merge.

@ilpyoyang ilpyoyang force-pushed the main branch 2 times, most recently from a22d354 to f5dd94c Compare February 12, 2024 13:12
@fmbenhassine
Copy link
Contributor

Thank you for the PR! It fixes the issue 👍

However, for the test, there is no need to test the copy constructor itself. I think something like the test suggested in #4438 is enough to validate the fix and cover the code path for any future regression.

Could you please update the test and rebase/squash the changes? It should then be good to merge. Thank you upfront!

@fmbenhassine fmbenhassine added the status: waiting-for-reporter Issues for which we are waiting for feedback from the reporter label Feb 13, 2024
This commit includes tests for the copy constructor of AbstractTaskletStepBuilder and for the faultTolerant method, specifically after taskExecutor has been set.
@ilpyoyang
Copy link
Contributor Author

Hello @fmbenhassine!
I've updated the commits as per your guidance. The test code now matches the issue details. I've stuck with the TaskletStep approach, finding it most suitable. Please review at your convenience. Thank you!

@fmbenhassine
Copy link
Contributor

Thank you for the updates, @Ilpyo-Yang ! I noticed that the tests pass even without the changes in the PR. I think we can simplify tests by checking that the type of the repeat template is correct whether the task executor is set before or after .faultTolerant(). I will take care of that on merge.

fmbenhassine added a commit that referenced this pull request Feb 20, 2024
- Update tests
- Move test class to the core module
fmbenhassine added a commit that referenced this pull request Feb 20, 2024
- Update tests
- Move test class to the core module

(cherry picked from commit 4b8e504)
@fmbenhassine
Copy link
Contributor

LGTM 👍 Rebased and merged as b9ba8ff. I just updated the tests as mentioned before in 4b8e504. Thank you for your contribution!

@fmbenhassine fmbenhassine added pr-for: bug in: core and removed status: waiting-for-reporter Issues for which we are waiting for feedback from the reporter labels Feb 20, 2024
fmbenhassine added a commit that referenced this pull request Feb 20, 2024
- Update tests
- Move test class to the core module

(cherry picked from commit 4b8e504)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect step configuration when setting the taskExecutor before faultTolerant()
4 participants