Repository navigation
[2.0] Fixes #38016: keep the orphan sweep from failing a live reindex job while it initializes (#38093) - #38120
Conversation
… 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.
❌ PR checklist incompleteThis PR cannot be merged until the following are addressed on its linked issue:
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 |
Code Review ✅ Approved 🔴 High riskMoving 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 VerificationRules No rules evaluated Functional validation Not enabledAuto-approval Not enabledFor more verification details, comment OptionsDisplay: compact → Showing compact verification results. Comment with these commands to change the behavior for this request:
Was this helpful? React with 👍 / 👎 | Powered by Gitar — free for open source |
| private SearchIndexJob initializePartitionsWithLockRenewal( | ||
| UUID jobId, ReindexingConfiguration reindexConfig) { | ||
| Thread lockRenewal = |
There was a problem hiding this comment.
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!
| recoveryManager.performStartupRecovery(); | ||
|
|
||
| verify(appRuns) | ||
| .markRunningEntriesInterrupted( | ||
| eq(List.of("SearchIndexingApplication")), | ||
| anyString(), | ||
| anyLong(), | ||
| eq(jobRecord.createdAt())); |
There was a problem hiding this comment.
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()); |
There was a problem hiding this comment.
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!
| boolean stillInitializing = | ||
| coordinator | ||
| .getJob(jobId) | ||
| .map(job -> job.getStatus() == IndexJobStatus.INITIALIZING) | ||
| .orElse(false); |
There was a problem hiding this comment.
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!
Describe your changes:
Fixes #38016
Backport of #38093 to 2.0. The bug is the same on 2.0.
DistributedSearchIndexExecutor.createJob(),DistributedSearchIndexCoordinator.initializePartitions()andJobRecoveryManagermatchmainapart 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'supdatedAtuntilinitializePartitions()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):
createJob): the lock moves onto the job as soon as the job row exists, and a renewal loop keeps the lock and the job'supdatedAtfresh 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.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 andupdatedAtthe 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.failIfUnchanged, which takes the job state the sweep read and the record counts as records, andmarkReadyIfInitializing.ADR:2026-11-18-the-reindex-orphan-sweep-fails-a-job-only-under-its-lock-and-unchangedcomes along, so the code's citations resolve on 2.0.DISTRIBUTED_INDEXING.mdis updated to match.Differences from #38093:
CollectionDAO(theSearchReindexDAOssplit is main-only), so the two new statements and their records live there. Imports point atCollectionDAO.*.startedBeforecutoff and the run timestamp taken when the run starts.Type of change:
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 betweencreateJob()andexecute()is still not renewed, as onmain. 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
updatedAtare renewed while it builds.Unit tests
mvn -B clean package -pl :openmetadata-service -am -Dtest=…on this branch: 178 tests inDistributedSearchIndexCoordinatorTest,DistributedSearchIndexExecutorTest,JobRecoveryManagerTest,JobRecoveryOrphanDetectionTest,OrphanJobMonitorTest,DistributedJobParticipantTestandDistributedIndexingStrategyTest, 0 failures.Backend integration tests
SearchReindexOrphanSweepIT(@Isolated: it holds the cluster-wide reindex lock). Against the realsearch_index_job/search_reindex_locktables it covers the sweep's two outcomes,failIfUnchangedmissing after a write, andmarkReadyIfInitializingmoving an INITIALIZING job but leaving a FAILED one.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.CollectionDAO.SearchIndexJobDAOthrough Jdbi, set up asJdbiUtilsdoes, on throwaway MySQL 8.3 and PostgreSQL 15 containers with the migration'ssearch_index_jobDDL. On both engines:markReadyIfInitializingmoved 1 row, then 0;failIfUnchangedmoved 0 for a staleupdatedAtor a wrong status, and 1 for the current read;statscleared.Ingestion integration tests
Playwright (UI) tests
Manual testing performed
createJob(),initializePartitions(),runLockRefreshLoop(),JobRecoveryManager, the job DAO and the app-run interruption DAO withmain's: the same logic, so the bug and the fix both apply.UI screen recording / screenshots:
Not applicable — backend-only fix.
Checklist:
Fixes <issue-number>: <short explanation>Fixes #<issue-number>above.🤖 Generated with Claude Code
Satisfy the repository’s style and behavior-test requirements before merging.
Findings
Summary
This backport keeps reindex jobs alive while partitions are built and guards the orphan sweep against stale reads.
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]Reviews (1) · Last reviewed commit: "[2.0] Fixes #38016: keep the orphan swee..." · Reviewed by Greptile