| 1 |
/* |
| 2 |
* client.c: Consensus Cluster Client tracker |
| 3 |
* |
| 4 |
* Copyright (c) International Business Machines Corp., 2002 |
| 5 |
* Author: Ram Pai ([email protected]) |
| 6 |
* |
| 7 |
* |
| 8 |
* This program is free software; you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU General Public License as published by |
| 10 |
* the Free Software Foundation; either version 2 of the License, or |
| 11 |
* (at your option) any later version. |
| 12 |
* |
| 13 |
* This program is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU General Public License |
| 19 |
* along with this program; if not, write to the Free Software |
| 20 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 |
* |
| 22 |
*/ |
| 23 |
#include <lha_internal.h> |
| 24 |
#include <ccm.h> |
| 25 |
#include <ccmlib.h> |
| 26 |
|
| 27 |
typedef struct ccm_client_s { |
| 28 |
int ccm_clid; |
| 29 |
int ccm_flags; |
| 30 |
struct IPC_CHANNEL *ccm_ipc_client; |
| 31 |
} ccm_client_t; |
| 32 |
|
| 33 |
#define CL_INIT 0x0 |
| 34 |
#define CL_LLM 0x1 |
| 35 |
#define CL_MEM 0x2 |
| 36 |
#define CL_ERROR 0x4 |
| 37 |
|
| 38 |
|
| 39 |
typedef struct ccm_ipc_s { |
| 40 |
int count; |
| 41 |
struct IPC_MESSAGE ipcmsg;/*this should be the last field*/ |
| 42 |
} ccm_ipc_t; |
| 43 |
|
| 44 |
static ccm_ipc_t *ipc_llm_message = NULL; /* active low level membership */ |
| 45 |
static ccm_ipc_t *ipc_mem_message = NULL; /* active membership */ |
| 46 |
static ccm_ipc_t *ipc_misc_message = NULL; /* active misc information */ |
| 47 |
|
| 48 |
#define MAXIPC 100 |
| 49 |
|
| 50 |
static gboolean membership_ready = FALSE; |
| 51 |
static void refresh_llm_msg(llm_info_t *llm); |
| 52 |
|
| 53 |
|
| 54 |
/* |
| 55 |
* the fully initialized clients. |
| 56 |
*/ |
| 57 |
static GHashTable *ccm_hashclient = NULL; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
static void |
| 62 |
send_message(ccm_client_t *ccm_client, ccm_ipc_t *msg) |
| 63 |
{ |
| 64 |
int send_rc; |
| 65 |
int do_warn = 0; |
| 66 |
struct IPC_CHANNEL *chan = ccm_client->ccm_ipc_client; |
| 67 |
|
| 68 |
++(msg->count); |
| 69 |
|
| 70 |
do { |
| 71 |
send_rc = chan->ops->send(chan, &(msg->ipcmsg)); |
| 72 |
if (send_rc == IPC_OK) |
| 73 |
break; |
| 74 |
|
| 75 |
if (chan->ops->get_chan_status(chan) != IPC_CONNECT) { |
| 76 |
ccm_debug(LOG_WARNING, |
| 77 |
"Channel is dead. Cannot send message." |
| 78 |
" farside_pid=%u", chan->farside_pid); |
| 79 |
break; |
| 80 |
} |
| 81 |
|
| 82 |
if (10 == ++do_warn) { |
| 83 |
cl_log(LOG_WARNING, |
| 84 |
"ipc channel blocked, farside_pid=%u, reason: %s", |
| 85 |
chan->farside_pid, chan->failreason); |
| 86 |
} |
| 87 |
/* FIXME this can livelock, if a ccm client does not consume |
| 88 |
* its messages! If we want to block, why not set the channel |
| 89 |
* to blocking mode in the first place? */ |
| 90 |
cl_shortsleep(); |
| 91 |
} while(send_rc == IPC_FAIL); |
| 92 |
|
| 93 |
return; |
| 94 |
} |
| 95 |
|
| 96 |
static void |
| 97 |
send_func(gpointer key, gpointer value, gpointer user_data) |
| 98 |
{ |
| 99 |
ccm_client_t *ccm_client = (ccm_client_t *)value; |
| 100 |
int msg_type = GPOINTER_TO_INT(user_data); |
| 101 |
switch (msg_type) { |
| 102 |
case CCM_EVICTED: |
| 103 |
if(ccm_client->ccm_flags == CL_MEM) { |
| 104 |
struct IPC_CHANNEL* chan = ccm_client->ccm_ipc_client; |
| 105 |
|
| 106 |
if (chan->ops->get_chan_status(chan) == IPC_CONNECT){ |
| 107 |
send_message(ccm_client, ipc_misc_message); |
| 108 |
}else { |
| 109 |
/* IPC is broken, the client is already gone |
| 110 |
* Do nothing |
| 111 |
*/ |
| 112 |
} |
| 113 |
ccm_client->ccm_flags = CL_INIT; |
| 114 |
} |
| 115 |
break; |
| 116 |
case CCM_INFLUX: |
| 117 |
send_message(ccm_client, ipc_misc_message); |
| 118 |
break; |
| 119 |
case CCM_NEW_MEMBERSHIP: |
| 120 |
if(membership_ready) { |
| 121 |
send_message(ccm_client, ipc_llm_message); |
| 122 |
send_message(ccm_client, ipc_mem_message); |
| 123 |
} |
| 124 |
break; |
| 125 |
default: |
| 126 |
ccm_log(LOG_ERR, "send_func:unknown message"); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
static void |
| 131 |
delete_message(ccm_ipc_t *ccmipc) |
| 132 |
{ |
| 133 |
g_free(ccmipc); |
| 134 |
} |
| 135 |
|
| 136 |
static |
| 137 |
void |
| 138 |
send_func_done(struct IPC_MESSAGE *ipcmsg) |
| 139 |
{ |
| 140 |
ccm_ipc_t *ccmipc = (ccm_ipc_t *)ipcmsg->msg_private; |
| 141 |
int count = --(ccmipc->count); |
| 142 |
|
| 143 |
if(count==0){ |
| 144 |
delete_message(ccmipc); |
| 145 |
} |
| 146 |
return; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
static ccm_ipc_t * |
| 151 |
create_message(void *data, int size) |
| 152 |
{ |
| 153 |
ccm_ipc_t *ipcmsg; |
| 154 |
|
| 155 |
ipcmsg = g_malloc(sizeof(ccm_ipc_t)+size); |
| 156 |
|
| 157 |
ipcmsg->count = 0; |
| 158 |
|
| 159 |
memset(&ipcmsg->ipcmsg, 0, sizeof(IPC_Message)); |
| 160 |
|
| 161 |
ipcmsg->ipcmsg.msg_body = ipcmsg+1; |
| 162 |
memcpy(ipcmsg->ipcmsg.msg_body, data, size); |
| 163 |
|
| 164 |
ipcmsg->ipcmsg.msg_len = size; |
| 165 |
ipcmsg->ipcmsg.msg_done = send_func_done; |
| 166 |
ipcmsg->ipcmsg.msg_private = ipcmsg; |
| 167 |
ipcmsg->ipcmsg.msg_buf = NULL; |
| 168 |
|
| 169 |
return ipcmsg; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
static void |
| 174 |
send_all(int msg_type) |
| 175 |
{ |
| 176 |
if(g_hash_table_size(ccm_hashclient)) { |
| 177 |
g_hash_table_foreach(ccm_hashclient, send_func, GINT_TO_POINTER(msg_type)); |
| 178 |
} |
| 179 |
return; |
| 180 |
} |
| 181 |
|
| 182 |
static void |
| 183 |
flush_func(gpointer key, gpointer value, gpointer user_data) |
| 184 |
{ |
| 185 |
struct IPC_CHANNEL *ipc_client = (struct IPC_CHANNEL *)key; |
| 186 |
int do_warn = 0; |
| 187 |
while(ipc_client->ops->is_sending_blocked(ipc_client)) { |
| 188 |
/* FIXME misbehaving client can live lock whole ccm layer! */ |
| 189 |
if (10 == ++do_warn) { |
| 190 |
cl_log(LOG_WARNING, |
| 191 |
"ipc channel blocked, farside_pid=%u", |
| 192 |
ipc_client->farside_pid); |
| 193 |
} |
| 194 |
cl_shortsleep(); |
| 195 |
if(ipc_client->ops->resume_io(ipc_client) == IPC_BROKEN) { |
| 196 |
break; |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
static void |
| 202 |
flush_all(void) |
| 203 |
{ |
| 204 |
if(g_hash_table_size(ccm_hashclient)) { |
| 205 |
g_hash_table_foreach(ccm_hashclient, flush_func, NULL); |
| 206 |
} |
| 207 |
return; |
| 208 |
} |
| 209 |
|
| 210 |
static void |
| 211 |
cleanup(void) |
| 212 |
{ |
| 213 |
membership_ready=FALSE; |
| 214 |
flush_all(); /* flush out all the messages to all the clients*/ |
| 215 |
if (ipc_mem_message) { |
| 216 |
delete_message(ipc_mem_message); |
| 217 |
} |
| 218 |
if (ipc_misc_message) { |
| 219 |
delete_message(ipc_misc_message); |
| 220 |
} |
| 221 |
ipc_mem_message = NULL; |
| 222 |
ipc_misc_message = NULL; |
| 223 |
|
| 224 |
/* NOTE: ipc_llm_message is never destroyed. */ |
| 225 |
/* Also, do not free the client structure. */ |
| 226 |
|
| 227 |
return; |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
void |
| 232 |
client_init(void) |
| 233 |
{ |
| 234 |
if(ccm_hashclient) { |
| 235 |
ccm_log(LOG_INFO, "client already initialized"); |
| 236 |
return; |
| 237 |
} |
| 238 |
ccm_hashclient = g_hash_table_new(g_direct_hash, |
| 239 |
g_direct_equal); |
| 240 |
|
| 241 |
return; |
| 242 |
} |
| 243 |
|
| 244 |
int |
| 245 |
client_add(struct IPC_CHANNEL *ipc_client) |
| 246 |
{ |
| 247 |
ccm_client_t *ccm_client; |
| 248 |
|
| 249 |
if(!ccm_hashclient) { |
| 250 |
ccm_log(LOG_ERR, "client subsystem not initialized"); |
| 251 |
return -1; |
| 252 |
} |
| 253 |
|
| 254 |
ccm_client = (ccm_client_t *) |
| 255 |
g_malloc(sizeof(ccm_client_t)); |
| 256 |
|
| 257 |
ccm_client->ccm_clid = 0; /* don't care, TOBEDONE */ |
| 258 |
ccm_client->ccm_ipc_client = ipc_client; |
| 259 |
ccm_client->ccm_flags = CL_INIT; |
| 260 |
|
| 261 |
send_func(ipc_client, ccm_client, (gpointer)CCM_NEW_MEMBERSHIP); |
| 262 |
|
| 263 |
g_hash_table_insert(ccm_hashclient, ipc_client, ccm_client); |
| 264 |
return 0; |
| 265 |
} |
| 266 |
|
| 267 |
static void |
| 268 |
client_destroy(struct IPC_CHANNEL *ipc_client) |
| 269 |
{ |
| 270 |
ccm_client_t *ccm_client; |
| 271 |
if((ccm_client = g_hash_table_lookup(ccm_hashclient, ipc_client)) |
| 272 |
!= NULL){ |
| 273 |
g_free(ccm_client); |
| 274 |
} |
| 275 |
/* IPC_Channel is automatically destroyed when channel is disconnected */ |
| 276 |
} |
| 277 |
|
| 278 |
void |
| 279 |
client_delete(struct IPC_CHANNEL *ipc_client) |
| 280 |
{ |
| 281 |
|
| 282 |
g_hash_table_remove(ccm_hashclient, ipc_client); |
| 283 |
client_destroy(ipc_client); |
| 284 |
return; |
| 285 |
} |
| 286 |
|
| 287 |
static gboolean |
| 288 |
destroy_func(gpointer key, gpointer value, gpointer user_data) |
| 289 |
{ |
| 290 |
struct IPC_CHANNEL *ipc_client = (struct IPC_CHANNEL *)key; |
| 291 |
|
| 292 |
client_destroy(ipc_client); |
| 293 |
return TRUE; |
| 294 |
} |
| 295 |
|
| 296 |
void |
| 297 |
client_delete_all(void) |
| 298 |
{ |
| 299 |
if(g_hash_table_size(ccm_hashclient)) { |
| 300 |
g_hash_table_foreach_remove(ccm_hashclient, destroy_func, NULL); |
| 301 |
} |
| 302 |
return; |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
static gboolean |
| 307 |
get_quorum(ccm_info_t* info) |
| 308 |
{ |
| 309 |
if (info->has_quorum != -1) { |
| 310 |
return info->has_quorum; |
| 311 |
} |
| 312 |
return ccm_calculate_quorum(info); |
| 313 |
} |
| 314 |
|
| 315 |
static void |
| 316 |
display_func(gpointer key, gpointer value, gpointer user_data) |
| 317 |
{ |
| 318 |
ccm_client_t * ccm_client = (ccm_client_t*) value; |
| 319 |
ccm_debug(LOG_DEBUG, "client: pid =%d", ccm_client->ccm_ipc_client->farside_pid); |
| 320 |
|
| 321 |
return; |
| 322 |
} |
| 323 |
|
| 324 |
void |
| 325 |
client_new_mbrship(ccm_info_t* info, void* borndata) |
| 326 |
{ |
| 327 |
/* creating enough heap memory in order to avoid allocation */ |
| 328 |
static struct born_s bornbuffer[MAXNODE+10]; |
| 329 |
ccm_meminfo_t *ccm=(ccm_meminfo_t *)bornbuffer; |
| 330 |
struct born_s *born_arry = (struct born_s *)borndata; |
| 331 |
int n = info->memcount; |
| 332 |
int trans = info->ccm_transition_major; |
| 333 |
int* member = info->ccm_member; |
| 334 |
int i, j; |
| 335 |
|
| 336 |
assert( n<= MAXNODE); |
| 337 |
|
| 338 |
membership_ready=TRUE; |
| 339 |
|
| 340 |
|
| 341 |
ccm->ev = CCM_NEW_MEMBERSHIP; |
| 342 |
ccm->n = n; |
| 343 |
ccm->trans = trans; |
| 344 |
ccm->quorum = get_quorum(info); |
| 345 |
(void)get_quorum; |
| 346 |
ccm_debug(LOG_DEBUG, "quorum is %d", ccm->quorum); |
| 347 |
|
| 348 |
|
| 349 |
for (i = 0; i < n; i++) { |
| 350 |
ccm->member[i].index = member[i]; |
| 351 |
ccm->member[i].bornon = -1; |
| 352 |
for (j = 0; j < n; j ++) { |
| 353 |
if (born_arry[j].index == ccm->member[i].index) { |
| 354 |
ccm->member[i].bornon = born_arry[j].bornon; |
| 355 |
} |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
if(ipc_mem_message && --(ipc_mem_message->count)==0){ |
| 360 |
delete_message(ipc_mem_message); |
| 361 |
} |
| 362 |
ipc_mem_message = create_message(ccm, |
| 363 |
(sizeof(ccm_meminfo_t) + n*sizeof(born_t))); |
| 364 |
ipc_mem_message->count++; |
| 365 |
refresh_llm_msg(&info->llm); |
| 366 |
#if 1 |
| 367 |
ccm_debug(LOG_DEBUG, "delivering new membership to %d clients: ", |
| 368 |
g_hash_table_size(ccm_hashclient)); |
| 369 |
if(g_hash_table_size(ccm_hashclient)){ |
| 370 |
g_hash_table_foreach(ccm_hashclient, display_func, NULL); |
| 371 |
} |
| 372 |
#else |
| 373 |
(void)display_func; |
| 374 |
#endif |
| 375 |
|
| 376 |
send_all(CCM_NEW_MEMBERSHIP); |
| 377 |
ccm_debug2(LOG_DEBUG, "membership state: new membership"); |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
void |
| 382 |
client_influx(void) |
| 383 |
{ |
| 384 |
int type = CCM_INFLUX; |
| 385 |
|
| 386 |
if(membership_ready){ |
| 387 |
membership_ready = FALSE; |
| 388 |
if(ipc_misc_message && --(ipc_misc_message->count)==0){ |
| 389 |
delete_message(ipc_misc_message); |
| 390 |
} |
| 391 |
ipc_misc_message = create_message(&type, sizeof(int)); |
| 392 |
ipc_misc_message->count++; |
| 393 |
send_all(CCM_INFLUX); |
| 394 |
} |
| 395 |
|
| 396 |
ccm_debug2(LOG_DEBUG, "membership state: not primary"); |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
void |
| 401 |
client_evicted(void) |
| 402 |
{ |
| 403 |
int type = CCM_EVICTED; |
| 404 |
if(ipc_misc_message && --(ipc_misc_message->count)==0){ |
| 405 |
delete_message(ipc_misc_message); |
| 406 |
} |
| 407 |
ipc_misc_message = create_message(&type, sizeof(int)); |
| 408 |
ipc_misc_message->count++; |
| 409 |
send_all(CCM_EVICTED); |
| 410 |
|
| 411 |
cleanup(); |
| 412 |
ccm_debug2(LOG_DEBUG, "membership state: evicted"); |
| 413 |
} |
| 414 |
|
| 415 |
void |
| 416 |
client_llm_init(llm_info_t *llm) |
| 417 |
{ |
| 418 |
refresh_llm_msg(llm); |
| 419 |
return; |
| 420 |
} |
| 421 |
|
| 422 |
void |
| 423 |
refresh_llm_msg(llm_info_t *llm) |
| 424 |
{ |
| 425 |
int maxnode = llm_get_nodecount(llm); |
| 426 |
int size = sizeof(ccm_llm_t)+ maxnode*sizeof(struct node_s); |
| 427 |
ccm_llm_t *data = (ccm_llm_t *)g_malloc(size); |
| 428 |
int i; |
| 429 |
|
| 430 |
data->ev = CCM_LLM; |
| 431 |
/* copy the relevent content of llm into data */ |
| 432 |
CLLM_SET_NODECOUNT(data,maxnode); |
| 433 |
CLLM_SET_MYNODE(data, llm_get_myindex(llm)); |
| 434 |
for ( i = 0; i < maxnode; i ++ ) { |
| 435 |
CLLM_SET_NODEID(data,i,llm_get_nodename(llm,i)); |
| 436 |
CLLM_SET_UUID(data,i,i); |
| 437 |
|
| 438 |
} |
| 439 |
|
| 440 |
if(ipc_llm_message && --(ipc_llm_message->count)==0){ |
| 441 |
delete_message(ipc_llm_message); |
| 442 |
} |
| 443 |
ipc_llm_message = create_message(data, size); |
| 444 |
ipc_llm_message->count++; |
| 445 |
g_free(data); |
| 446 |
|
| 447 |
return; |
| 448 |
} |
| 449 |
|