From 87ff5c17b1bb03a4096cb816a0d24acc5c727a31 Mon Sep 17 00:00:00 2001
From: xb205 <62425964+devxb@users.noreply.github.com>
Date: Sat, 23 Mar 2024 22:27:34 +0900
Subject: [PATCH] feat: Bind values to context in Orchestrator (#106)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
* refactor: Orchestrate와 Rollback 인터페이스에 Function을 제거한다
* feat: Context binding in orchestrator
* docs: Netx version 0.3.3 to 0.3.4
---
README.md | 14 +-
src/main/kotlin/org/rooftop/netx/api/Codec.kt | 2 +
.../kotlin/org/rooftop/netx/api/Context.kt | 19 +
.../rooftop/netx/api/ContextOrchestrate.kt | 6 +
.../org/rooftop/netx/api/ContextRollback.kt | 6 +
...{OrchestrateFunction.kt => Orchestrate.kt} | 2 +-
.../org/rooftop/netx/api/OrchestrateChain.kt | 53 ++-
.../api/{RollbackFunction.kt => Rollback.kt} | 2 +-
.../org/rooftop/netx/api/TypeReference.kt | 14 +
.../engine/AbstractOrchestrateListener.kt | 47 +-
.../netx/engine/DefaultOrchestrateChain.kt | 407 +++++++++++++-----
.../org/rooftop/netx/engine/JsonCodec.kt | 10 +
.../rooftop/netx/engine/OrchestrateEvent.kt | 1 +
.../netx/engine/OrchestratorManager.kt | 1 +
.../rooftop/netx/engine/listen/CommandType.kt | 6 +
.../listen/CommitOrchestrateListener.kt | 22 +-
.../engine/listen/JoinOrchestrateListener.kt | 21 +-
.../listen/MonoCommitOrchestrateListener.kt | 22 +-
.../listen/MonoJoinOrchestrateListener.kt | 21 +-
.../engine/listen/MonoOrchestrateCommand.kt | 32 ++
.../netx/engine/listen/MonoRollbackCommand.kt | 31 ++
.../listen/MonoRollbackOrchestrateListener.kt | 29 +-
.../listen/MonoStartOrchestrateListener.kt | 21 +-
.../netx/engine/listen/OrchestrateCommand.kt | 29 ++
.../netx/engine/listen/RollbackCommand.kt | 29 ++
.../listen/RollbackOrchestrateListener.kt | 35 +-
.../engine/listen/StartOrchestrateListener.kt | 21 +-
.../netx/client/OrchestratorConfigurer.kt | 6 +-
.../netx/engine/OrchestratorConfigurer.kt | 88 +++-
.../netx/engine/OrchestratorFactoryTest.kt | 2 +-
.../rooftop/netx/engine/OrchestratorTest.kt | 42 ++
31 files changed, 789 insertions(+), 252 deletions(-)
create mode 100644 src/main/kotlin/org/rooftop/netx/api/Context.kt
create mode 100644 src/main/kotlin/org/rooftop/netx/api/ContextOrchestrate.kt
create mode 100644 src/main/kotlin/org/rooftop/netx/api/ContextRollback.kt
rename src/main/kotlin/org/rooftop/netx/api/{OrchestrateFunction.kt => Orchestrate.kt} (55%)
rename src/main/kotlin/org/rooftop/netx/api/{RollbackFunction.kt => Rollback.kt} (55%)
create mode 100644 src/main/kotlin/org/rooftop/netx/api/TypeReference.kt
create mode 100644 src/main/kotlin/org/rooftop/netx/engine/listen/CommandType.kt
create mode 100644 src/main/kotlin/org/rooftop/netx/engine/listen/MonoOrchestrateCommand.kt
create mode 100644 src/main/kotlin/org/rooftop/netx/engine/listen/MonoRollbackCommand.kt
create mode 100644 src/main/kotlin/org/rooftop/netx/engine/listen/OrchestrateCommand.kt
create mode 100644 src/main/kotlin/org/rooftop/netx/engine/listen/RollbackCommand.kt
diff --git a/README.md b/README.md
index 40d9654..17c7712 100644
--- a/README.md
+++ b/README.md
@@ -6,7 +6,7 @@
-![version 0.3.3](https://img.shields.io/badge/version-0.3.3-black?labelColor=black&style=flat-square) ![jdk 17](https://img.shields.io/badge/minimum_jdk-17-orange?labelColor=black&style=flat-square) ![load-test](https://img.shields.io/badge/load%20test%2010%2C000%2C000-success-brightgreen?labelColor=black&style=flat-square)
+![version 0.3.4](https://img.shields.io/badge/version-0.3.4-black?labelColor=black&style=flat-square) ![jdk 17](https://img.shields.io/badge/minimum_jdk-17-orange?labelColor=black&style=flat-square) ![load-test](https://img.shields.io/badge/load%20test%2010%2C000%2C000-success-brightgreen?labelColor=black&style=flat-square)
![redis--stream](https://img.shields.io/badge/-redis--stream-da2020?style=flat-square&logo=Redis&logoColor=white)
Redis-Stream을 지원하는 Saga frame work 입니다.
@@ -88,7 +88,7 @@ class OrchestratorConfigurer(
fun orderOrchestartor(): Orchestrator { //
return orchestratorFactory.create("orderOrchestrator")
.start(
- function = { order -> // its order type
+ orchestrate = { order -> // its order type
// Start Transaction with your bussiness logic
// something like ... "Check valid seller"
return@start user
@@ -98,13 +98,19 @@ class OrchestratorConfigurer(
}
)
.joinReactive(
- function = { user -> // Before operations response type "User" flow here
+ orchestrate = { user -> // Before operations response type "User" flow here
// Webflux supports, should return Mono type.
},
// Can skip rollback operation, if you want
)
+ .joinWithContext(
+ contextOrchestrate = { context, request ->
+ context.set("key", request) // save data on context
+ context.decode("foo", Foo::class) // The context set in the upstream chain can be retrieved.
+ },
+ )
.commit(
- function = { request ->
+ orchestrate = { request ->
// When an error occurs, all rollbacks are called from the bottom up,
// starting from the location where the error occurred.
throw IllegalArgumentException("Oops! Something went wrong..")
diff --git a/src/main/kotlin/org/rooftop/netx/api/Codec.kt b/src/main/kotlin/org/rooftop/netx/api/Codec.kt
index 728cc0c..84eb70c 100644
--- a/src/main/kotlin/org/rooftop/netx/api/Codec.kt
+++ b/src/main/kotlin/org/rooftop/netx/api/Codec.kt
@@ -7,4 +7,6 @@ interface Codec {
fun encode(data: T): String
fun decode(data: String, type: KClass): T
+
+ fun decode(data: String, type: TypeReference): T
}
diff --git a/src/main/kotlin/org/rooftop/netx/api/Context.kt b/src/main/kotlin/org/rooftop/netx/api/Context.kt
new file mode 100644
index 0000000..0dcd3c9
--- /dev/null
+++ b/src/main/kotlin/org/rooftop/netx/api/Context.kt
@@ -0,0 +1,19 @@
+package org.rooftop.netx.api
+
+import kotlin.reflect.KClass
+
+data class Context internal constructor(
+ private val codec: Codec,
+ internal val contexts: MutableMap,
+) {
+
+ fun set(key: String, value: T) {
+ contexts[key] = codec.encode(value)
+ }
+
+ fun decodeContext(key: String, type: Class): T = decodeContext(key, type.kotlin)
+
+ fun decodeContext(key: String, type: KClass): T = contexts[key]?.let {
+ codec.decode(it, type)
+ } ?: throw NullPointerException("Cannot find context by key \"$key\"")
+}
diff --git a/src/main/kotlin/org/rooftop/netx/api/ContextOrchestrate.kt b/src/main/kotlin/org/rooftop/netx/api/ContextOrchestrate.kt
new file mode 100644
index 0000000..ca0e909
--- /dev/null
+++ b/src/main/kotlin/org/rooftop/netx/api/ContextOrchestrate.kt
@@ -0,0 +1,6 @@
+package org.rooftop.netx.api
+
+fun interface ContextOrchestrate {
+
+ fun orchestrate(context: Context, request: T): V
+}
diff --git a/src/main/kotlin/org/rooftop/netx/api/ContextRollback.kt b/src/main/kotlin/org/rooftop/netx/api/ContextRollback.kt
new file mode 100644
index 0000000..07e8d47
--- /dev/null
+++ b/src/main/kotlin/org/rooftop/netx/api/ContextRollback.kt
@@ -0,0 +1,6 @@
+package org.rooftop.netx.api
+
+fun interface ContextRollback {
+
+ fun rollback(context: Context, request: T): V
+}
diff --git a/src/main/kotlin/org/rooftop/netx/api/OrchestrateFunction.kt b/src/main/kotlin/org/rooftop/netx/api/Orchestrate.kt
similarity index 55%
rename from src/main/kotlin/org/rooftop/netx/api/OrchestrateFunction.kt
rename to src/main/kotlin/org/rooftop/netx/api/Orchestrate.kt
index c8472d9..d14d841 100644
--- a/src/main/kotlin/org/rooftop/netx/api/OrchestrateFunction.kt
+++ b/src/main/kotlin/org/rooftop/netx/api/Orchestrate.kt
@@ -1,6 +1,6 @@
package org.rooftop.netx.api
-fun interface OrchestrateFunction {
+fun interface Orchestrate {
fun orchestrate(request: T): V
}
diff --git a/src/main/kotlin/org/rooftop/netx/api/OrchestrateChain.kt b/src/main/kotlin/org/rooftop/netx/api/OrchestrateChain.kt
index 4d90d8f..40c5782 100644
--- a/src/main/kotlin/org/rooftop/netx/api/OrchestrateChain.kt
+++ b/src/main/kotlin/org/rooftop/netx/api/OrchestrateChain.kt
@@ -6,35 +6,64 @@ import reactor.core.publisher.Mono
interface OrchestrateChain {
fun join(
- function: OrchestrateFunction,
- rollback: RollbackFunction? = null,
+ orchestrate: Orchestrate,
+ rollback: Rollback? = null,
): DefaultOrchestrateChain
fun joinReactive(
- function: OrchestrateFunction>,
- rollback: RollbackFunction>? = null,
+ orchestrate: Orchestrate>,
+ rollback: Rollback>? = null,
+ ): DefaultOrchestrateChain
+
+ fun joinWithContext(
+ contextOrchestrate: ContextOrchestrate,
+ contextRollback: ContextRollback? = null,
+ ): DefaultOrchestrateChain
+
+ fun joinReactiveWithContext(
+ contextOrchestrate: ContextOrchestrate>,
+ contextRollback: ContextRollback>? = null,
): DefaultOrchestrateChain
fun commit(
- function: OrchestrateFunction,
- rollback: RollbackFunction? = null,
+ orchestrate: Orchestrate,
+ rollback: Rollback? = null,
): Orchestrator
fun commitReactive(
- function: OrchestrateFunction>,
- rollback: RollbackFunction>? = null,
+ orchestrate: Orchestrate>,
+ rollback: Rollback>? = null,
+ ): Orchestrator
+
+ fun commitWithContext(
+ contextOrchestrate: ContextOrchestrate,
+ contextRollback: ContextRollback? = null,
+ ): Orchestrator
+
+ fun commitReactiveWithContext(
+ contextOrchestrate: ContextOrchestrate>,
+ contextRollback: ContextRollback>? = null,
): Orchestrator
interface Pre {
fun start(
- function: OrchestrateFunction,
- rollback: RollbackFunction? = null,
+ orchestrate: Orchestrate,
+ rollback: Rollback? = null,
): DefaultOrchestrateChain
fun startReactive(
- function: OrchestrateFunction>,
- rollback: RollbackFunction>? = null,
+ orchestrate: Orchestrate>,
+ rollback: Rollback>? = null,
): DefaultOrchestrateChain
+ fun startWithContext(
+ contextOrchestrate: ContextOrchestrate,
+ contextRollback: ContextRollback? = null,
+ ): DefaultOrchestrateChain
+
+ fun startReactiveWithContext(
+ contextOrchestrate: ContextOrchestrate>,
+ contextRollback: ContextRollback>? = null,
+ ): DefaultOrchestrateChain
}
}
diff --git a/src/main/kotlin/org/rooftop/netx/api/RollbackFunction.kt b/src/main/kotlin/org/rooftop/netx/api/Rollback.kt
similarity index 55%
rename from src/main/kotlin/org/rooftop/netx/api/RollbackFunction.kt
rename to src/main/kotlin/org/rooftop/netx/api/Rollback.kt
index 6b8831c..088a204 100644
--- a/src/main/kotlin/org/rooftop/netx/api/RollbackFunction.kt
+++ b/src/main/kotlin/org/rooftop/netx/api/Rollback.kt
@@ -1,6 +1,6 @@
package org.rooftop.netx.api
-fun interface RollbackFunction {
+fun interface Rollback {
fun rollback(request: T): V
}
diff --git a/src/main/kotlin/org/rooftop/netx/api/TypeReference.kt b/src/main/kotlin/org/rooftop/netx/api/TypeReference.kt
new file mode 100644
index 0000000..aecd8ed
--- /dev/null
+++ b/src/main/kotlin/org/rooftop/netx/api/TypeReference.kt
@@ -0,0 +1,14 @@
+package org.rooftop.netx.api
+
+import java.lang.reflect.ParameterizedType
+import java.lang.reflect.Type
+
+
+abstract class TypeReference() {
+ val type: Type
+
+ init {
+ val superClass: Type = this.javaClass.genericSuperclass
+ type = (superClass as ParameterizedType).actualTypeArguments[0]
+ }
+}
diff --git a/src/main/kotlin/org/rooftop/netx/engine/AbstractOrchestrateListener.kt b/src/main/kotlin/org/rooftop/netx/engine/AbstractOrchestrateListener.kt
index f2739a2..26b70cd 100644
--- a/src/main/kotlin/org/rooftop/netx/engine/AbstractOrchestrateListener.kt
+++ b/src/main/kotlin/org/rooftop/netx/engine/AbstractOrchestrateListener.kt
@@ -1,6 +1,7 @@
package org.rooftop.netx.engine
import org.rooftop.netx.api.Codec
+import org.rooftop.netx.api.Context
import org.rooftop.netx.api.TransactionEvent
import org.rooftop.netx.api.TransactionManager
import reactor.core.publisher.Mono
@@ -42,30 +43,39 @@ internal abstract class AbstractOrchestrateListener internal c
castableType = type
}
- internal fun Mono.setNextCastableType(): Mono {
- return this.doOnNext {
- nextOrchestrateListener?.castableType = it::class
- nextRollbackOrchestrateListener?.castableType = it::class
+ internal fun Mono>.setNextCastableType(): Mono> {
+ return this.doOnNext { (request, _) ->
+ nextOrchestrateListener?.castableType = request::class
+ nextRollbackOrchestrateListener?.castableType = request::class
}
}
- protected fun Mono<*>.getHeldRequest(transactionEvent: TransactionEvent): Mono {
- return this.flatMap {
+ protected fun Mono.getHeldRequest(transactionEvent: TransactionEvent): Mono> {
+ return this.flatMap { event ->
requestHolder.getRequest(
"${transactionEvent.transactionId}:$orchestrateSequence",
getCastableType()
- )
+ ).map { it to event }
}
}
- protected fun Mono.holdRequestIfRollbackable(transactionEvent: TransactionEvent): Mono {
- return this.flatMap { request ->
- if (!isRollbackable) {
- Mono.just(request)
- }
- requestHolder.setRequest(
- "${transactionEvent.transactionId}:$orchestrateSequence",
- request
+ protected fun holdRequestIfRollbackable(request: T, transactionId: String): Mono {
+ if (!isRollbackable) {
+ Mono.just(request)
+ }
+ return requestHolder.setRequest(
+ "$transactionId:$orchestrateSequence",
+ request
+ )
+ }
+
+ protected fun Mono>.toOrchestrateEvent(): Mono {
+ return this.map { (response, context) ->
+ OrchestrateEvent(
+ orchestratorId = orchestratorId,
+ orchestrateSequence = orchestrateSequence + 1,
+ clientEvent = codec.encode(response),
+ context = codec.encode(context.contexts),
)
}
}
@@ -105,7 +115,12 @@ internal abstract class AbstractOrchestrateListener internal c
orchestrateEvent: OrchestrateEvent,
) {
val rollbackOrchestrateEvent =
- OrchestrateEvent(orchestrateEvent.orchestratorId, rollbackSequence, "")
+ OrchestrateEvent(
+ orchestrateEvent.orchestratorId,
+ rollbackSequence,
+ "",
+ orchestrateEvent.context,
+ )
transactionManager.rollback(
transactionId = transactionId,
cause = throwable.message ?: throwable.localizedMessage,
diff --git a/src/main/kotlin/org/rooftop/netx/engine/DefaultOrchestrateChain.kt b/src/main/kotlin/org/rooftop/netx/engine/DefaultOrchestrateChain.kt
index 6f1e118..6ea5df3 100644
--- a/src/main/kotlin/org/rooftop/netx/engine/DefaultOrchestrateChain.kt
+++ b/src/main/kotlin/org/rooftop/netx/engine/DefaultOrchestrateChain.kt
@@ -11,17 +11,55 @@ class DefaultOrchestrateChain private constru
private val orchestrateListener: AbstractOrchestrateListener,
private val rollbackOrchestrateListener: AbstractOrchestrateListener?,
private val beforeDefaultOrchestrateChain: DefaultOrchestrateChain? = null,
-): OrchestrateChain {
+) : OrchestrateChain {
private var nextDefaultOrchestrateChain: DefaultOrchestrateChain? = null
override fun join(
- function: OrchestrateFunction,
- rollback: RollbackFunction?,
+ orchestrate: Orchestrate,
+ rollback: Rollback?,
): DefaultOrchestrateChain {
- val nextJoinOrchestrateListener = getJoinOrchestrateListener(function)
- val nextRollbackOrchestrateListener = getRollbackOrchestrateListener(rollback)
+ val nextJoinOrchestrateListener =
+ getJoinOrchestrateListener(CommandType.DEFAULT, orchestrate)
+ val nextRollbackOrchestrateListener =
+ getRollbackOrchestrateListener(CommandType.DEFAULT, rollback)
+ return nextOrchestrateChain(nextJoinOrchestrateListener, nextRollbackOrchestrateListener)
+ }
+
+ override fun joinWithContext(
+ contextOrchestrate: ContextOrchestrate,
+ contextRollback: ContextRollback?
+ ): DefaultOrchestrateChain {
+ val nextJoinOrchestrateListener =
+ getJoinOrchestrateListener(CommandType.CONTEXT, contextOrchestrate)
+ val nextRollbackOrchestrateListener =
+ getRollbackOrchestrateListener(CommandType.CONTEXT, contextRollback)
+
+ return nextOrchestrateChain(nextJoinOrchestrateListener, nextRollbackOrchestrateListener)
+ }
+
+ private fun getJoinOrchestrateListener(
+ commandType: CommandType,
+ function: Any,
+ ) = JoinOrchestrateListener(
+ codec = chainContainer.codec,
+ transactionManager = chainContainer.transactionManager,
+ orchestratorId = orchestratorId,
+ orchestrateSequence = orchestrateSequence + 1,
+ orchestrateCommand = OrchestrateCommand(
+ commandType,
+ chainContainer.codec,
+ function
+ ),
+ requestHolder = chainContainer.requestHolder,
+ resultHolder = chainContainer.resultHolder,
+ )
+
+ private fun nextOrchestrateChain(
+ nextJoinOrchestrateListener: JoinOrchestrateListener,
+ nextRollbackOrchestrateListener: RollbackOrchestrateListener?
+ ): DefaultOrchestrateChain {
val nextDefaultOrchestrateChain = DefaultOrchestrateChain(
orchestratorId,
orchestrateSequence + 1,
@@ -35,24 +73,61 @@ class DefaultOrchestrateChain private constru
return nextDefaultOrchestrateChain
}
- private fun getJoinOrchestrateListener(function: OrchestrateFunction) =
- JoinOrchestrateListener(
- codec = chainContainer.codec,
- transactionManager = chainContainer.transactionManager,
- orchestratorId = orchestratorId,
- orchestrateSequence = orchestrateSequence + 1,
- orchestrateFunction = function,
- requestHolder = chainContainer.requestHolder,
- resultHolder = chainContainer.resultHolder,
- )
-
override fun joinReactive(
- function: OrchestrateFunction>,
- rollback: RollbackFunction>?,
+ orchestrate: Orchestrate>,
+ rollback: Rollback>?,
+ ): DefaultOrchestrateChain {
+ val nextJoinOrchestrateListener =
+ getMonoJoinOrchestrateListener(CommandType.DEFAULT, orchestrate)
+ val nextRollbackOrchestrateListener =
+ getMonoRollbackOrchestrateListener(CommandType.DEFAULT, rollback)
+
+ return nextOrchestrateChain(nextJoinOrchestrateListener, nextRollbackOrchestrateListener)
+ }
+
+ override fun joinReactiveWithContext(
+ contextOrchestrate: ContextOrchestrate>,
+ contextRollback: ContextRollback>?
): DefaultOrchestrateChain {
- val nextJoinOrchestrateListener = getMonoJoinOrchestrateListener(function)
- val nextRollbackOrchestrateListener = getMonoRollbackOrchestrateListener(rollback)
+ val nextJoinOrchestrateListener =
+ getMonoJoinOrchestrateListener(CommandType.CONTEXT, contextOrchestrate)
+ val nextRollbackOrchestrateListener =
+ getMonoRollbackOrchestrateListener(CommandType.CONTEXT, contextRollback)
+
+ val nextDefaultOrchestrateChain = DefaultOrchestrateChain(
+ orchestratorId,
+ orchestrateSequence + 1,
+ chainContainer,
+ nextJoinOrchestrateListener,
+ nextRollbackOrchestrateListener,
+ this,
+ )
+ this.nextDefaultOrchestrateChain = nextDefaultOrchestrateChain
+ return nextDefaultOrchestrateChain
+ }
+
+ private fun getMonoJoinOrchestrateListener(
+ commandType: CommandType,
+ function: Any,
+ ) = MonoJoinOrchestrateListener(
+ codec = chainContainer.codec,
+ transactionManager = chainContainer.transactionManager,
+ orchestratorId = orchestratorId,
+ orchestrateSequence = orchestrateSequence + 1,
+ monoOrchestrateCommand = MonoOrchestrateCommand(
+ commandType,
+ chainContainer.codec,
+ function
+ ),
+ requestHolder = chainContainer.requestHolder,
+ resultHolder = chainContainer.resultHolder,
+ )
+
+ private fun nextOrchestrateChain(
+ nextJoinOrchestrateListener: MonoJoinOrchestrateListener,
+ nextRollbackOrchestrateListener: MonoRollbackOrchestrateListener?
+ ): DefaultOrchestrateChain {
val nextDefaultOrchestrateChain = DefaultOrchestrateChain(
orchestratorId,
orchestrateSequence + 1,
@@ -66,24 +141,62 @@ class DefaultOrchestrateChain private constru
return nextDefaultOrchestrateChain
}
- private fun getMonoJoinOrchestrateListener(function: OrchestrateFunction>) =
- MonoJoinOrchestrateListener(
+ override fun commit(
+ orchestrate: Orchestrate,
+ rollback: Rollback?,
+ ): Orchestrator {
+ val nextCommitOrchestrateListener =
+ getCommitOrchestrateListener(CommandType.DEFAULT, orchestrate)
+ val nextRollbackOrchestrateListener =
+ getRollbackOrchestrateListener(CommandType.DEFAULT, rollback)
+
+ return createOrchestrator(nextCommitOrchestrateListener, nextRollbackOrchestrateListener)
+ }
+
+ override fun commitWithContext(
+ contextOrchestrate: ContextOrchestrate,
+ contextRollback: ContextRollback?
+ ): Orchestrator {
+ val nextCommitOrchestrateListener =
+ getCommitOrchestrateListener(CommandType.CONTEXT, contextOrchestrate)
+ val nextRollbackOrchestrateListener =
+ getRollbackOrchestrateListener(CommandType.CONTEXT, contextRollback)
+
+ return createOrchestrator(nextCommitOrchestrateListener, nextRollbackOrchestrateListener)
+ }
+
+ private fun getCommitOrchestrateListener(
+ commandType: CommandType,
+ function: Any,
+ ) = CommitOrchestrateListener(
+ codec = chainContainer.codec,
+ transactionManager = chainContainer.transactionManager,
+ orchestratorId = orchestratorId,
+ orchestrateSequence = orchestrateSequence + 1,
+ orchestrateCommand = OrchestrateCommand(commandType, chainContainer.codec, function),
+ resultHolder = chainContainer.resultHolder,
+ requestHolder = chainContainer.requestHolder,
+ )
+
+ private fun getRollbackOrchestrateListener(
+ commandType: CommandType,
+ rollback: Any?
+ ) = rollback?.let {
+ RollbackOrchestrateListener(
codec = chainContainer.codec,
transactionManager = chainContainer.transactionManager,
orchestratorId = orchestratorId,
orchestrateSequence = orchestrateSequence + 1,
- orchestrateFunction = function,
+ rollbackCommand = RollbackCommand(commandType, chainContainer.codec, it),
requestHolder = chainContainer.requestHolder,
resultHolder = chainContainer.resultHolder,
)
+ }
- override fun commit(
- function: OrchestrateFunction,
- rollback: RollbackFunction?,
+ private fun createOrchestrator(
+ nextCommitOrchestrateListener: CommitOrchestrateListener,
+ nextRollbackOrchestrateListener: RollbackOrchestrateListener?
): Orchestrator {
- val nextCommitOrchestrateListener = getCommitOrchestrateListener(function)
- val nextRollbackOrchestrateListener = getRollbackOrchestrateListener(rollback)
-
return OrchestratorCache.cache(orchestratorId) {
val nextDefaultOrchestrateChain = DefaultOrchestrateChain(
orchestratorId,
@@ -107,37 +220,34 @@ class DefaultOrchestrateChain private constru
}
}
- private fun getCommitOrchestrateListener(function: OrchestrateFunction) =
- CommitOrchestrateListener(
- codec = chainContainer.codec,
- transactionManager = chainContainer.transactionManager,
- orchestratorId = orchestratorId,
- orchestrateSequence = orchestrateSequence + 1,
- orchestrateFunction = function,
- resultHolder = chainContainer.resultHolder,
- requestHolder = chainContainer.requestHolder,
- )
+ override fun commitReactive(
+ orchestrate: Orchestrate>,
+ rollback: Rollback>?,
+ ): Orchestrator {
+ val nextJoinOrchestrateListener =
+ getMonoCommitOrchestrateListener(CommandType.DEFAULT, orchestrate)
+ val nextRollbackOrchestrateListener =
+ getMonoRollbackOrchestrateListener(CommandType.DEFAULT, rollback)
- private fun getRollbackOrchestrateListener(rollback: RollbackFunction?) =
- rollback?.let {
- RollbackOrchestrateListener(
- codec = chainContainer.codec,
- transactionManager = chainContainer.transactionManager,
- orchestratorId = orchestratorId,
- orchestrateSequence = orchestrateSequence + 1,
- rollbackFunction = it,
- requestHolder = chainContainer.requestHolder,
- resultHolder = chainContainer.resultHolder,
- )
- }
+ return createOrchestrator(nextJoinOrchestrateListener, nextRollbackOrchestrateListener)
+ }
- override fun commitReactive(
- function: OrchestrateFunction>,
- rollback: RollbackFunction>?,
+ override fun commitReactiveWithContext(
+ contextOrchestrate: ContextOrchestrate>,
+ contextRollback: ContextRollback>?
): Orchestrator {
- val nextJoinOrchestrateListener = getMonoCommitOrchestrateListener(function)
- val nextRollbackOrchestrateListener = getMonoRollbackOrchestrateListener(rollback)
+ val nextJoinOrchestrateListener =
+ getMonoCommitOrchestrateListener(CommandType.CONTEXT, contextOrchestrate)
+ val nextRollbackOrchestrateListener =
+ getMonoRollbackOrchestrateListener(CommandType.CONTEXT, contextRollback)
+
+ return createOrchestrator(nextJoinOrchestrateListener, nextRollbackOrchestrateListener)
+ }
+ private fun createOrchestrator(
+ nextJoinOrchestrateListener: MonoCommitOrchestrateListener,
+ nextRollbackOrchestrateListener: MonoRollbackOrchestrateListener?
+ ): Orchestrator {
return OrchestratorCache.cache(orchestratorId) {
val nextDefaultOrchestrateChain = DefaultOrchestrateChain(
orchestratorId,
@@ -178,7 +288,8 @@ class DefaultOrchestrateChain private constru
val orchestrateListeners = mutableListOf<
Pair, AbstractOrchestrateListener?>>()
- var defaultOrchestrateChainCursor: DefaultOrchestrateChain? = this
+ var defaultOrchestrateChainCursor: DefaultOrchestrateChain? =
+ this
while (defaultOrchestrateChainCursor != null) {
orchestrateListeners.add(
defaultOrchestrateChainCursor.orchestrateListener
@@ -187,7 +298,8 @@ class DefaultOrchestrateChain private constru
if (defaultOrchestrateChainCursor.beforeDefaultOrchestrateChain == null) {
break
}
- defaultOrchestrateChainCursor = defaultOrchestrateChainCursor.beforeDefaultOrchestrateChain
+ defaultOrchestrateChainCursor =
+ defaultOrchestrateChainCursor.beforeDefaultOrchestrateChain
}
orchestrateListeners.reverse()
@@ -246,29 +358,41 @@ class DefaultOrchestrateChain private constru
}
}
- private fun getMonoCommitOrchestrateListener(function: OrchestrateFunction>) =
- MonoCommitOrchestrateListener(
+ private fun getMonoCommitOrchestrateListener(
+ commandType: CommandType,
+ function: Any,
+ ) = MonoCommitOrchestrateListener(
+ codec = chainContainer.codec,
+ transactionManager = chainContainer.transactionManager,
+ orchestratorId = orchestratorId,
+ orchestrateSequence = orchestrateSequence + 1,
+ monoOrchestrateCommand = MonoOrchestrateCommand(
+ commandType,
+ chainContainer.codec,
+ function
+ ),
+ resultHolder = chainContainer.resultHolder,
+ requestHolder = chainContainer.requestHolder,
+ )
+
+ private fun getMonoRollbackOrchestrateListener(
+ commandType: CommandType,
+ rollback: Any?
+ ) = rollback?.let {
+ MonoRollbackOrchestrateListener(
codec = chainContainer.codec,
transactionManager = chainContainer.transactionManager,
orchestratorId = orchestratorId,
orchestrateSequence = orchestrateSequence + 1,
- orchestrateFunction = function,
- resultHolder = chainContainer.resultHolder,
+ monoRollbackCommand = MonoRollbackCommand(
+ commandType,
+ chainContainer.codec,
+ it
+ ),
requestHolder = chainContainer.requestHolder,
+ resultHolder = chainContainer.resultHolder,
)
-
- private fun getMonoRollbackOrchestrateListener(rollback: RollbackFunction>?) =
- rollback?.let {
- MonoRollbackOrchestrateListener(
- codec = chainContainer.codec,
- transactionManager = chainContainer.transactionManager,
- orchestratorId = orchestratorId,
- orchestrateSequence = orchestrateSequence + 1,
- rollbackFunction = it,
- requestHolder = chainContainer.requestHolder,
- resultHolder = chainContainer.resultHolder,
- )
- }
+ }
internal class Pre internal constructor(
private val orchestratorId: String,
@@ -277,14 +401,34 @@ class DefaultOrchestrateChain private constru
private val codec: Codec,
private val resultHolder: ResultHolder,
private val requestHolder: RequestHolder,
- ): OrchestrateChain.Pre {
+ ) : OrchestrateChain.Pre {
override fun start(
- function: OrchestrateFunction,
- rollback: RollbackFunction?,
+ orchestrate: Orchestrate,
+ rollback: Rollback?,
+ ): DefaultOrchestrateChain {
+ val startOrchestrateListener =
+ getStartOrchestrateListener(CommandType.DEFAULT, orchestrate)
+ val rollbackOrchestrateListener =
+ getRollbackOrchestrateListener(CommandType.DEFAULT, rollback)
+
+ return DefaultOrchestrateChain(
+ orchestratorId = orchestratorId,
+ orchestrateSequence = 0,
+ chainContainer = getStreamContainer(),
+ orchestrateListener = startOrchestrateListener,
+ rollbackOrchestrateListener = rollbackOrchestrateListener,
+ )
+ }
+
+ override fun startWithContext(
+ contextOrchestrate: ContextOrchestrate,
+ contextRollback: ContextRollback?
): DefaultOrchestrateChain {
- val startOrchestrateListener = getStartOrchestrateListener(function)
- val rollbackOrchestrateListener = getRollbackOrchestrateListener(rollback)
+ val startOrchestrateListener =
+ getStartOrchestrateListener(CommandType.CONTEXT, contextOrchestrate)
+ val rollbackOrchestrateListener =
+ getRollbackOrchestrateListener(CommandType.CONTEXT, contextRollback)
return DefaultOrchestrateChain(
orchestratorId = orchestratorId,
@@ -295,36 +439,50 @@ class DefaultOrchestrateChain private constru
)
}
- private fun getStartOrchestrateListener(function: OrchestrateFunction) =
- StartOrchestrateListener(
+ private fun getStartOrchestrateListener(
+ commandType: CommandType,
+ function: Any,
+ ) = StartOrchestrateListener(
+ codec = codec,
+ transactionManager = transactionManager,
+ orchestratorId = orchestratorId,
+ orchestrateSequence = 0,
+ orchestrateCommand = OrchestrateCommand(
+ commandType,
+ codec,
+ function,
+ ),
+ requestHolder = requestHolder,
+ resultHolder = resultHolder,
+ )
+
+ private fun getRollbackOrchestrateListener(
+ commandType: CommandType,
+ rollback: Any?
+ ) = rollback?.let {
+ RollbackOrchestrateListener(
codec = codec,
transactionManager = transactionManager,
orchestratorId = orchestratorId,
orchestrateSequence = 0,
- orchestrateFunction = function,
+ rollbackCommand = RollbackCommand(
+ commandType,
+ codec,
+ it
+ ),
requestHolder = requestHolder,
resultHolder = resultHolder,
)
-
- private fun getRollbackOrchestrateListener(rollback: RollbackFunction?) =
- rollback?.let {
- RollbackOrchestrateListener(
- codec = codec,
- transactionManager = transactionManager,
- orchestratorId = orchestratorId,
- orchestrateSequence = 0,
- rollbackFunction = it,
- requestHolder = requestHolder,
- resultHolder = resultHolder,
- )
- }
+ }
override fun startReactive(
- function: OrchestrateFunction>,
- rollback: RollbackFunction>?,
+ orchestrate: Orchestrate>,
+ rollback: Rollback>?,
): DefaultOrchestrateChain {
- val startOrchestrateListener = getMonoStartOrchestrateListener(function)
- val rollbackOrchestrateListener = getMonoRollbackOrchestrateListener