Skip to content

refactor(health): migrate health_service to backend-agnostic lazy database initialization - #1493

Merged
andrzejfudro merged 3 commits into
mainfrom
refactor/health-service-lazy-db-init
Oct 30, 2026
Merged

andrzejfudro merged 3 commits into
mainfrom
refactor/health-service-lazy-db-init

Conversation

@andrzejfudro

@andrzejfudro andrzejfudro commented Oct 29, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Refactor cmd/health_service from Spanner-specific lazy client initialization to backend-agnostic lazy database initialization backed by dbconfig, factory, health.SchemaVersionClient, and dbstore.

Key Changes

  • internal/storage/dbconfig: Add health_ess_spanner_database_name and health_xid_spanner_database_name legacy fallbacks in loadSpanner().
  • internal/storage/factory & internal/storage/postgres: Expose SchemaVersion() (health.SchemaVersionClient, int64) on *factory.Storage and pass context.WithoutCancel(ctx) when constructing Spanner clients and pgxpool pools so lazily initialized handles outlive the triggering RPC context while pool.Ping(ctx) still honors the request deadline.
  • cmd/health_service: Load dbconfig.Config in LoadConfig(), replace HealthServiceLazySpannerWrapper with HealthServiceLazyDBWrapper in lazy_db_client.go (opening factory.ESS and factory.XID on demand with retry on failure), report "Spanner ESS"/"Spanner XID" or "Postgres ESS"/"Postgres XID" based on configured backend ("unknown" otherwise), and run custom component checks whenever EnableComponents is enabled.

Assumptions

  • Legacy health_ess_spanner_database_name / health_xid_spanner_database_name env fallbacks stay until the per-service DB env vars are removed post-release; the health-service Helm configmap is updated separately.
  • Empty database paths/hosts are not rejected at startup: main fails fast only on invalid DB_BACKEND or numeric settings, and missing DB targets surface as unhealthy components at check time.
  • Detaching cancellation at handle construction affects all factory.NewStorage callers; this is safe because every caller closes storage explicitly via Close(), and queries/transactions keep using their per-call contexts.

Verification

  • bazel test //internal/storage/dbconfig/... //internal/storage/factory/... //internal/storage/postgres/... //cmd/health_service/... (Passed)
  • ./.githooks/pre-commit (Passed)

TAG=agy
CONV=58868279-57fe-42e4-abeb-60f90d279ec2

…abase initialization

Refactor `cmd/health_service` from Spanner-only lazy client initialization and schema version checks to backend-agnostic lazy database initialization built on `internal/storage/dbconfig`, `internal/storage/factory`, `internal/common/health`, and `pkg/components/store/dbstore`.

Expose `SchemaVersion()` on `*factory.Storage`, detach long-lived client and pool lifetimes from short-lived lazy initialization contexts via `context.WithoutCancel(ctx)`, add legacy `health_ess_spanner_database_name` and `health_xid_spanner_database_name` env fallbacks in `dbconfig.Load()`, and report backend-specific ESS/XID component names and schema versions in `HealthChecker`.

TAG=agy
CONV=58868279-57fe-42e4-abeb-60f90d279ec2

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the health service to support multiple database backends (Spanner and Postgres) by transitioning from Spanner-specific configurations and clients to a generalized database configuration and factory storage system. It updates the health checker, lazy client wrappers, configuration loading, and associated tests to support both backends. Additionally, it ensures that database connection pools are initialized with a context detached from caller cancellation so they are not prematurely torn down. I have no feedback to provide as there are no review comments.

… alias dbFactory

Return "unknown" from `essDBComponentName()` and `xidDBComponentName()` when the configured backend is neither Spanner nor Postgres, and import `internal/storage/factory` as `dbFactory` in `cmd/health_service`.

TAG=agy
CONV=58868279-57fe-42e4-abeb-60f90d279ec2
@andrzejfudro
andrzejfudro marked this pull request as ready for review October 29, 2026 17:12
@github-actions
github-actions Bot requested a review from AntonTyb October 29, 2026 17:12
@andrzejfudro
andrzejfudro requested review from janzak-dev and removed request for AntonTyb October 30, 2026 06:51
janzak-dev
janzak-dev previously approved these changes Oct 30, 2026
Comment thread cmd/health_service/lazy_db_client.go Resolved
@andrzejfudro
andrzejfudro disabled auto-merge October 30, 2026 09:39
Rename lazy_spanner_client{,_test}.go to lazy_db_client{,_test}.go, the
server field lazySpanner to lazyDB, and the mockLazySpanner test double
to mockLazyDB now that the wrapper is backend-agnostic.

TAG=agy
CONV=58868279-57fe-42e4-abeb-60f90d279ec2
@andrzejfudro
andrzejfudro added this pull request to the merge queue Oct 30, 2026
Merged via the queue into main with commit 242e913 Oct 30, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants