Merge lp:~vlad-lesin/percona-playback/query-log-from-stdin into lp:percona-playback

Proposed by Vlad Lesin
Status: Merged
Approved by: Stewart Smith
Approved revision: 165
Merged at revision: 167
Proposed branch: lp:~vlad-lesin/percona-playback/query-log-from-stdin
Merge into: lp:percona-playback
Diff against target: 221 lines (+125/-16)
3 files modified
Makefile.am (+5/-0)
percona_playback/query_log/query_log.cc (+43/-16)
percona_playback/test/basic-stdin.cc (+77/-0)
To merge this branch: bzr merge lp:~vlad-lesin/percona-playback/query-log-from-stdin
Reviewer Review Type Date Requested Status
Stewart Smith (community) Approve
Review via email: mp+134376@code.launchpad.net

Description of the change

The new option --query-log-stdin is added in query_log plugin to read
query log from stdin.

To post a comment you must log in.
Revision history for this message
Stewart Smith (stewart) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'Makefile.am'
--- Makefile.am 2012-09-17 05:54:15 +0000
+++ Makefile.am 2012-11-14 22:05:23 +0000
@@ -83,6 +83,7 @@
8383
84check_PROGRAMS += \84check_PROGRAMS += \
85 percona_playback/test/basic \85 percona_playback/test/basic \
86 percona_playback/test/basic-stdin \
86 percona_playback/test/basic-multiline \87 percona_playback/test/basic-multiline \
87 percona_playback/test/basic-queue-depth \88 percona_playback/test/basic-queue-depth \
88 percona_playback/test/crashme-slow \89 percona_playback/test/crashme-slow \
@@ -97,6 +98,7 @@
97 percona_playback/test/help98 percona_playback/test/help
9899
99percona_playback_test_basic_CXXFLAGS= $(AM_CXXFLAGS) -DSRCDIR=\"${srcdir}\"100percona_playback_test_basic_CXXFLAGS= $(AM_CXXFLAGS) -DSRCDIR=\"${srcdir}\"
101percona_playback_test_basic_stdin_CXXFLAGS= $(AM_CXXFLAGS) -DSRCDIR=\"${srcdir}\"
100percona_playback_test_basic_multiline_CXXFLAGS= $(AM_CXXFLAGS) -DSRCDIR=\"${srcdir}\"102percona_playback_test_basic_multiline_CXXFLAGS= $(AM_CXXFLAGS) -DSRCDIR=\"${srcdir}\"
101percona_playback_test_basic_queue_depth_CXXFLAGS= $(AM_CXXFLAGS) -DSRCDIR=\"${srcdir}\"103percona_playback_test_basic_queue_depth_CXXFLAGS= $(AM_CXXFLAGS) -DSRCDIR=\"${srcdir}\"
102percona_playback_test_crashme_slow_CXXFLAGS= $(AM_CXXFLAGS) -DSRCDIR=\"${srcdir}\"104percona_playback_test_crashme_slow_CXXFLAGS= $(AM_CXXFLAGS) -DSRCDIR=\"${srcdir}\"
@@ -115,6 +117,9 @@
115percona_playback_test_basic_SOURCES= percona_playback/test/basic.cc117percona_playback_test_basic_SOURCES= percona_playback/test/basic.cc
116percona_playback_test_basic_LDADD= $(LDADD) $(MYSQL_LIBS)118percona_playback_test_basic_LDADD= $(LDADD) $(MYSQL_LIBS)
117119
120percona_playback_test_basic_stdin_SOURCES= percona_playback/test/basic-stdin.cc
121percona_playback_test_basic_stdin_LDADD= $(LDADD) $(MYSQL_LIBS)
122
118percona_playback_test_basic_multiline_SOURCES= percona_playback/test/basic-multiline.cc123percona_playback_test_basic_multiline_SOURCES= percona_playback/test/basic-multiline.cc
119percona_playback_test_basic_multiline_LDADD= $(LDADD) $(MYSQL_LIBS)124percona_playback_test_basic_multiline_LDADD= $(LDADD) $(MYSQL_LIBS)
120125
121126
=== modified file 'percona_playback/query_log/query_log.cc'
--- percona_playback/query_log/query_log.cc 2012-09-17 05:54:15 +0000
+++ percona_playback/query_log/query_log.cc 2012-11-14 22:05:23 +0000
@@ -336,38 +336,29 @@
336 r->n_queries= queries;336 r->n_queries= queries;
337}337}
338338
339int run_query_log(const std::string &log_file, unsigned int run_count, struct percona_playback_run_result *r)
340{
341 FILE* input_file = fopen(log_file.c_str(),"r");
342 if (input_file == NULL)
343 return -1;
344
345 boost::thread log_reader_thread(LogReaderThread,input_file, run_count, r);
346
347 log_reader_thread.join();
348 fclose(input_file);
349
350 return 0;
351}
352
353class QueryLogPlugin : public percona_playback::InputPlugin339class QueryLogPlugin : public percona_playback::InputPlugin
354{340{
355private:341private:
356 po::options_description options;342 po::options_description options;
357 std::string file_name;343 std::string file_name;
358 unsigned int read_count;344 unsigned int read_count;
345 bool std_in;
359346
360public:347public:
361 QueryLogPlugin(const std::string &_name) :348 QueryLogPlugin(const std::string &_name) :
362 InputPlugin(_name),349 InputPlugin(_name),
363 options(_("Query Log Options")),350 options(_("Query Log Options")),
364 read_count(1)351 read_count(1),
352 std_in(false)
365 {};353 {};
366354
367 virtual boost::program_options::options_description* getProgramOptions() {355 virtual boost::program_options::options_description* getProgramOptions() {
368 options.add_options()356 options.add_options()
369 ("query-log-file",357 ("query-log-file",
370 po::value<std::string>(), _("Query log file"))358 po::value<std::string>(), _("Query log file"))
359 ("query-log-stdin",
360 po::value<bool>()->default_value(false)->zero_tokens(),
361 _("Read query log from stdin"))
371/* Disabled for 0.3 until we have something more universal.362/* Disabled for 0.3 until we have something more universal.
372 ("query-log-read-count",363 ("query-log-read-count",
373 po::value<unsigned int>(&read_count)->default_value(1),364 po::value<unsigned int>(&read_count)->default_value(1),
@@ -396,6 +387,7 @@
396 {387 {
397 if (!active &&388 if (!active &&
398 (vm.count("query-log-file") ||389 (vm.count("query-log-file") ||
390 !vm["query-log-stdin"].defaulted() ||
399// !vm["query-log-read-count"].defaulted() ||391// !vm["query-log-read-count"].defaulted() ||
400 !vm["query-log-preserve-query-time"].defaulted() ||392 !vm["query-log-preserve-query-time"].defaulted() ||
401 !vm["query-log-set-timestamp"].defaulted()))393 !vm["query-log-set-timestamp"].defaulted()))
@@ -409,8 +401,19 @@
409 if (!active)401 if (!active)
410 return 0;402 return 0;
411403
404 if (vm.count("query-log-file") && vm["query-log-stdin"].as<bool>())
405 {
406 fprintf(stderr, _(("The options --query-log-file and --query-log-stdin "
407 "can not be used together\n")));
408 return -1;
409 }
410
412 if (vm.count("query-log-file"))411 if (vm.count("query-log-file"))
413 file_name= vm["query-log-file"].as<std::string>();412 file_name= vm["query-log-file"].as<std::string>();
413 else if (vm["query-log-stdin"].as<bool>())
414 {
415 std_in = true;
416 }
414 else417 else
415 {418 {
416 fprintf(stderr, _("ERROR: --query-log-file is a required option.\n"));419 fprintf(stderr, _("ERROR: --query-log-file is a required option.\n"));
@@ -422,7 +425,31 @@
422425
423 virtual void run(percona_playback_run_result &result)426 virtual void run(percona_playback_run_result &result)
424 {427 {
425 run_query_log(file_name, read_count, &result);428 FILE* input_file;
429
430 if (std_in)
431 {
432 input_file = stdin;
433 }
434 else
435 {
436 input_file = fopen(file_name.c_str(),"r");
437 if (input_file == NULL)
438 {
439 fprintf(stderr,
440 _("ERROR: Error opening file '%s': %s"),
441 file_name.c_str(), strerror(errno));
442 return;
443 }
444 }
445
446 boost::thread log_reader_thread(LogReaderThread,
447 input_file,
448 read_count,
449 &result);
450
451 log_reader_thread.join();
452 fclose(input_file);
426 }453 }
427};454};
428455
429456
=== added file 'percona_playback/test/basic-stdin.cc'
--- percona_playback/test/basic-stdin.cc 1970-01-01 00:00:00 +0000
+++ percona_playback/test/basic-stdin.cc 2012-11-14 22:05:23 +0000
@@ -0,0 +1,77 @@
1/* BEGIN LICENSE
2 * Copyright (C) 2011-2012 Percona Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify it under
5 * the terms of the GNU General Public License version 3, as published by the
6 * Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranties of
10 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11 * PURPOSE. See the GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program. If not, see <http://www.gnu.org/licenses/>.
15 * END LICENSE */
16
17#include "config.h"
18
19#include <iostream>
20#include <cstdio>
21#include <string>
22#include <cstring>
23#include <assert.h>
24#include <unistd.h>
25
26#include <sys/types.h>
27#include <sys/stat.h>
28#include <fcntl.h>
29#include <errno.h>
30
31#include <percona_playback/percona_playback.h>
32
33/**
34 * @TODO Actually write a real test suite here
35 */
36int main(int argc, char **argv)
37{
38 (void)argc; (void)argv;
39
40 fprintf(stderr, "Working dir: %s\n\n", get_current_dir_name());
41
42 percona_playback_st *the_percona_playback= percona_playback_create("test_Percona Playback");
43 assert(the_percona_playback);
44
45 char dbplugin_opt[] = "--db-plugin=null";
46 char stdin_opt[] = "--query-log-stdin";
47
48 char *dbplugin_argv[3];
49 dbplugin_argv[0]= argv[0];
50 dbplugin_argv[1]= dbplugin_opt;
51 dbplugin_argv[2]= stdin_opt;
52
53 const char *query_log = SRCDIR"/percona_playback/test/basic-slow.log";
54 int fd = open(query_log, O_RDONLY);
55 if (fd < 0)
56 {
57 fprintf(stderr, "Error: open file '%s' error: %s\n", query_log, strerror(errno));
58 return -1;
59 }
60
61 if (dup2(fd, STDIN_FILENO) < 0)
62 {
63 fprintf(stderr, "Error: can't dup2 to stdin: %s\n", strerror(errno));
64 return -1;
65 }
66
67 assert(0 == percona_playback_argv(the_percona_playback, 3, dbplugin_argv));
68
69 struct percona_playback_run_result *r= percona_playback_run(the_percona_playback);
70
71 assert(r->err == 0);
72 assert(r->n_queries == 17);
73 assert(r->n_log_entries = 17);
74
75 percona_playback_destroy(&the_percona_playback);
76 return r->err;
77}

Subscribers

People subscribed via source and target branches

to all changes: