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