|
| 1 | +/* Copyright (c) 2024, Oracle and/or its affiliates. |
| 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, version 2.0, |
| 5 | + as published by the Free Software Foundation. |
| 6 | +
|
| 7 | + This program is designed to work with certain software (including |
| 8 | + but not limited to OpenSSL) that is licensed under separate terms, |
| 9 | + as designated in a particular file or component or in included license |
| 10 | + documentation. The authors of MySQL hereby grant you an additional |
| 11 | + permission to link the program and your derivative works with the |
| 12 | + separately licensed software that they have either included with |
| 13 | + the program or referenced in the documentation. |
| 14 | +
|
| 15 | + This program is distributed in the hope that it will be useful, |
| 16 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | + GNU General Public License, version 2.0, for more details. |
| 19 | +
|
| 20 | + You should have received a copy of the GNU General Public License |
| 21 | + along with this program; if not, write to the Free Software |
| 22 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 | + |
| 24 | +#include <mysql/components/service_implementation.h> |
| 25 | +#include <mysql/components/services/group_replication_management_service.h> |
| 26 | + |
| 27 | +#include "plugin/group_replication/include/leave_group_on_failure.h" |
| 28 | +#include "plugin/group_replication/include/plugin.h" |
| 29 | +#include "plugin/group_replication/include/services/management/management.h" |
| 30 | + |
| 31 | +std::chrono::steady_clock::time_point GR_start_time_maintain::gr_start_time = |
| 32 | + std::chrono::steady_clock::time_point::min(); |
| 33 | + |
| 34 | +void GR_start_time_maintain::reset_start_time() { |
| 35 | + gr_start_time = std::chrono::steady_clock::now(); |
| 36 | +} |
| 37 | + |
| 38 | +bool GR_start_time_maintain::check_if_quarantine_time_passed( |
| 39 | + int quarantime_time, unsigned int *seconds_since_member_join) { |
| 40 | + auto time_now = std::chrono::steady_clock::now(); |
| 41 | + auto time_diff = |
| 42 | + std::chrono::duration_cast<std::chrono::seconds>(time_now - gr_start_time) |
| 43 | + .count(); |
| 44 | + *seconds_since_member_join = time_diff; |
| 45 | + return gr_start_time != std::chrono::steady_clock::time_point::min() && |
| 46 | + time_diff > quarantime_time; |
| 47 | +} |
| 48 | + |
| 49 | +//////////////////////////////////////////////////////////////////////////////// |
| 50 | +namespace gr { |
| 51 | +namespace gr_management { |
| 52 | +DEFINE_METHOD(eject_status, eject, |
| 53 | + (int quarantine_time_in_seconds, |
| 54 | + unsigned int *seconds_since_member_join)) { |
| 55 | + DBUG_TRACE; |
| 56 | + if (local_member_info == nullptr || group_member_mgr == nullptr) { |
| 57 | + return GR_RM_NOT_A_MEMBER; |
| 58 | + } |
| 59 | + if (!local_member_info->in_primary_mode()) { |
| 60 | + return GR_RM_NOT_IN_SINGLE_PRIMARY_MODE; |
| 61 | + } |
| 62 | + if (local_member_info->get_role() != |
| 63 | + Group_member_info::MEMBER_ROLE_SECONDARY) { |
| 64 | + return GR_RM_NOT_A_SECONDARY_MEMBER; |
| 65 | + } |
| 66 | + if (group_member_mgr->get_number_of_members() < 3) { |
| 67 | + return GR_RM_NUMBER_OF_MEMBERS_LESS_THAN_THREE; |
| 68 | + } |
| 69 | + if (!GR_start_time_maintain::check_if_quarantine_time_passed( |
| 70 | + quarantine_time_in_seconds, seconds_since_member_join)) { |
| 71 | + return GR_RM_QUARANTINE_PERIOD_NOT_OVER; |
| 72 | + } |
| 73 | + |
| 74 | + std::string error_message("Service call to leave the group."); |
| 75 | + leave_group_on_failure::mask leave_actions; |
| 76 | + leave_actions.set(leave_group_on_failure::STOP_APPLIER, true); |
| 77 | + leave_actions.set(leave_group_on_failure::HANDLE_EXIT_STATE_ACTION, true); |
| 78 | + leave_actions.set(leave_group_on_failure::HANDLE_AUTO_REJOIN, true); |
| 79 | + leave_group_on_failure::leave(leave_actions, 0, nullptr, |
| 80 | + error_message.c_str()); |
| 81 | + return GR_RM_SUCCESS_LEFT_GROUP; |
| 82 | +} |
| 83 | + |
| 84 | +DEFINE_BOOL_METHOD(is_member_online_or_recovering, ()) { |
| 85 | + DBUG_TRACE; |
| 86 | + |
| 87 | + if (!plugin_is_group_replication_running()) return false; |
| 88 | + |
| 89 | + if (nullptr == local_member_info) return false; |
| 90 | + |
| 91 | + const Group_member_info::Group_member_status member_status = |
| 92 | + local_member_info->get_recovery_status(); |
| 93 | + if (member_status == Group_member_info::MEMBER_ONLINE || |
| 94 | + member_status == Group_member_info::MEMBER_IN_RECOVERY) { |
| 95 | + return true; |
| 96 | + } |
| 97 | + |
| 98 | + return false; |
| 99 | +} |
| 100 | +} // namespace gr_management |
| 101 | +} // namespace gr |
| 102 | +BEGIN_SERVICE_IMPLEMENTATION(group_replication, |
| 103 | + group_replication_management_service_v1) |
| 104 | +gr::gr_management::eject, gr::gr_management::is_member_online_or_recovering, |
| 105 | + END_SERVICE_IMPLEMENTATION(); |
| 106 | + |
| 107 | +bool register_group_replication_management_services() { |
| 108 | + DBUG_TRACE; |
| 109 | + |
| 110 | + DBUG_EXECUTE_IF("group_replication_management_service", return false;); |
| 111 | + |
| 112 | + my_service<SERVICE_TYPE(registry_registration)> reg("registry_registration", |
| 113 | + get_plugin_registry()); |
| 114 | + using group_replication_management_service_t = |
| 115 | + SERVICE_TYPE_NO_CONST(group_replication_management_service_v1); |
| 116 | + return reg->register_service( |
| 117 | + GROUP_REPLICATION_MANAGEMENT_SERVICE_NAME, |
| 118 | + reinterpret_cast<my_h_service>( |
| 119 | + const_cast<group_replication_management_service_t *>( |
| 120 | + &SERVICE_IMPLEMENTATION( |
| 121 | + group_replication, |
| 122 | + group_replication_management_service_v1)))); |
| 123 | +} |
| 124 | + |
| 125 | +bool unregister_group_replication_management_services() { |
| 126 | + DBUG_TRACE; |
| 127 | + my_service<SERVICE_TYPE(registry_registration)> reg("registry_registration", |
| 128 | + get_plugin_registry()); |
| 129 | + return reg->unregister(GROUP_REPLICATION_MANAGEMENT_SERVICE_NAME); |
| 130 | +} |
0 commit comments