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 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);
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) {
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);
59 if (node->rx_cnt == 0) {
67 if (node->dead_count >= ctdb->tunable.keepalive_limit) {
68 DEBUG(0,("dead count reached for node %u\n", node->pnn));
70 ctdb_send_keepalive(ctdb, node->pnn);
71 /* maybe tell the transport layer to kill the
77 if (node->tx_cnt == 0) {
78 DEBUG(5,("sending keepalive to %u\n", node->pnn));
79 ctdb_send_keepalive(ctdb, node->pnn);
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);
90 static void ctdb_check_health(struct event_context *ev, struct timed_event *te,
91 struct timeval t, void *private_data);
94 called when a health monitoring event script finishes
96 static void ctdb_health_callback(struct ctdb_context *ctdb, int status, void *p)
98 struct ctdb_node *node = ctdb->nodes[ctdb->pnn];
100 struct ctdb_node_flag_change c;
101 uint32_t next_interval;
104 c.old_flags = node->flags;
106 if (status != 0 && !(node->flags & NODE_FLAGS_UNHEALTHY)) {
107 DEBUG(0,("monitor event failed - disabling node\n"));
108 node->flags |= NODE_FLAGS_UNHEALTHY;
109 } else if (status == 0 && (node->flags & NODE_FLAGS_UNHEALTHY)) {
110 DEBUG(0,("monitor event OK - node re-enabled\n"));
111 node->flags &= ~NODE_FLAGS_UNHEALTHY;
114 if (node->flags & NODE_FLAGS_UNHEALTHY) {
115 next_interval = ctdb->tunable.monitor_retry;
117 next_interval = ctdb->tunable.monitor_interval;
120 event_add_timed(ctdb->ev, ctdb->monitor_context,
121 timeval_current_ofs(next_interval, 0),
122 ctdb_check_health, ctdb);
124 if (c.old_flags == node->flags) {
128 c.new_flags = node->flags;
130 data.dptr = (uint8_t *)&c;
131 data.dsize = sizeof(c);
133 /* tell the other nodes that something has changed */
134 ctdb_daemon_send_message(ctdb, CTDB_BROADCAST_CONNECTED,
135 CTDB_SRVID_NODE_FLAGS_CHANGED, data);
141 called when the startup event script finishes
143 static void ctdb_startup_callback(struct ctdb_context *ctdb, int status, void *p)
146 DEBUG(0,("startup event failed\n"));
147 } else if (status == 0) {
148 DEBUG(0,("startup event OK - enabling monitoring\n"));
149 ctdb->done_startup = true;
152 if (ctdb->done_startup) {
153 event_add_timed(ctdb->ev, ctdb->monitor_context,
155 ctdb_check_health, ctdb);
157 event_add_timed(ctdb->ev, ctdb->monitor_context,
158 timeval_current_ofs(ctdb->tunable.monitor_interval, 0),
159 ctdb_check_health, ctdb);
166 see if the event scripts think we are healthy
168 static void ctdb_check_health(struct event_context *ev, struct timed_event *te,
169 struct timeval t, void *private_data)
171 struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
174 if (ctdb->recovery_mode != CTDB_RECOVERY_NORMAL ||
175 (ctdb->monitoring_mode == CTDB_MONITORING_DISABLED && ctdb->done_startup)) {
176 event_add_timed(ctdb->ev, ctdb->monitor_context,
177 timeval_current_ofs(ctdb->tunable.monitor_interval, 0),
178 ctdb_check_health, ctdb);
182 if (!ctdb->done_startup) {
183 ret = ctdb_event_script_callback(ctdb,
184 timeval_current_ofs(ctdb->tunable.script_timeout, 0),
185 ctdb->monitor_context, ctdb_startup_callback,
188 ret = ctdb_event_script_callback(ctdb,
189 timeval_current_ofs(ctdb->tunable.script_timeout, 0),
190 ctdb->monitor_context, ctdb_health_callback,
195 DEBUG(0,("Unable to launch monitor event script\n"));
196 event_add_timed(ctdb->ev, ctdb->monitor_context,
197 timeval_current_ofs(ctdb->tunable.monitor_retry, 0),
198 ctdb_check_health, ctdb);
202 /* stop any monitoring */
203 void ctdb_stop_monitoring(struct ctdb_context *ctdb)
205 talloc_free(ctdb->monitor_context);
206 ctdb->monitor_context = talloc_new(ctdb);
207 CTDB_NO_MEMORY_FATAL(ctdb, ctdb->monitor_context);
211 start watching for nodes that might be dead
213 void ctdb_start_monitoring(struct ctdb_context *ctdb)
215 struct timed_event *te;
217 ctdb_stop_monitoring(ctdb);
219 te = event_add_timed(ctdb->ev, ctdb->monitor_context,
220 timeval_current_ofs(ctdb->tunable.keepalive_interval, 0),
221 ctdb_check_for_dead_nodes, ctdb);
222 CTDB_NO_MEMORY_FATAL(ctdb, te);
224 te = event_add_timed(ctdb->ev, ctdb->monitor_context,
225 timeval_current_ofs(ctdb->tunable.monitor_retry, 0),
226 ctdb_check_health, ctdb);
227 CTDB_NO_MEMORY_FATAL(ctdb, te);
232 modify flags on a node
234 int32_t ctdb_control_modflags(struct ctdb_context *ctdb, TDB_DATA indata)
236 struct ctdb_node_modflags *m = (struct ctdb_node_modflags *)indata.dptr;
238 struct ctdb_node_flag_change c;
239 struct ctdb_node *node = ctdb->nodes[ctdb->pnn];
240 uint32_t old_flags = node->flags;
242 node->flags |= m->set;
243 node->flags &= ~m->clear;
245 if (node->flags == old_flags) {
250 DEBUG(0, ("Control modflags on node %u - flags now 0x%x\n", ctdb->pnn, node->flags));
252 /* if we have been banned, go into recovery mode */
254 c.old_flags = old_flags;
255 c.new_flags = node->flags;
257 data.dptr = (uint8_t *)&c;
258 data.dsize = sizeof(c);
260 /* tell the other nodes that something has changed */
261 ctdb_daemon_send_message(ctdb, CTDB_BROADCAST_CONNECTED,
262 CTDB_SRVID_NODE_FLAGS_CHANGED, data);
264 if ((node->flags & NODE_FLAGS_BANNED) && !(old_flags & NODE_FLAGS_BANNED)) {
265 /* make sure we are frozen */
266 DEBUG(0,("This node has been banned - forcing freeze and recovery\n"));
267 /* Reset the generation id to 1 to make us ignore any
268 REQ/REPLY CALL/DMASTER someone sends to us.
269 We are now banned so we shouldnt service database calls
272 ctdb->vnn_map->generation = INVALID_GENERATION;
274 ctdb_start_freeze(ctdb);
275 ctdb_release_all_ips(ctdb);
276 ctdb->recovery_mode = CTDB_RECOVERY_ACTIVE;