-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathmain.c
413 lines (366 loc) · 10.1 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#include "pgreplay.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#ifdef WINDOWS
# include <windows.h>
#endif
/* from getopt */
extern char *optarg;
int debug_level = 0;
/* destination of statistics output */
FILE *sf;
/* if 1, backslash will escape the following single quote in string literal */
int backslash_quote = 0;
/* if 1, replay will skip idle intervals instead of sleeping */
int jump_enabled = 0;
/* extra connect options specified with the -X option */
char *extra_connstr;
/* wrapper for setenv, returns 0 on success and -1 on error */
static int do_setenv(const char *name, const char *value) {
int rc;
#ifdef WINDOWS
if (0 == SetEnvironmentVariable(name, value)) {
win_perror("Error setting environment variable", 0);
rc = -1;
} else {
rc = 0;
}
#else
if (-1 == (rc = setenv(name, value, 1))) {
fprintf(stderr, "Error setting environment variable\n");
}
#endif
return rc;
}
static void version(FILE *f) {
fprintf(f, "pgreplay %s\n", VERSION);
}
static void help(FILE *f) {
fprintf(f, "\n");
version(f);
fprintf(f, "==============\n");
fprintf(f, "\nUsage: pgreplay [<parse options>] [<replay options>] [<infile>]\n");
fprintf(f, " pgreplay -f [<parse options>] [-o <outfile>] [<infile>]\n");
fprintf(f, " pgreplay -r [<replay options>] [<infile>]\n\n");
fprintf(f, " The first form parses a PostgreSQL log file and replays the\n");
fprintf(f, "statements against a database.\n");
fprintf(f, " The second form parses a PostgreSQL log file and writes the\n");
fprintf(f, "contents to a \"replay file\" that can be replayed with -r.\n");
fprintf(f, " The third form replays a file generated with -f.\n\n");
fprintf(f, "Parse options:\n");
fprintf(f, " -c (assume CSV logfile)\n");
fprintf(f, " -b <timestamp> (start time for parsing logfile)\n");
fprintf(f, " -e <timestamp> (end time for parsing logfile)\n");
fprintf(f, " -q ( \\' in string literal is a single quote)\n\n");
fprintf(f, " -D <database> (database name to use as filter for parsing logfile)\n");
fprintf(f, " -U <username> (username to use as filter for parsing logfile)\n");
fprintf(f, "Replay options:\n");
fprintf(f, " -h <hostname>\n");
fprintf(f, " -p <port>\n");
fprintf(f, " -W <password> (must be the same for all users)\n");
fprintf(f, " -s <factor> (speed factor for replay)\n");
fprintf(f, " -E <encoding> (server encoding)\n");
fprintf(f, " -j (skip idle time during replay)\n");
fprintf(f, " -X <options> (extra libpq connect options)\n\n");
fprintf(f, " -n (dry-run, will replay file without running queries)\n\n");
fprintf(f, "Debugging:\n");
fprintf(f, " -d <level> (level between 1 and 3)\n");
fprintf(f, " -v (prints version and exits)\n");
}
int main(int argc, char **argv) {
int arg, parse_only = 0, replay_only = 0, port = -1, csv = 0,
parse_opt = 0, replay_opt = 0, rc = 0, dry_run = 0;
double factor = 1.0;
char *host = NULL, *encoding = NULL, *endptr, *passwd = NULL,
*outfilename = NULL, *infilename = NULL,
*database_only = NULL, *username_only = NULL,
start_time[24] = { '\0' }, end_time[24] = { '\0' };
const char *errmsg;
unsigned long portnr = 0l, debug = 0l, length;
replay_item_provider *provider;
replay_item_provider_init *provider_init;
replay_item_provider_finish *provider_finish;
replay_item_consumer *consumer;
replay_item_consumer_init *consumer_init;
replay_item_consumer_finish *consumer_finish;
replay_item *item = NULL;
/* initialize errno to avoid bogus error messages */
errno = 0;
/* parse arguments */
opterr = 0;
while (-1 != (arg = getopt(argc, argv, "vfro:h:p:W:s:E:d:cb:e:qjnX:D:U:"))) {
switch (arg) {
case 'v':
version(stdout);
return 0;
break;
case 'f':
parse_only = 1;
if (replay_only) {
fprintf(stderr, "Error: options -p and -r are mutually exclusive\n");
help(stderr);
return 1;
}
break;
case 'r':
replay_only = 1;
if (parse_only) {
fprintf(stderr, "Error: options -p and -r are mutually exclusive\n");
help(stderr);
return 1;
}
break;
case 'o':
outfilename = ('\0' == *optarg) ? NULL : optarg;
break;
case 'h':
replay_opt = 1;
host = ('\0' == *optarg) ? NULL : optarg;
break;
case 'p':
replay_opt = 1;
portnr = strtoul(optarg, &endptr, 0);
if (('\0' == *optarg) || ('\0' != *endptr)) {
fprintf(stderr, "Not a valid port number: \"%s\"\n", optarg);
help(stderr);
return 1;
}
if ((portnr < 1) || (65535 < portnr)) {
fprintf(stderr, "Port number must be between 1 and 65535\n");
help(stderr);
return 1;
}
port = (int)portnr;
break;
case 'W':
replay_opt = 1;
passwd = ('\0' == *optarg) ? NULL : optarg;
break;
case 's':
replay_opt = 1;
factor = strtod(optarg, &endptr);
if (('\0' == *optarg) || ('\0' != *endptr)) {
fprintf(stderr, "Not a valid floating point number: \"%s\"\n", optarg);
help(stderr);
return 1;
}
if (0 != errno) {
perror("Error converting speed factor");
help(stderr);
return 1;
}
if (factor <= 0.0) {
fprintf(stderr, "Factor must be greater than 0\n");
help(stderr);
return 1;
}
break;
case 'E':
replay_opt = 1;
encoding = ('\0' == *optarg) ? NULL : optarg;
break;
case 'd':
debug = strtoul(optarg, &endptr, 0);
if (('\0' == *optarg) || ('\0' != *endptr)) {
fprintf(stderr, "Not a valid debug level: \"%s\"\n", optarg);
help(stderr);
return 1;
}
if ((debug < 0) || (3 < debug)) {
fprintf(stderr, "Debug level must be between 0 and 3\n");
help(stderr);
return 1;
}
debug_level = (int)debug;
break;
case 'c':
parse_opt = 1;
csv = 1;
break;
case 'b':
parse_opt = 1;
if (NULL == (errmsg = parse_time(optarg, NULL))) {
strncpy(start_time, optarg, 23);
} else {
fprintf(stderr, "Error in begin timestamp: %s\n", errmsg);
help(stderr);
return 1;
}
break;
case 'e':
parse_opt = 1;
if (NULL == (errmsg = parse_time(optarg, NULL))) {
strncpy(end_time, optarg, 23);
} else {
fprintf(stderr, "Error in end timestamp: %s\n", errmsg);
help(stderr);
return 1;
}
break;
case 'q':
backslash_quote = 1;
break;
case 'j':
replay_opt = 1;
jump_enabled = 1;
break;
case 'n':
replay_opt = 1;
dry_run = 1;
break;
case 'X':
replay_opt = 1;
extra_connstr = optarg;
break;
case 'D':
parse_opt = 1;
if (NULL == database_only) {
length = strlen(optarg) + 3;
database_only = malloc(length);
if (NULL != database_only)
strcpy(database_only, "\\");
} else {
length = strlen(database_only) + strlen(optarg) + 2;
database_only = realloc(database_only, length);
}
if (NULL == database_only) {
fprintf(stderr, "Cannot allocate %lu bytes of memory\n", length);
return 1;
}
strcat(database_only, optarg);
strcat(database_only, "\\");
break;
case 'U':
parse_opt = 1;
if (NULL == username_only) {
length = strlen(optarg) + 3;
username_only = malloc(length);
if (NULL != username_only)
strcpy(username_only, "\\");
} else {
length = strlen(username_only) + strlen(optarg) + 2;
username_only = realloc(username_only, length);
}
if (NULL == username_only) {
fprintf(stderr, "Cannot allocate %lu bytes of memory\n", length);
return 1;
}
strcat(username_only, optarg);
strcat(username_only, "\\");
break;
case '?':
if (('?' == optopt) || ('h' == optopt)) {
help(stdout);
return 0;
} else {
fprintf(stderr, "Error: unknown option -%c\n", optopt);
help(stderr);
return 1;
}
break;
}
}
if (optind + 1 < argc) {
fprintf(stderr, "More than one argument given\n");
help(stderr);
return 1;
}
if (optind + 1 == argc) {
infilename = argv[optind];
}
if (parse_only && replay_opt) {
fprintf(stderr, "Error: cannot specify replay option with -f\n");
help(stderr);
return 1;
}
if (replay_only && parse_opt) {
fprintf(stderr, "Error: cannot specify parse option with -r\n");
help(stderr);
return 1;
}
if (NULL != outfilename) {
if (! parse_only) {
fprintf(stderr, "Error: option -o is only allowed with -f\n");
help(stderr);
return 1;
}
}
/* set default encoding */
if (NULL != encoding) {
if (-1 == do_setenv("PGCLIENTENCODING", encoding)) {
return 1;
}
}
/* figure out destination for statistics output */
if (parse_only && (NULL == outfilename)) {
sf = stderr; /* because replay file will go to stdout */
} else {
sf = stdout;
}
/* configure main loop */
if (replay_only) {
provider_init = &file_provider_init;
provider = &file_provider;
provider_finish = &file_provider_finish;
} else {
provider_init = &parse_provider_init;
provider = &parse_provider;
provider_finish = &parse_provider_finish;
}
if (parse_only) {
consumer_init = &file_consumer_init;
consumer_finish = &file_consumer_finish;
consumer = &file_consumer;
} else {
consumer_init = &database_consumer_init;
consumer_finish = &database_consumer_finish;
if (0 == dry_run) {
consumer = &database_consumer;
} else {
consumer = &database_consumer_dry_run;
}
}
/* main loop */
if (! (*provider_init)(
infilename,
csv,
(('\0' == start_time[0]) ? NULL : start_time),
(('\0' == end_time[0]) ? NULL : end_time),
database_only,
username_only
))
{
rc = 1;
}
if ((0 == rc) && (*consumer_init)(outfilename, host, port, passwd, factor)) {
/* try to get first item */
if (! (item = (*provider)())) {
rc = 1;
}
} else {
rc = 1;
}
while ((0 == rc) && (end_item != item)) {
switch ((*consumer)(item)) {
case 0: /* item not consumed */
break;
case 1: /* item consumed */
if (! (item = (*provider)())) {
rc = 1;
}
break;
default: /* error occurred */
rc = 1;
}
}
/* no statistics output if there was an error */
if (1 == rc) {
sf = NULL;
}
(*provider_finish)();
(*consumer_finish)(dry_run);
return rc;
}