Metrics reporting and retention
Generated page
Model gemma-mtp, commit ef58254ca7be, 2026-08-16, sources: 6. Edit the code or the hand-written documentation instead.
Diagram
Metrics snapshotting and rate reporting
The Metrics class provides a snapshot of the current state of the Broker, which is used to calculate performance. Instead of printing cumulative counters—which would require the operator to perform manual differentiation during an incident—the reportMetrics function transforms these raw values into rates (e.g., records per second) by comparing the current snapshot against a previous one over a specific time interval Main.kt:119-123. This interval is configurable via metricsIntervalMillis in the BooblikConfig BooblikConfig.kt:47.
The applyRetention timer
The broker itself is designed to be stateless regarding time, allowing tests to advance time manually by calling broker.applyRetention directly Main.kt:131-133. The actual temporal logic is encapsulated in a dedicated background coroutine launched in Main.kt:86 Main.kt:86. This loop uses delay(config.retentionCheckMillis) to determine when to trigger the retention logic Main.kt:141, ensuring that the "when" is decoupled from the "how" of the broker's internal logic Main.kt:136-144.
Retention by size and age
Retention is governed by two optional parameters defined in BooblikConfig BooblikConfig.kt:42-43:
| Key | Type | Description |
|---|---|---|
retentionBytes | Long? | Maximum total size of the log to keep per partition |
retentionMillis | Long? | Maximum age of a segment to keep |
When applyRetention is called, the broker removes segments that exceed these thresholds Main.kt:142.
Shutdown sequence and data integrity
To prevent data loss, the application follows a strict, layered shutdown sequence within the Runtime shutdown hook Main.kt:91-103. The order is critical:
- The
BooblikServeris closed firstMain.kt:94. - Background coroutines (metrics and retention) are cancelled via
background.cancel()to stop any pending tasksMain.kt:95. - The
brokeris closed lastMain.kt:98. This ensures that the broker's writers are still active while the server is shutting down, allowing any accepted batches to reach the disk before the underlying log is closedMain.kt:97-98.
Key files
| File | Lines | What is there |
|---|---|---|
…/app/Main.kt | 84-104 | The main loop, shutdown hook, and background coroutine orchestration. |
…/app/BooblikConfig.kt | 42-43 | Configuration properties for retention and metrics intervals. |
Behaviour that surprises
- Decoupled Time: The
Brokerdoes not have its own clock; it relies on an external caller (like theapplyRetentionloop inMain.kt) to tell it when to perform maintenanceMain.kt:131-133. - Shutdown Order: The
brokeris closed after the background coroutines are cancelled to ensure that any data currently being processed by the server can be flushed to the log before the log itself is closedMain.kt:95-98. - Rate-based Metrics:
reportMetricsspecifically avoids printing cumulative counters to prevent the need for manual differentiation during incident responseMain.kt:110-111.