dbopen.txt 8.93 KB
Newer Older
1 2 3 4
=====================
Connection Management
=====================

5 6 7

Here we exercise the connection management done by the DB class.

8 9
    >>> from ZODB import DB
    >>> from ZODB.MappingStorage import MappingStorage as Storage
10 11 12

Capturing log messages from DB is important for some of the examples:

13 14
    >>> from zope.testing.loggingsupport import InstalledHandler
    >>> handler = InstalledHandler('ZODB.DB')
15 16 17

Create a storage, and wrap it in a DB wrapper:

18 19
    >>> st = Storage()
    >>> db = DB(st)
20 21 22

By default, we can open 7 connections without any log messages:

23 24 25
    >>> conns = [db.open() for dummy in range(7)]
    >>> handler.records
    []
26 27 28

Open one more, and we get a warning:

29 30 31 32 33 34
    >>> conns.append(db.open())
    >>> len(handler.records)
    1
    >>> msg = handler.records[0]
    >>> print msg.name, msg.levelname, msg.getMessage()
    ZODB.DB WARNING DB.open() has 8 open connections with a pool_size of 7
35 36 37

Open 6 more, and we get 6 more warnings:

38 39 40 41 42 43 44 45
    >>> conns.extend([db.open() for dummy in range(6)])
    >>> len(conns)
    14
    >>> len(handler.records)
    7
    >>> msg = handler.records[-1]
    >>> print msg.name, msg.levelname, msg.getMessage()
    ZODB.DB WARNING DB.open() has 14 open connections with a pool_size of 7
46 47 48 49

Add another, so that it's more than twice the default, and the level
rises to critical:

50 51 52 53 54 55 56 57
    >>> conns.append(db.open())
    >>> len(conns)
    15
    >>> len(handler.records)
    8
    >>> msg = handler.records[-1]
    >>> print msg.name, msg.levelname, msg.getMessage()
    ZODB.DB CRITICAL DB.open() has 15 open connections with a pool_size of 7
58 59 60 61

While it's boring, it's important to verify that the same relationships
hold if the default pool size is overridden.

62 63 64 65 66 67 68 69
    >>> handler.clear()
    >>> st.close()
    >>> st = Storage()
    >>> PS = 2 # smaller pool size
    >>> db = DB(st, pool_size=PS)
    >>> conns = [db.open() for dummy in range(PS)]
    >>> handler.records
    []
70 71 72

A warning for opening one more:

73 74 75 76 77 78
    >>> conns.append(db.open())
    >>> len(handler.records)
    1
    >>> msg = handler.records[0]
    >>> print msg.name, msg.levelname, msg.getMessage()
    ZODB.DB WARNING DB.open() has 3 open connections with a pool_size of 2
79 80 81

More warnings through 4 connections:

82 83 84 85 86 87 88 89
    >>> conns.extend([db.open() for dummy in range(PS-1)])
    >>> len(conns)
    4
    >>> len(handler.records)
    2
    >>> msg = handler.records[-1]
    >>> print msg.name, msg.levelname, msg.getMessage()
    ZODB.DB WARNING DB.open() has 4 open connections with a pool_size of 2
90 91 92

And critical for going beyond that:

93 94 95 96 97 98 99 100
    >>> conns.append(db.open())
    >>> len(conns)
    5
    >>> len(handler.records)
    3
    >>> msg = handler.records[-1]
    >>> print msg.name, msg.levelname, msg.getMessage()
    ZODB.DB CRITICAL DB.open() has 5 open connections with a pool_size of 2
101 102 103

We can change the pool size on the fly:

104 105 106 107 108 109 110 111 112 113 114
    >>> handler.clear()
    >>> db.setPoolSize(6)
    >>> conns.append(db.open())
    >>> handler.records  # no log msg -- the pool is bigger now
    []
    >>> conns.append(db.open()) # but one more and there's a warning again
    >>> len(handler.records)
    1
    >>> msg = handler.records[0]
    >>> print msg.name, msg.levelname, msg.getMessage()
    ZODB.DB WARNING DB.open() has 7 open connections with a pool_size of 6
115 116 117

Enough of that.

118 119
    >>> handler.clear()
    >>> st.close()
120 121 122 123 124

More interesting is the stack-like nature of connection reuse.  So long as
we keep opening new connections, and keep them alive, all connections
returned are distinct:

125 126 127 128 129 130 131
    >>> st = Storage()
    >>> db = DB(st)
    >>> c1 = db.open()
    >>> c2 = db.open()
    >>> c3 = db.open()
    >>> c1 is c2 or c1 is c3 or c2 is c3
    False
132 133 134 135

Let's put some markers on the connections, so we can identify these
specific objects later:

136 137 138
    >>> c1.MARKER = 'c1'
    >>> c2.MARKER = 'c2'
    >>> c3.MARKER = 'c3'
139 140 141

Now explicitly close c1 and c2:

142 143
    >>> c1.close()
    >>> c2.close()
144 145 146 147 148

Reaching into the internals, we can see that db's connection pool now has
two connections available for reuse, and knows about three connections in
all:

149
    >>> pool = db.pool
150 151 152 153
    >>> len(pool.available)
    2
    >>> len(pool.all)
    3
154 155 156 157

Since we closed c2 last, it's at the top of the available stack, so will
be reused by the next open():

158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
    >>> c1 = db.open()
    >>> c1.MARKER
    'c2'
    >>> len(pool.available), len(pool.all)
    (1, 3)

    >>> c3.close()  # now the stack has c3 on top, then c1
    >>> c2 = db.open()
    >>> c2.MARKER
    'c3'
    >>> len(pool.available), len(pool.all)
    (1, 3)
    >>> c3 = db.open()
    >>> c3.MARKER
    'c1'
    >>> len(pool.available), len(pool.all)
    (0, 3)
175 176 177 178 179 180 181 182 183 184

What about the 3 in pool.all?  We've seen that closing connections doesn't
reduce pool.all, and it would be bad if DB kept connections alive forever.

In fact pool.all is a "weak set" of connections -- it holds weak references
to connections.  That alone doesn't keep connection objects alive.  The
weak set allows DB's statistics methods to return info about connections
that are still alive.


185 186
    >>> len(db.cacheDetailSize())  # one result for each connection's cache
    3
187 188 189 190 191 192

If a connection object is abandoned (it becomes unreachable), then it
will vanish from pool.all automatically.  However, connections are
involved in cycles, so exactly when a connection vanishes from pool.all
isn't predictable.  It can be forced by running gc.collect():

193 194 195 196 197 198 199 200
    >>> import gc
    >>> dummy = gc.collect()
    >>> len(pool.all)
    3
    >>> c3 = None
    >>> dummy = gc.collect()  # removes c3 from pool.all
    >>> len(pool.all)
    2
201 202 203 204

Note that c3 is really gone; in particular it didn't get added back to
the stack of available connections by magic:

205 206
    >>> len(pool.available)
    0
207 208 209

Nothing in that last block should have logged any msgs:

210 211
    >>> handler.records
    []
212 213 214 215

If "too many" connections are open, then closing one may kick an older
closed one out of the available connection stack.

216 217 218 219 220 221
    >>> st.close()
    >>> st = Storage()
    >>> db = DB(st, pool_size=3)
    >>> conns = [db.open() for dummy in range(6)]
    >>> len(handler.records)  # 3 warnings for the "excess" connections
    3
222
    >>> pool = db.pool
223 224
    >>> len(pool.available), len(pool.all)
    (0, 6)
225 226 227

Let's mark them:

228 229
    >>> for i, c in enumerate(conns):
    ...     c.MARKER = i
230 231 232

Closing connections adds them to the stack:

233 234 235 236 237
    >>> for i in range(3):
    ...     conns[i].close()
    >>> len(pool.available), len(pool.all)
    (3, 6)
    >>> del conns[:3]  # leave the ones with MARKERs 3, 4 and 5
238 239 240 241

Closing another one will purge the one with MARKER 0 from the stack
(since it was the first added to the stack):

242
    >>> [c.MARKER for (t, c) in pool.available]
243 244 245 246
    [0, 1, 2]
    >>> conns[0].close()  # MARKER 3
    >>> len(pool.available), len(pool.all)
    (3, 5)
247
    >>> [c.MARKER for (t, c) in pool.available]
248
    [1, 2, 3]
249 250 251

Similarly for the other two:

252 253 254
    >>> conns[1].close(); conns[2].close()
    >>> len(pool.available), len(pool.all)
    (3, 3)
255
    >>> [c.MARKER for (t, c) in pool.available]
256
    [3, 4, 5]
257 258 259

Reducing the pool size may also purge the oldest closed connections:

260 261 262
    >>> db.setPoolSize(2)  # gets rid of MARKER 3
    >>> len(pool.available), len(pool.all)
    (2, 2)
263
    >>> [c.MARKER for (t, c) in pool.available]
264
    [4, 5]
265 266 267 268

Since MARKER 5 is still the last one added to the stack, it will be the
first popped:

269 270 271 272 273
    >>> c1 = db.open(); c2 = db.open()
    >>> c1.MARKER, c2.MARKER
    (5, 4)
    >>> len(pool.available), len(pool.all)
    (0, 2)
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
Next:  when a closed Connection is removed from .available due to exceeding
pool_size, that Connection's cache is cleared (this behavior was new in
ZODB 3.6b6).  While user code may still hold a reference to that
Connection, once it vanishes from .available it's really not usable for
anything sensible (it can never be in the open state again).  Waiting for
gc to reclaim the Connection and its cache eventually works, but that can
take "a long time" and caches can hold on to many objects, and limited
resources (like RDB connections), for the duration.

    >>> st.close()
    >>> st = Storage()
    >>> db = DB(st, pool_size=2)
    >>> conn0 = db.open()
    >>> len(conn0._cache)  # empty now
    0
    >>> import transaction
    >>> conn0.root()['a'] = 1
    >>> transaction.commit()
    >>> len(conn0._cache)  # but now the cache holds the root object
    1

Now open more connections so that the total exceeds pool_size (2):

    >>> conn1 = db.open()
    >>> conn2 = db.open()
300
    >>> pool = db.pool
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
    >>> len(pool.all), len(pool.available)  # all Connections are in use
    (3, 0)

Return pool_size (2) Connections to the pool:

    >>> conn0.close()
    >>> conn1.close()
    >>> len(pool.all), len(pool.available)
    (3, 2)
    >>> len(conn0._cache)  # nothing relevant has changed yet
    1

When we close the third connection, conn0 will be booted from .all, and
we expect its cache to be cleared then:

    >>> conn2.close()
    >>> len(pool.all), len(pool.available)
    (2, 2)
    >>> len(conn0._cache)  # conn0's cache is empty again
    0
    >>> del conn0, conn1, conn2

323 324
Clean up.

325 326
    >>> st.close()
    >>> handler.uninstall()