2 ctdb main protocol code
4 Copyright (C) Andrew Tridgell 2006
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.
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.
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/>.
22 #include "lib/util/dlinklist.h"
23 #include "system/network.h"
24 #include "system/filesys.h"
25 #include "../include/ctdb_private.h"
28 choose the transport we will use
30 int ctdb_set_transport(struct ctdb_context *ctdb, const char *transport)
32 ctdb->transport = talloc_strdup(ctdb, transport);
33 CTDB_NO_MEMORY(ctdb, ctdb->transport);
39 Check whether an ip is a valid node ip
40 Returns the node id for this ip address or -1
42 int ctdb_ip_to_nodeid(struct ctdb_context *ctdb, const char *nodeip)
46 for (nodeid=0;nodeid<ctdb->num_nodes;nodeid++) {
47 if (ctdb->nodes[nodeid]->flags & NODE_FLAGS_DELETED) {
50 if (!strcmp(ctdb->nodes[nodeid]->address.address, nodeip)) {
59 choose the recovery lock file
61 int ctdb_set_recovery_lock_file(struct ctdb_context *ctdb, const char *file)
63 if (ctdb->recovery_lock_file != NULL) {
64 talloc_free(ctdb->recovery_lock_file);
65 ctdb->recovery_lock_file = NULL;
69 DEBUG(DEBUG_ALERT,("Recovery lock file set to \"\". Disabling recovery lock checking\n"));
70 ctdb->tunable.verify_recovery_lock = 0;
74 ctdb->recovery_lock_file = talloc_strdup(ctdb, file);
75 CTDB_NO_MEMORY(ctdb, ctdb->recovery_lock_file);
81 set the directory for the local databases
83 int ctdb_set_tdb_dir(struct ctdb_context *ctdb, const char *dir)
85 ctdb->db_directory = talloc_strdup(ctdb, dir);
86 if (ctdb->db_directory == NULL) {
93 set the directory for the persistent databases
95 int ctdb_set_tdb_dir_persistent(struct ctdb_context *ctdb, const char *dir)
97 ctdb->db_directory_persistent = talloc_strdup(ctdb, dir);
98 if (ctdb->db_directory_persistent == NULL) {
105 set the directory for internal state databases
107 int ctdb_set_tdb_dir_state(struct ctdb_context *ctdb, const char *dir)
109 ctdb->db_directory_state = talloc_strdup(ctdb, dir);
110 if (ctdb->db_directory_state == NULL) {
117 add a node to the list of nodes
119 static int ctdb_add_node(struct ctdb_context *ctdb, char *nstr)
121 struct ctdb_node *node, **nodep;
123 nodep = talloc_realloc(ctdb, ctdb->nodes, struct ctdb_node *, ctdb->num_nodes+1);
124 CTDB_NO_MEMORY(ctdb, nodep);
127 nodep = &ctdb->nodes[ctdb->num_nodes];
128 (*nodep) = talloc_zero(ctdb->nodes, struct ctdb_node);
129 CTDB_NO_MEMORY(ctdb, *nodep);
132 if (ctdb_parse_address(ctdb, node, nstr, &node->address) != 0) {
136 node->name = talloc_asprintf(node, "%s:%u",
137 node->address.address,
139 /* this assumes that the nodes are kept in sorted order, and no gaps */
140 node->pnn = ctdb->num_nodes;
142 /* nodes start out disconnected and unhealthy */
143 node->flags = (NODE_FLAGS_DISCONNECTED | NODE_FLAGS_UNHEALTHY);
145 if (ctdb->address.address &&
146 ctdb_same_address(&ctdb->address, &node->address)) {
147 /* for automatic binding to interfaces, see tcp_connect.c */
148 ctdb->pnn = node->pnn;
152 node->dead_count = 0;
158 add an entry for a "deleted" node to the list of nodes.
159 a "deleted" node is a node that is commented out from the nodes file.
160 this is used to prevent that subsequent nodes in the nodes list
161 change their pnn value if a node is "delete" by commenting it out and then
162 using "ctdb reloadnodes" at runtime.
164 static int ctdb_add_deleted_node(struct ctdb_context *ctdb)
166 struct ctdb_node *node, **nodep;
168 nodep = talloc_realloc(ctdb, ctdb->nodes, struct ctdb_node *, ctdb->num_nodes+1);
169 CTDB_NO_MEMORY(ctdb, nodep);
172 nodep = &ctdb->nodes[ctdb->num_nodes];
173 (*nodep) = talloc_zero(ctdb->nodes, struct ctdb_node);
174 CTDB_NO_MEMORY(ctdb, *nodep);
177 if (ctdb_parse_address(ctdb, node, "0.0.0.0", &node->address) != 0) {
178 DEBUG(DEBUG_ERR,("Failed to setup deleted node %d\n", ctdb->num_nodes));
182 node->name = talloc_strdup(node, "0.0.0.0:0");
184 /* this assumes that the nodes are kept in sorted order, and no gaps */
185 node->pnn = ctdb->num_nodes;
187 /* this node is permanently deleted/disconnected */
188 node->flags = NODE_FLAGS_DELETED|NODE_FLAGS_DISCONNECTED;
191 node->dead_count = 0;
198 setup the node list from a file
200 int ctdb_set_nlist(struct ctdb_context *ctdb, const char *nlist)
204 int i, j, num_present;
206 talloc_free(ctdb->nodes);
210 lines = file_lines_load(nlist, &nlines, ctdb);
212 ctdb_set_error(ctdb, "Failed to load nlist '%s'\n", nlist);
215 while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
220 for (i=0; i < nlines; i++) {
224 /* strip leading spaces */
225 while((*node == ' ') || (*node == '\t')) {
229 if (ctdb_add_deleted_node(ctdb) != 0) {
235 if (strcmp(node, "") == 0) {
238 if (ctdb_add_node(ctdb, node) != 0) {
245 /* initialize the vnn mapping table now that we have the nodes list,
246 skipping any deleted nodes
248 ctdb->vnn_map = talloc(ctdb, struct ctdb_vnn_map);
249 CTDB_NO_MEMORY(ctdb, ctdb->vnn_map);
251 ctdb->vnn_map->generation = INVALID_GENERATION;
252 ctdb->vnn_map->size = num_present;
253 ctdb->vnn_map->map = talloc_array(ctdb->vnn_map, uint32_t, ctdb->vnn_map->size);
254 CTDB_NO_MEMORY(ctdb, ctdb->vnn_map->map);
256 for(i=0, j=0; i < ctdb->vnn_map->size; i++) {
257 if (ctdb->nodes[i]->flags & NODE_FLAGS_DELETED) {
260 ctdb->vnn_map->map[j] = i;
270 setup the local node address
272 int ctdb_set_address(struct ctdb_context *ctdb, const char *address)
274 if (ctdb_parse_address(ctdb, ctdb, address, &ctdb->address) != 0) {
278 ctdb->name = talloc_asprintf(ctdb, "%s:%u",
279 ctdb->address.address,
286 return the number of active nodes
288 uint32_t ctdb_get_num_active_nodes(struct ctdb_context *ctdb)
292 for (i=0; i < ctdb->num_nodes; i++) {
293 if (!(ctdb->nodes[i]->flags & NODE_FLAGS_INACTIVE)) {
302 called when we need to process a packet. This can be a requeued packet
303 after a lockwait, or a real packet from another node
305 void ctdb_input_pkt(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
309 /* place the packet as a child of the tmp_ctx. We then use
310 talloc_free() below to free it. If any of the calls want
311 to keep it, then they will steal it somewhere else, and the
312 talloc_free() will only free the tmp_ctx */
313 tmp_ctx = talloc_new(ctdb);
314 talloc_steal(tmp_ctx, hdr);
316 DEBUG(DEBUG_DEBUG,(__location__ " ctdb request %u of type %u length %u from "
317 "node %u to %u\n", hdr->reqid, hdr->operation, hdr->length,
318 hdr->srcnode, hdr->destnode));
320 switch (hdr->operation) {
322 case CTDB_REPLY_CALL:
323 case CTDB_REQ_DMASTER:
324 case CTDB_REPLY_DMASTER:
325 /* we dont allow these calls when banned */
326 if (ctdb->nodes[ctdb->pnn]->flags & NODE_FLAGS_BANNED) {
327 DEBUG(DEBUG_DEBUG,(__location__ " ctdb operation %u"
329 " length %u from node %u to %u while node"
331 hdr->operation, hdr->reqid,
333 hdr->srcnode, hdr->destnode));
337 /* for ctdb_call inter-node operations verify that the
338 remote node that sent us the call is running in the
339 same generation instance as this node
341 if (ctdb->vnn_map->generation != hdr->generation) {
342 DEBUG(DEBUG_DEBUG,(__location__ " ctdb operation %u"
344 " length %u from node %u to %u had an"
345 " invalid generation id:%u while our"
346 " generation id is:%u\n",
347 hdr->operation, hdr->reqid,
349 hdr->srcnode, hdr->destnode,
350 hdr->generation, ctdb->vnn_map->generation));
355 switch (hdr->operation) {
357 CTDB_INCREMENT_STAT(ctdb, node.req_call);
358 ctdb_request_call(ctdb, hdr);
361 case CTDB_REPLY_CALL:
362 CTDB_INCREMENT_STAT(ctdb, node.reply_call);
363 ctdb_reply_call(ctdb, hdr);
366 case CTDB_REPLY_ERROR:
367 CTDB_INCREMENT_STAT(ctdb, node.reply_error);
368 ctdb_reply_error(ctdb, hdr);
371 case CTDB_REQ_DMASTER:
372 CTDB_INCREMENT_STAT(ctdb, node.req_dmaster);
373 ctdb_request_dmaster(ctdb, hdr);
376 case CTDB_REPLY_DMASTER:
377 CTDB_INCREMENT_STAT(ctdb, node.reply_dmaster);
378 ctdb_reply_dmaster(ctdb, hdr);
381 case CTDB_REQ_MESSAGE:
382 CTDB_INCREMENT_STAT(ctdb, node.req_message);
383 ctdb_request_message(ctdb, hdr);
386 case CTDB_REQ_CONTROL:
387 CTDB_INCREMENT_STAT(ctdb, node.req_control);
388 ctdb_request_control(ctdb, hdr);
391 case CTDB_REPLY_CONTROL:
392 CTDB_INCREMENT_STAT(ctdb, node.reply_control);
393 ctdb_reply_control(ctdb, hdr);
396 case CTDB_REQ_KEEPALIVE:
397 CTDB_INCREMENT_STAT(ctdb, keepalive_packets_recv);
401 DEBUG(DEBUG_CRIT,("%s: Packet with unknown operation %u\n",
402 __location__, hdr->operation));
407 talloc_free(tmp_ctx);
412 called by the transport layer when a node is dead
414 void ctdb_node_dead(struct ctdb_node *node)
416 if (node->flags & NODE_FLAGS_DISCONNECTED) {
417 DEBUG(DEBUG_INFO,("%s: node %s is already marked disconnected: %u connected\n",
418 node->ctdb->name, node->name,
419 node->ctdb->num_connected));
422 node->ctdb->num_connected--;
423 node->flags |= NODE_FLAGS_DISCONNECTED | NODE_FLAGS_UNHEALTHY;
425 node->dead_count = 0;
427 DEBUG(DEBUG_NOTICE,("%s: node %s is dead: %u connected\n",
428 node->ctdb->name, node->name, node->ctdb->num_connected));
429 ctdb_daemon_cancel_controls(node->ctdb, node);
431 if (node->ctdb->methods == NULL) {
432 DEBUG(DEBUG_ERR,(__location__ " Can not restart transport while shutting down daemon.\n"));
436 node->ctdb->methods->restart(node);
440 called by the transport layer when a node is connected
442 void ctdb_node_connected(struct ctdb_node *node)
444 if (!(node->flags & NODE_FLAGS_DISCONNECTED)) {
445 DEBUG(DEBUG_INFO,("%s: node %s is already marked connected: %u connected\n",
446 node->ctdb->name, node->name,
447 node->ctdb->num_connected));
450 node->ctdb->num_connected++;
451 node->dead_count = 0;
452 node->flags &= ~NODE_FLAGS_DISCONNECTED;
453 node->flags |= NODE_FLAGS_UNHEALTHY;
454 DEBUG(DEBUG_INFO,("%s: connected to %s - %u connected\n",
455 node->ctdb->name, node->name, node->ctdb->num_connected));
459 struct ctdb_context *ctdb;
460 struct ctdb_req_header *hdr;
465 triggered when a deferred packet is due
467 static void queue_next_trigger(struct event_context *ev, struct timed_event *te,
468 struct timeval t, void *private_data)
470 struct queue_next *q = talloc_get_type(private_data, struct queue_next);
471 ctdb_input_pkt(q->ctdb, q->hdr);
476 defer a packet, so it is processed on the next event loop
477 this is used for sending packets to ourselves
479 static void ctdb_defer_packet(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
481 struct queue_next *q;
482 q = talloc(ctdb, struct queue_next);
484 DEBUG(DEBUG_ERR,(__location__ " Failed to allocate deferred packet\n"));
488 q->hdr = talloc_memdup(ctdb, hdr, hdr->length);
489 if (q->hdr == NULL) {
490 DEBUG(DEBUG_ERR,("Error copying deferred packet to self\n"));
494 /* use this to put packets directly into our recv function */
495 ctdb_input_pkt(q->ctdb, q->hdr);
497 event_add_timed(ctdb->ev, q, timeval_zero(), queue_next_trigger, q);
503 broadcast a packet to all nodes
505 static void ctdb_broadcast_packet_all(struct ctdb_context *ctdb,
506 struct ctdb_req_header *hdr)
509 for (i=0; i < ctdb->num_nodes; i++) {
510 if (ctdb->nodes[i]->flags & NODE_FLAGS_DELETED) {
513 hdr->destnode = ctdb->nodes[i]->pnn;
514 ctdb_queue_packet(ctdb, hdr);
519 broadcast a packet to all nodes in the current vnnmap
521 static void ctdb_broadcast_packet_vnnmap(struct ctdb_context *ctdb,
522 struct ctdb_req_header *hdr)
525 for (i=0;i<ctdb->vnn_map->size;i++) {
526 hdr->destnode = ctdb->vnn_map->map[i];
527 ctdb_queue_packet(ctdb, hdr);
532 broadcast a packet to all connected nodes
534 static void ctdb_broadcast_packet_connected(struct ctdb_context *ctdb,
535 struct ctdb_req_header *hdr)
538 for (i=0; i < ctdb->num_nodes; i++) {
539 if (ctdb->nodes[i]->flags & NODE_FLAGS_DELETED) {
542 if (!(ctdb->nodes[i]->flags & NODE_FLAGS_DISCONNECTED)) {
543 hdr->destnode = ctdb->nodes[i]->pnn;
544 ctdb_queue_packet(ctdb, hdr);
550 queue a packet or die
552 void ctdb_queue_packet(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
554 struct ctdb_node *node;
556 switch (hdr->destnode) {
557 case CTDB_BROADCAST_ALL:
558 ctdb_broadcast_packet_all(ctdb, hdr);
560 case CTDB_BROADCAST_VNNMAP:
561 ctdb_broadcast_packet_vnnmap(ctdb, hdr);
563 case CTDB_BROADCAST_CONNECTED:
564 ctdb_broadcast_packet_connected(ctdb, hdr);
568 CTDB_INCREMENT_STAT(ctdb, node_packets_sent);
570 if (!ctdb_validate_pnn(ctdb, hdr->destnode)) {
571 DEBUG(DEBUG_CRIT,(__location__ " cant send to node %u that does not exist\n",
576 node = ctdb->nodes[hdr->destnode];
578 if (node->flags & NODE_FLAGS_DELETED) {
579 DEBUG(DEBUG_ERR, (__location__ " Can not queue packet to DELETED node %d\n", hdr->destnode));
583 if (node->pnn == ctdb->pnn) {
584 ctdb_defer_packet(ctdb, hdr);
588 if (ctdb->methods == NULL) {
589 DEBUG(DEBUG_ALERT, (__location__ " Can not queue packet. "
590 "Transport is DOWN\n"));
595 if (ctdb->methods->queue_pkt(node, (uint8_t *)hdr, hdr->length) != 0) {
596 ctdb_fatal(ctdb, "Unable to queue packet\n");
604 a valgrind hack to allow us to get opcode specific backtraces
605 very ugly, and relies on no compiler optimisation!
607 void ctdb_queue_packet_opcode(struct ctdb_context *ctdb, struct ctdb_req_header *hdr, unsigned opcode)
610 #define DO_OP(x) case x: ctdb_queue_packet(ctdb, hdr); break
712 ctdb_queue_packet(ctdb, hdr);