forked from rdio/pgpool2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool_process_context.c
241 lines (208 loc) · 5.31 KB
/
pool_process_context.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
/* -*-pgsql-c-*- */
/*
*
* $Header$
*
* pgpool: a language independent connection pool server for PostgreSQL
* written by Tatsuo Ishii
*
* Copyright (c) 2003-2010 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 "pool.h"
#include "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)
{
pool_error("pool_init_process_context: process_info is not set");
child_exit(1);
}
process_context->process_info = process_info;
if (!pool_config->backend_desc)
{
pool_error("pool_init_process_context: backend_desc is not set");
child_exit(1);
}
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 */
}
/*
* Return process context
*/
POOL_PROCESS_CONTEXT *pool_get_process_context(void)
{
if (!process_context)
{
pool_error("pool_get_process_context: process context is not initialized");
}
return process_context;
}
/*
* Return my process info
*/
ProcessInfo *pool_get_my_process_info(void)
{
if (!process_context)
{
pool_error("pool_get_my_process_info: process context is not initialized");
return NULL;
}
return &process_context->process_info[process_context->proc_id];
}
/*
* Increment local session id
*/
void pool_incremnet_local_session_id(void)
{
POOL_PROCESS_CONTEXT *p = pool_get_process_context();
if (!p)
{
pool_error("pool_incremnet_local_session_id: cannot get process context");
return;
}
p->local_session_id++;
}
/*
* Return byte size of connection info(ConnectionInfo) on shmem.
*/
int pool_coninfo_size(void)
{
int size;
size = pool_config->num_init_children *
pool_config->max_pool *
MAX_NUM_BACKENDS *
sizeof(ConnectionInfo);
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)
{
pool_error("pool_coninfo: invalid child number: %d", child);
return NULL;
}
if (connection_pool < 0 || connection_pool >= pool_config->max_pool)
{
pool_error("pool_coninfo: invalid connection_pool number: %d", connection_pool);
return NULL;
}
if (backend < 0 || backend >= MAX_NUM_BACKENDS)
{
pool_error("pool_coninfo: 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)
{
pool_error("pool_coninfo_pid: invalid child pid: %d", pid);
return NULL;
}
if (child < 0 || child >= pool_config->num_init_children)
{
pool_error("pool_coninfo_pid: invalid child number: %d", child);
return NULL;
}
if (connection_pool < 0 || connection_pool >= pool_config->max_pool)
{
pool_error("pool_coninfo_pid: invalid connection_pool number: %d", connection_pool);
return NULL;
}
if (backend < 0 || backend >= MAX_NUM_BACKENDS)
{
pool_error("pool_coninfo: invalid backend number: %d", backend);
return NULL;
}
return &con_info[child*pool_config->max_pool*MAX_NUM_BACKENDS+
connection_pool*MAX_NUM_BACKENDS+
backend];
}
/*
* 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++)
{
con = pool_coninfo(proc_id, pool_index, i);
if (con == NULL)
{
pool_error("pool_coninfo_set_frontend_connected: cannot get ConnectionInfo");
return;
}
con->connected = true;
}
}
/*
* 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++)
{
con = pool_coninfo(proc_id, pool_index, i);
if (con == NULL)
{
pool_error("pool_coninfo_unset_frontend_connected: cannot get ConnectionInfo");
return;
}
con->connected = false;
}
}