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
|
/* -*-pgsql-c-*- */
/*
*
* $Header$
*
* pgpool: a language independent connection pool server for PostgreSQL
* written by Tatsuo Ishii
*
* Copyright (c) 2003-2021 PgPool Global Development Group
*
* Permission to use, copy, modify, and distribute this software and
* its documentation for any purpose and without fee is hereby
* granted, provided that the above copyright notice appear in all
* copies and that both that copyright notice and this permission
* notice appear in supporting documentation, and that the name of the
* author not be used in advertising or publicity pertaining to
* distribution of the software without specific, written prior
* permission. The author makes no representations about the
* suitability of this software for any purpose. It is provided "as
* is" without express or implied warranty.
*
*/
#include <unistd.h>
#include <arpa/inet.h>
#include <time.h>
#include "pool.h"
#include "utils/elog.h"
#include "context/pool_process_context.h"
#include "pool_config.h" /* remove me afterwards */
static POOL_PROCESS_CONTEXT process_context_d;
static POOL_PROCESS_CONTEXT * process_context;
/*
* Initialize per process context
*/
void
pool_init_process_context(void)
{
process_context = &process_context_d;
if (!process_info)
ereport(FATAL,
(return_code(1),
errmsg("process info is not set")));
process_context->process_info = process_info;
if (!pool_config->backend_desc)
ereport(FATAL,
(return_code(1),
errmsg("backend desc is not set")));
process_context->backend_desc = pool_config->backend_desc;
process_context->proc_id = my_proc_id;
process_context->local_session_id = 0; /* initialize local session
* counter */
process_context->last_alarm_handler = SIG_IGN;
process_context->last_alarm_time = 0;
process_context->last_alarm_second = 0;
process_context->undo_alarm_second = 0;
}
/*
* Return process context
*/
POOL_PROCESS_CONTEXT *
pool_get_process_context(void)
{
return process_context;
}
/*
* Return my process info
*/
ProcessInfo *
pool_get_my_process_info(void)
{
if (!process_context)
ereport(FATAL,
(return_code(1),
errmsg("process context is not initialized")));
return &process_context->process_info[process_context->proc_id];
}
/*
* Increment local session id
*/
void
pool_increment_local_session_id(void)
{
POOL_PROCESS_CONTEXT *p = pool_get_process_context();
if (!p)
ereport(ERROR,
(errmsg("failed to get process context")));
p->local_session_id++;
}
/*
* Return byte size of connection info(ConnectionInfo) on shmem.
*/
size_t
pool_coninfo_size(void)
{
size_t size;
size = pool_config->num_init_children *
pool_config->max_pool *
MAX_NUM_BACKENDS *
sizeof(ConnectionInfo);
ereport(DEBUG1,
(errmsg("pool_coninfo_size: num_init_children (%d) * max_pool (%d) * MAX_NUM_BACKENDS (%d) * sizeof(ConnectionInfo) (%zu) = %zu bytes requested for shared memory",
pool_config->num_init_children,
pool_config->max_pool,
MAX_NUM_BACKENDS,
sizeof(ConnectionInfo),
size)));
return size;
}
/*
* Return number of elements of connection info(ConnectionInfo) on shmem.
*/
int
pool_coninfo_num(void)
{
int nelm;
nelm = pool_config->num_init_children *
pool_config->max_pool *
MAX_NUM_BACKENDS;
return nelm;
}
/*
* Return pointer to i th child, j th connection pool and k th backend
* of connection info on shmem.
*/
ConnectionInfo *
pool_coninfo(int child, int connection_pool, int backend)
{
if (child < 0 || child >= pool_config->num_init_children)
{
ereport(WARNING,
(errmsg("failed to get connection info, invalid child number: %d", child)));
return NULL;
}
if (connection_pool < 0 || connection_pool >= pool_config->max_pool)
{
ereport(WARNING,
(errmsg("failed to get connection info, invalid connection pool number: %d", connection_pool)));
return NULL;
}
if (backend < 0 || backend >= MAX_NUM_BACKENDS)
{
ereport(WARNING,
(errmsg("failed to get connection info, invalid backend number: %d", backend)));
return NULL;
}
return &con_info[child * pool_config->max_pool * MAX_NUM_BACKENDS +
connection_pool * MAX_NUM_BACKENDS +
backend];
}
/*
* Return pointer to child which has OS process id pid, j th connection
* pool and k th backend of connection info on shmem.
*/
ConnectionInfo *
pool_coninfo_pid(int pid, int connection_pool, int backend)
{
int child = -1;
int i;
for (i = 0; i < pool_config->num_init_children; i++)
{
if (process_info[i].pid == pid)
{
child = i;
break;
}
}
if (child < 0)
elog(ERROR, "failed to get child pid, invalid child PID:%d", pid);
if (child < 0 || child >= pool_config->num_init_children)
elog(ERROR, "failed to get child pid, invalid child no:%d", child);
if (connection_pool < 0 || connection_pool >= pool_config->max_pool)
elog(ERROR, "failed to get child pid, invalid connection pool no:%d", connection_pool);
if (backend < 0 || backend >= MAX_NUM_BACKENDS)
elog(ERROR, "failed to get child pid, invalid backend no:%d", backend);
return &con_info[child * pool_config->max_pool * MAX_NUM_BACKENDS +
connection_pool * MAX_NUM_BACKENDS +
backend];
}
/*
* locate and return the shared memory ConnectionInfo having the
* backend connection with the pid
* if the connection is found the *backend_node_id contains the backend node id
* of the backend node that has the connection
*/
ConnectionInfo *
pool_coninfo_backend_pid(int backend_pid, int *backend_node_id)
{
int child;
/*
* look for the child process that has the backend with the pid
*/
ereport(DEBUG1,
(errmsg("searching for the connection with backend pid:%d", backend_pid)));
for (child = 0; child < pool_config->num_init_children; child++)
{
int pool;
if (process_info[child].pid)
{
ProcessInfo *pi = pool_get_process_info(process_info[child].pid);
for (pool = 0; pool < pool_config->max_pool; pool++)
{
int backend_id;
for (backend_id = 0; backend_id < NUM_BACKENDS; backend_id++)
{
int poolBE = pool * MAX_NUM_BACKENDS + backend_id;
if (ntohl(pi->connection_info[poolBE].pid) == backend_pid)
{
ereport(DEBUG1,
(errmsg("found the connection with backend pid:%d on backend node %d", backend_pid, backend_id)));
*backend_node_id = backend_id;
return &pi->connection_info[poolBE];
}
}
}
}
}
return NULL;
}
/*
* sets the flag to mark that the connection will be terminated by the
* backend and it should not be considered as a backend node failure.
* This flag is used to handle pg_terminate_backend()
*/
void
pool_set_connection_will_be_terminated(ConnectionInfo * connInfo)
{
connInfo->swallow_termination = 1;
}
void
pool_unset_connection_will_be_terminated(ConnectionInfo * connInfo)
{
connInfo->swallow_termination = 0;
}
/*
* Set frontend connected flag
*/
void
pool_coninfo_set_frontend_connected(int proc_id, int pool_index)
{
ConnectionInfo *con;
int i;
for (i = 0; i < NUM_BACKENDS; i++)
{
if (!VALID_BACKEND(i))
continue;
con = pool_coninfo(proc_id, pool_index, i);
if (con == NULL)
{
elog(WARNING, "failed to get connection info while marking the frontend is connected for pool");
return;
}
con->connected = true;
con->client_connection_time = time(NULL);
}
}
/*
* Unset frontend connected flag
*/
void
pool_coninfo_unset_frontend_connected(int proc_id, int pool_index)
{
ConnectionInfo *con;
int i;
for (i = 0; i < NUM_BACKENDS; i++)
{
if (!VALID_BACKEND(i))
continue;
con = pool_coninfo(proc_id, pool_index, i);
if (con == NULL)
{
elog(WARNING, "failed to get connection info while marking the frontend is not connected for pool");
return;
}
con->connected = false;
con->client_disconnection_time = time(NULL);
}
}
/*
* Set an alarm clock and a signal handler.
* For pool_alarm_undo(), the alarm second and the old handler
* are saved, and the remaining time is calculated.
*/
void
pool_alarm(pool_sighandler_t handler, unsigned int second)
{
POOL_PROCESS_CONTEXT *p = pool_get_process_context();
time_t now = time(NULL);
alarm(second);
p->last_alarm_handler = pool_signal(SIGALRM, handler);
if (p->last_alarm_second)
{
p->undo_alarm_second = p->last_alarm_second - (now - p->last_alarm_time);
if (p->undo_alarm_second <= 0)
p->undo_alarm_second = 1;
}
p->last_alarm_time = now;
p->last_alarm_second = second;
}
/*
* Undo the alarm signal handler using the remaining time.
*/
void
pool_undo_alarm(void)
{
POOL_PROCESS_CONTEXT *p = pool_get_process_context();
if (p->undo_alarm_second)
{
alarm(p->undo_alarm_second);
pool_signal(SIGALRM, p->last_alarm_handler);
p->undo_alarm_second = 0;
}
else
{
alarm(0);
pool_signal(SIGALRM, SIG_IGN);
}
}
|