| 1 |
|
| 2 |
/* |
| 3 |
* ccmmem.c: membership routine |
| 4 |
* |
| 5 |
* Copyright (c) International Business Machines Corp., 2002 |
| 6 |
* Author: Ram Pai ([email protected]) |
| 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 |
|
| 24 |
#include <lha_internal.h> |
| 25 |
#include "ccm.h" |
| 26 |
#include "ccmmisc.h" |
| 27 |
|
| 28 |
void |
| 29 |
ccm_mem_reset(ccm_info_t* info) |
| 30 |
{ |
| 31 |
info->memcount = 0; |
| 32 |
return; |
| 33 |
} |
| 34 |
|
| 35 |
int |
| 36 |
ccm_mem_add(ccm_info_t* info, int index) |
| 37 |
{ |
| 38 |
|
| 39 |
int count; |
| 40 |
|
| 41 |
if (info == NULL){ |
| 42 |
ccm_log(LOG_ERR, "%s: NULL pointer", |
| 43 |
__FUNCTION__); |
| 44 |
return HA_FAIL; |
| 45 |
} |
| 46 |
if (index < 0 || index > llm_get_nodecount(&info->llm)){ |
| 47 |
ccm_log(LOG_ERR, "%s: index(%d) out of range", |
| 48 |
__FUNCTION__, index); |
| 49 |
return HA_FAIL; |
| 50 |
} |
| 51 |
|
| 52 |
count = info->memcount; |
| 53 |
info->ccm_member[count] = index; |
| 54 |
|
| 55 |
info->memcount++; |
| 56 |
|
| 57 |
if (info->memcount >= MAXNODE){ |
| 58 |
ccm_log(LOG_ERR, "%s: membership count(%d) out of range", |
| 59 |
__FUNCTION__, info->memcount); |
| 60 |
return HA_FAIL; |
| 61 |
} |
| 62 |
|
| 63 |
return HA_OK; |
| 64 |
} |
| 65 |
|
| 66 |
int |
| 67 |
ccm_get_memcount(ccm_info_t* info){ |
| 68 |
|
| 69 |
if (info == NULL){ |
| 70 |
ccm_log(LOG_ERR, "%s: NULL pointer", |
| 71 |
__FUNCTION__); |
| 72 |
return -1; |
| 73 |
} |
| 74 |
|
| 75 |
return info->memcount; |
| 76 |
|
| 77 |
} |
| 78 |
|
| 79 |
int |
| 80 |
ccm_mem_bitmapfill(ccm_info_t *info, |
| 81 |
const char *bitmap) |
| 82 |
{ |
| 83 |
llm_info_t *llm; |
| 84 |
uint i; |
| 85 |
|
| 86 |
llm = &info->llm; |
| 87 |
ccm_mem_reset(info); |
| 88 |
for ( i = 0 ; i < llm_get_nodecount(llm); i++ ) { |
| 89 |
if(bitmap_test(i, bitmap, MAXNODE)){ |
| 90 |
if (ccm_mem_add(info, i) != HA_OK){ |
| 91 |
ccm_log(LOG_ERR, "%s: adding node(%s)" |
| 92 |
"to member failed", |
| 93 |
__FUNCTION__, |
| 94 |
llm_get_nodename(llm, i)); |
| 95 |
return HA_FAIL; |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
return HA_OK; |
| 101 |
} |
| 102 |
|
| 103 |
int |
| 104 |
ccm_mem_strfill(ccm_info_t *info, |
| 105 |
const char *memlist) |
| 106 |
{ |
| 107 |
char *bitmap = NULL; |
| 108 |
int ret; |
| 109 |
|
| 110 |
bitmap_create(&bitmap, MAXNODE); |
| 111 |
if (bitmap == NULL){ |
| 112 |
ccm_log(LOG_ERR, "%s:bitmap creation failure", |
| 113 |
__FUNCTION__); |
| 114 |
return HA_FAIL; |
| 115 |
} |
| 116 |
if (ccm_str2bitmap((const char *) memlist, strlen(memlist), |
| 117 |
bitmap) < 0){ |
| 118 |
ccm_log(LOG_ERR, "%s: string(%s) to bitmap conversion failed", |
| 119 |
__FUNCTION__, memlist); |
| 120 |
return HA_FAIL; |
| 121 |
} |
| 122 |
|
| 123 |
ret = ccm_mem_bitmapfill(info, bitmap); |
| 124 |
|
| 125 |
bitmap_delete(bitmap); |
| 126 |
|
| 127 |
return ret; |
| 128 |
} |
| 129 |
|
| 130 |
gboolean |
| 131 |
node_is_member(ccm_info_t* info, const char* node) |
| 132 |
{ |
| 133 |
int i,indx; |
| 134 |
llm_info_t *llm = &info->llm; |
| 135 |
|
| 136 |
if (info == NULL || node == NULL){ |
| 137 |
ccm_log(LOG_ERR, "%s: NULL pointer", |
| 138 |
__FUNCTION__); |
| 139 |
return HA_FAIL; |
| 140 |
} |
| 141 |
|
| 142 |
for ( i = 0 ; i < ccm_get_memcount(info) ; i++ ) { |
| 143 |
indx = info->ccm_member[i]; |
| 144 |
if(strncmp(llm_get_nodename(llm, indx), node, |
| 145 |
NODEIDSIZE) == 0){ |
| 146 |
return TRUE; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
return FALSE; |
| 151 |
} |
| 152 |
|
| 153 |
gboolean |
| 154 |
i_am_member(ccm_info_t* info) |
| 155 |
{ |
| 156 |
llm_info_t* llm; |
| 157 |
int i; |
| 158 |
|
| 159 |
if (info == NULL){ |
| 160 |
ccm_log(LOG_ERR, "%s: NULL pointer", |
| 161 |
__FUNCTION__); |
| 162 |
return FALSE; |
| 163 |
} |
| 164 |
|
| 165 |
llm = &info->llm; |
| 166 |
if (llm->myindex <0){ |
| 167 |
ccm_log(LOG_ERR, "%s: myindex in llm is not set", |
| 168 |
__FUNCTION__); |
| 169 |
return FALSE; |
| 170 |
} |
| 171 |
|
| 172 |
for (i = 0; i < info->memcount; i++){ |
| 173 |
if (info->ccm_member[i] == llm->myindex){ |
| 174 |
return TRUE; |
| 175 |
} |
| 176 |
} |
| 177 |
return FALSE; |
| 178 |
} |
| 179 |
|
| 180 |
int |
| 181 |
ccm_mem_delete(ccm_info_t* info, int index) |
| 182 |
{ |
| 183 |
int i; |
| 184 |
int memcount; |
| 185 |
|
| 186 |
if (info == NULL){ |
| 187 |
ccm_log(LOG_ERR, "%s: NULL pointer", |
| 188 |
__FUNCTION__); |
| 189 |
return HA_FAIL; |
| 190 |
} |
| 191 |
if (index < 0 || index > llm_get_nodecount(&info->llm)){ |
| 192 |
ccm_log(LOG_ERR, "%s: index(%d) out of range", |
| 193 |
__FUNCTION__, index); |
| 194 |
return HA_FAIL; |
| 195 |
} |
| 196 |
|
| 197 |
memcount = info->memcount; |
| 198 |
for (i =0; i < info->memcount; i++){ |
| 199 |
if (info->ccm_member[i] == index){ |
| 200 |
info->ccm_member[i] = info->ccm_member[memcount-1]; |
| 201 |
info->memcount--; |
| 202 |
return HA_OK; |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
ccm_log(LOG_ERR, "%s: node index(%d) not found in membership", |
| 207 |
__FUNCTION__, index); |
| 208 |
return HA_FAIL; |
| 209 |
|
| 210 |
} |
| 211 |
|
| 212 |
int |
| 213 |
ccm_mem_update(ccm_info_t *info, const char *node, |
| 214 |
enum change_event_type change_type) |
| 215 |
{ |
| 216 |
llm_info_t *llm = &info->llm; |
| 217 |
|
| 218 |
if (change_type == NODE_LEAVE){ |
| 219 |
return ccm_mem_delete(info, llm_get_index(llm ,node)); |
| 220 |
}else{ |
| 221 |
return ccm_mem_add(info, llm_get_index(llm, node)); |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
int |
| 226 |
ccm_mem_filluptime(ccm_info_t* info, int* uptime_list, int uptime_size) |
| 227 |
{ |
| 228 |
int i; |
| 229 |
|
| 230 |
if (uptime_size != info->memcount){ |
| 231 |
ccm_log(LOG_ERR, "%s: uptime_list size (%d) != memcount(%d)", |
| 232 |
__FUNCTION__, uptime_size, info->memcount); |
| 233 |
return HA_FAIL; |
| 234 |
} |
| 235 |
|
| 236 |
for (i = 0; i < info->memcount; i++){ |
| 237 |
llm_set_uptime(&info->llm, info->ccm_member[i], uptime_list[i]); |
| 238 |
} |
| 239 |
|
| 240 |
return HA_OK; |
| 241 |
} |
| 242 |
|
| 243 |
int |
| 244 |
am_i_member_in_memlist(ccm_info_t *info, const char *memlist) |
| 245 |
{ |
| 246 |
char *bitmap = NULL; |
| 247 |
int numBytes, myindex; |
| 248 |
llm_info_t *llm; |
| 249 |
|
| 250 |
if (info == NULL || memlist == NULL){ |
| 251 |
ccm_log(LOG_ERR, "%s: NULL pointer", |
| 252 |
__FUNCTION__); |
| 253 |
return FALSE; |
| 254 |
} |
| 255 |
|
| 256 |
llm = &info->llm; |
| 257 |
bitmap_create(&bitmap, MAXNODE); |
| 258 |
if (bitmap == NULL){ |
| 259 |
ccm_log(LOG_ERR ,"%s: bitmap creatation failed", |
| 260 |
__FUNCTION__); |
| 261 |
return FALSE; |
| 262 |
} |
| 263 |
|
| 264 |
numBytes = ccm_str2bitmap(memlist, strlen(memlist), bitmap); |
| 265 |
|
| 266 |
myindex = llm_get_myindex(llm); |
| 267 |
|
| 268 |
if (bitmap_test(myindex, bitmap, numBytes*BitsInByte)){ |
| 269 |
bitmap_delete(bitmap); |
| 270 |
return TRUE; |
| 271 |
} |
| 272 |
|
| 273 |
bitmap_delete(bitmap); |
| 274 |
return FALSE; |
| 275 |
} |