drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
filtered_replicator.cc
Go to the documentation of this file.
1/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *
4 * Copyright (C) 2009 Sun Microsystems, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
38
39#include <config.h>
40#include <drizzled/gettext.h>
41#include <drizzled/plugin/transaction_applier.h>
42#include <drizzled/message/transaction.pb.h>
43#include <drizzled/plugin.h>
44
45#include <drizzled/item/string.h>
46#include "filtered_replicator.h"
47#include <boost/program_options.hpp>
49#include <vector>
50#include <string>
51namespace po= boost::program_options;
52using namespace std;
53using namespace drizzled;
54
55namespace drizzle_plugin
56{
57
58static string sysvar_filtered_replicator_sch_filters;
59static string sysvar_filtered_replicator_tab_filters;
60
61FilteredReplicator::FilteredReplicator(string name_arg,
62 const std::string &sch_filter,
63 const std::string &tab_filter,
64 const std::string &sch_regex,
65 const std::string &tab_regex) :
66 plugin::TransactionReplicator(name_arg),
67 schemas_to_filter(),
68 tables_to_filter(),
69 _sch_filter(sch_filter),
70 _tab_filter(tab_filter),
71 _sch_regex(sch_regex),
72 _tab_regex(tab_regex),
73 sch_re(NULL),
74 tab_re(NULL)
75{
76 /*
77 * Add each of the specified schemas to the vector of schemas
78 * to filter.
79 */
80 if (not _sch_filter.empty())
81 {
82 populateFilter(_sch_filter, schemas_to_filter);
83 }
84
85 /*
86 * Add each of the specified tables to the vector of tables
87 * to filter.
88 */
89 if (not _tab_filter.empty())
90 {
91 populateFilter(_tab_filter, tables_to_filter);
92 }
93
94 /*
95 * Compile the regular expression for schema's to filter
96 * if one is specified.
97 */
98 if (not _sch_regex.empty())
99 {
100 int error= 0;
101 PCRE2_SIZE error_offset= 0;
102 sch_re= pcre2_compile(reinterpret_cast<PCRE2_SPTR>(_sch_regex.c_str()),
103 PCRE2_ZERO_TERMINATED,
104 0,
105 &error,
106 &error_offset,
107 NULL);
108 }
109
110 /*
111 * Compile the regular expression for table's to filter
112 * if one is specified.
113 */
114 if (not _tab_regex.empty())
115 {
116 int error= 0;
117 PCRE2_SIZE error_offset= 0;
118 tab_re= pcre2_compile(reinterpret_cast<PCRE2_SPTR>(_tab_regex.c_str()),
119 PCRE2_ZERO_TERMINATED,
120 0,
121 &error,
122 &error_offset,
123 NULL);
124 }
125
126 pthread_mutex_init(&sch_vector_lock, NULL);
127 pthread_mutex_init(&tab_vector_lock, NULL);
128 pthread_mutex_init(&sysvar_sch_lock, NULL);
129 pthread_mutex_init(&sysvar_tab_lock, NULL);
130}
131
133{
134 if (sch_re)
135 {
136 pcre2_code_free(sch_re);
137 }
138 if (tab_re)
139 {
140 pcre2_code_free(tab_re);
141 }
142
143 pthread_mutex_destroy(&sch_vector_lock);
144 pthread_mutex_destroy(&tab_vector_lock);
145 pthread_mutex_destroy(&sysvar_sch_lock);
146 pthread_mutex_destroy(&sysvar_tab_lock);
147
148}
149
150void FilteredReplicator::parseStatementTableMetadata(const message::Statement &in_statement,
151 string &in_schema_name,
152 string &in_table_name) const
153{
154 switch (in_statement.type())
155 {
156 case message::Statement::INSERT:
157 {
158 const message::TableMetadata &metadata= in_statement.insert_header().table_metadata();
159 in_schema_name= metadata.schema_name();
160 in_table_name= metadata.table_name();
161 break;
162 }
163 case message::Statement::UPDATE:
164 {
165 const message::TableMetadata &metadata= in_statement.update_header().table_metadata();
166 in_schema_name= metadata.schema_name();
167 in_table_name= metadata.table_name();
168 break;
169 }
170 case message::Statement::DELETE:
171 {
172 const message::TableMetadata &metadata= in_statement.delete_header().table_metadata();
173 in_schema_name= metadata.schema_name();
174 in_table_name= metadata.table_name();
175 break;
176 }
177 case message::Statement::CREATE_SCHEMA:
178 {
179 in_schema_name= in_statement.create_schema_statement().schema().name();
180 in_table_name.clear();
181 break;
182 }
183 case message::Statement::ALTER_SCHEMA:
184 {
185 in_schema_name= in_statement.alter_schema_statement().after().name();
186 in_table_name.clear();
187 break;
188 }
189 case message::Statement::DROP_SCHEMA:
190 {
191 in_schema_name= in_statement.drop_schema_statement().schema_name();
192 in_table_name.clear();
193 break;
194 }
195 case message::Statement::CREATE_TABLE:
196 {
197 in_schema_name= in_statement.create_table_statement().table().schema();
198 in_table_name= in_statement.create_table_statement().table().name();
199 break;
200 }
201 case message::Statement::ALTER_TABLE:
202 {
203 in_schema_name= in_statement.alter_table_statement().after().schema();
204 in_table_name= in_statement.alter_table_statement().after().name();
205 break;
206 }
207 case message::Statement::DROP_TABLE:
208 {
209 const message::TableMetadata &metadata= in_statement.drop_table_statement().table_metadata();
210 in_schema_name= metadata.schema_name();
211 in_table_name= metadata.table_name();
212 break;
213 }
214 default:
215 {
216 /* All other types have no schema and table information */
217 in_schema_name.clear();
218 in_table_name.clear();
219 break;
220 }
221 }
222}
223
224plugin::ReplicationReturnCode
226 Session &in_session,
227 message::Transaction &to_replicate)
228{
229 string schema_name;
230 string table_name;
231
232 size_t num_statements= to_replicate.statement_size();
233
234 /*
235 * We build a new transaction message containing only Statement
236 * messages that have not been filtered.
237 *
238 * @todo A more efficient method would be to rework the pointers
239 * that the to_replicate.statement() vector contains and remove
240 * the statement pointers that are filtered...
241 */
242 message::Transaction filtered_transaction;
243
244 for (size_t x= 0; x < num_statements; ++x)
245 {
246 schema_name.clear();
247 table_name.clear();
248
249 const message::Statement &statement= to_replicate.statement(x);
250
251 /*
252 * First, we check to see if the command consists of raw SQL. If so,
253 * we need to parse this SQL and determine whether to filter the event
254 * based on the information we obtain from the parsed SQL.
255 * If not raw SQL, check if this event should be filtered or not
256 * based on the schema and table names in the command message.
257 *
258 * The schema and table names are stored in TableMetadata headers
259 * for most types of Statement messages.
260 */
261 if (statement.type() == message::Statement::RAW_SQL)
262 {
263 parseQuery(statement.sql(), schema_name, table_name);
264 }
265 else
266 {
267 parseStatementTableMetadata(statement, schema_name, table_name);
268 }
269
270 /*
271 * Convert the schema name and table name strings to lowercase so that it
272 * does not matter what case the table or schema name was specified in. We
273 * also keep all entries in the vectors of schemas and tables to filter in
274 * lowercase.
275 */
276 boost::to_lower(schema_name);
277 boost::to_lower(table_name);
278
279 if (! isSchemaFiltered(schema_name) && ! isTableFiltered(table_name))
280 {
281 message::Statement *s= filtered_transaction.add_statement();
282 *s= statement; /* copy contruct */
283 }
284 }
285
286 if (filtered_transaction.statement_size() > 0)
287 {
288
289 /*
290 * We can now simply call the applier's apply() method, passing
291 * along the supplied command.
292 */
293 message::TransactionContext *tc= filtered_transaction.mutable_transaction_context();
294 *tc= to_replicate.transaction_context(); /* copy construct */
295 return in_applier->apply(in_session, filtered_transaction);
296 }
297 return plugin::SUCCESS;
298}
299
301 std::vector<string> &filter)
302{
303 /*
304 * Convert the input string to lowercase so that all entries in the vector
305 * will be in lowercase.
306 */
307 boost::to_lower(input);
308 string::size_type last_pos= input.find_first_not_of(',', 0);
309 string::size_type pos= input.find_first_of(',', last_pos);
310
311 while (pos != string::npos || last_pos != string::npos)
312 {
313 filter.push_back(input.substr(last_pos, pos - last_pos));
314 last_pos= input.find_first_not_of(',', pos);
315 pos= input.find_first_of(',', last_pos);
316 }
317}
318
319bool FilteredReplicator::isSchemaFiltered(const string &schema_name)
320{
321 pthread_mutex_lock(&sch_vector_lock);
322 std::vector<string>::iterator it= find(schemas_to_filter.begin(), schemas_to_filter.end(), schema_name);
323 if (it != schemas_to_filter.end())
324 {
325 pthread_mutex_unlock(&sch_vector_lock);
326 return true;
327 }
328 pthread_mutex_unlock(&sch_vector_lock);
329
330 /*
331 * If regular expression matching is enabled for schemas to filter, then
332 * we check to see if this schema name matches the regular expression that
333 * has been specified.
334 */
335 if (not _sch_regex.empty() && sch_re)
336 {
337 pcre2_match_data *match_data= pcre2_match_data_create_from_pattern(sch_re, NULL);
338 if (match_data == NULL)
339 return false;
340 int32_t result= pcre2_match(sch_re,
341 reinterpret_cast<PCRE2_SPTR>(schema_name.c_str()),
342 schema_name.length(), 0, 0, match_data, NULL);
343 pcre2_match_data_free(match_data);
344 if (result >= 0)
345 return true;
346 }
347
348 return false;
349}
350
351bool FilteredReplicator::isTableFiltered(const string &table_name)
352{
353 pthread_mutex_lock(&tab_vector_lock);
354 std::vector<string>::iterator it= find(tables_to_filter.begin(), tables_to_filter.end(), table_name);
355 if (it != tables_to_filter.end())
356 {
357 pthread_mutex_unlock(&tab_vector_lock);
358 return true;
359 }
360 pthread_mutex_unlock(&tab_vector_lock);
361
362 /*
363 * If regular expression matching is enabled for tables to filter, then
364 * we check to see if this table name matches the regular expression that
365 * has been specified.
366 */
367 if (not _tab_regex.empty() && tab_re)
368 {
369 pcre2_match_data *match_data= pcre2_match_data_create_from_pattern(tab_re, NULL);
370 if (match_data == NULL)
371 return false;
372 int32_t result= pcre2_match(tab_re,
373 reinterpret_cast<PCRE2_SPTR>(table_name.c_str()),
374 table_name.length(), 0, 0, match_data, NULL);
375 pcre2_match_data_free(match_data);
376 if (result >= 0)
377 return true;
378 }
379
380 return false;
381}
382
383void FilteredReplicator::parseQuery(const string &sql,
384 string &schema_name,
385 string &table_name)
386{
387 /*
388 * Determine what type of SQL we are dealing with e.g. create table,
389 * drop table, etc.
390 */
391 string::size_type pos= sql.find_first_of(' ', 0);
392 string type= sql.substr(0, pos);
393
394 /*
395 * Convert the type string to uppercase here so that it doesn't
396 * matter what case the user entered the statement in.
397 */
398 boost::to_upper(type);
399
400 if (type.compare("DROP") == 0)
401 {
402 /*
403 * The schema and table name can be either the third word
404 * or the fifth word in a DROP TABLE statement...so we extract
405 * the third word from the SQL and see whether it is and IF or
406 * not.
407 */
408 pos= sql.find_first_of(' ', 11);
409 string cmp_str= sql.substr(11, pos - 11);
410 string target_name("");
411 if (cmp_str.compare("IF") == 0)
412 {
413 /* the name must be the fifth word */
414 pos= sql.find_first_of(' ', 21);
415 target_name= sql.substr(21, pos - 21);
416 }
417 else
418 {
419 target_name= cmp_str;
420 }
421 /*
422 * Determine whether the name is a concatenation of the schema
423 * name and table name i.e. schema.table or just the table name
424 * on its own.
425 */
426 pos= target_name.find_first_of('.', 0);
427 if (pos != string::npos)
428 {
429 /*
430 * There is a schema name here...
431 */
432 schema_name= target_name.substr(0, pos);
433 /*
434 * The rest of the name string is the table name.
435 */
436 table_name= target_name.substr(pos + 1);
437 }
438 else
439 {
440 table_name= target_name;
441 }
442 }
443 else if (type.compare("CREATE") == 0)
444 {
445 /*
446 * The schema and table name are always the third word
447 * in a CREATE TABLE statement...always (unless there is
448 * some crazy syntax I am unaware of).
449 */
450 pos= sql.find_first_of(' ', 13);
451 string target_name= sql.substr(13, pos - 13);
452 /*
453 * Determine whether the name is a concatenation of the schema
454 * name and table name i.e. schema.table or just the table name
455 * on its own.
456 */
457 pos= target_name.find_first_of('.', 0);
458 if (pos != string::npos)
459 {
460 /*
461 * There is a schema name here...
462 */
463 schema_name= target_name.substr(0, pos);
464 /*
465 * The rest of the name string is the table name.
466 */
467 table_name= target_name.substr(pos + 1);
468 }
469 else
470 {
471 table_name= target_name;
472 }
473 }
474 else
475 {
476 /* we only deal with DROP and CREATE table for the moment */
477 return;
478 }
479}
480
481void FilteredReplicator::setSchemaFilter(const string &input)
482{
483 pthread_mutex_lock(&sch_vector_lock);
484 pthread_mutex_lock(&sysvar_sch_lock);
485 _sch_filter= input;
486 schemas_to_filter.clear();
487 populateFilter(_sch_filter, schemas_to_filter);
488 pthread_mutex_unlock(&sysvar_sch_lock);
489 pthread_mutex_unlock(&sch_vector_lock);
490}
491
492void FilteredReplicator::setTableFilter(const string &input)
493{
494 pthread_mutex_lock(&tab_vector_lock);
495 pthread_mutex_lock(&sysvar_tab_lock);
496 _tab_filter= input;
497 tables_to_filter.clear();
498 populateFilter(_tab_filter, tables_to_filter);
499 pthread_mutex_unlock(&sysvar_tab_lock);
500 pthread_mutex_unlock(&tab_vector_lock);
501}
502
503static FilteredReplicator *filtered_replicator= NULL; /* The singleton replicator */
504
505static int filtered_schemas_validate(Session*, set_var *var)
506{
507 const char *input= var->value->str_value.ptr();
508 if (input && filtered_replicator)
509 {
510 filtered_replicator->setSchemaFilter(input);
511 return 0;
512 }
513 return 1;
514}
515
516
517static int filtered_tables_validate(Session*, set_var *var)
518{
519 const char *input= var->value->str_value.ptr();
520 if (input && filtered_replicator)
521 {
522 filtered_replicator->setTableFilter(input);
523 return 0;
524 }
525 return 1;
526}
527
528
529static int init(module::Context &context)
530{
531 const module::option_map &vm= context.getOptions();
532
533 filtered_replicator= new FilteredReplicator("filtered_replicator",
534 vm["filteredschemas"].as<string>(),
535 vm["filteredtables"].as<string>(),
536 vm["schemaregex"].as<string>(),
537 vm["tableregex"].as<string>());
538
539 context.add(filtered_replicator);
540 context.registerVariable(new sys_var_std_string("filteredschemas",
541 sysvar_filtered_replicator_sch_filters,
542 filtered_schemas_validate));
543 context.registerVariable(new sys_var_std_string("filteredtables",
544 sysvar_filtered_replicator_tab_filters,
545 filtered_tables_validate));
546
547 context.registerVariable(new sys_var_const_string_val("schemaregex",
548 vm["schemaregex"].as<string>()));
549 context.registerVariable(new sys_var_const_string_val("tableregex",
550 vm["tableregex"].as<string>()));
551
552 return 0;
553}
554
555static void init_options(drizzled::module::option_context &context)
556{
557 context("filteredschemas",
558 po::value<string>(&sysvar_filtered_replicator_sch_filters)->default_value(""),
559 N_("Comma-separated list of schemas to exclude"));
560 context("filteredtables",
561 po::value<string>(&sysvar_filtered_replicator_tab_filters)->default_value(""),
562 N_("Comma-separated list of tables to exclude"));
563 context("schemaregex",
564 po::value<string>()->default_value(""),
565 N_("Regular expression to apply to schemas to exclude"));
566 context("tableregex",
567 po::value<string>()->default_value(""),
568 N_("Regular expression to apply to tables to exclude"));
569}
570
571} /* namespace drizzle_plugin */
572
573DRIZZLE_DECLARE_PLUGIN
574{
575 DRIZZLE_VERSION_ID,
576 "filtered_replicator",
577 "0.2",
578 "Padraig O Sullivan",
579 N_("Replicates events filtered by schema or table name"),
580 PLUGIN_LICENSE_GPL,
581 drizzle_plugin::init,
582 NULL,
583 drizzle_plugin::init_options
584}
585DRIZZLE_DECLARE_PLUGIN_END;
void populateFilter(std::string input, std::vector< std::string > &filter)
bool isTableFiltered(const std::string &table_name)
void setTableFilter(const std::string &input)
void parseStatementTableMetadata(const drizzled::message::Statement &in_statement, std::string &in_schema_name, std::string &in_table_name) const
drizzled::plugin::ReplicationReturnCode replicate(drizzled::plugin::TransactionApplier *in_applier, drizzled::Session &in_session, drizzled::message::Transaction &to_replicate)
void parseQuery(const std::string &sql, std::string &schema_name, std::string &table_name)
bool isSchemaFiltered(const std::string &schema_name)
void setSchemaFilter(const std::string &input)
virtual ReplicationReturnCode apply(Session &in_session, const message::Transaction &to_apply)=0
if(!referred_tab||referred_tab->join !=join)
Convert from decimal to binary representation.
Definition common_fwd.h:215
TODO: Rename this file - func.h is stupid.
STL namespace.
An Proxy Wrapper around boost::program_options::variables_map.