Commit 7c665d9a authored by Tim Peters's avatar Tim Peters

Merge ZODB trunk changes checked in from a wrong project.

r29290 | frerich | 2005-02-24 17:36:00 -0500 (Thu, 24 Feb 2005)
Changed paths:
   M /Zope3/trunk/src/ZODB/tests/dbopen.txt
   ...
   minor editing

r29247 | gintautasm | 2005-02-22 06:40:26 -0500 (Tue, 22 Feb 2005)
Changed paths:
   M /Zope3/trunk/src/BTrees/Interfaces.py
   ...
   More minor nitpicks.  This should be the last one.
parent 98da6a92
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
from zope.interface import Interface from zope.interface import Interface
class ICollection(Interface): class ICollection(Interface):
def clear(): def clear():
...@@ -42,6 +43,7 @@ class IReadSequence(Interface): ...@@ -42,6 +43,7 @@ class IReadSequence(Interface):
to, but not including, index2. to, but not including, index2.
""" """
class IKeyed(ICollection): class IKeyed(ICollection):
def has_key(key): def has_key(key):
...@@ -76,6 +78,7 @@ class IKeyed(ICollection): ...@@ -76,6 +78,7 @@ class IKeyed(ICollection):
greater than or equal to the argument. greater than or equal to the argument.
""" """
class ISetMutable(IKeyed): class ISetMutable(IKeyed):
def insert(key): def insert(key):
...@@ -88,29 +91,34 @@ class ISetMutable(IKeyed): ...@@ -88,29 +91,34 @@ class ISetMutable(IKeyed):
"""Remove the key from the set.""" """Remove the key from the set."""
def update(seq): def update(seq):
"""Add the items from the given sequence to the set""" """Add the items from the given sequence to the set."""
class ISized(Interface): class ISized(Interface):
"anything supporting __len" """An object that supports __len__."""
def __len__(): def __len__():
"""Return the number of items in the container""" """Return the number of items in the container."""
class IKeySequence(IKeyed, ISized): class IKeySequence(IKeyed, ISized):
def __getitem__(index): def __getitem__(index):
"""Return the key in the given index position """Return the key in the given index position.
This allows iteration with for loops and use in functions, This allows iteration with for loops and use in functions,
like map and list, that read sequences. like map and list, that read sequences.
""" """
class ISet(IKeySequence, ISetMutable): class ISet(IKeySequence, ISetMutable):
pass pass
class ITreeSet(IKeyed, ISetMutable): class ITreeSet(IKeyed, ISetMutable):
pass pass
class IMinimalDictionary(ISized): class IMinimalDictionary(ISized):
def has_key(key): def has_key(key):
...@@ -205,6 +213,7 @@ class IDictionaryIsh(IKeyed, IMinimalDictionary): ...@@ -205,6 +213,7 @@ class IDictionaryIsh(IKeyed, IMinimalDictionary):
integer values, the normalization is division. integer values, the normalization is division.
""" """
class IBTree(IDictionaryIsh): class IBTree(IDictionaryIsh):
def insert(key, value): def insert(key, value):
...@@ -226,6 +235,7 @@ class IBTree(IDictionaryIsh): ...@@ -226,6 +235,7 @@ class IBTree(IDictionaryIsh):
key=generate_key() key=generate_key()
""" """
class IMerge(Interface): class IMerge(Interface):
"""Object with methods for merging sets, buckets, and trees. """Object with methods for merging sets, buckets, and trees.
...@@ -275,6 +285,7 @@ class IMerge(Interface): ...@@ -275,6 +285,7 @@ class IMerge(Interface):
collections. collections.
""" """
class IIMerge(IMerge): class IIMerge(IMerge):
"""Merge collections with integer value type. """Merge collections with integer value type.
...@@ -347,6 +358,7 @@ class IIMerge(IMerge): ...@@ -347,6 +358,7 @@ class IIMerge(IMerge):
Note that c1 and c2 must be collections. Note that c1 and c2 must be collections.
""" """
class IMergeIntegerKey(IMerge): class IMergeIntegerKey(IMerge):
"""IMerge-able objects with integer keys. """IMerge-able objects with integer keys.
......
############################################################################## =====================
# Connection Management
# Copyright (c) 2004 Zope Corporation and Contributors. =====================
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE
#
##############################################################################
Here we exercise the connection management done by the DB class. Here we exercise the connection management done by the DB class.
>>> from ZODB import DB >>> from ZODB import DB
>>> from ZODB.MappingStorage import MappingStorage as Storage >>> from ZODB.MappingStorage import MappingStorage as Storage
Capturing log messages from DB is important for some of the examples: Capturing log messages from DB is important for some of the examples:
>>> from zope.testing.loggingsupport import InstalledHandler >>> from zope.testing.loggingsupport import InstalledHandler
>>> handler = InstalledHandler('ZODB.DB') >>> handler = InstalledHandler('ZODB.DB')
Create a storage, and wrap it in a DB wrapper: Create a storage, and wrap it in a DB wrapper:
>>> st = Storage() >>> st = Storage()
>>> db = DB(st) >>> db = DB(st)
By default, we can open 7 connections without any log messages: By default, we can open 7 connections without any log messages:
>>> conns = [db.open() for dummy in range(7)] >>> conns = [db.open() for dummy in range(7)]
>>> handler.records >>> handler.records
[] []
Open one more, and we get a warning: Open one more, and we get a warning:
>>> conns.append(db.open()) >>> conns.append(db.open())
>>> len(handler.records) >>> len(handler.records)
1 1
>>> msg = handler.records[0] >>> msg = handler.records[0]
>>> print msg.name, msg.levelname, msg.getMessage() >>> print msg.name, msg.levelname, msg.getMessage()
ZODB.DB WARNING DB.open() has 8 open connections with a pool_size of 7 ZODB.DB WARNING DB.open() has 8 open connections with a pool_size of 7
Open 6 more, and we get 6 more warnings: Open 6 more, and we get 6 more warnings:
>>> conns.extend([db.open() for dummy in range(6)]) >>> conns.extend([db.open() for dummy in range(6)])
>>> len(conns) >>> len(conns)
14 14
>>> len(handler.records) >>> len(handler.records)
7 7
>>> msg = handler.records[-1] >>> msg = handler.records[-1]
>>> print msg.name, msg.levelname, msg.getMessage() >>> print msg.name, msg.levelname, msg.getMessage()
ZODB.DB WARNING DB.open() has 14 open connections with a pool_size of 7 ZODB.DB WARNING DB.open() has 14 open connections with a pool_size of 7
Add another, so that it's more than twice the default, and the level Add another, so that it's more than twice the default, and the level
rises to critical: rises to critical:
>>> conns.append(db.open()) >>> conns.append(db.open())
>>> len(conns) >>> len(conns)
15 15
>>> len(handler.records) >>> len(handler.records)
8 8
>>> msg = handler.records[-1] >>> msg = handler.records[-1]
>>> print msg.name, msg.levelname, msg.getMessage() >>> print msg.name, msg.levelname, msg.getMessage()
ZODB.DB CRITICAL DB.open() has 15 open connections with a pool_size of 7 ZODB.DB CRITICAL DB.open() has 15 open connections with a pool_size of 7
While it's boring, it's important to verify that the same relationships While it's boring, it's important to verify that the same relationships
hold if the default pool size is overridden. hold if the default pool size is overridden.
>>> handler.clear() >>> handler.clear()
>>> st.close() >>> st.close()
>>> st = Storage() >>> st = Storage()
>>> PS = 2 # smaller pool size >>> PS = 2 # smaller pool size
>>> db = DB(st, pool_size=PS) >>> db = DB(st, pool_size=PS)
>>> conns = [db.open() for dummy in range(PS)] >>> conns = [db.open() for dummy in range(PS)]
>>> handler.records >>> handler.records
[] []
A warning for opening one more: A warning for opening one more:
>>> conns.append(db.open()) >>> conns.append(db.open())
>>> len(handler.records) >>> len(handler.records)
1 1
>>> msg = handler.records[0] >>> msg = handler.records[0]
>>> print msg.name, msg.levelname, msg.getMessage() >>> print msg.name, msg.levelname, msg.getMessage()
ZODB.DB WARNING DB.open() has 3 open connections with a pool_size of 2 ZODB.DB WARNING DB.open() has 3 open connections with a pool_size of 2
More warnings through 4 connections: More warnings through 4 connections:
>>> conns.extend([db.open() for dummy in range(PS-1)]) >>> conns.extend([db.open() for dummy in range(PS-1)])
>>> len(conns) >>> len(conns)
4 4
>>> len(handler.records) >>> len(handler.records)
2 2
>>> msg = handler.records[-1] >>> msg = handler.records[-1]
>>> print msg.name, msg.levelname, msg.getMessage() >>> print msg.name, msg.levelname, msg.getMessage()
ZODB.DB WARNING DB.open() has 4 open connections with a pool_size of 2 ZODB.DB WARNING DB.open() has 4 open connections with a pool_size of 2
And critical for going beyond that: And critical for going beyond that:
>>> conns.append(db.open()) >>> conns.append(db.open())
>>> len(conns) >>> len(conns)
5 5
>>> len(handler.records) >>> len(handler.records)
3 3
>>> msg = handler.records[-1] >>> msg = handler.records[-1]
>>> print msg.name, msg.levelname, msg.getMessage() >>> print msg.name, msg.levelname, msg.getMessage()
ZODB.DB CRITICAL DB.open() has 5 open connections with a pool_size of 2 ZODB.DB CRITICAL DB.open() has 5 open connections with a pool_size of 2
We can change the pool size on the fly: We can change the pool size on the fly:
>>> handler.clear() >>> handler.clear()
>>> db.setPoolSize(6) >>> db.setPoolSize(6)
>>> conns.append(db.open()) >>> conns.append(db.open())
>>> handler.records # no log msg -- the pool is bigger now >>> handler.records # no log msg -- the pool is bigger now
[] []
>>> conns.append(db.open()) # but one more and there's a warning again >>> conns.append(db.open()) # but one more and there's a warning again
>>> len(handler.records) >>> len(handler.records)
1 1
>>> msg = handler.records[0] >>> msg = handler.records[0]
>>> print msg.name, msg.levelname, msg.getMessage() >>> print msg.name, msg.levelname, msg.getMessage()
ZODB.DB WARNING DB.open() has 7 open connections with a pool_size of 6 ZODB.DB WARNING DB.open() has 7 open connections with a pool_size of 6
Enough of that. Enough of that.
>>> handler.clear() >>> handler.clear()
>>> st.close() >>> st.close()
More interesting is the stack-like nature of connection reuse. So long as More interesting is the stack-like nature of connection reuse. So long as
we keep opening new connections, and keep them alive, all connections we keep opening new connections, and keep them alive, all connections
returned are distinct: returned are distinct:
>>> st = Storage() >>> st = Storage()
>>> db = DB(st) >>> db = DB(st)
>>> c1 = db.open() >>> c1 = db.open()
>>> c2 = db.open() >>> c2 = db.open()
>>> c3 = db.open() >>> c3 = db.open()
>>> c1 is c2 or c1 is c3 or c2 is c3 >>> c1 is c2 or c1 is c3 or c2 is c3
False False
Let's put some markers on the connections, so we can identify these Let's put some markers on the connections, so we can identify these
specific objects later: specific objects later:
>>> c1.MARKER = 'c1' >>> c1.MARKER = 'c1'
>>> c2.MARKER = 'c2' >>> c2.MARKER = 'c2'
>>> c3.MARKER = 'c3' >>> c3.MARKER = 'c3'
Now explicitly close c1 and c2: Now explicitly close c1 and c2:
>>> c1.close() >>> c1.close()
>>> c2.close() >>> c2.close()
Reaching into the internals, we can see that db's connection pool now has 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 two connections available for reuse, and knows about three connections in
all: all:
>>> pool = db._pools[''] >>> pool = db._pools['']
>>> len(pool.available) >>> len(pool.available)
2 2
>>> len(pool.all) >>> len(pool.all)
3 3
Since we closed c2 last, it's at the top of the available stack, so will Since we closed c2 last, it's at the top of the available stack, so will
be reused by the next open(): be reused by the next open():
>>> c1 = db.open() >>> c1 = db.open()
>>> c1.MARKER >>> c1.MARKER
'c2' 'c2'
>>> len(pool.available), len(pool.all) >>> len(pool.available), len(pool.all)
(1, 3) (1, 3)
>>> c3.close() # now the stack has c3 on top, then c1 >>> c3.close() # now the stack has c3 on top, then c1
>>> c2 = db.open() >>> c2 = db.open()
>>> c2.MARKER >>> c2.MARKER
'c3' 'c3'
>>> len(pool.available), len(pool.all) >>> len(pool.available), len(pool.all)
(1, 3) (1, 3)
>>> c3 = db.open() >>> c3 = db.open()
>>> c3.MARKER >>> c3.MARKER
'c1' 'c1'
>>> len(pool.available), len(pool.all) >>> len(pool.available), len(pool.all)
(0, 3) (0, 3)
What about the 3 in pool.all? We've seen that closing connections doesn't 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. reduce pool.all, and it would be bad if DB kept connections alive forever.
...@@ -191,97 +182,97 @@ weak set allows DB's statistics methods to return info about connections ...@@ -191,97 +182,97 @@ weak set allows DB's statistics methods to return info about connections
that are still alive. that are still alive.
>>> len(db.cacheDetailSize()) # one result for each connection's cache >>> len(db.cacheDetailSize()) # one result for each connection's cache
3 3
If a connection object is abandoned (it becomes unreachable), then it If a connection object is abandoned (it becomes unreachable), then it
will vanish from pool.all automatically. However, connections are will vanish from pool.all automatically. However, connections are
involved in cycles, so exactly when a connection vanishes from pool.all involved in cycles, so exactly when a connection vanishes from pool.all
isn't predictable. It can be forced by running gc.collect(): isn't predictable. It can be forced by running gc.collect():
>>> import gc >>> import gc
>>> dummy = gc.collect() >>> dummy = gc.collect()
>>> len(pool.all) >>> len(pool.all)
3 3
>>> c3 = None >>> c3 = None
>>> dummy = gc.collect() # removes c3 from pool.all >>> dummy = gc.collect() # removes c3 from pool.all
>>> len(pool.all) >>> len(pool.all)
2 2
Note that c3 is really gone; in particular it didn't get added back to Note that c3 is really gone; in particular it didn't get added back to
the stack of available connections by magic: the stack of available connections by magic:
>>> len(pool.available) >>> len(pool.available)
0 0
Nothing in that last block should have logged any msgs: Nothing in that last block should have logged any msgs:
>>> handler.records >>> handler.records
[] []
If "too many" connections are open, then closing one may kick an older If "too many" connections are open, then closing one may kick an older
closed one out of the available connection stack. closed one out of the available connection stack.
>>> st.close() >>> st.close()
>>> st = Storage() >>> st = Storage()
>>> db = DB(st, pool_size=3) >>> db = DB(st, pool_size=3)
>>> conns = [db.open() for dummy in range(6)] >>> conns = [db.open() for dummy in range(6)]
>>> len(handler.records) # 3 warnings for the "excess" connections >>> len(handler.records) # 3 warnings for the "excess" connections
3 3
>>> pool = db._pools[''] >>> pool = db._pools['']
>>> len(pool.available), len(pool.all) >>> len(pool.available), len(pool.all)
(0, 6) (0, 6)
Let's mark them: Let's mark them:
>>> for i, c in enumerate(conns): >>> for i, c in enumerate(conns):
... c.MARKER = i ... c.MARKER = i
Closing connections adds them to the stack: Closing connections adds them to the stack:
>>> for i in range(3): >>> for i in range(3):
... conns[i].close() ... conns[i].close()
>>> len(pool.available), len(pool.all) >>> len(pool.available), len(pool.all)
(3, 6) (3, 6)
>>> del conns[:3] # leave the ones with MARKERs 3, 4 and 5 >>> del conns[:3] # leave the ones with MARKERs 3, 4 and 5
Closing another one will purge the one with MARKER 0 from the stack Closing another one will purge the one with MARKER 0 from the stack
(since it was the first added to the stack): (since it was the first added to the stack):
>>> [c.MARKER for c in pool.available] >>> [c.MARKER for c in pool.available]
[0, 1, 2] [0, 1, 2]
>>> conns[0].close() # MARKER 3 >>> conns[0].close() # MARKER 3
>>> len(pool.available), len(pool.all) >>> len(pool.available), len(pool.all)
(3, 5) (3, 5)
>>> [c.MARKER for c in pool.available] >>> [c.MARKER for c in pool.available]
[1, 2, 3] [1, 2, 3]
Similarly for the other two: Similarly for the other two:
>>> conns[1].close(); conns[2].close() >>> conns[1].close(); conns[2].close()
>>> len(pool.available), len(pool.all) >>> len(pool.available), len(pool.all)
(3, 3) (3, 3)
>>> [c.MARKER for c in pool.available] >>> [c.MARKER for c in pool.available]
[3, 4, 5] [3, 4, 5]
Reducing the pool size may also purge the oldest closed connections: Reducing the pool size may also purge the oldest closed connections:
>>> db.setPoolSize(2) # gets rid of MARKER 3 >>> db.setPoolSize(2) # gets rid of MARKER 3
>>> len(pool.available), len(pool.all) >>> len(pool.available), len(pool.all)
(2, 2) (2, 2)
>>> [c.MARKER for c in pool.available] >>> [c.MARKER for c in pool.available]
[4, 5] [4, 5]
Since MARKER 5 is still the last one added to the stack, it will be the Since MARKER 5 is still the last one added to the stack, it will be the
first popped: first popped:
>>> c1 = db.open(); c2 = db.open() >>> c1 = db.open(); c2 = db.open()
>>> c1.MARKER, c2.MARKER >>> c1.MARKER, c2.MARKER
(5, 4) (5, 4)
>>> len(pool.available), len(pool.all) >>> len(pool.available), len(pool.all)
(0, 2) (0, 2)
Clean up. Clean up.
>>> st.close() >>> st.close()
>>> handler.uninstall() >>> handler.uninstall()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment