forked from timescale/timescaledb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_point.c
More file actions
326 lines (277 loc) · 8.98 KB
/
Copy pathdebug_point.c
File metadata and controls
326 lines (277 loc) · 8.98 KB
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
/*
* This file and its contents are licensed under the Apache License 2.0.
* Please see the included NOTICE for copyright information and
* LICENSE-APACHE for a copy of the license.
*/
#include "debug_point.h"
#include <postgres.h>
#include <fmgr.h>
#include <access/hash.h>
#include <access/xact.h>
#include <miscadmin.h>
#include <storage/ipc.h>
#include <storage/lock.h>
#include <utils/builtins.h>
#include "annotations.h"
#include "export.h"
#include "compat/compat.h"
TS_FUNCTION_INFO_V1(ts_debug_point_enable);
TS_FUNCTION_INFO_V1(ts_debug_point_release);
TS_FUNCTION_INFO_V1(ts_debug_point_id);
/*
* Debug points only exist in debug code and are intended to allow
* more controlled testing of the code.
*
* Debug points can be used as a wait point (1) or as a way to
* introduce error injections (2).
*
* (1) When used as wait points, execution will halt until the debug points are
* explicitly released.
*
* When waiting on a debug point, there is an attempt to take a shared lock on it.
* If the debug point is enabled by locking using an exclusive
* lock, this will block all waiters. Once the exclusive lock is released, all
* waiters will be able to proceed.
*
* (2) is similar to (1), but, instead of waiting for the debug point to be
* released, it will generate an error immediately.
*
*/
/* Tag for debug points.
*
* Each debug point is identified by a string that is hashed to a 8-byte
* number and used with the normal advisory locks available in PostgreSQL.
*/
typedef struct DebugPoint
{
const char *name;
LOCKTAG tag;
} DebugPoint;
static uint64
debug_point_name_to_id(const char *name)
{
return DatumGetUInt32(hash_any((const unsigned char *) name, strlen(name)));
}
static void
debug_point_init(DebugPoint *point, const char *name)
{
/* Use 64-bit hashing to get two independent 32-bit hashes */
uint64 hash = debug_point_name_to_id(name);
SET_LOCKTAG_ADVISORY(point->tag, MyDatabaseId, (uint32) (hash >> 32), (uint32) hash, 1);
point->name = pstrdup(name);
ereport(DEBUG3,
(errmsg("initializing debug point '%s' to use " UINT64_FORMAT, point->name, hash)));
}
static void
debug_point_enable(const DebugPoint *point)
{
LockAcquireResult lock_acquire_result;
ereport(DEBUG1, (errmsg("enabling debug point \"%s\"", point->name)));
lock_acquire_result = LockAcquire(&point->tag, ExclusiveLock, true, true);
switch (lock_acquire_result)
{
case LOCKACQUIRE_ALREADY_HELD:
case LOCKACQUIRE_ALREADY_CLEAR:
LockRelease(&point->tag, ExclusiveLock, true);
TS_FALLTHROUGH;
case LOCKACQUIRE_NOT_AVAIL:
ereport(ERROR,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("debug point \"%s\" already enabled", point->name)));
break;
case LOCKACQUIRE_OK:
break;
}
}
static void
debug_point_release(const DebugPoint *point)
{
ereport(DEBUG1, (errmsg("releasing debug point \"%s\"", point->name)));
if (!LockRelease(&point->tag, ExclusiveLock, true))
ereport(ERROR,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("cannot release debug point \"%s\"", point->name)));
}
/*
* Enable a debug point to block when being reached.
*
* This function will always succeed since we will not lock the debug point if
* it is already locked. A notice will be printed if the debug point is already
* enabled.
*/
Datum
ts_debug_point_enable(PG_FUNCTION_ARGS)
{
if (PG_ARGISNULL(0))
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("no name provided")));
text *name = PG_GETARG_TEXT_PP(0);
DebugPoint point;
debug_point_init(&point, text_to_cstring(name));
debug_point_enable(&point);
PG_RETURN_VOID();
}
/*
* Release a debug point allowing execution to proceed.
*/
Datum
ts_debug_point_release(PG_FUNCTION_ARGS)
{
if (PG_ARGISNULL(0))
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("no name provided")));
text *name = PG_GETARG_TEXT_PP(0);
DebugPoint point;
debug_point_init(&point, text_to_cstring(name));
debug_point_release(&point);
PG_RETURN_VOID();
}
/*
* Get the debug point identifier from the name.
*/
Datum
ts_debug_point_id(PG_FUNCTION_ARGS)
{
if (PG_ARGISNULL(0))
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("no name provided")));
text *name = PG_GETARG_TEXT_PP(0);
PG_RETURN_UINT64(debug_point_name_to_id(text_to_cstring(name)));
}
/*
* Wait for the debug point to be released.
*
* This is handled by first trying to get a shared lock, which will not block
* other sessions that try to grab the same lock but will block if an
* exclusive lock is already taken, and then release the lock immediately
* after.
*
* This function can decide to block while taking the shared lock or it can
* have a retry loop to take the share lock. This retry loop option is useful
* in cases where this function gets called from deep down inside a transaction
* where interrupts are not being served currently.
*/
void
ts_debug_point_wait(const char *name, bool blocking)
{
DebugPoint point;
LockAcquireResult lock_acquire_result pg_attribute_unused();
bool lock_release_result pg_attribute_unused();
/* Ensure that we are in a transaction before trying for locks */
if (!IsTransactionState())
return;
debug_point_init(&point, name);
ereport(DEBUG3, (errmsg("waiting on debug point '%s'", point.name)));
if (blocking)
lock_acquire_result = LockAcquire(&point.tag, ShareLock, true, false);
else
{
/*
* Trying to wait indefinitely here could lead to hangs. The current
* behavior is to retry for retry_count and return with a warning
* if that's crossed.
*
* If required, in future, we could take an additional option to decide
* if the caller wants to retry indefinitely or return with a warning.
* But the current behavior based on the "blocking" argument is ok for
* now.
*/
unsigned int retry_count = 1000;
/* try to acquire the lock without waiting. */
do
{
/* try to acquire the lock without waiting. */
lock_acquire_result = LockAcquire(&point.tag, ShareLock, true, true);
if (lock_acquire_result == LOCKACQUIRE_OK)
break;
/* don't dare to take a lock when the proc is exiting! */
if (proc_exit_inprogress || ProcDiePending)
return;
if (retry_count == 0)
{
elog(WARNING, "timeout while acquiring debug point lock");
return;
}
retry_count--;
/* retry after some time */
pg_usleep(100L);
} while (lock_acquire_result == LOCKACQUIRE_NOT_AVAIL);
}
Assert(lock_acquire_result == LOCKACQUIRE_OK);
lock_release_result = LockRelease(&point.tag, ShareLock, true);
Assert(lock_release_result);
ereport(DEBUG3, (errmsg("proceeding after debug point '%s'", point.name)));
}
/*
* Produce an error in case if the debug point is enabled.
*
* The idea is to enable the debug point separately first which
* acquires an ExclusiveLock on this tag. With the debug point enabled, this function
* when invoked will not get the ShareLock and will be able to raise
* the error as desired.
*
* ShareLock is used instead of an ExclusiveLock to prevent concurrent sessions reaching
* the same injection point from raising false conflicts.
*
* A ShareLock request from the same session that holds an ExclusiveLock
* always succeeds since a session never conflicts with itself, so we
* additionally check with LockHeldByMe to detect same-session injection.
*/
void
ts_debug_point_raise_error_if_enabled(const char *name)
{
DebugPoint point;
LockAcquireResult lock_acquire_result;
debug_point_init(&point, name);
lock_acquire_result = LockAcquire(&point.tag, ShareLock, true, true);
switch (lock_acquire_result)
{
case LOCKACQUIRE_OK:
/* ShareLock granted means no other session holds ExclusiveLock.
* But we still need to check whether this session itself enabled
* the injection. */
LockRelease(&point.tag, ShareLock, true);
if (LockHeldByMeCompat(&point.tag, ExclusiveLock, false))
{
break;
}
return;
case LOCKACQUIRE_ALREADY_HELD:
case LOCKACQUIRE_ALREADY_CLEAR:
LockRelease(&point.tag, ShareLock, true);
return;
case LOCKACQUIRE_NOT_AVAIL:
break;
}
ereport(ERROR,
(errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
errmsg("error injected at debug point '%s'", point.name)));
}
/*
* One-shot variant: release the debug point before raising the error.
* This allows error recovery code paths to call the same function
* without hitting the injection again.
*/
void
ts_debug_point_raise_error_oneshot(const char *name)
{
DebugPoint point;
LockAcquireResult lock_acquire_result;
debug_point_init(&point, name);
lock_acquire_result = LockAcquire(&point.tag, ShareLock, true, true);
switch (lock_acquire_result)
{
case LOCKACQUIRE_OK:
LockRelease(&point.tag, ShareLock, true);
if (LockHeldByMeCompat(&point.tag, ExclusiveLock, false))
break;
return;
case LOCKACQUIRE_ALREADY_HELD:
case LOCKACQUIRE_ALREADY_CLEAR:
LockRelease(&point.tag, ShareLock, true);
return;
case LOCKACQUIRE_NOT_AVAIL:
break;
}
debug_point_release(&point);
ereport(ERROR,
(errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION),
errmsg("error injected at debug point '%s'", point.name)));
}