4
4
// Copyright (c) 2018 InnoGames GmbH
5
5
//
6
6
7
+ #define FMT_HEADER_ONLY
8
+
7
9
#include < fmt/format.h>
8
10
#include < fmt/printf.h>
9
11
#include < iostream>
15
17
#include " config.h"
16
18
#include " healthcheck.h"
17
19
#include " healthcheck_dns.h"
20
+ #include " healthcheck_dummy.h"
18
21
#include " healthcheck_http.h"
19
22
#include " healthcheck_ping.h"
20
23
#include " healthcheck_postgres.h"
@@ -91,15 +94,15 @@ Healthcheck::Healthcheck(const nlohmann::json &config,
91
94
this ->ran = false ;
92
95
93
96
// Initialize healthchecks state basing on state of parent node.
94
- // Proper initial state for the healthcheck quarantees no
97
+ // Proper initial state for the healthcheck guarantees no
95
98
// unnecessary messages.
96
- if (parent_lbnode->get_state () == LbNode::STATE_UP ) {
97
- hard_state = STATE_UP;
98
- last_state = STATE_UP;
99
+ if (parent_lbnode->is_up () ) {
100
+ hard_state = HealthcheckState:: STATE_UP;
101
+ last_state = HealthcheckState:: STATE_UP;
99
102
failure_counter = 0 ;
100
103
} else {
101
- hard_state = STATE_DOWN;
102
- last_state = STATE_DOWN;
104
+ hard_state = HealthcheckState:: STATE_DOWN;
105
+ last_state = HealthcheckState:: STATE_DOWN;
103
106
failure_counter = max_failed_checks;
104
107
}
105
108
}
@@ -130,10 +133,12 @@ Healthcheck *Healthcheck::healthcheck_factory(const nlohmann::json &config,
130
133
new Healthcheck_postgres (config, _parent_lbnode, ip_address);
131
134
else if (type == " dns" )
132
135
new_healthcheck = new Healthcheck_dns (config, _parent_lbnode, ip_address);
136
+ else if (type == " dummy" )
137
+ new_healthcheck = new Healthcheck_dummy (config, _parent_lbnode, ip_address);
133
138
else
134
139
return NULL ;
135
140
136
- log (MSG_INFO, new_healthcheck, " state: created" );
141
+ log (MessageType:: MSG_INFO, new_healthcheck, " state: created" );
137
142
138
143
return new_healthcheck;
139
144
}
@@ -158,7 +163,7 @@ int Healthcheck::schedule_healthcheck(struct timespec *now) {
158
163
is_running = true ;
159
164
160
165
if (verbose > 1 )
161
- log (MSG_INFO, this , " scheduling" );
166
+ log (MessageType:: MSG_INFO, this , " scheduling" );
162
167
163
168
return true ;
164
169
}
@@ -176,26 +181,33 @@ void Healthcheck::finalize() {
176
181
// the health check. If it wouldn't be called, the process is not
177
182
// going to continue.
178
183
void Healthcheck::end_check (HealthcheckResult result, string message) {
179
- msgType log_type;
184
+ MessageType log_type;
180
185
string statemsg;
181
186
182
187
switch (result) {
183
- case HC_PASS:
184
- log_type = MSG_STATE_UP;
185
- this ->last_state = STATE_UP;
188
+ case HealthcheckResult:: HC_PASS:
189
+ log_type = MessageType:: MSG_STATE_UP;
190
+ this ->last_state = HealthcheckState:: STATE_UP;
186
191
statemsg = fmt::sprintf (" state: up message: %s" , message);
187
192
this ->handle_result (statemsg);
188
193
break ;
189
194
190
- case HC_FAIL:
191
- log_type = MSG_STATE_DOWN;
192
- this ->last_state = STATE_DOWN;
195
+ case HealthcheckResult:: HC_FAIL:
196
+ log_type = MessageType:: MSG_STATE_DOWN;
197
+ this ->last_state = HealthcheckState:: STATE_DOWN;
193
198
statemsg = fmt::sprintf (" state: down message: %s" , message);
194
199
this ->handle_result (statemsg);
195
200
break ;
196
201
197
- case HC_PANIC:
198
- log_type = MSG_CRIT;
202
+ case HealthcheckResult::HC_DRAIN:
203
+ log_type = MessageType::MSG_STATE_DOWN;
204
+ this ->last_state = HealthcheckState::STATE_DRAIN;
205
+ statemsg = fmt::sprintf (" state: down with draining, message: %s" , message);
206
+ this ->handle_result (statemsg);
207
+ break ;
208
+
209
+ case HealthcheckResult::HC_PANIC:
210
+ log_type = MessageType::MSG_CRIT;
199
211
statemsg = fmt::sprintf (" state: failure message: %s" , message);
200
212
log (log_type, this , statemsg);
201
213
exit (2 );
@@ -212,33 +224,44 @@ void Healthcheck::end_check(HealthcheckResult result, string message) {
212
224
void Healthcheck::handle_result (string message) {
213
225
string fail_message;
214
226
bool changed = false ;
215
- int log_level = MSG_INFO;
227
+ MessageType log_level = MessageType:: MSG_INFO;
216
228
217
229
// If a healtcheck has passed, zero the failure counter.
218
- if (last_state == STATE_UP)
230
+ if (last_state == HealthcheckState:: STATE_UP)
219
231
failure_counter = 0 ;
220
232
221
- // Change from DOWN to UP. The healthcheck has passed again.
222
- if (hard_state == STATE_DOWN && last_state == STATE_UP) {
223
- hard_state = STATE_UP;
233
+ if (hard_state == HealthcheckState::STATE_UP) {
234
+ switch (last_state) {
235
+ case HealthcheckState::STATE_UP:
236
+ // No change, make compiler happy.
237
+ break ;
238
+ case HealthcheckState::STATE_DOWN:
239
+ // Change from UP to DOWN. The healthcheck has failed.
240
+ changed = true ;
241
+ log_level = MessageType::MSG_STATE_DOWN;
242
+ failure_counter++;
243
+ fail_message =
244
+ fmt::sprintf (" failure: %d of %d" , failure_counter, max_failed_checks);
245
+ // Mark the hard DOWN state only after the number of failed checks is
246
+ // reached.
247
+ if (failure_counter >= max_failed_checks) {
248
+ hard_state = HealthcheckState::STATE_DOWN;
249
+ }
250
+ break ;
251
+ case HealthcheckState::STATE_DRAIN:
252
+ // Change from UP to DRAIN. The healthcheck has failed with draining.
253
+ changed = true ;
254
+ log_level = MessageType::MSG_STATE_DOWN;
255
+ // Make it fail immediately not waiting for max_failed.
256
+ hard_state = HealthcheckState::STATE_DRAIN;
257
+ break ;
258
+ }
259
+ } else if (last_state == HealthcheckState::STATE_UP) {
260
+ // Change from any state to UP. The healthcheck has passed again.
261
+ hard_state = HealthcheckState::STATE_UP;
224
262
failure_counter = 0 ;
225
263
changed = true ;
226
- log_level = MSG_STATE_UP;
227
- }
228
- // Change from UP to DOWN. The healthcheck has failed.
229
- else if (hard_state == STATE_UP && last_state == STATE_DOWN) {
230
- changed = true ;
231
- log_level = MSG_STATE_DOWN;
232
-
233
- failure_counter++;
234
- fail_message =
235
- fmt::sprintf (" failure: %d of %d" , failure_counter, max_failed_checks);
236
-
237
- // Mark the hard DOWN state only after the number of failed checks is
238
- // reached.
239
- if (failure_counter >= max_failed_checks) {
240
- hard_state = STATE_DOWN;
241
- }
264
+ log_level = MessageType::MSG_STATE_UP;
242
265
}
243
266
244
267
if (changed || verbose) {
0 commit comments