-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
bulk-cdk: add more exception classifier implementations, add extra checks #44824
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
77df72a
bulk-cdk: flesh out exception handling
postamar 40269e7
bulk-cdk-core-base: add RegexExceptionClassifier
postamar b0aac7f
bulk-cdk: add extra checks and JdbcExtraChecks
postamar 12771ff
suppress spotbugs warnings
postamar 2fcb22c
Merge branch 'master' into exception-classifiers
postamar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
45 changes: 45 additions & 0 deletions
45
...te-cdk/bulk/core/base/src/main/kotlin/io/airbyte/cdk/output/DefaultExceptionClassifier.kt
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,45 @@ | ||
/* | ||
* Copyright (c) 2024 Airbyte, Inc., all rights reserved. | ||
*/ | ||
|
||
package io.airbyte.cdk.output | ||
|
||
import io.airbyte.cdk.ConfigErrorException | ||
import io.airbyte.cdk.ConnectorErrorException | ||
import io.airbyte.cdk.SystemErrorException | ||
import io.airbyte.cdk.TransientErrorException | ||
import io.micronaut.context.annotation.ConfigurationProperties | ||
import io.micronaut.context.annotation.Value | ||
import jakarta.inject.Singleton | ||
|
||
const val DEFAULT_CLASSIFIER_PREFIX = "${EXCEPTION_CLASSIFIER_PREFIX}.default" | ||
|
||
/** Default implementation of [ExceptionClassifier]. */ | ||
@Singleton | ||
@ConfigurationProperties(DEFAULT_CLASSIFIER_PREFIX) | ||
class DefaultExceptionClassifier( | ||
@Value("\${$DEFAULT_CLASSIFIER_PREFIX.order:1}") override val orderValue: Int | ||
) : ExceptionClassifier { | ||
|
||
override fun classify(e: Throwable): ConnectorError? { | ||
return when (val connectorErrorException: ConnectorErrorException? = unwind(e)) { | ||
is ConfigErrorException -> ConfigError(connectorErrorException.message!!) | ||
is TransientErrorException -> TransientError(connectorErrorException.message!!) | ||
is SystemErrorException -> SystemError(connectorErrorException.message) | ||
null -> null | ||
} | ||
} | ||
|
||
/** Recursively walks the causes of [e] and returns the last [ConnectorErrorException]. */ | ||
fun unwind(e: Throwable): ConnectorErrorException? { | ||
var connectorErrorException: ConnectorErrorException? = null | ||
var unwound: Throwable? = e | ||
while (unwound != null) { | ||
if (unwound is ConnectorErrorException) { | ||
connectorErrorException = unwound | ||
} | ||
unwound = unwound.cause | ||
} | ||
return connectorErrorException | ||
} | ||
} |
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
48 changes: 48 additions & 0 deletions
48
airbyte-cdk/bulk/core/base/src/main/kotlin/io/airbyte/cdk/output/ExceptionHandler.kt
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,48 @@ | ||
/* | ||
* Copyright (c) 2024 Airbyte, Inc., all rights reserved. | ||
*/ | ||
|
||
package io.airbyte.cdk.output | ||
|
||
import io.airbyte.cdk.util.ApmTraceUtils | ||
import io.airbyte.protocol.models.v0.AirbyteErrorTraceMessage | ||
import jakarta.inject.Singleton | ||
import org.apache.commons.lang3.exception.ExceptionUtils | ||
|
||
/** [ExceptionHandler] applies all available [ExceptionClassifier] implementations in sequence. */ | ||
@Singleton | ||
class ExceptionHandler(val classifiers: List<ExceptionClassifier>) { | ||
|
||
fun classify(e: Throwable): ConnectorError { | ||
for (classifier in classifiers) { | ||
val classified: ConnectorError? = classifier.classify(e) | ||
if (classified != null) { | ||
return classified | ||
} | ||
} | ||
return SystemError(e.message) | ||
} | ||
|
||
/** Maps [e] to a [AirbyteErrorTraceMessage] to be passed to the [OutputConsumer]. */ | ||
fun handle(e: Throwable): AirbyteErrorTraceMessage { | ||
ApmTraceUtils.addExceptionToTrace(e) | ||
val errorTraceMessage = | ||
AirbyteErrorTraceMessage() | ||
.withInternalMessage(e.toString()) | ||
.withStackTrace(ExceptionUtils.getStackTrace(e)) | ||
return when (val classified: ConnectorError = classify(e)) { | ||
is ConfigError -> | ||
errorTraceMessage | ||
.withFailureType(AirbyteErrorTraceMessage.FailureType.CONFIG_ERROR) | ||
.withMessage(classified.displayMessage) | ||
is TransientError -> | ||
errorTraceMessage | ||
.withFailureType(AirbyteErrorTraceMessage.FailureType.TRANSIENT_ERROR) | ||
.withMessage(classified.displayMessage) | ||
is SystemError -> | ||
errorTraceMessage | ||
.withFailureType(AirbyteErrorTraceMessage.FailureType.SYSTEM_ERROR) | ||
.withMessage(classified.displayMessage ?: e.message) | ||
} | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
airbyte-cdk/bulk/core/base/src/main/kotlin/io/airbyte/cdk/output/RegexExceptionClassifier.kt
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,62 @@ | ||
/* | ||
* Copyright (c) 2024 Airbyte, Inc., all rights reserved. | ||
*/ | ||
|
||
package io.airbyte.cdk.output | ||
|
||
import io.micronaut.context.annotation.EachProperty | ||
import io.micronaut.context.annotation.Parameter | ||
import io.micronaut.context.annotation.Requires | ||
import io.micronaut.context.annotation.Value | ||
import jakarta.inject.Singleton | ||
|
||
const val REGEX_CLASSIFIER_PREFIX = "${EXCEPTION_CLASSIFIER_PREFIX}.regex" | ||
|
||
/** [ExceptionClassifier] implementation based on regexes applied to the exception message. */ | ||
@Singleton | ||
@Requires(property = "${REGEX_CLASSIFIER_PREFIX}.rules") | ||
class RegexExceptionClassifier( | ||
@Value("\${${REGEX_CLASSIFIER_PREFIX}.order:10}") override val orderValue: Int, | ||
override val rules: List<RegexExceptionClassifierRule>, | ||
) : RuleBasedExceptionClassifier<RegexExceptionClassifierRule> { | ||
|
||
init { | ||
for (rule in rules) { | ||
rule.validate() | ||
} | ||
} | ||
} | ||
|
||
/** Micronaut configuration object for [RuleBasedExceptionClassifier] rules. */ | ||
@EachProperty("${REGEX_CLASSIFIER_PREFIX}.rules", list = true) | ||
class RegexExceptionClassifierRule( | ||
@param:Parameter override val ordinal: Int, | ||
) : RuleBasedExceptionClassifier.Rule { | ||
|
||
// Micronaut configuration objects work better with mutable properties. | ||
override lateinit var error: RuleBasedExceptionClassifier.ErrorKind | ||
lateinit var pattern: String | ||
lateinit var inputExample: String | ||
override var group: String? = null | ||
override var output: String? = null | ||
override var referenceLinks: List<String> = emptyList() | ||
|
||
val regex: Regex by lazy { | ||
pattern.toRegex(setOf(RegexOption.MULTILINE, RegexOption.IGNORE_CASE)) | ||
} | ||
|
||
override fun matches(e: Throwable): Boolean = | ||
e.message?.let { regex.containsMatchIn(it) } ?: false | ||
|
||
override fun validate() { | ||
require(runCatching { error }.isSuccess) { "error kind must be set" } | ||
require(runCatching { pattern }.isSuccess) { "regex pattern must be set" } | ||
require(runCatching { inputExample }.isSuccess) { | ||
"input exception message example must be set" | ||
} | ||
val compileResult: Result<Regex> = runCatching { regex } | ||
require(compileResult.isSuccess) { | ||
"regex pattern error: ${compileResult.exceptionOrNull()?.message}" | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...dk/bulk/core/base/src/test/kotlin/io/airbyte/cdk/output/DefaultExceptionClassifierTest.kt
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,48 @@ | ||
/* | ||
* Copyright (c) 2024 Airbyte, Inc., all rights reserved. | ||
*/ | ||
|
||
package io.airbyte.cdk.output | ||
|
||
import io.airbyte.cdk.ConfigErrorException | ||
import io.airbyte.cdk.SystemErrorException | ||
import io.airbyte.cdk.TransientErrorException | ||
import io.micronaut.test.extensions.junit5.annotation.MicronautTest | ||
import jakarta.inject.Inject | ||
import org.junit.jupiter.api.Assertions | ||
import org.junit.jupiter.api.Test | ||
|
||
@MicronautTest | ||
class DefaultExceptionClassifierTest { | ||
|
||
@Inject lateinit var classifier: DefaultExceptionClassifier | ||
|
||
@Test | ||
fun testConfigError() { | ||
Assertions.assertEquals( | ||
ConfigError("foo"), | ||
classifier.classify(ConfigErrorException("foo")), | ||
) | ||
} | ||
|
||
@Test | ||
fun testTransientError() { | ||
Assertions.assertEquals( | ||
TransientError("bar"), | ||
classifier.classify(TransientErrorException("bar")), | ||
) | ||
} | ||
|
||
@Test | ||
fun testSystemError() { | ||
Assertions.assertEquals( | ||
SystemError("baz"), | ||
classifier.classify(SystemErrorException("baz")), | ||
) | ||
} | ||
|
||
@Test | ||
fun testUnclassified() { | ||
Assertions.assertNull(classifier.classify(RuntimeException("quux"))) | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice I like this - enables us to override some specific errors.