child_manager.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #pragma once
  2. //
  3. // Copyright (c) 2019-2022 Ivan Baidakou (basiliscos) (the dot dmol at gmail dot com)
  4. //
  5. // Distributed under the MIT Software License
  6. //
  7. #include "plugin_base.h"
  8. #include <unordered_set>
  9. #include "rotor/detail/child_info.h"
  10. namespace rotor::plugin {
  11. /** \struct child_manager_plugin_t
  12. *
  13. * \brief supervisor's plugin for child-actors housekeeping
  14. *
  15. * The plugin is responsible for child actors creation, initialization
  16. * and shutdown. The supervisor policy is also handled here, e.g.
  17. * if an child-actor fails to initialize self, the supervisor
  18. * might shutdown self.
  19. *
  20. */
  21. struct child_manager_plugin_t : public plugin_base_t {
  22. using plugin_base_t::plugin_base_t;
  23. /** The plugin unique identity to allow further static_cast'ing*/
  24. static const void *class_identity;
  25. const void *identity() const noexcept override;
  26. void activate(actor_base_t *actor) noexcept override;
  27. void deactivate() noexcept override;
  28. /** \brief pre-initializes child and sends create_child message to the supervisor */
  29. virtual void create_child(const actor_ptr_t &actor) noexcept;
  30. /** \brief records spawner (via generating a new address) and sends
  31. * spawn_actor_t message to the supersior */
  32. virtual void spawn(factory_t factory, const pt::time_duration &period, restart_policy_t policy, size_t max_attempts,
  33. bool escalate) noexcept;
  34. /** \brief removes the child from the supervisor
  35. *
  36. * If there was an error, the supervisor might trigger shutdown self (in accordance with policy)
  37. *
  38. * Otherwise, if all is ok, it continues the supervisor initialization
  39. */
  40. virtual void remove_child(const actor_base_t &actor) noexcept;
  41. /** \brief reaction on child shutdown failure
  42. *
  43. * By default it is forwarded to system context, which terminates program by default
  44. */
  45. virtual void on_shutdown_fail(actor_base_t &actor, const extended_error_ptr_t &ec) noexcept;
  46. /** \brief sends initialization request upon actor creation message */
  47. virtual void on_create(message::create_actor_t &message) noexcept;
  48. /** \brief reaction on (maybe unsuccessful) init confirmatinon
  49. *
  50. * Possibilities:
  51. * - shutdown child
  52. * - shutdown self (supervisor)
  53. * - continue init
  54. *
  55. */
  56. virtual void on_init(message::init_response_t &message) noexcept;
  57. /** \brief reaction on shutdown trigger
  58. *
  59. * Possibilities:
  60. * - shutdown self (supervisor), via shutdown request
  61. * - shutdown self (supervisor) directly, if there is no root supervisor
  62. * - shutdown via request for a child
  63. *
  64. */
  65. virtual void on_shutdown_trigger(message::shutdown_trigger_t &message) noexcept;
  66. /** \brief reaction on shutdown confirmation (i.e. perform some cleanings) */
  67. virtual void on_shutdown_confirm(message::shutdown_response_t &message) noexcept;
  68. /** \brief actually attempts to spawn a new actor via spawner */
  69. virtual void on_spawn(message::spawn_actor_t &message) noexcept;
  70. bool handle_init(message::init_request_t *) noexcept override;
  71. bool handle_shutdown(message::shutdown_request_t *) noexcept override;
  72. void handle_start(message::start_trigger_t *message) noexcept override;
  73. bool handle_unsubscription(const subscription_point_t &point, bool external) noexcept override;
  74. /** \brief generic non-public fields accessor */
  75. template <typename T> auto &access() noexcept;
  76. private:
  77. using actors_map_t = std::unordered_map<address_ptr_t, detail::child_info_ptr_t>;
  78. using spawning_map_t = std::unordered_map<request_id_t, detail::child_info_ptr_t>;
  79. bool has_initializing() const noexcept;
  80. void init_continue() noexcept;
  81. void request_shutdown(const extended_error_ptr_t &ec) noexcept;
  82. void cancel_init(const actor_base_t *child) noexcept;
  83. void request_shutdown(detail::child_info_t &child_state, const extended_error_ptr_t &ec) noexcept;
  84. void on_spawn_timer(request_id_t timer_id, bool cancelled) noexcept;
  85. size_t active_actors() noexcept;
  86. /** \brief type for keeping list of initializing actors (during supervisor inititalization) */
  87. using initializing_actors_t = std::unordered_set<address_ptr_t>;
  88. /** \brief local address to local actor (intrusive pointer) mapping */
  89. actors_map_t actors_map;
  90. spawning_map_t spawning_map;
  91. };
  92. } // namespace rotor::plugin