-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathredis.py
More file actions
465 lines (395 loc) · 17 KB
/
redis.py
File metadata and controls
465 lines (395 loc) · 17 KB
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
# Copyright 2021 The Feast Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import logging
from datetime import datetime, timezone
from enum import Enum
from typing import (
Any,
ByteString,
Callable,
Dict,
List,
Literal,
Optional,
Sequence,
Tuple,
Union,
)
from google.protobuf.timestamp_pb2 import Timestamp
from pydantic import StrictStr
from feast import Entity, FeatureView, RepoConfig, utils
from feast.infra.online_stores.helpers import _mmh3, _redis_key, _redis_key_prefix
from feast.infra.online_stores.online_store import OnlineStore
from feast.protos.feast.types.EntityKey_pb2 import EntityKey as EntityKeyProto
from feast.protos.feast.types.Value_pb2 import Value as ValueProto
from feast.repo_config import FeastConfigBaseModel
try:
from redis import Redis
from redis import asyncio as redis_asyncio
from redis.cluster import ClusterNode, RedisCluster
from redis.sentinel import Sentinel
except ImportError as e:
from feast.errors import FeastExtrasDependencyImportError
raise FeastExtrasDependencyImportError("redis", str(e))
logger = logging.getLogger(__name__)
class RedisType(str, Enum):
redis = "redis"
redis_cluster = "redis_cluster"
redis_sentinel = "redis_sentinel"
class RedisOnlineStoreConfig(FeastConfigBaseModel):
"""Online store config for Redis store"""
type: Literal["redis"] = "redis"
"""Online store type selector"""
redis_type: RedisType = RedisType.redis
"""Redis type: redis or redis_cluster"""
sentinel_master: StrictStr = "mymaster"
"""Sentinel's master name"""
connection_string: StrictStr = "localhost:6379"
"""Connection string containing the host, port, and configuration parameters for Redis
format: host:port,parameter1,parameter2 eg. redis:6379,db=0 """
key_ttl_seconds: Optional[int] = None
"""(Optional) redis key bin ttl (in seconds) for expiring entities"""
full_scan_for_deletion: Optional[bool] = True
"""(Optional) whether to scan for deletion of features"""
class RedisOnlineStore(OnlineStore):
"""
Redis implementation of the online store interface.
See https://github.com/feast-dev/feast/blob/master/docs/specs/online_store_format.md#redis-online-store-format
for more details about the data model for this implementation.
Attributes:
_client: Redis connection.
"""
_client: Optional[Union[Redis, RedisCluster]] = None
_client_async: Optional[Union[redis_asyncio.Redis, redis_asyncio.RedisCluster]] = (
None
)
def delete_entity_values(self, config: RepoConfig, join_keys: List[str]):
client = self._get_client(config.online_store)
deleted_count = 0
prefix = _redis_key_prefix(join_keys)
with client.pipeline(transaction=False) as pipe:
for _k in client.scan_iter(
b"".join([prefix, b"*", config.project.encode("utf8")])
):
pipe.delete(_k)
deleted_count += 1
pipe.execute()
logger.debug(f"Deleted {deleted_count} rows for entity {', '.join(join_keys)}")
def delete_table(self, config: RepoConfig, table: FeatureView):
"""
Delete all rows in Redis for a specific feature view
Args:
config: Feast config
table: Feature view to delete
"""
client = self._get_client(config.online_store)
deleted_count = 0
prefix = _redis_key_prefix(table.join_keys)
redis_hash_keys = [_mmh3(f"{table.name}:{f.name}") for f in table.features]
redis_hash_keys.append(bytes(f"_ts:{table.name}", "utf8"))
with client.pipeline(transaction=False) as pipe:
for _k in client.scan_iter(
b"".join([prefix, b"*", config.project.encode("utf8")])
):
_tables = {
_hk[4:] for _hk in client.hgetall(_k) if _hk.startswith(b"_ts:")
}
if bytes(table.name, "utf8") not in _tables:
continue
if len(_tables) == 1:
pipe.delete(_k)
else:
pipe.hdel(_k, *redis_hash_keys)
deleted_count += 1
pipe.execute()
logger.debug(f"Deleted {deleted_count} rows for feature view {table.name}")
def update(
self,
config: RepoConfig,
tables_to_delete: Sequence[FeatureView],
tables_to_keep: Sequence[FeatureView],
entities_to_delete: Sequence[Entity],
entities_to_keep: Sequence[Entity],
partial: bool,
):
"""
Delete data from feature views that are no longer in use.
Args:
config: Feast config
tables_to_delete: Feature views to delete
tables_to_keep: Feature views to keep
entities_to_delete: Entities to delete
entities_to_keep: Entities to keep
partial: Whether to do a partial update
"""
online_store_config = config.online_store
assert isinstance(online_store_config, RedisOnlineStoreConfig)
if online_store_config.full_scan_for_deletion:
for table in tables_to_delete:
self.delete_table(config, table)
def teardown(
self,
config: RepoConfig,
tables: Sequence[FeatureView],
entities: Sequence[Entity],
):
"""
We delete the keys in redis for tables/views being removed.
"""
join_keys_to_delete = set(tuple(table.join_keys) for table in tables)
for join_keys in join_keys_to_delete:
self.delete_entity_values(config, list(join_keys))
@staticmethod
def _parse_connection_string(connection_string: str):
"""
Reads Redis connections string using format
for RedisCluster:
redis1:6379,redis2:6379,skip_full_coverage_check=true,ssl=true,password=...
for Redis:
redis_master:6379,db=0,ssl=true,password=...
"""
startup_nodes = [
dict(zip(["host", "port"], c.split(":")))
for c in connection_string.split(",")
if "=" not in c
]
params = {}
for c in connection_string.split(","):
if "=" in c:
kv = c.split("=", 1)
try:
kv[1] = json.loads(kv[1])
except json.JSONDecodeError:
...
it = iter(kv)
params.update(dict(zip(it, it)))
return startup_nodes, params
def _get_client(self, online_store_config: RedisOnlineStoreConfig):
"""
Creates the Redis client RedisCluster or Redis depending on configuration
"""
if not self._client:
startup_nodes, kwargs = self._parse_connection_string(
online_store_config.connection_string
)
if online_store_config.redis_type == RedisType.redis_cluster:
kwargs["startup_nodes"] = [
ClusterNode(**node) for node in startup_nodes
]
self._client = RedisCluster(**kwargs)
elif online_store_config.redis_type == RedisType.redis_sentinel:
sentinel_hosts = []
for item in startup_nodes:
sentinel_hosts.append((item["host"], int(item["port"])))
sentinel = Sentinel(sentinel_hosts, **kwargs)
master = sentinel.master_for(online_store_config.sentinel_master)
self._client = master
else:
kwargs["host"] = startup_nodes[0]["host"]
kwargs["port"] = startup_nodes[0]["port"]
self._client = Redis(**kwargs)
return self._client
async def _get_client_async(self, online_store_config: RedisOnlineStoreConfig):
if not self._client_async:
startup_nodes, kwargs = self._parse_connection_string(
online_store_config.connection_string
)
if online_store_config.redis_type == RedisType.redis_cluster:
kwargs["startup_nodes"] = [
redis_asyncio.cluster.ClusterNode(**node) for node in startup_nodes
]
self._client_async = redis_asyncio.RedisCluster(**kwargs)
elif online_store_config.redis_type == RedisType.redis_sentinel:
sentinel_hosts = []
for item in startup_nodes:
sentinel_hosts.append((item["host"], int(item["port"])))
sentinel = redis_asyncio.Sentinel(sentinel_hosts, **kwargs)
master = sentinel.master_for(online_store_config.sentinel_master)
self._client_async = master
else:
kwargs["host"] = startup_nodes[0]["host"]
kwargs["port"] = startup_nodes[0]["port"]
self._client_async = redis_asyncio.Redis(**kwargs)
return self._client_async
def online_write_batch(
self,
config: RepoConfig,
table: FeatureView,
data: List[
Tuple[EntityKeyProto, Dict[str, ValueProto], datetime, Optional[datetime]]
],
progress: Optional[Callable[[int], Any]],
) -> None:
online_store_config = config.online_store
assert isinstance(online_store_config, RedisOnlineStoreConfig)
client = self._get_client(online_store_config)
project = config.project
feature_view = table.name
ts_key = f"_ts:{feature_view}"
keys = []
# redis pipelining optimization: send multiple commands to redis server without waiting for every reply
with client.pipeline(transaction=False) as pipe:
# check if a previous record under the key bin exists
# TODO: investigate if check and set is a better approach rather than pulling all entity ts and then setting
# it may be significantly slower but avoids potential (rare) race conditions
for entity_key, _, _, _ in data:
redis_key_bin = _redis_key(
project,
entity_key,
entity_key_serialization_version=config.entity_key_serialization_version,
)
keys.append(redis_key_bin)
pipe.hmget(redis_key_bin, ts_key)
prev_event_timestamps = pipe.execute()
# flattening the list of lists. `hmget` does the lookup assuming a list of keys in the key bin
prev_event_timestamps = [i[0] for i in prev_event_timestamps]
for redis_key_bin, prev_event_time, (_, values, timestamp, _) in zip(
keys, prev_event_timestamps, data
):
# Convert incoming timestamp to millisecond-aware datetime
aware_ts = utils.make_tzaware(timestamp)
# Build protobuf timestamp with nanos
ts = Timestamp()
ts.FromDatetime(aware_ts)
# New timestamp in nanoseconds
new_total_nanos = ts.seconds * 1_000_000_000 + ts.nanos
# Compare against existing timestamp (nanosecond precision)
if prev_event_time:
prev_ts = Timestamp()
prev_ts.ParseFromString(prev_event_time)
prev_total_nanos = prev_ts.seconds * 1_000_000_000 + prev_ts.nanos
# Skip only if older OR exact same instant
if prev_total_nanos and new_total_nanos <= prev_total_nanos:
if progress:
progress(1)
continue
# Store full timestamp (seconds + nanos)
entity_hset = {ts_key: ts.SerializeToString()}
for feature_name, val in values.items():
f_key = _mmh3(f"{feature_view}:{feature_name}")
entity_hset[f_key] = val.SerializeToString()
pipe.hset(redis_key_bin, mapping=entity_hset)
if online_store_config.key_ttl_seconds:
pipe.expire(
name=redis_key_bin, time=online_store_config.key_ttl_seconds
)
results = pipe.execute()
if progress:
progress(len(results))
def _generate_redis_keys_for_entities(
self, config: RepoConfig, entity_keys: List[EntityKeyProto]
) -> List[bytes]:
keys = []
for entity_key in entity_keys:
redis_key_bin = _redis_key(
config.project,
entity_key,
entity_key_serialization_version=config.entity_key_serialization_version,
)
keys.append(redis_key_bin)
return keys
def _generate_hset_keys_for_features(
self,
feature_view: FeatureView,
requested_features: Optional[List[str]] = None,
) -> Tuple[List[str], List[str]]:
if not requested_features:
requested_features = [f.name for f in feature_view.features]
hset_keys = [_mmh3(f"{feature_view.name}:{k}") for k in requested_features]
ts_key = f"_ts:{feature_view.name}"
hset_keys.append(ts_key)
requested_features.append(ts_key)
return requested_features, hset_keys
def _convert_redis_values_to_protobuf(
self,
redis_values: List[List[ByteString]],
feature_view: str,
requested_features: List[str],
):
result: List[Tuple[Optional[datetime], Optional[Dict[str, ValueProto]]]] = []
for values in redis_values:
features = self._get_features_for_entity(
values, feature_view, requested_features
)
result.append(features)
return result
def online_read(
self,
config: RepoConfig,
table: FeatureView,
entity_keys: List[EntityKeyProto],
requested_features: Optional[List[str]] = None,
) -> List[Tuple[Optional[datetime], Optional[Dict[str, ValueProto]]]]:
online_store_config = config.online_store
assert isinstance(online_store_config, RedisOnlineStoreConfig)
client = self._get_client(online_store_config)
feature_view = table
requested_features, hset_keys = self._generate_hset_keys_for_features(
feature_view, requested_features
)
keys = self._generate_redis_keys_for_entities(config, entity_keys)
with client.pipeline(transaction=False) as pipe:
for redis_key_bin in keys:
pipe.hmget(redis_key_bin, hset_keys)
redis_values = pipe.execute()
return self._convert_redis_values_to_protobuf(
redis_values, feature_view.name, requested_features
)
async def online_read_async(
self,
config: RepoConfig,
table: FeatureView,
entity_keys: List[EntityKeyProto],
requested_features: Optional[List[str]] = None,
) -> List[Tuple[Optional[datetime], Optional[Dict[str, ValueProto]]]]:
online_store_config = config.online_store
assert isinstance(online_store_config, RedisOnlineStoreConfig)
client = await self._get_client_async(online_store_config)
feature_view = table
requested_features, hset_keys = self._generate_hset_keys_for_features(
feature_view, requested_features
)
keys = self._generate_redis_keys_for_entities(config, entity_keys)
async with client.pipeline(transaction=False) as pipe:
for redis_key_bin in keys:
pipe.hmget(redis_key_bin, hset_keys)
redis_values = await pipe.execute()
return self._convert_redis_values_to_protobuf(
redis_values, feature_view.name, requested_features
)
def _get_features_for_entity(
self,
values: List[ByteString],
feature_view: str,
requested_features: List[str],
) -> Tuple[Optional[datetime], Optional[Dict[str, ValueProto]]]:
res_val = dict(zip(requested_features, values))
res_ts = Timestamp()
ts_val = res_val.pop(f"_ts:{feature_view}")
if ts_val:
res_ts.ParseFromString(bytes(ts_val))
res = {}
for feature_name, val_bin in res_val.items():
val = ValueProto()
if val_bin:
val.ParseFromString(bytes(val_bin))
res[feature_name] = val
if not res:
return None, None
else:
# reconstruct full timestamp including nanos
total_seconds = res_ts.seconds + res_ts.nanos / 1_000_000_000.0
timestamp = datetime.fromtimestamp(total_seconds, tz=timezone.utc)
return timestamp, res