Storage Write Queue Observer Architecture & Inverted Dependency Governance
Overview
This architecture document defines the governance rules for decoupled event dispatching and dependency inversion within the Content-Addressable Storage (CAS) write queue subsystem (pkg/storage/cas).
Background & Problem Statement
Prior to this specification, ListingIndexWriteQueue relied on global atomic function pointers (globalListingIndexBatchEventCallback, SetListingIndexBatchEventCallback) to communicate batch lifecycle events across packages. The presentation layer (cmd/zqk/system) mutated these global variables at startup to bind coordinator event emission.
This pattern violated core architectural layering invariants:
- Presentation Layer Contamination: Presentation components mutated low-level storage internal hooks.
- Multi-Instance Interference: Concurrent test cases or isolated storage instances shared the same mutable global pointer, causing event leakage and test race conditions.
- Inverted Dependency Injection: Hook mutation bypassed compile-time interface contracts.
Architectural Solution: Observer Pattern
The storage write queue subsystem implements the Observer pattern at the instance level:
┌─────────────────────────────────┐
│ ListingIndexWriteQueue │
│ (Manages per-kind write queues)│
└────────────────┬────────────────┘
│
▼ RegisterBatchEventListener(listener)
┌──────────────────────────────────────────────────┐
│ ListingIndexBatchEventListener │
│ <<interface>> │
│ + OnListingIndexBatchEvent(...) │
└──────────────────────────────────────────────────┘
▲
│ implements
┌────────────────┴──────────────────┐
│ Coordinator/Logging Observers │
│ (e.g. ListingIndexBatchEventFunc)
└───────────────────────────────────┘
Invariants
- Instance-Scoped Listeners:
ListingIndexWriteQueuemaintains an internal slice of registered listeners protected by a read-write mutex. - Zero Global Mutation: Presentation and coordinator layers wire observers onto the queue instance directly rather than mutating package-level function pointers.
- Concurrency Safety: Workers dispatch events asynchronously to instance-registered listeners without holding internal queue locks, ensuring non-blocking event flow and zero cross-queue leakage.