Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion sdk/python/feast/infra/online_stores/bigtable.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,18 @@ def _write_rows_to_bt(
utils.make_tzaware(timestamp).isoformat().encode(),
)
rows.append(bt_row)
bt_table.mutate_rows(rows)
# mutate_rows does not raise when rows fail. It returns one status per row,
# and None for a row that never got a response (google-cloud-bigtable < 2.45).
statuses = bt_table.mutate_rows(rows)
failed = [status for status in statuses if status is None or status.code != 0]
if failed:
# Only counts and the status code: the status message can carry the backend
# address, and the feature server returns exception text to the caller.
first = "no status" if failed[0] is None else f"code={failed[0].code}"
raise RuntimeError(
f"Failed to write {len(failed)} of {len(rows)} rows to Bigtable table "
f"{bt_table.table_id}; first failed status: {first}"
)

if progress:
progress(len(rows))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
from dataclasses import dataclass, field
from datetime import datetime, timezone
from typing import List
from unittest.mock import patch
from unittest.mock import MagicMock, patch

import pytest

pytest.importorskip("google.cloud.bigtable")

from concurrent import futures # noqa: E402

from google.rpc import status_pb2 # noqa: E402

import feast.infra.online_stores.bigtable as bigtable_module # noqa: E402
from feast.infra.offline_stores.dask import DaskOfflineStoreConfig # noqa: E402
from feast.infra.online_stores.bigtable import ( # noqa: E402
Expand Down Expand Up @@ -117,3 +119,69 @@ def test_write_concurrency_sets_thread_pool_size():
store.online_write_batch(config, feature_view, _rows(4), None)

mock_pool.assert_called_once_with(max_workers=3)


OK = status_pb2.Status(code=0)
UNAVAILABLE = status_pb2.Status(code=14, message="connection refused")


def _mock_bt_table(statuses):
bt_table = MagicMock()
bt_table.table_id = "tbl"
bt_table.direct_row.side_effect = lambda row_key: MagicMock(row_key=row_key)
bt_table.mutate_rows.return_value = statuses
return bt_table


def _write_rows(bt_table, n, progress=None):
with patch.object(BigtableOnlineStore, "_compute_row_key", return_value=b"key"):
BigtableOnlineStore()._write_rows_to_bt(
rows_to_write=_rows(n),
bt_table=bt_table,
feature_view_name="fv",
config=_make_repo_config(),
progress=progress,
)


def test_write_rows_to_bt_reports_progress_when_all_rows_succeed():
progress = MagicMock()
_write_rows(_mock_bt_table([OK, OK, OK]), 3, progress)
progress.assert_called_once_with(3)


def test_write_rows_to_bt_raises_on_failed_row_status():
# mutate_rows reports per-row failures in its return value instead of raising.
progress = MagicMock()
with pytest.raises(RuntimeError) as exc_info:
_write_rows(_mock_bt_table([OK, UNAVAILABLE, OK]), 3, progress)
message = str(exc_info.value)
assert "1 of 3 rows" in message
assert "tbl" in message
assert "code=14" in message
# The row key and the status message stay out of the error, which a server may return to its caller.
assert "key" not in message
assert "connection refused" not in message
progress.assert_not_called()


def test_write_rows_to_bt_raises_when_row_has_no_status():
# google-cloud-bigtable < 2.45 leaves None for rows still failing at the retry deadline.
with pytest.raises(RuntimeError, match=r"2 of 2 rows .*no status"):
_write_rows(_mock_bt_table([None, None]), 2)


def test_online_write_batch_raises_on_failed_row_status():
bt_table = _mock_bt_table([UNAVAILABLE, UNAVAILABLE])
client = MagicMock()
client.instance.return_value.table.return_value = bt_table

with (
patch.object(BigtableOnlineStore, "_get_client", return_value=client),
patch.object(BigtableOnlineStore, "_get_table_name", return_value="tbl"),
patch.object(BigtableOnlineStore, "_compute_row_key", return_value=b"key"),
pytest.raises(RuntimeError, match="2 of 2 rows"),
):
BigtableOnlineStore().online_write_batch(
_make_repo_config(), MockFeatureView(name="fv"), _rows(2), None
)
Loading