root / snf-pithos-backend / pithos / backends / util.py @ cf53943b
History | View | Annotate | Download (5.8 kB)
1 |
# Copyright 2011-2012 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
from objpool import ObjectPool |
35 |
from new import instancemethod |
36 |
from select import select |
37 |
from traceback import print_exc |
38 |
from pithos.backends import connect_backend |
39 |
|
40 |
USAGE_LIMIT = 500
|
41 |
|
42 |
|
43 |
class PithosBackendPool(ObjectPool): |
44 |
def __init__(self, size=None, db_module=None, db_connection=None, |
45 |
block_module=None, block_path=None, block_umask=None, |
46 |
queue_module=None, queue_hosts=None, |
47 |
queue_exchange=None, free_versioning=True, |
48 |
quotaholder_enabled=True,
|
49 |
quotaholder_url=None, quotaholder_token=None, |
50 |
quotaholder_client_poolsize=None,
|
51 |
block_params=None,
|
52 |
public_url_security=None,
|
53 |
public_url_alphabet=None,
|
54 |
account_quota_policy=None,
|
55 |
container_quota_policy=None,
|
56 |
container_versioning_policy=None
|
57 |
): |
58 |
super(PithosBackendPool, self).__init__(size=size) |
59 |
self.db_module = db_module
|
60 |
self.db_connection = db_connection
|
61 |
self.block_module = block_module
|
62 |
self.block_path = block_path
|
63 |
self.block_umask = block_umask
|
64 |
self.queue_module = queue_module
|
65 |
self.block_params = block_params
|
66 |
self.queue_hosts = queue_hosts
|
67 |
self.queue_exchange = queue_exchange
|
68 |
self.quotaholder_enabled = quotaholder_enabled
|
69 |
self.quotaholder_url = quotaholder_url
|
70 |
self.quotaholder_token = quotaholder_token
|
71 |
self.quotaholder_client_poolsize = quotaholder_client_poolsize
|
72 |
self.free_versioning = free_versioning
|
73 |
self.public_url_security = public_url_security
|
74 |
self.public_url_alphabet = public_url_alphabet
|
75 |
self.account_quota_policy = account_quota_policy
|
76 |
self.container_quota_policy = container_quota_policy
|
77 |
self.container_versioning_policy = container_versioning_policy
|
78 |
|
79 |
def _pool_create(self): |
80 |
backend = connect_backend( |
81 |
db_module=self.db_module,
|
82 |
db_connection=self.db_connection,
|
83 |
block_module=self.block_module,
|
84 |
block_path=self.block_path,
|
85 |
block_umask=self.block_umask,
|
86 |
queue_module=self.queue_module,
|
87 |
block_params=self.block_params,
|
88 |
queue_hosts=self.queue_hosts,
|
89 |
queue_exchange=self.queue_exchange,
|
90 |
quotaholder_enabled=self.quotaholder_enabled,
|
91 |
quotaholder_url=self.quotaholder_url,
|
92 |
quotaholder_token=self.quotaholder_token,
|
93 |
quotaholder_client_poolsize=self.quotaholder_client_poolsize,
|
94 |
free_versioning=self.free_versioning,
|
95 |
public_url_security=self.public_url_security,
|
96 |
public_url_alphabet=self.public_url_alphabet,
|
97 |
account_quota_policy=self.account_quota_policy,
|
98 |
container_quota_policy=self.container_quota_policy,
|
99 |
container_versioning_policy=self.container_versioning_policy)
|
100 |
|
101 |
backend._real_close = backend.close |
102 |
backend.close = instancemethod(_pooled_backend_close, backend, |
103 |
type(backend))
|
104 |
backend._pool = self
|
105 |
backend._use_count = USAGE_LIMIT |
106 |
backend.messages = [] |
107 |
return backend
|
108 |
|
109 |
def _pool_verify(self, backend): |
110 |
wrapper = backend.wrapper |
111 |
conn = wrapper.conn |
112 |
if conn.closed:
|
113 |
return False |
114 |
|
115 |
if conn.in_transaction():
|
116 |
conn.close() |
117 |
return False |
118 |
|
119 |
try:
|
120 |
fd = conn.connection.connection.fileno() |
121 |
r, w, x = select([fd], (), (), 0)
|
122 |
if r:
|
123 |
conn.close() |
124 |
return False |
125 |
except:
|
126 |
print_exc() |
127 |
return False |
128 |
|
129 |
return True |
130 |
|
131 |
def _pool_cleanup(self, backend): |
132 |
c = backend._use_count - 1
|
133 |
if c < 0: |
134 |
backend._real_close() |
135 |
return True |
136 |
|
137 |
backend._use_count = c |
138 |
wrapper = backend.wrapper |
139 |
if wrapper.trans is not None: |
140 |
conn = wrapper.conn |
141 |
if conn.closed:
|
142 |
wrapper.trans = None
|
143 |
else:
|
144 |
wrapper.rollback() |
145 |
backend.messages = [] |
146 |
return False |
147 |
|
148 |
|
149 |
def _pooled_backend_close(backend): |
150 |
backend._pool.pool_put(backend) |