util.py 5.92 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# Copyright 2011-2012 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
#   1. Redistributions of source code must retain the above
#      copyright notice, this list of conditions and the following
#      disclaimer.
#
#   2. Redistributions in binary form must reproduce the above
#      copyright notice, this list of conditions and the following
#      disclaimer in the documentation and/or other materials
#      provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.

34
from objpool import ObjectPool
35
36
37
38
39
40
41
42
43
44
45
from new import instancemethod
from select import select
from traceback import print_exc
from pithos.backends import connect_backend

USAGE_LIMIT = 500


class PithosBackendPool(ObjectPool):
    def __init__(self, size=None, db_module=None, db_connection=None,
                 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
50
                 astakos_url=None, service_token=None,
                 astakosclient_poolsize=None,
51
                 block_params=None,
52
                 public_url_security=None,
53
54
55
56
57
                 public_url_alphabet=None,
                 account_quota_policy=None,
                 container_quota_policy=None,
                 container_versioning_policy=None
        ):
58
59
60
61
62
63
        super(PithosBackendPool, self).__init__(size=size)
        self.db_module = db_module
        self.db_connection = db_connection
        self.block_module = block_module
        self.block_path = block_path
        self.block_umask = block_umask
64
65
        self.block_size = block_size
        self.hash_algorithm = hash_algorithm
66
        self.queue_module = queue_module
67
        self.block_params = block_params
68
69
        self.queue_hosts = queue_hosts
        self.queue_exchange = queue_exchange
70
71
72
        self.astakos_url = astakos_url
        self.service_token = service_token
        self.astakosclient_poolsize = astakosclient_poolsize
73
        self.free_versioning = free_versioning
74
75
76
77
78
        self.public_url_security = public_url_security
        self.public_url_alphabet = public_url_alphabet
        self.account_quota_policy = account_quota_policy
        self.container_quota_policy = container_quota_policy
        self.container_versioning_policy = container_versioning_policy
79
80

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

        backend._real_close = backend.close
        backend.close = instancemethod(_pooled_backend_close, backend,
                                       type(backend))
        backend._pool = self
        backend._use_count = USAGE_LIMIT
108
        backend.messages = []
109
110
111
112
113
114
115
116
117
118
119
120
121
122
        return backend

    def _pool_verify(self, backend):
        wrapper = backend.wrapper
        conn = wrapper.conn
        if conn.closed:
            return False

        if conn.in_transaction():
            conn.close()
            return False

        try:
            fd = conn.connection.connection.fileno()
123
124
125
126
127
128
129
130
131
132
133
        except AttributeError:
            # probably sqlite, assume success
            pass
        else:
            try:
                r, w, x = select([fd], (), (), 0)
                if r:
                    conn.close()
                    return False
            except:
                print_exc()
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
                return False

        return True

    def _pool_cleanup(self, backend):
        c = backend._use_count - 1
        if c < 0:
            backend._real_close()
            return True

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


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