ctdb-daemon: Separate prototypes for common client/server functions
[samba.git] / ctdb / 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 "replace.h"
22 #include "system/filesys.h"
23 #include "system/network.h"
24 #include "system/time.h"
25 #include "system/wait.h"
26
27 #include <talloc.h>
28 #include <tevent.h>
29
30 #include "lib/util/debug.h"
31 #include "lib/util/samba_util.h"
32
33 #include "ctdb_private.h"
34 #include "ctdb_logging.h"
35
36 #include "common/common.h"
37
38
39 /*
40   see if any nodes are dead
41  */
42 static void ctdb_check_for_dead_nodes(struct tevent_context *ev,
43                                       struct tevent_timer *te,
44                                       struct timeval t, void *private_data)
45 {
46         struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
47         int i;
48
49         /* send a keepalive to all other nodes, unless */
50         for (i=0;i<ctdb->num_nodes;i++) {
51                 struct ctdb_node *node = ctdb->nodes[i];
52
53                 if (node->flags & NODE_FLAGS_DELETED) {
54                         continue;
55                 }
56
57                 if (node->pnn == ctdb->pnn) {
58                         continue;
59                 }
60                 
61                 if (node->flags & NODE_FLAGS_DISCONNECTED) {
62                         /* it might have come alive again */
63                         if (node->rx_cnt != 0) {
64                                 ctdb_node_connected(node);
65                         }
66                         continue;
67                 }
68
69
70                 if (node->rx_cnt == 0) {
71                         node->dead_count++;
72                 } else {
73                         node->dead_count = 0;
74                 }
75
76                 node->rx_cnt = 0;
77
78                 if (node->dead_count >= ctdb->tunable.keepalive_limit) {
79                         DEBUG(DEBUG_NOTICE,("dead count reached for node %u\n", node->pnn));
80                         ctdb_node_dead(node);
81                         ctdb_send_keepalive(ctdb, node->pnn);
82                         /* maybe tell the transport layer to kill the
83                            sockets as well?
84                         */
85                         continue;
86                 }
87                 
88                 DEBUG(DEBUG_DEBUG,("sending keepalive to %u\n", node->pnn));
89                 ctdb_send_keepalive(ctdb, node->pnn);
90
91                 node->tx_cnt = 0;
92         }
93
94         tevent_add_timer(ctdb->ev, ctdb->keepalive_ctx,
95                          timeval_current_ofs(ctdb->tunable.keepalive_interval, 0),
96                          ctdb_check_for_dead_nodes, ctdb);
97 }
98
99
100 void ctdb_start_keepalive(struct ctdb_context *ctdb)
101 {
102         struct tevent_timer *te;
103
104         ctdb->keepalive_ctx = talloc_new(ctdb);
105         CTDB_NO_MEMORY_FATAL(ctdb, ctdb->keepalive_ctx);
106
107         te = tevent_add_timer(ctdb->ev, ctdb->keepalive_ctx,
108                               timeval_current_ofs(ctdb->tunable.keepalive_interval, 0),
109                               ctdb_check_for_dead_nodes, ctdb);
110         CTDB_NO_MEMORY_FATAL(ctdb, te);
111
112         DEBUG(DEBUG_NOTICE,("Keepalive monitoring has been started\n"));
113 }
114
115 void ctdb_stop_keepalive(struct ctdb_context *ctdb)
116 {
117         talloc_free(ctdb->keepalive_ctx);
118         ctdb->keepalive_ctx = NULL;
119 }
120