Skip to content

Local

BackendNotReady

BackendNotReady

Source: hetu/local.py:99

Bases: Exception

后端还不能用(如 SQLite 库文件不存在)。不新建库文件。

The backend is not usable yet (e.g. the SQLite database file does not exist).


IdentityRequired

IdentityRequired(system: str) -> None

Source: hetu/local.py:106

Bases: ValueError

USER 权限的 System 必须显式指定玩家身份(caller)。

生产里 USER 端点不放行未登录(caller=0)的连接;默认用 caller=0 跑,会让 upsert(owner=ctx.caller) 这类写法在库里悄悄建出 owner=0 的行。

Attributes

  • system (Any) — No description.

LocalApp

LocalApp(
    namespace: str,
    instance_name: str,
    backends: dict[str, hetu.data.backend.Backend],
    tbl_mgr: hetu.manager.ComponentTableManager,
    *,
    verify_tables: bool,
    address: str = 'local',
) -> None

Source: hetu/local.py:199

进程内的 HeTu 应用运行时:簇已建好、后端已连好,提供与 Sandbox 相同的调用 / 读写 API。

生产 / 开发库请用 open_local_app(config) 打开;直接构造用于已自备 backends / tbl_mgr 的场景(Sandbox 即是)。

In-process app runtime; open it with open_local_app(config).

Attributes

  • namespace (str) — No description.

  • instance_name (str) — No description.

  • backends (dict[str, Backend]) — No description.

  • tbl_mgr (ComponentTableManager) — No description.

  • client (HeadlessClient ) — 表直读写客户端:get/range/insert/upsert 都经它;多表事务用 client.session

  • address (str) — 作为 ctx.address:hetu call / shell 为 “cli”,Sandbox 为 “sandbox”

  • lease (SnowflakeLease | None) — 发号租约(open_local_app(mint_ids=True) 时持有),aclose() 时释放

Methods

verify_table

verify_table(tbl: Table) -> None

Source: hetu/local.py:253

第一次用到这张表时核对表结构与簇(结果缓存),不一致抛 TableNotReady 。

按需而不是开局全量:只为碰到的表读 meta;正在改的无关组件不挡路。本地簇编号整体 漂移时,碰到的表自己的 cluster_id 也对不上,照样拦得住。

verify_components

verify_components(comps: Iterable[__annotationlib_name_1__]) -> None

Source: hetu/local.py:266

校验一组组件的表(System 调用前对 full_components 调用)

resolve_identity

resolve_identity(system: str, caller: int | None, group: str | None) -> tuple[int, str]

Source: hetu/local.py:277

没给全身份时的默认值,见模块函数 resolve_identity(严格规则)。

new_context

new_context(
    caller: int,
    group: str,
    user_data: dict | None = None,
) -> hetu.system.context.SystemContext

Source: hetu/local.py:283

建一个直接跑 System 用的上下文:没有连接(connection_id=0、request=None)。

user_data 传入则作为 ctx.user_data(同一 dict 对调用方可见)。

call_system

call_system(
    system: str,
    *args: Any,
    caller: int | None = None,
    group: str | None = None,
    user_data: dict | None = None,
    uuid: str = '',
    raw: bool = False,
) -> Any

Source: hetu/local.py:304

绕过 Endpoint 层、以 caller / group 身份直接跑一个 System,默认返回 client SDK 实际收到的 payload。

没给身份时按 resolve_identity 推默认值(Sandbox 覆盖为宽松规则:caller=0、 group=“guest”)。

默认(raw=False)按 server receiver.rpc() 的 framing 处理 System 返回值, 并过一遍与生产同款的 msgpack 序列化往返(见 to_client_payload),以暴露在 真实 wire 上才会出现的问题:

  • System 返回 ResponseToClient(msg) → 返回 msgpack 往返后的 msg; 不可序列化的 payload(如 numpy 标量、自定义对象)会在此抛 TypeError, tuple/set 会如实变成 list,与 client 实际收到的一致。
  • System 返回 None 或任意普通值 → 返回字符串 "ok"(普通返回值在 wire 上 被无视,仅用于 System 间嵌套调用)。
  • System 返回 RejectResponse → 原样返回该对象。

raw=True 时跳过上述处理,原样返回 System 的返回值。

内部会开事务、自动在 RaceCondition 时重试。

Run a System as caller/group; by default returns what the client SDK actually receives. Pass raw=True for the System’s untouched return value.

to_client_payload

to_client_payload(rtn: Any) -> Any

Source: hetu/local.py:356

把 System 返回值按 server receiver.rpc() 的 framing + 真实 msgpack 往返, 返回 client SDK 实际收到的 payload。不可序列化的 payload 会在此抛 TypeError。

get

get(comp: Any, **query: Any) -> np.record | None

Source: hetu/local.py:381

按 unique/index 字段读一行;无则返回 None(语义同 repo.get)。

comp 可传 Component 类或其名字字符串;query 只允许一个带索引的字段, 如 get("Player", owner=1234);复合索引(groupby)要同时给出全部分组列,如 get("Rank", level=5, zone=3)。已知行必然存在时,改用 must_get 可免去判空。

must_get

must_get(comp: Any, **query: Any) -> np.record

Source: hetu/local.py:392

同 get,但断言该行存在:命中返回该行,未命中抛 LookupError。

range

range(
    comp: Any,
    index_name: str | None = None,
    _left: Any = None,
    _right: Any = None,
    limit: int = 10,
    desc: bool = False,
    **kwargs: Any,
) -> np.recarray

Source: hetu/local.py:401

按索引区间读多行。默认闭区间 [left, right]。

签名与 repo.range 对齐,两种形态都支持:位置参数 range(comp, "value", 1.0, 2.0),或 kwarg 区间 range(comp, value=(1.0, 2.0))。 复合索引(groupby)要同时给出全部分组列:range(comp, level=(1, 10), zone=3),或位置 参数加 _group={"zone": 3}。comp 可传 Component 类或其名字字符串;索引字段必须带 index/unique。 limit 默认 10(与 repo.range 一致,注意超出会静默截断),负数表示不限制; desc=True 降序。返回 numpy.recarray(c-struct array),无数据时为空数组。

insert

insert(comp: Any, **fields: Any) -> int

Source: hetu/local.py:427

插入一行并返回其 id,省去手写 new_row() + repo.insert(row) 的样板。

只需给关心的字段,其余字段保留组件默认值。传 id= 可指定主键,否则自动生成 雪花 id(返回值即该 id)。_version 由引擎管理,不能设置。

comp 可传 Component 类或其名字字符串;重复 unique 会抛 UniqueViolation 。 直接落库(绕过 System / 不做权限检查)。

upsert

upsert(comp: Any, **anchor: Any) -> AsyncIterator[np.record]

Source: hetu/local.py:454

以 async with 语法 upsert 一行,镜像 repo.upsert:按 unique 字段锚定 查询,块内修改字段,退出块时自动 update/insert 并 commit。

用法 / Usage::

async with app.upsert(RLSComp, owner=1001) as row:
    row.value = 50

anchor 只能给一个 unique 字段(如 owner=.../id=...);组内唯一的复合 索引要同时给出全部分组列(如 name="x", zone=3)。直接落库。

aclose

aclose() -> None

Source: hetu/local.py:476

释放发号租约(写精确水位),再关闭全部后端连接。


TableNotReady

TableNotReady(comp_name: str, status: str) -> None

Source: hetu/local.py:75

Bases: Exception

组件表在库里不存在,或与本地代码的表结构 / 簇不一致。不建表、不迁移,交给服务器和 hetu upgrade。

The component table is missing or its schema / cluster differs from the local code.

Attributes

  • comp_name (Any) — No description.

  • status (Any) — No description.


open_local_app

open_local_app(
    config: dict,
    *,
    instance: str | None = None,
    mint_ids: bool = True,
    address: str = 'local',
) -> hetu.local.LocalApp

Source: hetu/local.py:528

按配置打开一个进程内应用:加载 APP_FILE、建簇、连上全部后端,mint_ids 时租 worker id。

不建表、不迁移:每张表第一次用到时核对表结构与簇,不一致抛 TableNotReady 。 不新建 SQLite 库文件(不存在抛 BackendNotReady )。

Open an in-process app from a config dict. Never creates or migrates tables.

Parameters

  • config (Any) — config.yml 读出来的 dict(APP_FILE 已解析成可用路径),至少要有 APP_FILE / NAMESPACE / INSTANCES / BACKENDS。

  • instance (Any) — 实例名,默认 INSTANCES[0]。

  • mint_ids (Any) — 是否初始化雪花 ID(要跑会 insert 的 System / 写入就要)。会租一个 worker id(工具进程 模式:Redis 从 1023 往下,SQLite 在预留段里),接上 INSTANCES[0] 的时间戳高水位, 起续约 / 预留水位两个后台任务;aclose() 时写精确水位并释放。

  • address (Any) — 作为 ctx.address。