utils: use ShardIdentity
in postgres_client.rs
#12806
Open
+390
−164
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.
Summary
This PR refactors
postgres_client.rs
to use the existingShardIdentity
type instead of individual primitive shard fields, improving type safety and reducing code duplication.Fixes #9823
Problem
The
ConnectionConfigArgs
struct inutils/postgres_client.rs
was manually tracking shard information using individual primitive fields:This approach had several issues:
ShardIdentity
typeOption<T>
fields instead of one cohesive parameterSolution
Phase 1: Move
ShardIdentity
toutils
ShardIdentity
struct frompageserver_api/shard.rs
toutils/shard.rs
ShardLayout
,ShardConfigError
,DEFAULT_STRIPE_SIZE
pageserver_api
to re-export fromutils
for backward compatibilitypageserver_api
Phase 2: Refactor
ConnectionConfigArgs
shard: Option<ShardIdentity>
fieldoptions()
method to extract values fromShardIdentity
Changes Made
Core Infrastructure
libs/utils/src/shard.rs
: AddedShardIdentity
struct with all core methodslibs/pageserver_api/src/shard.rs
: Now re-exports from utils with pageserver-specific extensionslibs/utils/src/postgres_client.rs
: Refactored to useOption<ShardIdentity>
Updated Files
safekeeper/src/recovery.rs
: UpdatedConnectionConfigArgs
constructorpageserver/src/tenant/timeline/walreceiver/connection_manager.rs
: Updated constructorBenefits
Type Safety
Simplified API
Code Consistency
ShardIdentity
type as the rest of the codebaseTesting
Performance Impact
None - this is a purely structural refactoring with no runtime performance implications. The generated connection options remain identical.
Migration Guide
For code outside this repository using
ConnectionConfigArgs
:Or for unsharded connections:
Backward Compatibility
ShardIdentity
continue to work via re-exportsConnectionConfigArgs
Related
ShardIdentity
toutils
to avoid circular dependencies utils: useShardIdentity
in `postgres_client.rs #9823Review Notes
This refactoring demonstrates several Rust best practices:
The changes are purely structural with no behavioral modifications, making this a safe refactoring that improves code quality without affecting functionality.