Skip to content

Fix a bug that a gRPC server doesn't work with JournalStorage#6004

Merged
nabenabe0928 merged 3 commits intooptuna:masterfrom
fusawa-yugo:fusawa-yugo/survey-error-in-grpc-server
Mar 24, 2025
Merged

Fix a bug that a gRPC server doesn't work with JournalStorage#6004
nabenabe0928 merged 3 commits intooptuna:masterfrom
fusawa-yugo:fusawa-yugo/survey-error-in-grpc-server

Conversation

@fusawa-yugo
Copy link
Copy Markdown
Contributor

@fusawa-yugo fusawa-yugo commented Mar 7, 2025

Motivation

Currently, the gRPC server fails to function properly when using JournalStorage.

Description of the changes

The RepeatedScalarContainer class is used to convey arrays for protobuf.
However, an instance of RepeatedScalarContainer cannot be converted to JSON.
To address this, I made the following change:

values = list(request.values)

Additionally, to prevent issues like this in the future, I added grpc_journal_file to the STORAGE_MODES for pytest:

STORAGE_MODES: list[Any] = [
    "inmemory",
    "sqlite",
    "cached_sqlite",
    "journal",
    "journal_redis",
    "grpc_rdb",
    "grpc_journal_file",
]

@c-bata c-bata added the bug Issue/PR about behavior that is broken. Not for typos/examples/CI/test but for Optuna itself. label Mar 7, 2025
@y0z y0z assigned c-bata, gen740 and nabenabe0928 and unassigned c-bata and gen740 Mar 10, 2025
@y0z
Copy link
Copy Markdown
Member

y0z commented Mar 10, 2025

@c-bata @nabenabe0928 Could you review this PR?

Copy link
Copy Markdown
Member

@c-bata c-bata left a comment

Choose a reason for hiding this comment

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

LGTM! I confirmed that this PR allows the following code to work correctly.

$ python3 -c "import optuna; from optuna.storages import run_grpc_proxy_server; run_grpc_proxy_server(storage=optuna.storages.JournalStorage(optuna.storages.journal.JournalFileBackend('./tmp/journal.log')), host='localhost', port=13000)"
import optuna


def objective(trial):
    x = trial.suggest_float("x", -10, 10)
    return x ** 2


if __name__ == "__main__":
    study = optuna.create_study(
        study_name="distributed-example",
        storage=optuna.storages.GrpcStorageProxy(host="localhost", port=13000),
        load_if_exists=True,
    )
    study.optimize(objective, n_trials=100)

@c-bata c-bata removed their assignment Mar 13, 2025
# We don't actually require that all storages to preserve the order of parameters,
# but all current implementations except for GrpcStorageProxy do, so we test this property.
if storage_mode == "grpc":
if storage_mode == "grpc_rdb" or storage_mode == "grpc_journal_file":
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
if storage_mode == "grpc_rdb" or storage_mode == "grpc_journal_file":
if storage_mode in ("grpc_rdb", "grpc_journal_file"):

@pytest.mark.parametrize("storage_mode", STORAGE_MODES)
def test_get_trials(storage_mode: str) -> None:
if storage_mode == "grpc":
if storage_mode == "grpc_rdb" or storage_mode == "grpc_journal_file":
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
if storage_mode == "grpc_rdb" or storage_mode == "grpc_journal_file":
if storage_mode in ("grpc_rdb", "grpc_journal_file"):

Copy link
Copy Markdown
Contributor

@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 confirmed that the modification works as intended!
Please apply my comments and then I will approve this PR!

@fusawa-yugo
Copy link
Copy Markdown
Contributor Author

@nabenabe0928
Thank you for your review!!
I revised them. Plus, I have corrected the parts I previously overlooked.
Could you check it again?

Copy link
Copy Markdown
Contributor

@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 updates, LGTM!

@nabenabe0928 nabenabe0928 added this to the v4.3.0 milestone Mar 24, 2025
@nabenabe0928 nabenabe0928 removed their assignment Mar 24, 2025
@nabenabe0928 nabenabe0928 merged commit faf7e85 into optuna:master Mar 24, 2025
14 checks passed
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.

5 participants