跳至内容

Headless

ClusterChanged

ClusterChanged(comp_name: str, old_id: int, new_id: int) -> None

Source: hetu/headless.py:107

Bases: HeadlessError

check_schema() 发现表的 cluster_id 变了(服务器已迁簇),请重新 connect 。

Raised by check_schema() when the table’s cluster id changed on the server.

Attributes

  • comp_name (Any) — No description.

  • old_id (Any) — No description.

  • new_id (Any) — No description.


HeadlessClient

HeadlessClient(
    backend: hetu.data.backend.Backend,
    instance: str,
    tables: collections.abc.Iterable[hetu.data.backend.table.Table],
    *,
    explicit_ids_only: bool = True,
    owns_backend: bool = True,
) -> None

Source: hetu/headless.py:258

无服务器进程的表直读写客户端,用 connect() 创建。

  • table(comp) → 现有 Table :非事务读走 servant_get / servant_range / servant_get_many;提交序号列(commit_seq)的命令表用 servant_seq_read 按号 消费、确认缺口,servant_seq_head / servant_seq_head_many 读发到第几号 (direct_set 不保证通知一致,请勿使用)。
  • session(*comps) → HeadlessSession :s[Comp] 即现有 SessionRepository , 退出 async with 即 commit,提交路径与 System 完全相同。
  • 不发雪花号:insert 的行 id 必须非零,upsert 只在锚定 id=<显式值> 时允许新建。
  • check_schema():重读 meta,簇 / schema 变了就抛异常,由调用方重连或退出。

Table client for trusted non-server processes; create with connect().

Attributes

  • backend (Any) — No description.

  • instance (Any) — No description.

  • explicit_ids_only (Any) — No description.

  • tables (Mapping[str, Table ]) — 按组件名索引的全部已认表 / All resolved tables keyed by component name.

Methods

resolve_tables_

resolve_tables_(
    backend: hetu.data.backend.Backend,
    instance: str,
    components: collections.abc.Sequence[type[hetu.data.component.BaseComponent] | str],
) -> list[hetu.data.backend.table.Table]

Source: hetu/headless.py:298

内部方法:按服务器 meta 把组件解析成 Table (cluster_id 来自 meta,不本地重算)。

传类 → 与 meta 比对数据布局,不一致抛 SchemaMismatch ,通过则用本地类; 传名字 → 用 meta 里的 schema 生成类(不注册进 ComponentDefines)。 表不存在抛 TableNotFound ,不建表。

from_backend

from_backend(
    backend: hetu.data.backend.Backend,
    instance: str,
    components: collections.abc.Sequence[type[hetu.data.component.BaseComponent] | str],
) -> Self

Source: hetu/headless.py:343

用一个已有的 Backend 创建 client(不接管其生命周期,close() 不会关它)。

Create a client on an existing Backend; the backend is not owned.

table

table(
    comp: type[hetu.data.component.BaseComponent] | str,
) -> hetu.data.backend.table.Table

Source: hetu/headless.py:360

按组件类或组件名取 Table (非事务读用)。未声明的组件抛 KeyError。

Get the Table of a declared component by class or name.

session

session(
    *comps: type[hetu.data.component.BaseComponent] | str,
    only_master: bool = True,
) -> hetu.headless.HeadlessSession

Source: hetu/headless.py:382

开一个事务,声明本事务会碰的组件(必须同簇,否则 ValueError)。

only_master: 事务内读取(get / range)是否只走 master,默认 True—— headless 写量小,读 replica 省不了什么,却会把复制延迟变成 RaceCondition 空转。 轮询用的 servant_* 不受此影响。

Open a transaction over the given (same-cluster) components.

check_schema

check_schema() -> None

Source: hetu/headless.py:417

重读所有已认表的 meta,簇或数据布局变了就抛异常(TableNotFound / ClusterChanged / SchemaMismatch ),不自动刷新——由调用方重新 connect 或退出。 建议在租约 / 心跳循环里周期调用。

Re-read table metas; raise if the cluster id or layout changed on the server.

close

close() -> None

Source: hetu/headless.py:435

关闭连接(仅当 backend 由本 client 创建时才关它)/ Close owned backend.


HeadlessError

HeadlessError

Source: hetu/headless.py:68

Bases: Exception

headless client 的异常基类 / Base class of headless client errors.


HeadlessSession

HeadlessSession(
    backend: hetu.data.backend.Backend,
    instance: str,
    cluster_id: int,
    comps: collections.abc.Iterable[type[hetu.data.component.BaseComponent]],
) -> None

Source: hetu/headless.py:220

Bases: Session

headless 事务:Session 的子类,只多一个 s[Comp] 取 SessionRepository 。

组件白名单在 HeadlessClient.session(*comps) 时给定(并已校验同簇),s[Comp] 只 放行声明过的组件,其余一律 KeyError;同一组件返回同一个 repo,跨 retry() 复用。 retry() / commit() / discard() 等全部继承自 Session。

Session subclass adding s[Comp] → SessionRepository , restricted to the components declared in HeadlessClient.session(...).


SchemaMismatch

SchemaMismatch(comp_name: str, diff: list[str]) -> None

Source: hetu/headless.py:89

Bases: HeadlessError

本地组件类的数据布局(namespace、列名、dtype、unique、index)与服务器 meta 不一致。

diff 逐条列出差异,方向为 服务器 -> 本地。

Local component class layout differs from the server-side table meta.

Attributes

  • comp_name (Any) — No description.

  • diff (Any) — No description.


TableNotFound

TableNotFound(instance: str, comp_name: str) -> None

Source: hetu/headless.py:72

Bases: HeadlessError

组件表在后端不存在(没有 meta)。建表 / 迁移权归服务器,headless 不建表。

The component table does not exist on the backend; headless never creates tables.

Attributes

  • instance (Any) — No description.

  • comp_name (Any) — No description.


connect

connect(
    backend_config: dict,
    instance: str,
    components: collections.abc.Sequence[type[hetu.data.component.BaseComponent] | str],
) -> hetu.headless.HeadlessClient

Source: hetu/headless.py:447

连接后端,按服务器写入的表 meta 认下 components,返回 HeadlessClient 。

不 import aiohttp、不需要 app 文件、不建簇、不建表、不初始化 SnowflakeID。 connect / 使用 / close 必须在同一个 event loop 上(Redis 异步连接绑定 loop)。

Parameters

  • backend_config (Any) — config.yml 里 BACKENDS[x] 那个 dict(type / master / servants …), Redis 与 SQLite 都支持。

  • instance (Any) — 服务器实例名(INSTANCES 里的一个),表按实例隔离。

  • components (Any) — 组件类列表(本地定义须与服务器数据布局一致,否则 SchemaMismatch ),或组件名 (schema 取自服务器 meta,本地零定义,用 client.table(name).comp_cls 拿类), 可混用。表不存在抛 TableNotFound 。

  • Connect (Any) — No description.

  • table (Any) — No description.