scripts: Add support for CTDB_DBDIR in tmpfs
[ctdb.git] / server / ctdb_keepalive.c
1 /* 
2    monitoring links to all other nodes to detect dead nodes
3
4
5    Copyright (C) Ronnie Sahlberg 2007
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 3 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, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "system/filesys.h"
23 #include "system/wait.h"
24 #include "../include/ctdb_private.h"
25
26
27 /*
28   see if any nodes are dead
29  */
30 static void ctdb_check_for_dead_nodes(struct event_context *ev, struct timed_event *te, 
31                                       struct timeval t, void *private_data)
32 {
33         struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
34         int i;
35
36         /* send a keepalive to all other nodes, unless */
37         for (i=0;i<ctdb->num_nodes;i++) {
38                 struct ctdb_node *node = ctdb->nodes[i];
39
40                 if (node->flags & NODE_FLAGS_DELETED) {
41                         continue;
42                 }
43
44                 if (node->pnn == ctdb->pnn) {
45                         continue;
46                 }
47                 
48                 if (node->flags & NODE_FLAGS_DISCONNECTED) {
49                         /* it might have come alive again */
50                         if (node->rx_cnt != 0) {
51                                 ctdb_node_connected(node);
52                         }
53                         continue;
54                 }
55
56
57                 if (node->rx_cnt == 0) {
58                         node->dead_count++;
59                 } else {
60                         node->dead_count = 0;
61                 }
62
63                 node->rx_cnt = 0;
64
65                 if (node->dead_count >= ctdb->tunable.keepalive_limit) {
66                         DEBUG(DEBUG_NOTICE,("dead count reached for node %u\n", node->pnn));
67                         ctdb_node_dead(node);
68                         ctdb_send_keepalive(ctdb, node->pnn);
69                         /* maybe tell the transport layer to kill the
70                            sockets as well?
71                         */
72                         continue;
73                 }
74                 
75                 DEBUG(DEBUG_DEBUG,("sending keepalive to %u\n", node->pnn));
76                 ctdb_send_keepalive(ctdb, node->pnn);
77
78                 node->tx_cnt = 0;
79         }
80         
81         event_add_timed(ctdb->ev, ctdb->keepalive_ctx,
82                         timeval_current_ofs(ctdb->tunable.keepalive_interval, 0), 
83                         ctdb_check_for_dead_nodes, ctdb);
84 }
85
86
87 void ctdb_start_keepalive(struct ctdb_context *ctdb)
88 {
89         struct timed_event *te;
90
91         ctdb->keepalive_ctx = talloc_new(ctdb);
92         CTDB_NO_MEMORY_FATAL(ctdb, ctdb->keepalive_ctx);
93
94         te = event_add_timed(ctdb->ev, ctdb->keepalive_ctx,
95                              timeval_current_ofs(ctdb->tunable.keepalive_interval, 0), 
96                              ctdb_check_for_dead_nodes, ctdb);
97         CTDB_NO_MEMORY_FATAL(ctdb, te);
98
99         DEBUG(DEBUG_NOTICE,("Keepalive monitoring has been started\n"));
100 }
101
102 void ctdb_stop_keepalive(struct ctdb_context *ctdb)
103 {
104         talloc_free(ctdb->keepalive_ctx);
105         ctdb->keepalive_ctx = NULL;
106 }
107