| 1 | /* Copyright 2010 Codership Oy <http://www.codership.com> |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License as published by |
| 5 | the Free Software Foundation; version 2 of the License. |
| 6 | |
| 7 | This program is distributed in the hope that it will be useful, |
| 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | GNU General Public License for more details. |
| 11 | |
| 12 | You should have received a copy of the GNU General Public License |
| 13 | along with this program; if not, write to the Free Software |
| 14 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA */ |
| 15 | |
| 16 | #include "mariadb.h" |
| 17 | #include <mysqld.h> |
| 18 | #include "wsrep_priv.h" |
| 19 | #include "wsrep_utils.h" |
| 20 | |
| 21 | |
| 22 | static const char* _status_str(wsrep_member_status_t status) |
| 23 | { |
| 24 | switch (status) |
| 25 | { |
| 26 | case WSREP_MEMBER_UNDEFINED: return "Undefined" ; |
| 27 | case WSREP_MEMBER_JOINER: return "Joiner" ; |
| 28 | case WSREP_MEMBER_DONOR: return "Donor" ; |
| 29 | case WSREP_MEMBER_JOINED: return "Joined" ; |
| 30 | case WSREP_MEMBER_SYNCED: return "Synced" ; |
| 31 | default: return "Error(?)" ; |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | void wsrep_notify_status (wsrep_member_status_t status, |
| 36 | const wsrep_view_info_t* view) |
| 37 | { |
| 38 | if (!wsrep_notify_cmd || 0 == strlen(wsrep_notify_cmd)) |
| 39 | { |
| 40 | WSREP_INFO("wsrep_notify_cmd is not defined, skipping notification." ); |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | char cmd_buf[1 << 16]; // this can be long |
| 45 | long cmd_len = sizeof(cmd_buf) - 1; |
| 46 | char* cmd_ptr = cmd_buf; |
| 47 | long cmd_off = 0; |
| 48 | |
| 49 | cmd_off += snprintf (cmd_ptr + cmd_off, cmd_len - cmd_off, "%s" , |
| 50 | wsrep_notify_cmd); |
| 51 | |
| 52 | if (status >= WSREP_MEMBER_UNDEFINED && status < WSREP_MEMBER_ERROR) |
| 53 | { |
| 54 | cmd_off += snprintf (cmd_ptr + cmd_off, cmd_len - cmd_off, " --status %s" , |
| 55 | _status_str(status)); |
| 56 | } |
| 57 | else |
| 58 | { |
| 59 | /* here we preserve provider error codes */ |
| 60 | cmd_off += snprintf (cmd_ptr + cmd_off, cmd_len - cmd_off, |
| 61 | " --status 'Error(%d)'" , status); |
| 62 | } |
| 63 | |
| 64 | if (0 != view) |
| 65 | { |
| 66 | char uuid_str[40]; |
| 67 | |
| 68 | wsrep_uuid_print (&view->state_id.uuid, uuid_str, sizeof(uuid_str)); |
| 69 | cmd_off += snprintf (cmd_ptr + cmd_off, cmd_len - cmd_off, |
| 70 | " --uuid %s" , uuid_str); |
| 71 | |
| 72 | cmd_off += snprintf (cmd_ptr + cmd_off, cmd_len - cmd_off, |
| 73 | " --primary %s" , view->view >= 0 ? "yes" : "no" ); |
| 74 | |
| 75 | cmd_off += snprintf (cmd_ptr + cmd_off, cmd_len - cmd_off, |
| 76 | " --index %d" , view->my_idx); |
| 77 | |
| 78 | if (view->memb_num) |
| 79 | { |
| 80 | cmd_off += snprintf (cmd_ptr + cmd_off, cmd_len - cmd_off, " --members" ); |
| 81 | |
| 82 | for (int i = 0; i < view->memb_num; i++) |
| 83 | { |
| 84 | wsrep_uuid_print (&view->members[i].id, uuid_str, sizeof(uuid_str)); |
| 85 | cmd_off += snprintf (cmd_ptr + cmd_off, cmd_len - cmd_off, |
| 86 | "%c%s/%s/%s" , i > 0 ? ',' : ' ', |
| 87 | uuid_str, view->members[i].name, |
| 88 | view->members[i].incoming); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | if (cmd_off == cmd_len) |
| 94 | { |
| 95 | WSREP_ERROR("Notification buffer too short (%ld). Aborting notification." , |
| 96 | cmd_len); |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | wsp::process p(cmd_ptr, "r" , NULL); |
| 101 | |
| 102 | p.wait(); |
| 103 | int err = p.error(); |
| 104 | |
| 105 | if (err) |
| 106 | { |
| 107 | WSREP_ERROR("Notification command failed: %d (%s): \"%s\"" , |
| 108 | err, strerror(err), cmd_ptr); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | |