Usage of java.time.Clock for timestamps isn't consistently performed in Pulsar code base
In some of the existing code locations, PulsarService's java.time.Clock clock field is used for timestamps, however it's not consistently applied in the code base. Would it be useful to use java.time.Clock for timestamps?
Originally posted by @lhotari in https://github.com/apache/pulsar/pull/25658#discussion_r3178499222
Hi @lhotari , I wanted to help out with #25660. Since there are hundreds of instances of System.currentTimeMillis() across the entire codebase, I thought it would be safest to scope this first PR strictly to a few files in the broker module to keep the review manageable and limit.
If this approach looks good to you, I can open follow-up PRs for the other modules (like client, connectors, etc.) in smaller batches. Let me know if you prefer a different strategy!
If this approach looks good to you, I can open follow-up PRs for the other modules (like client, connectors, etc.) in smaller batches. Let me know if you prefer a different strategy!
I'm sorry that the issue description isn't clear. It's an observation that I made earlier.
Some thoughts:
It's better to first research and create a plan before going forward. As your draft PR already mentions in the description, the main reason for doing this is to be able to write tests that wouldn't be tied to the real wall clock and could use a mock clock that could be controlled by the test.
In the research (that an AI agent could perform), it would be good to find out why the current code base contains this mixed usage of java.time.Clock.millis() and System.currentTimeMillis() and what tests currently rely on the Clock.
In addition, the research should look into current time sensitive tests (message expiry?) that would benefit of having a consistent usage of java.time.Clock across the code base.
One minor detail about the existing PR #25935. When locating the Clock instance, it's better to store the instance in a field instead of having a long chain of calls for each usage such as subscription.getTopic().getBrokerService().getPulsar().getClock().millis(). One of the benefits is that in unit tests, the class doesn't need to mock all levels to be able to inject a mock Clock instance.
In the Pulsar code base, we haven't paid sufficient attention to have the code base structured as "units" which could be tested with unit tests. Most tests in Pulsar aren't really unit tests since they require a large amount of mocked dependencies or a complete mocked Pulsar broker to run. The problem that this causes is that boundaries of "units" fade out and in the code some parts become very coupled without a clear separation of concerns. In Pulsar code base, the lack of clear definition of a concurrency model brings in yet another challenge.
Summary: This issue is just a tip of the iceberg of improving testability and code quality in the Pulsar code base. We can first focus on the java.time.Clock research part. Would you like to perform that? You could also store the research results in https://gist.github.com/ as markdown with possible mermaid diagrams and reference that in a comment on this issue. (Gists can only hold text files. When updating the files in a gist, it's possible to checkout the gist as a git repository locally and push updates as git commits. )
Yeah, that long method chain making the tests brittle, I'm also definitely felt that pain while trying to write the mocks for this draft PR! I would to take on the research part. I will pause the code changes on this PR, dive into the codebase to map out the current Clock usages and time-sensitive tests, and put together a markdown report in a Gist for us to review.
Summary: This issue is just a tip of the iceberg of improving testability and code quality in the Pulsar code base. We can first focus on the
java.time.Clockresearch part. Would you like to perform that? You could also store the research results in https://gist.github.com/ as markdown with possible mermaid diagrams and reference that in a comment on this issue. (Gists can only hold text files. When updating the files in a gist, it's possible to checkout the gist as a git repository locally and push updates as git commits. )
Hi @lhotari ! I spent time digging into the codebase to perform the java.time.Clock research.
I have documented the full findings and architectural proposal here: gist link
The research highlighted exactly what you suspected:
There are over 180 instances of Thread.sleep() in the broker test suite.
Tests like PersistentMessageExpiryMonitorTest are currently forced to halt CI execution completely (e.g., sleeping for 2 real seconds) just to verify a 1-second TTL rule. Standardizing the clock will completely eliminate these blocking sleeps.
Based on the Concurrency guidelines and your feedback regarding the boundaries of "units," I also updated my draft PR #25935.
I reverted the changes to the deeply coupled classes (like Consumer) to avoid massive dependency chains. Instead, I scoped the PR strictly to 3 classes (MessageDeduplication, SchemaRegistryStats, PulsarStats) where the Clock can be cleanly provided via constructor injection, adhering to safe publication and strict unit isolation.
Let me know if this research aligns with your vision, and if those 3 refactored files represent the correct architectural pattern for moving forward!
For the record: the follow-up PR #25935 was closed without merging, so this is still open. The research gist linked above is the starting point; per the earlier review, the clock should be injected and kept in a field (not reached through getBrokerService().getPulsar().getClock() chains) so that time-sensitive tests such as PersistentMessageExpiryMonitorTest can use a controllable clock.