MagicStack / MagicStack/asyncpg

Extremely long delay grabbing type info for string array (and likely other types) on CockroachDB

未关闭
#1,158 15 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

主要语言
Python
星标
8.1k
派生
468
PR 合并指标
30 天内没有已合并 PR

描述

I was having atrocious and unacceptable delays in my production environment that I wasn't seeing locally, using CockroachDB cloud. Found the cause was the introspection of types. I'm using the latest version of the CockroachDB drivers.

recursion-statements

I wrote this hack to work around the issue. It caches the result in local memory and also caches it to redis so that new instances don't see it. You can change the key for the redis cache using an environment variable so that new versions aren't locked to old values.

If someone wants to turn it into part of the product, please be my guest. I won't have time for it. In the mean time. here's the hack that does monkey patching:

introspection_result_cache: dict[tuple[str, int, str], Any] = {}

orig_introspection_types = asyncpg.Connection._introspect_types

INTROSPECTION_KEY = os.environ.get(
    "ASYNCPG_INTROSPECTION_CACHE_KEY", "ASYNCPG_INTROSPECTION_CACHE_KEY"
)

introspection_lock = asyncio.Lock()


class FauxResult:
    _binary_fields = ("kind", "elemdelim")
    column_order = [
        "oid",
        "ns",
        "name",
        "kind",
        "basetype",
        "elemtype",
        "elemdelim",
        "range_subtype",
        "attrtypoids",
        "attrnames",
        "depth",
        "basetype_name",
        "elemtype_name",
        "range_subtype_name",
    ]

    def __init__(self, row=None, data: dict | None = None) -> None:
        if row:
            self.data = dict(row)
        else:
            assert data
            self.data = data

    def __getattr__(self, name: str) -> Any:
        return self.data[name]

    def __getitem__(self, idx_or_column_name: int | str) -> Any:
        if isinstance(idx_or_column_name, int):
            return self.data[self.column_order[idx_or_column_name]]
        return self.data[idx_or_column_name]

    def for_serialization(self) -> dict:
        result = copy.copy(self.data)
        for field in self._binary_fields:
            if (value := self.data.get(field)) is not None:
                result[field] = value.decode()
        return result

    @classmethod
    def from_serialization(cls, data: dict) -> Self:
        for field in cls._binary_fields:
            if (value := data.get(field)) is not None:
                data[field] = value.encode()
        return cls(data=data)


class FauxPreparedStatementState:
    def __init__(self, name) -> None:
        self.name = name


async def to_redis_cache(
    host: str, port: int, database: str, inspection_types: tuple[list, Any]
) -> None:
    pss = FauxPreparedStatementState(inspection_types[1].name)
    results = [FauxResult(row) for row in inspection_types[0]]
    await redis_client().set(
        INTROSPECTION_KEY + f"-{host}-{port}-{database}",
        orjson.dumps([[result.for_serialization() for result in results], pss.name]),
    )


async def from_redis_cache(host: str, port: int, database: str) -> tuple[list, Any] | None:
    data = await redis_client().get(INTROSPECTION_KEY + f"-{host}-{port}-{database}")
    if data is None:
        return None
    results, pss_name = orjson.loads(data)
    pss = FauxPreparedStatementState(pss_name)
    return [FauxResult.from_serialization(row) for row in results], pss


def apply_introspection_caching():

    async def new_introspect_types(self, *args, **kwargs) -> Any:
        host: str
        port: int
        database: str
        host, port = self._addr
        database = self._params.database
        if (cached_val := introspection_result_cache.get((host, port, database))) is not None:
            return cached_val
        async with introspection_lock:
            redis_cached_value = await from_redis_cache(host, port, database)
        if redis_cached_value is not None:
            introspection_result_cache[host, port, database] = redis_cached_value
            return redis_cached_value
        result = await orig_introspection_types(self, *args, **kwargs)
        await to_redis_cache(host, port, database, result)
        return result

    asyncpg.Connection._introspect_types = new_introspect_types

贡献指南

这个仓库没有索引到贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

该 issue 将 asyncpg.Connection._introspect_types 确定为入口点,并报告 CockroachDB 类型内省存在严重延迟;从这里开始,针对 CockroachDB 复现该行为。提议的缓存 workaround 展示了一种可能的方向,但预期的修复方案和完成标准需要 maintainer 达成一致。

由索引模型根据 Issue 内容生成。

评估

技术栈
postgresql, python
领域
databases
Issue 类型
缺陷
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
需要澄清
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。