From 050c297e33db280a966008db3e788dcf4d90cd23 Mon Sep 17 00:00:00 2001 From: jiwidi Date: Wed, 28 Oct 2026 11:54:54 +0100 Subject: [PATCH 1/2] fix: Break event timestamp ties by created timestamp in Milvus write batches A batch with two rows for one entity and the same event timestamp kept the first row, whatever its created timestamp. The offline stores break such ties on the created timestamp, and the Redis online store keeps the last row it writes. The Milvus store now keeps the row with the latest event timestamp, then the latest created timestamp, then the last row of the batch, so a correction with a newer created timestamp is not lost. Signed-off-by: jiwidi --- .../milvus_online_store/milvus.py | 10 +++-- .../online_store/test_milvus_remote.py | 33 +++++++++++++++ .../online_store/test_milvus_online_store.py | 40 +++++++++++++++++++ 3 files changed, 79 insertions(+), 4 deletions(-) diff --git a/sdk/python/feast/infra/online_stores/milvus_online_store/milvus.py b/sdk/python/feast/infra/online_stores/milvus_online_store/milvus.py index 722d8a6ec7b..5c0402190d0 100644 --- a/sdk/python/feast/infra/online_stores/milvus_online_store/milvus.py +++ b/sdk/python/feast/infra/online_stores/milvus_online_store/milvus.py @@ -546,10 +546,12 @@ def online_write_batch( single_entity_record[field] = _default_for_milvus_type( field_type ) - # Store only the latest event timestamp per entity - if ( - entity_key_str not in unique_entities - or unique_entities[entity_key_str]["event_ts"] < timestamp_int + # Keep one row per entity: the latest event timestamp wins, then the + # latest created timestamp, then the last row of the batch. + previous = unique_entities.get(entity_key_str) + if previous is None or (previous["event_ts"], previous["created_ts"]) <= ( + timestamp_int, + created_ts_int, ): unique_entities[entity_key_str] = single_entity_record diff --git a/sdk/python/tests/integration/online_store/test_milvus_remote.py b/sdk/python/tests/integration/online_store/test_milvus_remote.py index d49abe76a95..37393bce444 100644 --- a/sdk/python/tests/integration/online_store/test_milvus_remote.py +++ b/sdk/python/tests/integration/online_store/test_milvus_remote.py @@ -166,6 +166,39 @@ def test_scalar_feature_view_round_trip( assert rows[0] is not None and rows[0]["city"].string_val == "Paris" +def test_write_batch_tie_break( + tmp_path: Path, project: str, store: MilvusOnlineStore +) -> None: + """Equal event timestamps: the row with the latest created timestamp wins.""" + config = _repo_config(tmp_path, project, consistency_level="Strong") + fv = _scalar_feature_view() + store.update(config, [], [fv], [], [], partial=False) + base = datetime(2024, 1, 1, tzinfo=timezone.utc) + + store.online_write_batch( + config, + fv, + [ + ( + _entity_key(1), + {"city": ValueProto(string_val="Rome")}, + base, + base + timedelta(seconds=2), + ), + ( + _entity_key(1), + {"city": ValueProto(string_val="Oslo")}, + base, + base + timedelta(seconds=1), + ), + ], + progress=None, + ) + + rows = _read(store, config, fv, [1], ["city"]) + assert rows[0] is not None and rows[0]["city"].string_val == "Rome" + + def _vector_feature_view(name: str = "driver_embeddings") -> FeatureView: return FeatureView( name=name, diff --git a/sdk/python/tests/unit/infra/online_store/test_milvus_online_store.py b/sdk/python/tests/unit/infra/online_store/test_milvus_online_store.py index d5e227c8b68..4e510645ebb 100644 --- a/sdk/python/tests/unit/infra/online_store/test_milvus_online_store.py +++ b/sdk/python/tests/unit/infra/online_store/test_milvus_online_store.py @@ -160,6 +160,46 @@ def test_scalar_feature_view_round_trip_with_placeholder(tmp_path: Path) -> None assert placeholder["params"]["dim"] == PLACEHOLDER_VECTOR_DIM +@pytest.mark.parametrize( + "rows, expected_city", + [ + # Equal event timestamps: the latest created timestamp wins, in any order. + ([("Oslo", 0, 1), ("Rome", 0, 2)], "Rome"), + ([("Rome", 0, 2), ("Oslo", 0, 1)], "Rome"), + # A newer event timestamp wins over a newer created timestamp. + ([("Oslo", 1, 1), ("Rome", 0, 2)], "Oslo"), + # Equal timestamps: the last row of the batch wins. + ([("Oslo", 0, 1), ("Rome", 0, 1)], "Rome"), + ], +) +def test_write_batch_keeps_the_newest_row_per_entity( + tmp_path: Path, rows: List[Tuple[str, int, int]], expected_city: str +) -> None: + store = MilvusOnlineStore() + config = _lite_config(tmp_path) + fv = _scalar_feature_view() + base = datetime(2024, 1, 1, tzinfo=timezone.utc) + + store.online_write_batch( + config, + fv, + [ + ( + _entity_key(1), + {"city": ValueProto(string_val=city)}, + base + timedelta(seconds=event_ts), + base + timedelta(seconds=created_ts), + ) + for city, event_ts, created_ts in rows + ], + progress=None, + ) + + rows_read = _read(store, config, fv, [1], ["city"]) + assert rows_read[0] is not None + assert rows_read[0]["city"].string_val == expected_city + + @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) From 411f2e3c57081f61b2b979f5588cc341064dc047 Mon Sep 17 00:00:00 2001 From: jiwidi Date: Wed, 28 Oct 2026 12:50:26 +0100 Subject: [PATCH 2/2] test: Cover a Milvus write batch with several entities Signed-off-by: jiwidi --- .../online_store/test_milvus_online_store.py | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/sdk/python/tests/unit/infra/online_store/test_milvus_online_store.py b/sdk/python/tests/unit/infra/online_store/test_milvus_online_store.py index 4e510645ebb..9c61f3d0b8e 100644 --- a/sdk/python/tests/unit/infra/online_store/test_milvus_online_store.py +++ b/sdk/python/tests/unit/infra/online_store/test_milvus_online_store.py @@ -200,6 +200,29 @@ def test_write_batch_keeps_the_newest_row_per_entity( assert rows_read[0]["city"].string_val == expected_city +def test_write_batch_keeps_one_row_per_entity(tmp_path: Path) -> None: + store = MilvusOnlineStore() + config = _lite_config(tmp_path) + fv = _scalar_feature_view() + now = datetime(2024, 1, 1, tzinfo=timezone.utc) + + store.online_write_batch( + config, + fv, + [ + (_entity_key(1), {"city": ValueProto(string_val="Oslo")}, now, now), + (_entity_key(2), {"city": ValueProto(string_val="Rome")}, now, now), + ], + progress=None, + ) + + rows = _read(store, config, fv, [1, 2], ["city"]) + assert [row["city"].string_val for row in rows if row is not None] == [ + "Oslo", + "Rome", + ] + + @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)