第 1 次修正/擴充 · 2026-11-29 · Host seq 68218/68219/68220

中央接收機:16 個 FTNN 主程序 × 100 SIDs
+8 部來源機無間斷循環 CSV

範圍只講「收數、落盤、對齊、重播、CSV 接收」;唔計策略/指標/交易算法運算。前版係 4-node forwarding budget,已於 receipt 68215 交付;今版按新固定拓撲補齊中央 Server sizing、burst/backpressure/WAL、CSV heavy 程度及 16 線新舊校對。前版原文一字不刪,放喺最底。

前版:3,648 B · SHA-256 d4d47a65b4ec3b42ad94c354905cf9a9eeeedd73a3d56b71df0dc70add57b5f2。

0 · 一句定案

16 條 connection 本身唔重;真正樽頸係每秒 events、每 event bytes、CSV 重複量、fsync 次數同 disk retention。
Oracle 4-core/24GB 可以做第一階段「只收+batch WAL+ACK+壓縮 CSV blob spool」,但未有實測 event-rate 前,唔可以保證同一部機再同步解析 16 份 continuous CSV、做 dedup/projector/compaction。若單機一定要連 CSV parse/project 一齊做,建議規劃 8–12 vCPU/32–64GB/1–2TB local NVMe/10GbE;實際定案由 1×/5×/10× replay 壓測決定。
固定 4-core/24GB 現機可先用:canonical ingest + durable spool;CSV 只驗 manifest/hash 後存 immutable blob,非同步慢慢 parse。
唔應該喺現機同步做HTTP request handler 逐行 parse 50-column CSV、逐 event fsync、16 writers 直寫同一 CSV、同步 projection/compaction。
升級優先次序① local NVMe / retention ② CPU 8–12 cores ③ 10GbE(若量到 burst 需要)④ RAM;24GB 通常唔係第一樽頸。

1 · 先識計:1600 隻唔等於一個固定流量

同樣 1,600 SIDs,如果市場靜止,可以好低;如果全 tick+full OrderBook 喺開市 burst,每秒事件可差幾十倍。容量必須用以下公式,而唔係只睇 symbol 數:

aggregate_events_per_sec = Σ(每個 flow / 每個 SID 實測 events/s)
wire_MB_per_sec = events_per_sec × average_wire_bytes × protocol_overhead / 1,000,000
disk_GB_per_day = average_disk_MB_per_sec × 86,400 / 1,000
required_local_spool = disk_GB_per_day × offline_days × 1.30 headroom

注意:protocol_overhead 建議先用 1.2–1.5;disk 仲要計 WAL + index + CSV archive,
唔可以只計 network body。
純示例,唔係實測預測Aggregate events/s若 500 B/eventRaw/day
每 SID 平均 5 events/s8,0004 MB/s約 346 GB/day
每 SID 平均 20 events/s32,00016 MB/s約 1.38 TB/day
每 SID 平均 100 events/s160,00080 MB/s約 6.91 TB/day

OrderBook event 往往大過 500 B;JSON debug envelope 亦大過 binary。反過來,唔係每隻股票每秒都有咁多更新。所以要由來源機先量 p50/p95/p99 events/s、bytes/event、5秒 burst,先可買死規格。

2 · Oracle 4-core/24GB 點配先最穩

若暫時唔升級:將佢限定做「Receiver / Spooler」

項目4-core/24GB 建議原因
CPU1 async front door;1 batched WAL/ACK worker;最多 1–2 decode/CSV workers。用 bounded worker pool,唔好每 stream 開無限 worker。預留 scheduler/OS/TLS 空間;request event loop 絕不做 blocking fsync。
RAM每 stream queue 64–128MB hard cap;16 條合共約 1–2GB;process RSS 設上限;其餘留俾 page cache及 DB/index。24GB 夠 bounded buffer,但 unbounded queue 幾多 RAM 都會爆。
Disklocal NVMe,ext4/XFS;最少 1TB 只作短 spool,較穩 2TB;長期 retention 搬 object storage。WAL 與 CSV spool 分目錄/quota。boot volume/NFS latency 會令 fsync tail latency飆升;1TB 喺 10MB/s 都只約 1 日多。
Network1GbE 可作 pilot,但要驗 p99 <50–70MB/s;未知 full-OB burst 或單機兼備份,建議 10GbE。1GbE 理論 125MB/s,實務唔應長期貼頂;TLS、重傳、CSV同時上傳要留位。
FD / kernelnofile ≥ 65536;TCP keepalive;合理 receive buffers;每 stream 唯一身份;唔靠改巨型 socket buffer掩飾 backpressure。16 connections唔多,但 WAL segments、temp uploads、index files 會食 FD。
服務隔離event ingest、CSV ingest、projector 三個 process/cgroup;server.js event loop只驗 header+enqueue。CSV parser卡住唔應拖死 canonical event ACK。

如果一部機要「收+parse CSV+dedup+project」

建議起步 8–12 vCPU、32–64GB RAM、1–2TB local NVMe、10GbE。如果實測 medium profile已超過 10–20MB/s 持續或 5× burst,最好將 CSV workers/object storage拆走,而唔係一味加 RAM。RAM 只可吸短 burst,唔可以解決長期輸入大過disk drain。

3 · 正確 ingest:先 durable,後解析

每個 FTNN 主程序(共16)
  callback → bounded queue → local append-only WAL → batch encoder/sender
             └→ CSV projector(獨立;唔block callback)

中央 Server
  TLS front door
    ├─ /ingest/v1/events      → validate envelope/hash → raw WAL/spool → fsync → ACK offset
    └─ /ingest/v1/csv-batches → validate manifest/hash → immutable blob spool → ACK batch_seq
                                      ↓(兩條都係 ACK 後)
                          async decode / dedup / reorder / projector
                                      ↓
                          canonical store → API / V2 JSON / V1 CSV

ACK 定義:只可以 ACK「中央 disk 已 durable」嘅最高連續 offset/batch sequence,唔可以一收到 socket bytes就 ACK。Sender 斷線後由最後 durable ACK + 1 重送。

Batch 建議

4 · Burst、backpressure、WAL、去重、保序逐樣做

問題錯做法正確接收做法
Burst靠大 RAM queue頂住,無上限。per-stream hard cap+global cap;高/低 watermark;5×及10× replay soak;超過 drain rate即落 source WAL。
Backpressurecallback block住等 Server/CSV。callback只enqueue;sender收到 429/503 + Retry-After就退避;本地 WAL保留;中央唔 silent drop。
WAL每 event open/write/fsync。append segment、CRC、batch fsync、segment hash chain;Server ACK最高 contiguous durable offset。
Dedup只以 payload hash 去重。event_id = source_id + process_id + boot_id + source_offset;真市場可合法連續出相同 payload,唔應誤刪。
Order將16條線按 Server arrival 排成「全局真順序」。只保證每 stream offset order;跨 stream用 event-time watermark + bounded lateness;global arrival只係觀測時間。
Gapoffset跳咗照project。first_offset > durable+1 就記 gap、hold affected projection、要求 resend missing range。
Disk16個 producer直append同一 CSV。按 source/process/boot/date/hour partition;單 writer;後台先merge。
CSV + canonical並行兩份都當新行情 ingest,造成雙計。canonical係唯一 source of truth;CSV係projection/audit。若 CSV 係兼容輸入,就要明確標 lane,轉canonical後以同一event_id去重。

5 · Continuous CSV 會唔會 heavy?

會,尤其係「不斷重傳整份 CSV」時。重點唔止 network bytes;CSV 仲有 UTF-8 decode、quote parsing、50欄重複、hash、fsync、index、再寫 canonical/projection嘅 write amplification。

用已驗證 GUI 50-column CSV 做下限示例

舊實測:8,144,579 B/18,266 rows ≈ 446 B/row。若每個主程序每輪只出 100-row full snapshot:

16 programs 一輪輪速只計 raw CSV/day
約 713KB每 60 秒一輪約 1.03GB/day
約 713KB每 10 秒一輪約 6.16GB/day
約 713KB每 1 秒一輪約 61.6GB/day

呢個只係「每輪100 rows」下限。現有 Server V1 sidecar係每次來源 event 都寫一行完整50欄 snapshot;活躍市場每秒可以多行,實際可高好多。如果每次重傳一個不斷長大嘅 CSV,network 會出現近似二次重複量,必須禁止。

而家 CSV 套路要改

  1. 唔傳整個 growing file:傳 immutable delta batches,或者按已ACK byte offset只傳新 bytes。
  2. 唔喺 central hot path逐行變換:先 spool compressed closed batch,ACK;worker之後 parse。
  3. 唔每 event 複寫50欄:canonical reducer合併更新;V1 CSV按固定 cadence(例如每分鐘/每批)或 on-demand export。若業務硬性要求每event,另開低優先 lane及quota。
  4. CSV同time sidecar綁埋:同一manifest列兩個hash;缺一就唔套用。GUI export filename時間只係「輸出時間」,唔係行情時間。
  5. 每個 source/process獨立partition:中央唔准16線同寫一個共享CSV。

6 · 16 條非同步線點校新舊:唔好只睇檔名/到達時間

Server 可以自行校對,但一定要來源一齊送 identity、sequence、offset 同多種 timestamp。最可靠新舊判據係同一 boot 入面嘅 monotonic source offset/batch_seq;timestamp只係輔助。

每個 batch 必帶 manifest

{
  "schema": "A88_INGEST_MANIFEST_V1",
  "kind": "canonical_events | csv_delta | csv_snapshot",
  "source_host_id": "machine-01",
  "process_id": "main-01",
  "boot_id": "uuid-per-process-start",
  "stream_id": "machine-01/main-01/quote",
  "batch_seq": 1042,
  "first_source_offset": 880001,
  "last_source_offset": 881000,
  "min_exchange_event_time_ms": 1795...,
  "max_exchange_event_time_ms": 1795...,
  "min_source_receive_wall_ms": 1795...,
  "max_source_receive_wall_ms": 1795...,
  "source_receive_mono_start_ns": 331...,
  "source_receive_mono_end_ns": 332...,
  "snapshot_as_of_event_time_ms": null,
  "batch_created_at_ms": 1795...,
  "batch_closed_at_ms": 1795...,
  "ntp_offset_ms": 12,
  "row_count": 1000,
  "uncompressed_bytes": 500000,
  "compressed_bytes": 90000,
  "sha256": "...",
  "previous_batch_sha256": "..."
}

四種時間唔可以混埋

時間用途可否直接判新舊
exchange/event time市場事件真正時間;tick/OB/Kline排序及late-event watermark。同 flow內重要,但history/backfill本身會舊,唔可一刀切丟。
source receive wall time來源機收到時間;跨機比較前要 NTP/chrony。有clock skew,唔可單獨作idempotency。
source monotonic time同一process量latency/次序,唔受wall-clock跳動。只可同一 boot 比,跨機無共同原點。
server receive time觀察network delay、arrival order。遲到舊batch可能最後先到,唔等於最新。

Server apply 死次序

  1. 驗身份、schema、row count、bytes、SHA-256;未close/hash唔啱入 quarantine。
  2. 以 (stream_id, boot_id, batch_seq) 查重;已 durable同hash相同=ACK duplicate,唔再apply。
  3. 同 boot 比 offset:last_offset ≤ durable_offset 全舊;有overlap只取未見尾段;first_offset > durable+1 記 gap並要求重送。
  4. 新 boot 必須先有 BOOT control event;offset由新namespace開始,唔同舊 boot數字硬比。
  5. Event lane:按 flow-specific lateness window入reorder buffer;超窗event仍可入canonical history/audit,但唔一定覆蓋 latest state。
  6. CSV snapshot lane:比較 snapshot_seq + snapshot_as_of_event_time;比已套用watermark舊就標 STALE_ARCHIVE_ONLY,唔覆蓋新snapshot。
  7. 只有 WAL fsync後更新 durable watermark+ACK。每次projector套用紀錄 input watermark,restart可重播。
直接回答「有啲機傳過嚟已經係舊 batch,咁就唔使要」:
對「latest snapshot」可以唔套用,但唔建議收到即物理刪除。先標 duplicate/stale,短期保留 audit;canonical event若係合法 late event仍可能要留歷史。History/backfill 必須用獨立 lane,否則佢天生舊 timestamp會被誤殺。

7 · NTP、watermark 與 initial thresholds

8 · Backpressure 狀態機(新人照做)

中央狀態ReceiverSource sender
NORMAL(queue <60%)正常接收、batch fsync、ACK。正常batch。
HIGH(60–80%)縮小decode並發;CSV parse暫停;仍優先canonical WAL。增大batch/壓縮;保留local WAL。
CRITICAL(80–95%)CSV endpoint回429/Retry-After;canonical只收可durable部分。停止刪local segments;指數退避;唔block FTNN callback。
FULL/disk低水位fail closed回503;告警;絕不假ACK/silent drop。local WAL hold;到本地quota先按已批准retention策略,唔自行丟。

9 · 監控同 acceptance gate

每 stream必睇

events/s、wire bytes/s、compressed ratio、queue depth、local WAL bytes、oldest unacked age、durable offset、gap/duplicate/stale count、event lag p50/p95/p99、ACK latency、reconnect/resend。

中央必睇

CPU/iowait/RSS/swap、disk MB/s/IOPS/fsync p95/p99、disk free days、ingest/parse/project queues、CSV parse seconds/GB、open FDs、TLS errors、WAL recovery time。

上線前死 gate

  1. 先錄一部機一個program至少一個完整peak window,得出真 rate distribution。
  2. 離線 replay 1× → 5× → 10×;再16 stream同時開,模擬60秒burst、網絡斷5分鐘、重送、duplicate、out-of-order、舊CSV snapshot。
  3. 1× sustained:CPU p95 <70%、無swap、iowait <10%、queue不單調增、offset 0 gap、false drop 0。
  4. ACK一定喺fsync後;kill -9/restart後由 durable watermark replay,state hash一致。
  5. CSV timeout/parser crash唔影響canonical ACK;舊snapshot唔覆蓋新state。
  6. disk保留至少30% headroom;local spool足夠預計最長斷線(建議先6–24h)。

10 · 分階段落地建議

Phase做法4-core/24GB 判定
0 Measure每個process只加metrics;量 events/s、bytes、CSV cadence/size。可以。
1 Durable ingest16 canonical streams→batched WAL;CSV→compressed immutable spool;唔parse。首選 pilot。
2 Dedup/replay加offset ledger、gaps、idempotency、watermarks;1×/5×/10×測。視CPU/disk;worker要bounded。
3 CSV async parse最多1–2 workers;canonical優先;生成projection。若peak queue增長就拆機/升8–12 cores。
4 Long retentionclosed segments搬object storage;local只留hot spool。必要,否則disk遲早滿。

最終建議:唔好因為「24GB好似夠」就一次過將16 live streams、16 continuous CSV parser、server.js projection全部塞同一 event loop。先將 4/24 定位成 durable landing zone;canonical event係主水源,CSV係獨立低優先 compatibility/export lane。壓測證明有餘量先加parse;否則升級或拆 CSV worker。

11 · 本報告證據邊界

舊版全文(Revision 0,receipt 68215)

以下以 escaped source 原樣保留,供逐字核對;今版內容以上方 Revision 1 為準。

<!doctype html><html><head><meta charset="utf-8"><title>A88 FTNN Cluster and Forwarding Budget</title><style>body{background:#07131b;color:#eef9fb;font:15px/1.65 system-ui,sans-serif}main{width:min(980px,95%);margin:auto}table{width:100%;border-collapse:collapse;background:#0d2530}th,td{padding:9px;border-bottom:1px solid #2e5969;text-align:left}.ok{color:#59dfa9}.warn{color:#ffc86f}.bad{color:#ff8f8f}code{font-family:ui-monospace,monospace}</style></head><body><main><h1>FTNN cluster capacity+source forwarding</h1><h2>1-core / 6GB node</h2><table><thead><tr><th>Layout</th><th>Theoretical SIDs</th><th>判定</th></tr></thead><tbody><tr><td>4 nodes × 1 process ×100</td><td>400</td><td class="ok">Recommended starting topology</td></tr><tr><td>4 nodes × 2 processes ×100</td><td>800</td><td class="warn">Pilot only;2 processes爭1 core,full tick+OB+Kline容易event lag/drop</td></tr><tr><td>4 nodes × 4 processes ×100</td><td>1600</td><td class="bad">Not recommended;4:1 CPU oversubscription</td></tr></tbody></table><p>6GB RAM放2個瘦身FTNN大機會夠(約2.4–3.5GB+OS/cache),但CPU先爆。每程序滿100隻且有orderbook/tick時,1 core安全預算係1個process。兩個process只可喺live-light/低成交symbol試,history必須分phase。</p><h2>同一總資源點揀</h2><table><tbody><tr><th>1×4-core/24GB</th><td>4 processes最簡單、共享cache、管理少;但單機故障全停,四tunnel競爭同host I/O。</td></tr><tr><th>4×1-core/6GB,各1 process</th><td class="ok">同樣4 processes,但fault/tunnel/profile隔離最好;推薦。</td></tr><tr><th>4×1-core/6GB,各2 process</th><td>總8 process但仍只4 cores;唔會憑分機變8 cores。只作階梯壓測。</td></tr><tr><th>各4 process</th><td class="bad">唔係可用容量,係排隊延遲;唔建議。</td></tr></tbody></table><p>升級gate:每node CPU p95&lt;80%、load&lt;0.8/core、event lag p95&lt;2s、no gaps/drop、RSS&lt;4.5GB、no swap,先由1→2 processes。8 tunnels只係network paths,唔增加compute。</p><h2>源頭直接引流到 server.js 機</h2><p class="ok"><b>可以,而且係正確架構。</b>FTNN node只做subscribe+strict decode+local WAL;將canonical events batch送到server.js node,由中央做replay state、V2 JSON同V1 CSV projection。</p><pre><code>FTNN callback → local append-only WAL → batch/compress sender
  → TLS ingest /ingest/v1/events → idempotent event store
  → projector → server.js state/API → V1 CSV / V2 JSON</code></pre><h3>唔好直接「裸轉socket」</h3><ul><li>本地先WAL,server斷線唔丟數;</li><li>event_id+source offset+schema version,server ACK最高durable offset;</li><li>按SID/shard保序;duplicate由event_id去;</li><li>50–200ms或100–1000 events一batch,gzip/zstd;</li><li>backpressure有上限,超限落disk唔block FTNN callback;</li><li>mTLS/owner token、固定內網,不經公共proxy。</li></ul><h2>效能</h2><p>將CSV/JSON projection同大檔fsync搬走,可減FTNN node I/O同CPU jitter;網絡只傳一次canonical event。35隻長跑central JSON約155MB/約2.5h,平均流量低,但orderbook有burst;400隻應以1–5MB/s burst budget做壓測。中央server.js要獨立CPU/disk、批量commit,否則會變新樽頸。</p><h2>建議定案</h2><ol><li>先4台×1 process×100 SIDs=400。</li><li>每台本地WAL+一條sender,集中去server.js ingest。</li><li>穩定30–60分鐘後,只喺低load node試第2 process;唔直接上8/16。</li><li>history/backfill另時段或另worker,唔同live callback並發。</li></ol></main></body></html>