| 1 |
/* |
| 2 |
* ccmmisc.c: Miscellaneous Consensus Cluster Service functions |
| 3 |
* |
| 4 |
* Copyright (c) International Business Machines Corp., 2002 |
| 5 |
* Author: Ram Pai ([email protected]) |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program; if not, write to the Free Software |
| 19 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 |
*/ |
| 21 |
#include <lha_internal.h> |
| 22 |
#include <ccm.h> |
| 23 |
#include <stdlib.h> |
| 24 |
#ifdef HAVE_MALLINFO |
| 25 |
#include <malloc.h> |
| 26 |
#endif |
| 27 |
#include "ccmmisc.h" |
| 28 |
|
| 29 |
#if 0 |
| 30 |
int |
| 31 |
ccm_bitmap2str(const char *bitmap, char* memlist, int size) |
| 32 |
{ |
| 33 |
int num_member = 0; |
| 34 |
char* p; |
| 35 |
int i; |
| 36 |
|
| 37 |
if (bitmap == NULL || |
| 38 |
memlist == NULL || |
| 39 |
size <= 0){ |
| 40 |
ccm_log(LOG_ERR, "invalid arguments"); |
| 41 |
return -1; |
| 42 |
} |
| 43 |
|
| 44 |
p =memlist; |
| 45 |
for ( i = 0 ; i < MAXNODE; i++ ) { |
| 46 |
if(bitmap_test(i, bitmap, MAXNODE)){ |
| 47 |
num_member++; |
| 48 |
p += sprintf(p, "%d ", i); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
return strnlen(memlist, size); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
int |
| 57 |
ccm_str2bitmap(const char *_memlist, int size, char *bitmap) |
| 58 |
{ |
| 59 |
char memlist[MAX_MEMLIST_STRING]; |
| 60 |
char* p; |
| 61 |
int num_members = 0; |
| 62 |
|
| 63 |
if (memlist == NULL |
| 64 |
|| size <= 0 |
| 65 |
|| size >= MAX_MEMLIST_STRING |
| 66 |
|| bitmap == NULL){ |
| 67 |
ccm_log(LOG_ERR, "invalid arguments"); |
| 68 |
return -1; |
| 69 |
} |
| 70 |
|
| 71 |
memset(memlist, 0, MAX_MEMLIST_STRING); |
| 72 |
memcpy(memlist, _memlist, size); |
| 73 |
|
| 74 |
p = strtok(memlist, " "); |
| 75 |
while ( p != NULL){ |
| 76 |
int i = atoi(p); |
| 77 |
bitmap_mark(i, bitmap, MAXNODE); |
| 78 |
num_members ++; |
| 79 |
p = strtok(NULL, " "); |
| 80 |
} |
| 81 |
|
| 82 |
return num_members; |
| 83 |
|
| 84 |
} |
| 85 |
#else |
| 86 |
|
| 87 |
int |
| 88 |
ccm_str2bitmap(const char *memlist, int size, char *bitmap) |
| 89 |
{ |
| 90 |
int outbytes = B64_maxbytelen(size); |
| 91 |
|
| 92 |
if (size == 0) { |
| 93 |
return 0; |
| 94 |
} |
| 95 |
|
| 96 |
outbytes = base64_to_binary(memlist, size, bitmap, outbytes); |
| 97 |
|
| 98 |
return outbytes; |
| 99 |
} |
| 100 |
|
| 101 |
int |
| 102 |
ccm_bitmap2str(const char *bitmap, char *memlist, int size) |
| 103 |
{ |
| 104 |
int maxstrsize; |
| 105 |
int bytes; |
| 106 |
|
| 107 |
bytes = MAXNODE / BitsInByte; |
| 108 |
if (MAXNODE%BitsInByte != 0){ |
| 109 |
bytes++; |
| 110 |
} |
| 111 |
maxstrsize = B64_stringlen(bytes)+1; |
| 112 |
|
| 113 |
if (maxstrsize > MAX_MEMLIST_STRING){ |
| 114 |
ccm_log(LOG_ERR, "MAX_MEMLIST_STRING is too small(%d), sized required %d", |
| 115 |
MAX_MEMLIST_STRING, maxstrsize); |
| 116 |
return -1; |
| 117 |
} |
| 118 |
return binary_to_base64(bitmap, bytes, memlist, size); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
#endif |
| 123 |
|
| 124 |
|
| 125 |
longclock_t |
| 126 |
ccm_get_time(void) |
| 127 |
{ |
| 128 |
return time_longclock(); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
/* |
| 133 |
* given two times, and a timeout interval(in milliseconds), |
| 134 |
* return true if the timeout has occured, else return |
| 135 |
* false. |
| 136 |
* NOTE: 'timeout' is in milliseconds. |
| 137 |
*/ |
| 138 |
int |
| 139 |
ccm_timeout(longclock_t t1, longclock_t t2, unsigned long timeout) |
| 140 |
{ |
| 141 |
longclock_t t1cl; |
| 142 |
|
| 143 |
t1cl = add_longclock(t1 , msto_longclock(timeout)); |
| 144 |
|
| 145 |
if(cmp_longclock(t1cl, t2) < 0) { |
| 146 |
return TRUE; |
| 147 |
} |
| 148 |
return FALSE; |
| 149 |
} |
| 150 |
|
| 151 |
void |
| 152 |
ccm_check_memoryleak(void) |
| 153 |
{ |
| 154 |
#ifdef HAVE_MALLINFO |
| 155 |
/* check for memory leaks */ |
| 156 |
struct mallinfo i; |
| 157 |
static int arena=0; |
| 158 |
static int count = 0; |
| 159 |
|
| 160 |
++count; |
| 161 |
/* Mallinfo is surprisingly expensive */ |
| 162 |
if (count >= 60) { |
| 163 |
count = 0; |
| 164 |
i = mallinfo(); |
| 165 |
if(arena==0) { |
| 166 |
arena = i.arena; |
| 167 |
} else if(arena < i.arena) { |
| 168 |
ccm_debug(LOG_WARNING, |
| 169 |
"leaking memory? previous arena=%d " |
| 170 |
"present arena=%d", |
| 171 |
arena, i.arena); |
| 172 |
arena=i.arena; |
| 173 |
} |
| 174 |
} |
| 175 |
#endif |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
/* |
| 180 |
* When ccm running on a node leaves the cluster voluntarily it |
| 181 |
* sends a leave message to the other nodes in the cluster. |
| 182 |
* Similarly whenever ccm running on some node of the cluster, |
| 183 |
* dies the local heartbeat delivers a leave message to ccm. |
| 184 |
* And whenever some node in the cluster dies, local heartbeat |
| 185 |
* informs the death through a callback. |
| 186 |
* In all these cases, ccm is informed about the loss of the node, |
| 187 |
* asynchronously, in some context where immidiate processing of |
| 188 |
* the message is not possible. |
| 189 |
* The following set of routines act as a cache that keep track |
| 190 |
* of message leaves and facilitates the delivery of these |
| 191 |
* messages at a convinient time. |
| 192 |
* |
| 193 |
*/ |
| 194 |
|
| 195 |
static char *leave_bitmap=NULL; |
| 196 |
|
| 197 |
void |
| 198 |
leave_init(void) |
| 199 |
{ |
| 200 |
int numBytes; |
| 201 |
|
| 202 |
assert(!leave_bitmap); |
| 203 |
numBytes = bitmap_create(&leave_bitmap, MAXNODE); |
| 204 |
memset(leave_bitmap, 0, numBytes); |
| 205 |
} |
| 206 |
|
| 207 |
void |
| 208 |
leave_reset(void) |
| 209 |
{ |
| 210 |
int numBytes = bitmap_size(MAXNODE); |
| 211 |
if(!leave_bitmap) { |
| 212 |
return; |
| 213 |
} |
| 214 |
memset(leave_bitmap, 0, numBytes); |
| 215 |
return; |
| 216 |
} |
| 217 |
|
| 218 |
void |
| 219 |
leave_cache(int i) |
| 220 |
{ |
| 221 |
assert(leave_bitmap); |
| 222 |
bitmap_mark(i, leave_bitmap, MAXNODE); |
| 223 |
} |
| 224 |
|
| 225 |
int |
| 226 |
leave_get_next(void) |
| 227 |
{ |
| 228 |
int i; |
| 229 |
|
| 230 |
assert(leave_bitmap); |
| 231 |
for ( i = 0 ; i < MAXNODE; i++ ) { |
| 232 |
if(bitmap_test(i,leave_bitmap,MAXNODE)) { |
| 233 |
bitmap_clear(i,leave_bitmap,MAXNODE); |
| 234 |
return i; |
| 235 |
} |
| 236 |
} |
| 237 |
return -1; |
| 238 |
} |
| 239 |
|
| 240 |
int |
| 241 |
leave_any(void) |
| 242 |
{ |
| 243 |
if(bitmap_count(leave_bitmap,MAXNODE)){ |
| 244 |
return TRUE; |
| 245 |
} |
| 246 |
return FALSE; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
gboolean |
| 252 |
part_of_cluster(int state) |
| 253 |
{ |
| 254 |
if (state >= CCM_STATE_END |
| 255 |
|| state < 0){ |
| 256 |
ccm_log(LOG_ERR, "part_of_cluster:wrong state(%d)", state); |
| 257 |
return FALSE; |
| 258 |
} |
| 259 |
|
| 260 |
if (state == CCM_STATE_VERSION_REQUEST |
| 261 |
|| state == CCM_STATE_NONE){ |
| 262 |
return FALSE; |
| 263 |
} |
| 264 |
|
| 265 |
return TRUE; |
| 266 |
|
| 267 |
|
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
/* the ccm strings tokens communicated aross the wire. |
| 272 |
* these are the values for the F_TYPE names. |
| 273 |
*/ |
| 274 |
|
| 275 |
#define TYPESTRSIZE 32 |
| 276 |
char ccm_type_str[CCM_TYPE_LAST + 1][TYPESTRSIZE] = { |
| 277 |
"CCM_TYPE_PROTOVERSION", |
| 278 |
"CCM_TYPE_PROTOVERSION_RESP", |
| 279 |
"CCM_TYPE_JOIN", |
| 280 |
"CCM_TYPE_REQ_MEMLIST", |
| 281 |
"CCM_TYPE_RES_MEMLIST", |
| 282 |
"CCM_TYPE_FINAL_MEMLIST", |
| 283 |
"CCM_TYPE_ABORT", |
| 284 |
"CCM_TYPE_LEAVE", |
| 285 |
"CCM_TYPE_TIMEOUT", |
| 286 |
"CCM_TYPE_NODE_LEAVE_NOTICE", |
| 287 |
"CCM_TYPE_NODE_LEAVE", |
| 288 |
"CCM_TYPE_MEM_LIST", |
| 289 |
"CCM_TYPE_ALIVE", |
| 290 |
"CCM_TYPE_NEW_NODE", |
| 291 |
"CCM_TYPE_STATE_INFO", |
| 292 |
"CCM_TYPE_RESTART", |
| 293 |
"CCM_TYPE_LAST" |
| 294 |
}; |
| 295 |
|
| 296 |
|
| 297 |
int |
| 298 |
ccm_string2type(const char *type) |
| 299 |
{ |
| 300 |
int i; |
| 301 |
|
| 302 |
for ( i = CCM_TYPE_PROTOVERSION; i <= CCM_TYPE_LAST; i++ ) { |
| 303 |
if (strncmp(ccm_type_str[i], type, TYPESTRSIZE) == 0){ |
| 304 |
return i; |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
/* this message is not any type of ccm state messages |
| 309 |
* but some other message from heartbeat |
| 310 |
*/ |
| 311 |
|
| 312 |
return -1; |
| 313 |
} |
| 314 |
|
| 315 |
char * |
| 316 |
ccm_type2string(enum ccm_type type) |
| 317 |
{ |
| 318 |
return ccm_type_str[type]; |
| 319 |
} |
| 320 |
|