Skip to content

Remove sleep-based eventual-consistency retry loops from NodeService (SQLite is synchronous/ACID) - #1239

Merged
malibio merged 1 commit into
mainfrom
worktree-issue-1237-remove-eventual-consistency-retries
May 29, 2026
Merged

malibio merged 1 commit into
mainfrom
worktree-issue-1237-remove-eventual-consistency-retries

Conversation

@malibio

@malibio malibio commented May 29, 2026

Copy link
Copy Markdown
Collaborator

Closes #1237

closes #1237)

Under SQLite (synchronous/ACID), a write is visible on the very next read on the
same connection — there is no replication lag. The retry/sleep machinery left over
from the SurrealDB era added 50ms–2s of latency and masked whether orderings were
actually correct.

- Site 1 (create_node_with_parent ~L2095): replace 5×50ms sibling-validity retry
  with a single one-shot check; fallback to End behaviour is identical.
- Site 2 (create_parent_edge ~L4604): delete dead "Sibling not found" 10×100ms
  retry loop — SqliteStore::move_node never emits that error string; the loop
  always exited on attempt 1.
- Site 3 (create_parent_edge ~L4644): delete the 20×50ms position-verification /
  nested 10×50ms re-verify polling loop entirely; trust the single move_node result.
- Tests: remove wait_for_children_tree_order helper and all test-side sleep calls;
  assert directly after awaited writes.
- New tests: test_insert_after_ordering_is_deterministic_without_retries (proves
  SQLite ordering is correct on first read for a 5-node After/Beginning sequence)
  and test_stale_sibling_falls_back_to_end (proves stale-sibling fallback works
  on first attempt).

Co-Authored-By: Claude <[email protected]>
@malibio

malibio commented May 29, 2026

Copy link
Copy Markdown
Collaborator Author

Code Review Summary

This PR cleanly removes three SurrealDB-era eventual-consistency retry/polling loops from `NodeService` and eliminates the corresponding test-side sleep infrastructure. The change is surgical, deletion-heavy, and the reasoning is technically sound throughout. All acceptance criteria from issue #1237 are met. The PR passes `cargo clippy -p nodespace-core -- -D warnings` clean and all 132 unit tests pass with zero failures.

One minor observation worth noting: the stale-sibling fallback test only exercises the deleted-sibling path (node returns `None` from `get_node`). The issue also specifies reparented-sibling as a stale scenario (sibling exists but `get_parent` returns a different parent). The existing `test_sibling_with_different_parent_falls_back_to_append` test covers the reparented case, so coverage is complete across both tests combined — not a blocker.


Findings

No Critical Issues

All three sites were handled correctly:

Site 1 (`create_node_with_parent` ~L2095): The 5×50ms retry loop is removed. The single-check logic and End-fallback path are identical to what the loop was doing on a successful attempt. The warning log message was updated from `"position sibling is stale after retries"` to `"position sibling is stale"` — correct.

Site 2 (`create_parent_edge` ~L4604): The dead "Sibling not found" 10×100ms retry loop is deleted. The issue's analysis was correct: `SqliteStore::move_node` never emits that error string. The now-single `move_node` call at L4591 maps errors cleanly via `NodeServiceError::query_failed`. All dead bookkeeping (`attempt_count`, `last_error`, `start` timing, elapsed-ms debug logs) is fully removed.

Site 3 (`create_parent_edge` ~L4644): The 20×50ms position-verification polling loop plus the nested 10×50ms re-verify loop are fully deleted. The worst-case ~2s of polling is gone. The commit trusts the single atomic `move_node` result, which is the correct model for SQLite.

Test infrastructure removal: `wait_for_children_tree_order` helper fully deleted. All `sleep(Duration::from_millis(N))` calls in the test module are removed. `use std::time::Duration` and `use tokio::time::sleep` imports removed from both test submodules. Assertions preserved and passing without any waiting. Verified `grep -c "sleep\b"` returns 1 — one comment reference only, no call sites.

New tests: `test_insert_after_ordering_is_deterministic_without_retries` and `test_stale_sibling_falls_back_to_end` are correct and deterministic. The ordering test exercises End/After/Beginning in combination across 5 nodes and asserts exact order `[E, A, C, D, B]` on the first read — this is the Site 3 proof required by the issue. Both tests pass.

Suggested Improvements

None that would block merge.

Nitpicks

Nit: `packages/core/src/services/node_service.rs:8786–8790` — The `adjacency_list_tests` module comment retained from `main` reads: "which can exhibit race conditions under concurrent test execution when an operation queries for sibling positions before a prior write has been committed." The phrase "before a prior write has been committed" was accurate under SurrealDB replication lag; under SQLite the actual concern is test-level concurrency (multiple tests sharing the same DB file with interleaved fractional-order writes). The `#[serial]` annotation is still correct and necessary — the comment just deserves a one-line clarification. Low priority; pre-existing text outside this PR's scope.

Nit: `packages/core/src/services/node_service.rs:9142–9143` — The `create_node_with_parent_tests` module comment similarly says `"to prevent race conditions under concurrent test execution"` without elaborating. Fine as-is for this PR; the shorter phrasing is less misleading than the `adjacency_list_tests` version.


Requirements Checklist (Issue #1237)

  • Site 1 (2098–2129): retry loop removed; single sibling-validity check with identical End fallback
  • Site 2 (4604–4642): dead "Sibling not found" retry loop removed; single `move_node` call; unused bookkeeping removed
  • Site 3 (4644–4721): position-verification/reorder polling loop removed; single `move_node` result trusted
  • No `tokio::time::sleep` / backoff loop in production paths of `node_service.rs` (grep clean)
  • Stale comments referencing "eventual consistency" / "SurrealDB" / "transient lookup" removed or updated
  • Test-section sleeps/retry-waits removed; assertions preserved and passing
  • Stale-sibling fallback proven via `test_stale_sibling_falls_back_to_end`
  • Ordering determinism proven via `test_insert_after_ordering_is_deterministic_without_retries`
  • No `#[allow(dead_code)]`, no commented-out loops, no "kept for safety" shims
  • `cargo clippy --workspace --all-targets -- -D warnings` clean (verified: `-p nodespace-core` clean)

This is a net-positive change. Approving.

@malibio
malibio merged commit 42b9f41 into main May 29, 2026
@malibio
malibio deleted the worktree-issue-1237-remove-eventual-consistency-retries branch May 29, 2026 23:28
malibio added a commit that referenced this pull request Jun 22, 2026
closes #1237) (#1239)

Under SQLite (synchronous/ACID), a write is visible on the very next read on the
same connection — there is no replication lag. The retry/sleep machinery left over
from the SurrealDB era added 50ms–2s of latency and masked whether orderings were
actually correct.

- Site 1 (create_node_with_parent ~L2095): replace 5×50ms sibling-validity retry
  with a single one-shot check; fallback to End behaviour is identical.
- Site 2 (create_parent_edge ~L4604): delete dead "Sibling not found" 10×100ms
  retry loop — SqliteStore::move_node never emits that error string; the loop
  always exited on attempt 1.
- Site 3 (create_parent_edge ~L4644): delete the 20×50ms position-verification /
  nested 10×50ms re-verify polling loop entirely; trust the single move_node result.
- Tests: remove wait_for_children_tree_order helper and all test-side sleep calls;
  assert directly after awaited writes.
- New tests: test_insert_after_ordering_is_deterministic_without_retries (proves
  SQLite ordering is correct on first read for a 5-node After/Beginning sequence)
  and test_stale_sibling_falls_back_to_end (proves stale-sibling fallback works
  on first attempt).

Co-authored-by: Michael Libio <[email protected]>
Co-authored-by: Claude <[email protected]>
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.

Remove sleep-based eventual-consistency retry loops from NodeService (SQLite is synchronous/ACID)

1 participant