fix: use millisecond precision for dataEntry metadata ETag - #24046
Conversation
… collisions The dataEntry/metadata ETag was hashed from a last-modified timestamp truncated to whole seconds (DateUtils.toLongDate). When a metadata change landed in the same wall-clock second as a previous fetch, the ETag was unchanged, so clients could be served a stale 304 Not Modified. This also intermittently broke CI: DataSetMetadataTest. dataSetMetadataEtagFunctionalityTest asserts the ETag changes after a dataset is created, which collided whenever the create happened in the same second as the prior read (~25-30% of master api-test runs). Switch to DateUtils.toLongDateWithMillis so the timestamp keeps millisecond precision. The MD5 hash length is unchanged. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Update the expected hash in testGetEtag to match the millisecond-precision input, and add testGetEtagDistinguishesSubSecondChanges to lock in that two timestamps one millisecond apart now produce different ETags. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests.
Additional details and impacted files@@ Coverage Diff @@
## master #24046 +/- ##
=============================================
- Coverage 69.04% 56.96% -12.09%
+ Complexity 709 555 -154
=============================================
Files 3684 3684
Lines 141646 141620 -26
Branches 16453 16452 -1
=============================================
- Hits 97797 80668 -17129
- Misses 36243 53524 +17281
+ Partials 7606 7428 -178
Flags with carried forward coverage won't be shown. Click here to find out more.
... and 986 files with indirect coverage changes Continue to review full report in Codecov by Sentry.
🚀 New features to boost your workflow:
|
jbee
left a comment
There was a problem hiding this comment.
I feel this recognizes the issue and then applies the wrong fix. The cause was to not use the timestamp as it nature of being a number but to use some formatting which just makes is unnecessary complicated and error prone. So I suggest the fix is to use timestamp as numbers, not format the number differently.
|
Do you mean @jbee to not use the md5 hash to generate the etag string? Because the main change with the timestamp here is not that we format it differently, but add more granular information by adding milisecs in addition to seconds, since seconds where to rough to pass the test. In theory, this fix leaves the test still flaky, but to be able to trigger that failure we would need a significantly faster execution time of the tested code |
|
@stian-sandvold I am saying this String value =
String.format("%s-%s", DateUtils.toLongDateWithMillis(lastModified), user.getUid());could and should just be String value = String.format("%d-%s", lastModified, user.getUid()); |
Co-authored-by: Stian Sandvold <stian@dhis2.org>
3c76cce to
6970795
Compare
Co-authored-by: Stian Sandvold <stian@dhis2.org>
|



What
The
GET /api/dataEntry/metadataETag is built from the latestlastUpdatedtimestamp across the relevant metadata types, hashed together with the user UID. That timestamp was formatted withDateUtils.toLongDate, which truncates to whole seconds. This bumps it toDateUtils.toLongDateWithMillisso sub-second changes are reflected in the ETag.Why
With second-level precision, two distinct metadata states that happen within the same wall-clock second produce the same ETag. Consequences:
304 Not Modifiedand miss the update until the next change.DataSetMetadataTest.dataSetMetadataEtagFunctionalityTestcreates a dataset and asserts the ETag changed (line 103). When the create lands in the same second as the preceding read, the ETags collide and the assertion fails. This has been intermittently failing theRun api testsjob on master (the same test/assertion across multiple unrelated commits, ~25-30% of runs).Millisecond precision makes same-instant collisions astronomically unlikely and fixes both issues. The MD5 hash length is unchanged, so the existing 34-character ETag assertions still hold.
Testing
mvn -pl dhis-web-api spotless:checkpasses.dataSetMetadataEtagFunctionalityTestcontinues to cover the behaviour.🤖 Generated with Claude Code