@@ -2,111 +2,8 @@ use crate::durability::{request_durability, spawn_close as spawn_durability_clos
22use crate :: error:: { DBError , RestoreSnapshotError } ;
33use crate :: metrics:: ExecutionCounters ;
44use crate :: metrics:: ENGINE_METRICS ;
5-
6- /// Whether SpacetimeDB is run in memory, or persists objects and
7- /// a message log to disk.
8- #[ derive( Clone , Copy ) ]
9- pub enum Storage {
10- /// The object store is in memory, and no message log is kept.
11- Memory ,
12-
13- /// The object store is persisted to disk, and a message log is kept.
14- Disk ,
15- }
16-
17- /// Internal database config parameters
18- #[ derive( Clone , Copy ) ]
19- pub struct Config {
20- /// Specifies the object storage model.
21- pub storage : Storage ,
22- /// Specifies the page pool max size in bytes.
23- pub page_pool_max_size : Option < usize > ,
24- }
25-
26- /// A message that is processed by the [`spawn_metrics_recorder`] actor.
27- /// We use a separate task to record metrics to avoid blocking transactions.
28- pub struct MetricsMessage {
29- /// The reducer the produced these metrics.
30- reducer : Option < ReducerName > ,
31- /// Metrics from a mutable transaction.
32- metrics_for_writer : Option < TxMetrics > ,
33- /// Metrics from a read-only transaction.
34- /// A message may have metrics for both types of transactions,
35- /// because metrics for a reducer and its subscription updates are recorded together.
36- metrics_for_reader : Option < TxMetrics > ,
37- /// The row updates for an immutable transaction.
38- /// Needed for insert and delete counters.
39- tx_data : Option < Arc < TxData > > ,
40- /// Cached metrics counters for each workload type.
41- counters : Arc < EnumMap < WorkloadType , ExecutionCounters > > ,
42- }
43-
44- /// The handle used to send work to the tx metrics recorder.
45- #[ derive( Clone ) ]
46- pub struct MetricsRecorderQueue {
47- tx : spacetimedb_runtime:: sync:: mpsc:: UnboundedSender < MetricsMessage > ,
48- }
49-
50- impl MetricsRecorderQueue {
51- pub fn send_metrics (
52- & self ,
53- reducer : Option < ReducerName > ,
54- metrics_for_writer : Option < TxMetrics > ,
55- metrics_for_reader : Option < TxMetrics > ,
56- tx_data : Option < Arc < TxData > > ,
57- counters : Arc < EnumMap < WorkloadType , ExecutionCounters > > ,
58- ) {
59- if let Err ( err) = self . tx . send ( MetricsMessage {
60- reducer,
61- metrics_for_writer,
62- metrics_for_reader,
63- tx_data,
64- counters,
65- } ) {
66- log:: warn!( "failed to send metrics: {err}" ) ;
67- }
68- }
69- }
70-
71- fn record_metrics (
72- MetricsMessage {
73- reducer,
74- metrics_for_writer,
75- metrics_for_reader,
76- tx_data,
77- counters,
78- } : MetricsMessage ,
79- ) {
80- if let Some ( tx_metrics) = metrics_for_writer {
81- tx_metrics. report ( tx_data. as_deref ( ) , reducer. as_ref ( ) , |wl| & counters[ wl] ) ;
82- }
83- if let Some ( tx_metrics) = metrics_for_reader {
84- tx_metrics. report ( None , reducer. as_ref ( ) , |wl| & counters[ wl] ) ;
85- }
86- }
87-
88- const TX_METRICS_RECORDING_INTERVAL : std:: time:: Duration = std:: time:: Duration :: from_millis ( 5 ) ;
89-
90- /// Spawns a task for recording transaction metrics.
91- /// Returns the handle for pushing metrics to the recorder.
92- pub fn spawn_tx_metrics_recorder (
93- handle : & spacetimedb_runtime:: Handle ,
94- ) -> ( MetricsRecorderQueue , spacetimedb_runtime:: AbortHandle ) {
95- let handle_clone = handle. clone ( ) ;
96- let ( tx, mut rx) = spacetimedb_runtime:: sync:: mpsc:: unbounded_channel ( ) ;
97- let abort_handle = handle
98- . spawn ( async move {
99- loop {
100- handle_clone. sleep ( TX_METRICS_RECORDING_INTERVAL ) . await ;
101- while let Ok ( metrics) = rx. try_recv ( ) {
102- record_metrics ( metrics) ;
103- }
104- }
105- } )
106- . abort_handle ( ) ;
107- ( MetricsRecorderQueue { tx } , abort_handle)
108- }
1095use crate :: util:: asyncify;
6+ use crate :: MetricsRecorderQueue ;
1107use anyhow:: { anyhow, Context } ;
1118use enum_map:: EnumMap ;
1129use spacetimedb_commitlog:: repo:: OnNewSegmentFn ;
@@ -1935,6 +1832,7 @@ fn default_row_count_fn(db: Identity) -> RowCountFn {
19351832pub mod tests_utils {
19361833 use crate :: snapshot;
19371834 use crate :: snapshot:: SnapshotWorker ;
1835+ use crate :: MetricsRecorderQueue ;
19381836
19391837 use super :: * ;
19401838 use core:: ops:: Deref ;
0 commit comments