-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathconnection.py
271 lines (206 loc) · 6.6 KB
/
connection.py
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#!/usr/bin/env python3
"""
@author: xi
@since: 2018-04-13
"""
import collections
import queue
import threading
from . import client
from . import conf
from .exceptions import *
from .namespace import Namespace
class Threads(object):
def __init__(self, num_threads, max_tasks):
self._num_threads = num_threads
self._done_lock = threading.Semaphore(0)
self._ready_lock = threading.Semaphore(0)
self._queue = queue.Queue(max_tasks)
self._threads = [
threading.Thread(target=self._target)
for _ in range(num_threads)
]
for thread in self._threads:
thread.setDaemon(True)
thread.start()
def _target(self):
while True:
task = self._queue.get(block=True)
if task is None:
break
fn, args, callback = task
ret = fn(*args)
if callback is not None:
callback(ret)
def put_task(self, fn, args, callback=None):
assert fn is not None
if args is None:
args = ()
self._queue.put((fn, args, callback), block=True)
def wait_all(self):
for _ in range(self._num_threads):
task = (
lambda: self._done_lock.release(),
(),
lambda _: self._ready_lock.acquire()
)
self._queue.put(task, block=True)
for _ in range(self._num_threads):
self._done_lock.acquire()
for _ in range(self._num_threads):
self._ready_lock.release()
def terminate(self):
for _ in range(self._num_threads):
self._queue.put(None, block=True)
for thread in self._threads:
thread.join()
class Connection(object):
def __init__(self, on_close, zkquorum):
"""Connection.
Args:
on_close: Callback when connection close.
zkquorum (str): Zookeeper quorum. Comma-separated list of hosts to connect to.
e.g., '127.0.0.1:2181,127.0.0.1:2182,[::1]:2183'
Raises:
TransportError
ZookeeperProtocolError
NoSuchZookeeperNodeError
"""
self._on_close = on_close
self._zkquorum = zkquorum
self._client = client.Client(zkquorum)
self._namespaces = dict()
self._threads = Threads(conf.num_threads_per_conn, conf.num_tasks_per_conn)
@property
def zkquorum(self):
return self._zkquorum
@property
def client(self):
"""Client object.
Returns:
client.Client: Client object.
"""
return self._client
@property
def threads(self):
return self._threads
def close(self):
self._threads.wait_all()
self._on_close(self)
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.close()
def namespaces(self):
"""List namespaces.
Returns:
list[str]: List of namespace names.
"""
return self._client.namespaces()
def namespace(self, name, create_if_not_exists=True):
"""Get a namespace object.
Args:
name (str): Name of the namespace.
create_if_not_exists (bool): Create a new namespace if the required namespace does not exist.
Returns:
Namespace: Namespace object.
Raises:
NamespaceNotFoundError
NamespaceExistError
ServerIOError
RequestError
TransportError
ZookeeperProtocolError
ServiceProtocolError
NoSuchZookeeperNodeError
"""
if name in self._namespaces:
return self._namespaces[name]
try:
self._client.namespace(name)
except NamespaceNotFoundError as e:
if create_if_not_exists:
self._client.create_namespace(name)
else:
raise e
ns = Namespace(self, name)
self._namespaces[name] = ns
return ns
def __getitem__(self, name):
"""Get a namespace object.
If the namespace does not exist, always create a new one.
Args:
name (str): Name of the namespace.
Returns:
Namespace: Namespace object.
Raises:
NamespaceNotFoundError
ServerIOError
RequestError
TransportError
ZookeeperProtocolError
ServiceProtocolError
NoSuchZookeeperNodeError
"""
return self.namespace(name)
def create_namespace(self, name, props=None):
"""Create a namespace.
Args:
name (str): Name of the namespace.
props (dict[str, str]): Custom properties.
Raises:
NamespaceExistError
ServerIOError
RequestError
TransportError
ZookeeperProtocolError
ServiceProtocolError
NoSuchZookeeperNodeError
"""
self._client.create_namespace(name, props)
def delete_namespace(self, name):
"""Delete a namespace.
Args:
name (str): Namespace name.
Raises:
NamespaceNotFoundError
ServerIOError
RequestError
TransportError
ZookeeperProtocolError
ServiceProtocolError
NoSuchZookeeperNodeError
"""
self._client.delete_namespace(name)
if name in self._namespaces:
del self._namespaces[name]
class ConnectionPool(object):
def __init__(self, zkquorum, max_size=10):
"""Connection pool.
Args:
zkquorum (str): Zookeeper quorum. Comma-separated list of hosts to connect to.
e.g., '127.0.0.1:2181,127.0.0.1:2182,[::1]:2183'
max_size (int): Max pool size.
"""
self._zkquorum = zkquorum
self._max_size = max_size
self._conns = collections.deque()
def connect(self):
"""Get a database connection.
Returns:
Connection: A connection object.
"""
if len(self._conns) > 0:
return self._conns.pop(0)
else:
return Connection(self._on_conn_close, self._zkquorum)
def _on_conn_close(self, conn):
"""Callback when connection close.
Not really close(delete) a connection, just put it back to the pool.
Args:
conn (Connection): Connection object to close.
"""
if len(self._conns) < self._max_size:
self._conns.append(conn)
else:
del conn