-
Notifications
You must be signed in to change notification settings - Fork 614
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
63aab8f
commit 89f6bc2
Showing
17 changed files
with
176 additions
and
73 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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package io.kestra.jdbc; | ||
|
||
import io.kestra.core.models.tasks.retrys.Exponential; | ||
import io.kestra.core.utils.RetryUtils; | ||
import jakarta.inject.Inject; | ||
import jakarta.inject.Singleton; | ||
import org.jooq.DSLContext; | ||
import org.jooq.TransactionalCallable; | ||
import org.jooq.TransactionalRunnable; | ||
|
||
import java.sql.SQLException; | ||
import java.time.Duration; | ||
import java.util.function.Predicate; | ||
|
||
@Singleton | ||
public class DSLContextWrapper { | ||
private final DSLContext dslContext; | ||
|
||
private final RetryUtils retryUtils; | ||
|
||
@Inject | ||
public DSLContextWrapper(DSLContext dslContext, RetryUtils retryUtils) { | ||
this.dslContext = dslContext; | ||
this.retryUtils = retryUtils; | ||
} | ||
|
||
private <T> RetryUtils.Instance<T, RuntimeException> retryer() { | ||
return retryUtils.of( | ||
Exponential.builder() | ||
.interval(Duration.ofMillis(10)) | ||
.maxAttempt(10) | ||
.maxInterval(Duration.ofMillis(100)) | ||
.build() | ||
); | ||
} | ||
|
||
private static <E extends Throwable> Predicate<E> predicate() { | ||
return (e) -> { | ||
if (!(e.getCause() instanceof SQLException)) { | ||
return false; | ||
} | ||
|
||
SQLException cause = (SQLException) e.getCause(); | ||
|
||
return | ||
// standard deadlock | ||
cause.getSQLState().equals("40001") || | ||
// postgres deadlock | ||
cause.getSQLState().equals("40P01"); | ||
}; | ||
} | ||
|
||
public void transaction(TransactionalRunnable transactional) { | ||
RetryUtils.Instance<Object, Throwable> of = retryUtils.of(Exponential.builder() | ||
.interval(Duration.ofMillis(10)) | ||
.maxAttempt(10) | ||
.maxInterval(Duration.ofMillis(100)) | ||
.build() | ||
); | ||
|
||
this.<Void>retryer().runRetryIf( | ||
predicate(), | ||
() -> { | ||
dslContext.transaction(transactional); | ||
return null; | ||
} | ||
); | ||
} | ||
|
||
public <T> T transactionResult(TransactionalCallable<T> transactional) { | ||
RetryUtils.Instance<Object, Throwable> of = retryUtils.of(Exponential.builder() | ||
.interval(Duration.ofMillis(10)) | ||
.maxAttempt(10) | ||
.maxInterval(Duration.ofMillis(100)) | ||
.build() | ||
); | ||
|
||
return this.<T>retryer().runRetryIf( | ||
predicate(), | ||
() -> dslContext.transactionResult(transactional) | ||
); | ||
} | ||
} |
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
Oops, something went wrong.