21 #include <sys/resource.h> 31 #define STORM_INTERVAL 2 32 #define STORM_MULTIPLIER 5 47 crm_info(
"Election %s complete", e->name);
57 static gboolean election_timer_cb(gpointer user_data)
61 crm_info(
"Election %s %p timed out", e->name, e);
78 static guint count = 0;
89 e->uname = strdup(uname);
99 if(e && uname && e->voted) {
100 g_hash_table_remove(e->voted, uname);
107 crm_trace(
"Resetting election %s", e->name);
112 crm_trace(
"Destroying voted cache with %d members", g_hash_table_size(e->voted));
113 g_hash_table_destroy(e->voted);
153 crm_err(
"No election defined");
158 crm_uptime(
struct timeval *output)
160 static time_t expires = 0;
161 static struct rusage info;
163 time_t tm_now = time(NULL);
165 if (expires < tm_now) {
168 info.ru_utime.tv_sec = 0;
169 info.ru_utime.tv_usec = 0;
170 rc = getrusage(RUSAGE_SELF, &info);
176 crm_perror(LOG_ERR,
"Could not calculate the current uptime");
181 crm_debug(
"Current CPU usage is: %lds, %ldus", (
long)info.ru_utime.tv_sec,
182 (
long)info.ru_utime.tv_usec);
186 output->tv_sec = info.ru_utime.tv_sec;
187 output->tv_usec = info.ru_utime.tv_usec;
193 crm_compare_age(
struct timeval your_age)
195 struct timeval our_age;
197 crm_uptime(&our_age);
199 if (our_age.tv_sec > your_age.tv_sec) {
200 crm_debug(
"Win: %ld vs %ld (seconds)", (
long)our_age.tv_sec, (
long)your_age.tv_sec);
202 }
else if (our_age.tv_sec < your_age.tv_sec) {
203 crm_debug(
"Loose: %ld vs %ld (seconds)", (
long)our_age.tv_sec, (
long)your_age.tv_sec);
205 }
else if (our_age.tv_usec > your_age.tv_usec) {
206 crm_debug(
"Win: %ld.%ld vs %ld.%ld (usec)",
207 (
long)our_age.tv_sec, (
long)our_age.tv_usec, (
long)your_age.tv_sec, (
long)your_age.tv_usec);
209 }
else if (our_age.tv_usec < your_age.tv_usec) {
210 crm_debug(
"Loose: %ld.%ld vs %ld.%ld (usec)",
211 (
long)our_age.tv_sec, (
long)our_age.tv_usec, (
long)your_age.tv_sec, (
long)your_age.tv_usec);
222 xmlNode *vote = NULL;
226 crm_trace(
"Not voting in election: not initialized");
232 crm_trace(
"Cannot vote yet: %p", our_node);
250 crm_debug(
"Started election %d", e->count);
252 g_hash_table_destroy(e->voted);
256 election_timeout_start(e);
272 voted_size = g_hash_table_size(e->voted);
278 if (voted_size >= num_members) {
281 if (voted_size > num_members) {
287 while (g_hash_table_iter_next(&gIter, NULL, (gpointer *) & node)) {
293 g_hash_table_iter_init(&gIter, e->voted);
294 while (g_hash_table_iter_next(&gIter, (gpointer *) & key, NULL)) {
300 election_complete(e);
304 crm_debug(
"Still waiting on %d non-votes (%d total)",
305 num_members - voted_size, num_members);
311 #define loss_dampen 2 318 int election_id = -1;
319 int log_level = LOG_INFO;
320 gboolean use_born_on = FALSE;
321 gboolean done = FALSE;
322 gboolean we_loose = FALSE;
323 const char *op = NULL;
324 const char *from = NULL;
325 const char *reason =
"unknown";
326 const char *election_owner = NULL;
327 crm_node_t *our_node = NULL, *your_node = NULL;
329 static int election_wins = 0;
331 xmlNode *novote = NULL;
332 time_t tm_now = time(NULL);
333 static time_t expires = 0;
334 static time_t last_election_loss = 0;
343 crm_info(
"Not voting in election: not initialized");
347 crm_info(
"Not voting in election: no peer cache");
359 if (e->voted == NULL) {
361 e->voted = g_hash_table_new_full(
crm_str_hash, g_str_equal,
371 if(can_win == FALSE) {
372 reason =
"Not eligible";
376 reason =
"We are not part of the cluster";
380 }
else if (election_id != e->count &&
crm_str_eq(our_node->
uuid, election_owner, TRUE)) {
382 reason =
"Superseded";
387 reason =
"Peer is not part of our cluster";
388 log_level = LOG_WARNING;
392 char *op_copy = strdup(op);
393 char *uname_copy = strdup(from);
398 g_hash_table_replace(e->voted, uname_copy, op_copy);
403 struct timeval your_age;
411 your_age.tv_sec = tv_sec;
412 your_age.tv_usec = tv_usec;
414 age = crm_compare_age(your_age);
416 char *op_copy = strdup(op);
417 char *uname_copy = strdup(from);
422 g_hash_table_replace(e->voted, uname_copy, op_copy);
433 }
else if (age < 0) {
437 }
else if (age > 0) {
441 }
else if (use_born_on && your_node->born < our_node->
born) {
445 }
else if (use_born_on && your_node->born > our_node->
born) {
448 }
else if (e->uname == NULL) {
449 reason =
"Unknown host name";
452 }
else if (strcasecmp(e->uname, from) > 0) {
453 reason =
"Host name";
457 reason =
"Host name";
466 if (expires < tm_now) {
470 }
else if (done == FALSE && we_loose == FALSE) {
477 if (election_wins > (peers * peers)) {
478 crm_warn(
"Election storm detected: %d elections in %d seconds", election_wins,
487 do_crm_log(log_level + 1,
"Election %d (current: %d, owner: %s): Processed %s from %s (%s)",
488 election_id, e->count, election_owner, op, from, reason);
491 }
else if(we_loose == FALSE) {
492 do_crm_log(log_level,
"Election %d (owner: %s) pass: %s from %s (%s)",
493 election_id, election_owner, op, from, reason);
495 if (last_election_loss == 0
496 || tm_now - last_election_loss > (time_t)
loss_dampen) {
498 last_election_loss = 0;
506 crm_info(
"Election %d ignore: We already lost an election less than %ds ago (%s)",
507 election_id, loss_dampen, ctime(&last_election_loss));
513 do_crm_log(log_level,
"Election %d (owner: %s) lost: %s from %s (%s)",
514 election_id, election_owner, op, from, reason);
524 last_election_loss = tm_now;
#define CRM_CHECK(expr, failure_action)
void crm_write_blackbox(int nsig, struct qb_log_callsite *callsite)
void mainloop_timer_start(mainloop_timer_t *t)
guint mainloop_timer_set_period(mainloop_timer_t *t, guint period_ms)
void mainloop_timer_del(mainloop_timer_t *t)
gboolean is_heartbeat_cluster(void)
gboolean crm_is_peer_active(const crm_node_t *node)
struct mainloop_timer_s mainloop_timer_t
crm_node_t * crm_get_peer(unsigned int id, const char *uname)
void g_hash_destroy_str(gpointer data)
void election_timeout_stop(election_t *e)
guint crm_active_peers(void)
void mainloop_timer_stop(mainloop_timer_t *t)
#define F_CRM_ELECTION_AGE_S
Wrappers for and extensions to glib mainloop.
struct election_s election_t
enum election_result election_count_vote(election_t *e, xmlNode *vote, bool can_win)
#define crm_warn(fmt, args...)
#define crm_debug(fmt, args...)
void election_vote(election_t *e)
#define crm_trace(fmt, args...)
#define do_crm_log(level, fmt, args...)
Log a message.
Wrappers for and extensions to libxml2.
int crm_element_value_int(xmlNode *data, const char *name, int *dest)
const char * crm_element_value(xmlNode *data, const char *name)
void free_xml(xmlNode *child)
gboolean crm_str_eq(const char *a, const char *b, gboolean use_case)
void election_timeout_set_period(election_t *e, guint period)
election_t * election_init(const char *name, const char *uname, guint period_ms, GSourceFunc cb)
void election_fini(election_t *e)
const char * crm_xml_add(xmlNode *node, const char *name, const char *value)
const char * crm_xml_add_int(xmlNode *node, const char *name, int value)
#define F_CRM_ELECTION_AGE_US
#define crm_perror(level, fmt, args...)
Log a system error message.
void election_reset(election_t *e)
#define crm_err(fmt, args...)
int compare_version(const char *version1, const char *version2)
mainloop_timer_t * mainloop_timer_add(const char *name, guint period_ms, bool repeat, GSourceFunc cb, void *userdata)
bool election_check(election_t *e)
#define F_CRM_ELECTION_ID
char * crm_strdup_printf(char const *format,...) __attribute__((__format__(__printf__
gboolean send_cluster_message(crm_node_t *node, enum crm_ais_msg_types service, xmlNode *data, gboolean ordered)
#define create_request(task, xml_data, host_to, sys_to, sys_from, uuid_from)
GHashTable * crm_peer_cache
#define crm_info(fmt, args...)
void election_remove(election_t *e, const char *uname)
enum election_result election_state(election_t *e)
gboolean is_classic_ais_cluster(void)
Functions for conducting elections.
#define F_CRM_ELECTION_OWNER