📄 ctdb_takeover.c
字号:
/* ctdb recovery code Copyright (C) Ronnie Sahlberg 2007 Copyright (C) Andrew Tridgell 2007 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>.*/#include "includes.h"#include "lib/events/events.h"#include "lib/tdb/include/tdb.h"#include "lib/util/dlinklist.h"#include "system/network.h"#include "system/filesys.h"#include "system/wait.h"#include "../include/ctdb_private.h"#define TAKEOVER_TIMEOUT() timeval_current_ofs(ctdb->tunable.takeover_timeout,0)#define CTDB_ARP_INTERVAL 1#define CTDB_ARP_REPEAT 3struct ctdb_takeover_arp { struct ctdb_context *ctdb; uint32_t count; struct sockaddr_in sin; struct ctdb_tcp_list *tcp_list;};/* lists of tcp endpoints */struct ctdb_tcp_list { struct ctdb_tcp_list *prev, *next; uint32_t vnn; struct sockaddr_in saddr; struct sockaddr_in daddr;};/* list of clients to kill on IP release */struct ctdb_client_ip { struct ctdb_client_ip *prev, *next; struct ctdb_context *ctdb; struct sockaddr_in ip; uint32_t client_id;};/* send a gratuitous arp */static void ctdb_control_send_arp(struct event_context *ev, struct timed_event *te, struct timeval t, void *private_data){ struct ctdb_takeover_arp *arp = talloc_get_type(private_data, struct ctdb_takeover_arp); int ret; struct ctdb_tcp_list *tcp; ret = ctdb_sys_send_arp(&arp->sin, arp->ctdb->takeover.interface); if (ret != 0) { DEBUG(0,(__location__ " sending of arp failed (%s)\n", strerror(errno))); } for (tcp=arp->tcp_list;tcp;tcp=tcp->next) { DEBUG(2,("sending tcp tickle ack for %u->%s:%u\n", (unsigned)ntohs(tcp->daddr.sin_port), inet_ntoa(tcp->saddr.sin_addr), (unsigned)ntohs(tcp->saddr.sin_port))); ret = ctdb_sys_send_tcp(&tcp->saddr, &tcp->daddr, 0, 0, 0); if (ret != 0) { DEBUG(0,(__location__ " Failed to send tcp tickle ack for %s\n", inet_ntoa(tcp->saddr.sin_addr))); } } arp->count++; if (arp->count == CTDB_ARP_REPEAT) { talloc_free(arp); return; } event_add_timed(arp->ctdb->ev, arp->ctdb->takeover.last_ctx, timeval_current_ofs(CTDB_ARP_INTERVAL, 0), ctdb_control_send_arp, arp);}struct takeover_callback_state { struct ctdb_req_control *c; struct sockaddr_in *sin;};/* called when takeip event finishes */static void takeover_ip_callback(struct ctdb_context *ctdb, int status, void *private_data){ struct takeover_callback_state *state = talloc_get_type(private_data, struct takeover_callback_state); struct ctdb_takeover_arp *arp; char *ip = inet_ntoa(state->sin->sin_addr); struct ctdb_tcp_list *tcp; ctdb_start_monitoring(ctdb); if (status != 0) { DEBUG(0,(__location__ " Failed to takeover IP %s on interface %s\n", ip, ctdb->takeover.interface)); ctdb_request_control_reply(ctdb, state->c, NULL, status, NULL); talloc_free(state); return; } if (!ctdb->takeover.last_ctx) { ctdb->takeover.last_ctx = talloc_new(ctdb); if (!ctdb->takeover.last_ctx) goto failed; } arp = talloc_zero(ctdb->takeover.last_ctx, struct ctdb_takeover_arp); if (!arp) goto failed; arp->ctdb = ctdb; arp->sin = *state->sin; /* add all of the known tcp connections for this IP to the list of tcp connections to send tickle acks for */ for (tcp=ctdb->tcp_list;tcp;tcp=tcp->next) { if (state->sin->sin_addr.s_addr == tcp->daddr.sin_addr.s_addr) { struct ctdb_tcp_list *t2 = talloc(arp, struct ctdb_tcp_list); if (t2 == NULL) goto failed; *t2 = *tcp; DLIST_ADD(arp->tcp_list, t2); } } event_add_timed(arp->ctdb->ev, arp->ctdb->takeover.last_ctx, timeval_zero(), ctdb_control_send_arp, arp); /* the control succeeded */ ctdb_request_control_reply(ctdb, state->c, NULL, 0, NULL); talloc_free(state); return;failed: ctdb_request_control_reply(ctdb, state->c, NULL, -1, NULL); talloc_free(state); return;}/* take over an ip address */int32_t ctdb_control_takeover_ip(struct ctdb_context *ctdb, struct ctdb_req_control *c, TDB_DATA indata, bool *async_reply){ int ret; struct takeover_callback_state *state; struct ctdb_public_ip *pip = (struct ctdb_public_ip *)indata.dptr; char *ip = inet_ntoa(pip->sin.sin_addr); /* update out node table */ ctdb->nodes[pip->vnn]->takeover_vnn = pip->takeover_vnn; /* if our kernel already has this IP, do nothing */ if (ctdb_sys_have_ip(ip)) { return 0; } state = talloc(ctdb, struct takeover_callback_state); CTDB_NO_MEMORY(ctdb, state); state->c = talloc_steal(ctdb, c); state->sin = talloc(ctdb, struct sockaddr_in); CTDB_NO_MEMORY(ctdb, state->sin); *state->sin = pip->sin; DEBUG(0,("Takover of IP %s/%u on interface %s\n", ip, ctdb->nodes[ctdb->vnn]->public_netmask_bits, ctdb->takeover.interface)); ctdb_stop_monitoring(ctdb); ret = ctdb_event_script_callback(ctdb, timeval_current_ofs(ctdb->tunable.script_timeout, 0), state, takeover_ip_callback, state, "takeip %s %s %u", ctdb->takeover.interface, ip, ctdb->nodes[ctdb->vnn]->public_netmask_bits); if (ret != 0) { DEBUG(0,(__location__ " Failed to takeover IP %s on interface %s\n", ip, ctdb->takeover.interface)); talloc_free(state); return -1; } /* tell ctdb_control.c that we will be replying asynchronously */ *async_reply = true; return 0;}/* kill any clients that are registered with a IP that is being released */static void release_kill_clients(struct ctdb_context *ctdb, struct in_addr in){ struct ctdb_client_ip *ip; for (ip=ctdb->client_ip_list; ip; ip=ip->next) { if (ip->ip.sin_addr.s_addr == in.s_addr) { struct ctdb_client *client = ctdb_reqid_find(ctdb, ip->client_id, struct ctdb_client); if (client->pid != 0) { DEBUG(0,(__location__ " Killing client pid %u for IP %s on client_id %u\n", (unsigned)client->pid, inet_ntoa(in), ip->client_id)); kill(client->pid, SIGKILL); } } }}/* called when releaseip event finishes */static void release_ip_callback(struct ctdb_context *ctdb, int status, void *private_data){ struct takeover_callback_state *state = talloc_get_type(private_data, struct takeover_callback_state); char *ip = inet_ntoa(state->sin->sin_addr); TDB_DATA data; struct ctdb_tcp_list *tcp; ctdb_start_monitoring(ctdb); /* send a message to all clients of this node telling them that the cluster has been reconfigured and they should release any sockets on this IP */ data.dptr = (uint8_t *)ip; data.dsize = strlen(ip)+1; ctdb_daemon_send_message(ctdb, ctdb->vnn, CTDB_SRVID_RELEASE_IP, data); /* kill clients that have registered with this IP */ release_kill_clients(ctdb, state->sin->sin_addr); /* tell other nodes about any tcp connections we were holding with this IP */ for (tcp=ctdb->tcp_list;tcp;tcp=tcp->next) { if (tcp->vnn == ctdb->vnn && state->sin->sin_addr.s_addr == tcp->daddr.sin_addr.s_addr) { struct ctdb_control_tcp_vnn t; t.vnn = ctdb->vnn; t.src = tcp->saddr; t.dest = tcp->daddr; data.dptr = (uint8_t *)&t; data.dsize = sizeof(t); ctdb_daemon_send_control(ctdb, CTDB_BROADCAST_CONNECTED, 0, CTDB_CONTROL_TCP_ADD, 0, CTDB_CTRL_FLAG_NOREPLY, data, NULL, NULL); } } /* the control succeeded */ ctdb_request_control_reply(ctdb, state->c, NULL, 0, NULL); talloc_free(state);}/* release an ip address */int32_t ctdb_control_release_ip(struct ctdb_context *ctdb, struct ctdb_req_control *c, TDB_DATA indata, bool *async_reply){ int ret; struct takeover_callback_state *state; struct ctdb_public_ip *pip = (struct ctdb_public_ip *)indata.dptr; char *ip = inet_ntoa(pip->sin.sin_addr); /* update out node table */ ctdb->nodes[pip->vnn]->takeover_vnn = pip->takeover_vnn; if (!ctdb_sys_have_ip(ip)) { return 0; } DEBUG(0,("Release of IP %s/%u on interface %s\n", ip, ctdb->nodes[ctdb->vnn]->public_netmask_bits, ctdb->takeover.interface)); /* stop any previous arps */ talloc_free(ctdb->takeover.last_ctx); ctdb->takeover.last_ctx = NULL; state = talloc(ctdb, struct takeover_callback_state); CTDB_NO_MEMORY(ctdb, state); state->c = talloc_steal(state, c); state->sin = talloc(state, struct sockaddr_in); CTDB_NO_MEMORY(ctdb, state->sin); *state->sin = pip->sin; ctdb_stop_monitoring(ctdb); ret = ctdb_event_script_callback(ctdb, timeval_current_ofs(ctdb->tunable.script_timeout, 0), state, release_ip_callback, state, "releaseip %s %s %u", ctdb->takeover.interface, ip, ctdb->nodes[ctdb->vnn]->public_netmask_bits); if (ret != 0) { DEBUG(0,(__location__ " Failed to release IP %s on interface %s\n", ip, ctdb->takeover.interface)); talloc_free(state); return -1; } /* tell the control that we will be reply asynchronously */ *async_reply = true; return 0;}/* setup the event script*/int ctdb_set_event_script(struct ctdb_context *ctdb, const char *script){ ctdb->takeover.event_script = talloc_strdup(ctdb, script); CTDB_NO_MEMORY(ctdb, ctdb->takeover.event_script); return 0;}/* setup the public address list from a file*/int ctdb_set_public_addresses(struct ctdb_context *ctdb, const char *alist){ char **lines; int nlines; int i; lines = file_lines_load(alist, &nlines, ctdb); if (lines == NULL) { ctdb_set_error(ctdb, "Failed to load public address list '%s'\n", alist); return -1; } while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) { nlines--; } if (nlines != ctdb->num_nodes) { DEBUG(0,("Number of lines in %s does not match number of nodes!\n", alist)); talloc_free(lines); return -1; } for (i=0;i<nlines;i++) { char *p; struct in_addr in; ctdb->nodes[i]->public_address = talloc_strdup(ctdb->nodes[i], lines[i]); CTDB_NO_MEMORY(ctdb, ctdb->nodes[i]->public_address); ctdb->nodes[i]->takeover_vnn = -1; /* see if they supplied a netmask length */ p = strchr(ctdb->nodes[i]->public_address, '/'); if (!p) { DEBUG(0,("You must supply a netmask for public address %s\n", ctdb->nodes[i]->public_address)); return -1; } *p = 0; ctdb->nodes[i]->public_netmask_bits = atoi(p+1); if (ctdb->nodes[i]->public_netmask_bits > 32) { DEBUG(0, ("Illegal netmask for IP %s\n", ctdb->nodes[i]->public_address)); return -1; } if (inet_aton(ctdb->nodes[i]->public_address, &in) == 0) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -