shepherd-rs 0.2.0

Shepherd is a resilient, non-blocking orchestrator that persistently transforms and delivers data—built for remote, compute-heavy workloads.
Documentation
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
//! # Simple Processor
//!
//! This module provides a simple implementation of the `Processor` trait.
//!
//! ## Overview
//! - **SimpleProcessor**: Processes transformation and consumption attempts.
//! - **Error Handling**: Defines custom error types for processing operations.
//!
//! ## Example
//! ```rust
//! let processor = SimpleProcessor::new();
//! processor.processor_loop();
//! ```

use std::fmt::Debug;
use std::sync::Arc;

use async_trait::async_trait;
use thiserror::Error;
use tokio::sync::{Mutex, mpsc};

use crate::config::Config;
use crate::consumer::consumer::ConsumeAttemptResult;
use crate::consumer::{ConsumeAttempt, ConsumeAttemptCreator};
use crate::database::Database;
use crate::emitter::emitter::EmissionState;
use crate::processor::Processor;
use crate::processor::processor::ProcessorHandles;
use crate::transform::{TransformAttempt, TransformAttemptCreator, TransformRequest};
use crate::worker::worker_manager::WorkerManagerResult;

pub struct SimpleProcessor<CFG: Config, TR, TA: TransformAttempt, TAC, CA: ConsumeAttempt, CAC, DB>
{
    emitter_output_recv: mpsc::Receiver<TR>,
    _emitter_hints: mpsc::Sender<EmissionState>,
    _emitter_state: EmissionState,

    current_in_process_transform_attempts: u32,
    worker_manager_input_sender: mpsc::Sender<TA>,
    worker_manager_output_recv: mpsc::Receiver<WorkerManagerResult<TA>>,

    consumer_input_sender: mpsc::Sender<CA>,
    consumer_output_recv: mpsc::Receiver<ConsumeAttemptResult<CA>>,

    kill_signal_receiver: mpsc::Receiver<()>,

    database: DB,

    transform_attempt_creator: TAC,
    consume_attempt_creator: CAC,

    _max_in_process_transform_attempts: u32,
    _config: Arc<Mutex<CFG>>,
}

#[derive(Error, Debug)]
pub enum ProcessorError {
    #[error("max in-process transform attempts reached")]
    MaxInProcessTransformAttemptsReached,

    #[error("failed creating transform attempt: {0}")]
    TransformAttemptCreationFailed(String),

    #[error("failed sending to worker manager input channel")]
    WorkerManagerInputSendFailed(String),

    #[error("failed creating consumption attempt: {0}")]
    ConsumeAttemptCreationFailed(String),

    #[error("failed sending to consumer input channel")]
    ConsumerInputSendFailed(String),

    #[error("database error: {0}")]
    DatabaseError(String),

    #[error("unknown error occurred")]
    Unknown,
}

#[async_trait]
impl<CFG, TR, TA, TAC, CA, CAC, DB> Processor for SimpleProcessor<CFG, TR, TA, TAC, CA, CAC, DB>
where
    CFG: Config<KeyType = String, ValueType = Vec<u8>> + Send + Sync + 'static,
    TR: TransformRequest,
    TA: TransformAttempt<
            TransformRequestIdentifier = TR::Identifier,
            CallArgsType = TR::Input,
            ReturnType = TR::Output,
        >,
    TAC: TransformAttemptCreator<
            TransformRequest = TR,
            TransformAttempt = TA,
            Input = TR::Input,
            Output = TR::Output,
        >,
    CA: ConsumeAttempt<
            TransformRequestIdentifier = TR::Identifier,
            TransformAttemptIdentifier = TA::Identifier,
            ConsumeVal = TR::Output,
        >,
    CAC: ConsumeAttemptCreator<TransformAttempt = TA, ConsumeAttempt = CA, Output = TR::Output>,
    DB: Database<
            TransformRequest = TR,
            TransformAttempt = TA,
            ConsumeAttempt = CA,
            Input = TR::Input,
            Output = TR::Output,
        >,
{
    type Config = CFG;
    type ConsumeAttempt = CA;
    type ConsumeAttemptCreator = CAC;
    type Database = DB;
    type Input = TR::Input;
    type Output = TR::Output;
    type ProcessorError = ProcessorError;
    type TransformAttempt = TA;
    type TransformAttemptCreator = TAC;
    type TransformRequest = TR;

    async fn new(
        init_config: Arc<Mutex<CFG>>,
        database: Self::Database,
        transform_attempt_creator: Self::TransformAttemptCreator,
        consume_attempt_creator: Self::ConsumeAttemptCreator,
    ) -> (Self, ProcessorHandles<TR, TA, CA>) {
        let init_config_mutex_guard = init_config.lock().await;
        let transform_request_channel_size = init_config_mutex_guard
            .get("processor.transform_request_channel_size".to_string())
            .await
            .expect("Failed to get transform request channel size");

        let size: toml::Value =
            serde_json::from_slice(&transform_request_channel_size).expect("Failed to parse size");

        let transform_request_channel_size =
            size.as_integer().expect("Failed to parse channel size") as usize;

        let transform_attempt_channel_size = init_config_mutex_guard
            .get("processor.transform_attempt_channel_size".to_string())
            .await
            .expect("Failed to get transform attempt channel size");

        let size: toml::Value = serde_json::from_slice(&transform_attempt_channel_size)
            .expect("Failed to parse transform attempt channel size");

        let transform_attempt_channel_size =
            size.as_integer().expect("Failed to parse channel size") as usize;

        let consume_attempt_channel_size = init_config_mutex_guard
            .get("processor.consume_attempt_channel_size".to_string())
            .await
            .expect("Failed to get consume attempt channel size");

        let size: toml::Value = serde_json::from_slice(&consume_attempt_channel_size)
            .expect("failed to parse consume attempt channel size");

        let consume_attempt_channel_size =
            size.as_integer().expect("Failed to parse channel size") as usize;

        let max_in_process_transform_attempts = init_config_mutex_guard
            .get("processor.max_in_process_transform_attempts".to_string())
            .await
            .expect("Failed to get max_in_process_transform_attempts");

        let size: toml::Value = serde_json::from_slice(&max_in_process_transform_attempts)
            .expect("Failed to parse max in process transform attempts");

        let max_in_process_transform_attempts =
            size.as_integer().expect("Failed to parse channel size") as u32;

        drop(init_config_mutex_guard);

        let (emitter_output_sender, emitter_output_recv) =
            mpsc::channel(transform_request_channel_size);
        let (emitter_hints, emitter_hints_recv) = mpsc::channel(1);

        let (worker_manager_input_sender, worker_manager_input_recv) =
            mpsc::channel(transform_attempt_channel_size);
        let (worker_manager_output_sender, worker_manager_output_recv) =
            mpsc::channel(transform_attempt_channel_size);

        let (consumer_input_sender, consumer_input_recv) =
            mpsc::channel(consume_attempt_channel_size);
        let (consumer_output_sender, consumer_output_recv) =
            mpsc::channel(consume_attempt_channel_size);
        let (kill_signal_sender, kill_signal_receiver) = mpsc::channel(1);

        (
            Self {
                emitter_output_recv,
                _emitter_hints: emitter_hints,
                _emitter_state: EmissionState::Operational,
                current_in_process_transform_attempts: 0,
                worker_manager_input_sender,
                worker_manager_output_recv,
                consumer_input_sender,
                consumer_output_recv,
                kill_signal_receiver,
                database,
                transform_attempt_creator,
                consume_attempt_creator,
                _max_in_process_transform_attempts: max_in_process_transform_attempts,
                _config: init_config.clone(),
            },
            ProcessorHandles {
                emitter_output_sender,
                emitter_hints_recv,
                worker_manager_input_recv,
                worker_manager_output_sender,
                consumer_input_recv,
                consumer_output_sender,
                kill_signal_sender,
            },
        )
    }

    async fn processor_loop(&mut self) {
        loop {
            let _res = tokio::select! {
                Some(transform_request) = self.emitter_output_recv.recv() => {
                    self.process_emitter_output(transform_request).await
                },
                Some(worker_output) = self.worker_manager_output_recv.recv() => {
                    self.process_worker_output(worker_output).await
                },
                Some(consume_output) = self.consumer_output_recv.recv() => {
                    self.process_consumer_output(consume_output).await
                },
                Some(_) = self.kill_signal_receiver.recv() => {
                    // Handle the kill signal to stop processing
                    return;
                },
            };
        }
    }
}

impl<CFG, TR, TA, TAC, CA, CAC, DB> SimpleProcessor<CFG, TR, TA, TAC, CA, CAC, DB>
where
    CFG: Config<KeyType = String, ValueType = Vec<u8>> + Send + Sync + 'static,
    TR: TransformRequest,
    TA: TransformAttempt<
            TransformRequestIdentifier = TR::Identifier,
            CallArgsType = TR::Input,
            ReturnType = TR::Output,
        >,
    TAC: TransformAttemptCreator<
            TransformRequest = TR,
            TransformAttempt = TA,
            Input = TR::Input,
            Output = TR::Output,
        >,
    CA: ConsumeAttempt<
            TransformRequestIdentifier = TR::Identifier,
            TransformAttemptIdentifier = TA::Identifier,
            ConsumeVal = TR::Output,
        >,
    CAC: ConsumeAttemptCreator<TransformAttempt = TA, ConsumeAttempt = CA, Output = TR::Output>,
    DB: Database<
            TransformRequest = TR,
            TransformAttempt = TA,
            ConsumeAttempt = CA,
            Input = TR::Input,
            Output = TR::Output,
        >,
{
    async fn process_emitter_output(
        &mut self,
        transform_request: TR,
    ) -> Result<(), <Self as Processor>::ProcessorError> {
        self.database
            .register_transform_request(&transform_request)
            .await
            .map_err(|e| ProcessorError::DatabaseError(e.to_string()))?;

        // // Check if the emitter is operational
        // // If the emitter is operational, but the current in-process attempts
        // // have reached the maximum allowed, we should halt the emitter.
        // // If the emitter is already halted, we should not process any more
        // // requests
        // if self.current_in_process_transform_attempts >=
        // self.max_in_process_transform_attempts {     if self.emitter_state !=
        // EmissionState::Halt {         self.emitter_hints.
        // send(EmissionState::Halt).await.unwrap();         self.emitter_state
        // = EmissionState::Halt;     }
        //     // TODO: Send to data layer regardless, add to backlog or similar
        //     return Err(ProcessorError::MaxInProcessTransformAttemptsReached);
        // }

        // In case we still have capacity for transform attempts,
        // create an attempt and send it to the worker
        let new_attempt = self
            .transform_attempt_creator
            .create_new_attempt(&transform_request)
            .await
            .map_err(|e| ProcessorError::TransformAttemptCreationFailed(e.to_string()))?;

        self.current_in_process_transform_attempts =
            self.current_in_process_transform_attempts.saturating_add(1);

        self.database
            .register_transform_attempt(&new_attempt)
            .await
            .map_err(|e| ProcessorError::DatabaseError(e.to_string()))?;

        self.worker_manager_input_sender
            .send(new_attempt)
            .await
            .map_err(|e| ProcessorError::WorkerManagerInputSendFailed(e.to_string()))?;

        Ok(())
    }

    async fn process_worker_output(
        &mut self,
        worker_output: WorkerManagerResult<TA>,
    ) -> Result<(), <Self as Processor>::ProcessorError> {
        self.database
            .update_transform_attempt(&worker_output)
            .await
            .map_err(|e| ProcessorError::DatabaseError(e.to_string()))?;

        match worker_output {
            WorkerManagerResult::Success(transform_attempt_identifier, return_package) => {
                let consume_attempt = self
                    .consume_attempt_creator
                    .create_new_attempt(&TA::from_return_package(
                        transform_attempt_identifier.clone(),
                        return_package,
                    ))
                    .await;

                if let Err(e) = consume_attempt {
                    log::error!(
                        "Failed to create consume attempt for transform attempt {:?}: {}",
                        transform_attempt_identifier,
                        e
                    );
                    // Gracefully handle the error, archive since we can't do anything else
                    self.database
                        .archive_request_with_id(&transform_attempt_identifier.into())
                        .await
                        .map_err(|e| ProcessorError::DatabaseError(e.to_string()))?;
                    return Ok(());
                }

                let consume_attempt = consume_attempt.unwrap();

                self.database
                    .register_consume_attempt(&consume_attempt)
                    .await
                    .map_err(|e| ProcessorError::DatabaseError(e.to_string()))?;

                self.consumer_input_sender
                    .send(consume_attempt)
                    .await
                    .map_err(|e| ProcessorError::ConsumerInputSendFailed(e.to_string()))?;
            }
            WorkerManagerResult::Failure(transform_attempt_identifier, return_package) => {
                let reattempt = self
                    .transform_attempt_creator
                    .create_new_reattempt(transform_attempt_identifier.clone(), return_package)
                    .await;

                if let Err(e) = reattempt {
                    log::error!(
                        "Failed to create reattempt for transform attempt {:?}: {}",
                        transform_attempt_identifier,
                        e
                    );
                    // Gracefully handle the error, archive since we can't do anything else

                    self.database
                        .archive_request_with_id(&transform_attempt_identifier.into())
                        .await
                        .map_err(|e| ProcessorError::DatabaseError(e.to_string()))?;

                    return Ok(());
                }

                let reattempt = reattempt.unwrap();

                self.database
                    .register_transform_attempt(&reattempt)
                    .await
                    .map_err(|e| ProcessorError::DatabaseError(e.to_string()))?;

                self.worker_manager_input_sender
                    .send(reattempt)
                    .await
                    .map_err(|e| ProcessorError::WorkerManagerInputSendFailed(e.to_string()))?;
            }
        }
        Ok(())
    }

    async fn process_consumer_output(
        &mut self,
        consume_output: ConsumeAttemptResult<CA>,
    ) -> Result<(), <Self as Processor>::ProcessorError> {
        self.database
            .update_consume_attempt(consume_output.clone())
            .await
            .map_err(|e| ProcessorError::DatabaseError(e.to_string()))?;

        match consume_output {
            ConsumeAttemptResult::Success(consume_id, _return_ctx) => {
                // just archive the request
                self.database
                    .archive_request_with_id(&consume_id.into())
                    .await
                    .map_err(|e| ProcessorError::DatabaseError(e.to_string()))?;
            }
            ConsumeAttemptResult::Failure(consume_id, return_ctx) => {
                let reattempt = self
                    .consume_attempt_creator
                    .create_new_reattempt(consume_id.clone().into(), return_ctx)
                    .await;

                if let Err(e) = reattempt {
                    log::error!(
                        "Failed to create reattempt for consume attempt {:?}: {}",
                        consume_id,
                        e
                    );
                    // Gracefully handle the error, archive since we can't do anything else
                    self.database
                        .archive_request_with_id(&consume_id.into())
                        .await
                        .map_err(|e| ProcessorError::DatabaseError(e.to_string()))?;

                    return Ok(());
                }

                let reattempt = reattempt.unwrap();

                self.database
                    .register_consume_attempt(&reattempt)
                    .await
                    .map_err(|e| ProcessorError::DatabaseError(e.to_string()))?;

                self.consumer_input_sender
                    .send(reattempt)
                    .await
                    .map_err(|e| ProcessorError::ConsumerInputSendFailed(e.to_string()))?;
            }
        }
        Ok(())
    }
}