author | Aurelien Campeas <aurelien.campeas@logilab.fr> |
Wed, 09 Sep 2009 18:38:25 +0200 | |
branch | 3.5 |
changeset 3155 | 14e30e2520d7 |
parent 2765 | 5e2525d7b1b1 |
child 2835 | 04034421b072 |
child 3691 | ccd72f500daa |
permissions | -rw-r--r-- |
0 | 1 |
"""CubicWeb server connections pool : |
2 |
||
3 |
* the rql repository has a limited number of connections pools, each of them |
|
4 |
dealing with a set of connections on each source used by the repository |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
5 |
|
0 | 6 |
* operation may be registered by hooks during a transaction, which will be |
7 |
fired when the pool is commited or rollbacked |
|
8 |
||
9 |
This module defined the `ConnectionsPool` class and a set of abstract classes |
|
10 |
for operation. |
|
11 |
||
12 |
||
13 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
14 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 15 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
16 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 17 |
""" |
18 |
__docformat__ = "restructuredtext en" |
|
19 |
||
20 |
import sys |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
21 |
|
0 | 22 |
class ConnectionsPool(object): |
23 |
"""handle connections on a set of sources, at some point associated to a |
|
24 |
user session |
|
25 |
""" |
|
26 |
||
27 |
def __init__(self, sources): |
|
28 |
# dictionnary of (source, connection), indexed by sources'uri |
|
29 |
self.source_cnxs = {} |
|
30 |
for source in sources: |
|
31 |
self.source_cnxs[source.uri] = (source, source.get_connection()) |
|
32 |
if not 'system' in self.source_cnxs: |
|
33 |
self.source_cnxs['system'] = self.source_cnxs[sources[0].uri] |
|
34 |
self._cursors = {} |
|
35 |
||
2765
5e2525d7b1b1
reconnect without argument reconnect all sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
36 |
def __getitem__(self, uri): |
5e2525d7b1b1
reconnect without argument reconnect all sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
37 |
"""subscription notation provide access to sources'cursors""" |
5e2525d7b1b1
reconnect without argument reconnect all sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
38 |
try: |
5e2525d7b1b1
reconnect without argument reconnect all sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
39 |
cursor = self._cursors[uri] |
5e2525d7b1b1
reconnect without argument reconnect all sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
40 |
except KeyError: |
5e2525d7b1b1
reconnect without argument reconnect all sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
41 |
cursor = self.source_cnxs[uri][1].cursor() |
5e2525d7b1b1
reconnect without argument reconnect all sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
42 |
if cursor is not None: |
5e2525d7b1b1
reconnect without argument reconnect all sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
43 |
# None possible on sources without cursor support such as ldap |
5e2525d7b1b1
reconnect without argument reconnect all sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
44 |
self._cursors[uri] = cursor |
5e2525d7b1b1
reconnect without argument reconnect all sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
45 |
return cursor |
5e2525d7b1b1
reconnect without argument reconnect all sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
46 |
|
0 | 47 |
def commit(self): |
48 |
"""commit the current transaction for this user""" |
|
49 |
# FIXME: what happends if a commit fail |
|
50 |
# would need a two phases commit or like, but I don't know how to do |
|
51 |
# this using the db-api... |
|
52 |
for source, cnx in self.source_cnxs.values(): |
|
53 |
# let exception propagates |
|
54 |
cnx.commit() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
55 |
|
0 | 56 |
def rollback(self): |
57 |
"""rollback the current transaction for this user""" |
|
58 |
for source, cnx in self.source_cnxs.values(): |
|
59 |
# catch exceptions, rollback other sources anyway |
|
60 |
try: |
|
61 |
cnx.rollback() |
|
62 |
except: |
|
63 |
source.critical('rollback error', exc_info=sys.exc_info()) |
|
64 |
||
65 |
def close(self, i_know_what_i_do=False): |
|
66 |
"""close all connections in the pool""" |
|
67 |
if i_know_what_i_do is not True: # unexpected closing safety belt |
|
68 |
raise RuntimeError('pool shouldn\'t be closed') |
|
69 |
for cu in self._cursors.values(): |
|
70 |
try: |
|
71 |
cu.close() |
|
72 |
except: |
|
73 |
continue |
|
74 |
for _, cnx in self.source_cnxs.values(): |
|
75 |
try: |
|
76 |
cnx.close() |
|
77 |
except: |
|
78 |
continue |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
79 |
|
0 | 80 |
# internals ############################################################### |
81 |
||
2063
fe4278b50388
fix [re]set_pool prototype
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
82 |
def pool_set(self): |
0 | 83 |
"""pool is being set""" |
84 |
self.check_connections() |
|
85 |
||
2063
fe4278b50388
fix [re]set_pool prototype
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
86 |
def pool_reset(self): |
0 | 87 |
"""pool is being reseted""" |
88 |
for source, cnx in self.source_cnxs.values(): |
|
89 |
source.pool_reset(cnx) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
90 |
|
0 | 91 |
def sources(self): |
92 |
"""return the source objects handled by this pool""" |
|
93 |
# implementation details of flying insert requires the system source |
|
94 |
# first |
|
2765
5e2525d7b1b1
reconnect without argument reconnect all sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
95 |
yield self.source_cnxs['system'][0] |
0 | 96 |
for uri, (source, cursor) in self.source_cnxs.items(): |
97 |
if uri == 'system': |
|
98 |
continue |
|
99 |
yield source |
|
100 |
#return [source_cnx[0] for source_cnx in self.source_cnxs.values()] |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
101 |
|
0 | 102 |
def source(self, uid): |
103 |
"""return the source object with the given uri""" |
|
104 |
return self.source_cnxs[uid][0] |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
105 |
|
0 | 106 |
def connection(self, uid): |
107 |
"""return the connection on the source object with the given uri""" |
|
108 |
return self.source_cnxs[uid][1] |
|
109 |
||
2765
5e2525d7b1b1
reconnect without argument reconnect all sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
110 |
def reconnect(self, source=None): |
5e2525d7b1b1
reconnect without argument reconnect all sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
111 |
"""reopen a connection for this source or all sources if none specified |
5e2525d7b1b1
reconnect without argument reconnect all sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
112 |
""" |
5e2525d7b1b1
reconnect without argument reconnect all sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
113 |
if source is None: |
5e2525d7b1b1
reconnect without argument reconnect all sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
114 |
sources = self.sources() |
5e2525d7b1b1
reconnect without argument reconnect all sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
115 |
else: |
5e2525d7b1b1
reconnect without argument reconnect all sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
116 |
sources = (source,) |
5e2525d7b1b1
reconnect without argument reconnect all sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
117 |
for source in sources: |
5e2525d7b1b1
reconnect without argument reconnect all sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
118 |
source.info('trying to reconnect') |
5e2525d7b1b1
reconnect without argument reconnect all sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
119 |
self.source_cnxs[source.uri] = (source, source.get_connection()) |
5e2525d7b1b1
reconnect without argument reconnect all sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
120 |
self._cursors.pop(source.uri, None) |
0 | 121 |
|
122 |
def check_connections(self): |
|
123 |
for source, cnx in self.source_cnxs.itervalues(): |
|
124 |
newcnx = source.check_connection(cnx) |
|
125 |
if newcnx is not None: |
|
126 |
self.reset_connection(source, newcnx) |
|
127 |
||
128 |
def reset_connection(self, source, cnx): |
|
129 |
self.source_cnxs[source.uri] = (source, cnx) |
|
130 |
self._cursors.pop(source.uri, None) |
|
131 |
||
132 |
||
133 |
class Operation(object): |
|
134 |
"""an operation is triggered on connections pool events related to |
|
135 |
commit / rollback transations. Possible events are: |
|
136 |
||
137 |
precommit: |
|
138 |
the pool is preparing to commit. You shouldn't do anything things which |
|
139 |
has to be reverted if the commit fail at this point, but you can freely |
|
140 |
do any heavy computation or raise an exception if the commit can't go. |
|
141 |
You can add some new operation during this phase but their precommit |
|
142 |
event won't be triggered |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
143 |
|
0 | 144 |
commit: |
145 |
the pool is preparing to commit. You should avoid to do to expensive |
|
146 |
stuff or something that may cause an exception in this event |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
147 |
|
0 | 148 |
revertcommit: |
149 |
if an operation failed while commited, this event is triggered for |
|
150 |
all operations which had their commit event already to let them |
|
151 |
revert things (including the operation which made fail the commit) |
|
152 |
||
153 |
rollback: |
|
154 |
the transaction has been either rollbacked either |
|
155 |
* intentionaly |
|
156 |
* a precommit event failed, all operations are rollbacked |
|
157 |
* a commit event failed, all operations which are not been triggered for |
|
158 |
commit are rollbacked |
|
159 |
||
160 |
order of operations may be important, and is controlled according to: |
|
161 |
* operation's class |
|
162 |
""" |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
163 |
|
0 | 164 |
def __init__(self, session, **kwargs): |
165 |
self.session = session |
|
166 |
self.user = session.user |
|
167 |
self.repo = session.repo |
|
168 |
self.schema = session.repo.schema |
|
169 |
self.config = session.repo.config |
|
170 |
self.__dict__.update(kwargs) |
|
171 |
self.register(session) |
|
172 |
# execution information |
|
173 |
self.processed = None # 'precommit', 'commit' |
|
174 |
self.failed = False |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
175 |
|
0 | 176 |
def register(self, session): |
177 |
session.add_operation(self, self.insert_index()) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
178 |
|
0 | 179 |
def insert_index(self): |
180 |
"""return the index of the lastest instance which is not a |
|
181 |
LateOperation instance |
|
182 |
""" |
|
183 |
for i, op in enumerate(self.session.pending_operations): |
|
184 |
if isinstance(op, (LateOperation, SingleLastOperation)): |
|
185 |
return i |
|
186 |
return None |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
187 |
|
0 | 188 |
def handle_event(self, event): |
189 |
"""delegate event handling to the opertaion""" |
|
190 |
getattr(self, event)() |
|
191 |
||
192 |
def precommit_event(self): |
|
193 |
"""the observed connections pool is preparing a commit""" |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
194 |
|
0 | 195 |
def revertprecommit_event(self): |
196 |
"""an error went when pre-commiting this operation or a later one |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
197 |
|
0 | 198 |
should revert pre-commit's changes but take care, they may have not |
199 |
been all considered if it's this operation which failed |
|
200 |
""" |
|
201 |
||
202 |
def commit_event(self): |
|
203 |
"""the observed connections pool is commiting""" |
|
204 |
raise NotImplementedError() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
205 |
|
0 | 206 |
def revertcommit_event(self): |
207 |
"""an error went when commiting this operation or a later one |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
208 |
|
0 | 209 |
should revert commit's changes but take care, they may have not |
210 |
been all considered if it's this operation which failed |
|
211 |
""" |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
212 |
|
0 | 213 |
def rollback_event(self): |
214 |
"""the observed connections pool has been rollbacked |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
215 |
|
0 | 216 |
do nothing by default, the operation will just be removed from the pool |
217 |
operation list |
|
218 |
""" |
|
219 |
||
220 |
||
221 |
class PreCommitOperation(Operation): |
|
222 |
"""base class for operation only defining a precommit operation |
|
223 |
""" |
|
224 |
||
225 |
def precommit_event(self): |
|
226 |
"""the observed connections pool is preparing a commit""" |
|
227 |
raise NotImplementedError() |
|
228 |
||
229 |
def commit_event(self): |
|
230 |
"""the observed connections pool is commiting""" |
|
231 |
||
232 |
||
233 |
class LateOperation(Operation): |
|
234 |
"""special operation which should be called after all possible (ie non late) |
|
235 |
operations |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
236 |
""" |
0 | 237 |
def insert_index(self): |
238 |
"""return the index of the lastest instance which is not a |
|
239 |
SingleLastOperation instance |
|
240 |
""" |
|
241 |
for i, op in enumerate(self.session.pending_operations): |
|
242 |
if isinstance(op, SingleLastOperation): |
|
243 |
return i |
|
244 |
return None |
|
245 |
||
246 |
||
247 |
class SingleOperation(Operation): |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
248 |
"""special operation which should be called once""" |
0 | 249 |
def register(self, session): |
250 |
"""override register to handle cases where this operation has already |
|
251 |
been added |
|
252 |
""" |
|
253 |
operations = session.pending_operations |
|
254 |
index = self.equivalent_index(operations) |
|
255 |
if index is not None: |
|
256 |
equivalent = operations.pop(index) |
|
257 |
else: |
|
258 |
equivalent = None |
|
259 |
session.add_operation(self, self.insert_index()) |
|
260 |
return equivalent |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
261 |
|
0 | 262 |
def equivalent_index(self, operations): |
263 |
"""return the index of the equivalent operation if any""" |
|
264 |
equivalents = [i for i, op in enumerate(operations) |
|
265 |
if op.__class__ is self.__class__] |
|
266 |
if equivalents: |
|
267 |
return equivalents[0] |
|
268 |
return None |
|
269 |
||
270 |
||
271 |
class SingleLastOperation(SingleOperation): |
|
272 |
"""special operation which should be called once and after all other |
|
273 |
operations |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
274 |
""" |
0 | 275 |
def insert_index(self): |
276 |
return None |
|
277 |
||
278 |
from logging import getLogger |
|
279 |
from cubicweb import set_log_methods |
|
280 |
set_log_methods(Operation, getLogger('cubicweb.session')) |