Skip to content

Fixes #38016: keep the orphan sweep from failing a live reindex job while it initializes - #38093

Merged
mohityadav766 merged 3 commits into
mainfrom
fix/38016-orphan-sweep-live-reindex
Nov 18, 2026
Merged

mohityadav766 merged 3 commits into
mainfrom
fix/38016-orphan-sweep-live-reindex

Conversation

@mohityadav766

@mohityadav766 mohityadav766 commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

Describe your changes:

Fixes #38016

Detail's report still holds on main. OrphanJobMonitor runs JobRecoveryManager.performStartupRecovery() on every server every 2 minutes. While a coordinator builds a job's partitions:

  • it holds the reindex lock under a placeholder id, which hasLiveCoordinatorLock can't tie to the job;
  • nothing renews that lock (5-minute TTL) or the job's updatedAt until initializePartitions() returns. On a large catalog that step walks every entity table and can pass the sweep's 10-minute staleness threshold.

The sweep then calls failJob(), which takes no lock and writes FAILED unconditionally. It also cancels pending partitions and ends every running SearchIndexingApplication run with "Job abandoned due to server crash or shutdown". The coordinator carries on and writes READY over the FAILED row. It then either runs the job while the run record says failed (if the placeholder lock row is still there) or fails it itself at the lock transfer. Later changes didn't close this: #29547 made a live lock under the job's own id count as proof of life, but during initialization the lock isn't under that id.

What changed:

  • Executor (createJob): the lock moves onto the job as soon as the job row exists, and a renewal loop keeps the lock and the job's updatedAt fresh while partitions are built. The loop ends when initialization returns. execute() starts its own, and a run that fails before reaching execute() must not keep the lock alive. If the partitions fail to build, the job is marked FAILED with the cause instead of staying INITIALIZING. With a freshly renewed updatedAt, an INITIALIZING row would block new runs for ten minutes and then be failed as "abandoned".
  • Sweep (JobRecoveryManager): failing an orphan now takes the reindex lock first, as recovering or terminalizing one already did, so it never acts alongside another server's sweep or a running reindex. A job it can't lock waits for a later sweep. The FAILED write is a compare-and-swap on the status and updatedAt the sweep read. Partition cancellation, lock release and ending the app run happen only when it lands. Only app runs that started before the job was created are ended, so the run whose start triggered the sweep keeps running. Force-completing a job stuck in STOPPING uses the same compare-and-swap.
  • Coordinator: INITIALIZING → READY is conditional, so a job failed while its partitions were built stays failed.
  • DAO: failIfUnchanged, which takes the job state the sweep read and the record counts as records, and markReadyIfInitializing.
  • Recorded as ADR:2026-11-18-the-reindex-orphan-sweep-fails-a-job-only-under-its-lock-and-unchanged. DISTRIBUTED_INDEXING.md is updated to match.

Type of change:

  • Bug fix

High-level design:

  • Approach: give a live initializing coordinator the two liveness signals the sweep already trusts (a lock under the job's own id, and a fresh updatedAt), and make the sweep's FAILED write as careful as its recover/terminalize siblings (reindex lock plus compare-and-swap).
  • Alternatives considered:
    • Acquire the lock under the real job id before inserting the job row. That means passing the id into coordinator.createJob, and the renewal loop would have to tolerate a missing row. Moving the lock right after the insert gives the sweep the same signal with a smaller change, and nothing can sweep a job before its row exists.
    • Keep one renewal running from createJob() through execute(). That would also cover staged-index creation. But if the strategy fails before execute() (for example, staged index creation throws), nothing stops the executor, so the lock would be renewed forever and block every later reindex.
    • The lock alone, or the compare-and-swap alone. A sweep holding the lock can still act on what it read before taking it. Without the lock, two servers' sweeps can interleave a recover and a fail on the same job.
  • Rollout (mixed versions during an upgrade): new coordinators keep updatedAt fresh, so sweepers on the old code leave them alone. Coordinators on the old code renew nothing while they initialize, so their long initializations can still be swept until every server runs this code.
  • Unchanged (follow-up): staged-index creation between createJob() and execute() is still not renewed, and a failure there isn't recorded on the job. That job is READY, which the sweep recovers rather than fails, so new runs are refused until the job is an hour old and the sweep fails it. main behaves the same. A follow-up can fail the job with a READY → FAILED compare-and-swap when the strategy throws before execute().

Tests:

Use cases covered

  • A coordinator whose partition build outlasts the lock TTL keeps its job: the job's own lock and updatedAt are renewed while it builds.
  • A stale job whose reindex lock is held, unexpired, under an id the sweep can't tie to it is not failed.
  • A job whose coordinator stopped (lock expired) is still failed, with the lock taken first.
  • The sweep cancels no partitions and ends no app run for a job written after it read it. A STOPPING job that finished in the meantime keeps its outcome and its lock.
  • Failing an abandoned job ends only app runs that started before the job was created.
  • A job failed while its partitions were built is not marked READY.
  • A run whose partition build fails marks its job FAILED with the cause and frees the lock; a job something else already ended keeps its outcome.

Unit tests

  • I added unit tests for the new/changed logic.
  • Files: JobRecoveryManagerTest (5 new or rewritten tests, fail-path tests moved to the lock and the compare-and-swap), DistributedSearchIndexExecutorTest (3 new), DistributedSearchIndexCoordinatorTest (1 new, 1 updated).
  • testStartupRecovery_FailsJobWhenLockBelongsToDifferentJob asserted the old behaviour: fail an orphan while another holder has the reindex lock. It is now testStartupRecovery_LeavesOrphanForALaterSweepWhileTheReindexLockIsHeld.
  • mvn -B package -pl :openmetadata-service -am -Dtest=… at the pushed commit: 182 tests in DistributedSearchIndexCoordinatorTest, DistributedSearchIndexExecutorTest, JobRecoveryManagerTest, JobRecoveryOrphanDetectionTest, OrphanJobMonitorTest, DistributedJobParticipantTest and DistributedIndexingStrategyTest, 0 failures. The planner's focused unit step is the first three (130 tests).
  • Coverage: 76/78 changed executable lines covered (97%): JobRecoveryManager 35/35, DistributedSearchIndexCoordinator 10/10, DistributedSearchIndexExecutor 31/33. The two misses are the log in failJobStillInitializing's catch, reached only if reading the job throws while the build failure is already propagating. Measured with JaCoCo 0.8.10 on those three classes' unit tests; whole-class percentages mean little for 1,400-line classes.
  • Each fix is load-bearing. Putting back main's behaviour in one round (sweep fails without the lock; cleanup on a missed compare-and-swap; app runs ended up to now; READY written unconditionally; no renewal during initialization) fails exactly the 6 new or inverted tests and nothing else. One mutation per round for the rest: releasing the placeholder id instead of the job's lock, or leaving a failed build's job INITIALIZING, fails createJobFailsTheJobAndFreesItsLockWhenBuildingPartitionsFails; failing a job something else already ended fails createJobKeepsTheOutcomeOfAJobEndedWhileItsPartitionsWereBuilt.

Backend integration tests

  • Added SearchReindexOrphanSweepIT (@Isolated: it holds the cluster-wide reindex lock). Against the real search_index_job / search_reindex_lock tables it covers:
    • an orphan is left for a later sweep while another holder has the reindex lock, and failed once that lock has expired;
    • failIfUnchanged misses after a write;
    • markReadyIfInitializing moves an INITIALIZING job and leaves a FAILED one.
  • Ran it locally at 7a01214, MySQL + Elasticsearch, parallel lane (the planner's focused step: mvn -B verify -pl :openmetadata-integration-tests -am -Pmysql-elasticsearch -DintegrationTests.lane=parallel -Dit.test=SearchReindexOrphanSweepIT …): 5 tests, 0 failures. The merge queue runs the other engines. It also passes on the 2.0 backport, [2.0] Fixes #38016: keep the orphan sweep from failing a live reindex job while it initializes (#38093) #38120.
  • I called the real SearchIndexJobDAO through Jdbi, set up as JdbiUtils does, on throwaway MySQL 8.3 and PostgreSQL 15 containers with the migration's search_index_job DDL. On both engines:
    • markReadyIfInitializing moved 1 row, then 0;
    • failIfUnchanged moved 0 for a stale updatedAt or a wrong status, and 1 for the current read;
    • READY on the FAILED row moved 0;
    • the final row had the expected counts, timestamps and error, with stats cleared.

Ingestion integration tests

Not applicable.

Playwright (UI) tests

Not applicable.

Visual regression baselines

  • Not applicable (no rendering change).

Manual testing performed

  1. Traced the INITIALIZING path on main: createJob() → initializePartitions() → transferReindexLock(), the lock TTL and refresh start, isJobOrphaned() / hasLiveCoordinatorLock(), processOrphanedJob() → failJob(), and the unconditional update behind both.
  2. Called the real SearchIndexJobDAO through Jdbi on MySQL 8.3 and PostgreSQL 15 (results above).
  3. Put back main's behaviour at each fix and confirmed the matching unit test fails (results above).

UI screen recording:

Not applicable — backend-only fix.

Checklist:

  • I have read the CONTRIBUTING document.

  • My PR title is Fixes <issue-number>: <short explanation>

  • My PR is linked to a GitHub issue via Fixes #<issue-number> above.

  • I have commented on my code, particularly in hard-to-understand areas.

  • For JSON Schema changes: I updated the migration scripts or explained why it is not needed. (no schema change)

  • For a UI feature or feature task: as part of PR creation, I ran the PR build in Docker with sample data and embedded a verified GitHub-hosted recording plus build/health evidence above (or explained why this requirement is not applicable). (no UI change)

  • I have added tests (unit / integration / Playwright as applicable) and listed them above.

  • Self-review: I reviewed the final diff against REVIEW.md (the pr-checklist skill runs it) and fixed every 🔴 finding, or answered it on the line below.
    Reviewed at f7df8a7, in two passes. The first pass, at c2d9b2d, raised one 🔴 and two 🟡, all fixed in f7df8a7: the missing decision record (added); comments that credited the sweep's lock with a case that can't occur (reworded, tests renamed); and a failed partition build that left the job INITIALIZING, blocking new runs for ten minutes (now marked FAILED). The second pass found nothing blocking. Greptile's one finding (failIfUnchanged took eight positional parameters) is fixed in 7a01214; 182 unit tests pass there.

  • Decision check: if this PR decides something the next contributor could undo — a schema field, REST path or format another component or repo must agree with, a new default or limit, an ordering that must hold, or a rule stated only in this description — it adds a record under docs/decisions/ (see its README.md). This description is gone from git log after the squash-merge. (ADR:2026-11-18-the-reindex-orphan-sweep-fails-a-job-only-under-its-lock-and-unchanged)

  • I have added a test that covers the exact scenario we are fixing. For complex issues, comment the issue number in the test for future reference.

🤖 Generated with Claude Code

RetriggerView in GreptileConfidence Score: 5/5

The latest changes appear safe to merge.

Summary

No blocking issues found in this follow-up. The latest change groups the job state and record counts into named records without changing how abandoned jobs are failed.

  • A partition build keeps its reindex job visibly active.
  • The orphan sweep fails jobs only under the shared lock and from an unchanged state.

Tests were not run during this review.

Reviews (4) · Last reviewed commit: "Bind failIfUnchanged's read state and co..." · Reviewed by Greptile

…hile it initializes

The orphan sweep runs on every server every two minutes. A coordinator
building a job's partitions held the reindex lock under a placeholder id, and
nothing renewed that lock or the job's updatedAt until initialization
finished. On a large catalog the sweep therefore failed the live job after ten
minutes as "Job abandoned due to server crash or shutdown", and the
coordinator could then mark it READY again and run it.

- The executor moves the lock onto the job as soon as the job row exists, and
  renews the lock and updatedAt while the partitions are built. The renewal
  ends with initialization; execute() starts its own.
- The sweep takes the reindex lock before failing an orphan, as it already
  does before recovering one, and fails it only if its status and updatedAt
  are unchanged since it was read. Partitions are cancelled and the app run is
  ended only when that write lands, and only runs that started before the job
  was created are ended.
- The coordinator marks a job READY only while it is still INITIALIZING.
…o build

- ADR:2026-11-18-the-reindex-orphan-sweep-fails-a-job-only-under-its-lock-and-unchanged
  states the lock handoff, the sweep's lock and compare-and-swap, and the
  STOPPING force-complete exception.
- The lock in the sweep's fail path keeps it from acting alongside another
  server's sweep or a running reindex; the comments and test names now say so.
- When partitions fail to build, the job is marked FAILED with the cause once
  the lock renewal has stopped, unless something else already ended it.
  Before, it stayed INITIALIZING with a freshly renewed updatedAt and blocked
  new runs for ten minutes before the sweep failed it as abandoned.
@mohityadav766
mohityadav766 requested a review from a team as a code owner November 18, 2026 07:43
@github-actions

github-actions Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

✅ PR checks passed

The linked issue has a description and all required Shipping project fields set. Thanks!

@mohityadav766 mohityadav766 added the To release Will cherry-pick this PR into the release branch label Nov 18, 2026
failIfUnchanged took eight positional parameters, five of them adjacent
longs, so a caller could swap a timestamp and a count unnoticed. The job state
the sweep read (JobSnapshot: id, status, updatedAt) and the record counts
(RecordCounts: processed, success, failed) are now records bound with
@BindMethods, as OntologyReferenceDAO binds AxiomPosition.
@gitar-bot

gitar-bot Bot commented Nov 18, 2026

Copy link
Copy Markdown
Code Review ✅ Approved 🔴 High risk

Moving reindex lock ownership and renewal, and guarding orphan failures with compare-and-swap, changes shared-state coordination; interruptions could disrupt reindex availability.

Keeps a live reindex job's lock and updatedAt renewed while its partitions are built, and makes the orphan sweep fail a job only under the reindex lock with a compare-and-swap on the status and updatedAt it read. No issues found.

Verification

Rules No rules evaluated

Functional validation Not enabled

Set up

Auto-approval Not enabled

Set up

For more verification details, comment gitar display:verbose.

Options

Display: compact → Showing compact verification results.

Comment with these commands to change the behavior for this request:

Compact
gitar display:verbose         

Was this helpful? React with 👍 / 👎 | Powered by Gitar — free for open source

@mohityadav766
mohityadav766 merged commit 48b3e6f into main Nov 18, 2026
61 checks passed
@mohityadav766
mohityadav766 deleted the fix/38016-orphan-sweep-live-reindex branch November 18, 2026 09:03
@github-actions

Copy link
Copy Markdown
Contributor

🤖 Backport to 2.0 is under way. It is built from openmetadata-collate, which holds the credentials needed to open a PR here, and it will comment again with the result — nothing is needed from you unless it reports otherwise.

@gh-bot-collate

Copy link
Copy Markdown

🤖 Backport to 2.0 failed. The conflict could not be resolved, so this change is not on that branch. Resolver run.

mohityadav766 added a commit that referenced this pull request Nov 19, 2026
… job while it initializes (#38093) (#38120)

Backport of #38093. The orphan sweep runs on every server every two minutes.
A coordinator building a job's partitions held the reindex lock under a
placeholder id, and nothing renewed that lock or the job's updatedAt until
initialization finished. On a large catalog the sweep therefore failed the
live job after ten minutes as "Job abandoned due to server crash or
shutdown", and the coordinator could then mark it READY again and run it.

- The executor moves the lock onto the job as soon as the job row exists, and
  renews the lock and updatedAt while the partitions are built. If they fail
  to build, the job is marked FAILED with the cause.
- The sweep takes the reindex lock before failing an orphan, as it already
  does before recovering one, and fails it only if its status and updatedAt
  are unchanged since it was read. Partitions are cancelled and the app run is
  ended only when that write lands, and only runs that started before the job
  was created are ended.
- The coordinator marks a job READY only while it is still INITIALIZING.
- Recorded as
  ADR:2026-11-18-the-reindex-orphan-sweep-fails-a-job-only-under-its-lock-and-unchanged.

On 2.0 the search-index job DAO is still nested in CollectionDAO, so the two
new statements live there instead of in SearchReindexDAOs.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

To release Will cherry-pick this PR into the release branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Detail Bug] Search indexing: Orphan sweep can falsely fail a live distributed reindex job during initialization

2 participants