1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
//! See [super]

use super::super::{
    stream_executor::StreamExecutor,
    mutiny_stream::MutinyStream,
    types::{FullDuplexMultiChannel},
};
use std::{
    sync::Arc,
    fmt::Debug,
    time::Duration,
    future::Future,
    marker::PhantomData,
};
use indexmap::IndexMap;
use futures::{Stream};
use tokio::{
    sync::{RwLock},
};


/// `Multi` is an event handler capable of having several "listeners" -- all of which receives all events.\
/// With this struct, it is possible to:
///   - produce events
///   - spawn new `Stream`s & executors
///   - close `Stream`s (and executors)
/// Example:
/// ```nocompile
/// {reactive_mutiny::Instruments::MetricsWithoutLogs.into()}
pub struct Multi<ItemType:          Debug + Sync + Send + 'static,
                 MultiChannelType:  FullDuplexMultiChannel<'static, ItemType, DerivedItemType> + Sync + Send,
                 const INSTRUMENTS: usize,
                 DerivedItemType:   Debug + Sync + Send + 'static> {
    pub multi_name:     String,
    pub channel:        Arc<MultiChannelType>,
    pub executor_infos: RwLock<IndexMap<String, ExecutorInfo<INSTRUMENTS>>>,
        _phantom:       PhantomData<(ItemType, MultiChannelType, DerivedItemType)>,
}

impl<ItemType:          Debug + Send + Sync + 'static,
     MultiChannelType:  FullDuplexMultiChannel<'static, ItemType, DerivedItemType> + Sync + Send + 'static,
     const INSTRUMENTS: usize,
     DerivedItemType:   Debug + Sync + Send + 'static>
Multi<ItemType, MultiChannelType, INSTRUMENTS, DerivedItemType> {

    pub fn new<IntoString: Into<String>>(multi_name: IntoString) -> Self {
        let multi_name = multi_name.into();
        Multi {
            multi_name:     multi_name.clone(),
            channel:        MultiChannelType::new(multi_name.clone()),
            executor_infos: RwLock::new(IndexMap::new()),
            _phantom:       PhantomData::default(),
        }
    }

    pub fn stream_name(self: &Self) -> &str {
        &self.multi_name
    }

    #[inline(always)]
    #[must_use]
    pub fn try_send<F: FnOnce(&mut ItemType)>(&self, setter: F) -> bool {
        self.channel.try_send(setter)
    }

    #[inline(always)]
    pub fn send<F: FnOnce(&mut ItemType)>(&self, setter: F) {
        self.channel.send(setter);
    }

    #[inline(always)]
    pub fn send_derived(&self, arc_item: &DerivedItemType) {
        self.channel.send_derived(arc_item);
    }

    #[inline(always)]
    #[must_use]
    pub fn try_send_movable(&self, item: ItemType) -> bool {
        self.channel.try_send_movable(item)
    }

    #[inline(always)]
    pub fn buffer_size(&self) -> u32 {
        self.channel.buffer_size()
    }

    #[inline(always)]
    pub fn pending_items_count(&self) -> u32 {
        self.channel.pending_items_count()
    }

    /// Spawns a new listener of all subsequent events sent to this `Multi`, processing them through the `Stream` returned by `pipeline_builder()`,
    /// which generates events that are Futures & Fallible.
    #[must_use]
    pub async fn spawn_executor<IntoString:             Into<String>,
                                OutItemType:            Send + Debug,
                                OutStreamType:          Stream<Item=OutType> + Send + 'static,
                                OutType:                Future<Output=Result<OutItemType, Box<dyn std::error::Error + Send + Sync>>> + Send,
                                ErrVoidAsyncType:       Future<Output=()> + Send + 'static,
                                CloseVoidAsyncType:     Future<Output=()> + Send + 'static>

                               (&self,
                                concurrency_limit:         u32,
                                futures_timeout:           Duration,
                                pipeline_name:             IntoString,
                                pipeline_builder:          impl FnOnce(MutinyStream<'static, ItemType, MultiChannelType, DerivedItemType>) -> OutStreamType,
                                on_err_callback:           impl Fn(Box<dyn std::error::Error + Send + Sync>)                               -> ErrVoidAsyncType   + Send + Sync + 'static,
                                on_close_callback:         impl FnOnce(Arc<StreamExecutor<INSTRUMENTS>>)                                   -> CloseVoidAsyncType + Send + Sync + 'static)

                               -> Result<(), Box<dyn std::error::Error>> {

        let (in_stream, in_stream_id) = self.channel.create_stream_for_new_events();
        let out_stream = pipeline_builder(in_stream);
        self.spawn_executor_from_stream(concurrency_limit, futures_timeout, pipeline_name, in_stream_id, out_stream, on_err_callback, on_close_callback).await
    }

    /// For channels that allow it (like [channels::reference::mmap_log::MmapLog]), spawns two listeners for events sent to this `Multi`:
    ///   1) One for past events -- to be processed by the stream returned by `oldies_pipeline_builder()`;
    ///   2) Another one for subsequent events -- to be processed by the stream returned by `newies_pipeline_builder()`.
    /// By using this method, it is assumed that both pipeline builders returns Futures & Fallible events. If this is not so, see [spawn_non_futures_non_fallible_oldies_executor].\
    /// The stream splitting is guaranteed not to drop any events and `sequential_transition` may be used to indicate if old events should be processed first or if both old and new events
    /// may be processed simultaneously (in an inevitable out-of-order fashion).
    #[must_use]
    pub async fn spawn_oldies_executor<IntoString:             Into<String>,
                                       OutItemType:            Send + Debug,
                                       OldiesOutStreamType:    Stream<Item=OutType> + Sync + Send + 'static,
                                       NewiesOutStreamType:    Stream<Item=OutType> + Sync + Send + 'static,
                                       OutType:                Future<Output=Result<OutItemType, Box<dyn std::error::Error + Send + Sync>>> + Send,
                                       ErrVoidAsyncType:       Future<Output=()> + Send + 'static,
                                       CloseVoidAsyncType:     Future<Output=()> + Send + 'static>

                                      (self:                      &Arc<Self>,
                                       concurrency_limit:         u32,
                                       sequential_transition:     bool,
                                       futures_timeout:           Duration,
                                       oldies_pipeline_name:      IntoString,
                                       oldies_pipeline_builder:   impl FnOnce(MutinyStream<'static, ItemType, MultiChannelType, DerivedItemType>) -> OldiesOutStreamType,
                                       oldies_on_close_callback:  impl FnOnce(Arc<StreamExecutor<INSTRUMENTS>>)                                   -> CloseVoidAsyncType + Send + Sync + 'static,
                                       newies_pipeline_name:      IntoString,
                                       newies_pipeline_builder:   impl FnOnce(MutinyStream<'static, ItemType, MultiChannelType, DerivedItemType>) -> NewiesOutStreamType + Send + Sync + 'static,
                                       newies_on_close_callback:  impl FnOnce(Arc<StreamExecutor<INSTRUMENTS>>)                                   -> CloseVoidAsyncType  + Send + Sync + 'static,
                                       on_err_callback:           impl Fn(Box<dyn std::error::Error + Send + Sync>)                               -> ErrVoidAsyncType + Send + Sync + 'static)

                                      -> Result<(), Box<dyn std::error::Error>> {

        let ((oldies_in_stream, oldies_in_stream_id),
             (newies_in_stream, newies_in_stream_id)) = self.channel.create_streams_for_old_and_new_events();

        let cloned_self = Arc::clone(&self);
        let oldies_pipeline_name = oldies_pipeline_name.into();
        let newies_pipeline_name = Arc::new(newies_pipeline_name.into());
        let on_err_callback_ref1 = Arc::new(on_err_callback);
        let on_err_callback_ref2 = Arc::clone(&on_err_callback_ref1);
        let oldies_out_stream = oldies_pipeline_builder(oldies_in_stream);
        let newies_out_stream = newies_pipeline_builder(newies_in_stream);

        match sequential_transition {
            true => {
                self.spawn_executor_from_stream(concurrency_limit, futures_timeout, oldies_pipeline_name, oldies_in_stream_id, oldies_out_stream,
                                                move |err| on_err_callback_ref1(err),
                                                move |executor| {
                                                    let cloned_self = Arc::clone(&cloned_self);
                                                    let on_err_callback_ref2 = Arc::clone(&on_err_callback_ref2);
                                                    let newies_pipeline_name = Arc::clone(&newies_pipeline_name);
                                                    async move {
                                                        cloned_self.spawn_executor_from_stream(concurrency_limit, futures_timeout, newies_pipeline_name.as_str(), newies_in_stream_id, newies_out_stream,
                                                                                              move |err| on_err_callback_ref2(err),
                                                                                              move |executor| newies_on_close_callback(executor)).await
                                                            .map_err(|err| format!("Multi::spawn_oldies_executor(): could not start `newies`/sequential executor: {:?}", err))
                                                            .expect("CANNOT SPAWN NEWIES EXECUTOR AFTER OLDIES HAD COMPLETE");
                                                        oldies_on_close_callback(executor).await;
                                                    }
                                                } ).await
                    .map_err(|err| format!("Multi::spawn_oldies_executor(): could not start `oldies`/sequential executor: {:?}", err))?;
            },
            false => {
                self.spawn_executor_from_stream(concurrency_limit, futures_timeout, oldies_pipeline_name, oldies_in_stream_id, oldies_out_stream,
                                                move |err| on_err_callback_ref1(err),
                                                move |executor| oldies_on_close_callback(executor)).await
                    .map_err(|err| format!("Multi::spawn_oldies_executor(): could not start `oldies` executor: {:?}", err))?;
                self.spawn_executor_from_stream(concurrency_limit, futures_timeout, newies_pipeline_name.as_str(), newies_in_stream_id, newies_out_stream,
                                                move |err| on_err_callback_ref2(err),
                                                move |executor| newies_on_close_callback(executor)).await
                    .map_err(|err| format!("Multi::spawn_oldies_executor(): could not start `newies` executor: {:?}", err))?;
            },
        }
        Ok(())
    }

    /// Internal method with common code for [spawn_executor()] & [spawn_oldies_executor()].
    #[must_use]
    async fn spawn_executor_from_stream<IntoString:             Into<String>,
                                        OutItemType:            Send + Debug,
                                        OutStreamType:          Stream<Item=OutType> + Send + 'static,
                                        OutType:                Future<Output=Result<OutItemType, Box<dyn std::error::Error + Send + Sync>>> + Send,
                                        ErrVoidAsyncType:       Future<Output=()> + Send + 'static,
                                        CloseVoidAsyncType:     Future<Output=()> + Send + 'static>

                                       (&self,
                                        concurrency_limit:         u32,
                                        futures_timeout:           Duration,
                                        pipeline_name:             IntoString,
                                        stream_id:                 u32,
                                        pipelined_stream:          OutStreamType,
                                        on_err_callback:           impl Fn(Box<dyn std::error::Error + Send + Sync>) -> ErrVoidAsyncType   + Send + Sync + 'static,
                                        on_close_callback:         impl FnOnce(Arc<StreamExecutor<INSTRUMENTS>>)         -> CloseVoidAsyncType + Send + Sync + 'static)

                                       -> Result<(), Box<dyn std::error::Error>> {

        let executor = StreamExecutor::with_futures_timeout(format!("{}: {}", self.stream_name(), pipeline_name.into()), futures_timeout);
        self.add_executor(Arc::clone(&executor), stream_id).await?;
        executor
            .spawn_executor(
                concurrency_limit,
                on_err_callback,
                move |executor| on_close_callback(executor),
                pipelined_stream
            );
        Ok(())
    }

    /// Spawns a new listener of all subsequent events sent to this `Multi`, processing them through the `Stream` returned by `pipeline_builder()`,
    /// which generates events that are Non-Futures & Non-Fallible.
    #[must_use]
    pub async fn spawn_non_futures_non_fallible_executor<IntoString:             Into<String>,
                                                         OutItemType:            Send + Debug,
                                                         OutStreamType:          Stream<Item=OutItemType> + Send + 'static,
                                                         CloseVoidAsyncType:     Future<Output=()> + Send + 'static>

                                                        (&self,
                                                         concurrency_limit:        u32,
                                                         pipeline_name:            IntoString,
                                                         pipeline_builder:         impl FnOnce(MutinyStream<'static, ItemType, MultiChannelType, DerivedItemType>) -> OutStreamType,
                                                         on_close_callback:        impl FnOnce(Arc<StreamExecutor<INSTRUMENTS>>)                                   -> CloseVoidAsyncType + Send + Sync + 'static)

                                                        -> Result<(), Box<dyn std::error::Error>> {

        let (in_stream, in_stream_id) = self.channel.create_stream_for_new_events();
        let out_stream = pipeline_builder(in_stream);
        self.spawn_non_futures_non_fallible_executor_from_stream(concurrency_limit, pipeline_name, in_stream_id, out_stream, on_close_callback).await
    }

    /// For channels that allow it (like [channels::reference::mmap_log::MmapLog]), spawns two listeners for events sent to this `Multi`:
    ///   1) One for past events -- to be processed by the stream returned by `oldies_pipeline_builder()`;
    ///   2) Another one for subsequent events -- to be processed by the stream returned by `newies_pipeline_builder()`.
    /// By using this method, it is assumed that both pipeline builders returns non-Futures & non-Fallible events. If this is not so, see [spawn_oldies_executor].\
    /// The stream splitting is guaranteed not to drop any events and `sequential_transition` may be used to indicate if old events should be processed first or if both old and new events
    /// may be processed simultaneously (in an inevitable out-of-order fashion).
    #[must_use]
    pub async fn spawn_non_futures_non_fallible_oldies_executor<IntoString:             Into<String>,
                                                                OutItemType:            Send + Debug,
                                                                OldiesOutStreamType:    Stream<Item=OutItemType> + Sync + Send + 'static,
                                                                NewiesOutStreamType:    Stream<Item=OutItemType> + Sync + Send + 'static,
                                                                CloseVoidAsyncType:     Future<Output=()> + Send + 'static>

                                                               (self:                     &Arc<Self>,
                                                                concurrency_limit:        u32,
                                                                sequential_transition:    bool,
                                                                oldies_pipeline_name:     IntoString,
                                                                oldies_pipeline_builder:  impl FnOnce(MutinyStream<'static, ItemType, MultiChannelType, DerivedItemType>) -> OldiesOutStreamType,
                                                                oldies_on_close_callback: impl FnOnce(Arc<StreamExecutor<INSTRUMENTS>>)                                   -> CloseVoidAsyncType + Send + Sync + 'static,
                                                                newies_pipeline_name:     IntoString,
                                                                newies_pipeline_builder:  impl FnOnce(MutinyStream<'static, ItemType, MultiChannelType, DerivedItemType>) -> NewiesOutStreamType,
                                                                newies_on_close_callback: impl FnOnce(Arc<StreamExecutor<INSTRUMENTS>>)                                   -> CloseVoidAsyncType + Send + Sync + 'static)

                                                               -> Result<(), Box<dyn std::error::Error>> {

        let ((oldies_in_stream, oldies_in_stream_id),
             (newies_in_stream, newies_in_stream_id)) = self.channel.create_streams_for_old_and_new_events();

        let cloned_self = Arc::clone(&self);
        let oldies_pipeline_name = oldies_pipeline_name.into();
        let newies_pipeline_name = Arc::new(newies_pipeline_name.into());
        let oldies_out_stream = oldies_pipeline_builder(oldies_in_stream);
        let newies_out_stream = newies_pipeline_builder(newies_in_stream);

        match sequential_transition {
            true => {
                self.spawn_non_futures_non_fallible_executor_from_stream(concurrency_limit, oldies_pipeline_name, oldies_in_stream_id, oldies_out_stream,
                                                                         move |executor| {
                                                                             let cloned_self = Arc::clone(&cloned_self);
                                                                             let newies_pipeline_name = Arc::clone(&newies_pipeline_name);
                                                                             async move {
                                                                                 cloned_self.spawn_non_futures_non_fallible_executor_from_stream(concurrency_limit, newies_pipeline_name.as_str(), newies_in_stream_id, newies_out_stream,
                                                                                                                                                move |executor| newies_on_close_callback(executor)).await
                                                                                     .map_err(|err| format!("Multi::spawn_non_futures_non_fallible_oldies_executor(): could not start `newies` executor: {:?}", err))
                                                                                     .expect("CANNOT SPAWN NEWIES EXECUTOR AFTER OLDIES HAD COMPLETE");
                                                                                 oldies_on_close_callback(executor).await;
                                                                             }
                                                                         }).await
                    .map_err(|err| format!("Multi::spawn_non_futures_non_fallible_oldies_executor(): could not start `oldies`/sequential executor: {:?}", err))?;

            },
            false => {
                self.spawn_non_futures_non_fallible_executor_from_stream(concurrency_limit, oldies_pipeline_name, oldies_in_stream_id, oldies_out_stream,
                                                          move |executor| oldies_on_close_callback(executor)).await
                    .map_err(|err| format!("Multi::spawn_non_futures_non_fallible_oldies_executor(): could not start `oldies` executor: {:?}", err))?;
                self.spawn_non_futures_non_fallible_executor_from_stream(concurrency_limit, newies_pipeline_name.as_str(), newies_in_stream_id, newies_out_stream,
                                                                         move |executor| newies_on_close_callback(executor)).await
                    .map_err(|err| format!("Multi::spawn_non_futures_non_fallible_oldies_executor(): could not start `newies` executor: {:?}", err))?;
            },
        }
        Ok(())
    }

    /// Internal method with common code for [spawn_non_futures_non_fallible_executor()] & [spawn_non_futures_non_fallible_oldies_executor()].
    #[must_use]
    async fn spawn_non_futures_non_fallible_executor_from_stream<IntoString:             Into<String>,
                                                                 OutItemType:            Send + Debug,
                                                                 OutStreamType:          Stream<Item=OutItemType> + Send + 'static,
                                                                 CloseVoidAsyncType:     Future<Output=()> + Send + 'static>

                                                                (&self,
                                                                 concurrency_limit:         u32,
                                                                 pipeline_name:             IntoString,
                                                                 stream_id:                 u32,
                                                                 pipelined_stream:          OutStreamType,
                                                                 on_close_callback:         impl FnOnce(Arc<StreamExecutor<INSTRUMENTS>>) -> CloseVoidAsyncType + Send + Sync + 'static)

                                                                -> Result<(), Box<dyn std::error::Error>> {

        let executor = StreamExecutor::new(format!("{}: {}", self.stream_name(), pipeline_name.into()));
        self.add_executor(Arc::clone(&executor), stream_id).await?;
        executor
            .spawn_non_futures_non_fallible_executor(
                concurrency_limit,
                move |executor| on_close_callback(executor),
                pipelined_stream
            );
        Ok(())
    }

    /// Closes this `Multi`, in isolation -- flushing pending events, closing the producers,
    /// waiting for all events to be fully processed and calling all executor's "on close" callbacks.\
    /// If this `Multi` share resources with another one (which will get dumped by the "on close"
    /// callback), most probably you want to close them atomically -- see [multis_close_async!()]
    pub async fn close(self: &Self, timeout: Duration) {
        self.channel.gracefully_end_all_streams(timeout).await;
    }

    /// Asynchronously blocks until all resources associated with the executor responsible for `pipeline_name` are freed:
    ///   1) immediately causes `pipeline_name` to cease receiving new elements by removing it from the active list
    ///   2) wait for all pending elements to be processed
    ///   3) remove the queue/channel and wake the Stream to see that it has ended
    ///   4) waits for the executor to inform it ceased its execution
    ///   5) return, dropping all resources\
    /// Note it might make sense to spawn this operation by a `Tokio task`, for it may block indefinitely if the Stream has no timeout.\
    /// Also note that timing out this operation is not advisable, for resources won't be freed until it reaches the last step.\
    /// Returns false if there was no executor associated with `pipeline_name`.
    pub async fn flush_and_cancel_executor<IntoString: Into<String>>
                                          (self:          &Self,
                                           pipeline_name: IntoString,
                                           timeout:       Duration) -> bool {

        let executor_name = format!("{}: {}", self.multi_name, pipeline_name.into());
        // remove the pipeline from the active list
        let mut executor_infos = self.executor_infos.write().await;
        let executor_info = match executor_infos.remove(&executor_name) {
            Some(executor) => executor,
            None => return false,
        };
        drop(executor_infos);

        // wait until all elements are taken out from the queue
        executor_info.stream_executor.report_scheduled_to_finish();
        self.channel.gracefully_end_stream(executor_info.stream_id, timeout).await;
        true
    }

    /// Registers an executor within this `Multi` so it can be managed -- closed, inquired for stats, etc
    async fn add_executor(&self, stream_executor: Arc<StreamExecutor<INSTRUMENTS>>, stream_id: u32) -> Result<(), Box<dyn std::error::Error>> {
        let mut internal_multis = self.executor_infos.write().await;
        if internal_multis.contains_key(&stream_executor.executor_name()) {
            Err(Box::from(format!("an executor with the same name is already present: '{}'", stream_executor.executor_name())))
        } else {
            internal_multis.insert(stream_executor.executor_name(), ExecutorInfo { stream_executor, stream_id });
            Ok(())
        }
    }

}

/// Macro to close, atomically-ish, all [Multi]s passed in as parameters
#[macro_export]
macro_rules! multis_close_async {
    ($timeout: expr,
     $($multi: expr),+) => {
        {
            tokio::join!( $( $multi.channel.flush($timeout), )+ );
            tokio::join!( $( $multi.channel.gracefully_end_all_streams($timeout), )+ );
        }
    }
}
pub use multis_close_async;
pub use crate::types::ChannelCommon;

/// Keeps track of the `stream_executor` associated to each `stream_id`
pub struct ExecutorInfo<const INSTRUMENTS: usize> {
    pub stream_executor: Arc<StreamExecutor<INSTRUMENTS>>,
    pub stream_id: u32,
}