关于「生产库无损演进:回填节奏、双写核对与写入冻结窗口」,《单机零停机热迁与十秒止血》给出的定量结论是什么?

结论:第五章给出回填批量、分块哈希核对与写入冻结的算法,随机抽 1000 行只有 0.3017 的把握看到 7 个坏行之一。

数据口径:书中只讲通用的数学模型,例子用虚构的 app-blue、app-green 与 db;参数为示意,全部计算由书中程序生成,程序不连接任何服务器;本书是非个人化的经营与工程参考,生产变更须按运维协议人工放行。 书中以标注框区分已发表的证据、可运行的 Python 计算(22 段程序)、原典与作者的假说。

English version

对照数据表

下表数字由本章 Python 程序原样打印,可重新运行复现。 程序只用 Python 标准库与结论吻合的打印值: 0.3017, 1000

chunked hashing360 hashes, 7 bad rows found of 7
rows compared one by one after drilling175
random sampling, chance to see one of the 7 bad rows:
sample100 rows -> 0.0345
sample1000 rows -> 0.3017
sample2995 rows -> 0.6788
sample 10000 rows0.9922
rows to sample for 95 per cent at divergence rate q:
q0.0100 -> 299 rows
q0.0010 -> 2995 rows
q0.0001 -> 29956 rows
查看完整验算程序
# Chapter 5. p52: proving that old and new columns agree after a dual
# write: chunked hashes against random sampling.
# Model: a fictional table of 20000 rows is written to the old and the
# new column; a seeded fault corrupts 7 rows of the new copy. Method 1
# hashes chunks of 500 rows on both sides with SHA-256 (hashlib) and
# drills into differing chunks with sub-chunks of 25 rows. Method 2
# samples n rows at random; the chance that it sees at least one of the
# d bad rows is 1 - C(N - d, n) / C(N, n). The sample size that catches
# a divergence rate q with 95 per cent confidence is ln 0.05 / ln(1 - q).
# Table sizes, fault count and chunk sizes are illustrative.
import hashlib
import math
import random

rng = random.Random(20261113)
N = 20000
BAD = 7
CHUNK = 500
SUB = 25

old = ["row-%05d|v=%d" % (i, (i * 7919) % 10007) for i in range(N)]
new = list(old)
for i in sorted(rng.sample(range(N), BAD)):
    new[i] = new[i] + "|x"


def digest(rows):
    h = hashlib.sha256()
    for r in rows:
        h.update(r.encode("ascii"))
        h.update(b"\n")
    return h.hexdigest()


hashes = 0
found = []
for c in range(0, N, CHUNK):
    hashes += 2
    if digest(old[c:c + CHUNK]) != digest(new[c:c + CHUNK]):
        for s in range(c, c + CHUNK, SUB):
            hashes += 2
            if digest(old[s:s + SUB]) != digest(new[s:s + SUB]):
                found += [i for i in range(s, s + SUB)
                          if old[i] != new[i]]
print("chunked hashing: %d hashes, %d bad rows found of %d" % (
    hashes, len(found), BAD))
print("rows compared one by one after drilling: %d" % (
    SUB * len({i // SUB for i in found})))


def p_see(n, d=BAD, total=N):
    log_miss = math.fsum(math.log((total - d - k) / (total - k))
                         for k in range(n))
    return 1.0 - math.exp(log_miss)


print("random sampling, chance to see one of the %d bad rows:" % BAD)
for n in (100, 1000, 2995, 10000):
    print("  sample %5d rows -> %.4f" % (n, p_see(n)))
print("rows to sample for 95 per cent at divergence rate q:")
for q in (0.01, 0.001, 0.0001):
    print("  q = %.4f -> %d rows" % (
        q, math.ceil(math.log(0.05) / math.log(1.0 - q))))

唯一数据源与引用

范雅福(Fan, Y.). (2026). 《单机多容器零停机热迁与回滚、共享隧道架构生产库无损演进与十秒止血契约母典》第 5 章「生产库无损演进:回填节奏、双写核对与写入冻结窗口」(版本 1.0.0). 5D Library. DOI 待发布

DOI 待发布 · 专著页正式发布后上线(本站暂无专著页) · 5DL-B-269 · v1.0.0 · CC-BY-NC-ND-4.0

BibTeX
@book{fan2026269,
  author    = {Fan, Yafu},
  title     = {Zero-Downtime Hot Migration and Rollback on Multi-Container Single Hosts: Lossless Schema Evolution behind a Shared Tunnel and a Ten-Second Stop-the-Bleeding Contract},
  year      = {2026},
  publisher = {5D Library},
  edition   = {1.0.0},
  note      = {DOI pending}
}

本书其他章节结论