forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathendpoint_state.hh
171 lines (141 loc) · 4.58 KB
/
endpoint_state.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
*
* Modified by ScyllaDB
* Copyright (C) 2015-present ScyllaDB
*/
/*
* SPDX-License-Identifier: (AGPL-3.0-or-later and Apache-2.0)
*/
#pragma once
#include "utils/serialization.hh"
#include "gms/heart_beat_state.hh"
#include "gms/application_state.hh"
#include "gms/versioned_value.hh"
#include <optional>
#include <chrono>
namespace gms {
/**
* This abstraction represents both the HeartBeatState and the ApplicationState in an EndpointState
* instance. Any state for a given endpoint can be retrieved from this instance.
*/
class endpoint_state {
public:
using clk = seastar::lowres_system_clock;
private:
heart_beat_state _heart_beat_state;
std::map<application_state, versioned_value> _application_state;
/* fields below do not get serialized */
clk::time_point _update_timestamp;
bool _is_alive;
bool _is_normal = false;
public:
bool operator==(const endpoint_state& other) const {
return _heart_beat_state == other._heart_beat_state &&
_application_state == other._application_state &&
_update_timestamp == other._update_timestamp &&
_is_alive == other._is_alive;
}
endpoint_state() noexcept
: _heart_beat_state(0)
, _update_timestamp(clk::now())
, _is_alive(true) {
update_is_normal();
}
endpoint_state(heart_beat_state initial_hb_state) noexcept
: _heart_beat_state(initial_hb_state)
, _update_timestamp(clk::now())
, _is_alive(true) {
update_is_normal();
}
endpoint_state(heart_beat_state&& initial_hb_state,
const std::map<application_state, versioned_value>& application_state)
: _heart_beat_state(std::move(initial_hb_state))
, _application_state(application_state)
, _update_timestamp(clk::now())
, _is_alive(true) {
update_is_normal();
}
// Valid only on shard 0
heart_beat_state& get_heart_beat_state() noexcept {
return _heart_beat_state;
}
// Valid only on shard 0
const heart_beat_state& get_heart_beat_state() const noexcept {
return _heart_beat_state;
}
void set_heart_beat_state_and_update_timestamp(heart_beat_state hbs) noexcept {
update_timestamp();
_heart_beat_state = hbs;
}
const versioned_value* get_application_state_ptr(application_state key) const noexcept;
/**
* TODO replace this with operations that don't expose private state
*/
// @Deprecated
std::map<application_state, versioned_value>& get_application_state_map() noexcept {
return _application_state;
}
const std::map<application_state, versioned_value>& get_application_state_map() const noexcept {
return _application_state;
}
void add_application_state(application_state key, versioned_value value) {
_application_state[key] = std::move(value);
update_is_normal();
}
void add_application_state(const endpoint_state& es) {
_application_state = es._application_state;
update_is_normal();
}
/* getters and setters */
/**
* @return System.nanoTime() when state was updated last time.
*
* Valid only on shard 0.
*/
clk::time_point get_update_timestamp() const noexcept {
return _update_timestamp;
}
void update_timestamp() noexcept {
_update_timestamp = clk::now();
}
bool is_alive() const noexcept {
return _is_alive;
}
void set_alive(bool alive) noexcept {
_is_alive = alive;
}
void mark_alive() noexcept {
set_alive(true);
}
void mark_dead() noexcept {
set_alive(false);
}
std::string_view get_status() const noexcept {
constexpr std::string_view empty = "";
auto* app_state = get_application_state_ptr(application_state::STATUS);
if (!app_state) {
return empty;
}
const auto& value = app_state->value;
if (value.empty()) {
return empty;
}
auto pos = value.find(',');
if (pos == sstring::npos) {
return std::string_view(value);
}
return std::string_view(value.c_str(), pos);
}
bool is_shutdown() const noexcept {
return get_status() == versioned_value::SHUTDOWN;
}
bool is_normal() const noexcept {
return _is_normal;
}
void update_is_normal() noexcept {
_is_normal = get_status() == versioned_value::STATUS_NORMAL;
}
bool is_cql_ready() const noexcept;
friend std::ostream& operator<<(std::ostream& os, const endpoint_state& x);
};
} // gms