-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(saga): Add support for re-entrance of sagas (#4019)
- Loading branch information
1 parent
b03a28a
commit 515528e
Showing
12 changed files
with
334 additions
and
10 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
54 changes: 54 additions & 0 deletions
54
...in/com/netflix/spinnaker/clouddriver/saga/flow/seekers/SagaCommandCompletedEventSeeker.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,54 @@ | ||
/* | ||
* Copyright 2019 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.netflix.spinnaker.clouddriver.saga.flow.seekers | ||
|
||
import com.netflix.spinnaker.clouddriver.saga.SagaCommandCompleted | ||
import com.netflix.spinnaker.clouddriver.saga.flow.SagaFlow | ||
import com.netflix.spinnaker.clouddriver.saga.flow.Seeker | ||
import com.netflix.spinnaker.clouddriver.saga.flow.convertActionStepToCommandName | ||
import com.netflix.spinnaker.clouddriver.saga.models.Saga | ||
import org.slf4j.LoggerFactory | ||
|
||
/** | ||
* Seeks the [SagaFlowIterator] index to the next command following a [SagaCommandCompleted] event. | ||
*/ | ||
internal class SagaCommandCompletedEventSeeker : Seeker { | ||
|
||
private val log by lazy { LoggerFactory.getLogger(javaClass) } | ||
|
||
override fun invoke(currentIndex: Int, steps: List<SagaFlow.Step>, saga: Saga): Int? { | ||
val completionEvents = saga.getEvents().filterIsInstance<SagaCommandCompleted>() | ||
if (completionEvents.isEmpty()) { | ||
// If there are no completion events, we don't need to seek at all. | ||
return null | ||
} | ||
|
||
val lastCompletedCommand = completionEvents.last().command | ||
val step = steps | ||
.filterIsInstance<SagaFlow.ActionStep>() | ||
.find { convertActionStepToCommandName(it) == lastCompletedCommand } | ||
|
||
if (step == null) { | ||
// Not the end of the world if this seeker doesn't find a correlated step, but it's definitely an error case | ||
log.error("Could not find step associated with last completed command ($lastCompletedCommand)") | ||
return null | ||
} | ||
|
||
return (steps.indexOf(step) + 1).also { | ||
log.debug("Suggesting to seek index to $it") | ||
} | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...main/kotlin/com/netflix/spinnaker/clouddriver/saga/flow/seekers/SagaCommandEventSeeker.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,53 @@ | ||
/* | ||
* Copyright 2019 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.netflix.spinnaker.clouddriver.saga.flow.seekers | ||
|
||
import com.netflix.spinnaker.clouddriver.saga.SagaCommand | ||
import com.netflix.spinnaker.clouddriver.saga.flow.SagaFlow | ||
import com.netflix.spinnaker.clouddriver.saga.flow.Seeker | ||
import com.netflix.spinnaker.clouddriver.saga.flow.convertActionStepToCommandClass | ||
import com.netflix.spinnaker.clouddriver.saga.models.Saga | ||
import org.slf4j.LoggerFactory | ||
|
||
/** | ||
* Seeks the [SagaFlowIterator] index to the next incomplete, but committed [SagaCommand]. | ||
*/ | ||
internal class SagaCommandEventSeeker : Seeker { | ||
|
||
private val log by lazy { LoggerFactory.getLogger(javaClass) } | ||
|
||
override fun invoke(currentIndex: Int, steps: List<SagaFlow.Step>, saga: Saga): Int? { | ||
val commands = saga.getEvents().filterIsInstance<SagaCommand>() | ||
if (commands.isEmpty()) { | ||
// No commands, nothing to seek to | ||
return null | ||
} | ||
|
||
val lastCommand = commands.last() | ||
val step = steps | ||
.filterIsInstance<SagaFlow.ActionStep>() | ||
.find { convertActionStepToCommandClass(it) == lastCommand } | ||
|
||
if (step == null) { | ||
log.error("Could not find step associated with last incomplete command ($lastCommand)") | ||
return null | ||
} | ||
|
||
return (steps.indexOf(step) + 1).also { | ||
log.debug("Suggesting to seek index to $it") | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
clouddriver-saga/src/main/kotlin/com/netflix/spinnaker/clouddriver/saga/flow/util.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,43 @@ | ||
/* | ||
* Copyright 2019 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.netflix.spinnaker.clouddriver.saga.flow | ||
|
||
import com.netflix.spinnaker.clouddriver.saga.SagaCommand | ||
import com.netflix.spinnaker.clouddriver.saga.exceptions.SagaSystemException | ||
import com.netflix.spinnaker.clouddriver.saga.getStepCommandName | ||
import org.springframework.core.ResolvableType | ||
|
||
/** | ||
* Derives a [SagaCommand] name from a [SagaFlow.ActionStep]. | ||
*/ | ||
internal fun convertActionStepToCommandName(step: SagaFlow.ActionStep): String = | ||
getStepCommandName(convertActionStepToCommandClass(step)) | ||
|
||
/** | ||
* Derives a [SagaCommand] Class from a [SagaFlow.ActionStep]. | ||
*/ | ||
internal fun convertActionStepToCommandClass(step: SagaFlow.ActionStep): Class<SagaCommand> { | ||
val actionType = ResolvableType.forClass(step.action) | ||
.also { it.resolve() } | ||
|
||
val commandType = actionType.interfaces | ||
.find { SagaAction::class.java.isAssignableFrom(it.rawClass!!) } | ||
?.getGeneric(0) | ||
?: throw SagaSystemException("Could not resolve SagaCommand type from ActionStep: $step") | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
return commandType.rawClass as Class<SagaCommand> | ||
} |
34 changes: 34 additions & 0 deletions
34
clouddriver-saga/src/main/kotlin/com/netflix/spinnaker/clouddriver/saga/util.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,34 @@ | ||
/* | ||
* Copyright 2019 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.netflix.spinnaker.clouddriver.saga | ||
|
||
import com.fasterxml.jackson.annotation.JsonTypeName | ||
|
||
/** | ||
* Get the name of the provided [command] instance. | ||
* | ||
*/ | ||
internal fun getStepCommandName(command: SagaCommand): String = | ||
getStepCommandName(command.javaClass) | ||
|
||
/** | ||
* Get the name of the provided [commandClass]. | ||
* | ||
* TODO(rz): Do we want our own annotation instead of relying on [JsonTypeName]? | ||
*/ | ||
internal fun getStepCommandName(commandClass: Class<SagaCommand>): String = | ||
commandClass.getAnnotation(JsonTypeName::class.java)?.value ?: commandClass.simpleName |
Oops, something went wrong.