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

Fix a bug in _create_new_trial and refactor it #5497

Merged

Conversation

eukaryo
Copy link
Collaborator

@eukaryo eukaryo commented Jun 17, 2024

Motivation

I want to refactor _create_new_trial
I want _create_new_trial to generate no empty trial_id when transaction processing fails.

(note: I want to clarify by separating single issue in PR #5466 , which involves multiple issues and becomes complicated.)

Description of the changes

In the current implementation, with _create_scoped_session is outside and while loop is inside, so if the transaction fails in _get_prepared_new_trial, Optuna retries without rollback of the preceding find_or_raise_by_id. As a result, empty trial_id can be generated. (I confirmed the behaviour by a simple experiment that inserts a line raising an exception at random in _get_prepared_new_trial.)
In the proposed implementation, with _create_scoped_session is inside the for loop, so a rollback occurs against every fail of transaction.

Note

In the current inplementation, only sqlalchemy_exc.OperationalError: will be caught, and other exceptions are re-raised as is. I believe the proposed implementation does not change the behaviour.

@eukaryo eukaryo changed the title Update _create_new_trial Refactor _create_new_trial Jun 17, 2024
@eukaryo
Copy link
Collaborator Author

eukaryo commented Jun 17, 2024

PR #5466 proposed to change _create_scoped_session, whereas the present PR does not require any change of _create_scoped_session, and the present PR and PR #5496 will cover the all features in PR #5466.

@eukaryo
Copy link
Collaborator Author

eukaryo commented Jun 18, 2024

I will add unittest(s) if possible; I assume it is not difficult and will be done today. The unittest(s) will confirm that the try-excepts works as I intended, and will improve the code coverage.

Copy link
Collaborator

@nabenabe0928 nabenabe0928 left a comment

Choose a reason for hiding this comment

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

Thank you for the PR, I left a comment!

optuna/storages/_rdb/storage.py Show resolved Hide resolved
@nabenabe0928 nabenabe0928 added the bug Issue/PR about behavior that is broken. Not for typos/examples/CI/test but for Optuna itself. label Jun 19, 2024
@nabenabe0928 nabenabe0928 added this to the v4.0.0 milestone Jun 19, 2024
@nabenabe0928 nabenabe0928 added code-fix Change that does not change the behavior, such as code refactoring. bug Issue/PR about behavior that is broken. Not for typos/examples/CI/test but for Optuna itself. and removed bug Issue/PR about behavior that is broken. Not for typos/examples/CI/test but for Optuna itself. code-fix Change that does not change the behavior, such as code refactoring. labels Jun 20, 2024
@eukaryo eukaryo changed the title Refactor _create_new_trial Bugfix and refactor _create_new_trial Jun 20, 2024
optuna/storages/_rdb/storage.py Outdated Show resolved Hide resolved
optuna/storages/_rdb/storage.py Show resolved Hide resolved
Co-authored-by: Shuhei Watanabe <47781922+nabenabe0928@users.noreply.github.com>
Copy link
Collaborator

@nabenabe0928 nabenabe0928 left a comment

Choose a reason for hiding this comment

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

LGTM!

@nabenabe0928 nabenabe0928 removed their assignment Jun 21, 2024
Copy link
Member

@HideakiImamura HideakiImamura left a comment

Choose a reason for hiding this comment

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

LGTM.

@HideakiImamura HideakiImamura merged commit ca489a0 into optuna:master Jun 24, 2024
14 checks passed
@nabenabe0928 nabenabe0928 changed the title Bugfix and refactor _create_new_trial Fix a bug in _create_new_trial and refactor it Jun 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue/PR about behavior that is broken. Not for typos/examples/CI/test but for Optuna itself.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants