-
Notifications
You must be signed in to change notification settings - Fork 3
/
pg_simula.c
455 lines (381 loc) · 10.6 KB
/
pg_simula.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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/* -------------------------------------------------------------------------
*
* pg_simula.c
*
* Database system failure simulation tool for PostgreSQL.
*
* -------------------------------------------------------------------------
*/
#include "postgres.h"
#include "access/transam.h"
#include "access/xact.h"
#include "executor/spi.h"
#include "commands/extension.h"
#include "fmgr.h"
#include "funcapi.h"
#include "lib/stringinfo.h"
#include "libpq/libpq-be.h"
#include "libpq/auth.h"
#include "miscadmin.h"
#include "optimizer/planner.h"
#include "optimizer/prep.h"
#include "pgstat.h"
#include "replication/syncrep.h"
#include "storage/ipc.h"
#include "storage/spin.h"
#include "tcop/utility.h"
#include "utils/builtins.h"
#include "utils/snapmgr.h"
#include "utils/ps_status.h"
#include "utils/memutils.h"
#define EVENT_TABLE_NAME "simula_events"
#define MAX_LENGTH 100
PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(clear_all_events);
void _PG_init(void);
void _PG_fini(void);
typedef struct SimualEvent
{
char operation[MAX_LENGTH];
char action[MAX_LENGTH];
int sec;
} SimulaEvent;
List *SimulaEvents = NIL;
PG_FUNCTION_INFO_V1(add_simula_event);
/* pg_simula hook functions */
static PlannedStmt *pg_simula_planner(Query *parse, int cursorOptions,
ParamListInfo boundParams);
static void pg_simula_ProcessUtility(PlannedStmt *pstmt,
const char *queryString,
ProcessUtilityContext context,
ParamListInfo params,
QueryEnvironment *queryEnv,
DestReceiver *dest,
char *completionTag);
static void pg_simula_ClientAuthentication(Port *port, int status);
static void pg_simula_xact_callback(XactEvent event, void *arg);
static void reloadEventTableData(void);
static void doEventIfAny(const char *commandTag);
static bool isPgSimulaLoaded(void);
static bool needReloadAndEvent(const char *commandTag);
/* Simulation functions */
static void error_func(int sec);
static void panic_func(int sec);
static void wait_func(int sec);
static void fatal_func(int sec);
typedef void (*act_func) (int sec);
typedef struct Action
{
char *action;
act_func func;
} Action;
Action ActionTable[] =
{
{"error", error_func},
{"panic", panic_func},
{"wait", wait_func},
{"fatal", fatal_func},
{NULL, NULL}
};
static planner_hook_type prev_planner = NULL;
static ProcessUtility_hook_type prev_ProcessUtility = NULL;
static ClientAuthentication_hook_type prev_ClientAuthentication = NULL;
static bool in_simula_event_progress = false;
static bool registered_to_callback = false;
/* GUC parameter */
static bool simulation_enabled = false;
static bool connection_refused = false;
void
_PG_init(void)
{
DefineCustomBoolVariable("pg_simula.enabled",
"Enable simulation mode",
NULL,
&simulation_enabled,
false,
PGC_USERSET,
0,
NULL,
NULL,
NULL);
DefineCustomBoolVariable("pg_simula.connection_refuse",
"Refuse all new connections",
NULL,
&connection_refused,
false,
PGC_USERSET,
0,
NULL,
NULL,
NULL);
prev_planner = planner_hook;
planner_hook = pg_simula_planner;
prev_ProcessUtility = ProcessUtility_hook;
ProcessUtility_hook = pg_simula_ProcessUtility;
prev_ClientAuthentication = ClientAuthentication_hook;
ClientAuthentication_hook = pg_simula_ClientAuthentication;
}
/* Uninstall hook functions */
void _PG_fini(void)
{
planner_hook = prev_planner;
ProcessUtility_hook = prev_ProcessUtility;
}
static bool
needReloadAndEvent(const char *commandTag)
{
if (simulation_enabled &&
!in_simula_event_progress &&
IsTransactionState() &&
pg_strcasecmp(commandTag, "START TRANSACTION") != 0 &&
pg_strcasecmp(commandTag, "BEGIN") != 0)
return true;
return false;
}
static void
reloadEventTableData(void)
{
StringInfoData buf;
int ret;
int ntup;
int i;
list_free_deep(SimulaEvents);
SimulaEvents = NIL;
if (!isPgSimulaLoaded())
return;
SetCurrentStatementStartTimestamp();
SPI_connect();
PushActiveSnapshot(GetTransactionSnapshot());
initStringInfo(&buf);
appendStringInfo(&buf, "SELECT * FROM public.%s", EVENT_TABLE_NAME);
ret = SPI_execute(buf.data, true, 0);
if (ret != SPI_OK_SELECT)
elog(ERROR, "SPI_execute falied: error code %d", ret);
ntup = SPI_processed;
if (ret > 0 && SPI_tuptable != NULL)
{
TupleDesc tupdesc = SPI_tuptable->tupdesc;
SPITupleTable *tuptable = SPI_tuptable;
for (i = 0; i < ntup; i++)
{
HeapTuple tuple = tuptable->vals[i];
SimulaEvent *event;
MemoryContext old;
char *operation = SPI_getvalue(tuple, tupdesc, 1);
char *action = SPI_getvalue(tuple, tupdesc, 2);
char *sec = SPI_getvalue(tuple, tupdesc, 3);
old = MemoryContextSwitchTo(TopMemoryContext);
event = palloc(sizeof(SimulaEvent));
strncpy(event->operation, operation, MAX_LENGTH);
strncpy(event->action, action, MAX_LENGTH);
event->sec = atoi(sec);
SimulaEvents = lappend(SimulaEvents, event);
MemoryContextSwitchTo(old);
}
}
SPI_finish();
PopActiveSnapshot();
}
Datum
add_simula_event(PG_FUNCTION_ARGS)
{
text *operation = PG_GETARG_TEXT_P(0);
text *action = PG_GETARG_TEXT_P(1);
int sec = PG_GETARG_INT32(2);
char *ope_str = text_to_cstring(operation);
char *act_str = text_to_cstring(action);
Action *act;
bool found = false;
StringInfoData buf;
int ret;
in_simula_event_progress = true;
for (act = ActionTable; act->action != NULL; act++)
{
if (pg_strcasecmp(act->action, act_str) == 0)
{
found = true;
break;
}
}
if (!found)
ereport(ERROR, (errmsg("invalid action: \"%s\"", act_str)));
initStringInfo(&buf);
appendStringInfo(&buf,
"INSERT INTO %s VALUES('%s', '%s', %d) ON CONFLICT "
"ON CONSTRAINT simula_events_pkey "
"DO UPDATE SET (action, sec) = (excluded.action, excluded.sec)",
EVENT_TABLE_NAME,
ope_str, act_str, sec);
SetCurrentStatementStartTimestamp();
SPI_connect();
PushActiveSnapshot(GetTransactionSnapshot());
ret = SPI_execute(buf.data, false, 0);
SPI_finish();
PopActiveSnapshot();
in_simula_event_progress = false;
PG_RETURN_BOOL(ret);
}
/* Clear all simulation events */
Datum
clear_all_events(PG_FUNCTION_ARGS)
{
int ret;
StringInfoData buf;
in_simula_event_progress = true;
SetCurrentStatementStartTimestamp();
SPI_connect();
PushActiveSnapshot(GetTransactionSnapshot());
initStringInfo(&buf);
appendStringInfo(&buf, "TRUNCATE %s", EVENT_TABLE_NAME);
ret = SPI_execute(buf.data, false, 0);
SPI_finish();
PopActiveSnapshot();
in_simula_event_progress = false;
PG_RETURN_BOOL(ret);
}
/* Check if pg_simula is already loaded (created) on the database */
static bool
isPgSimulaLoaded(void)
{
return OidIsValid(get_extension_oid("pg_simula", true));
}
static void
pg_simula_xact_callback(XactEvent event, void *arg)
{
in_simula_event_progress = false;
}
/*
* Detect SQL command other than utility commands.
*/
static PlannedStmt *
pg_simula_planner(Query *parse, int cursorOptions, ParamListInfo boundParams)
{
const char *commandTag;
commandTag = CreateCommandTag((Node *) &parse->type);
/* Register callback function if not yet */
if (!registered_to_callback)
{
RegisterXactCallback(pg_simula_xact_callback, NULL);
registered_to_callback = true;
}
if (needReloadAndEvent(commandTag))
{
/* in_simulat_event_progress is turned off at end of the transaction */
in_simula_event_progress = true;
reloadEventTableData();
doEventIfAny(commandTag);
in_simula_event_progress = false;
}
if (prev_planner)
return (*prev_planner) (parse, cursorOptions, boundParams);
else
return standard_planner(parse, cursorOptions, boundParams);
}
static void
pg_simula_ProcessUtility(PlannedStmt *pstmt,
const char *queryString,
ProcessUtilityContext context,
ParamListInfo params,
QueryEnvironment *queryEnv,
DestReceiver *dest,
char *completionTag)
{
Node *parsetree = pstmt->utilityStmt;
const char *commandTag;
commandTag = CreateCommandTag(parsetree);
/* Register callback function if not yet */
if (!registered_to_callback)
{
RegisterXactCallback(pg_simula_xact_callback, NULL);
registered_to_callback = true;
}
if (needReloadAndEvent(commandTag))
{
/* in_simulat_event_progress is turned off at end of the transaction */
in_simula_event_progress = true;
reloadEventTableData();
doEventIfAny(commandTag);
in_simula_event_progress = false;
}
/* Call the standard process utility chain. */
if (prev_ProcessUtility)
(*prev_ProcessUtility) (pstmt, queryString, context, params,
queryEnv, dest, completionTag);
else
standard_ProcessUtility(pstmt, queryString, context, params,
queryEnv, dest, completionTag);
}
/*
* Always reject the connection.
*
* This function is a copy function from auth_failed(auth.c)
*/
static void
pg_simula_ClientAuthentication(Port *port, int status)
{
const char *errstr;
int errcode_return = ERRCODE_INVALID_AUTHORIZATION_SPECIFICATION;
if (!connection_refused)
return;
/*
* If we failed due to EOF from client, just quit; there's no point in
* trying to send a message to the client, and not much point in logging
* the failure in the postmaster log. (Logging the failure might be
* desirable, were it not for the fact that libpq closes the connection
* unceremoniously if challenged for a password when it hasn't got one to
* send. We'll get a useless log entry for every psql connection under
* password auth, even if it's perfectly successful, if we log STATUS_EOF
* events.)
*/
if (status == STATUS_EOF)
proc_exit(0);
errstr = gettext_noop("authentication failed by pg_simula");
ereport(FATAL,
(errcode(errcode_return),
errmsg(errstr, port->user_name), 0));
/* doesn't return */
}
static void
doEventIfAny(const char *commandTag)
{
ListCell *cell;
foreach(cell, SimulaEvents)
{
SimulaEvent *event = lfirst(cell);
/* Found the target, do specified action */
if (pg_strcasecmp(event->operation, commandTag) == 0)
{
Action *act = ActionTable;
/* Walk through ActionTable in order to find the action function */
for (act = ActionTable; act->action != NULL; act++)
{
if (pg_strcasecmp(act->action, event->action) == 0)
{
/* expected to be at most one action per command */
act->func(event->sec);
return;
}
}
}
}
}
static void
error_func(int sec)
{
ereport(ERROR, (errmsg("simulation of ERROR by pg_simula")));
}
static void
panic_func(int sec)
{
ereport(PANIC, (errmsg("simulation of PANIC by pg_simula")));
}
static void
wait_func(int sec)
{
pg_usleep(sec * 1000 * 1000);
}
static void
fatal_func(int sec)
{
ereport(FATAL, (errmsg("simulation of FATAL by pg_simula")));
}