]> git.proxmox.com Git - ceph.git/blame - ceph/src/crimson/osd/state.h
update ceph source to reef 18.2.1
[ceph.git] / ceph / src / crimson / osd / state.h
CommitLineData
11fdf7f2
TL
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
20effc67
TL
9#include <seastar/core/shared_future.hh>
10
11fdf7f2
TL
11class OSDMap;
12
aee94f69
TL
13namespace crimson::osd {
14
15// seastar::sharded puts start_single on core 0
16constexpr 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 */
28class OSDState : public seastar::peering_sharded_service<OSDState> {
11fdf7f2
TL
29
30 enum class State {
31 INITIALIZING,
32 PREBOOT,
33 BOOTING,
34 ACTIVE,
f67539c2 35 PRESTOP,
11fdf7f2
TL
36 STOPPING,
37 WAITING_FOR_HEALTHY,
38 };
39
40 State state = State::INITIALIZING;
20effc67 41 mutable seastar::shared_promise<> wait_for_active;
11fdf7f2 42
aee94f69
TL
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 }
11fdf7f2
TL
55public:
56 bool is_initializing() const {
aee94f69 57 ceph_assert(seastar::this_shard_id() == PRIMARY_CORE);
11fdf7f2
TL
58 return state == State::INITIALIZING;
59 }
60 bool is_preboot() const {
aee94f69 61 ceph_assert(seastar::this_shard_id() == PRIMARY_CORE);
11fdf7f2
TL
62 return state == State::PREBOOT;
63 }
64 bool is_booting() const {
aee94f69 65 ceph_assert(seastar::this_shard_id() == PRIMARY_CORE);
11fdf7f2
TL
66 return state == State::BOOTING;
67 }
68 bool is_active() const {
69 return state == State::ACTIVE;
70 }
20effc67
TL
71 seastar::future<> when_active() const {
72 return is_active() ? seastar::now()
73 : wait_for_active.get_shared_future();
74 };
f67539c2 75 bool is_prestop() const {
aee94f69 76 ceph_assert(seastar::this_shard_id() == PRIMARY_CORE);
f67539c2
TL
77 return state == State::PRESTOP;
78 }
11fdf7f2
TL
79 bool is_stopping() const {
80 return state == State::STOPPING;
81 }
82 bool is_waiting_for_healthy() const {
aee94f69 83 ceph_assert(seastar::this_shard_id() == PRIMARY_CORE);
11fdf7f2
TL
84 return state == State::WAITING_FOR_HEALTHY;
85 }
86 void set_preboot() {
aee94f69 87 ceph_assert(seastar::this_shard_id() == PRIMARY_CORE);
11fdf7f2
TL
88 state = State::PREBOOT;
89 }
90 void set_booting() {
aee94f69 91 ceph_assert(seastar::this_shard_id() == PRIMARY_CORE);
11fdf7f2
TL
92 state = State::BOOTING;
93 }
aee94f69
TL
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 });
11fdf7f2 100 }
f67539c2 101 void set_prestop() {
aee94f69 102 ceph_assert(seastar::this_shard_id() == PRIMARY_CORE);
f67539c2
TL
103 state = State::PRESTOP;
104 }
aee94f69
TL
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 });
11fdf7f2
TL
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";
f67539c2 118 case State::PRESTOP: return "prestop";
11fdf7f2
TL
119 case State::STOPPING: return "stopping";
120 case State::WAITING_FOR_HEALTHY: return "waiting_for_healthy";
121 default: return "???";
122 }
123 }
124};
125
126inline std::ostream&
127operator<<(std::ostream& os, const OSDState& s) {
128 return os << s.to_string();
129}
aee94f69 130}