Skip to content
Closed
7 changes: 7 additions & 0 deletions changelog.d/ITL-1457.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
### Changed

- A SpiralDB source keeps the opened snapshot of its pinned version, so that reading it
again does not fetch its schema and plan again (ITL-1457). Every read and check still opens
the table by name and then checks the dataset's schema table before anything is scanned,
and reads through the kept snapshot only when the name leads to that same id and version.
Rows, order, hashes, refusals and warnings are unchanged.
49 changes: 43 additions & 6 deletions src/orcapod/core/sources/spiraldb_table_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
from orcapod.core.streams.base import check_head_size
from orcapod.core.streams.view import ViewStatus, StreamView
from orcapod.databases.spiraldb_connector import (
OpenedSnapshot,
SpiralDBConnector,
SpiralDBTable,
SpiralDBTableNotFoundError,
Expand Down Expand Up @@ -354,6 +355,7 @@ def __init__(
self._overrides = None if overrides is None else dict(overrides)
self._connection: SpiralDBConnector | None = None
self._connection_lock = Lock()
self._kept: OpenedSnapshot | None = None # of the pinned version, or of none
self._pid = os.getpid()
self._closed = False
self._condition = Condition()
Expand Down Expand Up @@ -386,6 +388,7 @@ def __init__(
# One version for every read of this source object. A given version is
# used as it is; a table that was never written has no version to pin.
self._asof = asof if asof is not None else (table.version() or None)
self._keep(table)

# The default tags are the primary key, whatever identifies the rows.
resolved_tags = (
Expand Down Expand Up @@ -717,6 +720,7 @@ def close(self) -> None:
if self._connection is not None:
self._connection.close()
self._connection = None
self._kept = None

def __enter__(self) -> SpiralDBTableSource:
"""Return this source for use as a context manager."""
Expand All @@ -736,9 +740,33 @@ def _qualified(self) -> str:
"""The table as error messages name it."""
return f"{self._project_id}/{self._dataset}.{self._table_name}"

def _keep(self, table: SpiralDBTable) -> None:
"""Keep the snapshot of ``table`` for reading it again, if it is the pinned one.

Args:
table: This source's table, its schema accepted.
"""
snapshot = table.opened_snapshot()
if (
snapshot is not None
and self._asof is not None
and snapshot.id_and_version == (self._table_id, str(self._asof))
):
# No lock: a read that ends in a forked child must not wait for one.
self._kept = snapshot
if self._closed:
self._kept = None

def _lookup_table(self) -> SpiralDBTable:
"""Resolve the name and check that it still identifies this source's table."""
table = self._connector().lookup_table(self._table_name)
"""Resolve the name and check that it still identifies this source's table.

A kept snapshot is read again if the name still leads to it; every refusal is
judged afresh (ITL-1457).
"""
kept = self._kept
table = self._connector().lookup_table(self._table_name, kept)
if kept is not None and not table.reads(kept):
self._kept = None # versions only move on: it is not read again
table_id = table.table_id
if table_id != self._table_id:
raise ValueError(
Expand All @@ -758,7 +786,10 @@ def row_count_estimate(self) -> int:
cost grows with the table.
"""
asof = self._asof
return self._lookup_table().row_count_estimate(asof=asof)
table = self._lookup_table()
count = table.row_count_estimate(asof=asof)
self._keep(table)
return count

@override
def check_for_new_data(self) -> bool:
Expand Down Expand Up @@ -797,8 +828,10 @@ def check_for_new_data(self) -> bool:
)
version = table.version() or None
if version is None or (self._asof is not None and version <= self._asof):
self._keep(table)
return False
self._asof = version
self._keep(table)
self._update_modified_time()
return True

Expand Down Expand Up @@ -841,9 +874,12 @@ def _batch_streams(self) -> Generator["ArrowTableStream", None, None]:
"""
asof = self._asof
table = self._lookup_table()
yield from self._read_batches(
table, asof=asof, limit=self._limit, max_rows=self._max_rows
)
try:
yield from self._read_batches(
table, asof=asof, limit=self._limit, max_rows=self._max_rows
)
finally:
self._keep(table)

def _read_batches(
self,
Expand Down Expand Up @@ -1021,6 +1057,7 @@ def _head(
columns=columns,
all_info=all_info,
)
self._keep(table)
if rows.num_rows < head_limit or head_limit == limit:
return _Head(rows, True, rows.num_rows, False)
bound, total = self._bounded_count(n, table)
Expand Down
162 changes: 142 additions & 20 deletions src/orcapod/databases/spiraldb_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
import urllib.parse
import urllib.request
import warnings
from collections.abc import Iterator, Mapping
from collections.abc import Generator, Iterator, Mapping
from contextlib import contextmanager
from copy import copy
from threading import local
from typing import TYPE_CHECKING, Any, cast
from typing import TYPE_CHECKING, Any, NamedTuple, cast

import pyarrow as pa
import pyarrow.compute as pc
Expand Down Expand Up @@ -400,6 +400,24 @@ def _equals(array: Any, value: Any) -> Any:
return compute.fill_null(mask, False)


class OpenedSnapshot(NamedTuple):
"""A table's snapshot that was opened and whose schema was fetched, kept for reading it
again (``SpiralDBConnector.lookup_table``, ITL-1457). A snapshot never changes.

Attributes:
handle: The SDK's handle.
found: The snapshot's schema, as SpiralDB reports it.
"""

handle: Any
found: pa.Schema

@property
def id_and_version(self) -> tuple[str, str]:
"""Which snapshot it is."""
return str(self.handle.resource_id), str(self.handle.version_id)


class SpiralDBTable:
"""One SpiralDB table, looked up once (``SpiralDBConnector.lookup_table``).

Expand All @@ -419,13 +437,41 @@ def __init__(
self._table = table
self._table_name = table_name
self._readings: dict[str, _Reading] = {}
# The handle this lookup opened, while ``_kept`` is read in its place.
self._opened_now: Any | None = None
self._kept: OpenedSnapshot | None = None

def _read_kept(self, kept: OpenedSnapshot) -> None:
"""Read the snapshot this lookup opened through a handle of it opened before."""
self._opened_now, self._table, self._kept = self._table, kept.handle, kept

def reads(self, kept: OpenedSnapshot) -> bool:
"""Whether this lookup reads through ``kept``."""
return self._kept is kept

def _leave_kept(self) -> bool:
"""Go back from the kept handle to the one this lookup opened, as if none had been
kept; return whether there was one to leave."""
if self._opened_now is None:
return False
self._table, self._opened_now, self._kept = self._opened_now, None, None
self._readings.clear()
return True

def opened_snapshot(self) -> OpenedSnapshot | None:
"""Return this lookup's snapshot to keep, once its schema was accepted."""
reading = self._readings.get(str(self._table.version_id))
return None if reading is None else OpenedSnapshot(self._table, reading.found)

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
kept = self._kept if table is self._table else None
self._readings[version] = self._connector._reading( # pyright: ignore[reportPrivateUsage] # owns the handle
table, self._table_name, None if kept is None else kept.found
)
return self._readings[version]

@property
Expand Down Expand Up @@ -469,8 +515,41 @@ def scan_batches(
limit: int | None = None,
asof: int | None = None,
) -> Iterator[pa.RecordBatch]:
"""Yield prepared Arrow batches from this resolved table in key order."""
connector = self._connector
"""Yield prepared Arrow batches from this resolved table in key order.

Of a kept handle that fails before it gave a batch nothing was used: the snapshot
is then read through the handle this lookup opened, as if none had been kept.
"""
batches = self._scan(where=where, limit=limit, asof=asof)
if self._opened_now is None or self._at(asof) is not self._table:
yield from batches
return
# Judged outside the attempt: a refusal is a refusal.
_ = self._reading(self._table)
try:
first = next(batches, None)
except (TableSchemaError, NullInNonNullableColumnError):
raise
except Exception as exc:
if not self._leave_kept():
raise
logger.debug("a kept SpiralDB snapshot could not be read again: %s", exc)
batches, first = self._scan(where=where, limit=limit, asof=asof), None
try:
if first is not None:
yield first
yield from batches
finally:
batches.close()

def _scan(
self,
*,
where: Mapping[str, object] | None,
limit: int | None,
asof: int | None,
) -> Generator[pa.RecordBatch, None, None]:
"""Yield the batches of ``scan_batches``."""
table = self._at(asof)
reading = self._reading(table)
predicate = None
Expand All @@ -487,7 +566,7 @@ def scan_batches(
continue
equals = sp.eq(sp.column(column), literal)
predicate = equals if predicate is None else sp.and_(predicate, equals)
with connector._service_call("read", self._table_name): # pyright: ignore[reportPrivateUsage] # owns the handle
with self._connector._service_call("read", self._table_name): # pyright: ignore[reportPrivateUsage] # owns the handle
reader = table.scan(
filter=predicate,
# The limit counts rows that match every condition.
Expand All @@ -497,7 +576,7 @@ def scan_batches(
left = limit
try:
while left is None or left > 0:
with connector._service_call("read", self._table_name): # pyright: ignore[reportPrivateUsage] # owns the handle
with self._connector._service_call("read", self._table_name): # pyright: ignore[reportPrivateUsage] # owns the handle
batch = next(reader, None)
if batch is None:
return
Expand Down Expand Up @@ -534,18 +613,32 @@ def count_rows(self, *, asof: int | None = None, at_most: int | None = None) ->
known = (self.table_id, str(table.version_id))
counted, exact = counts.get(known, (0, False))
if not exact and (at_most is None or counted < at_most):
with connector._service_call("count the rows of", self._table_name): # pyright: ignore[reportPrivateUsage] # owns the handle
reader = table.scan(
[self.key_columns[0]], limit=at_most, batch_rows=_SCAN_BATCH_ROWS
).to_reader()
try:
counted = sum(batch.num_rows for batch in reader)
finally:
reader.close()
try:
counted = self._count_keys(table, at_most)
except Exception as exc:
if table is not self._table or not self._leave_kept():
raise
logger.debug("a kept SpiralDB snapshot could not be counted: %s", exc)
counted = None
if counted is None:
table = self._table
_ = self._reading(table)
counted = self._count_keys(table, at_most)
exact = at_most is None or counted < at_most
counts[known] = (counted, exact)
return counted if at_most is None else min(counted, at_most)

def _count_keys(self, table: Any, at_most: int | None) -> int:
"""Count the keys of a snapshot, at most ``at_most`` of them."""
with self._connector._service_call("count the rows of", self._table_name): # pyright: ignore[reportPrivateUsage] # owns the handle
reader = table.scan(
[self.key_columns[0]], limit=at_most, batch_rows=_SCAN_BATCH_ROWS
).to_reader()
try:
return sum(batch.num_rows for batch in reader)
finally:
reader.close()

def row_count_estimate(self, asof: int | None = None) -> int:
"""Return the table's row count, exact, by reading every key (``count_rows``)."""
return self.count_rows(asof=asof)
Expand Down Expand Up @@ -703,11 +796,20 @@ def get_table_names(self) -> list[str]:
)
return sorted(name for name in listed.values() if name != SCHEMA_TABLE)

def lookup_table(self, table_name: str) -> SpiralDBTable:
def lookup_table(
self, table_name: str, kept: OpenedSnapshot | None = None
) -> SpiralDBTable:
"""Look the table up once, for several items of its metadata (ITL-1350).

The table is opened by name, and its dataset's schema table is checked before
anything is read from it, also when a snapshot was kept. If the name leads to that
very snapshot (one id and one version), the returned table reads through its
handle, whose schema and plan need not be fetched again; if not, ``kept`` plays no
part.

Args:
table_name: Plain table name (no dataset prefix).
kept: A snapshot opened before, by a connector on this client.

Returns:
The table: its id and key columns, and its schema and version when asked.
Expand All @@ -731,7 +833,13 @@ def lookup_table(self, table_name: str) -> SpiralDBTable:
)
if table is None:
raise SpiralDBTableNotFoundError(missing)
return SpiralDBTable(self, table, table_name)
found = SpiralDBTable(self, table, table_name)
if kept is not None and kept.id_and_version == (
found.table_id,
str(table.version_id),
):
found._read_kept(kept) # pyright: ignore[reportPrivateUsage] # made here
return found

def _recorded_schema(
self, table: Any, table_name: str, found: pa.Schema
Expand Down Expand Up @@ -769,16 +877,30 @@ def _recorded_schema(
)
return row.schema

def _reading(self, table: Any, table_name: str) -> _Reading:
def _reading(
self, table: Any, table_name: str, known: pa.Schema | None = None
) -> _Reading:
"""Return how an opened snapshot of ``table_name`` is read.

The dataset's schema table is looked at last, after the table was opened and its
schema is known, and before anything is read: every time, also for a kept snapshot.

Args:
table: The SDK's handle.
table_name: Plain table name.
known: The snapshot's schema, if it was fetched before; it never changes.

Raises:
TableSchemaError: If the table's row cannot be read or disagrees with it.
"""
self._require_open()
qualified = self._qualified(table_name)
with self._service_call("read the schema of", table_name):
found = table.schema()
found: pa.Schema
if known is not None:
found = known
else:
with self._service_call("read the schema of", table_name):
found = table.schema()
with _about_table(qualified):
recorded = self._recorded_schema(table, table_name, found)
return _Reading(found, recorded, qualified)
Expand Down
Loading
Loading