->monitor_context is NULL when monitoring is disabled.
[sahlberg/ctdb.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         /* 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                 if (node->pnn == ctdb->pnn) {
40                         continue;
41                 }
42                 
43                 if (node->flags & NODE_FLAGS_DISCONNECTED) {
44                         /* it might have come alive again */
45                         if (node->rx_cnt != 0) {
46                                 ctdb_node_connected(node);
47                         }
48                         continue;
49                 }
50
51
52                 if (node->rx_cnt == 0) {
53                         node->dead_count++;
54                 } else {
55                         node->dead_count = 0;
56                 }
57
58                 node->rx_cnt = 0;
59
60                 if (node->dead_count >= ctdb->tunable.keepalive_limit) {
61                         DEBUG(0,("dead count reached for node %u\n", node->pnn));
62                         ctdb_node_dead(node);
63                         ctdb_send_keepalive(ctdb, node->pnn);
64                         /* maybe tell the transport layer to kill the
65                            sockets as well?
66                         */
67                         continue;
68                 }
69                 
70                 if (node->tx_cnt == 0) {
71                         DEBUG(5,("sending keepalive to %u\n", node->pnn));
72                         ctdb_send_keepalive(ctdb, node->pnn);
73                 }
74
75                 node->tx_cnt = 0;
76         }
77         
78         event_add_timed(ctdb->ev, ctdb->monitor_context, 
79                         timeval_current_ofs(ctdb->tunable.keepalive_interval, 0), 
80                         ctdb_check_for_dead_nodes, ctdb);
81 }
82
83 static void ctdb_check_health(struct event_context *ev, struct timed_event *te, 
84                               struct timeval t, void *private_data);
85
86 /*
87   called when a health monitoring event script finishes
88  */
89 static void ctdb_health_callback(struct ctdb_context *ctdb, int status, void *p)
90 {
91         struct ctdb_node *node = ctdb->nodes[ctdb->pnn];
92         TDB_DATA data;
93         struct ctdb_node_flag_change c;
94         uint32_t next_interval;
95
96         c.pnn = ctdb->pnn;
97         c.old_flags = node->flags;
98
99         if (status != 0 && !(node->flags & NODE_FLAGS_UNHEALTHY)) {
100                 DEBUG(0,("monitor event failed - disabling node\n"));
101                 node->flags |= NODE_FLAGS_UNHEALTHY;
102         } else if (status == 0 && (node->flags & NODE_FLAGS_UNHEALTHY)) {
103                 DEBUG(0,("monitor event OK - node re-enabled\n"));
104                 node->flags &= ~NODE_FLAGS_UNHEALTHY;
105         }
106
107         if (node->flags & NODE_FLAGS_UNHEALTHY) {
108                 next_interval = ctdb->tunable.monitor_retry;
109         } else {
110                 next_interval = ctdb->tunable.monitor_interval;
111         }
112
113         if (ctdb->monitor_context == NULL) {
114                 DEBUG(0,(__location__ " monitoring was disabled while running"
115                         " healthcheck. Health checks postphoned until"
116                         " monitoring is re-enabled.\n"));
117         } else {
118                 event_add_timed(ctdb->ev, ctdb->monitor_context, 
119                                 timeval_current_ofs(next_interval, 0), 
120                                 ctdb_check_health, ctdb);
121         }
122
123         if (c.old_flags == node->flags) {
124                 return;
125         }
126
127         c.new_flags = node->flags;
128
129         data.dptr = (uint8_t *)&c;
130         data.dsize = sizeof(c);
131
132         /* tell the other nodes that something has changed */
133         ctdb_daemon_send_message(ctdb, CTDB_BROADCAST_CONNECTED,
134                                  CTDB_SRVID_NODE_FLAGS_CHANGED, data);
135
136 }
137
138
139 /*
140   called when the startup event script finishes
141  */
142 static void ctdb_startup_callback(struct ctdb_context *ctdb, int status, void *p)
143 {
144         if (status != 0) {
145                 DEBUG(0,("startup event failed\n"));
146         } else if (status == 0) {
147                 DEBUG(0,("startup event OK - enabling monitoring\n"));
148                 ctdb->done_startup = true;
149         }
150
151         if (ctdb->done_startup) {
152                 if (ctdb->monitor_context == NULL) {
153                         DEBUG(0,(__location__ " monitoring was disabled while "
154                                 "running startup event. "
155                                 "startup event postphoned until "
156                                 "monitoring is re-enabled.\n"));
157                 } else {
158                         event_add_timed(ctdb->ev, ctdb->monitor_context, 
159                                         timeval_zero(),
160                                         ctdb_check_health, ctdb);
161                 }
162         } else {
163                 if (ctdb->monitor_context == NULL) {
164                         DEBUG(0,(__location__ " monitoring was disabled while "
165                                 "running startup event. "
166                                 "Health cheack postphoned until "
167                                 "monitoring is re-enabled.\n"));
168                 } else {
169                         event_add_timed(ctdb->ev, ctdb->monitor_context, 
170                                         timeval_current_ofs(ctdb->tunable.monitor_interval, 0),
171                                         ctdb_check_health, ctdb);
172                 }
173         }
174
175 }
176
177
178 /*
179   see if the event scripts think we are healthy
180  */
181 static void ctdb_check_health(struct event_context *ev, struct timed_event *te, 
182                               struct timeval t, void *private_data)
183 {
184         struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
185         int ret;
186
187         if (ctdb->recovery_mode != CTDB_RECOVERY_NORMAL ||
188             (ctdb->monitoring_mode == CTDB_MONITORING_DISABLED && ctdb->done_startup)) {
189                 event_add_timed(ctdb->ev, ctdb->monitor_context,
190                                 timeval_current_ofs(ctdb->tunable.monitor_interval, 0), 
191                                 ctdb_check_health, ctdb);
192                 return;
193         }
194         
195         if (!ctdb->done_startup) {
196                 ret = ctdb_event_script_callback(ctdb, 
197                                                  timeval_current_ofs(ctdb->tunable.script_timeout, 0),
198                                                  ctdb->monitor_context, ctdb_startup_callback, 
199                                                  ctdb, "startup");
200         } else {
201                 ret = ctdb_event_script_callback(ctdb, 
202                                                  timeval_current_ofs(ctdb->tunable.script_timeout, 0),
203                                                  ctdb->monitor_context, ctdb_health_callback, 
204                                                  ctdb, "monitor");
205         }
206
207         if (ret != 0) {
208                 DEBUG(0,("Unable to launch monitor event script\n"));
209                 event_add_timed(ctdb->ev, ctdb->monitor_context, 
210                                 timeval_current_ofs(ctdb->tunable.monitor_retry, 0), 
211                                 ctdb_check_health, ctdb);
212         }       
213 }
214
215 /* stop any monitoring */
216 void ctdb_stop_monitoring(struct ctdb_context *ctdb)
217 {
218         talloc_free(ctdb->monitor_context);
219         ctdb->monitor_context = NULL;
220
221         ctdb->monitoring_mode  = CTDB_MONITORING_DISABLED;
222         DEBUG(0,("Monitoring has been stopped\n"));
223 }
224
225 /*
226   start watching for nodes that might be dead
227  */
228 void ctdb_start_monitoring(struct ctdb_context *ctdb)
229 {
230         struct timed_event *te;
231
232         if (ctdb->monitoring_mode == CTDB_MONITORING_ACTIVE) {
233                 return;
234         }
235
236         ctdb_stop_monitoring(ctdb);
237
238         ctdb->monitor_context = talloc_new(ctdb);
239         CTDB_NO_MEMORY_FATAL(ctdb, ctdb->monitor_context);
240
241         te = event_add_timed(ctdb->ev, ctdb->monitor_context,
242                              timeval_current_ofs(ctdb->tunable.keepalive_interval, 0), 
243                              ctdb_check_for_dead_nodes, ctdb);
244         CTDB_NO_MEMORY_FATAL(ctdb, te);
245
246         te = event_add_timed(ctdb->ev, ctdb->monitor_context,
247                              timeval_current_ofs(ctdb->tunable.monitor_retry, 0), 
248                              ctdb_check_health, ctdb);
249         CTDB_NO_MEMORY_FATAL(ctdb, te);
250
251         ctdb->monitoring_mode  = CTDB_MONITORING_ACTIVE;
252         DEBUG(0,("Monitoring has been started\n"));
253 }
254
255
256 /*
257   modify flags on a node
258  */
259 int32_t ctdb_control_modflags(struct ctdb_context *ctdb, TDB_DATA indata)
260 {
261         struct ctdb_node_modflags *m = (struct ctdb_node_modflags *)indata.dptr;
262         TDB_DATA data;
263         struct ctdb_node_flag_change c;
264         struct ctdb_node *node = ctdb->nodes[ctdb->pnn];
265         uint32_t old_flags = node->flags;
266
267         node->flags |= m->set;
268         node->flags &= ~m->clear;
269
270         if (node->flags == old_flags) {
271                 DEBUG(0, ("Control modflags on node %u - Unchanged - flags 0x%x\n", ctdb->pnn, node->flags));
272                 return 0;
273         }
274
275         DEBUG(0, ("Control modflags on node %u - flags now 0x%x\n", ctdb->pnn, node->flags));
276
277         /* if we have been banned, go into recovery mode */
278         c.pnn = ctdb->pnn;
279         c.old_flags = old_flags;
280         c.new_flags = node->flags;
281
282         data.dptr = (uint8_t *)&c;
283         data.dsize = sizeof(c);
284
285         /* tell the other nodes that something has changed */
286         ctdb_daemon_send_message(ctdb, CTDB_BROADCAST_CONNECTED,
287                                  CTDB_SRVID_NODE_FLAGS_CHANGED, data);
288
289         if ((node->flags & NODE_FLAGS_BANNED) && !(old_flags & NODE_FLAGS_BANNED)) {
290                 /* make sure we are frozen */
291                 DEBUG(0,("This node has been banned - forcing freeze and recovery\n"));
292                 /* Reset the generation id to 1 to make us ignore any
293                    REQ/REPLY CALL/DMASTER someone sends to us.
294                    We are now banned so we shouldnt service database calls
295                    anymore.
296                 */
297                 ctdb->vnn_map->generation = INVALID_GENERATION;
298
299                 ctdb_start_freeze(ctdb);
300                 ctdb_release_all_ips(ctdb);
301                 ctdb->recovery_mode = CTDB_RECOVERY_ACTIVE;
302         }
303         
304         return 0;
305 }