Debian Codegraph @

membership/ccm/ccmllm.c

raw
1 /*
2 * ccmllm.c: Low Level membership routines.
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 */
22
23 #include <lha_internal.h>
24 #include "ccm.h"
25
26
27 int
28 llm_get_nodecount(llm_info_t* llm){
29
30 if (llm == NULL){
31 ccm_log(LOG_ERR, "%s: NULL pointer",
32 __FUNCTION__);
33 return -1;
34 }
35
36 return llm->nodecount;
37 }
38
39 int
40 llm_get_live_nodecount(llm_info_t *llm)
41 {
42 int count = 0;
43 int i;
44
45 if (llm == NULL){
46 ccm_log(LOG_ERR, "%s: NULL pointer",
47 __FUNCTION__);
48 return -1;
49 }
50
51 for ( i = 0 ; i < llm->nodecount; i++ ) {
52 const char* status = llm->nodes[i].status;
53
54 if (STRNCMP_CONST(status, DEADSTATUS) != 0){
55 count++;
56 }
57 }
58
59 return count;
60 }
61
62
63 const char *
64 llm_get_nodename(llm_info_t *llm, const int index)
65 {
66 if (llm == NULL){
67 ccm_log(LOG_ERR, "%s: NULL pointer",
68 __FUNCTION__);
69 return "<INVALID LLM POINTER>";
70 }
71
72 if (index < 0 || index > MAXNODE){
73 ccm_log(LOG_ERR, "%s: index(%d) out of range",
74 __FUNCTION__, index);
75 return "<INVALID NODE INDEX>";
76 }
77
78 return llm->nodes[index].nodename;
79
80 }
81
82 char *
83 llm_get_nodestatus(llm_info_t* llm, const int index)
84 {
85 if (llm == NULL){
86 ccm_log(LOG_ERR, "%s: NULL pointer",
87 __FUNCTION__);
88 return NULL;
89 }
90
91 if (index < 0 || index > MAXNODE){
92 ccm_log(LOG_ERR, "%s: index(%d) out of range",
93 __FUNCTION__, index);
94 return NULL;
95 }
96
97 return llm->nodes[index].status;
98
99 }
100
101 int
102 llm_node_cmp(llm_info_t *llm, int indx1, int indx2)
103 {
104 return strncmp(llm_get_nodename(llm, indx1),
105 llm_get_nodename(llm, indx2), NODEIDSIZE);
106 }
107
108
109
110 void
111 llm_display(llm_info_t *llm)
112 {
113 unsigned int i;
114 ccm_debug2(LOG_DEBUG, "total node number is %d", llm->nodecount);
115 for (i = 0 ;i < llm->nodecount; i++){
116 ccm_debug2(LOG_DEBUG, "node %d =%s, status=%s",
117 i, llm->nodes[i].nodename, llm->nodes[i].status);
118 }
119
120 }
121
122 int
123 llm_get_myindex(llm_info_t* llm)
124 {
125 if (llm == NULL){
126 ccm_log(LOG_ERR, "NULL pointer");
127 return -1;
128 }
129 return llm->myindex;
130 }
131
132 const char*
133 llm_get_mynodename(llm_info_t* llm)
134 {
135 if (llm == NULL){
136 ccm_log(LOG_ERR, "%s: NULL pointer",
137 __FUNCTION__);
138 return NULL;
139 }
140
141 if (llm->myindex < 0){
142 ccm_log(LOG_ERR, "%s: mynode is not set",
143 __FUNCTION__);
144 return NULL;
145 }
146
147 return llm->nodes[llm->myindex].nodename;
148
149 }
150
151 int
152 llm_get_index(llm_info_t *llm, const char *node)
153 {
154 int low,high,mid;
155 int value;
156
157 /*binary search */
158 low = 0;
159 high = llm->nodecount - 1;
160 do {
161 mid = (low+high+1)/2;
162 value = strncmp(llm_get_nodename(llm, mid), node, NODEIDSIZE);
163 if(value==0) {
164 return mid;
165 }
166
167 if(high == low) {
168 break;
169 }
170
171 if(value > 0) {
172 high=mid-1;
173 }
174 else {
175 low=mid+1;
176 }
177 } while(high>=low);
178
179 return -1;
180 }
181
182 int
183 llm_status_update(llm_info_t *llm, const char *node,
184 const char *status, char* oldstatus)
185 {
186 int i;
187
188 i = llm_get_index(llm, node);
189 if(i == -1){
190 return HA_FAIL;
191 }
192
193 if (oldstatus){
194 strncpy(oldstatus, llm->nodes[i].status, STATUSSIZE);
195 }
196
197 strncpy(llm->nodes[i].status, status, STATUSSIZE);
198
199 return HA_OK;
200 }
201
202
203 int
204 llm_is_valid_node(llm_info_t *llm,
205 const char *node)
206 {
207 if(llm_get_index(llm, node) == -1 ) {
208 return FALSE;
209 }
210 return TRUE;
211 }
212
213 int
214 llm_init(llm_info_t *llm)
215 {
216 if (llm == NULL){
217 ccm_log(LOG_ERR, "%s: NULL pointer",
218 __FUNCTION__);
219
220 return HA_FAIL;
221 }
222
223 llm->nodecount = 0;
224 llm->myindex = -1;
225
226 return HA_OK;
227 }
228
229
230 int
231 llm_del(llm_info_t* llm,
232 const char* node)
233 {
234 int i;
235 int j;
236
237 for ( i = 0 ;i < llm->nodecount; i++){
238 if (strncmp(llm->nodes[i].nodename, node, NODEIDSIZE)==0){
239 break;
240 }
241 }
242
243 if (i == llm->nodecount){
244 ccm_log(LOG_ERR, "%s: Node %s not found in llm",
245 __FUNCTION__,
246 node);
247 return HA_FAIL;
248 }
249
250 if (llm->myindex > i){
251 llm->myindex --;
252 }else if (llm->myindex ==i){
253 ccm_log(LOG_ERR, "%s: deleting myself in ccm is not allowed",
254 __FUNCTION__);
255 return HA_FAIL;
256 }
257
258
259 for ( j = i; j< llm->nodecount - 1; j++){
260 strncpy(llm->nodes[j].nodename, llm->nodes[j+1].nodename, NODEIDSIZE);
261 strncpy(llm->nodes[j].status, llm->nodes[j+1].status, STATUSSIZE);
262
263 }
264
265 llm->nodecount --;
266
267 return HA_OK;
268 }
269
270
271
272 int
273 llm_add(llm_info_t *llm,
274 const char *node,
275 const char *status,
276 const char *mynode,
277 const char *site,
278 int weight)
279 {
280 int nodecount;
281 int i, j;
282
283 nodecount = llm->nodecount;
284 if (nodecount < 0 || nodecount > MAXNODE ){
285 ccm_log(LOG_ERR, "nodecount out of range(%d)",
286 nodecount);
287 return HA_FAIL;
288 }
289
290
291
292 for ( i = 0 ; i < nodecount ; i++ ) {
293 int value = strncmp(llm_get_nodename(llm, i),
294 node, NODEIDSIZE);
295 if (value == 0){
296 ccm_log(LOG_ERR, "%s: adding same node(%s) twice(?)",
297 __FUNCTION__, node);
298 return HA_FAIL;
299 }
300 if (value > 0) {
301 break;
302 }
303 }
304
305 for ( j = nodecount; j > i; j-- ) {
306 llm->nodes[j] = llm->nodes[j - 1];
307 }
308
309 llm->nodes[i].join_request = FALSE;
310 strncpy(llm->nodes[i].nodename, node,NODEIDSIZE);
311 strncpy(llm->nodes[i].status, status, STATUSSIZE);
312 strncpy(llm->nodes[i].site, site, PATH_MAX);
313 llm->nodes[i].weight = weight;
314 llm->nodecount++;
315
316 if (llm->myindex >= i) {
317 llm->myindex++;
318 }
319
320 if (llm->myindex < 0
321 && strncmp(mynode, node, NODEIDSIZE) == 0) {
322 llm->myindex = i;
323 }
324
325 if (llm->myindex >= llm->nodecount){
326 ccm_log(LOG_ERR, "%s: myindex(%d) out of range,"
327 "llm->nodecount =%d",
328 __FUNCTION__, llm->myindex, llm->nodecount);
329 return HA_FAIL;
330 }
331
332 return HA_OK;
333 }
334
335 int
336 llm_set_joinrequest(llm_info_t* llm, int index, gboolean value, int major_trans)
337 {
338 if (llm == NULL){
339 ccm_log(LOG_ERR, "%s: NULL pointer",
340 __FUNCTION__);
341 return HA_FAIL;
342 }
343
344 if (index < 0 || index > MAXNODE){
345 ccm_log(LOG_ERR, "%s: index(%d) out of range",
346 __FUNCTION__, index);
347 return HA_FAIL;
348 }
349
350 llm->nodes[index].join_request = value;
351 llm->nodes[index].join_request_major_trans = major_trans;
352 return HA_OK;
353 }
354
355 gboolean
356 llm_get_joinrequest(llm_info_t* llm, int index)
357 {
358 if (llm == NULL){
359 ccm_log(LOG_ERR, "%s: NULL pointer",
360 __FUNCTION__);
361 return FALSE;
362 }
363
364 if (index < 0 || index > MAXNODE){
365 ccm_log(LOG_ERR, "%s: index(%d) out of range",
366 __FUNCTION__, index);
367 return FALSE;
368 }
369
370 return llm->nodes[index].join_request;
371
372 }
373
374
375 gboolean
376 llm_get_joinrequest_majortrans(llm_info_t* llm, int index)
377 {
378 if (llm == NULL){
379 ccm_log(LOG_ERR, "%s: NULL pointer",
380 __FUNCTION__);
381 return FALSE;
382 }
383
384 if (index < 0 || index > MAXNODE){
385 ccm_log(LOG_ERR, "%s: index(%d) out of range",
386 __FUNCTION__, index);
387 return FALSE;
388 }
389
390 return llm->nodes[index].join_request_major_trans;
391
392 }
393
394 int
395 llm_set_change(llm_info_t* llm, int index, gboolean value)
396 {
397 if (llm == NULL){
398 ccm_log(LOG_ERR, "%s: NULL pointer",
399 __FUNCTION__);
400 return HA_FAIL;
401 }
402
403 if (index < 0 || index > MAXNODE){
404 ccm_log(LOG_ERR, "%s: index(%d) out of range",
405 __FUNCTION__, index);
406 return HA_FAIL;
407 }
408
409 llm->nodes[index].receive_change_msg = value;
410
411 return HA_OK;
412 }
413
414 gboolean
415 llm_get_change(llm_info_t* llm, int index)
416 {
417 if (llm == NULL){
418 ccm_log(LOG_ERR, "%s: NULL pointer",
419 __FUNCTION__);
420 return FALSE;
421 }
422
423 if (index < 0 || index > MAXNODE){
424 ccm_log(LOG_ERR, "%s: index(%d) out of range",
425 __FUNCTION__, index);
426 return FALSE;
427 }
428
429 return llm->nodes[index].receive_change_msg;
430 }
431
432 int
433 llm_set_uptime(llm_info_t* llm, int index, int uptime)
434 {
435 if (llm == NULL){
436 ccm_log(LOG_ERR, "%s: NULL pointer",
437 __FUNCTION__);
438 return FALSE;
439 }
440
441 if (index < 0 || index > MAXNODE){
442 ccm_log(LOG_ERR, "%s: index(%d) out of range",
443 __FUNCTION__, index);
444 return FALSE;
445 }
446
447 if (uptime < 0){
448 ccm_log(LOG_ERR, "%s: Negative uptime %d for node %d [%s]",
449 __FUNCTION__, uptime, index, llm_get_nodename(llm,index));
450 return FALSE;
451
452 }
453
454 llm->nodes[index].uptime = uptime;
455
456 return HA_OK;
457 }
458
459 int
460 llm_get_uptime(llm_info_t* llm, int index)
461 {
462
463 if (llm == NULL){
464 ccm_log(LOG_ERR, "%s: NULL pointer",
465 __FUNCTION__);
466 return -1;
467 }
468
469 if (index < 0 || index > MAXNODE){
470 ccm_log(LOG_ERR, "%s: index(%d) out of range",
471 __FUNCTION__, index);
472 return -1;
473 }
474
475 return llm->nodes[index].uptime;
476
477
478 }