Skip to content

Fix gRPC trial datetime serialization - #6832

Merged
c-bata merged 1 commit into
optuna:masterfrom
gen740:fix/grpc-datetime-serialization
Sep 18, 2026
Merged

c-bata merged 1 commit into
optuna:masterfrom
gen740:fix/grpc-datetime-serialization

Conversation

@gen740

@gen740 gen740 commented Aug 28, 2026 •

Copy link
Copy Markdown
Member

Motivation

This is the gRPC-side follow-up to #6708.

Although #6776 made timestamp handling in the storage backends consistent, gRPC still serialized trial datetimes without timezone information. As a result,
datetimes could be interpreted using the gRPC server's local timezone.

Description of the changes

  • Serialize trial datetimes as UTC-aware ISO 8601 strings.
  • Deserialize them back into the naive local datetime representation expected by FrozenTrial.
  • Preserve the empty-string representation for missing datetimes.
  • Extend the existing gRPC client/server compatibility tests to cover template trials with multiple timezones.

Checklist

  • I used an LLM while working on this PR.

@c-bata c-bata self-assigned this Aug 28, 2026
@c-bata

c-bata commented Sep 3, 2026

Copy link
Copy Markdown
Member

@yen-0 Could you review this PR?

@github-actions

Copy link
Copy Markdown
Contributor

This pull request has not seen any recent activity.

@github-actions github-actions Bot added the stale Exempt from stale bot labeling. label Sep 10, 2026
@c-bata c-bata removed the stale Exempt from stale bot labeling. label Sep 11, 2026
@yen-0

yen-0 commented Sep 11, 2026

Copy link
Copy Markdown
Collaborator

Certainly!

@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 Sep 11, 2026

@yen-0 yen-0 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM. The timestamp conversion looks correct and is consistent with the previous storage fixes.

One note: changing the existing wire representation from the legacy format to timezone-aware ISO-8601 makes new servers incompatible with older gRPC storage proxy clients. I think this behavior should be documented in the gRPC storage proxy documentation or release notes.

@c-bata
c-bata requested a review from yen-0 September 16, 2026 22:56

@yen-0 yen-0 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM! To verify gRPC trial timestamp serialization with multiple concurrent clients in different timezones, using both SQLite and MySQL backends, one gRPC server was started for each backend on ubuntu. Three clients connected concurrently to each server:

Client 1: Asia/Tokyo
Client 2: America/Los_Angeles
Client 3: UTC

The server process used TZ=UTC. Each client created and completed a trial through GrpcStorageProxy, then read both timestamps back.

Server code

import os
import time
from optuna.storages import get_storage
from optuna.storages._grpc.server import make_server

server = make_server(get_storage(os.environ["STORAGE_URL"]), "127.0.0.1", 13001)
server.start()
print("READY", flush=True)
time.sleep(60)

The server was started for each backend with:

STORAGE_URL="$storage_url" TZ=UTC uv run python server.py > server.log 2>&1 &

Client code

import os
from optuna.storages import GrpcStorageProxy
from optuna.study import StudyDirection
from optuna.trial import TrialState

backend = os.environ["BACKEND"]
zone = os.environ["CLIENT_ZONE"]
storage = GrpcStorageProxy(host="127.0.0.1", port=13001)
storage.wait_server_ready(timeout=10)
study_id = storage.create_new_study(
    [StudyDirection.MINIMIZE], study_name=f"{backend}-{zone}"
)
trial_id = storage.create_new_trial(study_id)
storage.set_trial_state_values(trial_id, TrialState.COMPLETE, [1.0])
trial = storage.get_trial(trial_id)
assert trial.datetime_start is not None
assert trial.datetime_complete is not None
print(backend, zone, trial.datetime_start.isoformat(), trial.datetime_complete.isoformat())
storage.close()

For each backend, the three clients were launched concurrently with:

for entry in "Asia/Tokyo" "America/Los_Angeles" "UTC"; do
  BACKEND="$backend" CLIENT_ZONE="$entry" TZ="$entry" uv run python client.py \
    > "client-${entry//\//_}.log" 2>&1 &
done
wait
cat client-*.log
cat server.log

Results

SQLite results:

sqlite America/Los_Angeles 2026-09-17T18:04:18.477684 2026-09-17T18:04:18.483003
sqlite Asia/Tokyo 2026-09-18T10:04:18.465569 2026-09-18T10:04:18.470433
sqlite UTC 2026-09-18T01:04:18.408082 2026-09-18T01:04:18.415874

MySQL results:

mysql America/Los_Angeles 2026-09-17T18:05:19 2026-09-17T18:05:19
mysql Asia/Tokyo 2026-09-18T10:05:19 2026-09-18T10:05:19
mysql UTC 2026-09-18T01:05:19 2026-09-18T01:05:19

All six client runs completed successfully. The local times differ by the expected timezone offsets, while representing the same server-side instants. The MySQL run also confirms that the behavior works with the database backend relevant to typical GrpcStorageProxy deployments.

Compatibility note

This change updates the gRPC timestamp wire representation from the legacy format to timezone-aware ISO-8601. New servers are therefore incompatible with older GrpcStorageProxy clients. This behavior should be noted.

@c-bata c-bata left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM.

@c-bata c-bata added this to the v5.1.0 milestone Sep 18, 2026
@c-bata
c-bata merged commit f194136 into optuna:master Sep 18, 2026
15 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.

3 participants