]> git.proxmox.com Git - ceph.git/blob - ceph/src/crimson/osd/state.h
update ceph source to reef 18.2.1
[ceph.git] / ceph / src / crimson / osd / state.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #pragma once
5
6 #include <string_view>
7 #include <ostream>
8
9 #include <seastar/core/shared_future.hh>
10
11 class OSDMap;
12
13 namespace crimson::osd {
14
15 // seastar::sharded puts start_single on core 0
16 constexpr core_id_t PRIMARY_CORE = 0;
17
18 /**
19 * OSDState
20 *
21 * Maintains state representing the OSD's progress from booting through
22 * shutdown.
23 *
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).
27 */
28 class OSDState : public seastar::peering_sharded_service<OSDState> {
29
30 enum class State {
31 INITIALIZING,
32 PREBOOT,
33 BOOTING,
34 ACTIVE,
35 PRESTOP,
36 STOPPING,
37 WAITING_FOR_HEALTHY,
38 };
39
40 State state = State::INITIALIZING;
41 mutable seastar::shared_promise<> wait_for_active;
42
43 /// Sets local instance state to active, called from set_active
44 void _set_active() {
45 state = State::ACTIVE;
46 wait_for_active.set_value();
47 wait_for_active = {};
48 }
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{});
53 wait_for_active = {};
54 }
55 public:
56 bool is_initializing() const {
57 ceph_assert(seastar::this_shard_id() == PRIMARY_CORE);
58 return state == State::INITIALIZING;
59 }
60 bool is_preboot() const {
61 ceph_assert(seastar::this_shard_id() == PRIMARY_CORE);
62 return state == State::PREBOOT;
63 }
64 bool is_booting() const {
65 ceph_assert(seastar::this_shard_id() == PRIMARY_CORE);
66 return state == State::BOOTING;
67 }
68 bool is_active() const {
69 return state == State::ACTIVE;
70 }
71 seastar::future<> when_active() const {
72 return is_active() ? seastar::now()
73 : wait_for_active.get_shared_future();
74 };
75 bool is_prestop() const {
76 ceph_assert(seastar::this_shard_id() == PRIMARY_CORE);
77 return state == State::PRESTOP;
78 }
79 bool is_stopping() const {
80 return state == State::STOPPING;
81 }
82 bool is_waiting_for_healthy() const {
83 ceph_assert(seastar::this_shard_id() == PRIMARY_CORE);
84 return state == State::WAITING_FOR_HEALTHY;
85 }
86 void set_preboot() {
87 ceph_assert(seastar::this_shard_id() == PRIMARY_CORE);
88 state = State::PREBOOT;
89 }
90 void set_booting() {
91 ceph_assert(seastar::this_shard_id() == PRIMARY_CORE);
92 state = State::BOOTING;
93 }
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();
99 });
100 }
101 void set_prestop() {
102 ceph_assert(seastar::this_shard_id() == PRIMARY_CORE);
103 state = State::PRESTOP;
104 }
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();
110 });
111 }
112 std::string_view to_string() const {
113 switch (state) {
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 "???";
122 }
123 }
124 };
125
126 inline std::ostream&
127 operator<<(std::ostream& os, const OSDState& s) {
128 return os << s.to_string();
129 }
130 }