Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix recover index bug when Flint data index is deleted accidentally #241

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,8 @@ WITH (

Currently Flint index job ID is same as internal Flint index name in [OpenSearch](./index.md#OpenSearch) section below.

- **Recover Job**: Initiates a restart of the index refresh job and transition the Flint index to the 'refreshing' state. Additionally, it includes functionality to clean up the metadata log entry in the event that the Flint data index is no longer present in OpenSearch.

```sql
RECOVER INDEX JOB <id>
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,20 @@ class FlintSpark(val spark: SparkSession) extends Logging {
}
} else {
logInfo("Index to be recovered either doesn't exist or not auto refreshed")
if (index.isEmpty) {
/*
* If execution reaches this point, it indicates that the Flint index is corrupted.
* In such cases, clean up the metadata log, as the index data no longer exists.
* There is a very small possibility that users may recreate the index in the
* interim, but metadata log get deleted by this cleanup process.
*/
logWarning("Cleaning up metadata log as index data has been deleted")
flintClient
.startTransaction(indexName, dataSourceName)
.initialLog(_ => true)
.finalLog(_ => NO_LOG_ENTRY)
.commit(_ => {})
}
false
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,23 @@ class FlintSparkTransactionITSuite extends OpenSearchTransactionSuite with Match
.create()
} should have message s"Flint index $testFlintIndex already exists"
}

test("should clean up metadata log entry if index data has been deleted") {
flint
.skippingIndex()
.onTable(testTable)
.addPartitions("year", "month")
.options(FlintSparkIndexOptions(Map("auto_refresh" -> "true")))
.create()
flint.refreshIndex(testFlintIndex)

// Simulate the situation that user delete index data directly and then refresh exits
spark.streams.active.find(_.name == testFlintIndex).get.stop()
deleteIndex(testFlintIndex)

// Index state is refreshing and expect recover API clean it up
latestLogEntry(testLatestId) should contain("state" -> "refreshing")
flint.recoverIndex(testFlintIndex)
latestLogEntry(testLatestId) shouldBe empty
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ case class JobOperator(
}

try {
// Stop SparkSession if streaming job succeeds
if (!exceptionThrown && streaming) {
// Wait for streaming job complete if no error and there is streaming job running
if (!exceptionThrown && streaming && spark.streams.active.nonEmpty) {
// wait if any child thread to finish before the main thread terminates
spark.streams.awaitAnyTermination()
}
Expand Down
Loading