ctdbd: When a node is connected, log at DEBUG NOTICE not DEBUG_INFO
[martins/ctdb.git] / server / ctdb_server.c
1 /* 
2    ctdb main protocol code
3
4    Copyright (C) Andrew Tridgell  2006
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "tdb.h"
22 #include "lib/util/dlinklist.h"
23 #include "system/network.h"
24 #include "system/filesys.h"
25 #include "../include/ctdb_private.h"
26
27 /*
28   choose the transport we will use
29 */
30 int ctdb_set_transport(struct ctdb_context *ctdb, const char *transport)
31 {
32         ctdb->transport = talloc_strdup(ctdb, transport);
33         CTDB_NO_MEMORY(ctdb, ctdb->transport);
34
35         return 0;
36 }
37
38 /*
39   Check whether an ip is a valid node ip
40   Returns the node id for this ip address or -1
41 */
42 int ctdb_ip_to_nodeid(struct ctdb_context *ctdb, const char *nodeip)
43 {
44         int nodeid;
45
46         for (nodeid=0;nodeid<ctdb->num_nodes;nodeid++) {
47                 if (ctdb->nodes[nodeid]->flags & NODE_FLAGS_DELETED) {
48                         continue;
49                 }
50                 if (!strcmp(ctdb->nodes[nodeid]->address.address, nodeip)) {
51                         return nodeid;
52                 }
53         }
54
55         return -1;
56 }
57
58 /*
59   choose the recovery lock file
60 */
61 int ctdb_set_recovery_lock_file(struct ctdb_context *ctdb, const char *file)
62 {
63         if (ctdb->recovery_lock_file != NULL) {
64                 talloc_free(ctdb->recovery_lock_file);
65                 ctdb->recovery_lock_file = NULL;
66         }
67
68         if (file == NULL) {
69                 DEBUG(DEBUG_ALERT,("Recovery lock file set to \"\". Disabling recovery lock checking\n"));
70                 ctdb->tunable.verify_recovery_lock = 0;
71                 return 0;
72         }
73
74         ctdb->recovery_lock_file = talloc_strdup(ctdb, file);
75         CTDB_NO_MEMORY(ctdb, ctdb->recovery_lock_file);
76
77         return 0;
78 }
79
80 /*
81   add a node to the list of nodes
82 */
83 static int ctdb_add_node(struct ctdb_context *ctdb, char *nstr)
84 {
85         struct ctdb_node *node, **nodep;
86
87         nodep = talloc_realloc(ctdb, ctdb->nodes, struct ctdb_node *, ctdb->num_nodes+1);
88         CTDB_NO_MEMORY(ctdb, nodep);
89
90         ctdb->nodes = nodep;
91         nodep = &ctdb->nodes[ctdb->num_nodes];
92         (*nodep) = talloc_zero(ctdb->nodes, struct ctdb_node);
93         CTDB_NO_MEMORY(ctdb, *nodep);
94         node = *nodep;
95
96         if (ctdb_parse_address(ctdb, node, nstr, &node->address) != 0) {
97                 return -1;
98         }
99         node->ctdb = ctdb;
100         node->name = talloc_asprintf(node, "%s:%u", 
101                                      node->address.address, 
102                                      node->address.port);
103         /* this assumes that the nodes are kept in sorted order, and no gaps */
104         node->pnn = ctdb->num_nodes;
105
106         /* nodes start out disconnected and unhealthy */
107         node->flags = (NODE_FLAGS_DISCONNECTED | NODE_FLAGS_UNHEALTHY);
108
109         if (ctdb->address.address &&
110             ctdb_same_address(&ctdb->address, &node->address)) {
111                 /* for automatic binding to interfaces, see tcp_connect.c */
112                 ctdb->pnn = node->pnn;
113         }
114
115         ctdb->num_nodes++;
116         node->dead_count = 0;
117
118         return 0;
119 }
120
121 /*
122   add an entry for a "deleted" node to the list of nodes.
123   a "deleted" node is a node that is commented out from the nodes file.
124   this is used to prevent that subsequent nodes in the nodes list
125   change their pnn value if a node is "delete" by commenting it out and then
126   using "ctdb reloadnodes" at runtime.
127 */
128 static int ctdb_add_deleted_node(struct ctdb_context *ctdb)
129 {
130         struct ctdb_node *node, **nodep;
131
132         nodep = talloc_realloc(ctdb, ctdb->nodes, struct ctdb_node *, ctdb->num_nodes+1);
133         CTDB_NO_MEMORY(ctdb, nodep);
134
135         ctdb->nodes = nodep;
136         nodep = &ctdb->nodes[ctdb->num_nodes];
137         (*nodep) = talloc_zero(ctdb->nodes, struct ctdb_node);
138         CTDB_NO_MEMORY(ctdb, *nodep);
139         node = *nodep;
140         
141         if (ctdb_parse_address(ctdb, node, "0.0.0.0", &node->address) != 0) {
142                 DEBUG(DEBUG_ERR,("Failed to setup deleted node %d\n", ctdb->num_nodes));
143                 return -1;
144         }
145         node->ctdb = ctdb;
146         node->name = talloc_strdup(node, "0.0.0.0:0");
147
148         /* this assumes that the nodes are kept in sorted order, and no gaps */
149         node->pnn = ctdb->num_nodes;
150
151         /* this node is permanently deleted/disconnected */
152         node->flags = NODE_FLAGS_DELETED|NODE_FLAGS_DISCONNECTED;
153
154         ctdb->num_nodes++;
155         node->dead_count = 0;
156
157         return 0;
158 }
159
160
161 /*
162   setup the node list from a file
163 */
164 static int ctdb_set_nlist(struct ctdb_context *ctdb, const char *nlist)
165 {
166         char **lines;
167         int nlines;
168         int i, j, num_present;
169
170         talloc_free(ctdb->nodes);
171         ctdb->nodes     = NULL;
172         ctdb->num_nodes = 0;
173
174         lines = file_lines_load(nlist, &nlines, ctdb);
175         if (lines == NULL) {
176                 ctdb_set_error(ctdb, "Failed to load nlist '%s'\n", nlist);
177                 return -1;
178         }
179         while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
180                 nlines--;
181         }
182
183         num_present = 0;
184         for (i=0; i < nlines; i++) {
185                 char *node;
186
187                 node = lines[i];
188                 /* strip leading spaces */
189                 while((*node == ' ') || (*node == '\t')) {
190                         node++;
191                 }
192                 if (*node == '#') {
193                         if (ctdb_add_deleted_node(ctdb) != 0) {
194                                 talloc_free(lines);
195                                 return -1;
196                         }
197                         continue;
198                 }
199                 if (strcmp(node, "") == 0) {
200                         continue;
201                 }
202                 if (ctdb_add_node(ctdb, node) != 0) {
203                         talloc_free(lines);
204                         return -1;
205                 }
206                 num_present++;
207         }
208
209         /* initialize the vnn mapping table now that we have the nodes list,
210            skipping any deleted nodes
211         */
212         ctdb->vnn_map = talloc(ctdb, struct ctdb_vnn_map);
213         CTDB_NO_MEMORY(ctdb, ctdb->vnn_map);
214
215         ctdb->vnn_map->generation = INVALID_GENERATION;
216         ctdb->vnn_map->size = num_present;
217         ctdb->vnn_map->map = talloc_array(ctdb->vnn_map, uint32_t, ctdb->vnn_map->size);
218         CTDB_NO_MEMORY(ctdb, ctdb->vnn_map->map);
219
220         for(i=0, j=0; i < ctdb->vnn_map->size; i++) {
221                 if (ctdb->nodes[i]->flags & NODE_FLAGS_DELETED) {
222                         continue;
223                 }
224                 ctdb->vnn_map->map[j] = i;
225                 j++;
226         }
227         
228         talloc_free(lines);
229         return 0;
230 }
231
232 void ctdb_load_nodes_file(struct ctdb_context *ctdb)
233 {
234         int ret;
235
236         ret = ctdb_set_nlist(ctdb, ctdb->nodes_file);
237         if (ret == -1) {
238                 DEBUG(DEBUG_ALERT,("ctdb_set_nlist failed - %s\n", ctdb_errstr(ctdb)));
239                 exit(1);
240         }
241 }
242
243 /*
244   setup the local node address
245 */
246 int ctdb_set_address(struct ctdb_context *ctdb, const char *address)
247 {
248         if (ctdb_parse_address(ctdb, ctdb, address, &ctdb->address) != 0) {
249                 return -1;
250         }
251         
252         ctdb->name = talloc_asprintf(ctdb, "%s:%u", 
253                                      ctdb->address.address, 
254                                      ctdb->address.port);
255         return 0;
256 }
257
258
259 /*
260   return the number of active nodes
261 */
262 uint32_t ctdb_get_num_active_nodes(struct ctdb_context *ctdb)
263 {
264         int i;
265         uint32_t count=0;
266         for (i=0; i < ctdb->num_nodes; i++) {
267                 if (!(ctdb->nodes[i]->flags & NODE_FLAGS_INACTIVE)) {
268                         count++;
269                 }
270         }
271         return count;
272 }
273
274
275 /*
276   called when we need to process a packet. This can be a requeued packet
277   after a lockwait, or a real packet from another node
278 */
279 void ctdb_input_pkt(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
280 {
281         TALLOC_CTX *tmp_ctx;
282
283         /* place the packet as a child of the tmp_ctx. We then use
284            talloc_free() below to free it. If any of the calls want
285            to keep it, then they will steal it somewhere else, and the
286            talloc_free() will only free the tmp_ctx */
287         tmp_ctx = talloc_new(ctdb);
288         talloc_steal(tmp_ctx, hdr);
289
290         DEBUG(DEBUG_DEBUG,(__location__ " ctdb request %u of type %u length %u from "
291                  "node %u to %u\n", hdr->reqid, hdr->operation, hdr->length,
292                  hdr->srcnode, hdr->destnode));
293
294         switch (hdr->operation) {
295         case CTDB_REQ_CALL:
296         case CTDB_REPLY_CALL:
297         case CTDB_REQ_DMASTER:
298         case CTDB_REPLY_DMASTER:
299                 /* we dont allow these calls when banned */
300                 if (ctdb->nodes[ctdb->pnn]->flags & NODE_FLAGS_BANNED) {
301                         DEBUG(DEBUG_DEBUG,(__location__ " ctdb operation %u"
302                                 " request %u"
303                                 " length %u from node %u to %u while node"
304                                 " is banned\n",
305                                  hdr->operation, hdr->reqid,
306                                  hdr->length, 
307                                  hdr->srcnode, hdr->destnode));
308                         goto done;
309                 }
310
311                 /* for ctdb_call inter-node operations verify that the
312                    remote node that sent us the call is running in the
313                    same generation instance as this node
314                 */
315                 if (ctdb->vnn_map->generation != hdr->generation) {
316                         DEBUG(DEBUG_DEBUG,(__location__ " ctdb operation %u"
317                                 " request %u"
318                                 " length %u from node %u to %u had an"
319                                 " invalid generation id:%u while our"
320                                 " generation id is:%u\n", 
321                                  hdr->operation, hdr->reqid,
322                                  hdr->length, 
323                                  hdr->srcnode, hdr->destnode, 
324                                  hdr->generation, ctdb->vnn_map->generation));
325                         goto done;
326                 }
327         }
328
329         switch (hdr->operation) {
330         case CTDB_REQ_CALL:
331                 CTDB_INCREMENT_STAT(ctdb, node.req_call);
332                 ctdb_request_call(ctdb, hdr);
333                 break;
334
335         case CTDB_REPLY_CALL:
336                 CTDB_INCREMENT_STAT(ctdb, node.reply_call);
337                 ctdb_reply_call(ctdb, hdr);
338                 break;
339
340         case CTDB_REPLY_ERROR:
341                 CTDB_INCREMENT_STAT(ctdb, node.reply_error);
342                 ctdb_reply_error(ctdb, hdr);
343                 break;
344
345         case CTDB_REQ_DMASTER:
346                 CTDB_INCREMENT_STAT(ctdb, node.req_dmaster);
347                 ctdb_request_dmaster(ctdb, hdr);
348                 break;
349
350         case CTDB_REPLY_DMASTER:
351                 CTDB_INCREMENT_STAT(ctdb, node.reply_dmaster);
352                 ctdb_reply_dmaster(ctdb, hdr);
353                 break;
354
355         case CTDB_REQ_MESSAGE:
356                 CTDB_INCREMENT_STAT(ctdb, node.req_message);
357                 ctdb_request_message(ctdb, hdr);
358                 break;
359
360         case CTDB_REQ_CONTROL:
361                 CTDB_INCREMENT_STAT(ctdb, node.req_control);
362                 ctdb_request_control(ctdb, hdr);
363                 break;
364
365         case CTDB_REPLY_CONTROL:
366                 CTDB_INCREMENT_STAT(ctdb, node.reply_control);
367                 ctdb_reply_control(ctdb, hdr);
368                 break;
369
370         case CTDB_REQ_KEEPALIVE:
371                 CTDB_INCREMENT_STAT(ctdb, keepalive_packets_recv);
372                 break;
373
374         default:
375                 DEBUG(DEBUG_CRIT,("%s: Packet with unknown operation %u\n", 
376                          __location__, hdr->operation));
377                 break;
378         }
379
380 done:
381         talloc_free(tmp_ctx);
382 }
383
384
385 /*
386   called by the transport layer when a node is dead
387 */
388 void ctdb_node_dead(struct ctdb_node *node)
389 {
390         if (node->flags & NODE_FLAGS_DISCONNECTED) {
391                 DEBUG(DEBUG_INFO,("%s: node %s is already marked disconnected: %u connected\n", 
392                          node->ctdb->name, node->name, 
393                          node->ctdb->num_connected));
394                 return;
395         }
396         node->ctdb->num_connected--;
397         node->flags |= NODE_FLAGS_DISCONNECTED | NODE_FLAGS_UNHEALTHY;
398         node->rx_cnt = 0;
399         node->dead_count = 0;
400
401         DEBUG(DEBUG_NOTICE,("%s: node %s is dead: %u connected\n", 
402                  node->ctdb->name, node->name, node->ctdb->num_connected));
403         ctdb_daemon_cancel_controls(node->ctdb, node);
404
405         if (node->ctdb->methods == NULL) {
406                 DEBUG(DEBUG_ERR,(__location__ " Can not restart transport while shutting down daemon.\n"));
407                 return;
408         }
409
410         node->ctdb->methods->restart(node);
411 }
412
413 /*
414   called by the transport layer when a node is connected
415 */
416 void ctdb_node_connected(struct ctdb_node *node)
417 {
418         if (!(node->flags & NODE_FLAGS_DISCONNECTED)) {
419                 DEBUG(DEBUG_INFO,("%s: node %s is already marked connected: %u connected\n", 
420                          node->ctdb->name, node->name, 
421                          node->ctdb->num_connected));
422                 return;
423         }
424         node->ctdb->num_connected++;
425         node->dead_count = 0;
426         node->flags &= ~NODE_FLAGS_DISCONNECTED;
427         node->flags |= NODE_FLAGS_UNHEALTHY;
428         DEBUG(DEBUG_NOTICE,
429               ("%s: connected to %s - %u connected\n", 
430                node->ctdb->name, node->name, node->ctdb->num_connected));
431 }
432
433 struct queue_next {
434         struct ctdb_context *ctdb;
435         struct ctdb_req_header *hdr;
436 };
437
438
439 /*
440   triggered when a deferred packet is due
441  */
442 static void queue_next_trigger(struct event_context *ev, struct timed_event *te, 
443                                struct timeval t, void *private_data)
444 {
445         struct queue_next *q = talloc_get_type(private_data, struct queue_next);
446         ctdb_input_pkt(q->ctdb, q->hdr);
447         talloc_free(q);
448 }       
449
450 /*
451   defer a packet, so it is processed on the next event loop
452   this is used for sending packets to ourselves
453  */
454 static void ctdb_defer_packet(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
455 {
456         struct queue_next *q;
457         q = talloc(ctdb, struct queue_next);
458         if (q == NULL) {
459                 DEBUG(DEBUG_ERR,(__location__ " Failed to allocate deferred packet\n"));
460                 return;
461         }
462         q->ctdb = ctdb;
463         q->hdr = talloc_memdup(ctdb, hdr, hdr->length);
464         if (q->hdr == NULL) {
465                 DEBUG(DEBUG_ERR,("Error copying deferred packet to self\n"));
466                 return;
467         }
468 #if 0
469         /* use this to put packets directly into our recv function */
470         ctdb_input_pkt(q->ctdb, q->hdr);
471 #else
472         event_add_timed(ctdb->ev, q, timeval_zero(), queue_next_trigger, q);
473 #endif
474 }
475
476
477 /*
478   broadcast a packet to all nodes
479 */
480 static void ctdb_broadcast_packet_all(struct ctdb_context *ctdb, 
481                                       struct ctdb_req_header *hdr)
482 {
483         int i;
484         for (i=0; i < ctdb->num_nodes; i++) {
485                 if (ctdb->nodes[i]->flags & NODE_FLAGS_DELETED) {
486                         continue;
487                 }
488                 hdr->destnode = ctdb->nodes[i]->pnn;
489                 ctdb_queue_packet(ctdb, hdr);
490         }
491 }
492
493 /*
494   broadcast a packet to all nodes in the current vnnmap
495 */
496 static void ctdb_broadcast_packet_vnnmap(struct ctdb_context *ctdb, 
497                                          struct ctdb_req_header *hdr)
498 {
499         int i;
500         for (i=0;i<ctdb->vnn_map->size;i++) {
501                 hdr->destnode = ctdb->vnn_map->map[i];
502                 ctdb_queue_packet(ctdb, hdr);
503         }
504 }
505
506 /*
507   broadcast a packet to all connected nodes
508 */
509 static void ctdb_broadcast_packet_connected(struct ctdb_context *ctdb, 
510                                             struct ctdb_req_header *hdr)
511 {
512         int i;
513         for (i=0; i < ctdb->num_nodes; i++) {
514                 if (ctdb->nodes[i]->flags & NODE_FLAGS_DELETED) {
515                         continue;
516                 }
517                 if (!(ctdb->nodes[i]->flags & NODE_FLAGS_DISCONNECTED)) {
518                         hdr->destnode = ctdb->nodes[i]->pnn;
519                         ctdb_queue_packet(ctdb, hdr);
520                 }
521         }
522 }
523
524 /*
525   queue a packet or die
526 */
527 void ctdb_queue_packet(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
528 {
529         struct ctdb_node *node;
530
531         switch (hdr->destnode) {
532         case CTDB_BROADCAST_ALL:
533                 ctdb_broadcast_packet_all(ctdb, hdr);
534                 return;
535         case CTDB_BROADCAST_VNNMAP:
536                 ctdb_broadcast_packet_vnnmap(ctdb, hdr);
537                 return;
538         case CTDB_BROADCAST_CONNECTED:
539                 ctdb_broadcast_packet_connected(ctdb, hdr);
540                 return;
541         }
542
543         CTDB_INCREMENT_STAT(ctdb, node_packets_sent);
544
545         if (!ctdb_validate_pnn(ctdb, hdr->destnode)) {
546                 DEBUG(DEBUG_CRIT,(__location__ " cant send to node %u that does not exist\n", 
547                          hdr->destnode));
548                 return;
549         }
550
551         node = ctdb->nodes[hdr->destnode];
552
553         if (node->flags & NODE_FLAGS_DELETED) {
554                 DEBUG(DEBUG_ERR, (__location__ " Can not queue packet to DELETED node %d\n", hdr->destnode));
555                 return;
556         }
557
558         if (node->pnn == ctdb->pnn) {
559                 ctdb_defer_packet(ctdb, hdr);
560                 return;
561         }
562
563         if (ctdb->methods == NULL) {
564                 DEBUG(DEBUG_ALERT, (__location__ " Can not queue packet. "
565                                     "Transport is DOWN\n"));
566                 return;
567         }
568
569         node->tx_cnt++;
570         if (ctdb->methods->queue_pkt(node, (uint8_t *)hdr, hdr->length) != 0) {
571                 ctdb_fatal(ctdb, "Unable to queue packet\n");
572         }
573 }
574
575
576
577
578 /*
579   a valgrind hack to allow us to get opcode specific backtraces
580   very ugly, and relies on no compiler optimisation!
581 */
582 void ctdb_queue_packet_opcode(struct ctdb_context *ctdb, struct ctdb_req_header *hdr, unsigned opcode)
583 {
584         switch (opcode) {
585 #define DO_OP(x) case x: ctdb_queue_packet(ctdb, hdr); break
586                 DO_OP(1);
587                 DO_OP(2);
588                 DO_OP(3);
589                 DO_OP(4);
590                 DO_OP(5);
591                 DO_OP(6);
592                 DO_OP(7);
593                 DO_OP(8);
594                 DO_OP(9);
595                 DO_OP(10);
596                 DO_OP(11);
597                 DO_OP(12);
598                 DO_OP(13);
599                 DO_OP(14);
600                 DO_OP(15);
601                 DO_OP(16);
602                 DO_OP(17);
603                 DO_OP(18);
604                 DO_OP(19);
605                 DO_OP(20);
606                 DO_OP(21);
607                 DO_OP(22);
608                 DO_OP(23);
609                 DO_OP(24);
610                 DO_OP(25);
611                 DO_OP(26);
612                 DO_OP(27);
613                 DO_OP(28);
614                 DO_OP(29);
615                 DO_OP(30);
616                 DO_OP(31);
617                 DO_OP(32);
618                 DO_OP(33);
619                 DO_OP(34);
620                 DO_OP(35);
621                 DO_OP(36);
622                 DO_OP(37);
623                 DO_OP(38);
624                 DO_OP(39);
625                 DO_OP(40);
626                 DO_OP(41);
627                 DO_OP(42);
628                 DO_OP(43);
629                 DO_OP(44);
630                 DO_OP(45);
631                 DO_OP(46);
632                 DO_OP(47);
633                 DO_OP(48);
634                 DO_OP(49);
635                 DO_OP(50);
636                 DO_OP(51);
637                 DO_OP(52);
638                 DO_OP(53);
639                 DO_OP(54);
640                 DO_OP(55);
641                 DO_OP(56);
642                 DO_OP(57);
643                 DO_OP(58);
644                 DO_OP(59);
645                 DO_OP(60);
646                 DO_OP(61);
647                 DO_OP(62);
648                 DO_OP(63);
649                 DO_OP(64);
650                 DO_OP(65);
651                 DO_OP(66);
652                 DO_OP(67);
653                 DO_OP(68);
654                 DO_OP(69);
655                 DO_OP(70);
656                 DO_OP(71);
657                 DO_OP(72);
658                 DO_OP(73);
659                 DO_OP(74);
660                 DO_OP(75);
661                 DO_OP(76);
662                 DO_OP(77);
663                 DO_OP(78);
664                 DO_OP(79);
665                 DO_OP(80);
666                 DO_OP(81);
667                 DO_OP(82);
668                 DO_OP(83);
669                 DO_OP(84);
670                 DO_OP(85);
671                 DO_OP(86);
672                 DO_OP(87);
673                 DO_OP(88);
674                 DO_OP(89);
675                 DO_OP(90);
676                 DO_OP(91);
677                 DO_OP(92);
678                 DO_OP(93);
679                 DO_OP(94);
680                 DO_OP(95);
681                 DO_OP(96);
682                 DO_OP(97);
683                 DO_OP(98);
684                 DO_OP(99);
685                 DO_OP(100);
686         default: 
687                 ctdb_queue_packet(ctdb, hdr);
688                 break;
689         }
690 }