1use std::collections::HashMap;
2use std::fmt::Debug;
3use std::path::PathBuf;
4use std::sync::atomic::AtomicBool;
5use std::sync::atomic::AtomicU64;
6use std::sync::atomic::AtomicUsize;
7use std::sync::atomic::Ordering;
8use std::sync::Arc;
9use std::thread;
10use std::time::Duration;
11use std::time::Instant;
12use std::time::UNIX_EPOCH;
13
14use conjure_object::BearerToken;
15use conjure_object::ResourceIdentifier;
16use nominal_api::tonic::io::nominal::scout::api::proto::array_points::ArrayType;
17use nominal_api::tonic::io::nominal::scout::api::proto::points::PointsType;
18use nominal_api::tonic::io::nominal::scout::api::proto::ArrayPoints;
19use nominal_api::tonic::io::nominal::scout::api::proto::Channel;
20use nominal_api::tonic::io::nominal::scout::api::proto::DoublePoint;
21use nominal_api::tonic::io::nominal::scout::api::proto::IntegerPoint;
22use nominal_api::tonic::io::nominal::scout::api::proto::Points;
23use nominal_api::tonic::io::nominal::scout::api::proto::Series;
24use nominal_api::tonic::io::nominal::scout::api::proto::StringPoint;
25use nominal_api::tonic::io::nominal::scout::api::proto::WriteRequestNominal;
26use parking_lot::Condvar;
27use parking_lot::Mutex;
28use parking_lot::MutexGuard;
29use tracing::debug;
30use tracing::error;
31use tracing::info;
32
33use crate::client::NominalApiClients;
34use crate::client::PRODUCTION_API_URL;
35use crate::consumer::AvroFileConsumer;
36use crate::consumer::DualWriteRequestConsumer;
37use crate::consumer::ListeningWriteRequestConsumer;
38use crate::consumer::NominalCoreConsumer;
39use crate::consumer::RequestConsumerWithFallback;
40use crate::consumer::WriteRequestConsumer;
41use crate::listener::LoggingListener;
42use crate::types::ChannelDescriptor;
43use crate::types::IntoPoints;
44use crate::types::IntoTimestamp;
45
46#[derive(Debug, Clone)]
47pub struct NominalStreamOpts {
48 pub max_points_per_record: usize,
49 pub max_request_delay: Duration,
50 pub max_buffered_requests: usize,
51 pub request_dispatcher_tasks: usize,
52 pub base_api_url: String,
53}
54
55impl Default for NominalStreamOpts {
56 fn default() -> Self {
57 Self {
58 max_points_per_record: 250_000,
59 max_request_delay: Duration::from_millis(100),
60 max_buffered_requests: 4,
61 request_dispatcher_tasks: 8,
62 base_api_url: PRODUCTION_API_URL.to_string(),
63 }
64 }
65}
66
67#[derive(Default)]
68pub struct NominalDatasetStreamBuilder {
69 stream_to_core: Option<(BearerToken, ResourceIdentifier, tokio::runtime::Handle)>,
70 stream_to_file: Option<PathBuf>,
71 file_fallback: Option<PathBuf>,
72 listeners: Vec<Arc<dyn crate::listener::NominalStreamListener>>,
73 opts: NominalStreamOpts,
74}
75
76impl Debug for NominalDatasetStreamBuilder {
77 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
78 f.debug_struct("NominalDatasetStreamBuilder")
79 .field("stream_to_core", &self.stream_to_core.is_some())
80 .field("stream_to_file", &self.stream_to_file)
81 .field("file_fallback", &self.file_fallback)
82 .field("listeners", &self.listeners.len())
83 .finish()
84 }
85}
86
87impl NominalDatasetStreamBuilder {
88 pub fn new() -> Self {
89 Self::default()
90 }
91}
92
93impl NominalDatasetStreamBuilder {
94 pub fn stream_to_core(
95 self,
96 bearer_token: BearerToken,
97 dataset: ResourceIdentifier,
98 handle: tokio::runtime::Handle,
99 ) -> NominalDatasetStreamBuilder {
100 NominalDatasetStreamBuilder {
101 stream_to_core: Some((bearer_token, dataset, handle)),
102 stream_to_file: self.stream_to_file,
103 file_fallback: self.file_fallback,
104 listeners: self.listeners,
105 opts: self.opts,
106 }
107 }
108
109 pub fn stream_to_file(mut self, file_path: impl Into<PathBuf>) -> Self {
110 self.stream_to_file = Some(file_path.into());
111 self
112 }
113
114 pub fn with_file_fallback(mut self, file_path: impl Into<PathBuf>) -> Self {
115 self.file_fallback = Some(file_path.into());
116 self
117 }
118
119 pub fn add_listener(
120 mut self,
121 listener: Arc<dyn crate::listener::NominalStreamListener>,
122 ) -> Self {
123 self.listeners.push(listener);
124 self
125 }
126
127 pub fn with_listeners(
128 mut self,
129 listeners: Vec<Arc<dyn crate::listener::NominalStreamListener>>,
130 ) -> Self {
131 self.listeners = listeners;
132 self
133 }
134
135 pub fn with_options(mut self, opts: NominalStreamOpts) -> Self {
136 self.opts = opts;
137 self
138 }
139
140 #[cfg(feature = "logging")]
141 fn init_logging(self, directive: Option<&str>) -> Self {
142 use tracing_subscriber::layer::SubscriberExt;
143 use tracing_subscriber::util::SubscriberInitExt;
144
145 let base = tracing_subscriber::EnvFilter::builder()
147 .with_default_directive(tracing_subscriber::filter::LevelFilter::DEBUG.into());
148 let env_filter = match directive {
149 Some(d) => base.parse_lossy(d),
150 None => base.from_env_lossy(),
151 };
152
153 let subscriber = tracing_subscriber::registry()
154 .with(
155 tracing_subscriber::fmt::layer()
156 .with_thread_ids(true)
157 .with_thread_names(true)
158 .with_line_number(true),
159 )
160 .with(env_filter);
161
162 if let Err(error) = subscriber.try_init() {
163 eprintln!("nominal streaming failed to enable logging: {error}");
164 }
165
166 self
167 }
168
169 #[cfg(feature = "logging")]
170 pub fn enable_logging(self) -> Self {
171 self.init_logging(None)
172 }
173
174 #[cfg(feature = "logging")]
175 pub fn enable_logging_with_directive(self, log_directive: &str) -> Self {
176 self.init_logging(Some(log_directive))
177 }
178
179 pub fn build(self) -> NominalDatasetStream {
180 let core_consumer = self.core_consumer();
181 let file_consumer = self.file_consumer();
182 let fallback_consumer = self.fallback_consumer();
183
184 match (core_consumer, file_consumer, fallback_consumer) {
185 (None, None, _) => panic!("nominal dataset stream must either stream to file or core"),
186 (Some(_), Some(_), Some(_)) => {
187 panic!("must choose one of stream_to_file and file_fallback when streaming to core")
188 }
189 (Some(core), None, None) => self.into_stream(core),
190 (Some(core), None, Some(fallback)) => {
191 self.into_stream(RequestConsumerWithFallback::new(core, fallback))
192 }
193 (None, Some(file), None) => self.into_stream(file),
194 (None, Some(file), Some(fallback)) => {
195 self.into_stream(RequestConsumerWithFallback::new(file, fallback))
197 }
198 (Some(core), Some(file), None) => {
199 self.into_stream(DualWriteRequestConsumer::new(core, file))
200 }
201 }
202 }
203
204 fn core_consumer(&self) -> Option<NominalCoreConsumer<BearerToken>> {
205 self.stream_to_core
206 .as_ref()
207 .map(|(auth_provider, dataset, handle)| {
208 NominalCoreConsumer::new(
209 NominalApiClients::from_uri(self.opts.base_api_url.as_str()),
210 handle.clone(),
211 auth_provider.clone(),
212 dataset.clone(),
213 )
214 })
215 }
216
217 fn file_consumer(&self) -> Option<AvroFileConsumer> {
218 self.stream_to_file
219 .as_ref()
220 .map(|path| AvroFileConsumer::new_with_full_path(path).unwrap())
221 }
222
223 fn fallback_consumer(&self) -> Option<AvroFileConsumer> {
224 self.file_fallback
225 .as_ref()
226 .map(|path| AvroFileConsumer::new_with_full_path(path).unwrap())
227 }
228
229 fn into_stream<C: WriteRequestConsumer + 'static>(self, consumer: C) -> NominalDatasetStream {
230 let mut listeners = self.listeners;
231 listeners.push(Arc::new(LoggingListener));
232 let listening_consumer = ListeningWriteRequestConsumer::new(consumer, listeners);
233 NominalDatasetStream::new_with_consumer(listening_consumer, self.opts)
234 }
235}
236
237#[deprecated]
239pub type NominalDatasourceStream = NominalDatasetStream;
240
241pub struct NominalDatasetStream {
242 opts: NominalStreamOpts,
243 running: Arc<AtomicBool>,
244 unflushed_points: Arc<AtomicUsize>,
245 primary_buffer: Arc<SeriesBuffer>,
246 secondary_buffer: Arc<SeriesBuffer>,
247 primary_handle: thread::JoinHandle<()>,
248 secondary_handle: thread::JoinHandle<()>,
249}
250
251impl NominalDatasetStream {
252 pub fn builder() -> NominalDatasetStreamBuilder {
253 NominalDatasetStreamBuilder::new()
254 }
255
256 pub fn new_with_consumer<C: WriteRequestConsumer + 'static>(
257 consumer: C,
258 opts: NominalStreamOpts,
259 ) -> Self {
260 let primary_buffer = Arc::new(SeriesBuffer::new(opts.max_points_per_record));
261 let secondary_buffer = Arc::new(SeriesBuffer::new(opts.max_points_per_record));
262
263 let (request_tx, request_rx) =
264 crossbeam_channel::bounded::<(WriteRequestNominal, usize)>(opts.max_buffered_requests);
265
266 let running = Arc::new(AtomicBool::new(true));
267 let unflushed_points = Arc::new(AtomicUsize::new(0));
268
269 let primary_handle = thread::Builder::new()
270 .name("nmstream_primary".to_string())
271 .spawn({
272 let points_buffer = Arc::clone(&primary_buffer);
273 let running = running.clone();
274 let tx = request_tx.clone();
275 move || {
276 batch_processor(running, points_buffer, tx, opts.max_request_delay);
277 }
278 })
279 .unwrap();
280
281 let secondary_handle = thread::Builder::new()
282 .name("nmstream_secondary".to_string())
283 .spawn({
284 let secondary_buffer = Arc::clone(&secondary_buffer);
285 let running = running.clone();
286 move || {
287 batch_processor(
288 running,
289 secondary_buffer,
290 request_tx,
291 opts.max_request_delay,
292 );
293 }
294 })
295 .unwrap();
296
297 let consumer = Arc::new(consumer);
298
299 for i in 0..opts.request_dispatcher_tasks {
300 thread::Builder::new()
301 .name(format!("nmstream_dispatch_{i}"))
302 .spawn({
303 let running = Arc::clone(&running);
304 let unflushed_points = Arc::clone(&unflushed_points);
305 let rx = request_rx.clone();
306 let consumer = consumer.clone();
307 move || {
308 debug!("starting request dispatcher");
309 request_dispatcher(running, unflushed_points, rx, consumer);
310 }
311 })
312 .unwrap();
313 }
314
315 NominalDatasetStream {
316 opts,
317 running,
318 unflushed_points,
319 primary_buffer,
320 secondary_buffer,
321 primary_handle,
322 secondary_handle,
323 }
324 }
325
326 pub fn double_writer<'a>(
327 &'a self,
328 channel_descriptor: &'a ChannelDescriptor,
329 ) -> NominalDoubleWriter<'a> {
330 NominalDoubleWriter {
331 writer: NominalChannelWriter::new(self, channel_descriptor),
332 }
333 }
334
335 pub fn string_writer<'a>(
336 &'a self,
337 channel_descriptor: &'a ChannelDescriptor,
338 ) -> NominalStringWriter<'a> {
339 NominalStringWriter {
340 writer: NominalChannelWriter::new(self, channel_descriptor),
341 }
342 }
343
344 pub fn integer_writer<'a>(
345 &'a self,
346 channel_descriptor: &'a ChannelDescriptor,
347 ) -> NominalIntegerWriter<'a> {
348 NominalIntegerWriter {
349 writer: NominalChannelWriter::new(self, channel_descriptor),
350 }
351 }
352
353 pub fn enqueue(&self, channel_descriptor: &ChannelDescriptor, new_points: impl IntoPoints) {
354 let new_points = new_points.into_points();
355 let new_count = points_len(&new_points);
356
357 self.when_capacity(new_count, |mut sb| {
358 sb.extend(channel_descriptor, new_points)
359 });
360 }
361
362 fn when_capacity(&self, new_count: usize, callback: impl FnOnce(SeriesBufferGuard)) {
363 self.unflushed_points
364 .fetch_add(new_count, Ordering::Release);
365
366 if self.primary_buffer.has_capacity(new_count) {
367 debug!("adding {} points to primary buffer", new_count);
368 callback(self.primary_buffer.lock());
369 } else if self.secondary_buffer.has_capacity(new_count) {
370 self.primary_handle.thread().unpark();
372 debug!("adding {} points to secondary buffer", new_count);
373 callback(self.secondary_buffer.lock());
374 } else {
375 let buf = if self.primary_buffer < self.secondary_buffer {
376 info!("waiting for primary buffer to flush to append {new_count} points...");
377 self.primary_handle.thread().unpark();
378 &self.primary_buffer
379 } else {
380 info!("waiting for secondary buffer to flush to append {new_count} points...");
381 self.secondary_handle.thread().unpark();
382 &self.secondary_buffer
383 };
384
385 buf.on_notify(callback);
386 }
387 }
388}
389
390pub struct NominalChannelWriter<'ds, T>
391where
392 Vec<T>: IntoPoints,
393{
394 channel: &'ds ChannelDescriptor,
395 stream: &'ds NominalDatasetStream,
396 last_flushed_at: Instant,
397 unflushed: Vec<T>,
398}
399
400impl<T> NominalChannelWriter<'_, T>
401where
402 Vec<T>: IntoPoints,
403{
404 fn new<'ds>(
405 stream: &'ds NominalDatasetStream,
406 channel: &'ds ChannelDescriptor,
407 ) -> NominalChannelWriter<'ds, T> {
408 NominalChannelWriter {
409 channel,
410 stream,
411 last_flushed_at: Instant::now(),
412 unflushed: vec![],
413 }
414 }
415
416 fn push_point(&mut self, point: T) {
417 self.unflushed.push(point);
418 if self.unflushed.len() >= self.stream.opts.max_points_per_record
419 || self.last_flushed_at.elapsed() > self.stream.opts.max_request_delay
420 {
421 debug!(
422 "conditionally flushing {:?}, ({} points, {:?} since last)",
423 self.channel,
424 self.unflushed.len(),
425 self.last_flushed_at.elapsed()
426 );
427 self.flush();
428 }
429 }
430
431 fn flush(&mut self) {
432 if self.unflushed.is_empty() {
433 return;
434 }
435 info!(
436 "flushing writer for {:?} with {} points",
437 self.channel,
438 self.unflushed.len()
439 );
440 self.stream.when_capacity(self.unflushed.len(), |mut buf| {
441 let to_flush: Vec<T> = self.unflushed.drain(..).collect();
442 buf.extend(self.channel, to_flush);
443 self.last_flushed_at = Instant::now();
444 })
445 }
446}
447
448impl<T> Drop for NominalChannelWriter<'_, T>
449where
450 Vec<T>: IntoPoints,
451{
452 fn drop(&mut self) {
453 info!("flushing then dropping writer for: {:?}", self.channel);
454 self.flush();
455 }
456}
457
458pub struct NominalDoubleWriter<'ds> {
459 writer: NominalChannelWriter<'ds, DoublePoint>,
460}
461
462impl NominalDoubleWriter<'_> {
463 pub fn push(&mut self, timestamp: impl IntoTimestamp, value: f64) {
464 self.writer.push_point(DoublePoint {
465 timestamp: Some(timestamp.into_timestamp()),
466 value,
467 });
468 }
469}
470
471pub struct NominalIntegerWriter<'ds> {
472 writer: NominalChannelWriter<'ds, IntegerPoint>,
473}
474
475impl NominalIntegerWriter<'_> {
476 pub fn push(&mut self, timestamp: impl IntoTimestamp, value: i64) {
477 self.writer.push_point(IntegerPoint {
478 timestamp: Some(timestamp.into_timestamp()),
479 value,
480 });
481 }
482}
483
484pub struct NominalStringWriter<'ds> {
485 writer: NominalChannelWriter<'ds, StringPoint>,
486}
487
488impl NominalStringWriter<'_> {
489 pub fn push(&mut self, timestamp: impl IntoTimestamp, value: impl Into<String>) {
490 self.writer.push_point(StringPoint {
491 timestamp: Some(timestamp.into_timestamp()),
492 value: value.into(),
493 });
494 }
495}
496
497struct SeriesBuffer {
498 points: Mutex<HashMap<ChannelDescriptor, PointsType>>,
499 count: AtomicUsize,
500 flush_time: AtomicU64,
501 condvar: Condvar,
502 max_capacity: usize,
503}
504
505struct SeriesBufferGuard<'sb> {
506 sb: MutexGuard<'sb, HashMap<ChannelDescriptor, PointsType>>,
507 count: &'sb AtomicUsize,
508}
509
510impl SeriesBufferGuard<'_> {
511 fn extend(&mut self, channel_descriptor: &ChannelDescriptor, points: impl IntoPoints) {
512 let points = points.into_points();
513 let new_point_count = points_len(&points);
514
515 if !self.sb.contains_key(channel_descriptor) {
516 self.sb.insert(channel_descriptor.clone(), points);
517 } else {
518 match (self.sb.get_mut(channel_descriptor).unwrap(), points) {
519 (PointsType::DoublePoints(existing), PointsType::DoublePoints(new)) => {
520 existing.points.extend(new.points)
521 }
522 (PointsType::StringPoints(existing), PointsType::StringPoints(new)) => {
523 existing.points.extend(new.points)
524 }
525 (PointsType::IntegerPoints(existing), PointsType::IntegerPoints(new)) => {
526 existing.points.extend(new.points)
527 }
528 (
529 PointsType::ArrayPoints(ArrayPoints {
530 array_type: Some(ArrayType::DoubleArrayPoints(existing)),
531 }),
532 PointsType::ArrayPoints(ArrayPoints {
533 array_type: Some(ArrayType::DoubleArrayPoints(new)),
534 }),
535 ) => existing.points.extend(new.points),
536 (
537 PointsType::ArrayPoints(ArrayPoints {
538 array_type: Some(ArrayType::StringArrayPoints(existing)),
539 }),
540 PointsType::ArrayPoints(ArrayPoints {
541 array_type: Some(ArrayType::StringArrayPoints(new)),
542 }),
543 ) => existing.points.extend(new.points),
544 (
545 PointsType::ArrayPoints(ArrayPoints { array_type: None }),
546 PointsType::ArrayPoints(ArrayPoints { array_type: None }),
547 ) => {}
548 (
550 PointsType::DoublePoints(_),
551 PointsType::IntegerPoints(_)
552 | PointsType::StringPoints(_)
553 | PointsType::ArrayPoints(_),
554 )
555 | (
556 PointsType::StringPoints(_),
557 PointsType::DoublePoints(_)
558 | PointsType::IntegerPoints(_)
559 | PointsType::ArrayPoints(_),
560 )
561 | (
562 PointsType::IntegerPoints(_),
563 PointsType::DoublePoints(_)
564 | PointsType::StringPoints(_)
565 | PointsType::ArrayPoints(_),
566 )
567 | (
568 PointsType::ArrayPoints(_),
569 PointsType::DoublePoints(_)
570 | PointsType::StringPoints(_)
571 | PointsType::IntegerPoints(_),
572 )
573 | (
574 PointsType::ArrayPoints(ArrayPoints {
575 array_type: Some(_),
576 }),
577 PointsType::ArrayPoints(ArrayPoints { array_type: None }),
578 )
579 | (
580 PointsType::ArrayPoints(ArrayPoints { array_type: None }),
581 PointsType::ArrayPoints(ArrayPoints {
582 array_type: Some(_),
583 }),
584 )
585 | (
586 PointsType::ArrayPoints(ArrayPoints {
587 array_type: Some(ArrayType::DoubleArrayPoints(_)),
588 }),
589 PointsType::ArrayPoints(ArrayPoints {
590 array_type: Some(ArrayType::StringArrayPoints(_)),
591 }),
592 )
593 | (
594 PointsType::ArrayPoints(ArrayPoints {
595 array_type: Some(ArrayType::StringArrayPoints(_)),
596 }),
597 PointsType::ArrayPoints(ArrayPoints {
598 array_type: Some(ArrayType::DoubleArrayPoints(_)),
599 }),
600 ) => {
601 panic!("mismatched types");
603 }
604 }
605 }
606
607 self.count.fetch_add(new_point_count, Ordering::Release);
608 }
609}
610
611impl PartialEq for SeriesBuffer {
612 fn eq(&self, other: &Self) -> bool {
613 self.flush_time.load(Ordering::Acquire) == other.flush_time.load(Ordering::Acquire)
614 }
615}
616
617impl PartialOrd for SeriesBuffer {
618 fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
619 let flush_time = self.flush_time.load(Ordering::Acquire);
620 let other_flush_time = other.flush_time.load(Ordering::Acquire);
621 flush_time.partial_cmp(&other_flush_time)
622 }
623}
624
625impl SeriesBuffer {
626 fn new(capacity: usize) -> Self {
627 Self {
628 points: Mutex::new(HashMap::new()),
629 count: AtomicUsize::new(0),
630 flush_time: AtomicU64::new(0),
631 condvar: Condvar::new(),
632 max_capacity: capacity,
633 }
634 }
635
636 fn has_capacity(&self, new_points_count: usize) -> bool {
641 let count = self.count.load(Ordering::Acquire);
642 count == 0 || count + new_points_count <= self.max_capacity
643 }
644
645 fn lock(&self) -> SeriesBufferGuard<'_> {
646 SeriesBufferGuard {
647 sb: self.points.lock(),
648 count: &self.count,
649 }
650 }
651
652 fn take(&self) -> (usize, Vec<Series>) {
653 let mut points = self.lock();
654 self.flush_time.store(
655 UNIX_EPOCH.elapsed().unwrap().as_nanos() as u64,
656 Ordering::Release,
657 );
658 let result = points
659 .sb
660 .drain()
661 .map(|(ChannelDescriptor { name, tags }, points)| {
662 let channel = Channel { name };
663 let points_obj = Points {
664 points_type: Some(points),
665 };
666 Series {
667 channel: Some(channel),
668 tags: tags
669 .map(|tags| tags.into_iter().collect())
670 .unwrap_or_default(),
671 points: Some(points_obj),
672 }
673 })
674 .collect();
675 let result_count = self
676 .count
677 .fetch_update(Ordering::Release, Ordering::Acquire, |_| Some(0))
678 .unwrap();
679 (result_count, result)
680 }
681
682 fn is_empty(&self) -> bool {
683 self.count() == 0
684 }
685
686 fn count(&self) -> usize {
687 self.count.load(Ordering::Acquire)
688 }
689
690 fn on_notify(&self, on_notify: impl FnOnce(SeriesBufferGuard)) {
691 let mut points_lock = self.points.lock();
692 if !points_lock.is_empty() {
695 self.condvar.wait(&mut points_lock);
696 } else {
697 debug!("buffer emptied since last check, skipping condvar wait");
698 }
699 on_notify(SeriesBufferGuard {
700 sb: points_lock,
701 count: &self.count,
702 });
703 }
704
705 fn notify(&self) -> bool {
706 self.condvar.notify_one()
707 }
708}
709
710fn batch_processor(
711 running: Arc<AtomicBool>,
712 points_buffer: Arc<SeriesBuffer>,
713 request_chan: crossbeam_channel::Sender<(WriteRequestNominal, usize)>,
714 max_request_delay: Duration,
715) {
716 loop {
717 debug!("starting processor loop");
718 if points_buffer.is_empty() {
719 if !running.load(Ordering::Acquire) {
720 debug!("batch processor thread exiting due to running flag");
721 drop(request_chan);
722 break;
723 } else {
724 debug!("empty points buffer, waiting");
725 thread::park_timeout(max_request_delay);
726 }
727 continue;
728 }
729 let (point_count, series) = points_buffer.take();
730
731 if points_buffer.notify() {
732 debug!("notified one waiting thread after clearing points buffer");
733 }
734
735 let write_request = WriteRequestNominal { series };
736
737 if request_chan.is_full() {
738 debug!("ready to queue request but request channel is full");
739 }
740 let rep = request_chan.send((write_request, point_count));
741 debug!("queued request for processing");
742 if rep.is_err() {
743 error!("failed to send request to dispatcher");
744 } else {
745 debug!("finished submitting request");
746 }
747
748 thread::park_timeout(max_request_delay);
749 }
750 debug!("batch processor thread exiting");
751}
752
753impl Drop for NominalDatasetStream {
754 fn drop(&mut self) {
755 debug!("starting drop for NominalDatasetStream");
756 self.running.store(false, Ordering::Release);
757 while self.unflushed_points.load(Ordering::Acquire) > 0 {
758 debug!(
759 "waiting for all points to be flushed before dropping stream, {} points remaining",
760 self.unflushed_points.load(Ordering::Acquire)
761 );
762 thread::sleep(Duration::from_millis(50));
764 }
765 }
766}
767
768fn request_dispatcher<C: WriteRequestConsumer + 'static>(
769 running: Arc<AtomicBool>,
770 unflushed_points: Arc<AtomicUsize>,
771 request_rx: crossbeam_channel::Receiver<(WriteRequestNominal, usize)>,
772 consumer: Arc<C>,
773) {
774 let mut total_request_time = 0;
775 loop {
776 match request_rx.recv() {
777 Ok((request, point_count)) => {
778 debug!("received writerequest from channel");
779 let req_start = Instant::now();
780 match consumer.consume(&request) {
781 Ok(_) => {
782 let time = req_start.elapsed().as_millis();
783 debug!("request of {} points sent in {} ms", point_count, time);
784 total_request_time += time as u64;
785 }
786 Err(e) => {
787 error!("Failed to send request: {e:?}");
788 }
789 }
790 unflushed_points.fetch_sub(point_count, Ordering::Release);
791
792 if unflushed_points.load(Ordering::Acquire) == 0 && !running.load(Ordering::Acquire)
793 {
794 info!("all points flushed, closing dispatcher thread");
795 drop(request_rx);
797 break;
798 }
799 }
800 Err(e) => {
801 debug!("request channel closed, exiting dispatcher thread. info: '{e}'");
802 break;
803 }
804 }
805 }
806 debug!(
807 "request dispatcher thread exiting. total request time: {}",
808 total_request_time
809 );
810}
811
812fn points_len(points_type: &PointsType) -> usize {
813 match points_type {
814 PointsType::DoublePoints(points) => points.points.len(),
815 PointsType::StringPoints(points) => points.points.len(),
816 PointsType::IntegerPoints(points) => points.points.len(),
817 PointsType::ArrayPoints(points) => match &points.array_type {
819 Some(ArrayType::DoubleArrayPoints(points)) => points.points.len(),
820 Some(ArrayType::StringArrayPoints(points)) => points.points.len(),
821 None => 0,
822 },
823 }
824}