Repository navigation
Expand file tree
/
Copy pathtest_milvus_online_store.py
More file actions
581 lines (480 loc) · 19.9 KB
/
Copy pathtest_milvus_online_store.py
File metadata and controls
581 lines (480 loc) · 19.9 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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
"""Unit tests for the Milvus online store, run against Milvus Lite or a mocked client."""
from datetime import datetime, timedelta, timezone
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple
from unittest.mock import MagicMock, patch
import pytest
from pydantic import ValidationError
from pymilvus import DataType, MilvusClient
from pymilvus.client.types import LoadState
from feast import Entity, FeatureView
from feast.field import Field
from feast.infra.online_stores.milvus_online_store.milvus import (
PLACEHOLDER_VECTOR_DIM,
PLACEHOLDER_VECTOR_FIELD,
MilvusOnlineStore,
MilvusOnlineStoreConfig,
)
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 RepoConfig
from feast.types import Array, Float32, Int64, String
from feast.value_type import ValueType
MILVUS_MODULE = "feast.infra.online_stores.milvus_online_store.milvus"
def _lite_config(tmp_path: Path, **online_store: Any) -> RepoConfig:
return RepoConfig(
project="test_milvus",
provider="local",
registry=str(tmp_path / "registry.db"),
online_store={
"type": "milvus",
"path": str(tmp_path / "online_store.db"),
"embedding_dim": 2,
**online_store,
},
entity_key_serialization_version=3,
repo_path=tmp_path,
)
def _mock_config(**online_store: Any) -> MagicMock:
config = MagicMock()
config.project = "test_milvus"
config.entity_key_serialization_version = 3
config.registry.enable_online_feature_view_versioning = False
config.provider = "local"
config.repo_path = None
config.online_store = MilvusOnlineStoreConfig(**online_store)
return config
def _mock_client(mock_client_cls: MagicMock, has_collection: bool) -> MagicMock:
mock_client = MagicMock()
mock_client_cls.return_value = mock_client
mock_client.has_collection.return_value = has_collection
mock_client.prepare_index_params.side_effect = MilvusClient.prepare_index_params
return mock_client
def _created_indexes(mock_client: MagicMock) -> Dict[str, Dict[str, Any]]:
"""Map field name -> index definition for indexes passed to the client."""
index_params = []
for call in (
mock_client.create_collection.call_args,
mock_client.create_index.call_args,
):
if call is not None and call.kwargs.get("index_params") is not None:
index_params.extend(call.kwargs["index_params"])
return {index.field_name: index.to_dict() for index in index_params}
def _driver_entity() -> Entity:
return Entity(name="driver_id", join_keys=["driver_id"], value_type=ValueType.INT64)
def _scalar_feature_view(name: str = "driver_stats") -> FeatureView:
return FeatureView(
name=name,
entities=[_driver_entity()],
ttl=timedelta(days=1),
schema=[
Field(name="driver_id", dtype=Int64),
Field(name="trips_today", dtype=Float32),
Field(name="city", dtype=String),
],
)
def _entity_key(driver_id: int) -> EntityKeyProto:
return EntityKeyProto(
join_keys=["driver_id"], entity_values=[ValueProto(int64_val=driver_id)]
)
def _write_rows(
store: MilvusOnlineStore,
config: RepoConfig,
fv: FeatureView,
rows: Dict[int, Dict[str, ValueProto]],
) -> None:
now = datetime.now(timezone.utc)
store.online_write_batch(
config,
fv,
[(_entity_key(k), dict(v), now, now) for k, v in rows.items()],
progress=None,
)
def _read(
store: MilvusOnlineStore,
config: RepoConfig,
fv: FeatureView,
driver_ids: List[int],
features: List[str],
) -> List[Optional[Dict[str, ValueProto]]]:
results = store.online_read(
config, fv, [_entity_key(d) for d in driver_ids], features
)
return [values for _, values in results]
def test_scalar_feature_view_round_trip_with_placeholder(tmp_path: Path) -> None:
config = _lite_config(tmp_path)
fv = _scalar_feature_view()
store = MilvusOnlineStore()
store.update(config, [], [fv], [], [], partial=False)
_write_rows(
store,
config,
fv,
{
1: {
"trips_today": ValueProto(float_val=3.0),
"city": ValueProto(string_val="Paris"),
},
2: {
"trips_today": ValueProto(float_val=5.0),
"city": ValueProto(string_val="Rome"),
},
},
)
rows = _read(store, config, fv, [1, 2, 3], ["trips_today", "city"])
assert rows[0] is not None and rows[0]["city"].string_val == "Paris"
assert rows[1] is not None and rows[1]["trips_today"].float_val == 5.0
assert rows[2] is None
collection = store.client.describe_collection("test_milvus_driver_stats")
placeholder = next(
f for f in collection["fields"] if f["name"] == PLACEHOLDER_VECTOR_FIELD
)
assert placeholder["params"]["dim"] == PLACEHOLDER_VECTOR_DIM
@patch(f"{MILVUS_MODULE}.MilvusClient")
def test_placeholder_vector_is_indexed_and_valid(mock_client_cls: MagicMock) -> None:
mock_client = _mock_client(mock_client_cls, has_collection=False)
store = MilvusOnlineStore()
store._get_or_create_collection(_mock_config(), _scalar_feature_view())
schema = mock_client.create_collection.call_args.kwargs["schema"]
placeholder = next(f for f in schema.fields if f.name == PLACEHOLDER_VECTOR_FIELD)
assert placeholder.dtype == DataType.FLOAT_VECTOR
# Milvus servers reject vectors with fewer than two dimensions.
assert placeholder.params["dim"] >= 2
assert PLACEHOLDER_VECTOR_FIELD in _created_indexes(mock_client)
@patch(f"{MILVUS_MODULE}.MilvusClient")
def test_placeholder_values_are_finite_and_match_collection_dim(
mock_client_cls: MagicMock,
) -> None:
mock_client = _mock_client(mock_client_cls, has_collection=True)
# A collection created by an older Feast version, with a 1-dim placeholder.
mock_client.describe_collection.return_value = {
"collection_name": "test_milvus_driver_stats",
"fields": [
{"name": "driver_id_pk", "type": DataType.VARCHAR, "params": {}},
{"name": "driver_id", "type": DataType.VARCHAR, "params": {}},
{"name": "event_ts", "type": DataType.INT64, "params": {}},
{"name": "created_ts", "type": DataType.INT64, "params": {}},
{"name": "trips_today", "type": DataType.VARCHAR, "params": {}},
{"name": "city", "type": DataType.VARCHAR, "params": {}},
{
"name": PLACEHOLDER_VECTOR_FIELD,
"type": DataType.FLOAT_VECTOR,
"params": {"dim": 1},
},
],
}
store = MilvusOnlineStore()
config = _mock_config()
_write_rows(
store,
config,
_scalar_feature_view(),
{
1: {
"trips_today": ValueProto(float_val=1.0),
"city": ValueProto(string_val="Oslo"),
}
},
)
data = mock_client.upsert.call_args.kwargs["data"]
assert data[0][PLACEHOLDER_VECTOR_FIELD] == [0.0]
def _existing_collection_description() -> Dict[str, Any]:
return {
"collection_name": "test_milvus_driver_stats",
"fields": [
{"name": "driver_id_pk", "type": DataType.VARCHAR, "params": {}},
{"name": "driver_id", "type": DataType.VARCHAR, "params": {}},
{"name": "event_ts", "type": DataType.INT64, "params": {}},
{"name": "created_ts", "type": DataType.INT64, "params": {}},
{"name": "trips_today", "type": DataType.VARCHAR, "params": {}},
{"name": "city", "type": DataType.VARCHAR, "params": {}},
{
"name": PLACEHOLDER_VECTOR_FIELD,
"type": DataType.FLOAT_VECTOR,
"params": {"dim": PLACEHOLDER_VECTOR_DIM},
},
],
}
@pytest.mark.parametrize(
"load_state, expected_loads",
[(LoadState.Loaded, 0), (LoadState.NotLoad, 1)],
)
@patch(f"{MILVUS_MODULE}.MilvusClient")
def test_existing_collection_is_loaded_at_most_once(
mock_client_cls: MagicMock, load_state: LoadState, expected_loads: int
) -> None:
mock_client = _mock_client(mock_client_cls, has_collection=True)
mock_client.describe_collection.return_value = _existing_collection_description()
mock_client.get_load_state.return_value = {"state": load_state}
mock_client.query.return_value = []
store = MilvusOnlineStore()
config = _mock_config()
fv = _scalar_feature_view()
for _ in range(5):
store.online_read(config, fv, [_entity_key(1)], ["city"])
assert mock_client.load_collection.call_count == expected_loads
assert mock_client.query.call_count == 5
@patch(f"{MILVUS_MODULE}.MilvusClient")
def test_new_collection_is_created_with_indexes_so_it_loads(
mock_client_cls: MagicMock,
) -> None:
mock_client = _mock_client(mock_client_cls, has_collection=False)
store = MilvusOnlineStore()
store._get_or_create_collection(_mock_config(), _scalar_feature_view())
# MilvusClient.create_collection loads the collection when it is given
# index params; creating indexes separately would leave it unloaded.
assert mock_client.create_collection.call_args.kwargs["index_params"]
mock_client.create_index.assert_not_called()
mock_client.load_collection.assert_not_called()
def test_load_collection_not_called_per_query(tmp_path: Path) -> None:
config = _lite_config(tmp_path)
fv = _scalar_feature_view()
store = MilvusOnlineStore()
store.update(config, [], [fv], [], [], partial=False)
_write_rows(
store,
config,
fv,
{
1: {
"trips_today": ValueProto(float_val=1.0),
"city": ValueProto(string_val="Oslo"),
}
},
)
assert store.client is not None
with patch.object(
store.client, "load_collection", wraps=store.client.load_collection
) as load_spy:
for _ in range(3):
_read(store, config, fv, [1], ["city"])
store.retrieve_online_documents_v2(
config, fv, ["city"], embedding=None, top_k=1, query_string="Oslo"
)
assert load_spy.call_count == 0
@pytest.mark.parametrize(
"online_store, expected_kwargs",
[
# Existing configs build exactly the same client arguments as before.
({}, {"uri": "http://localhost:19530", "token": ""}),
(
{
"host": "https://milvus.internal",
"port": 443,
"username": "u",
"password": "p",
},
{"uri": "https://milvus.internal:443", "token": "u:p"},
),
# token takes precedence over username/password.
(
{"username": "u", "password": "p", "token": "api-key"},
{"uri": "http://localhost:19530", "token": "api-key"},
),
# uri takes precedence over host/port.
(
{
"uri": "https://in01-abc.zillizcloud.com",
"host": "http://ignored",
"token": "k",
},
{"uri": "https://in01-abc.zillizcloud.com", "token": "k"},
),
(
{
"uri": "https://in01-abc.zillizcloud.com",
"token": "k",
"db_name": "catalog",
},
{
"uri": "https://in01-abc.zillizcloud.com",
"token": "k",
"db_name": "catalog",
},
),
],
)
@patch(f"{MILVUS_MODULE}.MilvusClient")
def test_remote_client_arguments(
mock_client_cls: MagicMock,
online_store: Dict[str, Any],
expected_kwargs: Dict[str, Any],
) -> None:
config = _mock_config(**online_store)
config.provider = "gcp"
MilvusOnlineStore()._connect(config)
mock_client_cls.assert_called_once_with(**expected_kwargs)
@patch(f"{MILVUS_MODULE}.MilvusClient")
def test_uri_takes_precedence_over_lite_path(mock_client_cls: MagicMock) -> None:
config = _mock_config(path="online_store.db", uri="http://milvus:19530", token="k")
MilvusOnlineStore()._connect(config)
mock_client_cls.assert_called_once_with(uri="http://milvus:19530", token="k")
@patch(f"{MILVUS_MODULE}.MilvusClient")
def test_lite_path_used_without_uri(mock_client_cls: MagicMock) -> None:
config = _mock_config(path="/tmp/online_store.db", token="ignored")
MilvusOnlineStore()._connect(config)
mock_client_cls.assert_called_once_with("/tmp/online_store.db")
def _vector_feature_view(name: str = "driver_embeddings") -> FeatureView:
return FeatureView(
name=name,
entities=[_driver_entity()],
ttl=timedelta(days=1),
schema=[
Field(name="driver_id", dtype=Int64),
Field(
name="embedding",
dtype=Array(Float32),
vector_index=True,
vector_search_metric="COSINE",
),
Field(name="city", dtype=String),
],
)
def _vector_rows() -> Dict[int, Dict[str, ValueProto]]:
def embedding(x: float, y: float) -> ValueProto:
value = ValueProto()
value.float_list_val.val.extend([x, y])
return value
return {
1: {"embedding": embedding(1.0, 0.0), "city": ValueProto(string_val="Paris")},
2: {"embedding": embedding(0.0, 1.0), "city": ValueProto(string_val="Rome")},
}
def _search_with_mock(**online_store: Any) -> Dict[str, Any]:
"""Create a collection and search it with a mocked client.
Returns the index definition of the embedding field and the search kwargs.
"""
with patch(f"{MILVUS_MODULE}.MilvusClient") as mock_client_cls:
mock_client = _mock_client(mock_client_cls, has_collection=False)
mock_client.describe_collection.return_value = {
"collection_name": "test_milvus_driver_embeddings",
"fields": [
{"name": "driver_id_pk", "type": DataType.VARCHAR, "params": {}},
{"name": "event_ts", "type": DataType.INT64, "params": {}},
{"name": "created_ts", "type": DataType.INT64, "params": {}},
{
"name": "embedding",
"type": DataType.FLOAT_VECTOR,
"params": {"dim": 2},
},
{"name": "city", "type": DataType.VARCHAR, "params": {}},
],
}
mock_client.search.return_value = [[]]
store = MilvusOnlineStore()
store.retrieve_online_documents_v2(
_mock_config(embedding_dim=2, **online_store),
_vector_feature_view(),
["embedding", "city"],
embedding=[1.0, 0.0],
top_k=1,
)
return {
"index": _created_indexes(mock_client)["embedding"],
"search": mock_client.search.call_args.kwargs,
}
def test_default_index_and_search_params_unchanged() -> None:
result = _search_with_mock(index_type="IVF_FLAT")
assert result["index"]["index_type"] == "IVF_FLAT"
assert result["index"]["nlist"] == 128
assert result["search"]["search_params"]["params"] == {"nprobe": 10}
def test_autoindex_gets_no_index_or_search_params_by_default() -> None:
result = _search_with_mock(index_type="AUTOINDEX")
# Milvus servers reject AUTOINDEX with any build param besides the metric.
assert result["index"] == {
"field_name": "embedding",
"index_type": "AUTOINDEX",
"index_name": "vector_index_embedding",
"metric_type": "COSINE",
}
assert result["search"]["search_params"]["params"] == {}
def test_index_and_search_params_pass_through() -> None:
result = _search_with_mock(
index_type="HNSW",
index_params={"M": 16, "efConstruction": 200},
search_params={"ef": 64},
)
assert result["index"]["M"] == 16
assert result["index"]["efConstruction"] == 200
assert "nlist" not in result["index"]
assert result["search"]["search_params"]["params"] == {"ef": 64}
def test_autoindex_search_with_level(tmp_path: Path) -> None:
config = _lite_config(tmp_path, index_type="AUTOINDEX", search_params={"level": 2})
fv = _vector_feature_view()
store = MilvusOnlineStore()
store.update(config, [], [fv], [], [], partial=False)
_write_rows(store, config, fv, _vector_rows())
results = store.retrieve_online_documents_v2(
config,
fv,
["embedding", "city"],
embedding=[1.0, 0.0],
top_k=1,
distance_metric="COSINE",
)
assert len(results) == 1
assert results[0][2] is not None and results[0][2]["city"].string_val == "Paris"
def _consistency_calls(
**online_store: Any,
) -> Tuple[List[Dict[str, Any]], List[Dict[str, Any]]]:
"""Return the kwargs of create_collection calls and of query/search calls."""
with patch(f"{MILVUS_MODULE}.MilvusClient") as mock_client_cls:
mock_client = _mock_client(mock_client_cls, has_collection=False)
mock_client.describe_collection.return_value = {
"collection_name": "test_milvus_driver_embeddings",
"fields": [
{"name": "driver_id_pk", "type": DataType.VARCHAR, "params": {}},
{"name": "event_ts", "type": DataType.INT64, "params": {}},
{"name": "created_ts", "type": DataType.INT64, "params": {}},
{
"name": "embedding",
"type": DataType.FLOAT_VECTOR,
"params": {"dim": 2},
},
{"name": "city", "type": DataType.VARCHAR, "params": {}},
],
}
mock_client.search.return_value = [[]]
mock_client.query.return_value = []
store = MilvusOnlineStore()
config = _mock_config(embedding_dim=2, **online_store)
fv = _vector_feature_view()
store.online_read(config, fv, [_entity_key(1)], ["city"])
store.retrieve_online_documents_v2(
config, fv, ["embedding", "city"], embedding=[1.0, 0.0], top_k=1
)
store.retrieve_online_documents_v2(
config, fv, ["city"], embedding=None, top_k=1, query_string="Paris"
)
create_calls = [
call.kwargs for call in mock_client.create_collection.call_args_list
]
read_calls = [
call.kwargs
for call in mock_client.query.call_args_list
+ mock_client.search.call_args_list
]
return create_calls, read_calls
def test_consistency_levels_not_sent_when_unset() -> None:
create_calls, read_calls = _consistency_calls()
assert len(create_calls) == 1 and len(read_calls) == 3
assert all("consistency_level" not in kwargs for kwargs in create_calls)
assert all("consistency_level" not in kwargs for kwargs in read_calls)
def test_consistency_level_applied_to_reads_and_searches_only() -> None:
create_calls, read_calls = _consistency_calls(consistency_level="Strong")
assert "consistency_level" not in create_calls[0]
assert len(read_calls) == 3
assert all(kwargs["consistency_level"] == "Strong" for kwargs in read_calls)
def test_collection_consistency_level_applied_on_create_only() -> None:
create_calls, read_calls = _consistency_calls(collection_consistency_level="Strong")
assert create_calls[0]["consistency_level"] == "Strong"
assert all("consistency_level" not in kwargs for kwargs in read_calls)
def test_collection_and_read_consistency_levels_independent() -> None:
create_calls, read_calls = _consistency_calls(
collection_consistency_level="Session", consistency_level="Strong"
)
assert create_calls[0]["consistency_level"] == "Session"
assert all(kwargs["consistency_level"] == "Strong" for kwargs in read_calls)
@pytest.mark.parametrize("field", ["consistency_level", "collection_consistency_level"])
def test_invalid_consistency_level_rejected(field: str) -> None:
with pytest.raises(ValidationError):
MilvusOnlineStoreConfig(**{field: "Immediate"})