Skip to content

Commit

Permalink
chore(saga): Adding logs to SagaService (#3945)
Browse files Browse the repository at this point in the history
  • Loading branch information
robzienert authored Aug 15, 2019
1 parent f71feb2 commit fe186a2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ import com.netflix.spectator.api.Registry
import com.netflix.spinnaker.clouddriver.saga.exceptions.SagaIntegrationException
import com.netflix.spinnaker.clouddriver.saga.exceptions.SagaMissingRequiredCommandException
import com.netflix.spinnaker.clouddriver.saga.exceptions.SagaNotFoundException
import com.netflix.spinnaker.clouddriver.saga.exceptions.SagaSystemException
import com.netflix.spinnaker.clouddriver.saga.flow.SagaAction
import com.netflix.spinnaker.clouddriver.saga.flow.SagaFlow
import com.netflix.spinnaker.clouddriver.saga.flow.SagaFlowIterator
import com.netflix.spinnaker.clouddriver.saga.models.Saga
import com.netflix.spinnaker.clouddriver.saga.persistence.SagaRepository
import com.netflix.spinnaker.kork.exceptions.SpinnakerException
import com.netflix.spinnaker.kork.exceptions.SystemException
import org.slf4j.LoggerFactory
import org.springframework.context.ApplicationContext
import org.springframework.context.ApplicationContextAware
import org.springframework.core.ResolvableType
Expand Down Expand Up @@ -55,8 +56,6 @@ import org.springframework.core.ResolvableType
*
* val result = sagaService.applyBlocking(flow, DoMyAction())
* ```
*
* TODO(rz): SagaAction timing metrics?
*/
@Beta
class SagaService(
Expand All @@ -66,13 +65,17 @@ class SagaService(

private lateinit var applicationContext: ApplicationContext

private val sagaInvocationId = registry.createId("sagas.invocations")
private val log by lazy { LoggerFactory.getLogger(javaClass) }

private val actionInvocationsId = registry.createId("sagas.actions.invocations")

fun <T> applyBlocking(flow: SagaFlow, startingCommand: SagaCommand): T? {
val initialSaga = initializeSaga(startingCommand)

log.info("Applying saga: ${initialSaga.name}/${initialSaga.id}")

if (initialSaga.isComplete()) {
log.info("Saga already complete, exiting early: ${initialSaga.name}/${initialSaga.id}")
return invokeCompletionHandler(initialSaga, flow)
}

Expand All @@ -82,19 +85,22 @@ class SagaService(
val saga = flowState.saga
val action = flowState.action

log.debug("Applying saga action ${action.javaClass.simpleName} for ${saga.name}/${saga.id}")

val requiredCommand: Class<SagaCommand> = getRequiredCommand(action)
if (!saga.completed(requiredCommand)) {
val stepCommand = saga.getNextCommand(requiredCommand)
?: throw SagaMissingRequiredCommandException("Missing required command ${requiredCommand.simpleName}")

// TODO(rz): error handling
val result = try {
action.apply(stepCommand, saga).also {
registry
.counter(actionInvocationsId.withTags("result", "success", "action", action.javaClass.simpleName))
.increment()
}
} catch (e: Exception) {
log.error(
"Encountered error while applying action '${action.javaClass.simpleName}' on ${saga.name}/${saga.id}", e)
saga.addEvent(SagaActionErrorOccurred(
sagaName = saga.name,
sagaId = saga.id,
Expand Down Expand Up @@ -149,6 +155,7 @@ class SagaService(
return sagaRepository.get(command.sagaName, command.sagaId)
?: Saga(command.sagaName, command.sagaId)
.also {
log.debug("Initializing new saga: ${it.name}/${it.id}")
it.addEvent(command)
sagaRepository.save(it)
}
Expand Down Expand Up @@ -184,7 +191,7 @@ class SagaService(
@Suppress("UNCHECKED_CAST")
return rawClass as Class<SagaCommand>
}
throw SystemException("not a command")
throw SagaSystemException("Resolved next action is not a SagaCommand: ${rawClass.simpleName}")
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ package com.netflix.spinnaker.clouddriver.saga.controllers

import com.netflix.spinnaker.clouddriver.saga.models.Saga
import com.netflix.spinnaker.clouddriver.saga.persistence.SagaRepository
import com.netflix.spinnaker.clouddriver.saga.persistence.SagaRepository.ListCriteria
import com.netflix.spinnaker.kork.web.exceptions.NotFoundException
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController

@RequestMapping("/saga")
Expand All @@ -29,6 +31,28 @@ class SagaController(
private val sagaRepository: SagaRepository
) {

@GetMapping
fun listAll(
@RequestParam("running", required = false) running: Boolean? = null,
@RequestParam("names", required = false) names: List<String>? = null
): List<Saga> {
return sagaRepository.list(ListCriteria(
running = running,
names = names
))
}

@GetMapping("/{name}")
fun listByName(
@PathVariable("name") name: String,
@RequestParam("running", required = false) running: Boolean? = null
): List<Saga> {
return sagaRepository.list(ListCriteria(
running = running,
names = listOf(name)
))
}

@GetMapping("/{name}/{id}")
fun get(@PathVariable("name") name: String, @PathVariable("id") id: String): Saga {
return sagaRepository.get(name, id)
Expand Down

0 comments on commit fe186a2

Please sign in to comment.