merged patch from tridge
[metze/ctdb/wip.git] / server / ctdb_monitor.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 "lib/events/events.h"
23 #include "system/filesys.h"
24 #include "system/wait.h"
25 #include "../include/ctdb_private.h"
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         if (ctdb->monitoring_mode == CTDB_MONITORING_DISABLED) {
37                 event_add_timed(ctdb->ev, ctdb->monitor_context, 
38                         timeval_current_ofs(ctdb->tunable.keepalive_interval, 0), 
39                         ctdb_check_for_dead_nodes, ctdb);
40                 return;
41         }
42
43         /* send a keepalive to all other nodes, unless */
44         for (i=0;i<ctdb->num_nodes;i++) {
45                 struct ctdb_node *node = ctdb->nodes[i];
46                 if (node->pnn == ctdb->pnn) {
47                         continue;
48                 }
49                 
50                 if (node->flags & NODE_FLAGS_DISCONNECTED) {
51                         /* it might have come alive again */
52                         if (node->rx_cnt != 0) {
53                                 ctdb_node_connected(node);
54                         }
55                         continue;
56                 }
57
58
59                 if (node->rx_cnt == 0) {
60                         node->dead_count++;
61                 } else {
62                         node->dead_count = 0;
63                 }
64
65                 node->rx_cnt = 0;
66
67                 if (node->dead_count >= ctdb->tunable.keepalive_limit) {
68                         DEBUG(0,("dead count reached for node %u\n", node->pnn));
69                         ctdb_node_dead(node);
70                         ctdb_send_keepalive(ctdb, node->pnn);
71                         /* maybe tell the transport layer to kill the
72                            sockets as well?
73                         */
74                         continue;
75                 }
76                 
77                 if (node->tx_cnt == 0) {
78                         DEBUG(5,("sending keepalive to %u\n", node->pnn));
79                         ctdb_send_keepalive(ctdb, node->pnn);
80                 }
81
82                 node->tx_cnt = 0;
83         }
84         
85         event_add_timed(ctdb->ev, ctdb->monitor_context, 
86                         timeval_current_ofs(ctdb->tunable.keepalive_interval, 0), 
87                         ctdb_check_for_dead_nodes, ctdb);
88 }
89
90 static void ctdb_check_health(struct event_context *ev, struct timed_event *te, 
91                               struct timeval t, void *private_data);
92
93 /*
94   called when a health monitoring event script finishes
95  */
96 static void ctdb_health_callback(struct ctdb_context *ctdb, int status, void *p)
97 {
98         struct ctdb_node *node = ctdb->nodes[ctdb->pnn];
99         TDB_DATA data;
100         struct ctdb_node_flag_change c;
101
102         event_add_timed(ctdb->ev, ctdb->monitor_context, 
103                         timeval_current_ofs(ctdb->tunable.monitor_interval, 0), 
104                         ctdb_check_health, ctdb);
105
106         c.pnn = ctdb->pnn;
107         c.old_flags = node->flags;
108
109         if (status != 0 && !(node->flags & NODE_FLAGS_UNHEALTHY)) {
110                 DEBUG(0,("monitor event failed - disabling node\n"));
111                 node->flags |= NODE_FLAGS_UNHEALTHY;
112         } else if (status == 0 && (node->flags & NODE_FLAGS_UNHEALTHY)) {
113                 DEBUG(0,("monitor event OK - node re-enabled\n"));
114                 ctdb->nodes[ctdb->pnn]->flags &= ~NODE_FLAGS_UNHEALTHY;
115         } else {
116                 /* no change */
117                 return;
118         }
119
120         c.new_flags = node->flags;
121
122         data.dptr = (uint8_t *)&c;
123         data.dsize = sizeof(c);
124
125         /* tell the other nodes that something has changed */
126         ctdb_daemon_send_message(ctdb, CTDB_BROADCAST_CONNECTED,
127                                  CTDB_SRVID_NODE_FLAGS_CHANGED, data);
128
129 }
130
131
132 /*
133   see if the event scripts think we are healthy
134  */
135 static void ctdb_check_health(struct event_context *ev, struct timed_event *te, 
136                               struct timeval t, void *private_data)
137 {
138         struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
139         int ret;
140
141         if (ctdb->monitoring_mode == CTDB_MONITORING_DISABLED) {
142                 event_add_timed(ctdb->ev, ctdb->monitor_context,
143                                 timeval_current_ofs(ctdb->tunable.monitor_interval, 0), 
144                                 ctdb_check_health, ctdb);
145                 return;
146         }
147         
148         ret = ctdb_event_script_callback(ctdb, 
149                                          timeval_current_ofs(ctdb->tunable.script_timeout, 0),
150                                          ctdb->monitor_context, ctdb_health_callback, ctdb, "monitor");
151         if (ret != 0) {
152                 DEBUG(0,("Unable to launch monitor event script\n"));
153                 event_add_timed(ctdb->ev, ctdb->monitor_context, 
154                                 timeval_current_ofs(ctdb->tunable.monitor_interval, 0), 
155                                 ctdb_check_health, ctdb);
156         }       
157 }
158
159 /* stop any monitoring */
160 void ctdb_stop_monitoring(struct ctdb_context *ctdb)
161 {
162         talloc_free(ctdb->monitor_context);
163         ctdb->monitor_context = talloc_new(ctdb);
164         CTDB_NO_MEMORY_FATAL(ctdb, ctdb->monitor_context);
165 }
166
167 /*
168   start watching for nodes that might be dead
169  */
170 void ctdb_start_monitoring(struct ctdb_context *ctdb)
171 {
172         struct timed_event *te;
173
174         ctdb_stop_monitoring(ctdb);
175
176         te = event_add_timed(ctdb->ev, ctdb->monitor_context,
177                              timeval_current_ofs(ctdb->tunable.keepalive_interval, 0), 
178                              ctdb_check_for_dead_nodes, ctdb);
179         CTDB_NO_MEMORY_FATAL(ctdb, te);
180
181         te = event_add_timed(ctdb->ev, ctdb->monitor_context,
182                              timeval_current_ofs(ctdb->tunable.monitor_interval, 0), 
183                              ctdb_check_health, ctdb);
184         CTDB_NO_MEMORY_FATAL(ctdb, te);
185 }
186
187
188 /*
189   modify flags on a node
190  */
191 int32_t ctdb_control_modflags(struct ctdb_context *ctdb, TDB_DATA indata)
192 {
193         struct ctdb_node_modflags *m = (struct ctdb_node_modflags *)indata.dptr;
194         TDB_DATA data;
195         struct ctdb_node_flag_change c;
196         struct ctdb_node *node = ctdb->nodes[ctdb->pnn];
197         uint32_t old_flags = node->flags;
198
199         node->flags |= m->set;
200         node->flags &= ~m->clear;
201
202         if (node->flags == old_flags) {
203                 /* no change */
204                 return 0;
205         }
206
207         DEBUG(0, ("Control modflags on node %u - flags now 0x%x\n", ctdb->pnn, node->flags));
208
209         /* if we have been banned, go into recovery mode */
210         c.pnn = ctdb->pnn;
211         c.old_flags = old_flags;
212         c.new_flags = node->flags;
213
214         data.dptr = (uint8_t *)&c;
215         data.dsize = sizeof(c);
216
217         /* tell the other nodes that something has changed */
218         ctdb_daemon_send_message(ctdb, CTDB_BROADCAST_CONNECTED,
219                                  CTDB_SRVID_NODE_FLAGS_CHANGED, data);
220
221         if ((node->flags & NODE_FLAGS_BANNED) && !(old_flags & NODE_FLAGS_BANNED)) {
222                 /* make sure we are frozen */
223                 DEBUG(0,("This node has been banned - forcing freeze and recovery\n"));
224                 /* Reset the generation id to 1 to make us ignore any
225                    REQ/REPLY CALL/DMASTER someone sends to us.
226                    We are now banned so we shouldnt service database calls
227                    anymore.
228                 */
229                 ctdb->vnn_map->generation = INVALID_GENERATION;
230
231                 ctdb_start_freeze(ctdb);
232                 ctdb_release_all_ips(ctdb);
233                 ctdb->recovery_mode = CTDB_RECOVERY_ACTIVE;
234         }
235         
236         return 0;
237 }