author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> |
Wed, 10 Jun 2009 16:13:29 +0200 | |
branch | stable |
changeset 2092 | f5102472243d |
parent 2063 | fe4278b50388 |
child 2765 | 5e2525d7b1b1 |
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 |
||
36 |
def commit(self): |
|
37 |
"""commit the current transaction for this user""" |
|
38 |
# FIXME: what happends if a commit fail |
|
39 |
# would need a two phases commit or like, but I don't know how to do |
|
40 |
# this using the db-api... |
|
41 |
for source, cnx in self.source_cnxs.values(): |
|
42 |
# let exception propagates |
|
43 |
cnx.commit() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
44 |
|
0 | 45 |
def rollback(self): |
46 |
"""rollback the current transaction for this user""" |
|
47 |
for source, cnx in self.source_cnxs.values(): |
|
48 |
# catch exceptions, rollback other sources anyway |
|
49 |
try: |
|
50 |
cnx.rollback() |
|
51 |
except: |
|
52 |
source.critical('rollback error', exc_info=sys.exc_info()) |
|
53 |
||
54 |
def close(self, i_know_what_i_do=False): |
|
55 |
"""close all connections in the pool""" |
|
56 |
if i_know_what_i_do is not True: # unexpected closing safety belt |
|
57 |
raise RuntimeError('pool shouldn\'t be closed') |
|
58 |
for cu in self._cursors.values(): |
|
59 |
try: |
|
60 |
cu.close() |
|
61 |
except: |
|
62 |
continue |
|
63 |
for _, cnx in self.source_cnxs.values(): |
|
64 |
try: |
|
65 |
cnx.close() |
|
66 |
except: |
|
67 |
continue |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
68 |
|
0 | 69 |
# internals ############################################################### |
70 |
||
2063
fe4278b50388
fix [re]set_pool prototype
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
71 |
def pool_set(self): |
0 | 72 |
"""pool is being set""" |
73 |
self.check_connections() |
|
74 |
||
2063
fe4278b50388
fix [re]set_pool prototype
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
75 |
def pool_reset(self): |
0 | 76 |
"""pool is being reseted""" |
77 |
for source, cnx in self.source_cnxs.values(): |
|
78 |
source.pool_reset(cnx) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
79 |
|
0 | 80 |
def __getitem__(self, uri): |
81 |
"""subscription notation provide access to sources'cursors""" |
|
82 |
try: |
|
83 |
cursor = self._cursors[uri] |
|
84 |
except KeyError: |
|
85 |
cursor = self.source_cnxs[uri][1].cursor() |
|
86 |
if cursor is not None: |
|
87 |
# None possible on sources without cursor support such as ldap |
|
88 |
self._cursors[uri] = cursor |
|
89 |
return cursor |
|
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 |
|
95 |
yield self.source_cnxs['system'] |
|
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 |
||
110 |
def reconnect(self, source): |
|
111 |
"""reopen a connection for this source""" |
|
112 |
source.info('trying to reconnect') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
113 |
self.source_cnxs[source.uri] = (source, source.get_connection()) |
0 | 114 |
del self._cursors[source.uri] |
115 |
||
116 |
def check_connections(self): |
|
117 |
for source, cnx in self.source_cnxs.itervalues(): |
|
118 |
newcnx = source.check_connection(cnx) |
|
119 |
if newcnx is not None: |
|
120 |
self.reset_connection(source, newcnx) |
|
121 |
||
122 |
def reset_connection(self, source, cnx): |
|
123 |
self.source_cnxs[source.uri] = (source, cnx) |
|
124 |
self._cursors.pop(source.uri, None) |
|
125 |
||
126 |
||
127 |
class Operation(object): |
|
128 |
"""an operation is triggered on connections pool events related to |
|
129 |
commit / rollback transations. Possible events are: |
|
130 |
||
131 |
precommit: |
|
132 |
the pool is preparing to commit. You shouldn't do anything things which |
|
133 |
has to be reverted if the commit fail at this point, but you can freely |
|
134 |
do any heavy computation or raise an exception if the commit can't go. |
|
135 |
You can add some new operation during this phase but their precommit |
|
136 |
event won't be triggered |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
137 |
|
0 | 138 |
commit: |
139 |
the pool is preparing to commit. You should avoid to do to expensive |
|
140 |
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
|
141 |
|
0 | 142 |
revertcommit: |
143 |
if an operation failed while commited, this event is triggered for |
|
144 |
all operations which had their commit event already to let them |
|
145 |
revert things (including the operation which made fail the commit) |
|
146 |
||
147 |
rollback: |
|
148 |
the transaction has been either rollbacked either |
|
149 |
* intentionaly |
|
150 |
* a precommit event failed, all operations are rollbacked |
|
151 |
* a commit event failed, all operations which are not been triggered for |
|
152 |
commit are rollbacked |
|
153 |
||
154 |
order of operations may be important, and is controlled according to: |
|
155 |
* operation's class |
|
156 |
""" |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
157 |
|
0 | 158 |
def __init__(self, session, **kwargs): |
159 |
self.session = session |
|
160 |
self.user = session.user |
|
161 |
self.repo = session.repo |
|
162 |
self.schema = session.repo.schema |
|
163 |
self.config = session.repo.config |
|
164 |
self.__dict__.update(kwargs) |
|
165 |
self.register(session) |
|
166 |
# execution information |
|
167 |
self.processed = None # 'precommit', 'commit' |
|
168 |
self.failed = False |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
169 |
|
0 | 170 |
def register(self, session): |
171 |
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
|
172 |
|
0 | 173 |
def insert_index(self): |
174 |
"""return the index of the lastest instance which is not a |
|
175 |
LateOperation instance |
|
176 |
""" |
|
177 |
for i, op in enumerate(self.session.pending_operations): |
|
178 |
if isinstance(op, (LateOperation, SingleLastOperation)): |
|
179 |
return i |
|
180 |
return None |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
181 |
|
0 | 182 |
def handle_event(self, event): |
183 |
"""delegate event handling to the opertaion""" |
|
184 |
getattr(self, event)() |
|
185 |
||
186 |
def precommit_event(self): |
|
187 |
"""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
|
188 |
|
0 | 189 |
def revertprecommit_event(self): |
190 |
"""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
|
191 |
|
0 | 192 |
should revert pre-commit's changes but take care, they may have not |
193 |
been all considered if it's this operation which failed |
|
194 |
""" |
|
195 |
||
196 |
def commit_event(self): |
|
197 |
"""the observed connections pool is commiting""" |
|
198 |
raise NotImplementedError() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
199 |
|
0 | 200 |
def revertcommit_event(self): |
201 |
"""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
|
202 |
|
0 | 203 |
should revert commit's changes but take care, they may have not |
204 |
been all considered if it's this operation which failed |
|
205 |
""" |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
206 |
|
0 | 207 |
def rollback_event(self): |
208 |
"""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
|
209 |
|
0 | 210 |
do nothing by default, the operation will just be removed from the pool |
211 |
operation list |
|
212 |
""" |
|
213 |
||
214 |
||
215 |
class PreCommitOperation(Operation): |
|
216 |
"""base class for operation only defining a precommit operation |
|
217 |
""" |
|
218 |
||
219 |
def precommit_event(self): |
|
220 |
"""the observed connections pool is preparing a commit""" |
|
221 |
raise NotImplementedError() |
|
222 |
||
223 |
def commit_event(self): |
|
224 |
"""the observed connections pool is commiting""" |
|
225 |
||
226 |
||
227 |
class LateOperation(Operation): |
|
228 |
"""special operation which should be called after all possible (ie non late) |
|
229 |
operations |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
230 |
""" |
0 | 231 |
def insert_index(self): |
232 |
"""return the index of the lastest instance which is not a |
|
233 |
SingleLastOperation instance |
|
234 |
""" |
|
235 |
for i, op in enumerate(self.session.pending_operations): |
|
236 |
if isinstance(op, SingleLastOperation): |
|
237 |
return i |
|
238 |
return None |
|
239 |
||
240 |
||
241 |
class SingleOperation(Operation): |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
242 |
"""special operation which should be called once""" |
0 | 243 |
def register(self, session): |
244 |
"""override register to handle cases where this operation has already |
|
245 |
been added |
|
246 |
""" |
|
247 |
operations = session.pending_operations |
|
248 |
index = self.equivalent_index(operations) |
|
249 |
if index is not None: |
|
250 |
equivalent = operations.pop(index) |
|
251 |
else: |
|
252 |
equivalent = None |
|
253 |
session.add_operation(self, self.insert_index()) |
|
254 |
return equivalent |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
255 |
|
0 | 256 |
def equivalent_index(self, operations): |
257 |
"""return the index of the equivalent operation if any""" |
|
258 |
equivalents = [i for i, op in enumerate(operations) |
|
259 |
if op.__class__ is self.__class__] |
|
260 |
if equivalents: |
|
261 |
return equivalents[0] |
|
262 |
return None |
|
263 |
||
264 |
||
265 |
class SingleLastOperation(SingleOperation): |
|
266 |
"""special operation which should be called once and after all other |
|
267 |
operations |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1132
diff
changeset
|
268 |
""" |
0 | 269 |
def insert_index(self): |
270 |
return None |
|
271 |
||
272 |
from logging import getLogger |
|
273 |
from cubicweb import set_log_methods |
|
274 |
set_log_methods(Operation, getLogger('cubicweb.session')) |