pulsar icon indicating copy to clipboard operation
pulsar copied to clipboard

[cleanup][broker] Standardize java.time.Clock usage in broker service

Open Praveenkumar76 opened this issue 6 months ago • 0 comments

Fixes apache#25660

Motivation

The usage of java.time.Clock is currently inconsistent across the broker module, with many classes directly relying on System.currentTimeMillis() for timestamp generation.

Standardizing timestamp handling through the centralized PulsarService clock improves consistency across the codebase and makes time-dependent behavior easier to control during testing. This reduces test flakiness by enabling deterministic time mocking in unit and integration tests.

Because there are hundreds of occurrences of System.currentTimeMillis() across the repository, this PR intentionally scopes the change to a tightly focused set of classes. It specifically targets classes where the Clock dependency can be cleanly injected via the constructor, ensuring a clear separation of concerns and keeping the review safe and manageable.

Modifications

Replaced direct usages of System.currentTimeMillis() with this.clock.millis() by utilizing field injection for the Clock dependency in the following broker module classes:

  • MessageDeduplication.java
  • SchemaRegistryStats.java
  • PulsarStats.java

Additional changes:

  • Left existing System.nanoTime() usages unchanged, as they are used for monotonic latency measurements rather than wall-clock timestamps.

Verifying this change

This change can be verified as follows:

  • Confirmed successful compilation and that existing broker tests continue to pass.
  • Verified timestamp-related logic in the modified classes now consistently uses the centralized broker clock via constructor injection.
  • Confirmed no behavioral changes in latency-related code paths using System.nanoTime().

Does this pull request potentially affect one of the following parts:

  • [ ] Dependencies (add or upgrade a dependency)
  • [ ] The public API
  • [ ] The schema
  • [ ] The default values of configurations
  • [ ] The threading model
  • [ ] The binary protocol
  • [ ] The REST endpoints
  • [ ] The admin CLI options
  • [ ] The metrics
  • [ ] Anything that affects deployment

Praveenkumar76 avatar Jun 04 '26 17:06 Praveenkumar76