Statistics
| Branch: | Tag: | Revision:

root / snf-pithos-backend / pithos / backends / util.py @ d0b67cbc

History | View | Annotate | Download (6.5 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
                 block_size=None, hash_algorithm=None,
47
                 queue_module=None, queue_hosts=None,
48
                 queue_exchange=None, free_versioning=True,
49
                 astakos_auth_url=None, service_token=None,
50
                 astakosclient_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
                 archipelago_conf_file=None,
58
                 xseg_pool_size=8,
59
                 map_check_interval=None):
60
        super(PithosBackendPool, self).__init__(size=size)
61
        self.db_module = db_module
62
        self.db_connection = db_connection
63
        self.block_module = block_module
64
        self.block_path = block_path
65
        self.block_umask = block_umask
66
        self.block_size = block_size
67
        self.hash_algorithm = hash_algorithm
68
        self.queue_module = queue_module
69
        self.block_params = block_params
70
        self.queue_hosts = queue_hosts
71
        self.queue_exchange = queue_exchange
72
        self.astakos_auth_url = astakos_auth_url
73
        self.service_token = service_token
74
        self.astakosclient_poolsize = astakosclient_poolsize
75
        self.free_versioning = free_versioning
76
        self.public_url_security = public_url_security
77
        self.public_url_alphabet = public_url_alphabet
78
        self.account_quota_policy = account_quota_policy
79
        self.container_quota_policy = container_quota_policy
80
        self.container_versioning_policy = container_versioning_policy
81
        self.archipelago_conf_file = archipelago_conf_file
82
        self.xseg_pool_size = xseg_pool_size
83
        self.map_check_interval = map_check_interval
84

    
85
    def _pool_create(self):
86
        backend = connect_backend(
87
            db_module=self.db_module,
88
            db_connection=self.db_connection,
89
            block_module=self.block_module,
90
            block_path=self.block_path,
91
            block_umask=self.block_umask,
92
            block_size=self.block_size,
93
            hash_algorithm=self.hash_algorithm,
94
            queue_module=self.queue_module,
95
            block_params=self.block_params,
96
            queue_hosts=self.queue_hosts,
97
            queue_exchange=self.queue_exchange,
98
            astakos_auth_url=self.astakos_auth_url,
99
            service_token=self.service_token,
100
            astakosclient_poolsize=self.astakosclient_poolsize,
101
            free_versioning=self.free_versioning,
102
            public_url_security=self.public_url_security,
103
            public_url_alphabet=self.public_url_alphabet,
104
            account_quota_policy=self.account_quota_policy,
105
            container_quota_policy=self.container_quota_policy,
106
            container_versioning_policy=self.container_versioning_policy,
107
            archipelago_conf_file=self.archipelago_conf_file,
108
            xseg_pool_size=self.xseg_pool_size,
109
            map_check_interval=self.map_check_interval)
110

    
111
        backend._real_close = backend.close
112
        backend.close = instancemethod(_pooled_backend_close, backend,
113
                                       type(backend))
114
        backend._pool = self
115
        backend._use_count = USAGE_LIMIT
116
        backend.messages = []
117
        return backend
118

    
119
    def _pool_verify(self, backend):
120
        wrapper = backend.wrapper
121
        conn = wrapper.conn
122
        if conn.closed:
123
            return False
124

    
125
        if conn.in_transaction():
126
            conn.close()
127
            return False
128

    
129
        try:
130
            fd = conn.connection.connection.fileno()
131
        except AttributeError:
132
            # probably sqlite, assume success
133
            pass
134
        else:
135
            try:
136
                r, w, x = select([fd], (), (), 0)
137
                if r:
138
                    conn.close()
139
                    return False
140
            except:
141
                print_exc()
142
                return False
143

    
144
        return True
145

    
146
    def _pool_cleanup(self, backend):
147
        c = backend._use_count - 1
148
        if c < 0:
149
            backend._real_close()
150
            return True
151

    
152
        backend._use_count = c
153
        wrapper = backend.wrapper
154
        if wrapper.trans is not None:
155
            conn = wrapper.conn
156
            if conn.closed:
157
                wrapper.trans = None
158
            else:
159
                wrapper.rollback()
160
        backend.messages = []
161
        return False
162

    
163
    def shutdown(self):
164
        while True:
165
            backend = self.pool_get(create=False)
166
            if backend is None:
167
                break
168
            self.pool_put(None)
169
            backend._real_close()
170

    
171

    
172
def _pooled_backend_close(backend):
173
    backend._pool.pool_put(backend)