-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpggearman.c
317 lines (261 loc) · 9.31 KB
/
pggearman.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
/*
* Gearman PostgreSQL Functions
* Copyright (C) 2009 Eric Day, Selena Deckelmann
*
* Use and distribution licensed under the BSD license. See
* the COPYING file in the parent directory for full text.
*/
#include "postgres.h"
#include "fmgr.h"
#include "utils/builtins.h"
#include "utils/memutils.h"
#include "utils/guc.h"
#include "utils/elog.h"
#include "pggearman.h"
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
/*
* PostgreSQL <= 8.2 provided VARATT_SIZEP(p), but later versions use
* SET_VARSIZE() (and also define the VARATT_SIZEP_DEPRECATED
* symbol). Therefore, define the latter in terms of the former if
* necessary.
*/
#ifndef SET_VARSIZE
#define SET_VARSIZE(ptr, size) VARATT_SIZEP(ptr) = (size)
#endif
enum
{
pg_gman_do,
pg_gman_do_high,
pg_gman_do_low,
pg_gman_do_background,
pg_gman_do_high_background,
pg_gman_do_low_background
};
/* Per-backend global state. */
struct gearman_global
{
gearman_client_st *client;
text *result;
MemoryContext pg_ctx;
};
static struct gearman_global globals;
char *gearman_servers;
static Datum gman_run_cmd(int type, PG_FUNCTION_ARGS);
static void *gman_malloc(size_t size, void *arg);
static void gman_free(void *ptr, void *arg);
static GucStringAssignHook *assign_gearman_servers_guc(const char *newval,
bool doit,
GucSource source);
static GucShowHook *show_gearman_servers_guc(void);
static gearman_return_t do_servers_add(char *host_str);
#if PG_VERSION_NUM >= 90600
/* GetConfigOptionByName has a new signature from 9.6 on */
#define GetConfigOptionByName(name, varname) GetConfigOptionByName(name, varname, false)
#endif /* PG_VERSION_NUM */
void _PG_init(void)
{
MemoryContext old_ctx;
char *new_value = NULL;
globals.pg_ctx = AllocSetContextCreate(TopMemoryContext,
"pggearman global context",
ALLOCSET_SMALL_MINSIZE,
ALLOCSET_SMALL_INITSIZE,
ALLOCSET_SMALL_MAXSIZE);
old_ctx = MemoryContextSwitchTo(globals.pg_ctx);
PG_TRY();
{
new_value = GetConfigOptionByName("pggearman.default_servers", NULL);
}
PG_CATCH();
{
DefineCustomStringVariable("pggearman.default_servers",
"Comma-separated list of gearman servers "
"to connect to.",
"Specified as a comma-separated list of "
"host:port (port is optional).",
&gearman_servers,
#if defined(PG_VERSION_NUM) && (80400 <= PG_VERSION_NUM)
NULL,
#endif
PGC_USERSET,
#if defined(PG_VERSION_NUM) && (80400 <= PG_VERSION_NUM)
GUC_LIST_INPUT,
#endif
#if defined(PG_VERSION_NUM) && (90100 <= PG_VERSION_NUM)
NULL, // GucStringCheckHook parameter for postgres 9.1+ <<== here additional parameter
#endif
(GucStringAssignHook) assign_gearman_servers_guc,
(GucShowHook) show_gearman_servers_guc);
}
PG_END_TRY();
globals.client = gearman_client_create(NULL);
gearman_client_set_workload_malloc_fn(globals.client, gman_malloc, NULL);
gearman_client_set_workload_free_fn(globals.client, gman_free, NULL);
if (new_value != NULL) {
do_servers_add(new_value);
}
MemoryContextSwitchTo(old_ctx);
}
GucShowHook *show_gearman_servers_guc(void)
{
return (GucShowHook *) gearman_servers;
}
GucStringAssignHook *assign_gearman_servers_guc(const char *newval,
bool doit, GucSource source)
{
bool ret;
ret = do_servers_add((char *) newval);
if (ret)
return (GucStringAssignHook *) newval;
else
return NULL;
}
static gearman_return_t do_servers_add(char *host_str)
{
gearman_return_t ret;
MemoryContext old_ctx;
old_ctx = MemoryContextSwitchTo(globals.pg_ctx);
gearman_client_remove_servers(globals.client);
#if defined(PG_VERSION_NUM) && (90100 <= PG_VERSION_NUM)
// for version 9.1+ u cannot call gearman_client_add_servers if host is null
if(host_str) ret = gearman_client_add_servers(globals.client, host_str);
else ret = GEARMAN_SUCCESS; // ret success anyway, but dont call
#else
ret = gearman_client_add_servers(globals.client, host_str);
#endif
MemoryContextSwitchTo(old_ctx);
if (ret != GEARMAN_SUCCESS)
elog(ERROR, "%s", gearman_client_error(globals.client));
PG_RETURN_BOOL(ret == GEARMAN_SUCCESS);
}
/*
* This is called when we're being unloaded from a process. Note that
* this only happens when we're being replaced by a LOAD (e.g. it
* doesn't happen on process exit), so we can't depend on it being
* called.
*/
void _PG_fini(void)
{
gearman_client_free(globals.client);
MemoryContextDelete(globals.pg_ctx);
}
Datum gman_servers_set(PG_FUNCTION_ARGS)
{
text *get_servers = PG_GETARG_TEXT_P(0);
char *servers;
servers = DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(get_servers)));
return do_servers_add(servers);
}
Datum gman_do(PG_FUNCTION_ARGS)
{
PG_RETURN_TEXT_P(gman_run_cmd(pg_gman_do, fcinfo));
}
Datum gman_do_high(PG_FUNCTION_ARGS)
{
PG_RETURN_TEXT_P(gman_run_cmd(pg_gman_do_high, fcinfo));
}
Datum gman_do_low(PG_FUNCTION_ARGS)
{
PG_RETURN_TEXT_P(gman_run_cmd(pg_gman_do_low, fcinfo));
}
Datum gman_do_background(PG_FUNCTION_ARGS)
{
PG_RETURN_TEXT_P(gman_run_cmd(pg_gman_do_background, fcinfo));
}
Datum gman_do_low_background(PG_FUNCTION_ARGS)
{
PG_RETURN_TEXT_P(gman_run_cmd(pg_gman_do_low_background, fcinfo));
}
Datum gman_do_high_background(PG_FUNCTION_ARGS)
{
PG_RETURN_TEXT_P(gman_run_cmd(pg_gman_do_high_background, fcinfo));
}
Datum gman_run_cmd(int type, PG_FUNCTION_ARGS) {
text *get_function;
char *function;
text *get_workload;
char *workload;
size_t workload_length;
text *result;
size_t result_length;
gearman_return_t ret;
char job_handle[GEARMAN_JOB_HANDLE_SIZE];
int background = 0;
if (PG_ARGISNULL(0))
elog(ERROR, "gearman function cannot be NULL");
if (PG_ARGISNULL(1))
elog(ERROR, "gearman workload cannot be NULL");
get_function = PG_GETARG_TEXT_P(0);
function = DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(get_function)));
get_workload = PG_GETARG_TEXT_P(1);
workload = DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(get_workload)));
workload_length = strlen(workload);
switch (type)
{
case pg_gman_do:
(void)gearman_client_do(globals.client, function, NULL, workload,
workload_length, &result_length, &ret);
break;
case pg_gman_do_high:
(void)gearman_client_do_high(globals.client, function, NULL,
workload, workload_length,
&result_length, &ret);
break;
case pg_gman_do_low:
(void)gearman_client_do_low(globals.client, function, NULL,
workload, workload_length,
&result_length, &ret);
break;
case pg_gman_do_background:
ret = gearman_client_do_background(globals.client, function, NULL,
workload, workload_length,
job_handle);
background = 1;
break;
case pg_gman_do_low_background:
ret = gearman_client_do_low_background(globals.client, function,
NULL, workload,
workload_length, job_handle);
background = 1;
break;
case pg_gman_do_high_background:
ret = gearman_client_do_high_background(globals.client, function,
NULL, workload,
workload_length,
job_handle);
background = 1;
break;
default:
elog(ERROR, "Gearman function type is undefined! Scary. Bailing out.");
}
if (ret != GEARMAN_SUCCESS)
elog(ERROR, "%s", gearman_client_error(globals.client));
if (background)
{
result_length = strlen(job_handle);
result = (text *) palloc(result_length + VARHDRSZ);
SET_VARSIZE(result, result_length + VARHDRSZ);
memcpy(VARDATA(result), job_handle, result_length);
}
else if (globals.result == NULL)
PG_RETURN_NULL();
else
{
result = globals.result;
globals.result = NULL;
}
PG_RETURN_TEXT_P(result);
}
static void *gman_malloc(size_t size, void *arg)
{
globals.result = (text *) palloc(size + VARHDRSZ);
SET_VARSIZE(globals.result, size + VARHDRSZ);
return VARDATA(globals.result);
}
static void gman_free(void *ptr, void *arg)
{
pfree(globals.result);
globals.result = NULL;
}