forked from MapServer/MapServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmappool.c
475 lines (377 loc) · 16.9 KB
/
mappool.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
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/******************************************************************************
* $Id$
*
* Project: MapServer
* Purpose: Implement new style connection pooling.
* Author: Frank Warmerdam, warmerdam@pobox.com
*
******************************************************************************
* Copyright (c) 1996-2005 Regents of the University of Minnesota.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies of this Software or works derived from this Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
******************************************************************************
New MapServer Connection Pooling
================================
This attempts to describe how the new connection pooling support (introduced
Sept/2004) works and what the maintainer of a connection type needs to do
to take advantage of it.
First, the new connection pooling support makes the assumption that
a connection can be abstracted as a void pointer. Further it assumes that
the connection identify is unique defined by the connection string and
the connection type on layers. So if two layers have the same connection
type and connection string that they can share a connection handle.
The old connection sharing was based on searching previous layers in the
same map for a layer to copy an existing connection from. That was ok for
sharing connection between layers in the same map, but didn't address the
need to share connections over a longer term as is the case in a FastCGI
situation where the cgi is long running and handles many CGI requests.
The mapool.c code essentially maintains a cache of open connections with the
following information for each:
- connectiontype / connection
- connection handle
- reference count
- life span indicator
- callback function to close the connection.
The life span indicator is controlled by the CLOSE_CONNECTION PROCESSING
option on the layer(s). If this is set to NORMAL (or not set at all) the
connection will be closed as soon as the reference count drops to zero. This
is MS_LIFE_ZEROREF, and in normal use results in connections only be shared
between layers in a given map as the connection will be closed when no layers
are open against it anymore. The other possible value for the CLOSE_CONNECTION
option is DEFER which basically results in the connection being kept open
till the application closes (msCleanup() will ensure the connection is closed
on exit if called). This case is called MS_LIFE_FOREVER.
The CLOSE_CONNECTION=ALWAYS setting provides to suppress the connection pooling
for a particular layer. In this case the MS_LIFE_SINGLE setting is used, which
ensures that a new connection is created for each request and it is always
closed when the connection is released. This kind of connection cannot be reused.
The callback is a function provided with the connection handle when it is
registered. It takes a single "void *" argument which is the connection
handle.
Updating a Driver
-----------------
The following are the steps to ensure a particular format/database supports
connection pooling (ie. mappostgis.c, maporacle.c, mapogr.cpp, etc)
We will use POSTGIS names for convenience.
1) in msPOSTGISLayerOpen() call msConnPoolRequest(layer) in order to get
a database connection.
layerinfo->conn = (PGconn *) msConnPoolRequest( layer );
2) In msPOSTGISLayerOpen(): if msConnPoolRequest() returned NULL then
manually open a connection to the database (ie. PQconnectcb()) and then
register this handle with the pool API by calling msmsConnPoolRegister().
layerinfo->conn = PQconnectdb( layer->connection );
if (PQstatus(layerinfo->conn) == CONNECTION_BAD)
<report error>
else
msConnPoolRegister( layer, layerinfo->conn,
msPOSTGISCloseConnection );
3) Implement a callback function (msPOSTGISCloseConnection) that the
connection pooling API can call when it wants to close the connection.
static void msPOSTGISCloseConnection( void *conn_handle )
{
PQfinish( (PGconn*) conn_handle );
}
4) In msPOSTGISLayerClose() release the connection handle instead of
directly closing it.
msConnPoolRelease( layer, layerinfo->conn );
layerinfo->conn = NULL;
5) If there was any use of msCheckConnection() or the "sameconnection"
member of the layerObj, then old style connection pooling is already
present. Remove it.
Thats it!
Other Notes
-----------
o The connection pooling API will report details about connection
registrations, requests, releases and closes if the layer debug flag is
on for the layers in question.
o The connection pooling API will let a connection be used/referenced multiple
times from a single thread, but will not allow a connection to be shared
between different threads concurrently. But if a connection is released
by one thread, it is available for use by another thread.
****************************************************************************/
#include "mapserver.h"
#include "mapthread.h"
/* defines for lifetime.
A positive number is a time-from-last use in seconds */
#define MS_LIFE_FOREVER -1
#define MS_LIFE_ZEROREF -2
#define MS_LIFE_SINGLE -3
typedef struct {
enum MS_CONNECTION_TYPE connectiontype;
char *connection;
int lifespan;
int ref_count;
void* thread_id;
int debug;
time_t last_used;
void *conn_handle;
void (*close)( void * );
} connectionObj;
/*
** These static structures are protected by the TLOCK_POOL mutex.
*/
static int connectionCount = 0;
static int connectionMax = 0;
static connectionObj *connections = NULL;
/************************************************************************/
/* msConnPoolRegister() */
/* */
/* Register a new connection with the connection pool tracker. */
/************************************************************************/
void msConnPoolRegister( layerObj *layer,
void *conn_handle,
void (*close_func)( void * ) )
{
const char *close_connection = NULL;
connectionObj *conn = NULL;
if( layer->debug )
msDebug( "msConnPoolRegister(%s,%s,%p)\n",
layer->name, layer->connection, conn_handle );
/* -------------------------------------------------------------------- */
/* We can't meaningful keep a connection with no connection or */
/* connection type string on the layer. */
/* -------------------------------------------------------------------- */
if( layer->connection == NULL ) {
if( layer->tileindex != NULL
&& layer->connectiontype == MS_OGR ) {
/* this is ok, no need to make a fuss */
} else {
msDebug( "%s: Missing CONNECTION on layer %s.\n",
"msConnPoolRegister()",
layer->name );
msSetError( MS_MISCERR,
"Missing CONNECTION on layer %s.",
"msConnPoolRegister()",
layer->name );
}
return;
}
/* -------------------------------------------------------------------- */
/* Grow the array of connection information objects if needed. */
/* -------------------------------------------------------------------- */
msAcquireLock( TLOCK_POOL );
if( connectionCount == connectionMax ) {
connectionMax += 10;
connectionObj* newConnections = (connectionObj *)
realloc(connections,
sizeof(connectionObj) * connectionMax );
if( newConnections == NULL ) {
msSetError(MS_MEMERR, NULL, "msConnPoolRegister()");
msReleaseLock( TLOCK_POOL );
return;
}
connections = newConnections;
}
/* -------------------------------------------------------------------- */
/* Set the new connection information. */
/* -------------------------------------------------------------------- */
conn = connections + connectionCount;
connectionCount++;
conn->connectiontype = layer->connectiontype;
conn->connection = msStrdup( layer->connection );
conn->close = close_func;
conn->ref_count = 1;
conn->thread_id = msGetThreadId();
conn->last_used = time(NULL);
conn->conn_handle = conn_handle;
conn->debug = layer->debug;
/* -------------------------------------------------------------------- */
/* Categorize the connection handling information. */
/* -------------------------------------------------------------------- */
close_connection =
msLayerGetProcessingKey( layer, "CLOSE_CONNECTION" );
if( close_connection == NULL )
close_connection = "NORMAL";
if( strcasecmp(close_connection,"NORMAL") == 0 )
conn->lifespan = MS_LIFE_ZEROREF;
else if( strcasecmp(close_connection,"DEFER") == 0 )
conn->lifespan = MS_LIFE_FOREVER;
else if( strcasecmp(close_connection,"ALWAYS") == 0 )
conn->lifespan = MS_LIFE_SINGLE;
else {
msDebug("msConnPoolRegister(): "
"Unrecognised CLOSE_CONNECTION value '%s'\n",
close_connection );
msSetError( MS_MISCERR, "Unrecognised CLOSE_CONNECTION value '%s'",
"msConnPoolRegister()",
close_connection );
conn->lifespan = MS_LIFE_ZEROREF;
}
msReleaseLock( TLOCK_POOL );
}
/************************************************************************/
/* msConnPoolClose() */
/* */
/* Close the indicated connection. The index in the connection */
/* table is passed. Remove the connection from the table as */
/* well. */
/************************************************************************/
static void msConnPoolClose( int conn_index )
{
connectionObj *conn = connections + conn_index;
if( conn->ref_count > 0 ) {
if( conn->debug )
msDebug( "msConnPoolClose(): "
"Closing connection %s even though ref_count=%d.\n",
conn->connection, conn->ref_count );
msSetError( MS_MISCERR,
"Closing connection %s even though ref_count=%d.",
"msConnPoolClose()",
conn->connection,
conn->ref_count );
}
if( conn->debug )
msDebug( "msConnPoolClose(%s,%p)\n",
conn->connection, conn->conn_handle );
if( conn->close != NULL )
conn->close( conn->conn_handle );
/* free malloced() stuff in this connection */
free( conn->connection );
connectionCount--;
if( connectionCount == 0 ) {
/* if there are no connections left we will "cleanup". */
connectionMax = 0;
free( connections );
connections = NULL;
} else {
/* move the last connection in place of our now closed one */
memcpy( connections + conn_index,
connections + connectionCount,
sizeof(connectionObj) );
}
}
/************************************************************************/
/* msConnPoolRequest() */
/* */
/* Ask for a connection from the connection pool for use with */
/* the current layer. If found (CONNECTION and CONNECTIONTYPE */
/* match) then return it and up the ref count. Otherwise */
/* return NULL. */
/************************************************************************/
void *msConnPoolRequest( layerObj *layer )
{
int i;
const char* close_connection;
if( layer->connection == NULL )
return NULL;
/* check if we must always create a new connection */
close_connection = msLayerGetProcessingKey( layer, "CLOSE_CONNECTION" );
if( close_connection && strcasecmp(close_connection,"ALWAYS") == 0 )
return NULL;
msAcquireLock( TLOCK_POOL );
for( i = 0; i < connectionCount; i++ ) {
connectionObj *conn = connections + i;
if( layer->connectiontype == conn->connectiontype
&& strcasecmp( layer->connection, conn->connection ) == 0
&& (conn->ref_count == 0 || conn->thread_id == msGetThreadId())
&& conn->lifespan != MS_LIFE_SINGLE) {
void *conn_handle = NULL;
conn->ref_count++;
conn->thread_id = msGetThreadId();
conn->last_used = time(NULL);
if( layer->debug ) {
msDebug( "msConnPoolRequest(%s,%s) -> got %p\n",
layer->name, layer->connection, conn->conn_handle );
conn->debug = layer->debug;
}
conn_handle = conn->conn_handle;
msReleaseLock( TLOCK_POOL );
return conn_handle;
}
}
msReleaseLock( TLOCK_POOL );
return NULL;
}
/************************************************************************/
/* msConnPoolRelease() */
/* */
/* Release the passed connection for the given layer. */
/* Internally the reference count is dropped, and the */
/* connection may be closed. We assume the caller has already */
/* acquired the pool lock. */
/************************************************************************/
void msConnPoolRelease( layerObj *layer, void *conn_handle )
{
int i;
if( layer->debug )
msDebug( "msConnPoolRelease(%s,%s,%p)\n",
layer->name, layer->connection, conn_handle );
if( layer->connection == NULL )
return;
msAcquireLock( TLOCK_POOL );
for( i = 0; i < connectionCount; i++ ) {
connectionObj *conn = connections + i;
if( layer->connectiontype == conn->connectiontype
&& strcasecmp( layer->connection, conn->connection ) == 0
&& conn->conn_handle == conn_handle ) {
conn->ref_count--;
conn->last_used = time(NULL);
if( conn->ref_count == 0 )
conn->thread_id = 0;
if( conn->ref_count == 0 && (conn->lifespan == MS_LIFE_ZEROREF || conn->lifespan == MS_LIFE_SINGLE) )
msConnPoolClose( i );
msReleaseLock( TLOCK_POOL );
return;
}
}
msReleaseLock( TLOCK_POOL );
msDebug( "%s: Unable to find handle for layer '%s'.\n",
"msConnPoolRelease()",
layer->name );
msSetError( MS_MISCERR,
"Unable to find handle for layer '%s'.",
"msConnPoolRelease()",
layer->name );
}
/************************************************************************/
/* msConnPoolMapCloseUnreferenced() */
/* */
/* Close any unreferenced connections. */
/************************************************************************/
void msConnPoolCloseUnreferenced()
{
int i;
/* this really needs to be commented out before commiting. */
/* msDebug( "msConnPoolCloseUnreferenced()\n" ); */
msAcquireLock( TLOCK_POOL );
for( i = connectionCount - 1; i >= 0; i-- ) {
connectionObj *conn = connections + i;
if( conn->ref_count == 0 ) {
/* for now we don't assume the locks are re-entrant, so release */
/* it so msConnPoolClose() can get it. */
msConnPoolClose( i );
}
}
msReleaseLock( TLOCK_POOL );
}
/************************************************************************/
/* msConnPoolFinalCleanup() */
/* */
/* Close any remaining open connections. This is normally */
/* called just before (voluntary) application termination. */
/************************************************************************/
void msConnPoolFinalCleanup()
{
/* this really needs to be commented out before commiting. */
/* msDebug( "msConnPoolFinalCleanup()\n" ); */
msAcquireLock( TLOCK_POOL );
while( connectionCount > 0 )
msConnPoolClose( 0 );
msReleaseLock( TLOCK_POOL );
}