Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix HCLRevision mismatch on zero logical clock #2122

Merged
merged 4 commits into from
Nov 6, 2024
Merged

Conversation

vroldanbet
Copy link
Contributor

@vroldanbet vroldanbet commented Nov 6, 2024

While working on #2120, I detected something I wasn't expecting: when comparing the revision out of the datastore write, and what came out of Watch API, the revisions were different.
We use SHOW COMMIT TIMESTAMP to retrieve a CRDB transaction timestamp. The value coming out of the update field in change streams had the same value, but the difference was the notation: one included the logical clock, the other didn't, but logical clocks were the same (zero):

  • revision generated out of the transaction in readTransactionCommitRev uses NewForHLC(hlcNow), which takes a Decimal. This in turn calls decimal.String(), which returns a number stripped out of the decimal part if it's zero.
  • revision obtained out of the changefeeds uses revisions.HLCRevisionFromString(details.Updated), and details.Updated always comes with the decimal part, even when it's zero

Both timestamps were the same, but had a different string representation, and led to a different HCLRevision logical lock, hence .Equal() method failing.

The problem originates in the use of the offset to represent the logical part of the clock. The representation of "zero" wasn't being used consistently, and in some parts of the code the revision was initialized with logical clock of zero, when it needed the offset padding.

⚠️ One of the consequences of this change is that moving forward string representation of the revision will be longer as it will always include the logical clock (when using HLCRevision, that is, which only applies to CRDB). This means caches that append the revision to keys will be longer.

@vroldanbet vroldanbet self-assigned this Nov 6, 2024
@github-actions github-actions bot added area/datastore Affects the storage system area/tooling Affects the dev or user toolchain (e.g. tests, ci, build tools) labels Nov 6, 2024
@vroldanbet vroldanbet force-pushed the hcl-zero-fix branch 3 times, most recently from a8a0b39 to da8f405 Compare November 6, 2024 17:41
While developing the test-case for `EmitImmediatelyStrategy` with CRDB,
I detected something I wasn't expecting: when comparing the revision out of the
datastore write, and what came out of Watch API, the revisions were different.

We use `SHOW COMMIT TIMESTAMP` to retrieve a CRDB [transaction timestamp]
(cockroachdb/cockroach#80848). The value coming out of
the `update` field in change streams had the same value, but the difference was
the notation: one included the logical clock, the other didn't, but logical
clocks were the same (zero):

- revision generated out of the transaction in `readTransactionCommitRev` uses
  `NewForHLC(hlcNow)`, which takes a `Decimal`. This in turn calls
  `decimal.String()`, which returns a number stripped out of the decimal part if
  it's zero.
- revision obtained out of the changefeeds uses `revisions.HLCRevisionFromString
  (details.Updated)`, and `details.Updated` always comes with the decimal part,
  even when it's zero

Both timestamps were the same, but had a different string representation,
and led to a different `HCLRevision` logical lock,
hence `.Equal()` method failing.
@vroldanbet vroldanbet marked this pull request as ready for review November 6, 2024 18:42
@vroldanbet vroldanbet requested a review from a team as a code owner November 6, 2024 18:42
Copy link
Member

@josephschorr josephschorr left a comment

Choose a reason for hiding this comment

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

LGTM

@vroldanbet vroldanbet added this pull request to the merge queue Nov 6, 2024
Merged via the queue into main with commit 554777d Nov 6, 2024
22 checks passed
@vroldanbet vroldanbet deleted the hcl-zero-fix branch November 6, 2024 19:33
@github-actions github-actions bot locked and limited conversation to collaborators Nov 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area/datastore Affects the storage system area/tooling Affects the dev or user toolchain (e.g. tests, ci, build tools)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants