forked from supabase/pg_net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.c
286 lines (229 loc) · 8.58 KB
/
worker.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
#include <postgres.h>
#include <pgstat.h>
#include <postmaster/bgworker.h>
#include <storage/ipc.h>
#include <storage/latch.h>
#include <storage/proc.h>
#include <fmgr.h>
#include <access/xact.h>
#include <executor/spi.h>
#include <utils/snapmgr.h>
#include <commands/extension.h>
#include <catalog/pg_extension.h>
#include <catalog/pg_type.h>
#include <miscadmin.h>
#include <utils/builtins.h>
#include <access/hash.h>
#include <utils/hsearch.h>
#include <utils/memutils.h>
#include <utils/jsonb.h>
#include <utils/guc.h>
#include <tcop/utility.h>
#include <curl/curl.h>
#include <curl/multi.h>
#include <sys/epoll.h>
#include <sys/timerfd.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <inttypes.h>
#include <storage/shmem.h>
#include "util.h"
#include "core.h"
#define MIN_LIBCURL_VERSION_NUM 0x075300 // This is the 7.83.0 version in hex as defined in curl/curlver.h
#define REQUIRED_LIBCURL_ERR_MSG "libcurl >= 7.83.0 is required, we use the curl_easy_nextheader() function added in this version"
_Static_assert(LIBCURL_VERSION_NUM, REQUIRED_LIBCURL_ERR_MSG); // test for older libcurl versions that don't even have LIBCURL_VERSION_NUM defined (e.g. libcurl 6.5).
_Static_assert(LIBCURL_VERSION_NUM >= MIN_LIBCURL_VERSION_NUM, REQUIRED_LIBCURL_ERR_MSG);
PG_MODULE_MAGIC;
static char* guc_ttl;
static int guc_batch_size;
static char* guc_database_name;
static MemoryContext CurlMemContext = NULL;
static shmem_startup_hook_type prev_shmem_startup_hook = NULL;
static long latch_timeout = 1000;
static volatile sig_atomic_t got_sigterm = false;
static volatile sig_atomic_t got_sighup = false;
static bool* restart_worker = NULL;
void _PG_init(void);
PGDLLEXPORT void pg_net_worker(Datum main_arg) pg_attribute_noreturn();
PG_FUNCTION_INFO_V1(worker_restart);
Datum worker_restart(PG_FUNCTION_ARGS) {
bool result = DatumGetBool(DirectFunctionCall1(pg_reload_conf, (Datum) NULL)); // reload the config
*restart_worker = true;
PG_RETURN_BOOL(result && *restart_worker); // TODO is not necessary to return a bool here, but we do it to maintain backward compatibility
}
static void
handle_sigterm(SIGNAL_ARGS)
{
int save_errno = errno;
got_sigterm = true;
if (MyProc)
SetLatch(&MyProc->procLatch);
errno = save_errno;
}
static void
handle_sighup(SIGNAL_ARGS)
{
int save_errno = errno;
got_sighup = true;
if (MyProc)
SetLatch(&MyProc->procLatch);
errno = save_errno;
}
static bool is_extension_loaded(){
Oid extensionOid;
StartTransactionCommand();
extensionOid = get_extension_oid("pg_net", true);
CommitTransactionCommand();
return OidIsValid(extensionOid);
}
void pg_net_worker(Datum main_arg) {
pqsignal(SIGTERM, handle_sigterm);
pqsignal(SIGHUP, handle_sighup);
pqsignal(SIGUSR1, procsignal_sigusr1_handler);
BackgroundWorkerUnblockSignals();
BackgroundWorkerInitializeConnection(guc_database_name, NULL, 0);
elog(INFO, "pg_net_worker started with a config of: pg_net.ttl=%s, pg_net.batch_size=%d, pg_net.database_name=%s", guc_ttl, guc_batch_size, guc_database_name);
int curl_ret = curl_global_init(CURL_GLOBAL_ALL);
if(curl_ret != CURLE_OK)
ereport(ERROR, errmsg("curl_global_init() returned %s\n", curl_easy_strerror(curl_ret)));
LoopState lstate = {
.epfd = epoll_create1(0),
.timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC),
.curl_mhandle = curl_multi_init(),
};
if (lstate.epfd < 0) {
ereport(ERROR, errmsg("Failed to create epoll file descriptor"));
}
if (lstate.timerfd < 0) {
ereport(ERROR, errmsg("Failed to create timerfd"));
}
if(!lstate.curl_mhandle)
ereport(ERROR, errmsg("curl_multi_init()"));
set_curl_mhandle(lstate.curl_mhandle, &lstate);
timerfd_settime(lstate.timerfd, 0, &(itimerspec){}, NULL);
epoll_ctl(lstate.epfd, EPOLL_CTL_ADD, lstate.timerfd, &(epoll_event){.events = EPOLLIN, .data.fd = lstate.timerfd});
while (!got_sigterm) {
WaitLatch(&MyProc->procLatch,
WL_LATCH_SET | WL_TIMEOUT | WL_EXIT_ON_PM_DEATH,
latch_timeout,
PG_WAIT_EXTENSION);
ResetLatch(&MyProc->procLatch);
CHECK_FOR_INTERRUPTS();
if(!is_extension_loaded()){
elog(DEBUG2, "pg_net_worker: extension not yet loaded");
continue;
}
if (got_sighup) {
got_sighup = false;
ProcessConfigFile(PGC_SIGHUP);
}
if (restart_worker && *restart_worker) {
*restart_worker = false;
elog(INFO, "Restarting pg_net worker");
break;
}
delete_expired_responses(guc_ttl, guc_batch_size);
consume_request_queue(lstate.curl_mhandle, guc_batch_size, CurlMemContext);
int running_handles = 0;
int maxevents = guc_batch_size + 1; // 1 extra for the timer
epoll_event *events = palloc0(sizeof(epoll_event) * maxevents);
do {
int nfds = epoll_wait(lstate.epfd, events, maxevents, /*timeout=*/1000);
if (nfds < 0) {
int save_errno = errno;
if(save_errno == EINTR) { // can happen when the epoll is interrupted, for example when running under GDB. Just continue in this case.
continue;
}
else {
ereport(ERROR, errmsg("epoll_wait() failed: %s", strerror(save_errno)));
break;
}
}
for (int i = 0; i < nfds; i++) {
if (events[i].data.fd == lstate.timerfd) {
EREPORT_MULTI(
curl_multi_socket_action(lstate.curl_mhandle, CURL_SOCKET_TIMEOUT, 0, &running_handles)
);
} else {
int ev_bitmask =
events[i].events & EPOLLIN ? CURL_CSELECT_IN:
events[i].events & EPOLLOUT ? CURL_CSELECT_OUT:
CURL_CSELECT_ERR;
EREPORT_MULTI(
curl_multi_socket_action(
lstate.curl_mhandle, events[i].data.fd,
ev_bitmask,
&running_handles)
);
if(running_handles <= 0) {
elog(DEBUG2, "last transfer done, kill timeout");
timerfd_settime(lstate.timerfd, 0, &(itimerspec){0}, NULL);
}
}
insert_curl_responses(&lstate, CurlMemContext);
}
} while (running_handles > 0); // run again while there are curl handles, this will prevent waiting for the latch_timeout (which will cause the cause the curl timeouts to be wrong)
pfree(events);
MemoryContextReset(CurlMemContext);
}
close(lstate.epfd);
close(lstate.timerfd);
curl_multi_cleanup(lstate.curl_mhandle);
curl_global_cleanup();
// causing a failure on exit will make the postmaster process restart the bg worker
proc_exit(EXIT_FAILURE);
}
static void net_shmem_startup(void) {
if (prev_shmem_startup_hook)
prev_shmem_startup_hook();
restart_worker = ShmemAlloc(sizeof(bool));
*restart_worker = false;
}
void _PG_init(void) {
if (IsBinaryUpgrade) {
return;
}
if (!process_shared_preload_libraries_in_progress) {
ereport(ERROR, errmsg("pg_net is not in shared_preload_libraries"),
errhint("Add pg_net to the shared_preload_libraries "
"configuration variable in postgresql.conf."));
}
RegisterBackgroundWorker(&(BackgroundWorker){
.bgw_flags = BGWORKER_SHMEM_ACCESS | BGWORKER_BACKEND_DATABASE_CONNECTION,
.bgw_start_time = BgWorkerStart_RecoveryFinished,
.bgw_library_name = "pg_net",
.bgw_function_name = "pg_net_worker",
.bgw_name = "pg_net " EXTVERSION " worker",
.bgw_restart_time = 1,
});
prev_shmem_startup_hook = shmem_startup_hook;
shmem_startup_hook = net_shmem_startup;
CurlMemContext = AllocSetContextCreate(TopMemoryContext,
"pg_net curl context",
ALLOCSET_DEFAULT_MINSIZE,
ALLOCSET_DEFAULT_INITSIZE,
ALLOCSET_DEFAULT_MAXSIZE);
DefineCustomStringVariable("pg_net.ttl",
"time to live for request/response rows",
"should be a valid interval type",
&guc_ttl,
"6 hours",
PGC_SUSET, 0,
NULL, NULL, NULL);
DefineCustomIntVariable("pg_net.batch_size",
"number of requests executed in one iteration of the background worker",
NULL,
&guc_batch_size,
200,
0, PG_INT16_MAX,
PGC_SUSET, 0,
NULL, NULL, NULL);
DefineCustomStringVariable("pg_net.database_name",
"Database where pg_net tables are located",
NULL,
&guc_database_name,
"postgres",
PGC_SIGHUP, 0,
NULL, NULL, NULL);
}