-
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.
refactor: Rollback when an exception is thrown in the listener (#116)
* refactor: TransactionHandler가 종료될때, 자동으로 commit, join, rollback event가 발행되도록 수정한다. * refactor: remove unused import and mutablelist to list * refactor: Remove duplicated codes
- Loading branch information
Showing
65 changed files
with
951 additions
and
1,114 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package org.rooftop.netx.api | ||
|
||
enum class SuccessWith { | ||
PUBLISH_JOIN, | ||
PUBLISH_COMMIT, | ||
END, | ||
} |
8 changes: 6 additions & 2 deletions
8
src/main/kotlin/org/rooftop/netx/api/TransactionCommitEvent.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 |
---|---|---|
@@ -1,9 +1,13 @@ | ||
package org.rooftop.netx.api | ||
|
||
class TransactionCommitEvent( | ||
class TransactionCommitEvent internal constructor( | ||
transactionId: String, | ||
nodeName: String, | ||
group: String, | ||
event: String?, | ||
codec: Codec, | ||
): TransactionEvent(transactionId, nodeName, group, event, codec) | ||
): TransactionEvent(transactionId, nodeName, group, event, codec) { | ||
|
||
override fun copy(): TransactionEvent = | ||
TransactionJoinEvent(transactionId, nodeName, group, event, codec) | ||
} |
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 |
---|---|---|
@@ -1,9 +1,13 @@ | ||
package org.rooftop.netx.api | ||
|
||
class TransactionJoinEvent( | ||
class TransactionJoinEvent internal constructor( | ||
transactionId: String, | ||
nodeName: String, | ||
group: String, | ||
event: String?, | ||
codec: Codec, | ||
): TransactionEvent(transactionId, nodeName, group, event, codec) | ||
) : TransactionEvent(transactionId, nodeName, group, event, codec) { | ||
|
||
override fun copy(): TransactionEvent = | ||
TransactionJoinEvent(transactionId, nodeName, group, event, codec) | ||
} |
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
12 changes: 4 additions & 8 deletions
12
src/main/kotlin/org/rooftop/netx/api/TransactionRollbackEvent.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 |
---|---|---|
@@ -1,18 +1,14 @@ | ||
package org.rooftop.netx.api | ||
|
||
import kotlin.reflect.KClass | ||
|
||
class TransactionRollbackEvent( | ||
class TransactionRollbackEvent internal constructor( | ||
transactionId: String, | ||
nodeName: String, | ||
group: String, | ||
event: String?, | ||
val cause: String, | ||
private val undo: String, | ||
private val codec: Codec, | ||
codec: Codec, | ||
) : TransactionEvent(transactionId, nodeName, group, event, codec) { | ||
|
||
fun <T : Any> decodeUndo(type: Class<T>): T = decodeUndo(type.kotlin) | ||
|
||
fun <T : Any> decodeUndo(type: KClass<T>): T = codec.decode(undo, type) | ||
override fun copy(): TransactionEvent = | ||
TransactionJoinEvent(transactionId, nodeName, group, event, codec) | ||
} |
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
8 changes: 6 additions & 2 deletions
8
src/main/kotlin/org/rooftop/netx/api/TransactionStartEvent.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 |
---|---|---|
@@ -1,9 +1,13 @@ | ||
package org.rooftop.netx.api | ||
|
||
class TransactionStartEvent( | ||
class TransactionStartEvent internal constructor( | ||
transactionId: String, | ||
nodeName: String, | ||
group: String, | ||
event: String?, | ||
codec: Codec, | ||
) : TransactionEvent(transactionId, nodeName, group, event, codec) | ||
) : TransactionEvent(transactionId, nodeName, group, event, codec) { | ||
|
||
override fun copy(): TransactionEvent = | ||
TransactionJoinEvent(transactionId, nodeName, group, event, codec) | ||
} |
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
57 changes: 53 additions & 4 deletions
57
src/main/kotlin/org/rooftop/netx/engine/AbstractDispatchFunction.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 |
---|---|---|
@@ -1,20 +1,69 @@ | ||
package org.rooftop.netx.engine | ||
|
||
import org.rooftop.netx.api.TransactionEvent | ||
import org.rooftop.netx.api.TransactionManager | ||
import reactor.core.scheduler.Schedulers | ||
import kotlin.reflect.KClass | ||
import kotlin.reflect.KFunction | ||
|
||
internal sealed class AbstractDispatchFunction<T>( | ||
protected val eventType: KClass<*>, | ||
private val eventType: KClass<*>, | ||
protected val function: KFunction<T>, | ||
protected val handler: Any, | ||
private val noRetryFor: Array<KClass<out Throwable>>, | ||
private val noRollbackFor: Array<KClass<out Throwable>>, | ||
private val nextState: NextTransactionState, | ||
private val transactionManager: TransactionManager, | ||
) { | ||
fun name(): String = function.name | ||
|
||
abstract fun call(transactionEvent: TransactionEvent): T | ||
|
||
protected fun isNoRetryFor(throwable: Throwable): Boolean { | ||
return noRetryFor.isNotEmpty() && throwable.cause != null && noRetryFor.contains(throwable.cause!!::class) | ||
protected fun isNoRollbackFor(throwable: Throwable): Boolean { | ||
return noRollbackFor.isNotEmpty() && throwable.cause != null && noRollbackFor.contains( | ||
throwable.cause!!::class | ||
) | ||
} | ||
|
||
protected fun isProcessable(transactionEvent: TransactionEvent): Boolean { | ||
return runCatching { | ||
transactionEvent.decodeEvent(eventType) | ||
}.onFailure { | ||
return it is NullPointerException && eventType == Any::class | ||
}.isSuccess | ||
} | ||
|
||
protected fun rollback(transactionEvent: TransactionEvent, throwable: Throwable) { | ||
transactionEvent.nextEvent?.let { | ||
transactionManager.rollback(transactionEvent.transactionId, throwable.getCause(), it) | ||
.subscribeOn(Schedulers.parallel()) | ||
.subscribe() | ||
} ?: transactionManager.rollback(transactionEvent.transactionId, throwable.getCause()) | ||
.subscribeOn(Schedulers.parallel()) | ||
.subscribe() | ||
} | ||
|
||
protected fun publishNextTransaction(transactionEvent: TransactionEvent) { | ||
when (nextState) { | ||
NextTransactionState.JOIN -> transactionEvent.nextEvent?.let { | ||
transactionManager.join(transactionEvent.transactionId, it) | ||
} ?: transactionManager.join(transactionEvent.transactionId) | ||
|
||
NextTransactionState.COMMIT -> transactionEvent.nextEvent?.let { | ||
transactionManager.commit(transactionEvent.transactionId, it) | ||
} ?: transactionManager.commit(transactionEvent.transactionId) | ||
|
||
NextTransactionState.END -> return | ||
}.subscribeOn(Schedulers.parallel()) | ||
.subscribe() | ||
} | ||
|
||
private fun Throwable.getCause(): String { | ||
return this.message ?: this.cause?.message ?: this::class.java.name | ||
} | ||
|
||
internal enum class NextTransactionState { | ||
JOIN, | ||
COMMIT, | ||
END | ||
} | ||
} |
Oops, something went wrong.