1 /* Copyright (c) 2015, 2021, 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 also distributed 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 included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software Foundation,
21    51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
22 
23 #include "read_mode_handler.h"
24 #include "plugin.h"
25 
enable_super_read_only_mode(Sql_service_command_interface * command_interface)26 long enable_super_read_only_mode(Sql_service_command_interface *command_interface)
27 {
28   DBUG_ENTER("set_super_read_only_mode");
29   long error =0;
30 
31 #ifndef NDEBUG
32   DBUG_EXECUTE_IF("group_replication_skip_read_mode", { DBUG_RETURN(0); });
33   DBUG_EXECUTE_IF("group_replication_read_mode_error", { DBUG_RETURN(1); });
34 #endif
35 
36   assert(command_interface != NULL);
37 
38   // Extract server values for the super read mode
39   longlong server_super_read_only_query=
40       command_interface->get_server_super_read_only();
41 
42   error= server_super_read_only_query == -1;
43 
44   // Setting the super_read_only mode on the server.
45   if (!error)
46   {
47     if(!server_super_read_only_query)
48       error= command_interface->set_super_read_only();
49   }
50   else
51   {
52     log_message(MY_ERROR_LEVEL,
53                 "Can't read the server value for the super_read_only"
54                 " variable."); /* purecov: inspected */
55   }
56 
57   DBUG_RETURN(error);
58 }
59 
disable_super_read_only_mode(Sql_service_command_interface * command_interface)60 long disable_super_read_only_mode(Sql_service_command_interface *command_interface)
61 {
62   DBUG_ENTER("reset_super_read_mode");
63   long error =0;
64 
65   assert(command_interface != NULL);
66 
67   error = command_interface->reset_read_only();
68 
69   DBUG_RETURN(error);
70 }
71 
enable_server_read_mode(enum_plugin_con_isolation session_isolation)72 int enable_server_read_mode(enum_plugin_con_isolation session_isolation)
73 {
74   Sql_service_command_interface *sql_command_interface=
75       new Sql_service_command_interface();
76   int error=
77     sql_command_interface->
78       establish_session_connection(session_isolation, get_plugin_pointer()) ||
79     sql_command_interface->set_interface_user(GROUPREPL_USER) ||
80     enable_super_read_only_mode(sql_command_interface);
81   delete sql_command_interface;
82   return error;
83 }
84 
disable_server_read_mode(enum_plugin_con_isolation session_isolation)85 int disable_server_read_mode(enum_plugin_con_isolation session_isolation)
86 {
87   Sql_service_command_interface *sql_command_interface=
88       new Sql_service_command_interface();
89   int error=
90     sql_command_interface->
91       establish_session_connection(session_isolation, get_plugin_pointer()) ||
92     sql_command_interface->set_interface_user(GROUPREPL_USER) ||
93     disable_super_read_only_mode(sql_command_interface);
94   delete sql_command_interface;
95   return error;
96 }
97 
get_read_mode_state(Sql_service_command_interface * sql_command_interface,bool * read_only_enabled,bool * super_read_only_enabled)98 long get_read_mode_state(Sql_service_command_interface *sql_command_interface,
99                          bool *read_only_enabled, bool *super_read_only_enabled)
100 {
101   DBUG_ENTER("get_read_mode_state");
102 
103   long error =0;
104 
105   assert(sql_command_interface != NULL);
106 
107   // Extract server values for the read mode
108   longlong server_read_only_query=
109       sql_command_interface->get_server_read_only();
110   longlong server_super_read_only_query=
111       sql_command_interface->get_server_super_read_only();
112 
113   error= server_read_only_query == -1 || server_super_read_only_query == -1;
114 
115   if (!error)
116   {
117     *read_only_enabled= (bool) server_read_only_query;
118     *super_read_only_enabled= (bool) server_super_read_only_query;
119   }
120   else
121   {
122     log_message(MY_ERROR_LEVEL,
123                 "Can't read the server values for the read_only and "
124                 "super_read_only variables."); /* purecov: inspected */
125   }
126 
127   DBUG_RETURN(error);
128 }
129 
set_read_mode_state(Sql_service_command_interface * sql_service_command,bool read_only_enabled,bool super_read_only_enabled)130 long set_read_mode_state(Sql_service_command_interface *sql_service_command,
131                          bool read_only_enabled, bool super_read_only_enabled)
132 {
133   DBUG_ENTER("set_read_mode_state");
134 
135   long error= 0;
136 
137   if (!read_only_enabled)
138     error|= sql_service_command->reset_read_only();
139   else if (!super_read_only_enabled)
140     error|= sql_service_command->reset_super_read_only();
141 
142   if (error)
143   {
144     //Do not throw an error as the user can reset the read mode
145     log_message(MY_ERROR_LEVEL,
146                 "It was not possible to reset the server read mode settings."
147                 " Try to reset them manually."); /* purecov: inspected */
148   }
149 
150   DBUG_RETURN(error);
151 }
152