You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is your feature request related to a problem? Please describe
The current implementation of ReplicaTopologyProvider defines regular expression patterns as individual static final Pattern constants at the class level (e.g., ROLE_PATTERN, SLAVE_PATTERN, etc.). This approach scatters related patterns across the codebase, making it harder to manage, maintain, and extend them. For example, adding a new pattern requires declaring a new constant and ensuring it aligns with existing patterns, which can lead to inconsistencies or errors. Additionally, the getNested method directly accepts Pattern objects, which reduces type safety and makes the code less intuitive when referencing patterns.
Describe the solution you'd like
We propose refactoring the pattern management in ReplicaTopologyProvider by introducing an InfoPatterns enum to encapsulate all regular expression patterns. The enum will group related patterns (e.g., ROLE, SLAVE, MASTER_HOST, etc.) in a single, cohesive structure, improving code organization and readability. Each enum constant will hold a compiled Pattern and provide a matcher method to streamline pattern usage.
The getNested method will be updated to accept an InfoPatterns enum instead of a raw Pattern, enhancing type safety and making the code more self-documenting. For example:
Improved organization: Patterns are grouped in a single enum, making it easier to locate and manage them.
Type safety: Using InfoPatterns instead of raw Pattern objects reduces the risk of passing incorrect patterns.
Readability: Referencing patterns like InfoPatterns.ROLE is more intuitive than ROLE_PATTERN.
Extensibility: Adding new patterns is as simple as adding a new enum constant.
Drawbacks:
Minor increase in code complexity due to the introduction of an enum.
Negligible performance overhead from enum initialization, which is mitigated since patterns are compiled only once.
Describe alternatives you've considered
Keep individual Pattern constants: Continue using static final Pattern constants as in the current implementation. This avoids introducing new constructs but retains the issues of scattered pattern definitions and lower type safety.
Use a Map or class to group patterns: Instead of an enum, patterns could be stored in a Map<String, Pattern> or a dedicated class. However, this approach is less type-safe than an enum and requires additional boilerplate for pattern access.
Inline patterns: Define patterns directly in methods where they are used. This would eliminate constants but make the code harder to maintain and less reusable.
The enum approach was chosen for its balance of type safety, readability, and maintainability.
Usage:
Developers using ReplicaTopologyProvider will not notice functional changes, as the public API (getNodes and getNodesAsync) remains unchanged. Internally, the class now uses InfoPatterns for pattern management, which simplifies maintenance for contributors.
Migration Strategy:
The change is backward-compatible, as it only affects the internal implementation of ReplicaTopologyProvider.
Replace all static final Pattern constants (ROLE_PATTERN, SLAVE_PATTERN, etc.) with the InfoPatterns enum.
Update the getNested method to accept InfoPatterns instead of Pattern.
Update all pattern references in the code to use InfoPatterns (e.g., InfoPatterns.ROLE.matcher(info) instead of ROLE_PATTERN.matcher(info)).
No user-facing changes are required, and existing tests should pass without modification.
Testing:
Existing tests for ReplicaTopologyProvider should cover the functionality, as the logic remains unchanged. Add new unit tests to verify that InfoPatterns correctly compiles and matches patterns for various INFO REPLICATION outputs.
The text was updated successfully, but these errors were encountered:
Feature Request
Is your feature request related to a problem? Please describe
The current implementation of
ReplicaTopologyProvider
defines regular expression patterns as individualstatic final Pattern
constants at the class level (e.g.,ROLE_PATTERN
,SLAVE_PATTERN
, etc.). This approach scatters related patterns across the codebase, making it harder to manage, maintain, and extend them. For example, adding a new pattern requires declaring a new constant and ensuring it aligns with existing patterns, which can lead to inconsistencies or errors. Additionally, thegetNested
method directly acceptsPattern
objects, which reduces type safety and makes the code less intuitive when referencing patterns.Describe the solution you'd like
We propose refactoring the pattern management in
ReplicaTopologyProvider
by introducing anInfoPatterns
enum to encapsulate all regular expression patterns. Theenum
will group related patterns (e.g.,ROLE
,SLAVE
,MASTER_HOST
, etc.) in a single, cohesive structure, improving code organization and readability. Each enum constant will hold a compiledPattern
and provide amatcher
method to streamline pattern usage.The
getNested
method will be updated to accept anInfoPatterns
enum instead of a rawPattern
, enhancing type safety and making the code more self-documenting. For example:Benefits:
enum
, making it easier to locate and manage them.InfoPatterns
instead of rawPattern
objects reduces the risk of passing incorrect patterns.InfoPatterns.ROLE
is more intuitive thanROLE_PATTERN
.Drawbacks:
enum
.Describe alternatives you've considered
Pattern
constants: Continue usingstatic final Pattern
constants as in the current implementation. This avoids introducing new constructs but retains the issues of scattered pattern definitions and lower type safety.Map
orclass
to group patterns: Instead of anenum
, patterns could be stored in aMap<String, Pattern>
or a dedicated class. However, this approach is less type-safe than anenum
and requires additional boilerplate for pattern access.The
enum
approach was chosen for its balance of type safety, readability, and maintainability.Teachability, Documentation, Adoption, Migration Strategy
Usage:
Developers using
ReplicaTopologyProvider
will not notice functional changes, as the public API (getNodes
andgetNodesAsync
) remains unchanged. Internally, the class now usesInfoPatterns
for pattern management, which simplifies maintenance for contributors.Migration Strategy:
ReplicaTopologyProvider
.static final Pattern
constants (ROLE_PATTERN
,SLAVE_PATTERN
, etc.) with theInfoPatterns
enum.getNested
method to acceptInfoPatterns
instead ofPattern
.InfoPatterns
(e.g.,InfoPatterns.ROLE.matcher(info)
instead ofROLE_PATTERN.matcher(info)
).Testing:
Existing tests for
ReplicaTopologyProvider
should cover the functionality, as the logic remains unchanged. Add new unit tests to verify thatInfoPatterns
correctly compiles and matches patterns for variousINFO REPLICATION
outputs.The text was updated successfully, but these errors were encountered: