| 1 |
|
| 2 |
/* |
| 3 |
* This program is free software; you can redistribute it and/or modify |
| 4 |
* it under the terms of the GNU General Public License as published by |
| 5 |
* the Free Software Foundation; either version 2 of the License, or |
| 6 |
* (at your option) any later version. |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
* GNU General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU General Public License |
| 14 |
* along with this program; if not, write to the Free Software |
| 15 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 16 |
*/ |
| 17 |
|
| 18 |
|
| 19 |
#include <lha_internal.h> |
| 20 |
|
| 21 |
#include "ccm.h" |
| 22 |
#include "ccmmsg.h" |
| 23 |
#include "ccmmisc.h" |
| 24 |
#include <clplumbing/cl_plugin.h> |
| 25 |
#include <clplumbing/cl_quorum.h> |
| 26 |
#include <clplumbing/cl_tiebreaker.h> |
| 27 |
#include <clplumbing/cl_misc.h> |
| 28 |
|
| 29 |
|
| 30 |
extern state_msg_handler_t state_msg_handler[]; |
| 31 |
|
| 32 |
struct ha_msg * ccm_readmsg(ccm_info_t *info, ll_cluster_t *hb); |
| 33 |
static GList* quorum_list = NULL; |
| 34 |
|
| 35 |
static struct ha_msg* |
| 36 |
ccm_handle_hbapiclstat(ccm_info_t *info, |
| 37 |
const char *orig, |
| 38 |
const char *status) |
| 39 |
{ |
| 40 |
int index; |
| 41 |
enum ccm_state state = info->state; |
| 42 |
|
| 43 |
if(state == CCM_STATE_NONE || |
| 44 |
state == CCM_STATE_VERSION_REQUEST) { |
| 45 |
return NULL; |
| 46 |
} |
| 47 |
|
| 48 |
assert(status); |
| 49 |
if(strncmp(status, JOINSTATUS, 5) == 0) { |
| 50 |
ccm_debug2(LOG_DEBUG, |
| 51 |
"ccm on %s started", orig); |
| 52 |
return NULL; |
| 53 |
} |
| 54 |
|
| 55 |
if(!orig){ |
| 56 |
return NULL; |
| 57 |
} |
| 58 |
|
| 59 |
index = llm_get_index(&info->llm, orig); |
| 60 |
if(index == -1) { |
| 61 |
return NULL; |
| 62 |
} |
| 63 |
|
| 64 |
return(ccm_create_leave_msg(info, index)); |
| 65 |
} |
| 66 |
|
| 67 |
/* |
| 68 |
* The callback function which is called when the status |
| 69 |
* of a node changes. |
| 70 |
*/ |
| 71 |
|
| 72 |
static int |
| 73 |
nodelist_update(ll_cluster_t* hb, ccm_info_t* info, |
| 74 |
const char *id, const char *status) |
| 75 |
{ |
| 76 |
llm_info_t *llm; |
| 77 |
char oldstatus[STATUSSIZE]; |
| 78 |
/* update the low level membership of the node |
| 79 |
* if the status moves from active to dead and if the member |
| 80 |
* is already part of the ccm, then we have to mimic a |
| 81 |
* leave message for us |
| 82 |
*/ |
| 83 |
ccm_debug2(LOG_DEBUG, |
| 84 |
"nodelist update: Node %s now has status %s", |
| 85 |
id, status); |
| 86 |
llm = &info->llm; |
| 87 |
|
| 88 |
if (llm_status_update(llm, id, status,oldstatus) != HA_OK) { |
| 89 |
ccm_log(LOG_ERR, "%s: updating status for node %s failed", |
| 90 |
__FUNCTION__, id); |
| 91 |
return HA_FAIL; |
| 92 |
} |
| 93 |
|
| 94 |
if (STRNCMP_CONST(status, DEADSTATUS) == 0){ |
| 95 |
|
| 96 |
if(node_is_member(info, id)){ |
| 97 |
leave_cache(llm_get_index(llm, id)); |
| 98 |
} |
| 99 |
|
| 100 |
} |
| 101 |
|
| 102 |
if ( strncmp(llm_get_mynodename(llm), id,NODEIDSIZE ) == 0){ |
| 103 |
return HA_OK; |
| 104 |
} |
| 105 |
|
| 106 |
ccm_debug(LOG_DEBUG, "status of node %s: %s -> %s" |
| 107 |
, id, oldstatus, status); |
| 108 |
|
| 109 |
if ( part_of_cluster(info->state) |
| 110 |
&& ( STRNCMP_CONST(oldstatus, DEADSTATUS) == 0 |
| 111 |
&& STRNCMP_CONST(status, DEADSTATUS) != 0)){ |
| 112 |
ccm_send_state_info(hb, info, id); |
| 113 |
} |
| 114 |
|
| 115 |
return HA_OK; |
| 116 |
} |
| 117 |
|
| 118 |
int |
| 119 |
ccm_control_process(ccm_info_t *info, ll_cluster_t * hb) |
| 120 |
{ |
| 121 |
struct ha_msg* msg; |
| 122 |
struct ha_msg* newmsg; |
| 123 |
const char* type; |
| 124 |
int ccm_msg_type; |
| 125 |
const char* orig=NULL; |
| 126 |
const char* status=NULL; |
| 127 |
llm_info_t* llm= &info->llm; |
| 128 |
const char* mynode = llm_get_mynodename(llm); |
| 129 |
const char* numnodes; |
| 130 |
int numnodes_val; |
| 131 |
|
| 132 |
repeat: |
| 133 |
/* read the next available message */ |
| 134 |
msg = ccm_readmsg(info, hb); /* this is non-blocking */ |
| 135 |
|
| 136 |
if (msg) { |
| 137 |
type = ha_msg_value(msg, F_TYPE); |
| 138 |
orig = ha_msg_value(msg, F_ORIG); |
| 139 |
status = ha_msg_value(msg, F_STATUS); |
| 140 |
|
| 141 |
ccm_debug(LOG_DEBUG, "recv msg %s from %s, status:%s" |
| 142 |
, type, orig, (status ? status : "[null ptr]")); |
| 143 |
ccm_message_debug2(LOG_DEBUG, msg); |
| 144 |
|
| 145 |
if(strcmp(type, T_APICLISTAT) == 0){ |
| 146 |
/* handle ccm status of on other nodes of the cluster */ |
| 147 |
if((newmsg = ccm_handle_hbapiclstat(info, orig, |
| 148 |
status)) == NULL) { |
| 149 |
ha_msg_del(msg); |
| 150 |
return TRUE; |
| 151 |
} |
| 152 |
ha_msg_del(msg); |
| 153 |
msg = newmsg; |
| 154 |
} else if(strcasecmp(type, T_STATUS) == 0){ |
| 155 |
const char* nodetype; |
| 156 |
const char* site; |
| 157 |
int weight; |
| 158 |
if (llm_is_valid_node(&info->llm, orig)){ |
| 159 |
|
| 160 |
if (nodelist_update(hb, info,orig, status) != HA_OK){ |
| 161 |
ccm_log(LOG_ERR, "%s: updating node status for " |
| 162 |
"nodelist failed(%s-%s)", |
| 163 |
__FUNCTION__, orig, status); |
| 164 |
return FALSE; |
| 165 |
} |
| 166 |
|
| 167 |
ha_msg_del(msg); |
| 168 |
return TRUE; |
| 169 |
} |
| 170 |
|
| 171 |
nodetype = hb->llc_ops->node_type(hb, orig); |
| 172 |
if (nodetype == NULL){ |
| 173 |
ccm_log(LOG_ERR, "%s: get node %s's type failed", |
| 174 |
__FUNCTION__, orig); |
| 175 |
return TRUE; |
| 176 |
} |
| 177 |
|
| 178 |
if (STRNCMP_CONST(nodetype, NORMALNODE) !=0 ){ |
| 179 |
return TRUE; |
| 180 |
} |
| 181 |
site = hb->llc_ops->node_site(hb, orig); |
| 182 |
weight = hb->llc_ops->node_weight(hb, orig); |
| 183 |
if (llm_add(llm, orig, status, mynode, site, weight) != HA_OK){ |
| 184 |
ccm_log(LOG_ERR, "%s: adding node(%s) to llm failed", |
| 185 |
__FUNCTION__,orig); |
| 186 |
return FALSE; |
| 187 |
} |
| 188 |
|
| 189 |
ccm_debug2(LOG_INFO, "after adding node %s", orig); |
| 190 |
llm_display(llm); |
| 191 |
|
| 192 |
jump_to_joining_state(hb, info, msg); |
| 193 |
|
| 194 |
} else if (strcasecmp(type, T_DELNODE) ==0){ |
| 195 |
const char* node = ha_msg_value(msg, F_NODE); |
| 196 |
if (node == NULL){ |
| 197 |
ccm_log(LOG_ERR, "%s: field node not found", |
| 198 |
__FUNCTION__); |
| 199 |
return FALSE; |
| 200 |
} |
| 201 |
|
| 202 |
if (llm_del(llm, node) != HA_OK){ |
| 203 |
ccm_log(LOG_ERR, "%s: deleting node %s failed", |
| 204 |
__FUNCTION__, node); |
| 205 |
return FALSE; |
| 206 |
} |
| 207 |
|
| 208 |
jump_to_joining_state(hb, info, msg); |
| 209 |
|
| 210 |
} |
| 211 |
|
| 212 |
} else { |
| 213 |
msg = timeout_msg_mod(info); |
| 214 |
} |
| 215 |
|
| 216 |
type = ha_msg_value(msg, F_TYPE); |
| 217 |
ccm_msg_type = ccm_string2type(type); |
| 218 |
|
| 219 |
if (ccm_msg_type < 0){ |
| 220 |
goto out; |
| 221 |
} |
| 222 |
|
| 223 |
numnodes = ha_msg_value(msg, F_NUMNODES); |
| 224 |
if(numnodes != NULL){ |
| 225 |
numnodes_val = atoi(numnodes); |
| 226 |
if (numnodes_val != info->llm.nodecount){ |
| 227 |
ccm_log(LOG_ERR |
| 228 |
, "%s: Node count from node %s does not agree" |
| 229 |
": local count=%d, count in message=%d" |
| 230 |
, __FUNCTION__ |
| 231 |
, orig, info->llm.nodecount, numnodes_val); |
| 232 |
ccm_log(LOG_ERR, "Please make sure ha.cf files on all" |
| 233 |
" nodes have same nodes list or add \"autojoin any\" " |
| 234 |
"to ha.cf"); |
| 235 |
ccm_log(LOG_INFO, "%s", "If this problem persists" |
| 236 |
", check the heartbeat 'hostcache' files" |
| 237 |
" in the cluster to look for problems."); |
| 238 |
exit(1); |
| 239 |
} |
| 240 |
} |
| 241 |
state_msg_handler[info->state](ccm_msg_type, msg, hb, info); |
| 242 |
|
| 243 |
|
| 244 |
out: |
| 245 |
if(ccm_msg_type != CCM_TYPE_TIMEOUT) { |
| 246 |
ha_msg_del(msg); |
| 247 |
} |
| 248 |
|
| 249 |
/* If there is another message in the channel, |
| 250 |
* process it now. |
| 251 |
*/ |
| 252 |
if (hb->llc_ops->msgready(hb)) |
| 253 |
goto repeat; |
| 254 |
|
| 255 |
return TRUE; |
| 256 |
} |
| 257 |
#define QUORUM_S "HA_quorum" |
| 258 |
#define TIEBREAKER_S "HA_tiebreaker" |
| 259 |
gboolean |
| 260 |
ccm_calculate_quorum(ccm_info_t* info) |
| 261 |
{ |
| 262 |
struct hb_quorum_fns* funcs = NULL; |
| 263 |
const char* quorum_env = NULL; |
| 264 |
char* quorum_str = NULL; |
| 265 |
char* end = NULL; |
| 266 |
char* begin = NULL; |
| 267 |
GList* cur = NULL; |
| 268 |
int rc; |
| 269 |
|
| 270 |
|
| 271 |
if (quorum_list == NULL){ |
| 272 |
quorum_env = cl_get_env(QUORUM_S); |
| 273 |
if (quorum_env == NULL){ |
| 274 |
ccm_debug(LOG_DEBUG, "No quorum selected," |
| 275 |
"using default quorum plugin(majority:twonodes)"); |
| 276 |
quorum_str = strdup("majority:twonodes"); |
| 277 |
} |
| 278 |
else { |
| 279 |
quorum_str = strdup(quorum_env); |
| 280 |
} |
| 281 |
|
| 282 |
begin = quorum_str; |
| 283 |
while (begin != NULL) { |
| 284 |
end = strchr(begin, ':'); |
| 285 |
if (end != NULL) { |
| 286 |
*end = 0; |
| 287 |
} |
| 288 |
funcs = cl_load_plugin("quorum", begin); |
| 289 |
if (funcs == NULL){ |
| 290 |
ccm_log(LOG_ERR, "%s: loading plugin %s failed", |
| 291 |
__FUNCTION__, begin); |
| 292 |
} |
| 293 |
else { |
| 294 |
funcs->init(ccm_on_quorum_changed |
| 295 |
, info->cluster, info->quorum_server); |
| 296 |
quorum_list = g_list_append(quorum_list, funcs); |
| 297 |
} |
| 298 |
begin = (end == NULL)? NULL:end+1; |
| 299 |
} |
| 300 |
free(quorum_str); |
| 301 |
} |
| 302 |
|
| 303 |
cur = g_list_first(quorum_list); |
| 304 |
while (cur != NULL) { |
| 305 |
int mem_weight = 0; |
| 306 |
int total_weight = 0; |
| 307 |
int i, node; |
| 308 |
|
| 309 |
for (i=0; i<info->memcount; i++) { |
| 310 |
node = info->ccm_member[i]; |
| 311 |
mem_weight+=info->llm.nodes[node].weight; |
| 312 |
} |
| 313 |
for (i=0; i<info->llm.nodecount; i++) { |
| 314 |
total_weight+=info->llm.nodes[i].weight; |
| 315 |
} |
| 316 |
funcs = (struct hb_quorum_fns*)cur->data; |
| 317 |
rc = funcs->getquorum(info->cluster, info->memcount, mem_weight |
| 318 |
, info->llm.nodecount, total_weight); |
| 319 |
|
| 320 |
if (rc == QUORUM_YES){ |
| 321 |
return TRUE; |
| 322 |
} |
| 323 |
else if (rc == QUORUM_NO){ |
| 324 |
return FALSE; |
| 325 |
} |
| 326 |
cur = g_list_next(cur); |
| 327 |
} |
| 328 |
ccm_debug(LOG_ERR, "all quorum plugins can't make a decision! " |
| 329 |
"assume lost quorum"); |
| 330 |
|
| 331 |
return FALSE; |
| 332 |
|
| 333 |
} |
| 334 |
gboolean |
| 335 |
ccm_stop_query_quorum(void) |
| 336 |
{ |
| 337 |
struct hb_quorum_fns* funcs = NULL; |
| 338 |
GList* cur = NULL; |
| 339 |
if (quorum_list == NULL){ |
| 340 |
return TRUE; |
| 341 |
} |
| 342 |
|
| 343 |
cur = g_list_first(quorum_list); |
| 344 |
while (cur != NULL) { |
| 345 |
funcs = (struct hb_quorum_fns*)cur->data; |
| 346 |
funcs->stop(); |
| 347 |
cur = g_list_next(cur); |
| 348 |
} |
| 349 |
return FALSE; |
| 350 |
} |