|
| 1 | +# ADR 0010: Atomic registration of prepared configuration batches |
| 2 | + |
| 3 | +- Status: Proposed |
| 4 | +- Date: 2026-11-12 |
| 5 | + |
| 6 | +## Context |
| 7 | + |
| 8 | +`prepare_configuration()` produces a complete `ConfigurationPreparedSet`. |
| 9 | +Each entry owns an `ILogger` and an `ILogFormatter` through `std::shared_ptr`, |
| 10 | +because factories may share those objects and the prepared result is copyable. |
| 11 | +The live `Logger` registry currently stores each pair in `LoggerStrategy` as |
| 12 | +`std::unique_ptr` and exposes `add_logger()` for one-at-a-time registration. |
| 13 | + |
| 14 | +Converting a shared pointer to a unique pointer through `get()` would create |
| 15 | +double ownership or dangling references. Registering entries one by one would |
| 16 | +also publish a partially applied configuration if a later entry is invalid. |
| 17 | +The existing copy-on-write strategy snapshot and shutdown checks must remain |
| 18 | +the publication boundary for concurrent dispatch. |
| 19 | + |
| 20 | +## Decision |
| 21 | + |
| 22 | +Add a separate configuration-registration layer; do not change parsing, |
| 23 | +binding resolution, or the existing `Logger::add_logger()` call contract in |
| 24 | +this slice. |
| 25 | + |
| 26 | +The future registration operation will accept a complete prepared set and an |
| 27 | +explicit registration-options object. It will: |
| 28 | + |
| 29 | +1. validate the entire batch before publication; |
| 30 | +2. build all new strategies and registration metadata while holding the |
| 31 | + registry write lock (or before taking it when no live state is inspected); |
| 32 | +3. check `m_shutdown` before preparation and again before publication; |
| 33 | +4. publish one immutable strategy-list snapshot with one release-store; |
| 34 | +5. return a stable mapping from each configuration `id` to its runtime index. |
| 35 | + |
| 36 | +The operation is append-only in v1. Existing strategies are preserved. New |
| 37 | +configuration entries use `single_mode=false` unless an explicit per-entry |
| 38 | +registration option says otherwise. The operation rejects duplicate IDs in the |
| 39 | +incoming batch and IDs already registered by this configuration-registration |
| 40 | +layer. Legacy `add_logger()` entries have no configuration ID and do not |
| 41 | +silently acquire one. Repeated registration is therefore an explicit append |
| 42 | +operation, not replacement or hot reload; callers that need replacement must |
| 43 | +use a separately designed API. |
| 44 | + |
| 45 | +The runtime index of an appended entry is its position in the published |
| 46 | +registry after the existing strategies. The returned ID/index mapping is the |
| 47 | +only supported way for a configuration caller to address a logger; vector |
| 48 | +order and backend type are not inferred from the ID. |
| 49 | + |
| 50 | +### Ownership boundary |
| 51 | + |
| 52 | +The recommended implementation changes `LoggerStrategy`'s internal owners to |
| 53 | +`std::shared_ptr`. The unchanged public `add_logger(std::unique_ptr<...>)` API |
| 54 | +will wrap each incoming unique pointer in a shared pointer internally. The new |
| 55 | +batch operation can then share the prepared logger and formatter without raw |
| 56 | +pointer conversion or an ownership transfer that invalidates the prepared |
| 57 | +set. Existing dispatch, per-strategy execution locking, and snapshot lifetime |
| 58 | +rules remain unchanged. |
| 59 | + |
| 60 | +The prepared set remains caller-owned after successful registration. Clearing |
| 61 | +or destroying it must not invalidate the live registry. A failed registration |
| 62 | +must leave both the caller's prepared set and the live registry unchanged. |
| 63 | + |
| 64 | +### Atomicity and side effects |
| 65 | + |
| 66 | +Atomicity covers caller-visible registry publication only. The prepared set is |
| 67 | +already materialized, so registration must not invoke factories, resolve |
| 68 | +bindings, parse input, or start a backend-specific lifecycle outside the |
| 69 | +normal logger construction contract. Backend shutdown remains governed by the |
| 70 | +existing `Logger::shutdown()` and strategy lifetime rules. |
| 71 | + |
| 72 | +## Consequences |
| 73 | + |
| 74 | +- Configuration batches become visible to dispatch all at once. |
| 75 | +- Existing applications retain the unique-pointer `add_logger()` API. |
| 76 | +- Shared ownership removes unsafe pointer conversion and allows prepared-set |
| 77 | + copies without changing logger lifetime semantics. |
| 78 | +- The internal `LoggerStrategy` owner type changes, so implementation work must |
| 79 | + cover shutdown, snapshots, `wait()`, clear/read APIs, targeted indices, and |
| 80 | + concurrent dispatch on both C++11 and C++17. |
| 81 | +- Registration does not provide replacement, rollback of external backend |
| 82 | + effects, file watching, or hot reload. |
| 83 | +- Configuration IDs require a small registry-side metadata map; they are not |
| 84 | + retrofitted onto legacy unnamed registrations. |
| 85 | + |
| 86 | +## Alternatives considered |
| 87 | + |
| 88 | +### Move prepared pointers into `unique_ptr` |
| 89 | + |
| 90 | +Rejected. It would make `ConfigurationPreparedSet` move-only, break the |
| 91 | +existing factory/shared-ownership contract, and make failed publication harder |
| 92 | +to preserve safely. |
| 93 | + |
| 94 | +### Convert `shared_ptr` through `get()` |
| 95 | + |
| 96 | +Rejected. Constructing a `unique_ptr` from that raw pointer would either cause |
| 97 | +double deletion or require a non-owning deleter, leaving lifetime dependent on |
| 98 | +the prepared set. |
| 99 | + |
| 100 | +### Register each entry with the existing `add_logger()` |
| 101 | + |
| 102 | +Rejected for a configuration batch. It exposes partial state and cannot return |
| 103 | +one atomic publication point or an unambiguous batch ID/index mapping. |
| 104 | + |
| 105 | +### Replace the registry wholesale on every configuration load |
| 106 | + |
| 107 | +Deferred. Replacement needs explicit semantics for in-flight records, |
| 108 | +shutdown, ID collisions, and rollback. It is not implicit hot reload. |
| 109 | + |
| 110 | +## References |
| 111 | + |
| 112 | +- [Configuration runtime preparation](../configuration-loading.md) |
| 113 | +- [ADR 0003: Immutable logger registry snapshots](0003-immutable-registry-snapshots.md) |
| 114 | +- [Logger registry implementation](../../include/logit_cpp/logit/Logger.hpp) |
| 115 | +- [Prepared runtime types](../../include/logit_cpp/logit/configuration_runtime.hpp) |
0 commit comments