1- // Copyright 2022 CeresDB Project Authors. Licensed under Apache-2.0.
1+ // Copyright 2022-2023 CeresDB Project Authors. Licensed under Apache-2.0.
22
3- use std:: { fmt, time:: Instant } ;
3+ use std:: {
4+ fmt,
5+ time:: { Duration , Instant } ,
6+ } ;
47
58use async_trait:: async_trait;
69use common_types:: {
@@ -12,6 +15,7 @@ use futures::StreamExt;
1215use log:: debug;
1316use snafu:: { ResultExt , Snafu } ;
1417use table_engine:: { predicate:: PredicateRef , table:: TableId } ;
18+ use trace_metric:: { MetricsCollector , TraceMetricWhenDrop } ;
1519
1620use crate :: {
1721 row_iter:: {
@@ -48,6 +52,7 @@ define_result!(Error);
4852#[ derive( Clone , Debug ) ]
4953pub struct ChainConfig < ' a > {
5054 pub request_id : RequestId ,
55+ pub metrics_collector : Option < MetricsCollector > ,
5156 pub deadline : Option < Instant > ,
5257 pub space_id : SpaceId ,
5358 pub table_id : TableId ,
@@ -116,7 +121,7 @@ impl<'a> Builder<'a> {
116121 false ,
117122 self . config . predicate . as_ref ( ) ,
118123 self . config . deadline ,
119- None ,
124+ self . config . metrics_collector . clone ( ) ,
120125 )
121126 . context ( BuildStreamFromMemtable ) ?;
122127 streams. push ( stream) ;
@@ -132,7 +137,7 @@ impl<'a> Builder<'a> {
132137 false ,
133138 self . config . predicate . as_ref ( ) ,
134139 self . config . deadline ,
135- None ,
140+ self . config . metrics_collector . clone ( ) ,
136141 )
137142 . context ( BuildStreamFromMemtable ) ?;
138143 streams. push ( stream) ;
@@ -147,7 +152,7 @@ impl<'a> Builder<'a> {
147152 self . config . sst_factory ,
148153 & self . config . sst_read_options ,
149154 self . config . store_picker ,
150- None ,
155+ self . config . metrics_collector . clone ( ) ,
151156 )
152157 . await
153158 . context ( BuildStreamFromSst ) ?;
@@ -168,41 +173,56 @@ impl<'a> Builder<'a> {
168173 streams,
169174 ssts : self . ssts ,
170175 next_stream_idx : 0 ,
171- inited : false ,
172- metrics : Metrics :: new ( self . memtables . len ( ) , total_sst_streams) ,
176+ inited_at : None ,
177+ created_at : Instant :: now ( ) ,
178+ metrics : Metrics :: new (
179+ self . memtables . len ( ) ,
180+ total_sst_streams,
181+ self . config . metrics_collector . clone ( ) ,
182+ ) ,
173183 } )
174184 }
175185}
176186
177187/// Metrics for [ChainIterator].
188+ #[ derive( TraceMetricWhenDrop ) ]
178189struct Metrics {
190+ #[ metric( number) ]
179191 num_memtables : usize ,
192+ #[ metric( number) ]
180193 num_ssts : usize ,
181194 /// Total batch fetched.
195+ #[ metric( number) ]
182196 total_batch_fetched : usize ,
183197 /// Total rows fetched.
198+ #[ metric( number) ]
184199 total_rows_fetched : usize ,
185200 /// Create time of the metrics.
186- create_at : Instant ,
201+ #[ metric( duration) ]
202+ since_create : Duration ,
187203 /// Inited time of the iterator.
188- inited_at : Option < Instant > ,
204+ #[ metric( duration) ]
205+ since_init : Duration ,
206+ #[ metric( collector) ]
207+ metrics_collector : Option < MetricsCollector > ,
189208}
190209
191210impl Metrics {
192- fn new ( num_memtables : usize , num_ssts : usize ) -> Self {
211+ fn new (
212+ num_memtables : usize ,
213+ num_ssts : usize ,
214+ metrics_collector : Option < MetricsCollector > ,
215+ ) -> Self {
193216 Self {
194217 num_memtables,
195218 num_ssts,
196219 total_batch_fetched : 0 ,
197220 total_rows_fetched : 0 ,
198- create_at : Instant :: now ( ) ,
199- inited_at : None ,
221+ since_create : Duration :: default ( ) ,
222+ since_init : Duration :: default ( ) ,
223+ metrics_collector,
200224 }
201225 }
202-
203- fn set_inited_time ( & mut self ) {
204- self . inited_at = Some ( Instant :: now ( ) ) ;
205- }
206226}
207227
208228impl fmt:: Debug for Metrics {
@@ -212,8 +232,8 @@ impl fmt::Debug for Metrics {
212232 . field ( "num_ssts" , & self . num_ssts )
213233 . field ( "total_batch_fetched" , & self . total_batch_fetched )
214234 . field ( "total_rows_fetched" , & self . total_rows_fetched )
215- . field ( "duration_since_create" , & self . create_at . elapsed ( ) )
216- . field ( "duration_since_init" , & self . inited_at . map ( |v| v . elapsed ( ) ) )
235+ . field ( "duration_since_create" , & self . since_create )
236+ . field ( "duration_since_init" , & self . since_init )
217237 . finish ( )
218238 }
219239}
@@ -234,19 +254,19 @@ pub struct ChainIterator {
234254 /// The range of the index is [0, streams.len()] and the iterator is
235255 /// exhausted if it reaches `streams.len()`.
236256 next_stream_idx : usize ,
237- inited : bool ,
238257
258+ inited_at : Option < Instant > ,
259+ created_at : Instant ,
239260 // metrics for the iterator.
240261 metrics : Metrics ,
241262}
242263
243264impl ChainIterator {
244265 fn init_if_necessary ( & mut self ) {
245- if self . inited {
266+ if self . inited_at . is_some ( ) {
246267 return ;
247268 }
248- self . inited = true ;
249- self . metrics . set_inited_time ( ) ;
269+ self . inited_at = Some ( Instant :: now ( ) ) ;
250270
251271 debug ! ( "Init ChainIterator, space_id:{}, table_id:{:?}, request_id:{}, total_streams:{}, schema:{:?}" ,
252272 self . space_id, self . table_id, self . request_id, self . streams. len( ) , self . schema
@@ -257,8 +277,8 @@ impl ChainIterator {
257277impl Drop for ChainIterator {
258278 fn drop ( & mut self ) {
259279 debug ! (
260- "Chain iterator dropped, space_id:{}, table_id:{:?}, request_id:{}, metrics:{:?}" ,
261- self . space_id, self . table_id, self . request_id, self . metrics,
280+ "Chain iterator dropped, space_id:{}, table_id:{:?}, request_id:{}, inited_at:{:?}, metrics:{:?}" ,
281+ self . space_id, self . table_id, self . request_id, self . inited_at , self . metrics,
262282 ) ;
263283 }
264284}
@@ -296,6 +316,13 @@ impl RecordBatchWithKeyIterator for ChainIterator {
296316 }
297317 }
298318
319+ self . metrics . since_create = self . created_at . elapsed ( ) ;
320+ self . metrics . since_init = self
321+ . inited_at
322+ . as_ref ( )
323+ . map ( |v| v. elapsed ( ) )
324+ . unwrap_or_default ( ) ;
325+
299326 Ok ( None )
300327 }
301328}
@@ -331,8 +358,9 @@ mod tests {
331358 streams,
332359 ssts : Vec :: new ( ) ,
333360 next_stream_idx : 0 ,
334- inited : false ,
335- metrics : Metrics :: new ( 0 , 0 ) ,
361+ inited_at : None ,
362+ created_at : Instant :: now ( ) ,
363+ metrics : Metrics :: new ( 0 , 0 , None ) ,
336364 } ;
337365
338366 check_iterator ( & mut chain_iter, expect_rows) . await ;
0 commit comments