2 monitoring links to all other nodes to detect dead nodes
5 Copyright (C) Ronnie Sahlberg 2007
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.
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.
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/>.
22 #include "lib/events/events.h"
23 #include "system/filesys.h"
24 #include "system/wait.h"
25 #include "../include/ctdb_private.h"
28 see if any nodes are dead
30 static void ctdb_check_for_dead_nodes(struct event_context *ev, struct timed_event *te,
31 struct timeval t, void *private_data)
33 struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
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) {
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);
52 if (node->rx_cnt == 0) {
60 if (node->dead_count >= ctdb->tunable.keepalive_limit) {
61 DEBUG(0,("dead count reached for node %u\n", node->pnn));
63 ctdb_send_keepalive(ctdb, node->pnn);
64 /* maybe tell the transport layer to kill the
70 if (node->tx_cnt == 0) {
71 DEBUG(5,("sending keepalive to %u\n", node->pnn));
72 ctdb_send_keepalive(ctdb, node->pnn);
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);
83 static void ctdb_check_health(struct event_context *ev, struct timed_event *te,
84 struct timeval t, void *private_data);
87 called when a health monitoring event script finishes
89 static void ctdb_health_callback(struct ctdb_context *ctdb, int status, void *p)
91 struct ctdb_node *node = ctdb->nodes[ctdb->pnn];
93 struct ctdb_node_flag_change c;
94 uint32_t next_interval;
97 c.old_flags = node->flags;
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;
107 if (node->flags & NODE_FLAGS_UNHEALTHY) {
108 next_interval = ctdb->tunable.monitor_retry;
110 next_interval = ctdb->tunable.monitor_interval;
113 event_add_timed(ctdb->ev, ctdb->monitor_context,
114 timeval_current_ofs(next_interval, 0),
115 ctdb_check_health, ctdb);
117 if (c.old_flags == node->flags) {
121 c.new_flags = node->flags;
123 data.dptr = (uint8_t *)&c;
124 data.dsize = sizeof(c);
126 /* tell the other nodes that something has changed */
127 ctdb_daemon_send_message(ctdb, CTDB_BROADCAST_CONNECTED,
128 CTDB_SRVID_NODE_FLAGS_CHANGED, data);
134 called when the startup event script finishes
136 static void ctdb_startup_callback(struct ctdb_context *ctdb, int status, void *p)
139 DEBUG(0,("startup event failed\n"));
140 } else if (status == 0) {
141 DEBUG(0,("startup event OK - enabling monitoring\n"));
142 ctdb->done_startup = true;
145 if (ctdb->done_startup) {
146 event_add_timed(ctdb->ev, ctdb->monitor_context,
148 ctdb_check_health, ctdb);
150 event_add_timed(ctdb->ev, ctdb->monitor_context,
151 timeval_current_ofs(ctdb->tunable.monitor_interval, 0),
152 ctdb_check_health, ctdb);
159 see if the event scripts think we are healthy
161 static void ctdb_check_health(struct event_context *ev, struct timed_event *te,
162 struct timeval t, void *private_data)
164 struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
167 if (ctdb->recovery_mode != CTDB_RECOVERY_NORMAL ||
168 (ctdb->monitoring_mode == CTDB_MONITORING_DISABLED && ctdb->done_startup)) {
169 event_add_timed(ctdb->ev, ctdb->monitor_context,
170 timeval_current_ofs(ctdb->tunable.monitor_interval, 0),
171 ctdb_check_health, ctdb);
175 if (!ctdb->done_startup) {
176 ret = ctdb_event_script_callback(ctdb,
177 timeval_current_ofs(ctdb->tunable.script_timeout, 0),
178 ctdb->monitor_context, ctdb_startup_callback,
181 ret = ctdb_event_script_callback(ctdb,
182 timeval_current_ofs(ctdb->tunable.script_timeout, 0),
183 ctdb->monitor_context, ctdb_health_callback,
188 DEBUG(0,("Unable to launch monitor event script\n"));
189 event_add_timed(ctdb->ev, ctdb->monitor_context,
190 timeval_current_ofs(ctdb->tunable.monitor_retry, 0),
191 ctdb_check_health, ctdb);
195 /* stop any monitoring */
196 void ctdb_stop_monitoring(struct ctdb_context *ctdb)
198 talloc_free(ctdb->monitor_context);
199 ctdb->monitor_context = NULL;
201 ctdb->monitoring_mode = CTDB_MONITORING_DISABLED;
202 DEBUG(0,("Monitoring has been stopped\n"));
206 start watching for nodes that might be dead
208 void ctdb_start_monitoring(struct ctdb_context *ctdb)
210 struct timed_event *te;
212 if (ctdb->monitoring_mode == CTDB_MONITORING_ACTIVE) {
216 ctdb_stop_monitoring(ctdb);
218 ctdb->monitor_context = talloc_new(ctdb);
219 CTDB_NO_MEMORY_FATAL(ctdb, ctdb->monitor_context);
221 te = event_add_timed(ctdb->ev, ctdb->monitor_context,
222 timeval_current_ofs(ctdb->tunable.keepalive_interval, 0),
223 ctdb_check_for_dead_nodes, ctdb);
224 CTDB_NO_MEMORY_FATAL(ctdb, te);
226 te = event_add_timed(ctdb->ev, ctdb->monitor_context,
227 timeval_current_ofs(ctdb->tunable.monitor_retry, 0),
228 ctdb_check_health, ctdb);
229 CTDB_NO_MEMORY_FATAL(ctdb, te);
231 ctdb->monitoring_mode = CTDB_MONITORING_ACTIVE;
232 DEBUG(0,("Monitoring has been started\n"));
237 modify flags on a node
239 int32_t ctdb_control_modflags(struct ctdb_context *ctdb, TDB_DATA indata)
241 struct ctdb_node_modflags *m = (struct ctdb_node_modflags *)indata.dptr;
243 struct ctdb_node_flag_change c;
244 struct ctdb_node *node = ctdb->nodes[ctdb->pnn];
245 uint32_t old_flags = node->flags;
247 node->flags |= m->set;
248 node->flags &= ~m->clear;
250 if (node->flags == old_flags) {
251 DEBUG(0, ("Control modflags on node %u - Unchanged - flags 0x%x\n", ctdb->pnn, node->flags));
255 DEBUG(0, ("Control modflags on node %u - flags now 0x%x\n", ctdb->pnn, node->flags));
257 /* if we have been banned, go into recovery mode */
259 c.old_flags = old_flags;
260 c.new_flags = node->flags;
262 data.dptr = (uint8_t *)&c;
263 data.dsize = sizeof(c);
265 /* tell the other nodes that something has changed */
266 ctdb_daemon_send_message(ctdb, CTDB_BROADCAST_CONNECTED,
267 CTDB_SRVID_NODE_FLAGS_CHANGED, data);
269 if ((node->flags & NODE_FLAGS_BANNED) && !(old_flags & NODE_FLAGS_BANNED)) {
270 /* make sure we are frozen */
271 DEBUG(0,("This node has been banned - forcing freeze and recovery\n"));
272 /* Reset the generation id to 1 to make us ignore any
273 REQ/REPLY CALL/DMASTER someone sends to us.
274 We are now banned so we shouldnt service database calls
277 ctdb->vnn_map->generation = INVALID_GENERATION;
279 ctdb_start_freeze(ctdb);
280 ctdb_release_all_ips(ctdb);
281 ctdb->recovery_mode = CTDB_RECOVERY_ACTIVE;