1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
9 #include <seastar/core/shared_future.hh>
13 namespace crimson::osd
{
15 // seastar::sharded puts start_single on core 0
16 constexpr core_id_t PRIMARY_CORE
= 0;
21 * Maintains state representing the OSD's progress from booting through
24 * Shards other than PRIMARY_CORE may use their local instance to check
25 * on ACTIVE and STOPPING. All other methods are restricted to
26 * PRIMARY_CORE (such methods start with an assert to this effect).
28 class OSDState
: public seastar::peering_sharded_service
<OSDState
> {
40 State state
= State::INITIALIZING
;
41 mutable seastar::shared_promise
<> wait_for_active
;
43 /// Sets local instance state to active, called from set_active
45 state
= State::ACTIVE
;
46 wait_for_active
.set_value();
49 /// Sets local instance state to stopping, called from set_stopping
50 void _set_stopping() {
51 state
= State::STOPPING
;
52 wait_for_active
.set_exception(crimson::common::system_shutdown_exception
{});
56 bool is_initializing() const {
57 ceph_assert(seastar::this_shard_id() == PRIMARY_CORE
);
58 return state
== State::INITIALIZING
;
60 bool is_preboot() const {
61 ceph_assert(seastar::this_shard_id() == PRIMARY_CORE
);
62 return state
== State::PREBOOT
;
64 bool is_booting() const {
65 ceph_assert(seastar::this_shard_id() == PRIMARY_CORE
);
66 return state
== State::BOOTING
;
68 bool is_active() const {
69 return state
== State::ACTIVE
;
71 seastar::future
<> when_active() const {
72 return is_active() ? seastar::now()
73 : wait_for_active
.get_shared_future();
75 bool is_prestop() const {
76 ceph_assert(seastar::this_shard_id() == PRIMARY_CORE
);
77 return state
== State::PRESTOP
;
79 bool is_stopping() const {
80 return state
== State::STOPPING
;
82 bool is_waiting_for_healthy() const {
83 ceph_assert(seastar::this_shard_id() == PRIMARY_CORE
);
84 return state
== State::WAITING_FOR_HEALTHY
;
87 ceph_assert(seastar::this_shard_id() == PRIMARY_CORE
);
88 state
= State::PREBOOT
;
91 ceph_assert(seastar::this_shard_id() == PRIMARY_CORE
);
92 state
= State::BOOTING
;
94 /// Sets all shards to active
95 seastar::future
<> set_active() {
96 ceph_assert(seastar::this_shard_id() == PRIMARY_CORE
);
97 return container().invoke_on_all([](auto& osd_state
) {
98 osd_state
._set_active();
102 ceph_assert(seastar::this_shard_id() == PRIMARY_CORE
);
103 state
= State::PRESTOP
;
105 /// Sets all shards to stopping
106 seastar::future
<> set_stopping() {
107 ceph_assert(seastar::this_shard_id() == PRIMARY_CORE
);
108 return container().invoke_on_all([](auto& osd_state
) {
109 osd_state
._set_stopping();
112 std::string_view
to_string() const {
114 case State::INITIALIZING
: return "initializing";
115 case State::PREBOOT
: return "preboot";
116 case State::BOOTING
: return "booting";
117 case State::ACTIVE
: return "active";
118 case State::PRESTOP
: return "prestop";
119 case State::STOPPING
: return "stopping";
120 case State::WAITING_FOR_HEALTHY
: return "waiting_for_healthy";
121 default: return "???";
127 operator<<(std::ostream
& os
, const OSDState
& s
) {
128 return os
<< s
.to_string();