Skip to content

fix(gate-hub): decide with and serve the verified season list - #168

Merged
TN019 merged 9 commits into
mainfrom
fix/active-season-list
Dec 1, 2026
Merged

TN019 merged 9 commits into
mainfrom
fix/active-season-list

Conversation

@TN019

@TN019 TN019 commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Why

The hub synced a data-centre list it never used for decisions, while /tickets reread the start file on every request, so the served bytes could differ from the list behind scan decisions or change mid-reply. A saved ETag could also keep the hub on an old list behind repeated 304 replies.

What changed

  • Mark tickets from new offline reports used under store_lock after reports_record, instead of inside the report store, and document the lock order.
  • Add failing cases first: test_hub_season_reload.py, test_hub_served_list.py and test_hub_tickets_send_change.py, with shared helpers in tests/hub_ticket_support.py.
  • The hub now syncs /season.txt into /tmp/gatepost-season.txt. A list is installed only if its SHA-256 matches the confirmed ETag, it loads under the start-file rules and holds at least one ticket; a rejected list leaves the current one in place and is logged as event=season_list state=rejected. The new store, decisions and /tickets switch together through a reference-counted in-memory snapshot (gate-hub/season.h), carrying used marks and recorded offline reports across the switch.
  • hub_sync_once sends If-None-Match only when the installed body still matches the saved ETag (test_hub_sync_stale_etag.py).
  • /tickets serves the snapshot, with ETag and length taken from it. gatepost_ticket_list_age_seconds now counts from installation, and gatepost_dc_sync_attempts_total{outcome="success"} means hub decisions use the data centre's current list at the end of the round.
  • Add test_hub_season_report_switch.py (a report arriving during a switch keeps its ticket used) and test_hub_season_fsync.py (the install is logged only after the file and directory fsyncs and the rename); adapt test_gate_list_version.py, test_hub_tls.py and test_sync_metrics.py to the season sync.

How to verify

On Linux with the build dependencies listed in Dockerfile.build, run from the repository root:

cmake --preset debug
cmake --build --preset debug
ctest --preset debug -R '^(hub_season_reload|hub_served_list|hub_tickets_send_change|hub_season_report_switch|hub_season_fsync|hub_sync_stale_etag|gate_list_version|sync_metrics|hub_tls|offline_reports)$' --output-on-failure

hub_season_fsync is registered on Linux only. A ticket that exists only in the synced season list must be accepted without restarting the hub, once only, while used marks from the start list are kept and /tickets serves the season list. Hub decisions still do not check per-ticket signatures; gates do.


背景

hub 同步了数据中心的票单,却从不拿它做判断;/tickets 又在每次请求时重读启动文件,提供的字节可能和扫描判断所用的票单不一样,甚至在回复途中发生变化。保存下来的 ETag 还可能让 hub 在一次次 304 响应后一直停留在旧票单上。

改动

  • 新的离线报告在 reports_record 返回之后,由 hub 在 store_lock 下标记对应门票已用,不再在报告存储内部标记;同时写明锁的顺序。
  • 先加入会失败的测试:test_hub_season_reload.py、test_hub_served_list.py、test_hub_tickets_send_change.py,共用的辅助代码在 tests/hub_ticket_support.py。
  • hub 改为把 /season.txt 同步到 /tmp/gatepost-season.txt。只有 SHA-256 与确认过的 ETag 一致、能按启动文件的规则加载、且至少有一张票的票单才会安装;不合格的票单不替换现有票单,并记录 event=season_list state=rejected。新的票单库、入场判断和 /tickets 通过一个带引用计数的内存快照(gate-hub/season.h)一起切换,已用标记和已记录的离线报告都会带过去。
  • hub_sync_once 只有在已安装的内容仍与保存的 ETag 相符时才发送 If-None-Match(test_hub_sync_stale_etag.py)。
  • /tickets 直接提供快照,ETag 和长度都取自快照。gatepost_ticket_list_age_seconds 改为从安装时刻算起;gatepost_dc_sync_attempts_total{outcome="success"} 表示这一轮结束时,hub 的判断用的是数据中心当前的票单。
  • 新增 test_hub_season_report_switch.py(切换期间到达的报告仍让门票保持已用)和 test_hub_season_fsync.py(文件和目录都 fsync、完成改名之后才记录安装);test_gate_list_version.py、test_hub_tls.py 和 test_sync_metrics.py 改用赛季同步。

怎么验证

在 Linux 环境安装 Dockerfile.build 列出的构建依赖后,从仓库根目录运行:

cmake --preset debug
cmake --build --preset debug
ctest --preset debug -R '^(hub_season_reload|hub_served_list|hub_tickets_send_change|hub_season_report_switch|hub_season_fsync|hub_sync_stale_etag|gate_list_version|sync_metrics|hub_tls|offline_reports)$' --output-on-failure

hub_season_fsync 只在 Linux 上注册。只存在于同步后赛季票单中的门票,无需重启 hub 就应放行且只放行一次;启动票单里的已用标记要保留,/tickets 要提供赛季票单。hub 的判断仍不核验每张票的签名,这由闸机负责。

Mateo Rossi and others added 9 commits October 28, 2026 11:30
…s heartbeat

The hub could not tell which list each gate had installed. List age
based on file time also kept growing through 304 confirmations.

Keep the verified SHA-256 and monotonic confirmation time at each gate.
Send the installed version and time since confirmation in CONFIG, then
compare it with the list the hub currently serves. Gates reporting no
version have unknown status, which is not an error. A gate retries an
empty CONFIG on the same connection for a hub not yet upgraded.

The hub trusts the reported list state as sent: CONFIG has no MAC, so
whoever holds a gate's certificate can report a false state for that
gate.

Co-authored-by: Ben Carter <ben.carter@elsworne.example>
On-call staff could only read raw /metrics. Provision a dashboard for
the existing hub series: incomplete ticket replies, TLS handshake
failures, and per-gate list status.

Use a separate Compose file so the venue lab and its drills need no
Grafana password. Pin both images by tag and digest.

Keep the earlier sync-metrics regression test for the existing sync
accounting fix, and check dashboard metrics against the hub.

Co-authored-by: Yuki Tanaka <yuki.tanaka@elsworne.example>
The hub recorded connections that had not identified themselves under
the name "unknown". Writing list status for every record made that
pseudo-gate look like a gate reporting no version.

Write list status only for gates that identified themselves and sent
a heartbeat. A CONFIG from an unidentified connection records nothing.
… the uplink watcher has stopped

Cut both data-centre exits, restore them, then kill the hub's uplink
watcher. Compare Mailpit message IDs across each separate fault.

Each case expects exactly one mail to manager@elsworne.example from
alerts@elsworne.example through the venue's own mail server, which
remains available without the data-centre link. The subjects are
"Data centre unreachable" and "Uplink watcher stopped". Allow 20 s:
5 s for the probe, 10 s for the alert threshold, 1 s for the alert
loop, and 4 s for mail submission; the watcher checks every second.

Today neither mail is sent, so both judgements fail. Exit 0 means both
pass, 1 means a judgement fails, and 2 means an environment or cleanup
failure.
…he uplink watcher stops

The hub logged data-centre failures, and the uplink watcher could exit
without notifying the venue.

Submit mail after the shared alert threshold, once per incident. Keep
sent incidents in the atomic state file across hub restarts, and re-arm
each alert when its condition recovers without sending recovery mail.

Have the watcher update a heartbeat file each loop. Its modification
time uses the clock shared with the hub, since a shell cannot portably
read a monotonic clock. Give restarted hubs a fresh threshold of grace.

Add a CI test for transient and repeated faults, recovery, mail headers,
restart persistence, and the watcher grace period.

Co-authored-by: Yuki Tanaka <yuki.tanaka@elsworne.example>
A signed barcode of about 159 characters does not fit the binary
list's 32-byte record. Publish a 20000-ticket signed text list at
/season.txt from both data-centre services at lab scale. Keep the
signing key in dc and dc2 only. Leave /tickets.gpb and the hub
unchanged. Test publication with the gates' signature verifier,
HTTP and TLS responses, caching, ranges, and tampering.
Move ticket marking out of add_entry. Startup journal loading still
marks each entry in order; after a new reports_record result, the hub
marks the ticket under store_lock before updating status and metrics
or replying.

The ticket store is read under store_lock during requests, and the
report lock is never held with it. Behaviour is unchanged: admission
was already thread-safe and the store does not change at run time
today. Document the lock order and startup journal marking.
The hub still rejects a signed ticket found only in the synced season list, although a successful sync should make that list available for decisions. After the start file changes in place, /tickets serves its new bytes while scans still use the loaded list; both should use the same snapshot. Growing or shrinking the file during a /tickets reply changes the bytes sent, and a short reply is counted as 200; the reply should contain the loaded list and be counted accurately.
The hub synced a list it never used for decisions, while /tickets reread the start file on every request. A header-time ETag could leave the hub on an old list behind repeated 304 replies.

The hub now syncs /season.txt, checks the installed body before conditional requests, and switches decisions and /tickets together using a reference-counted snapshot. It carries used admissions and recorded offline reports across a switch.

Here verified means the downloaded file SHA-256 matches the confirmed ETag, the whole list loads under the start-file rules, and the file and its directory have been synced.
@TN019

TN019 commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

Ingrid Larsen · reviewer

Can an offline report arriving during list preparation lose its used flag?


准备新票单时到达的离线报告,会不会丢掉已用标记?

@TN019

TN019 commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

Ben Carter · author

No. The switch copies the reported barcodes before taking store_lock, carries used marks by barcode, and copies the reports a second time after the switch to mark tickets that exist only in the new list. test_hub_season_report_switch.py holds the switch between the copy and the lock while a report arrives.


不会。切换前先复制已报告的条码,再拿 store_lock,按条码带过已用标记;切换后再复制一次报告,把只在新票单里的门票补标上。test_hub_season_report_switch.py 会让切换停在复制和加锁之间,同时送入一份报告。

@TN019

TN019 commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

Wang Hao · reviewer

What does a sync success mean on the dashboard now: a download, or an installed list?


现在面板上的同步 success 是指下载成功,还是票单已经装上?

@TN019

TN019 commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

Ben Carter · author

An installed list. A round counts as success only if, at its end, hub decisions use the data centre's current list, whether installed from a 200 or confirmed by a 304. Download, HTTP, validation and installation failures count as error.


是装上。只有这一轮结束时 hub 的判断用的是数据中心当前的票单,才算成功,不管是 200 后新装的,还是 304 确认的。下载、HTTP、校验和安装失败都记为 error。

@TN019
TN019 merged commit 591cfcc into main Dec 1, 2026
5 checks passed
@TN019
TN019 deleted the fix/active-season-list branch December 1, 2026 02:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant