diff --git a/changelog.d/ITL-1457.md b/changelog.d/ITL-1457.md new file mode 100644 index 000000000..8c34389da --- /dev/null +++ b/changelog.d/ITL-1457.md @@ -0,0 +1,10 @@ +### Changed + +- SpiralDB sources repeat fewer service calls (ITL-1457). The sources of one process that + use one service address and one sign-in share an SDK client, and a dataset's schema table + is read once for all of them while its version has not moved. Its version is still checked + at every lookup that prepares a read, now at the same time as the table's own schema is + fetched, and the checks of neighbouring SpiralDB sources before a run are made at the same + time. Rows, order, hashes, refusals and warnings are unchanged. A changed environment or + saved login, or a client whose call failed, gives a new source a new client; closing a + source affects no other. diff --git a/src/orcapod/core/sources/base.py b/src/orcapod/core/sources/base.py index 07221924c..6a9ad14f6 100644 --- a/src/orcapod/core/sources/base.py +++ b/src/orcapod/core/sources/base.py @@ -564,6 +564,16 @@ def check_for_new_data(self) -> bool: """ return False + def _begin_check(self) -> "Callable[[], Callable[[], bool]] | None": + """Return the part of ``check_for_new_data`` a run may do at the same time as that + of the sources beside it; ``None``, the default, for none. + + It changes nothing, warns of nothing and raises nothing a caller could see: it + returns the rest of the check, which does. The run calls that in its own thread, + in the sources' order, in place of ``check_for_new_data``. + """ + return None + # A source's rows are its own, not computed: a run() view says "all N rows". _RUN_ORIGIN: str = "held" diff --git a/src/orcapod/core/sources/spiraldb_table_source.py b/src/orcapod/core/sources/spiraldb_table_source.py index 8d534e5a3..7707c5d5c 100644 --- a/src/orcapod/core/sources/spiraldb_table_source.py +++ b/src/orcapod/core/sources/spiraldb_table_source.py @@ -8,9 +8,10 @@ Requires the ``spiraldb`` optional extra: ``pip install orcapod[spiraldb]``. Authentication is spiral-sdk's: ``SPIRAL_*`` environment variables, or the credentials ``spiral login`` saved. -The SDK client is reused for reads and freshness checks, including on restricted copies. -Use a new source after changing the SDK endpoint, credentials or options. Use the source -as a context manager or call ``close()`` after its reads and jobs have finished. +The SDK client is used by every source of one service address and sign-in in a process +(ITL-1457), and by its restricted copies. Use a new source after changing the SDK +endpoint, credentials or options: it signs in again. Use the source as a context manager +or call ``close()`` after its reads and jobs have finished; no other source is affected. Example:: @@ -42,8 +43,15 @@ import copy import dataclasses import os -from collections.abc import AsyncIterator, Collection, Generator, Iterator, Mapping -from concurrent.futures import ThreadPoolExecutor +from collections.abc import ( + AsyncIterator, + Callable, + Collection, + Generator, + Iterator, + Mapping, +) +from concurrent.futures import Future, ThreadPoolExecutor from threading import Lock from types import TracebackType from typing import Any, ClassVar, NamedTuple, TYPE_CHECKING @@ -781,11 +789,37 @@ def check_for_new_data(self) -> bool: ValueError: If the table was dropped and created again (another table id) or its columns changed: build the source again to read it. """ + return self._given_asof is None and self._pin_newer(self._lookup_table()) + + @override + def _begin_check(self) -> Callable[[], Callable[[], bool]] | None: + """Return the service calls of the check (ITL-1457), unless a subclass or a + caller replaced ``check_for_new_data``: what that does is not known.""" + if getattr(self.check_for_new_data, "__func__", None) is not _OWN_CHECK: + return None + return self._fetch_for_check + + def _fetch_for_check(self) -> Callable[[], bool]: + """Make the service calls of ``check_for_new_data`` and return the rest of it.""" + looked_up: Future[SpiralDBTable] = Future() if self._given_asof is not None: - return False + return lambda: False + try: + table = self._lookup_table() + table.fetch() + looked_up.set_result(table) + except BaseException as exc: + looked_up.set_exception(exc) + return lambda: self._pin_newer(looked_up.result()) + + def _pin_newer(self, table: SpiralDBTable) -> bool: + """Pin the version of ``table`` when it is newer (``check_for_new_data``). + + Args: + table: This source's table, looked up by this check. + """ # One lookup per check (ITL-1350): a table dropped and created again since # is found by this lookup, and its schema and version come from it. - table = self._lookup_table() schema = arrow_utils.drop_system_columns( table.arrow_schema().empty_table() ).schema @@ -1223,3 +1257,6 @@ def from_config( f"{source.table_id!r}: it is a different table." ) return source + + +_OWN_CHECK = SpiralDBTableSource.check_for_new_data diff --git a/src/orcapod/databases/spiraldb_connector.py b/src/orcapod/databases/spiraldb_connector.py index bd72e28fe..f0945607f 100644 --- a/src/orcapod/databases/spiraldb_connector.py +++ b/src/orcapod/databases/spiraldb_connector.py @@ -16,6 +16,7 @@ from __future__ import annotations +import hashlib import json import logging import math @@ -24,11 +25,13 @@ import urllib.parse import urllib.request import warnings -from collections.abc import Iterator, Mapping +from collections.abc import Callable, Iterator, Mapping +from concurrent.futures import Future from contextlib import contextmanager from copy import copy -from threading import local -from typing import TYPE_CHECKING, Any, cast +from threading import Lock, Thread, local +from typing import TYPE_CHECKING, Any, NamedTuple, TypeVar, cast +from weakref import WeakValueDictionary import pyarrow as pa import pyarrow.compute as pc @@ -41,11 +44,12 @@ ) from orcapod.databases.spiraldb_schema_table import ( SCHEMA_TABLE, + SchemaRow, SchemaTable, compared_columns, describe_difference, ) -from orcapod.databases.spiraldb_sign_in import require_sign_in +from orcapod.databases.spiraldb_sign_in import require_sign_in, saved_login_path from orcapod.errors import ( NullInNonNullableColumnError, SpiralDBWriteNotSupportedError, @@ -135,13 +139,20 @@ class SpiralDBTableNotFoundError(ValueError): """An exact table lookup found no matching table.""" +class _Health: + """Whether a call of one SDK client failed (``_SharedClient``).""" + + failed = False + + @contextmanager -def _service_call(doing: str) -> Iterator[None]: +def _service_call(doing: str, health: _Health | None = None) -> Iterator[None]: """Note on a failing SDK call what Orcapod was doing: SpiralDB's errors often hold a - status only. + status only. A client with a failed call is handed to no new connector. Args: doing: What the call does, and to what. + health: The client's, if it is shared. """ try: yield @@ -149,11 +160,17 @@ def _service_call(doing: str) -> Iterator[None]: # The SDK's own error class is not named here: it may not be installed. if not isinstance(exc, (TableSchemaError, NullInNonNullableColumnError)): exc.add_note(f"SpiralDB: could not {doing}") + if health is not None: + health.failed = True raise def _open_keyed_table( - client: Any, project_id: str, dataset: str, name: str + client: Any, + project_id: str, + dataset: str, + name: str, + health: _Health | None = None, ) -> Any | None: """Return the latest snapshot of a keyed table, or ``None`` if there is none. @@ -161,7 +178,7 @@ def _open_keyed_table( name means "no such table"; a missing project, a refused sign-in or a transport failure has another and is raised. """ - with _service_call(f"open table {project_id}/{dataset}/{name}"): + with _service_call(f"open table {project_id}/{dataset}/{name}", health): try: return client.keyed_table(project_id, dataset, name) except sp.SpiralError as exc: @@ -258,10 +275,11 @@ def _api_url(overrides: Mapping[str, str] | None) -> str | None: return api_url -def _new_client(overrides: Mapping[str, str] | None) -> Any: - """Return a spiral-sdk client. +def _client_options(overrides: Mapping[str, str] | None) -> tuple[str | None, Any]: + """Return the service address and the SDK's client class, a sign-in being present. Raises: + ValueError: As ``_api_url``. SpiralDBSignInError: If spiral-sdk would have to ask a person to sign in. ImportError: If spiral-sdk is not installed, or another package provides the ``spiral`` module (``pyspiral``, which must not be installed beside it). @@ -276,7 +294,112 @@ def _new_client(overrides: Mapping[str, str] | None) -> Any: 'install "orcapod[spiraldb]"' ) from exc require_sign_in() - return client_class(api_url=api_url) + return api_url, client_class + + +class _SharedClient: + """One SDK client, used by every connector of one address and sign-in (ITL-1457). + + Connectors hold it, and the registry of clients does not: it is released when the + last connector that uses it is closed. + + Attributes: + sdk: spiral-sdk's client. + pid: The process that made it. + health: Whether a call of it failed. + """ + + def __init__(self, sdk: Any) -> None: + """Hold a client made in this process.""" + self.sdk = sdk + self.pid = os.getpid() + self.health = _Health() + self._schemas: dict[tuple[str, str], SchemaTable] = {} + self._lock = Lock() + + def schema_table(self, project_id: str, dataset: str) -> SchemaTable: + """Return the schema table of a dataset: one object, whose rows every connector + on this client uses.""" + sdk, health = self.sdk, self.health + with self._lock: + return self._schemas.setdefault( + (project_id, dataset), + SchemaTable( + project_id, + dataset, + lambda: _open_keyed_table( + sdk, project_id, dataset, SCHEMA_TABLE, health + ), + lambda doing: _service_call( + f"{doing} of {project_id}/{dataset}", health + ), + ), + ) + + +_clients: WeakValueDictionary[Any, _SharedClient] = WeakValueDictionary() +_clients_lock = Lock() # held for the registry's own operations only + + +def _forget_clients() -> None: + """In a forked child: leave the parent's clients, and a lock a thread of it held.""" + global _clients, _clients_lock + _clients = WeakValueDictionary() + _clients_lock = Lock() + + +if hasattr(os, "register_at_fork"): + os.register_at_fork(after_in_child=_forget_clients) + + +def _sign_in_key() -> Any | None: + """Return what decides who a new client signs in as, without reading a credential. + + It is a digest of the whole environment (the SDK documents ``SPIRAL_*``, and takes + more: proxies, a home folder, a CI's identity token) and the state of the saved + login's file. ``None`` when it cannot be told: the client is then not shared. + """ + try: + environ = dict(os.environ) + path = saved_login_path(environ) + digest = hashlib.sha256(repr(sorted(environ.items())).encode()).hexdigest() + except (KeyError, RuntimeError, UnicodeError): + return None # changed while it was read, or no home folder + try: + found = path.stat() + state = (found.st_dev, found.st_ino, found.st_size) + state = (*state, found.st_mtime_ns, found.st_ctime_ns) + except OSError: + state = None + return digest, str(path), state + + +def _client_for(overrides: Mapping[str, str] | None) -> _SharedClient: + """Return this process's client of one address and sign-in, made on first use. + + Connectors built at the same time may each make one; they all use the first that was + made. No lock is held while a client signs in. + + Raises: + ValueError: As ``_client_options``, as are ``ImportError`` and + ``SpiralDBSignInError``. + """ + api_url, client_class = _client_options(overrides) + sign_in = _sign_in_key() + key = (os.getpid(), client_class, api_url, sign_in) + with _clients_lock: + shared = None if sign_in is None else _clients.get(key) + if shared is not None and not shared.health.failed: + return shared + made = _SharedClient(client_class(api_url=api_url)) + if sign_in is None or sign_in != _sign_in_key(): + return made # the sign-in changed meanwhile: whose it is is not known + with _clients_lock: + other = _clients.get(key) + if other is not None and not other.health.failed: + return other + _clients[key] = made + return made class _NoRedirect(urllib.request.HTTPRedirectHandler): @@ -345,6 +468,53 @@ def _list_keyed_tables(client: Any, project_id: str, dataset: str) -> dict[str, token = following +_T = TypeVar("_T") + + +def _work(call: Callable[[], _T], future: Future[_T]) -> None: + """Give ``future`` the outcome of ``call``.""" + try: + future.set_result(call()) + except BaseException as exc: + future.set_exception(exc) + finally: + # A kept error holds this frame: without these it would hold itself. + del call, future + + +def _start(call: Callable[[], _T]) -> Future[_T]: + """Make a service call in a thread of its own. + + Only the call: what it finds is judged, and warned about, by the thread that waits for + it, so warning filters and the line a warning names do not depend on this. + + Args: + call: The call. + + Returns: + Its outcome. + """ + future: Future[_T] = Future() + try: + Thread(target=_work, args=(call, future), name="spiraldb-call").start() + except RuntimeError: # no thread can be started (the interpreter is ending) + _work(call, future) + return future + + +class _Fetched(NamedTuple): + """What the service says about how a snapshot is read, fetched but not judged. + + Attributes: + found: The snapshot's schema, as SpiralDB reports it. + rows: The rows of the dataset's schema table as stored now, seen after the + snapshot was opened. + """ + + found: Future[pa.Schema] + rows: Future[Mapping[str, SchemaRow] | None] + + def _literal(value: Any, arrow_type: pa.DataType) -> Any: """Return ``value`` as a scan literal of exactly ``arrow_type`` (the service refuses another type), or ``None`` when the SDK has no literal that filters as Orcapod does. @@ -419,15 +589,29 @@ def __init__( self._table = table self._table_name = table_name self._readings: dict[str, _Reading] = {} + self._fetched: _Fetched | None = None def _reading(self, table: Any) -> _Reading: """Return how a snapshot of this table is read, worked out once per version.""" self._connector._require_open() # pyright: ignore[reportPrivateUsage] # owns the handle version = str(table.version_id) if version not in self._readings: - self._readings[version] = self._connector._reading(table, self._table_name) # pyright: ignore[reportPrivateUsage] # owns the handle + fetched, self._fetched = ( + (self._fetched, None) if table is self._table else (None, self._fetched) + ) + self._readings[version] = self._connector._reading( # pyright: ignore[reportPrivateUsage] # owns the handle + table, self._table_name, fetched + ) return self._readings[version] + def fetch(self) -> None: + """Fetch what ``arrow_schema`` needs and judge nothing: no refusal and no warning. + + The next ``arrow_schema`` judges it, in its own thread. For a check that fetches + several tables at the same time. + """ + self._fetched = self._connector._fetch(self._table, self._table_name) # pyright: ignore[reportPrivateUsage] # owns the handle + @property def table_id(self) -> str: """SpiralDB's own id of the table (no round trip).""" @@ -636,17 +820,13 @@ def __init__( self._dataset = dataset self._overrides = overrides self._closed = False - self._client = _new_client(overrides) - self._pid = os.getpid() - # Shared by every connector on this client (``share_client``). + self._shared = _client_for(overrides) + self._client = self._shared.sdk + self._health = self._shared.health # kept: a read may end after close() + self._pid = self._shared.pid + # Shared by the connectors ``share_client`` gives. self._counts: dict[tuple[str, str], tuple[int, bool]] = {} - client = self._client - self._schemas = SchemaTable( - project_id, - dataset, - lambda: _open_keyed_table(client, project_id, dataset, SCHEMA_TABLE), - lambda doing: _service_call(f"{doing} of {project_id}/{dataset}"), - ) + self._schemas = self._shared.schema_table(project_id, dataset) def _require_open(self) -> None: """Raise RuntimeError if this connector has been closed.""" @@ -665,7 +845,7 @@ def _qualified(self, table_name: str) -> str: def _service_call(self, doing: str, table_name: str) -> Any: """Return a context that notes on a failing SDK call what it was doing.""" - return _service_call(f"{doing} {self._qualified(table_name)}") + return _service_call(f"{doing} {self._qualified(table_name)}", self._health) def _require_user_table(self, table_name: str) -> None: """Refuse the reserved name of the dataset's schema table.""" @@ -727,14 +907,22 @@ def lookup_table(self, table_name: str) -> SpiralDBTable: ): raise SpiralDBTableNotFoundError(missing) table = _open_keyed_table( - self._client, self._project_id, self._dataset, table_name + self._client, + self._project_id, + self._dataset, + table_name, + self._health, ) if table is None: raise SpiralDBTableNotFoundError(missing) return SpiralDBTable(self, table, table_name) def _recorded_schema( - self, table: Any, table_name: str, found: pa.Schema + self, + table: Any, + table_name: str, + found: pa.Schema, + rows: Mapping[str, SchemaRow] | None, ) -> pa.Schema | None: """Return the schema recorded for an opened table; ``None``: as SpiralDB reports. @@ -742,12 +930,12 @@ def _recorded_schema( table: The SDK's handle. table_name: Plain table name. found: The handle's schema, as SpiralDB reports it. + rows: The rows of the dataset's schema table; ``None`` when it has none. Raises: - TableSchemaError: If the dataset's schema table cannot be read or is not - complete, or the table's row disagrees with the table. + TableSchemaError: If the table's row disagrees with the table. """ - row = self._schemas.row(str(table.resource_id)) + row = None if rows is None else rows.get(str(table.resource_id)) if row is None: _warn_unrecorded(self._qualified(table_name), str(table.resource_id)) return None @@ -769,18 +957,49 @@ def _recorded_schema( ) return row.schema - def _reading(self, table: Any, table_name: str) -> _Reading: + def _fetch(self, table: Any, table_name: str) -> _Fetched: + """Fetch how an opened snapshot of ``table_name`` is read, judging nothing. + + Both calls are made after the snapshot was opened, at the same time: no read uses + a schema whose schema table was not seen, complete, after its table was opened. + Both have ended when this returns. + """ + self._require_open() + rows = _start(self._schemas.rows) + found: Future[pa.Schema] = Future() + + def schema() -> pa.Schema: + with self._service_call("read the schema of", table_name): + return table.schema() + + try: + _work(schema, found) + finally: + _ = rows.exception() # waits + return _Fetched(found, rows) + + def _reading( + self, table: Any, table_name: str, fetched: _Fetched | None = None + ) -> _Reading: """Return how an opened snapshot of ``table_name`` is read. + Args: + table: The SDK's handle. + table_name: Plain table name. + fetched: What was fetched for it; fetched now when not given. + Raises: - TableSchemaError: If the table's row cannot be read or disagrees with it. + TableSchemaError: If the dataset's schema table cannot be read or is not + complete, or the table's row disagrees with the table. """ self._require_open() qualified = self._qualified(table_name) - with self._service_call("read the schema of", table_name): - found = table.schema() + if fetched is None: + fetched = self._fetch(table, table_name) + found = fetched.found.result() with _about_table(qualified): - recorded = self._recorded_schema(table, table_name, found) + rows = fetched.rows.result() + recorded = self._recorded_schema(table, table_name, found, rows) return _Reading(found, recorded, qualified) def get_pk_columns(self, table_name: str) -> list[str]: @@ -1052,7 +1271,9 @@ def close(self) -> None: """Close this connector and release its SDK client. Idempotent.""" if not self._closed: self._closed = True - # spiral-sdk's client has no close method. Drop the owning references. + # spiral-sdk's client has no close method. Drop the owning references: the + # client goes with the last connector that uses it. + del self._shared del self._client del self._schemas diff --git a/src/orcapod/databases/spiraldb_schema_table.py b/src/orcapod/databases/spiraldb_schema_table.py index 01e5731d1..9f1359271 100644 --- a/src/orcapod/databases/spiraldb_schema_table.py +++ b/src/orcapod/databases/spiraldb_schema_table.py @@ -434,16 +434,6 @@ def __init__( self._lock = Lock() self._kept: tuple[tuple[str, int], dict[str, SchemaRow]] | None = None - def row(self, table_id: str) -> SchemaRow | None: - """Return the row of ``table_id`` as stored now; ``None`` when the dataset has no - schema table, or a complete one without that table. - - Raises: - TableSchemaError: If the table cannot be read in full or is not complete. - """ - rows = self.rows() - return None if rows is None else rows.get(table_id) - def rows(self) -> Mapping[str, SchemaRow] | None: """Return every row by table id, as stored now; ``None`` when the dataset has no schema table. diff --git a/src/orcapod/pipeline/node_run.py b/src/orcapod/pipeline/node_run.py index b1799c170..7c7830184 100644 --- a/src/orcapod/pipeline/node_run.py +++ b/src/orcapod/pipeline/node_run.py @@ -14,8 +14,10 @@ from __future__ import annotations -from collections.abc import Iterable, Iterator, Mapping, Sequence +from collections.abc import Callable, Iterable, Iterator, Mapping, Sequence +from concurrent.futures import ThreadPoolExecutor from dataclasses import dataclass +from itertools import groupby from typing import TYPE_CHECKING, Any from orcapod.errors import OrcapodDatabaseError, OrcapodDatabaseWriteError @@ -206,6 +208,45 @@ def run_sources(streams: Iterable[Any]) -> list[Any]: return found +# The most sources whose checks fetch at the same time. +_CHECK_THREADS = 8 + + +def _checks(sources: Sequence[Any]) -> Iterator[tuple[Any, Callable[[], bool]]]: + """Yield each source with its check, in order (ITL-1457). + + Sources next to each other that offer the fetching part of their check + (``_begin_check``) do it at the same time; the rest of each check, which takes in + data, warns and raises, is the caller's to call, one after another. No such part is + moved before or after the check of a source that offers none: what that does is the + source's own. + + Args: + sources: The sources, in the order they are checked. + """ + for offers, group in groupby( + ((source, source._begin_check()) for source in sources), + key=lambda item: item[1] is not None, + ): + items = list(group) + if not offers or len(items) == 1: + for source, _ in items: + yield source, source.check_for_new_data + continue + with ThreadPoolExecutor( + min(_CHECK_THREADS, len(items)), thread_name_prefix="source-check" + ) as pool: + try: + fetched = [pool.submit(begin) for _, begin in items if begin] + except RuntimeError: # no thread can be started (the interpreter is ending) + fetched = [] + for index, (source, _) in enumerate(items): + yield ( + source, + fetched[index].result() if fetched else source.check_for_new_data, + ) + + def check_sources(streams: Iterable[Any]) -> tuple[str, ...]: """Ask each source feeding ``streams`` once whether its data changed (ITL-1102). @@ -229,9 +270,9 @@ def check_sources(streams: Iterable[Any]) -> tuple[str, ...]: from orcapod.utils.warning_utils import outside_orcapod_stacklevel, warn_at_frame unchecked: list[str] = [] - for source in run_sources(streams): + for source, check in _checks(run_sources(streams)): try: - source.check_for_new_data() + _ = check() except Exception as exc: name = source_name(source) unchecked.append(name) diff --git a/tests/test_databases/test_spiraldb_repeated_calls.py b/tests/test_databases/test_spiraldb_repeated_calls.py new file mode 100644 index 000000000..eb376c4d6 --- /dev/null +++ b/tests/test_databases/test_spiraldb_repeated_calls.py @@ -0,0 +1,760 @@ +"""What SpiralDB sources share, and what they may never share (ITL-1457), on the fake. + +One client per service address and sign-in, one state of a dataset's schema table per +client, and the checks before a run made at the same time. The results, refusals and +warnings are those of sources that share nothing. +""" + +from __future__ import annotations + +import collections +import gc +import os +import threading +import warnings +import weakref +from collections.abc import Callable, Iterator +from concurrent.futures import ThreadPoolExecutor +from pathlib import Path +from typing import Any + +import pyarrow as pa +import pytest + +from orcapod import PipelineJob +from orcapod.databases import InMemoryArrowDatabase, SpiralDBConnector +from orcapod.databases import spiraldb_connector as connector_module +from orcapod.databases import spiraldb_schema_table as st +from orcapod.errors import ( + SourceCheckFailedWarning, + TableSchemaError, + UnrecordedTableSchemaWarning, +) +from orcapod.operators import SelectColumns +from orcapod.pipeline import node_run +from orcapod.pipeline.async_orchestrator import AsyncPipelineOrchestrator +from orcapod.sources import DictSource, SpiralDBTableSource +from tests.test_databases import fake_spiral_sdk as fake + +_SCHEMA = pa.schema( + [ + pa.field("k", pa.int64(), False), + pa.field("j", pa.large_string(), False), + pa.field("x", pa.float64(), False), + pa.field("y", pa.large_string()), + ] +) +_TABLES = ("a", "b", "c") + + +def _rows(offset: int) -> pa.Table: + return pa.Table.from_pylist( + [ + {"k": i + offset, "j": f"j{i % 2}", "x": 0.5 * i, "y": None} + for i in range(4) + ], + schema=_SCHEMA, + ) + + +def _make(project: str, dataset: str) -> None: + made = fake.ExternalProject(project) + for offset, name in enumerate(_TABLES): + made.create_table( + f"{dataset}.{name}", key_schema=[_SCHEMA.field("k"), _SCHEMA.field("j")] + ).write(_rows(10 * offset)) + fake.migrate(project, dict.fromkeys(_TABLES, _SCHEMA), dataset) + + +@pytest.fixture() +def root(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Iterator[Path]: + """Two projects with two datasets of three tables each, and nothing shared yet.""" + monkeypatch.setattr(connector_module, "_warned_unrecorded", set()) + monkeypatch.setenv("SPIRAL_AUTH_FILE", str(tmp_path / "auth.json")) + with fake.install(tmp_path / "spiral"): + for project in ("p", "q"): + for dataset in ("default", "other"): + _make(project, dataset) + yield tmp_path + + +class _Calls: + """The SDK calls made, by kind, in order.""" + + def __init__(self) -> None: + """Start with none.""" + self.log: list[str] = [] + self.clients: list[weakref.ReferenceType[Any]] = [] + self.lock = threading.Lock() + + def add(self, what: str) -> None: + """Log a call.""" + with self.lock: + self.log.append(what) + + def take(self) -> dict[str, int]: + """Return the counts since the last time, and start again.""" + with self.lock: + counts = dict(collections.Counter(self.log)) + self.log.clear() + return counts + + +@pytest.fixture() +def calls(root: Path, monkeypatch: pytest.MonkeyPatch) -> _Calls: + """Every SDK call, logged.""" + made = _Calls() + init, opened = fake.FakeClient.__init__, fake.FakeClient.keyed_table + by_id = fake.FakeClient.keyed_table_by_id + schema, scan = fake.FakeKeyedTable.schema, fake.FakeKeyedTable.scan + + def is_schemas(table: Any) -> bool: + return table._table["name"] == st.SCHEMA_TABLE + + def new_init(self: Any, **given: Any) -> None: + made.add("client") + made.clients.append(weakref.ref(self)) + init(self, **given) + + def new_opened(self: Any, project: str, dataset: str, name: str, **given: Any): + made.add("open schemas" if name == st.SCHEMA_TABLE else "open") + return opened(self, project, dataset, name, **given) + + def new_by_id(self: Any, *args: Any, **given: Any): + made.add("open by id") + return by_id(self, *args, **given) + + def new_schema(self: Any): + if not is_schemas(self): + made.add("schema") + return schema(self) + + def new_scan(self: Any, *args: Any, **given: Any): + made.add("read schemas" if is_schemas(self) else "scan") + return scan(self, *args, **given) + + monkeypatch.setattr(fake.FakeClient, "__init__", new_init) + monkeypatch.setattr(fake.FakeClient, "keyed_table", new_opened) + monkeypatch.setattr(fake.FakeClient, "keyed_table_by_id", new_by_id) + monkeypatch.setattr(fake.FakeKeyedTable, "schema", new_schema) + monkeypatch.setattr(fake.FakeKeyedTable, "scan", new_scan) + return made + + +def _source( + name: str = "a", project: str = "p", dataset: str = "default", **given: Any +): + return SpiralDBTableSource( + project, name, dataset=dataset, tag_only_columns=True, **given + ) + + +def _ks(source: SpiralDBTableSource) -> list[Any]: + return source.as_table()["k"].to_pylist() + + +# ---------------------------------------------------------------------- the one client + + +def test_one_client_and_one_read_of_a_schema_table_serve_every_source( + calls: _Calls, +) -> None: + sources = [ + _source(n, p, d) for p in "pq" for d in ("default", "other") for n in "abc" + ] + assert calls.take() == { + "client": 1, + "open": 12, + "schema": 12, + "open schemas": 12, # checked at each lookup + "read schemas": 4, # read once per dataset + } + assert [_ks(s) for s in sources] == [ + [10 * i + j for j in range(4)] for _ in range(4) for i in range(3) + ] + + +def test_sharing_changes_no_result(root: Path) -> None: + """Sources that share everything and sources that share nothing are equal.""" + + def facts(source: SpiralDBTableSource) -> list[Any]: + return [ + source.source_id, + source.content_hash().to_string(), + source.pipeline_hash().to_string(), + source.output_schema(), + source.as_table(all_info=True), + [t.as_dict() for t, _ in source.iter_data()], + source.head(2).table, + source.row_count_estimate, + ] + + shared = [_source(n, d) for n, d in (("a", "p"), ("b", "p"), ("a", "q"))] + apart = [] + for name, project in (("a", "p"), ("b", "p"), ("a", "q")): + connector_module._forget_clients() # pyright: ignore[reportPrivateUsage] # so nothing is shared + apart.append(_source(name, project)) + assert [facts(s) for s in shared] == [facts(s) for s in apart] + + +def test_closing_a_source_breaks_no_other_and_the_last_releases_the_client( + calls: _Calls, +) -> None: + first, second = _source("a"), _source("b") + copy = first.where(j="j0") + first.close() + assert _ks(copy) == [0, 2] and _ks(second) == [10, 11, 12, 13] + with pytest.raises(RuntimeError, match="closed"): + first.as_table() + copy.close() + assert _ks(second) == [10, 11, 12, 13] + second.close() + gc.collect() + assert [c() for c in calls.clients] == [None] + assert _ks(_source("a")) == [0, 1, 2, 3] + assert len(calls.clients) == 2 + + +def test_a_scan_in_progress_survives_the_close_of_another_source(root: Path) -> None: + first, second = _source("a", batch_rows=1), _source("b") + rows = first.iter_data() + next(rows) + second.close() + assert len(list(rows)) == 3 + + +def test_a_failure_after_close_is_the_services_error( + root: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """A read may go on after ``close``, and its errors are still noted.""" + source = _source("a") + batches = SpiralDBConnector("p").lookup_table("a") + connector = batches._connector # pyright: ignore[reportPrivateUsage] # closed below + started = threading.Event() + + def fail(*_args: Any, **_given: Any) -> Any: + connector.close() + started.set() + raise fake.SpiralError("status 500") + + monkeypatch.setattr(fake.FakeKeyedTable, "scan", fail) + with pytest.raises(fake.SpiralError, match="500") as raised: + list(batches.scan_batches()) + assert started.is_set() + assert raised.value.__notes__ == ["SpiralDB: could not read p/default/a"] + source.close() + + +@pytest.mark.parametrize( + "change", + [ + {"SPIRAL_CLIENT_ID": "work_other"}, + {"SPIRAL_PRIVATE_KEY": "other"}, + {"SPIRAL_TOKEN": "t"}, + {"SPIRAL_WORKLOAD_ID": "w"}, + {"SPIRAL_AUTH_FILE": "elsewhere"}, + {"HOME": "elsewhere"}, + {"ACTIONS_ID_TOKEN_REQUEST_URL": "u"}, + {"HTTPS_PROXY": "http://localhost:1"}, + ], + ids=lambda c: next(iter(c)), +) +def test_a_changed_environment_is_another_client( + calls: _Calls, monkeypatch: pytest.MonkeyPatch, change: dict[str, str] +) -> None: + first = _source("a") + for name, value in change.items(): + monkeypatch.setenv(name, value) + second = _source("a") + assert calls.take()["client"] == 2 + # The second read the schema table with its own sign-in. + assert _ks(first) == _ks(second) + + +def test_a_changed_saved_login_is_another_client(calls: _Calls, root: Path) -> None: + kept = [_source("a")] + login = root / "auth.json" + for write in ( + lambda: login.write_text("one"), + lambda: login.write_text("two"), # another account, the same size + lambda: login.replace(root / "away"), + ): + write() + kept.append(_source("a")) + kept.append(_source("b")) + # No login, the first, the second; then none again, which is the first client's state. + assert calls.take()["client"] == 3 + assert {tuple(_ks(s)) for s in kept} == { + (0, 1, 2, 3), + (10, 11, 12, 13), + } + + +def test_another_address_is_another_client(calls: _Calls) -> None: + kept = [ + _source("a", overrides=overrides) + for overrides in ( + None, + {"server.url": "https://one.example"}, + {"server.url": "https://two.example"}, + {"server.url": "https://one.example"}, + None, + ) + ] + assert calls.take()["client"] == 3 + assert {tuple(_ks(s)) for s in kept} == {(0, 1, 2, 3)} + + +def test_a_sign_in_that_changes_while_connecting_is_not_shared( + calls: _Calls, root: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + init = fake.FakeClient.__init__ + writes = iter("xy") + + def renewing(self: Any, **given: Any) -> None: + init(self, **given) + if (text := next(writes, None)) is not None: + (root / "auth.json").write_text(text * len(calls.clients)) + + monkeypatch.setattr(fake.FakeClient, "__init__", renewing) + kept = [_source("a") for _ in range(4)] + assert calls.take()["client"] == 3 # two unknown, then one that is shared + assert {tuple(_ks(s)) for s in kept} == {(0, 1, 2, 3)} + + +def test_an_unknown_sign_in_is_not_shared( + calls: _Calls, monkeypatch: pytest.MonkeyPatch +) -> None: + def no_home(_environ: Any) -> Path: + raise RuntimeError("no home folder") + + monkeypatch.setattr(connector_module, "saved_login_path", no_home) + kept = [_source("a"), _source("a")] + assert calls.take()["client"] == 2 + assert {tuple(_ks(s)) for s in kept} == {(0, 1, 2, 3)} + + +@pytest.mark.parametrize("failing", ["open", "schema", "scan"]) +def test_a_client_whose_call_failed_is_given_to_no_new_source( + calls: _Calls, monkeypatch: pytest.MonkeyPatch, failing: str +) -> None: + """An expired or ended sign-in: a new source signs in again, as it always did.""" + source = _source("a") + cls, name = { + "open": (fake.FakeClient, "keyed_table"), + "schema": (fake.FakeKeyedTable, "schema"), + "scan": (fake.FakeKeyedTable, "scan"), + }[failing] + with monkeypatch.context() as broken: + + def denied(*_args: Any, **_given: Any) -> Any: + raise fake.SpiralError("status 401") + + broken.setattr(cls, name, denied) + with pytest.raises(fake.SpiralError, match="401"): + source.as_table() + again = _source("b") + assert calls.take()["client"] == 2 + _ = _source("c") + assert "client" not in calls.take() + # What was built keeps its own client, and recovers with it. + assert _ks(source) == [0, 1, 2, 3] and _ks(again) == [10, 11, 12, 13] + + +def test_a_missing_table_is_no_failure_of_the_client(calls: _Calls) -> None: + kept = _source("a") + with pytest.raises(ValueError, match="not found"): + _source("absent") + _ = _source("b") + assert calls.take()["client"] == 1 + kept.close() + + +def test_a_failed_connection_is_raised_by_each_source( + calls: _Calls, monkeypatch: pytest.MonkeyPatch +) -> None: + def refuse(self: Any, **_given: Any) -> None: + raise fake.SpiralError("sign-in refused") + + with monkeypatch.context() as broken: + broken.setattr(fake.FakeClient, "__init__", refuse) + for _ in range(2): + with pytest.raises(fake.SpiralError, match="refused"): + _source("a") + assert _ks(_source("a")) == [0, 1, 2, 3] + + +def test_many_threads_on_first_use_end_with_one_client(calls: _Calls) -> None: + ready = threading.Barrier(16) + + def build(index: int) -> tuple[SpiralDBTableSource, list[Any]]: + ready.wait(30) + source = _source("abc"[index % 3]) + return source, _ks(source) + + with ThreadPoolExecutor(16) as pool: + built = list(pool.map(build, range(16))) + assert [ks for _, ks in built] == [ + [10 * (i % 3) + j for j in range(4)] for i in range(16) + ] + used = {id(s._connection._client) for s, _ in built if s._connection} # pyright: ignore[reportPrivateUsage] # which client each holds + assert len(used) == 1 + calls.take() + _ = _source("a") + assert "client" not in calls.take() + + +@pytest.mark.skipif(not hasattr(os, "fork"), reason="os.fork is POSIX") +def test_a_forked_child_makes_its_own_client_and_never_waits( + root: Path, +) -> None: + """The registry's lock may be held by a thread that the child does not have.""" + parent = _source("a") + taken, release = threading.Event(), threading.Event() + lock = connector_module._clients_lock # pyright: ignore[reportPrivateUsage] # the inherited lock is the subject + + def hold() -> None: + with lock: + taken.set() + release.wait(30) + + holder = threading.Thread(target=hold) + holder.start() + assert taken.wait(10) + try: + with warnings.catch_warnings(): + warnings.simplefilter("ignore") # Python warns of a fork with threads + pid = os.fork() + if pid == 0: # the child: never returns to pytest + code = 3 + try: + import signal + + signal.alarm(20) + with pytest.raises(RuntimeError, match="after a fork"): + parent.as_table() + child = SpiralDBConnector("p") + same = child._client is parent._connection._client # pyright: ignore[reportPrivateUsage, reportOptionalMemberAccess] # must differ + code = int(same) + finally: + os._exit(code) + _, status = os.waitpid(pid, 0) + assert os.waitstatus_to_exitcode(status) == 0 + finally: + release.set() + holder.join(10) + assert _ks(parent) == [0, 1, 2, 3] + + +# ------------------------------------------------------------ the schema table's state + + +def _damage(project: str = "p", dataset: str = "default", **change: Any) -> None: + table = fake.FakeClient().keyed_table(project, dataset, st.SCHEMA_TABLE) + (marker,) = [ + r for r in table.scan().to_table().to_pylist() if r["table_id"] == st.MARKER_KEY + ] + table.append(pa.Table.from_pylist([{**marker, **change}], schema=st.ROW_SCHEMA)) + + +def test_every_source_refuses_a_schema_table_withdrawn_since(root: Path) -> None: + """At every read that opens a table, as without sharing; another dataset is read.""" + sources = [_source(n) for n in _TABLES] + other = [_source("a", "q"), _source("a", "p", "other")] + asof = _source("a", asof=sources[0]._asof) # pyright: ignore[reportPrivateUsage] # its version + copy = sources[0].where(j="j0") + marker = {"rows_digest": "withdrawn"} + kept = fake.FakeClient().keyed_table("p", "default", st.SCHEMA_TABLE) + kept = kept.scan().to_table() + _damage(**marker) + for source in (*sources, asof, copy): + for read in ( + source.as_table, + lambda source=source: list(source.iter_data()), + source.head, + lambda source=source: source.row_count_estimate, + ): + with pytest.raises(TableSchemaError, match="withdrawn"): + read() + assert [_ks(s) for s in other] == [[0, 1, 2, 3]] * 2 + with pytest.raises(TableSchemaError): + _source("a") + # Restored: every one of them reads again. + fake.FakeClient().keyed_table("p", "default", st.SCHEMA_TABLE).append(kept) + assert [_ks(s) for s in sources] == [ + [0, 1, 2, 3], + [10, 11, 12, 13], + [20, 21, 22, 23], + ] + assert _ks(copy) == [0, 2] + + +def test_a_changed_schema_table_is_read_again_once(calls: _Calls) -> None: + sources = [_source(n) for n in _TABLES] + calls.take() + made = fake.ExternalProject("p") + made.create_table("default.d", key_schema=[_SCHEMA.field("k")]).write( + _rows(0).drop_columns(["j"]) + ) + fake.migrate("p", {}) + calls.take() + for _ in range(2): + for source in sources: + assert len(_ks(source)) == 4 + counts = calls.take() + assert counts["read schemas"] == 1 and counts["open schemas"] == 6 + + +def test_the_schema_table_is_seen_after_the_open_and_before_any_scan( + calls: _Calls, +) -> None: + source = _source("a", max_rows=None) + for read in (source.as_table, source.head, lambda: source.row_count_estimate): + calls.take() + read() + log = calls.log + assert log[0] == "open" + assert set(log[1:3]) == {"schema", "open schemas"} + assert set(log[3:]) <= {"scan"} + + +def test_the_schema_table_and_the_tables_schema_are_fetched_at_the_same_time( + root: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + both = threading.Barrier(2) + opened, schema = fake.FakeClient.keyed_table, fake.FakeKeyedTable.schema + + def new_opened(self: Any, project: str, dataset: str, name: str, **given: Any): + if name == st.SCHEMA_TABLE: + both.wait(10) + return opened(self, project, dataset, name, **given) + + def new_schema(self: Any): + if self._table["name"] != st.SCHEMA_TABLE: + both.wait(10) + return schema(self) + + monkeypatch.setattr(fake.FakeClient, "keyed_table", new_opened) + monkeypatch.setattr(fake.FakeKeyedTable, "schema", new_schema) + assert _ks(_source("a")) == [0, 1, 2, 3] + + +def test_the_tables_own_error_comes_before_the_schema_tables( + root: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + source = _source("a") + _damage(table_count=9) + schema = fake.FakeKeyedTable.schema + + def new_schema(self: Any): + if self._table["name"] != st.SCHEMA_TABLE: + raise fake.SpiralError("status 503") + return schema(self) + + with monkeypatch.context() as broken: + broken.setattr(fake.FakeKeyedTable, "schema", new_schema) + with pytest.raises(fake.SpiralError, match="503") as raised: + source.as_table() + assert raised.value.__notes__ == [ + "SpiralDB: could not read the schema of p/default/a" + ] + with pytest.raises(TableSchemaError, match="p/default/a"): + source.as_table() + + +def test_two_sign_ins_never_share_what_one_may_read( + root: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + allowed = _source("a") + assert _ks(allowed) == [0, 1, 2, 3] + original = fake.FakeClient.keyed_table + trusted = allowed._connection._client # pyright: ignore[reportPrivateUsage, reportOptionalMemberAccess] # the first sign-in's + + def opened(self: Any, project: str, dataset: str, name: str, **given: Any): + if self is not trusted and name == st.SCHEMA_TABLE: + raise fake.SpiralError("status 403") + return original(self, project, dataset, name, **given) + + monkeypatch.setattr(fake.FakeClient, "keyed_table", opened) + monkeypatch.setenv("SPIRAL_CLIENT_ID", "work_second") + with pytest.raises(fake.SpiralError, match="403"): + _source("a") + assert _ks(allowed) == [0, 1, 2, 3] + + +def test_a_warning_is_issued_by_the_reading_thread(root: Path) -> None: + """So its filters and the line it names are the caller's.""" + made = fake.ExternalProject("p") + made.create_table("default.raw", key_schema=[_SCHEMA.field("k")]).write( + _rows(0).drop_columns(["j"]) + ) + with pytest.warns(UnrecordedTableSchemaWarning, match="raw has no row") as seen: + _source("raw") + assert [w.filename for w in seen] == [__file__] + + +def test_reads_work_where_no_thread_can_be_started( + root: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + def no_thread(*_args: Any, **_given: Any) -> None: + raise RuntimeError("can't start new thread") + + monkeypatch.setattr(threading.Thread, "start", no_thread) + sources = [_source(n) for n in _TABLES] + assert _ks(sources[0]) == [0, 1, 2, 3] + assert node_run.check_sources(sources) == () + + +# --------------------------------------------------------------- the checks before a run + + +def _job(sources: list[Any]) -> PipelineJob: + job = PipelineJob(name="checks", database=InMemoryArrowDatabase()) + with job: + for index, source in enumerate(sources): + SelectColumns(["k", "j", "x"])(source, label=f"n{index}") + return job + + +def _write(name: str, k: int) -> None: + fake.ExternalProject("p").table(f"default.{name}").write(_rows(k).slice(0, 1)) + + +def test_neighbouring_checks_are_made_at_the_same_time( + root: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + sources = [_source(n) for n in _TABLES] + job = _job(sources) + job.run() + _write("b", 77) + together = threading.Barrier(3) + opened = fake.FakeClient.keyed_table + + def new_opened(self: Any, project: str, dataset: str, name: str, **given: Any): + if name in _TABLES: + together.wait(10) + return opened(self, project, dataset, name, **given) + + modified = [s.last_modified for s in sources] + with monkeypatch.context() as slow: + slow.setattr(fake.FakeClient, "keyed_table", new_opened) + assert node_run.check_sources(sources) == () + moved = [s.last_modified != m for s, m in zip(sources, modified, strict=True)] + assert moved == [False, True, False] + job.run() + assert 77 in job.nodes["n1"].as_table()["k"].to_pylist() + + +def test_failing_checks_warn_in_the_sources_order(root: Path) -> None: + sources = [_source(n) for n in _TABLES] + made = fake.ExternalProject("p") + for name in ("a", "c"): + made.drop_table(f"default.{name}") + _write("b", 77) + with pytest.warns(SourceCheckFailedWarning) as seen: + unchecked = node_run.check_sources(sources) + assert [w.filename for w in seen] == [__file__] * 2 + assert "default.a" in str(seen[0].message) and "not found" in str(seen[0].message) + assert "default.c" in str(seen[1].message) + assert len(unchecked) == 2 + assert 77 in _ks(sources[1]) + + +def test_a_warning_made_an_error_leaves_the_later_sources_unchecked( + root: Path, +) -> None: + sources = [_source(n) for n in _TABLES] + fake.ExternalProject("p").drop_table("default.b") + _write("a", 77) + _write("c", 78) + with warnings.catch_warnings(): + warnings.simplefilter("error", SourceCheckFailedWarning) + with pytest.raises(SourceCheckFailedWarning): + node_run.check_sources(sources) + assert 77 in _ks(sources[0]) and 78 not in _ks(sources[2]) + + +def test_another_sources_check_keeps_its_place(root: Path) -> None: + """What a check of another kind does is its own: no fetch moves across it.""" + + class Writes(DictSource): + """Writes to a SpiralDB table when it is checked.""" + + def check_for_new_data(self) -> bool: + """Write a row the next source must find.""" + _write("c", 91) + return False + + class Counts(SpiralDBTableSource): + """Replaces the check.""" + + checked = 0 + + def check_for_new_data(self) -> bool: + """Count, then check.""" + type(self).checked += 1 + return super().check_for_new_data() + + writes = Writes([{"k": 1, "x": 1.0}], tag_columns=["k"], tag_only_columns=True) + counts = Counts("p", "b", tag_only_columns=True) + sources = [_source("a"), _source("b"), counts, writes, _source("c"), _source("a")] + assert node_run.check_sources(sources) == () + assert Counts.checked == 1 + last = sources[4] + assert isinstance(last, SpiralDBTableSource) and 91 in _ks(last) + + +def test_a_source_with_a_given_version_makes_no_call(calls: _Calls) -> None: + sources = [_source("a"), _source("b", asof=1), _source("c")] + calls.take() + _write("b", 5) + calls.take() + assert node_run.check_sources(sources) == () + assert calls.take() == {"open": 2, "schema": 2, "open schemas": 2} + + +def test_a_closed_source_is_reported_like_any_failing_check(root: Path) -> None: + sources = [_source("a"), _source("b")] + sources[0].close() + with pytest.warns(SourceCheckFailedWarning, match="is closed"): + assert len(node_run.check_sources(sources)) == 1 + + +# ------------------------------------------------------------------------------ budgets + + +def test_the_calls_each_action_makes(calls: _Calls) -> None: + """A call more is a call repeated.""" + read = {"open": 1, "schema": 1, "open schemas": 1, "scan": 1} + sources = [_source(n) for n in _TABLES] + assert calls.take() == { + "client": 1, + "open": 3, + "schema": 3, + "open schemas": 3, + "read schemas": 1, + } + first = sources[0] + actions: dict[str, tuple[Callable[[], Any], dict[str, int]]] = { + "first preview": (lambda: first.head(2), read | {"scan": 2}), + "preview": (lambda: first.head(2), read), + "run, the cap's count": (first.run, read), + "run": (first.run, read), + "where": (lambda: first.where(j="j0"), {}), + "restricted read": (lambda: first.where(j="j0").as_table(), read), + "count": ( + lambda: first.row_count_estimate, + {"open": 1, "schema": 1, "open schemas": 1}, + ), + } + for name, (action, budget) in actions.items(): + action() + assert calls.take() == budget, name + job = _job(sources) + for runner, budget in ( + (None, {"open": 6, "schema": 6, "open schemas": 6, "scan": 5}), + (None, {"open": 3, "schema": 3, "open schemas": 3}), + (AsyncPipelineOrchestrator(), {"open": 3, "schema": 3, "open schemas": 3}), + ): + job.run(orchestrator=runner) + assert calls.take() == budget