-
Notifications
You must be signed in to change notification settings - Fork 174
/
aerospike.py
284 lines (245 loc) · 9.06 KB
/
aerospike.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
272
273
274
275
276
277
278
279
280
281
282
283
284
import time
import typing
from typing import Any, Dict, Iterator, Optional, Tuple, Union
try: # pragma: no cover
import aerospike
except ImportError: # pragma: no cover
aerospike = None # noqa
from yarl import URL
from faust.stores import base
from faust.types import TP, AppT, CollectionT
if typing.TYPE_CHECKING: # pragma: no cover
from aerospike import SCAN_PRIORITY_MEDIUM, TTL_NEVER_EXPIRE, Client
else:
class Client: # noqa
"""Dummy Client."""
TTL_NEVER_EXPIRE = -1
SCAN_PRIORITY_MEDIUM = 2
if typing.TYPE_CHECKING: # pragma: no cover
import aerospike.exception.RecordNotFound
else:
class RecordNotFound(Exception): # noqa
"""Dummy Exception."""
aerospike_client: Client = None
class AeroSpikeStore(base.SerializedStore):
"""Aerospike table storage."""
client: Client
ttl: int
policies: typing.Mapping[str, Any]
BIN_KEY = "value_key"
USERNAME_KEY = "username"
HOSTS_KEY = "hosts"
PASSWORD_KEY = "password" # nosec
NAMESPACE_KEY = "namespace"
TTL_KEY = "ttl"
POLICIES_KEY = "policies"
CLIENT_OPTIONS_KEY = "client"
def __init__(
self,
url: Union[str, URL],
app: AppT,
table: CollectionT,
options: typing.Mapping[str, Any] = None,
**kwargs: Any,
) -> None:
try:
self.client = AeroSpikeStore.get_aerospike_client(options)
self.namespace = options.get(self.NAMESPACE_KEY, "")
self.ttl = options.get(self.TTL_KEY, aerospike.TTL_NEVER_EXPIRE)
self.policies = options.get(self.POLICIES_KEY, None)
table.use_partitioner = True
except Exception as ex:
self.logger.error(f"Error configuring aerospike client {ex}")
raise ex
super().__init__(url, app, table, **kwargs)
@staticmethod
def get_aerospike_client(aerospike_config: Dict[Any, Any]) -> Client:
global aerospike_client
if aerospike_client:
return aerospike_client
else:
client = aerospike.client(
aerospike_config.get(AeroSpikeStore.CLIENT_OPTIONS_KEY)
)
try:
client.connect(
aerospike_config.get(AeroSpikeStore.USERNAME_KEY),
aerospike_config.get(AeroSpikeStore.PASSWORD_KEY),
)
aerospike_client = client
return client
except Exception as e:
raise e
def _get(self, key: bytes) -> Optional[bytes]:
key = (self.namespace, self.table_name, key)
fun = self.client.get
try:
(key, meta, bins) = self.aerospike_fun_call_with_retry(fun=fun, key=key)
if bins:
return bins[self.BIN_KEY]
return None
except aerospike.exception.RecordNotFound as ex:
self.log.debug(f"key not found {key} exception {ex}")
raise KeyError(f"key not found {key}")
except Exception as ex:
self.log.error(
f"Error in set for table {self.table_name} exception {ex} key {key}"
)
raise ex
def _set(self, key: bytes, value: Optional[bytes]) -> None:
try:
fun = self.client.put
key = (self.namespace, self.table_name, key)
vt = {self.BIN_KEY: value}
self.aerospike_fun_call_with_retry(
fun=fun,
key=key,
bins=vt,
meta={"ttl": self.ttl},
policy={
"exists": aerospike.POLICY_EXISTS_IGNORE,
"key": aerospike.POLICY_KEY_SEND,
},
)
except Exception as ex:
self.log.error(
f"FaustAerospikeException Error in set for "
f"table {self.table_name} exception {ex} key {key}"
)
raise ex
def _del(self, key: bytes) -> None:
try:
key = (self.namespace, self.table_name, key)
self.aerospike_fun_call_with_retry(fun=self.client.remove, key=key)
except aerospike.exception.RecordNotFound as ex:
self.log.debug(
f"Error in delete for table {self.table_name} exception {ex} key {key}"
)
except Exception as ex:
self.log.error(
f"FaustAerospikeException Error in delete for "
f"table {self.table_name} exception {ex} key {key}"
)
raise ex
def _iterkeys(self) -> Iterator[bytes]:
try:
fun = self.client.scan
scan: aerospike.Scan = self.aerospike_fun_call_with_retry(
fun=fun, namespace=self.namespace, set=self.table_name
)
for result in scan.results():
yield result[0][2]
except Exception as ex:
self.log.error(
f"FaustAerospikeException Error in _iterkeys "
f"for table {self.table_name} exception {ex}"
)
raise ex
def _itervalues(self) -> Iterator[bytes]:
try:
fun = self.client.scan
scan: aerospike.Scan = self.aerospike_fun_call_with_retry(
fun=fun, namespace=self.namespace, set=self.table_name
)
for result in scan.results():
(key, meta, bins) = result
if bins:
yield bins[self.BIN_KEY]
else:
yield None
except Exception as ex:
self.log.error(
f"FaustAerospikeException Error "
f"in _itervalues for table {self.table_name}"
f" exception {ex}"
)
raise ex
def _iteritems(self) -> Iterator[Tuple[bytes, bytes]]:
try:
fun = self.client.scan
scan: aerospike.Scan = self.aerospike_fun_call_with_retry(
fun=fun, namespace=self.namespace, set=self.table_name
)
for result in scan.results():
(key_data, meta, bins) = result
(ns, set, policy, key) = key_data
if bins:
bins = bins[self.BIN_KEY]
yield key, bins
except Exception as ex:
self.log.error(
f"FaustAerospikeException Error in _iteritems "
f"for table {self.table_name} exception {ex}"
)
raise ex
def _size(self) -> int:
return 0
def _contains(self, key: bytes) -> bool:
try:
if self.app.conf.store_check_exists:
key = (self.namespace, self.table_name, key)
(key, meta) = self.aerospike_fun_call_with_retry(
fun=self.client.exists, key=key
)
if meta:
return True
else:
return False
else:
return True
except Exception as ex:
self.log.error(
f"FaustAerospikeException Error in _contains for table "
f"{self.table_name} exception "
f"{ex} key {key}"
)
raise ex
def _clear(self) -> None:
pass
def reset_state(self) -> None:
pass
def persisted_offset(self, tp: TP) -> Optional[int]:
"""Return the persisted offset.
This always returns :const:`None` when using the aerospike store.
"""
return None
def aerospike_fun_call_with_retry(self, fun, *args, **kwargs):
f_tries = self.app.conf.aerospike_retries_on_exception
f_delay = self.app.conf.aerospike_sleep_seconds_between_retries_on_exception
while f_tries > 1:
try:
return fun(*args, **kwargs)
except aerospike.exception.RecordNotFound as ex:
raise ex
except Exception:
time.sleep(f_delay)
f_tries -= 1
try:
return fun(*args, **kwargs)
except aerospike.exception.RecordNotFound as ex:
raise ex
except Exception as ex:
self.log.error(
f"FaustAerospikeException Error in aerospike "
f"operation for table {self.table_name} "
f"exception {ex} after retries"
)
if self.app.conf.crash_app_on_aerospike_exception:
self.app._crash(
ex
) # crash the app to prevent the offset from progressing
raise ex
async def backup_partition(
self, tp: Union[TP, int], flush: bool = True, purge: bool = False, keep: int = 1
) -> None:
"""Backup partition from this store.
Not yet implemented for Aerospike.
"""
raise NotImplementedError("Not yet implemented for Aerospike.")
def restore_backup(
self, tp: Union[TP, int], latest: bool = True, backup_id: int = 0
) -> None:
"""Restore partition backup from this store.
Not yet implemented for Aerospike.
"""
raise NotImplementedError("Not yet implemented for Aerospike.")