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
|
#include "pgqd.h"
#include <usual/string.h>
#include <usual/pgutil.h>
#include <ctype.h>
struct MaintOp {
struct List head;
const char *func_name;
const char *func_arg;
};
static struct MaintOp *next_op(struct PgDatabase *db)
{
struct List *el = statlist_pop(&db->maint_op_list);
if (!el)
return NULL;
return container_of(el, struct MaintOp, head);
}
static void free_op(struct MaintOp *op)
{
if (op) {
free(op->func_name);
free(op->func_arg);
free(op);
}
}
void free_maint(struct PgDatabase *db)
{
struct MaintOp *op;
strlist_free(db->maint_item_list);
db->maint_item_list = NULL;
while ((op = next_op(db)) != NULL) {
free_op(op);
}
free_op(db->cur_maint);
db->cur_maint = NULL;
}
static void close_maint(struct PgDatabase *db, double sleep_time)
{
log_debug("%s: close_maint, %f", db->name, sleep_time);
db->maint_state = DB_CLOSED;
pgs_reconnect(db->c_maint, sleep_time);
}
static void run_test_version(struct PgDatabase *db)
{
const char *q = "select 1 from pg_proc p, pg_namespace n"
" where p.pronamespace = n.oid"
" and p.proname = 'maint_operations'"
" and n.nspname = 'pgq'";
log_debug("%s: %s", db->name, q);
pgs_send_query_simple(db->c_maint, q);
db->maint_state = DB_MAINT_TEST_VERSION;
}
static bool has_ops(PGresult *res)
{
if (PQntuples(res) == 1 && atoi(PQgetvalue(res, 0, 0)) == 1)
return true;
return false;
}
static bool fill_op_list(struct PgDatabase *db, PGresult *res)
{
int i;
struct MaintOp *op = NULL;
const char *fname, *farg;
free_maint(db);
for (i = 0; i < PQntuples(res); i++) {
op = calloc(1, sizeof(*op));
if (!op)
return false;
list_init(&op->head);
fname = PQgetvalue(res, i, 0);
farg = NULL;
if (!PQgetisnull(res, i, 1))
farg = PQgetvalue(res, i, 1);
log_debug("load_op: %s / %s", fname, farg ? farg : "NULL");
op->func_name = strdup(fname);
if (!op->func_name)
goto failed;
if (farg) {
op->func_arg = strdup(farg);
if (!op->func_arg)
goto failed;
}
statlist_append(&db->maint_op_list, &op->head);
}
return true;
failed:
free_op(op);
return false;
}
static void run_op_list(struct PgDatabase *db)
{
const char *q = "select func_name, func_arg from pgq.maint_operations()";
log_debug("%s: %s", db->name, q);
pgs_send_query_simple(db->c_maint, q);
db->maint_state = DB_MAINT_LOAD_OPS;
}
static const char *stmt_names[] = {
"vacuum",
"vacuum analyze",
NULL
};
static void run_op(struct PgDatabase *db, PGresult *res)
{
struct MaintOp *op;
char buf[1024];
char namebuf[256];
const char **np;
if (db->cur_maint) {
if (res && PQntuples(res) > 0) {
const char *val = PQgetvalue(res, 0, 0);
if (val && atoi(val)) {
op = db->cur_maint;
goto repeat;
}
}
next:
free_op(db->cur_maint);
db->cur_maint = NULL;
}
op = next_op(db);
if (!op) {
stats.n_maint++;
close_maint(db, cf.maint_period);
return;
}
db->cur_maint = op;
repeat:
/* check if its magic statement */
for (np = stmt_names; *np; np++) {
if (strcasecmp(op->func_name, *np) != 0)
continue;
if (!pg_quote_fqident(namebuf, op->func_arg, sizeof(namebuf))) {
log_error("Bad table name? - %s", op->func_arg);
goto next;
}
/* run as a statement */
snprintf(buf, sizeof(buf), "%s %s", op->func_name, namebuf);
log_debug("%s: [%s]", db->name, buf);
pgs_send_query_simple(db->c_maint, buf);
goto done;
}
/* run as a function */
if (!pg_quote_fqident(namebuf, op->func_name, sizeof(namebuf))) {
log_error("Bad func name? - %s", op->func_name);
goto next;
}
if (op->func_arg) {
snprintf(buf, sizeof(buf), "select %s($1)", namebuf);
log_debug("%s: [%s]", db->name, buf);
pgs_send_query_params(db->c_maint, buf, 1, op->func_arg);
} else {
snprintf(buf, sizeof(buf), "select %s()", namebuf);
log_debug("%s: [%s]", db->name, buf);
pgs_send_query_simple(db->c_maint, buf);
}
done:
db->maint_state = DB_MAINT_OP;
}
static bool fill_items(struct PgDatabase *db, PGresult *res)
{
int i;
if (db->maint_item_list)
strlist_free(db->maint_item_list);
db->maint_item_list = strlist_new(USUAL_ALLOC);
if (!db->maint_item_list)
return false;
for (i = 0; i < PQntuples(res); i++) {
const char *item = PQgetvalue(res, i, 0);
if (item)
if (!strlist_append(db->maint_item_list, item))
return false;
}
return true;
}
static void run_queue_list(struct PgDatabase *db)
{
const char *q = "select queue_name from pgq.get_queue_info()";
log_debug("%s: %s", db->name, q);
pgs_send_query_simple(db->c_maint, q);
db->maint_state = DB_MAINT_LOAD_QUEUES;
}
static void run_vacuum_list(struct PgDatabase *db)
{
const char *q = "select * from pgq.maint_tables_to_vacuum()";
log_debug("%s: %s", db->name, q);
pgs_send_query_simple(db->c_maint, q);
db->maint_state = DB_MAINT_VACUUM_LIST;
}
static void run_rotate_part1(struct PgDatabase *db)
{
const char *q;
const char *qname;
qname = strlist_pop(db->maint_item_list);
q = "select pgq.maint_rotate_tables_step1($1)";
log_debug("%s: %s [%s]", db->name, q, qname);
pgs_send_query_params(db->c_maint, q, 1, qname);
free(qname);
db->maint_state = DB_MAINT_ROT1;
}
static void run_rotate_part2(struct PgDatabase *db)
{
const char *q = "select pgq.maint_rotate_tables_step2()";
log_debug("%s: %s", db->name, q);
pgs_send_query_simple(db->c_maint, q);
db->maint_state = DB_MAINT_ROT2;
}
static void run_vacuum(struct PgDatabase *db)
{
char qbuf[256];
const char *table;
table = strlist_pop(db->maint_item_list);
snprintf(qbuf, sizeof(qbuf), "vacuum %s", table);
log_debug("%s: %s", db->name, qbuf);
pgs_send_query_simple(db->c_maint, qbuf);
free(table);
db->maint_state = DB_MAINT_DO_VACUUM;
}
static void maint_handler(struct PgSocket *s, void *arg, enum PgEvent ev, PGresult *res)
{
struct PgDatabase *db = arg;
switch (ev) {
case PGS_CONNECT_OK:
log_debug("%s: starting maintenance", db->name);
if (db->has_maint_operations)
run_op_list(db);
else
run_test_version(db);
break;
case PGS_RESULT_OK:
if (PQresultStatus(res) != PGRES_TUPLES_OK) {
close_maint(db, 20);
return;
}
switch (db->maint_state) {
case DB_MAINT_TEST_VERSION:
if (has_ops(res)) {
db->has_maint_operations = true;
run_op_list(db);
} else {
run_queue_list(db);
}
break;
case DB_MAINT_LOAD_OPS:
if (!fill_op_list(db, res))
goto mem_err;
case DB_MAINT_OP:
run_op(db, res);
break;
case DB_MAINT_LOAD_QUEUES:
if (!fill_items(db, res))
goto mem_err;
case DB_MAINT_ROT1:
if (!strlist_empty(db->maint_item_list)) {
run_rotate_part1(db);
} else {
run_rotate_part2(db);
}
break;
case DB_MAINT_ROT2:
run_vacuum_list(db);
break;
case DB_MAINT_VACUUM_LIST:
if (!fill_items(db, res))
goto mem_err;
case DB_MAINT_DO_VACUUM:
if (!strlist_empty(db->maint_item_list)) {
run_vacuum(db);
} else {
close_maint(db, cf.maint_period);
}
break;
default:
fatal("bad state");
}
break;
case PGS_TIMEOUT:
log_debug("%s: maint timeout", db->name);
if (!pgs_connection_valid(db->c_maint))
launch_maint(db);
else
run_queue_list(db);
break;
default:
log_warning("%s: default reconnect", db->name);
pgs_reconnect(db->c_maint, 60);
}
return;
mem_err:
if (db->maint_item_list) {
strlist_free(db->maint_item_list);
db->maint_item_list = NULL;
}
pgs_disconnect(db->c_maint);
pgs_sleep(db->c_maint, 20);
}
void launch_maint(struct PgDatabase *db)
{
const char *cstr;
log_debug("%s: launch_maint", db->name);
if (!db->c_maint) {
if (db->maint_item_list) {
strlist_free(db->maint_item_list);
db->maint_item_list = NULL;
}
cstr = make_connstr(db->name);
db->c_maint = pgs_create(cstr, maint_handler, db);
}
if (!pgs_connection_valid(db->c_maint)) {
pgs_connect(db->c_maint);
} else {
/* Already have a connection, what are we doing here */
log_error("%s: maint already initialized", db->name);
return;
}
}
|