Skip to content

MINOR: Prefer MetadataRecordSerde.INSTANCE over new instances#22289

Merged
chia7712 merged 1 commit into
apache:trunkfrom
JiayaoS:fix/unitTest-RaftClusterSnapshotTest-INSTANCE
May 16, 2026
Merged

MINOR: Prefer MetadataRecordSerde.INSTANCE over new instances#22289
chia7712 merged 1 commit into
apache:trunkfrom
JiayaoS:fix/unitTest-RaftClusterSnapshotTest-INSTANCE

Conversation

@JiayaoS
Copy link
Copy Markdown
Contributor

@JiayaoS JiayaoS commented May 15, 2026

Replace new MetadataRecordSerde() with MetadataRecordSerde.INSTANCE
across the codebase. MetadataRecordSerde is stateless and thread-safe,
so a single shared instance is sufficient.

Also add Javadoc on the public constructor to guide future callers
toward the singleton.

Relevant discussions:

Verified that MetadataRecordSerde (and its parent
AbstractApiMessageSerde) has no mutable instance fields. All methods
(read/write/recordSize) operate solely on their parameters and local
variables, confirming no thread-safety or state-recycling concerns with
sharing a single instance.

Reviewers: Chia-Ping Tsai chia7712@gmail.com

@github-actions github-actions Bot added triage PRs from the community core Kafka Broker tests Test fixes (including flaky tests) small Small PRs labels May 15, 2026
try (var snapshot = RecordsSnapshotReader.of(
raftManager.raftLog().latestSnapshot().get(),
new MetadataRecordSerde(),
MetadataRecordSerde.INSTANCE,
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.

Would you mind checking the code base?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I will check all similar code.

@JiayaoS JiayaoS changed the title MINOR: Use MetadataRecordSerde.INSTANCE in RaftClusterSnapshotTest MINOR: Prefer MetadataRecordSerde.INSTANCE over new instances May 15, 2026
@github-actions github-actions Bot removed the triage PRs from the community label May 15, 2026

class MetadataRecordSerdeTest {

private static final MetadataRecordSerde SERDE = MetadataRecordSerde.INSTANCE;
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.

Could you remove SERDE and use MetadataRecordSerde.INSTANCE instead?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done. Should I change all similar calls to MetadataRecordSerde.INSTANCE?
ApiMessageAndVersion messageAndVersion = SERDE.read(accessor, record.valueSize());

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.

yes. It is already a static variable :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Replaced all direct instantiations of MetadataRecordSerde with
MetadataRecordSerde.INSTANCE, except where the serde is passed in
as a constructor or method parameter (e.g. KafkaRaftManager, BatchAccumulator).

@JiayaoS JiayaoS force-pushed the fix/unitTest-RaftClusterSnapshotTest-INSTANCE branch from 36321ab to 7d8a956 Compare May 15, 2026 20:35
@JiayaoS JiayaoS force-pushed the fix/unitTest-RaftClusterSnapshotTest-INSTANCE branch from 7d8a956 to 2f908f7 Compare May 15, 2026 20:36
Copy link
Copy Markdown
Member

@chia7712 chia7712 left a comment

Choose a reason for hiding this comment

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

LGTM

@chia7712 chia7712 merged commit 9dfc5a7 into apache:trunk May 16, 2026
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci-approved core Kafka Broker kraft small Small PRs tests Test fixes (including flaky tests) tools

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants