forked from opensearch-project/opensearch-spark
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Chen Dai <[email protected]>
- Loading branch information
Showing
2 changed files
with
90 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
...-integration/src/main/scala/org/opensearch/flint/spark/FlintSparkTransactionSupport.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.flint.spark | ||
|
||
import org.opensearch.flint.core.FlintClient | ||
import org.opensearch.flint.core.metadata.log.OptimisticTransaction | ||
|
||
import org.apache.spark.internal.Logging | ||
|
||
/** | ||
* Provides transaction support with proper error handling and logging capabilities. | ||
* | ||
* @note | ||
* This trait requires the mixing class to extend Spark's `Logging` to utilize its logging | ||
* functionalities. Meanwhile it needs to provide `FlintClient` and data source name so this | ||
* trait can help create transaction context. | ||
*/ | ||
trait FlintSparkTransactionSupport { self: Logging => | ||
|
||
/** Abstract FlintClient that need to be defined in the mixing class */ | ||
protected def flintClient: FlintClient | ||
|
||
/** Abstract data source name that need to be defined in the mixing class */ | ||
protected def dataSourceName: String | ||
|
||
/** | ||
* Executes a block of code within a transaction context, handling and logging errors | ||
* appropriately. This method logs the start and completion of the transaction and captures any | ||
* exceptions that occur, enriching them with detailed error messages before re-throwing. | ||
* | ||
* @param indexName | ||
* the name of the index on which the operation is performed | ||
* @param opName | ||
* the name of the operation, used for logging | ||
* @param forceInit | ||
* a boolean flag indicating whether to force the initialization of the transaction | ||
* @param opBlock | ||
* the operation block to execute within the transaction context, which takes an | ||
* `OptimisticTransaction` and returns a value of type `T` | ||
* @tparam T | ||
* the type of the result produced by the operation block | ||
* @return | ||
* the result of the operation block | ||
*/ | ||
def withTransaction[T](indexName: String, opName: String, forceInit: Boolean = false)( | ||
opBlock: OptimisticTransaction[T] => T): T = { | ||
logInfo(s"Starting index operation [$opName] with forceInit=$forceInit") | ||
try { | ||
// Create transaction (only have side effect if forceInit is true) | ||
val tx: OptimisticTransaction[T] = | ||
flintClient.startTransaction(indexName, dataSourceName, forceInit) | ||
|
||
val result = opBlock(tx) | ||
logInfo(s"Index operation [$opName] complete") | ||
result | ||
} catch { | ||
case e: Exception => | ||
// Extract and add root cause message to final error message | ||
val rootCauseMessage = extractRootCause(e) | ||
val detailedMessage = | ||
s"Failed to execute index operation [$opName] caused by: $rootCauseMessage" | ||
logError(detailedMessage, e) | ||
|
||
// Re-throw with new detailed error message | ||
throw new IllegalStateException(detailedMessage) | ||
} | ||
} | ||
|
||
private def extractRootCause(e: Throwable): String = { | ||
var cause = e | ||
while (cause.getCause != null && cause.getCause != cause) { | ||
cause = cause.getCause | ||
} | ||
|
||
if (cause.getLocalizedMessage != null) { | ||
return cause.getLocalizedMessage | ||
} | ||
if (cause.getMessage != null) { | ||
return cause.getMessage | ||
} | ||
cause.toString | ||
} | ||
} |