1 /*
2       Copyright (c) 2013, 2021, Oracle and/or its affiliates.
3 
4       This program is free software; you can redistribute it and/or modify
5       it under the terms of the GNU General Public License, version 2.0,
6       as published by the Free Software Foundation.
7 
8       This program is also distributed with certain software (including
9       but not limited to OpenSSL) that is licensed under separate terms,
10       as designated in a particular file or component or in included license
11       documentation.  The authors of MySQL hereby grant you an additional
12       permission to link the program and your derivative works with the
13       separately licensed software that they have included with MySQL.
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 /**
25   @file storage/perfschema/table_replication_applier_configuration.cc
26   Table replication_applier_configuration (implementation).
27 */
28 
29 #define HAVE_REPLICATION
30 
31 #include "my_global.h"
32 #include "table_replication_applier_configuration.h"
33 #include "pfs_instr_class.h"
34 #include "pfs_instr.h"
35 #include "rpl_slave.h"
36 #include "rpl_info.h"
37 #include "rpl_rli.h"
38 #include "rpl_mi.h"
39 #include "sql_parse.h"
40 #include "rpl_msr.h"   /* Multisource replication */
41 
42 THR_LOCK table_replication_applier_configuration::m_table_lock;
43 
44 /*
45   numbers in varchar count utf8 characters.
46 */
47 static const TABLE_FIELD_TYPE field_types[]=
48 {
49   {
50     {C_STRING_WITH_LEN("CHANNEL_NAME")},
51     {C_STRING_WITH_LEN("char(64)")},
52     {NULL,0}
53   },
54 
55   {
56     {C_STRING_WITH_LEN("DESIRED_DELAY")},
57     {C_STRING_WITH_LEN("int(11)")},
58     {NULL, 0}
59   }
60 };
61 
62 TABLE_FIELD_DEF
63 table_replication_applier_configuration::m_field_def=
64 { 2, field_types };
65 
66 PFS_engine_table_share
67 table_replication_applier_configuration::m_share=
68 {
69   { C_STRING_WITH_LEN("replication_applier_configuration") },
70   &pfs_readonly_acl,
71   table_replication_applier_configuration::create,
72   NULL, /* write_row */
73   NULL, /* delete_all_rows */
74   table_replication_applier_configuration::get_row_count,
75   sizeof(pos_t), /* ref length */
76   &m_table_lock,
77   &m_field_def,
78   false, /* checked */
79   false  /* perpetual */
80 };
81 
create(void)82 PFS_engine_table* table_replication_applier_configuration::create(void)
83 {
84   return new table_replication_applier_configuration();
85 }
86 
87 table_replication_applier_configuration
table_replication_applier_configuration()88   ::table_replication_applier_configuration()
89   : PFS_engine_table(&m_share, &m_pos),
90     m_row_exists(false), m_pos(0), m_next_pos(0)
91 {}
92 
93 table_replication_applier_configuration
~table_replication_applier_configuration()94   ::~table_replication_applier_configuration()
95 {}
96 
reset_position(void)97 void table_replication_applier_configuration::reset_position(void)
98 {
99   m_pos.m_index= 0;
100   m_next_pos.m_index= 0;
101 }
102 
103 
get_row_count()104 ha_rows table_replication_applier_configuration::get_row_count()
105 {
106  return channel_map.get_max_channels();
107 }
108 
109 
rnd_next(void)110 int table_replication_applier_configuration::rnd_next(void)
111 {
112   Master_info *mi;
113   channel_map.rdlock();
114 
115   for (m_pos.set_at(&m_next_pos);
116        m_pos.m_index < channel_map.get_max_channels();
117        m_pos.next())
118   {
119     mi= channel_map.get_mi_at_pos(m_pos.m_index);
120 
121     if (mi && mi->host[0])
122     {
123       make_row(mi);
124       m_next_pos.set_after(&m_pos);
125       channel_map.unlock();
126       return 0;
127     }
128   }
129 
130   channel_map.unlock();
131   return HA_ERR_END_OF_FILE;
132 }
133 
rnd_pos(const void * pos)134 int table_replication_applier_configuration::rnd_pos(const void *pos)
135 {
136   Master_info *mi;
137   int res= HA_ERR_RECORD_DELETED;
138 
139   set_position(pos);
140 
141   channel_map.rdlock();
142 
143   if ((mi= channel_map.get_mi_at_pos(m_pos.m_index)))
144   {
145     make_row(mi);
146     res= 0;
147   }
148 
149   channel_map.unlock();
150   return res;
151 }
152 
make_row(Master_info * mi)153 void table_replication_applier_configuration::make_row(Master_info *mi)
154 {
155   m_row_exists= false;
156 
157   assert(mi != NULL);
158   assert(mi->rli != NULL);
159 
160   mysql_mutex_lock(&mi->data_lock);
161   mysql_mutex_lock(&mi->rli->data_lock);
162 
163   m_row.channel_name_length= mi->get_channel() ? strlen(mi->get_channel()):0;
164   memcpy(m_row.channel_name, mi->get_channel(), m_row.channel_name_length);
165   m_row.desired_delay= mi->rli->get_sql_delay();
166 
167   mysql_mutex_unlock(&mi->rli->data_lock);
168   mysql_mutex_unlock(&mi->data_lock);
169 
170   m_row_exists= true;
171 }
172 
read_row_values(TABLE * table,unsigned char * buf,Field ** fields,bool read_all)173 int table_replication_applier_configuration::read_row_values(TABLE *table,
174                                                              unsigned char *buf,
175                                                              Field **fields,
176                                                              bool read_all)
177 {
178   Field *f;
179 
180   if (unlikely(! m_row_exists))
181     return HA_ERR_RECORD_DELETED;
182 
183   /*
184     Note:
185     There are no NULL columns in this table,
186     so there are no null bits reserved for NULL flags per column.
187     There are no VARCHAR columns either, so the record is not
188     in HA_OPTION_PACK_RECORD format as most other performance_schema tables.
189     When HA_OPTION_PACK_RECORD is not set,
190     the table record reserves an extra null byte, see open_binary_frm().
191   */
192 
193   assert(table->s->null_bytes == 1);
194   buf[0]= 0;
195 
196   for (; (f= *fields) ; fields++)
197   {
198     if (read_all || bitmap_is_set(table->read_set, f->field_index))
199     {
200       switch(f->field_index)
201       {
202       case 0: /**channel_name*/
203         set_field_char_utf8(f, m_row.channel_name, m_row.channel_name_length);
204         break;
205       case 1: /** desired_delay */
206         set_field_ulong(f, static_cast<ulong>(m_row.desired_delay));
207         break;
208       default:
209         assert(false);
210       }
211     }
212   }
213   return 0;
214 }
215