Skip to content

fix: mtree table-diff drops rows in the open-ended tail leaf#122

Merged
mason-sharp merged 2 commits into
mainfrom
ace-189
Jun 5, 2026
Merged

fix: mtree table-diff drops rows in the open-ended tail leaf#122
mason-sharp merged 2 commits into
mainfrom
ace-189

Conversation

@danolivo

@danolivo danolivo commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

Summary

ace mtree table-diff silently dropped any row on the non-reference node whose pkey sat past the reference's last_row. GeneratePkeyOffsetsQuery emits a final leaf with range_end = NULL, and getPkeyBatches ignored NULL bounds when building work-item slices — the open-ended tail produced no query. With n1=[id=1], n2=[id=2] (the bug report) the diff returned n1=[{id:1}], n2=[]; table-repair then deleted n1's row instead of converging.

Fix

getPkeyBatches now tracks hasOpenStart / hasOpenEnd and appends {nil, firstBoundary} or {lastBoundary, nil} on the open side(s); the fully-unbounded edge case emits {nil, nil}. processWorkItem already treats a nil bound as "no clause for this side", so no caller changes. The single-point {b, b} slice is skipped when an open-side slice would subsume it.

GeneratePkeyOffsetsQuery always emits a final leaf whose range_end is
NULL — LEAD() over the trailing seq=2 row returns NULL — so the
"open-ended tail" leaf is the common case, not a corner one.
getPkeyBatches collected only non-NULL boundaries when building slices,
so the open-ended tail contributed no slice, and any row on the
non-reference node whose pkey sat beyond the reference's last_row was
never queried by processWorkItem.

The bug surfaces whenever a non-reference node holds rows outside the
reference's pkey envelope. In the original report (n1=[id=1],
n2=[id=2], both nodes 1 row each → n1 wins reference on tied counts)
the diff JSON reports n1=[{id:1}] and n2=[]; id=2 falls into the
[1, NULL) tail and is dropped. table-repair then deletes n1's row
instead of converging both nodes.

getPkeyBatches now tracks hasOpenStart / hasOpenEnd while collecting
finite boundaries and appends an extra {nil, firstBoundary} or
{lastBoundary, nil} slice on the open side(s); for the degenerate case
where every mismatched leaf is fully unbounded it emits a single
{nil, nil} fully-open slice. processWorkItem already treats nil bounds
as "no clause for this side", so the resulting SQL becomes
WHERE pkey >= lastBoundary (or <= firstBoundary), or WHERE TRUE for the
fully-open form, and finally sees the previously-invisible rows.

TestMerkleTreeBidirectionalDiff pins three scenarios that distinguish
this from a generic "diff is unidirectional" bug:

* ExactReproducer (n1=[1], n2=[2]) mirrors the report verbatim.
* N2HasRowsAboveN1Max stacks n2 extras above n1 max with n1 still the
  reference; the open-ended-tail theory predicts n2 extras vanish from
  the diff, which the pre-fix run confirmed exactly.
* N1HasRowAboveN2Max flips row counts so n2 becomes the reference; the
  same bug surfaces mirrored on the n1 side, ruling out alternative
  explanations that do not depend on the reference role.

The test also documents a setup gotcha learned while writing it:
spock.repair_mode(true) does NOT suppress replication for tables
freshly added to a Spock replication set in this environment. Future
mtree tests that need divergent per-node data should omit
spock.repset_add_table rather than rely on repair_mode.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jun 5, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@danolivo, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 39 minutes and 7 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 611eaef1-4b76-4b65-9a3e-5a9485d05498

📥 Commits

Reviewing files that changed from the base of the PR and between 73c3ccd and c9f0682.

📒 Files selected for processing (1)
  • internal/consistency/mtree/merkle.go
📝 Walkthrough

Walkthrough

The PR fixes a merkle tree batch generation bug where open-ended leaf ranges (with NULL start or end boundaries) were not generating correct query coverage. The core fix reworks getPkeyBatches to detect open-sided ranges and generate explicit open-ended slices. A comprehensive integration test validates the fix across three divergence scenarios.

Changes

Merkle Tree Open-Ended Range Fix

Layer / File(s) Summary
Open-ended range detection and slice construction
internal/consistency/mtree/merkle.go
Boundary collection now tracks hasOpenStart and hasOpenEnd flags separately when RangeStart or RangeEnd is nil. Slice construction switches on sortedBoundaries length to conditionally create closed or open-ended slices. Explicit slices [nil, firstBoundary] and [lastBoundary, nil] are appended, and the function returns early with an empty batch set if no slices remain.
Integration test with helper for bidirectional diff validation
tests/integration/merkle_tree_test.go
TestMerkleTreeBidirectionalDiff reproduces the asymmetric diff bug across three seeded scenarios by creating tables on both nodes, truncating, seeding divergent rows, running merkle tree operations, and asserting diff-only row ID sets match expected values. Helper extractDiffIDs collects, converts, sorts, and returns row IDs from diff results for deterministic comparison. Import sort added to support ordering.

Poem

🐰 Hop-skip through those open ends,
Boundaries boundless, range extends,
Nil becomes a slice, neat and true,
Diff shines bright when sorted through!
Tests confirm our fix will hold,
Merkle trees now complete, bold! 🌳

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the main fix: mtree table-diff silently dropping rows in open-ended tail leaves, which matches the core issue addressed in the changeset.
Description check ✅ Passed The description provides clear context about the bug (dropped rows in open-ended tail), the root cause (NULL bounds ignored), an example scenario, and explains the fix (tracking open-side bounds and appending appropriate slices).
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch ace-189

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codacy-production

codacy-production Bot commented Jun 5, 2026

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 0 duplication

Metric Results
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@internal/consistency/mtree/merkle.go`:
- Around line 2198-2210: The loop that builds boundaries should treat "open"
bounds not only when RangeStart/RangeEnd is nil but also when they are empty
slices or slices containing only nils (as handled earlier at line ~1817); update
the loop in merkle.go that iterates allRanges to normalize each bound before
using it: for each r, determine isOpenStart/isOpenEnd by checking (r.RangeStart
== nil) OR (len(r.RangeStart) == 0) OR (allNil(r.RangeStart)) and likewise for
r.RangeEnd, set hasOpenStart/hasOpenEnd when open, otherwise append the
normalized bound to boundaries; reuse the existing allNil helper (or implement a
small check) so you don't append bogus []any{nil} entries.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 7e2e73b9-07ce-42b1-8161-82469c5119e1

📥 Commits

Reviewing files that changed from the base of the PR and between 4f1aeb5 and 73c3ccd.

📒 Files selected for processing (2)
  • internal/consistency/mtree/merkle.go
  • tests/integration/merkle_tree_test.go

Comment thread internal/consistency/mtree/merkle.go
Follow-up on bb94210. Two small cleanups:

* When sortedBoundaries has exactly one element AND either hasOpenStart
  or hasOpenEnd, the single-point {b, b} slice is fully subsumed by the
  open-side slice appended just below ({nil, b} or {b, nil}). The
  redundant slice spawned a second work item that queried the same row
  on both nodes; addRowToDiff masks the duplication in the output but
  the extra round-trip per worker is pure waste. Gate the case-1 emit
  on neither side being open.

* Trim the verbose explanatory comments to a single tight block. The
  commit body of bb94210 already documents the mechanism end-to-end;
  inline prose only needs to flag the non-obvious why. Also removes a
  copy-paste typo in the deleted open-side comment ("firstBoundary"
  used twice where the open-end half should have read "lastBoundary").

No behaviour change in the test suite: mtree integration (simple PK,
composite PK, --until, bidirectional) still all green.
@mason-sharp mason-sharp merged commit df5b29a into main Jun 5, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants