Programing/Akka

Akka Coordinated Shutdown

BUST 2018. 12. 11. 23:15

Akka Coordinated Shutdown

  • Akka의 Actor System 종료 과정에서 특정 Actor 또는 서비스에 대해 작동을 요청을 할수가 있다.
  • 밀린 메시지 처리 및 Connect 정리 등에 활용 될수 있는 기능이다.

Configuration

# CoordinatedShutdown is enabled by default and will run the tasks that
# are added to these phases by individual Akka modules and user logic.
#
# The phases are ordered as a DAG by defining the dependencies between the phases
# to make sure shutdown tasks are run in the right order.
#
# In general user tasks belong in the first few phases, but there may be use
# cases where you would want to hook in new phases or register tasks later in
# the DAG.
#
# Each phase is defined as a named config section with the
# following optional properties:
# - timeout=15s: Override the default-phase-timeout for this phase.
# - recover=off: If the phase fails the shutdown is aborted
#                and depending phases will not be executed.
# - enabled=off: Skip all tasks registered in this phase. DO NOT use
#                this to disable phases unless you are absolutely sure what the
#                consequences are. Many of the built in tasks depend on other tasks
#                having been executed in earlier phases and may break if those are disabled.
# depends-on=[]: Run the phase after the given phases
phases {

  # The first pre-defined phase that applications can add tasks to.
  # Note that more phases can be added in the application's
  # configuration by overriding this phase with an additional
  # depends-on.
  before-service-unbind {
  }

  # Stop accepting new incoming connections.
  # This is where you can register tasks that makes a server stop accepting new connections. Already
  # established connections should be allowed to continue and complete if possible.
  service-unbind {
    depends-on = [before-service-unbind]
  }

  # Wait for requests that are in progress to be completed.
  # This is where you register tasks that will wait for already established connections to complete, potentially
  # also first telling them that it is time to close down.
  service-requests-done {
    depends-on = [service-unbind]
  }

  # Final shutdown of service endpoints.
  # This is where you would add tasks that forcefully kill connections that are still around.
  service-stop {
    depends-on = [service-requests-done]
  }

  # Phase for custom application tasks that are to be run
  # after service shutdown and before cluster shutdown.
  before-cluster-shutdown {
    depends-on = [service-stop]
  }

  # Graceful shutdown of the Cluster Sharding regions.
  # This phase is not meant for users to add tasks to.
  cluster-sharding-shutdown-region {
    timeout = 10 s
    depends-on = [before-cluster-shutdown]
  }

  # Emit the leave command for the node that is shutting down.
  # This phase is not meant for users to add tasks to.
  cluster-leave {
    depends-on = [cluster-sharding-shutdown-region]
  }

  # Shutdown cluster singletons
  # This is done as late as possible to allow the shard region shutdown triggered in
  # the "cluster-sharding-shutdown-region" phase to complete before the shard coordinator is shut down.
  # This phase is not meant for users to add tasks to.
  cluster-exiting {
    timeout = 10 s
    depends-on = [cluster-leave]
  }

  # Wait until exiting has been completed
  # This phase is not meant for users to add tasks to.
  cluster-exiting-done {
    depends-on = [cluster-exiting]
  }

  # Shutdown the cluster extension
  # This phase is not meant for users to add tasks to.
  cluster-shutdown {
    depends-on = [cluster-exiting-done]
  }

  # Phase for custom application tasks that are to be run
  # after cluster shutdown and before ActorSystem termination.
  before-actor-system-terminate {
    depends-on = [cluster-shutdown]
  }

  # Last phase. See terminate-actor-system and exit-jvm above.
  # Don't add phases that depends on this phase because the
  # dispatcher and scheduler of the ActorSystem have been shutdown.
  # This phase is not meant for users to add tasks to.
  actor-system-terminate {
    timeout = 10 s
    depends-on = [before-actor-system-terminate]
  }
}
  • `akka.coordinated-shutdown.phases` 에 정의가 되어있다.


Shutdown Phase에 Task 추가하기

CoordinatedShutdown.get(system).addTask(
  CoordinatedShutdown.PhaseBeforeServiceUnbind(), "someTaskName",
  () -> {
    return akka.pattern.Patterns.ask(someActor, "stop", Duration.ofSeconds(5))
      .thenApply(reply -> Done.getInstance());
});
  • Code를 이용하여 Phase에 필요한 작업을 추가를 할수가 있다.
  • 리턴 값으로는 `CompletionStage<Done>`이 반드시 리턴이 되어야 한다.
  • taskName은 debug용, logging용으로 사용되는 이름.

Etc

  •  Coordinated Shutdown는 Jvm의 process exit에 의해 실행이 된다. jvm에 의해 종료되는 것이 아닌 custom이 필요한 경우에는 설정 등을 통해 할수있다. 자세한 내용은 문서를 참고

Reference



'Programing > Akka' 카테고리의 다른 글

Akka Stream Materialized values  (0) 2018.11.24
Akka Actor Hierarchy  (0) 2018.10.22
Akka Actor Life Cycle  (0) 2018.10.21
Akka Pulling Pattern  (0) 2018.10.13
Akka with Spring  (0) 2018.09.29