Skip to content

[2.0] Fixes #38016: keep the orphan sweep from failing a live reindex job while it initializes (#38093) - #38120

Merged
mohityadav766 merged 1 commit into
2.0from
backport/2.0/38093-orphan-sweep-live-reindex
Nov 19, 2026
Merged

mohityadav766 merged 1 commit into
2.0from
backport/2.0/38093-orphan-sweep-live-reindex

Conversation

@mohityadav766

@mohityadav766 mohityadav766 commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

Describe your changes:

Fixes #38016

Backport of #38093 to 2.0. The bug is the same on 2.0. DistributedSearchIndexExecutor.createJob(), DistributedSearchIndexCoordinator.initializePartitions() and JobRecoveryManager match main apart from import paths. While a coordinator builds a job's partitions, it holds the reindex lock under a placeholder id. Nothing renews that lock or the job's updatedAt until initializePartitions() returns. After ten minutes the orphan sweep, which runs on every server every two minutes, fails the live job as "Job abandoned due to server crash or shutdown". failJob() takes no lock and writes unconditionally, cancels pending partitions and ends the running app run. The coordinator then writes READY over the FAILED row.

What changed (same as #38093):

  • 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. If the partitions fail to build, the job is marked FAILED with the cause instead of staying INITIALIZING.
  • Sweep (JobRecoveryManager): failing an orphan 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. 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. 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.
  • The decision record ADR:2026-11-18-the-reindex-orphan-sweep-fails-a-job-only-under-its-lock-and-unchanged comes along, so the code's citations resolve on 2.0. DISTRIBUTED_INDEXING.md is updated to match.

Differences from #38093:

  • 2.0 still keeps the search-index job DAO nested in CollectionDAO (the SearchReindexDAOs split is main-only), so the two new statements and their records live there. Imports point at CollectionDAO.*.
  • Nothing was left out. Every changed method exists on 2.0 with the same behaviour, including the app-run interruption with its startedBefore cutoff and the run timestamp taken when the run starts.
  • Apart from the DAO's host file and import paths, the diff is line-for-line the same as Fixes #38016: keep the orphan sweep from failing a live reindex job while it initializes #38093's (compared file by file).

Type of change:

  • Bug fix

High-level design:

Same as #38093, which has the alternatives considered and the rollout notes. Short version: 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 lock-guarded and conditional. Staged-index creation between createJob() and execute() is still not renewed, as on main. A failure there leaves a READY job that blocks new runs for up to an hour; that is a separate follow-up.

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.
  • An orphan is left for a later sweep while another holder has the reindex lock, and failed once that lock has expired.
  • 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

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 the sweep's two outcomes, failIfUnchanged missing after a write, and markReadyIfInitializing moving an INITIALIZING job but leaving a FAILED one.
  • Ran it locally on this branch, MySQL + Elasticsearch, parallel lane (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.
  • I called this branch's CollectionDAO.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 (no ingestion changes).

Playwright (UI) tests

  • Not applicable (no UI changes).

Manual testing performed

  1. Compared 2.0's createJob(), initializePartitions(), runLockRefreshLoop(), JobRecoveryManager, the job DAO and the app-run interruption DAO with main's: the same logic, so the bug and the fix both apply.
  2. Called the real DAO through Jdbi on MySQL 8.3 and PostgreSQL 15 (results above).

UI screen recording / screenshots:

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 UI changes: I attached a screen recording and/or screenshots above. (no UI change)
  • I have added tests (unit / integration / Playwright as applicable) and listed them above.
  • 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: 4/5

Satisfy the repository’s style and behavior-test requirements before merging.

Findings

  1. P2 New values omit `final` ▶
  2. P2 Run cutoff lacks behavior coverage ▶
  3. P2 Assertion text bypasses constants ▶
  4. P2 New boolean breaks naming rules ▶

Summary

This backport keeps reindex jobs alive while partitions are built and guards the orphan sweep against stale reads.

  • Reindex jobs stay live while their partitions are built.
  • The orphan sweep fails jobs only when it holds the lock and the job is unchanged.

The new code needs the repository’s Java and testing rules applied before merging. mohityadav766 explicitly defers renewal during staged-index creation because extending renewal across that gap could keep a failed run’s lock alive forever.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Create job row] --> B[Move lock onto job]
  B --> C[Build partitions while renewing lock]
  C --> D{Still INITIALIZING?}
  D -->|Yes| E[Mark READY]
  D -->|No| F[Keep existing outcome]
  G[Orphan sweep] --> H[Acquire reindex lock]
  H --> I{Status and update time unchanged?}
  I -->|Yes| J[Fail job and clean up]
  I -->|No| K[Leave job alone]
Loading

Reviews (1) · Last reviewed commit: "[2.0] Fixes #38016: keep the orphan swee..." · Reviewed by Greptile

… job while it initializes (#38093)

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.
@mohityadav766
mohityadav766 requested a review from a team as a code owner November 18, 2026 13:02
@mohityadav766
mohityadav766 requested review from yan-3005 and removed request for a team November 18, 2026 13:02
@github-actions

Copy link
Copy Markdown
Contributor

❌ PR checklist incomplete

This PR cannot be merged until the following are addressed on its linked issue:

  • No GitHub issue is linked. Link an issue in the Development section of the PR (or add Fixes #12345 to the description). For a same-org cross-repo issue, add Fixes open-metadata/<repo>#123 to the description.

The fields live on the linked issue in the Shipping project (open the issue → right sidebar → Projects). After you set them, re-run this check (or push a commit) — issue/project changes do not re-trigger it automatically.

Maintainers can bypass this check by adding the skip-pr-checks label.

@gitar-bot

gitar-bot Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Code Review ✅ Approved 🔴 High risk

Moving reindex lock ownership and guarding orphan-job writes changes shared-state coordination; races could fail live jobs or disrupt reindex availability.

Backport of the reindex orphan sweep fix so a live coordinator building partitions keeps its lock and updatedAt fresh and is no longer failed as abandoned; the sweep's FAILED write is now lock-guarded and compare-and-swap. 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

Comment on lines +329 to +331
private SearchIndexJob initializePartitionsWithLockRenewal(
UUID jobId, ReindexingConfiguration reindexConfig) {
Thread lockRenewal =

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 New values omit final. lockRenewal, jobId, and reindexConfig are never reassigned, but none is declared final. The Java guide requires final on unchanged local variables and parameters. Apply that requirement here and throughout the new helpers.

Context Used: CLAUDE.md (source)

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Comment on lines +328 to +335
recoveryManager.performStartupRecovery();

verify(appRuns)
.markRunningEntriesInterrupted(
eq(List.of("SearchIndexingApplication")),
anyString(),
anyLong(),
eq(jobRecord.createdAt()));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Run cutoff lacks behavior coverage. This test checks the arguments passed to a mocked appRuns; it never creates an older run and a newer run. It cannot catch a wrong database predicate that ends both runs or neither. The testing guide requires testing outcomes and mocking external boundaries rather than internal classes. Add this case to SearchReindexOrphanSweepIT and assert which saved run changes.

Context Used: CLAUDE.md (source)

IllegalStateException failure =
assertThrows(IllegalStateException.class, () -> coordinator.initializePartitions(jobId));

assertTrue(failure.getMessage().contains("not marking it READY"), failure.getMessage());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Assertion text bypasses constants. The new assertion passes a raw string to contains(). The Java guide explicitly forbids raw string literals in contains() calls. Define a named constant for the expected message fragment and use it here.

Context Used: CLAUDE.md (source)

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Comment on lines +349 to +353
boolean stillInitializing =
coordinator
.getJob(jobId)
.map(job -> job.getStatus() == IndexJobStatus.INITIALIZING)
.orElse(false);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 New boolean breaks naming rules. stillInitializing does not use the question-form name required by the Java guide. Rename it to isStillInitializing so the new helper follows that directive.

Context Used: CLAUDE.md (source)

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

@mohityadav766
mohityadav766 merged commit 55a24b5 into 2.0 Nov 19, 2026
70 of 78 checks passed
@mohityadav766
mohityadav766 deleted the backport/2.0/38093-orphan-sweep-live-reindex branch November 19, 2026 07:23
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.

1 participant