Skip to main content

dtmrs-barrier

dtmrs 的子事务屏障 —— 业务服务(RM)侧接入用。

分支接口一定会被重复调用(TC 重试 + 崩溃恢复),而且可能乱序到达。 一张表 + 一条 INSERT IGNORE 同时解决三个问题:

难题 场景
幂等 同一分支被调两次 → 只执行一次
空回滚 正向没跑过就来了补偿 → 补偿空转
悬挂 补偿先到、正向后到 → 丢弃迟到的正向

零运行时依赖,只需要一个 DB-API 2.0 游标(psycopg2 / pymysql / sqlite3 都行)。

from dtmrs_barrier import Barrier, Decision, MYSQL

Barrier.migrate(conn, MYSQL)          # 启动时一次

b = Barrier(MYSQL, trans_type, gid, branch_id, op)
cur = conn.cursor()
try:
    if b.decide(cur) == Decision.EXECUTE:
        # 业务 SQL —— 必须用这个 cur,跟屏障记录同一个事务
        cur.execute("UPDATE account SET balance = balance - %s WHERE id = %s", (amt, uid))
    conn.commit()      # 原子性的来源
except Exception:
    conn.rollback()
    raise

NULL_COMPENSATION 和 DUPLICATED 都是正常路径,接口应返回成功。

⚠ 三条不能违反的

  1. 屏障表必须和业务表在同一个数据库实例 —— 不同实例没法共用本地事务,方案失效
  2. 业务 SQL 必须用传给 decide 的那个 cursor
  3. MySQL 上绝不能用 ON DUPLICATE KEY UPDATE —— 它重复时 rowcount 返回 1 不是 0, 整个算法依赖「冲突时必须是 0」

完整文档见 接入指南。

Apache-2.0

Metadata

Release files for dtmrs-barrier 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dtmrs-barrier 0.3.0
File Size Uploaded
dtmrs_barrier-0.3.0.tar.gz 8.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for dtmrs-barrier 0.3.0
File Interpreter ABI Platform
dtmrs_barrier-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 17.8 kB

Release files / dtmrs_barrier-0.3.0.tar.gz

Download URL dtmrs_barrier-0.3.0.tar.gz
Size 8.1 kB
Tags Source
SHA-256 checksum
How to use checksums
b95f2eb93f326c12f62fdfe544dd1fbd10a417079bfaeff14d14bad4226ab5cc
BLAKE2b-256 checksum
How to use checksums
8753c40974615c529b0dbd84bd429e309c83b51f9ee7eb2dcac91b0736a12b06
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 11, 2026.

Transparency log

Release files / dtmrs_barrier-0.3.0-py3-none-any.whl

Download URL dtmrs_barrier-0.3.0-py3-none-any.whl
Size 9.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2c71f6e1979d0103e1846e47be5c74702175123903020e976aec892c9792cc79
BLAKE2b-256 checksum
How to use checksums
2fe2038cf46a41ce8e0589e035282fec41c2b13541653c8643d2bb9317f017d4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 11, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files

0.2.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page