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

Proposed by Vlad Lesin on 2012-11-14
Status: Merged
Approved by: Stewart Smith on 2012-11-15
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) 2012-11-14 Approve on 2012-11-15
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.
Stewart Smith (stewart) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'Makefile.am'
2--- Makefile.am 2012-09-17 05:54:15 +0000
3+++ Makefile.am 2012-11-14 22:05:23 +0000
4@@ -83,6 +83,7 @@
5
6 check_PROGRAMS += \
7 percona_playback/test/basic \
8+ percona_playback/test/basic-stdin \
9 percona_playback/test/basic-multiline \
10 percona_playback/test/basic-queue-depth \
11 percona_playback/test/crashme-slow \
12@@ -97,6 +98,7 @@
13 percona_playback/test/help
14
15 percona_playback_test_basic_CXXFLAGS= $(AM_CXXFLAGS) -DSRCDIR=\"${srcdir}\"
16+percona_playback_test_basic_stdin_CXXFLAGS= $(AM_CXXFLAGS) -DSRCDIR=\"${srcdir}\"
17 percona_playback_test_basic_multiline_CXXFLAGS= $(AM_CXXFLAGS) -DSRCDIR=\"${srcdir}\"
18 percona_playback_test_basic_queue_depth_CXXFLAGS= $(AM_CXXFLAGS) -DSRCDIR=\"${srcdir}\"
19 percona_playback_test_crashme_slow_CXXFLAGS= $(AM_CXXFLAGS) -DSRCDIR=\"${srcdir}\"
20@@ -115,6 +117,9 @@
21 percona_playback_test_basic_SOURCES= percona_playback/test/basic.cc
22 percona_playback_test_basic_LDADD= $(LDADD) $(MYSQL_LIBS)
23
24+percona_playback_test_basic_stdin_SOURCES= percona_playback/test/basic-stdin.cc
25+percona_playback_test_basic_stdin_LDADD= $(LDADD) $(MYSQL_LIBS)
26+
27 percona_playback_test_basic_multiline_SOURCES= percona_playback/test/basic-multiline.cc
28 percona_playback_test_basic_multiline_LDADD= $(LDADD) $(MYSQL_LIBS)
29
30
31=== modified file 'percona_playback/query_log/query_log.cc'
32--- percona_playback/query_log/query_log.cc 2012-09-17 05:54:15 +0000
33+++ percona_playback/query_log/query_log.cc 2012-11-14 22:05:23 +0000
34@@ -336,38 +336,29 @@
35 r->n_queries= queries;
36 }
37
38-int run_query_log(const std::string &log_file, unsigned int run_count, struct percona_playback_run_result *r)
39-{
40- FILE* input_file = fopen(log_file.c_str(),"r");
41- if (input_file == NULL)
42- return -1;
43-
44- boost::thread log_reader_thread(LogReaderThread,input_file, run_count, r);
45-
46- log_reader_thread.join();
47- fclose(input_file);
48-
49- return 0;
50-}
51-
52 class QueryLogPlugin : public percona_playback::InputPlugin
53 {
54 private:
55 po::options_description options;
56 std::string file_name;
57 unsigned int read_count;
58+ bool std_in;
59
60 public:
61 QueryLogPlugin(const std::string &_name) :
62 InputPlugin(_name),
63 options(_("Query Log Options")),
64- read_count(1)
65+ read_count(1),
66+ std_in(false)
67 {};
68
69 virtual boost::program_options::options_description* getProgramOptions() {
70 options.add_options()
71 ("query-log-file",
72 po::value<std::string>(), _("Query log file"))
73+ ("query-log-stdin",
74+ po::value<bool>()->default_value(false)->zero_tokens(),
75+ _("Read query log from stdin"))
76 /* Disabled for 0.3 until we have something more universal.
77 ("query-log-read-count",
78 po::value<unsigned int>(&read_count)->default_value(1),
79@@ -396,6 +387,7 @@
80 {
81 if (!active &&
82 (vm.count("query-log-file") ||
83+ !vm["query-log-stdin"].defaulted() ||
84 // !vm["query-log-read-count"].defaulted() ||
85 !vm["query-log-preserve-query-time"].defaulted() ||
86 !vm["query-log-set-timestamp"].defaulted()))
87@@ -409,8 +401,19 @@
88 if (!active)
89 return 0;
90
91+ if (vm.count("query-log-file") && vm["query-log-stdin"].as<bool>())
92+ {
93+ fprintf(stderr, _(("The options --query-log-file and --query-log-stdin "
94+ "can not be used together\n")));
95+ return -1;
96+ }
97+
98 if (vm.count("query-log-file"))
99 file_name= vm["query-log-file"].as<std::string>();
100+ else if (vm["query-log-stdin"].as<bool>())
101+ {
102+ std_in = true;
103+ }
104 else
105 {
106 fprintf(stderr, _("ERROR: --query-log-file is a required option.\n"));
107@@ -422,7 +425,31 @@
108
109 virtual void run(percona_playback_run_result &result)
110 {
111- run_query_log(file_name, read_count, &result);
112+ FILE* input_file;
113+
114+ if (std_in)
115+ {
116+ input_file = stdin;
117+ }
118+ else
119+ {
120+ input_file = fopen(file_name.c_str(),"r");
121+ if (input_file == NULL)
122+ {
123+ fprintf(stderr,
124+ _("ERROR: Error opening file '%s': %s"),
125+ file_name.c_str(), strerror(errno));
126+ return;
127+ }
128+ }
129+
130+ boost::thread log_reader_thread(LogReaderThread,
131+ input_file,
132+ read_count,
133+ &result);
134+
135+ log_reader_thread.join();
136+ fclose(input_file);
137 }
138 };
139
140
141=== added file 'percona_playback/test/basic-stdin.cc'
142--- percona_playback/test/basic-stdin.cc 1970-01-01 00:00:00 +0000
143+++ percona_playback/test/basic-stdin.cc 2012-11-14 22:05:23 +0000
144@@ -0,0 +1,77 @@
145+/* BEGIN LICENSE
146+ * Copyright (C) 2011-2012 Percona Inc.
147+ *
148+ * This program is free software: you can redistribute it and/or modify it under
149+ * the terms of the GNU General Public License version 3, as published by the
150+ * Free Software Foundation.
151+ *
152+ * This program is distributed in the hope that it will be useful, but
153+ * WITHOUT ANY WARRANTY; without even the implied warranties of
154+ * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
155+ * PURPOSE. See the GNU General Public License for more details.
156+ *
157+ * You should have received a copy of the GNU General Public License along
158+ * with this program. If not, see <http://www.gnu.org/licenses/>.
159+ * END LICENSE */
160+
161+#include "config.h"
162+
163+#include <iostream>
164+#include <cstdio>
165+#include <string>
166+#include <cstring>
167+#include <assert.h>
168+#include <unistd.h>
169+
170+#include <sys/types.h>
171+#include <sys/stat.h>
172+#include <fcntl.h>
173+#include <errno.h>
174+
175+#include <percona_playback/percona_playback.h>
176+
177+/**
178+ * @TODO Actually write a real test suite here
179+ */
180+int main(int argc, char **argv)
181+{
182+ (void)argc; (void)argv;
183+
184+ fprintf(stderr, "Working dir: %s\n\n", get_current_dir_name());
185+
186+ percona_playback_st *the_percona_playback= percona_playback_create("test_Percona Playback");
187+ assert(the_percona_playback);
188+
189+ char dbplugin_opt[] = "--db-plugin=null";
190+ char stdin_opt[] = "--query-log-stdin";
191+
192+ char *dbplugin_argv[3];
193+ dbplugin_argv[0]= argv[0];
194+ dbplugin_argv[1]= dbplugin_opt;
195+ dbplugin_argv[2]= stdin_opt;
196+
197+ const char *query_log = SRCDIR"/percona_playback/test/basic-slow.log";
198+ int fd = open(query_log, O_RDONLY);
199+ if (fd < 0)
200+ {
201+ fprintf(stderr, "Error: open file '%s' error: %s\n", query_log, strerror(errno));
202+ return -1;
203+ }
204+
205+ if (dup2(fd, STDIN_FILENO) < 0)
206+ {
207+ fprintf(stderr, "Error: can't dup2 to stdin: %s\n", strerror(errno));
208+ return -1;
209+ }
210+
211+ assert(0 == percona_playback_argv(the_percona_playback, 3, dbplugin_argv));
212+
213+ struct percona_playback_run_result *r= percona_playback_run(the_percona_playback);
214+
215+ assert(r->err == 0);
216+ assert(r->n_queries == 17);
217+ assert(r->n_log_entries = 17);
218+
219+ percona_playback_destroy(&the_percona_playback);
220+ return r->err;
221+}

Subscribers

People subscribed via source and target branches

to all changes: