Fix incremental update algorithm in _CachedStorage's _read_trials_from_remote_storage#6310
Merged
c-bata merged 3 commits intooptuna:masterfrom Oct 27, 2025
Merged
Conversation
This reverts commit c21d991.
sawa3030
approved these changes
Oct 23, 2025
Collaborator
sawa3030
left a comment
There was a problem hiding this comment.
Thank you for the detailed PR description. LGTM
c-bata
reviewed
Oct 27, 2025
| storage._read_trials_from_remote_storage(study_id2) | ||
|
|
||
| storage.delete_study(study_id1) | ||
| assert storage._get_cached_trial(trial_id1) is None |
Member
There was a problem hiding this comment.
Could you please add the following test case that reproduces the bug you found? I confirmed that the test case below fails on the master branch and passes with this PR.
def test_unfinished_trial_ids() -> None:
# This test reproduces the bug fixed in https://github.com/optuna/optuna/pull/6310
with NamedTemporaryFilePool() as tempfile:
storage_url = f"sqlite:///{tempfile.name}"
study_name = "test-unfinished-trial-ids"
study1 = optuna.create_study(
study_name=study_name,
storage=_CachedStorage(RDBStorage(storage_url)),
load_if_exists=True,
)
study2 = optuna.create_study(
study_name=study_name,
storage=_CachedStorage(RDBStorage(storage_url)),
load_if_exists=True,
)
study1.add_trial(optuna.trial.create_trial(state=TrialState.RUNNING))
study2.add_trial(optuna.trial.create_trial(state=TrialState.COMPLETE, value=0.0))
assert len(study2.trials) == 2
c-bata
reviewed
Oct 27, 2025
Member
c-bata
left a comment
There was a problem hiding this comment.
I executed the benchmark script, which is the same one I prepared for PR #5704. I confirmed that this PR does not introduce performance degradation.
study.optimize (10 threads)
| n_trials | master | This PR |
|---|---|---|
| 10000 | 667.70 | 679.47 |
storage.get_all_trials x 20 (10 threads)
| n_trials | master | This PR |
|---|---|---|
| 10000 | 236.03 | 238.17 |
LGTM with a nit!
Member
Author
|
Thank you for your review. I have added a test as your comment. |
This file contains hidden or 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
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.
Motivation
#5704 changed the incremental update algorithm in
_CachedStorage's_read_trials_from_remote_storage. It introducedlast_finished_trial_idandunfinished_trial_ids, but the handling of them is buggy. When updatinglast_finished_trial_id, ifunfinished_trial_idsisn't properly set, it may fail to retrieve all trials.The following is a minimal reproducible example. Running Script 1 and then executing Script 2 in a separate process allows Script 2 to retrieve only one trial.
For fixing this problem, updating
last_finished_trial_idshould only be performed in_read_trials_from_remote_storagebecause they must be executed only when all trials have been considered.Description of the changes
statesargument to_read_trials_from_remote_storage#6288last_finished_trial_idandunfinished_trial_idsincreate_new_trialstatein_get_cached_trial