historyprovider 2.4.1

historyprovider-rs
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
use std::collections::{HashMap, HashSet, VecDeque};
use std::path::{Path, PathBuf};
use std::pin::Pin;
use std::sync::Arc;

use futures::channel::mpsc::UnboundedReceiver;
use futures::channel::oneshot::Sender as OneshotSender;
use futures::io::BufReader;
use futures::stream::FuturesUnordered;
use futures::StreamExt;
use log::{debug, error, info, warn};
use shvclient::{ClientCommandSender, ClientEventsReceiver};
use shvproto::DateTime as ShvDateTime;
use shvrpc::metamethod::AccessLevel;
use tokio_util::compat::TokioAsyncReadCompatExt;

use crate::datachange::DataChange;
use crate::journalentry::JournalEntry;
use crate::journalrw::{JournalReaderLog2, JournalWriterLog2, VALUE_FLAG_SPONTANEOUS_BIT};
use crate::sites::ParsedNotification;
use crate::util::{get_files, is_log2_file};
use crate::State;


#[derive(Debug)]
pub(crate) enum DirtyLogCommand {
    ProcessNotification(ParsedNotification),
    Trim {
        site: String
    },
    Get {
        site: String,
        response_tx: OneshotSender<Vec<JournalEntry>>,
    }
}

pub(crate) async fn dirtylog_task(
    _client_cmd_tx: ClientCommandSender,
    _client_evt_rx: ClientEventsReceiver,
    app_state: Arc<State>,
    mut cmd_rx: UnboundedReceiver<DirtyLogCommand>,
) {
    // Per site request
    enum Request {
        Get(OneshotSender<Vec<JournalEntry>>),
        Append(JournalEntry),
        Trim,
    }

    async fn process_request(site: String, journal_dir: PathBuf, request: Request) {
        match request {
            Request::Get(response_sender) => {
                // Load the dirty log and return it in the response channel
                let dirty_log_path = journal_dir.join(site).join("dirtylog");
                let res = match tokio::fs::File::open(&dirty_log_path).await {
                    Ok(file) => {
                        let reader = JournalReaderLog2::new(BufReader::new(file.compat())).enumerate();
                        reader
                            .filter_map(|(entry_no, entry_res)| {
                                let entry = entry_res.inspect_err(|err|
                                    warn!("Invalid journal entry no. {entry_no} in dirty log at {log_path}: {err}",
                                        log_path = dirty_log_path.to_string_lossy()
                                    )
                                )
                                    .ok();
                                async { entry }
                            })
                        .collect::<Vec<_>>()
                            .await
                    }
                    Err(err) => {
                        if err.kind() != std::io::ErrorKind::NotFound {
                            error!("Cannot open {log_path}: {err}", log_path = dirty_log_path.to_string_lossy());
                        }
                        Vec::new()
                    }
                };
                response_sender.send(res).unwrap_or_default();
            }
            Request::Append(journal_entry) => {
                let journal_site_path = journal_dir.join(site);
                if !journal_site_path.exists() {
                    debug!("Ignoring notification while journal directory {journal_site_path} for the site does not exist",
                        journal_site_path = journal_site_path.to_string_lossy()
                    );
                    return;
                }
                // Append to the site's dirty log
                let dirty_log_path = journal_site_path.join("dirtylog");
                let dirty_log_file = match tokio::fs::OpenOptions::new()
                    .create(true)
                    .append(true)
                    .open(&dirty_log_path)
                    .await {
                        Ok(file) => file,
                        Err(err) => {
                            error!("Cannot append a notification to dirty log. Cannot open file {file_path} for write: {err}",
                                file_path = dirty_log_path.to_string_lossy()
                            );
                            return;
                        }
                    };
                let mut writer = JournalWriterLog2::new(dirty_log_file.compat());
                writer.append(&journal_entry)
                    .await
                    .unwrap_or_else(|err|
                        error!("Cannot append a notification to dirty log {log_file}: {err}",
                            log_file = dirty_log_path.to_string_lossy()
                        )
                    );
            }
            Request::Trim => {
                info!("Trim dirty log start, site: {site}");
                // Get the latest entry from the site log and remove all entries up to that
                // entry from the dirty log
                let dirty_log_path = journal_dir.join(&site).join("dirtylog");
                let dirty_log_file = match tokio::fs::File::open(&dirty_log_path).await {
                    Ok(file) => file,
                    Err(err) => {
                        if err.kind() == std::io::ErrorKind::NotFound {
                            info!("Trim dirty log done, no dirty log file for the site {site}");
                        } else {
                            error!("Cannot trim dirty log. Cannot open file {file_path}: {err}", file_path = dirty_log_path.to_string_lossy());
                        }
                        return;
                    }
                };

                let latest_entry = {
                    let mut log_files = match get_files(journal_dir.join(&site), is_log2_file).await {
                        Ok(files) => files,
                        Err(err) => {
                            error!("Cannot trim dirty log. Cannot read journal dir entries: {err}");
                            return;
                        }
                    };
                    log_files.sort_by_key(|entry| std::cmp::Reverse(entry.file_name()));

                    let latest_entry = Box::pin(futures::stream::iter(log_files)
                        .map(|file_entry| file_entry.path())
                        .then(|file_path|
                            async move {
                                match tokio::fs::File::open(&file_path).await {
                                    Ok(file) => {
                                        let reader = JournalReaderLog2::new(BufReader::new(file.compat()));
                                        reader.fold(None, async |_, entry| entry.ok()).await
                                    }
                                    Err(err) => {
                                        error!("Cannot open file {file_path} while getting the last journal entry for trim dirtylog: {err}",
                                            file_path = file_path.to_string_lossy()
                                        );
                                        None
                                    }
                        }
                            })
                        .filter_map(async |entry| entry))
                        .next()
                        .await;

                    match latest_entry {
                        Some(entry) => entry,
                        None => {
                            info!("Trim dirty log done, no journal entries in synced files");
                            return;
                        }
                    }
                };

                // Remove all entries older than the latest entry from the dirty log
                let reader = JournalReaderLog2::new(BufReader::new(dirty_log_file.compat()));
                let trimmed_log = reader.filter_map(|entry| {
                    let passed_entry = entry
                        .as_ref()
                        .ok()
                        .filter(|entry| entry.epoch_msec >= latest_entry.epoch_msec)
                        .cloned();
                    async move { passed_entry }
                })
                .collect::<Vec<_>>()
                    .await;

                // Write the dirty log
                let dirty_log_file = match tokio::fs::OpenOptions::new()
                    .write(true)
                    .truncate(true)
                    .open(&dirty_log_path)
                    .await {
                        Ok(file) => file,
                        Err(err) => {
                            error!("Cannot trim dirty log. Cannot open file {file_path} for write: {err}",
                                file_path = dirty_log_path.to_string_lossy()
                            );
                            return;
                        }
                    };
                let mut writer = JournalWriterLog2::new(dirty_log_file.compat());
                for entry in &trimmed_log {
                    writer.append(entry)
                        .await
                        .unwrap_or_else(|err|
                            error!("Cannot write a journal entry to dirty log {log_file}: {err}",
                                log_file = dirty_log_path.to_string_lossy()
                            )
                        );
                }
                info!("Trim dirty log done, site: {site}");

            }
        }
    }

    // Schedules requests per site in order, across sites concurrently
    struct RequestScheduler {
        per_site: HashMap<String, VecDeque<Request>>,
        running: HashSet<String>,
        // Global pool of running site tasks (max 1 per site)
        inflight: FuturesUnordered<Pin<Box<dyn Future<Output = String> + Send>>>,
        journal_dir: PathBuf,
    }

    impl RequestScheduler {
        fn new(journal_dir: PathBuf) -> Self {
            Self {
                per_site: Default::default(),
                running: Default::default(),
                inflight: Default::default(),
                journal_dir,
            }
        }

        fn _schedule_next(&mut self, site: String) {
            if self.running.contains(&site) {
                return;
            }
            if let Some(queue) = self.per_site.get_mut(&site)
                && let Some(request) = queue.pop_front() {
                    self.running.insert(site.clone());
                    let journal_dir = self.journal_dir.clone();
                    self.inflight.push(Box::pin(async move {
                        process_request(site.clone(), journal_dir, request).await;
                        site
                    }));
            }
        }

        fn schedule_new(&mut self, site: String, request: Request) {
            self.per_site.entry(site.clone()).or_default().push_back(request);
            self._schedule_next(site);
        }

        fn on_finished(&mut self, site: String) {
            self.running.remove(&site);
            self._schedule_next(site);
        }
    }

    let mut request_scheduler = RequestScheduler::new(Path::new(&app_state.config.journal_dir).into());

    loop {
        futures::select! {
            command = cmd_rx.select_next_some() => match command {
                DirtyLogCommand::Trim { site } => {
                    request_scheduler.schedule_new(site, Request::Trim);
                }
                DirtyLogCommand::Get { site, response_tx } => {
                    request_scheduler.schedule_new(site, Request::Get(response_tx));
                }
                DirtyLogCommand::ProcessNotification(ParsedNotification { site_path, property_path, signal, param }) => {
                    let data_change = DataChange::from(param);
                    let journal_entry = JournalEntry {
                        epoch_msec: data_change.date_time.unwrap_or_else(ShvDateTime::now).epoch_msec(),
                        path: property_path,
                        signal,
                        source: Default::default(),
                        value: data_change.value,
                        access_level: AccessLevel::Read as _,
                        short_time: data_change.short_time.unwrap_or(-1),
                        user_id: Default::default(),
                        repeat: data_change.value_flags & (1 << VALUE_FLAG_SPONTANEOUS_BIT) == 0,
                        provisional: true, // data_change.value_flags & (1 << VALUE_FLAG_PROVISIONAL_BIT) != 0,
                    };
                    // Schedule next task
                    request_scheduler.schedule_new(site_path, Request::Append(journal_entry));
                }
            },
            site = request_scheduler.inflight.select_next_some() => {
                request_scheduler.on_finished(site);
            }
            complete => break,
        }
    }
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;

    use futures::channel::mpsc::{unbounded, UnboundedReceiver, UnboundedSender};
    use log::debug;
    use shvclient::clientapi::ClientCommand;
    use shvproto::DateTime;
    use shvrpc::rpcframe::RpcFrame;

    use crate::{datachange::DataChange, dirtylog::{DirtyLogCommand, dirtylog_task}, journalentry::JournalEntry, sync::SyncCommand, util::{DedupReceiver, init_logger, testing::{PrettyJoinError, TestStep, run_test}}};

    struct DirtylogTaskTestState {
        sender: UnboundedSender<DirtyLogCommand>,
        _sync_cmd_rx: DedupReceiver<SyncCommand>,
    }

    struct TestDirtyLogCommand(DirtyLogCommand);

    #[async_trait::async_trait]
    impl TestStep<DirtylogTaskTestState> for TestDirtyLogCommand {
        async fn exec(&self, _client_command_receiver: &mut UnboundedReceiver<ClientCommand>, _subscriptions: &mut HashMap<String, UnboundedSender<RpcFrame>>, state: &mut DirtylogTaskTestState) {
            let cmd = match &self.0 {
                DirtyLogCommand::ProcessNotification(msg) => DirtyLogCommand::ProcessNotification(msg.clone()),
                DirtyLogCommand::Trim { site } => DirtyLogCommand::Trim { site: site.clone() },
                DirtyLogCommand::Get { .. } => panic!("Cannot send DirtyLogCommand::Get through TestDirtyLogCommand"),
            };
            debug!(target: "test-driver", "Sending DirtyLogCommand::{cmd:?}");
            state.sender.unbounded_send(cmd).expect("Sending DirtyLogCommands should succeed");
        }
    }

    struct TestGetDirtyLog {
        site: String,
        expected: Vec<JournalEntry>,
    }

    #[async_trait::async_trait]
    impl TestStep<DirtylogTaskTestState> for TestGetDirtyLog {
        async fn exec(&self, _client_command_receiver: &mut UnboundedReceiver<ClientCommand>, _subscriptions: &mut HashMap<String, UnboundedSender<RpcFrame>>, state: &mut DirtylogTaskTestState) {
            debug!(target: "test-driver", "Sending DirtyLogCommand::Get");
            let (sender, receiver) = futures::channel::oneshot::channel();
            state.sender.unbounded_send(DirtyLogCommand::Get { site: self.site.clone(), response_tx: sender }).expect("Sending DirtyLogCommands should succeed");
            let dirtylog = receiver.await.expect("Getting dirtylog must succeed");
            assert_eq!(dirtylog, self.expected);
        }
    }

    struct TestCase<'a> {
        name: &'static str,
        steps: &'a [Box<dyn TestStep<DirtylogTaskTestState>>],
        starting_files: Vec<(&'static str, &'static str)>,
        expected_file_paths: Vec<(&'static str, &'a str)>,
    }

    #[tokio::test]
    async fn dirtylog_task_test() -> std::result::Result<(), PrettyJoinError> {
        init_logger();

        let test_cases = [
            TestCase {
                name: "ProcessNotification: journaldir doesn't exist",
                steps: &[
                    Box::new(TestDirtyLogCommand(DirtyLogCommand::ProcessNotification(crate::sites::ParsedNotification { site_path: "site1".into(), property_path: "some_value_node".into(), signal: "chng".into(), param: 20.into() })))
                ],
                starting_files: vec![],
                expected_file_paths: vec![],
            },
            TestCase {
                name: "ProcessNotification: notifications get written to disk",
                steps: &[
                    Box::new(TestDirtyLogCommand(DirtyLogCommand::ProcessNotification(crate::sites::ParsedNotification { site_path: "site1".into(), property_path: "some_value_node".into(), signal: "chng".into(), param: DataChange{
                        value: 20.into(),
                        date_time: Some(DateTime::from_iso_str("2022-07-07T00:00:00.000").expect("DateTime must work")),
                        value_flags: 0,
                        short_time: None,
                    }.into() }))),
                ],
                starting_files: vec![("site1/2022-07-07T18-06-15-000.log2", "")],
                expected_file_paths: vec![
                    ("site1/2022-07-07T18-06-15-000.log2", ""),
                    (
                        "site1/dirtylog",
                        "2022-07-07T00:00:00.000Z\t\tsome_value_node\t20\t\tchng\t4\t\n"
                    )
                ],
            },
            TestCase {
                name: "Get nonexisting dirtylog",
                steps: &[
                    Box::new(TestGetDirtyLog{
                        site: "site1".to_string(),
                        expected: vec![],
                    })
                ],
                starting_files: vec![("site1/2022-07-07T18-06-15-000.log2", "")],
                expected_file_paths: vec![
                    ("site1/2022-07-07T18-06-15-000.log2", ""),
                ],
            },
            TestCase {
                name: "Get existing empty dirtylog",
                steps: &[
                    Box::new(TestGetDirtyLog{
                        site: "site1".to_string(),
                        expected: vec![],
                    })
                ],
                starting_files: vec![("site1/dirtylog", "")],
                expected_file_paths: vec![
                    ("site1/dirtylog", ""),
                ],
            },
            TestCase {
                name: "Get existing dirtylog with entry",
                steps: &[
                    Box::new(TestGetDirtyLog{
                        site: "site1".to_string(),
                        expected: vec![JournalEntry {
                            epoch_msec: 1657152000000,
                            path: "some_value_node".into(),
                            signal: "chng".into(),
                            source: "get".into(),
                            value: 20.into(),
                            access_level: 8,
                            short_time: -1,
                            user_id: None,
                            repeat: true,
                            provisional: true
                        }],
                    })
                ],
                starting_files: vec![("site1/dirtylog", "2022-07-07T00:00:00.000Z\t\tsome_value_node\t20\t\tchng\t4\t\n")],
                expected_file_paths: vec![
                    ("site1/dirtylog", "2022-07-07T00:00:00.000Z\t\tsome_value_node\t20\t\tchng\t4\t\n"),
                ],
            },
            TestCase {
                name: "Existing dirtylog with invalid entries",
                steps: &[
                    Box::new(TestGetDirtyLog{
                        site: "site1".to_string(),
                        expected: vec![JournalEntry {
                            epoch_msec: 1657152000000,
                            path: "some_value_node".into(),
                            signal: "chng".into(),
                            source: "get".into(),
                            value: 20.into(),
                            access_level: 8,
                            short_time: -1,
                            user_id: None,
                            repeat: true,
                            provisional: true
                        }],
                    })
                ],
                starting_files: vec![
                    (
                        "site1/dirtylog",
                        concat!(
                            "2022-07-07T00:00:00.000Z\t\tsome_value_node\t20\t\tchng\t4\t\n",
                            "sadjfasjn",
                        )
                    )
                ],
                expected_file_paths: vec![
                    (
                        "site1/dirtylog",
                        concat!(
                            "2022-07-07T00:00:00.000Z\t\tsome_value_node\t20\t\tchng\t4\t\n",
                            "sadjfasjn",
                        )
                    )
                ],
            },
            TestCase {
                name: "Trim: journaldir doesn't exist",
                steps: &[
                    Box::new(TestDirtyLogCommand(DirtyLogCommand::Trim{site: "site1".to_string()}))
                ],
                starting_files: vec![],
                expected_file_paths: vec![],
            },
            TestCase {
                name: "Trim: no dirtylog",
                steps: &[
                    Box::new(TestDirtyLogCommand(DirtyLogCommand::Trim{site: "site1".to_string()}))
                ],
                starting_files: vec![("site1/2022-07-07T18-06-15-000.log2", "")],
                expected_file_paths: vec![
                    ("site1/2022-07-07T18-06-15-000.log2", ""),
                ],
            },
            TestCase {
                name: "Trim: no files to trim from",
                steps: &[
                    Box::new(TestDirtyLogCommand(DirtyLogCommand::Trim{site: "site1".to_string()}))
                ],
                starting_files: vec![("site1/dirtylog", "2022-07-07T00:00:00.000Z\t\tsome_value_node\t20\t\tchng\t4\t\n")],
                expected_file_paths: vec![
                    ("site1/dirtylog", "2022-07-07T00:00:00.000Z\t\tsome_value_node\t20\t\tchng\t4\t\n"),
                ],
            },
            TestCase {
                name: "Trim: nothing to trim",
                steps: &[
                    Box::new(TestDirtyLogCommand(DirtyLogCommand::Trim{site: "site1".to_string()}))
                ],
                starting_files: vec![("site1/dirtylog", "2022-07-07T00:00:00.000Z\t\tsome_value_node\t20\t\tchng\t4\t\n")],
                expected_file_paths: vec![
                    ("site1/dirtylog", "2022-07-07T00:00:00.000Z\t\tsome_value_node\t20\t\tchng\t4\t\n"),
                ],
            },
            TestCase {
                name: "Trim: emptying dirtylog",
                steps: &[
                    Box::new(TestDirtyLogCommand(DirtyLogCommand::Trim{site: "site1".to_string()}))
                ],
                starting_files: vec![
                    (
                        "site1/2022-07-06T18-06-15-000.log2",
                        concat!(
                            "2022-07-06T00:00:00.000Z\t\tsome_value_node\t20\t\tchng\t4\t\n",
                            "2022-07-08T00:00:00.000Z\t\tsome_value_node\t20\t\tchng\t4\t\n"
                        )
                    ),
                    ("site1/dirtylog", "2022-07-07T00:00:00.000Z\t\tsome_value_node\t20\t\tchng\t4\t\n")
                ],
                expected_file_paths: vec![
                    (
                        "site1/2022-07-06T18-06-15-000.log2",
                        concat!(
                            "2022-07-06T00:00:00.000Z\t\tsome_value_node\t20\t\tchng\t4\t\n",
                            "2022-07-08T00:00:00.000Z\t\tsome_value_node\t20\t\tchng\t4\t\n",
                        )
                    ),
                    ("site1/dirtylog", ""),
                ],
            },
            TestCase {
                name: "Trim: trimming some entries from the dirtylog",
                steps: &[
                    Box::new(TestDirtyLogCommand(DirtyLogCommand::Trim{site: "site1".to_string()}))
                ],
                starting_files: vec![
                    (
                        "site1/2022-07-06T18-06-15-000.log2",
                        "2022-07-06T00:00:00.000Z\t\tsome_value_node\t20\t\tchng\t4\t\n"
                    ),
                    (
                        "site1/dirtylog",
                        concat!(
                            "2022-07-06T00:00:00.000Z\t\tsome_value_node\t20\t\tchng\t4\t\n",
                            "2022-07-08T00:00:00.000Z\t\tsome_value_node\t20\t\tchng\t4\t\n",
                        )
                    ),
                ],
                expected_file_paths: vec![
                    (
                        "site1/2022-07-06T18-06-15-000.log2",
                        "2022-07-06T00:00:00.000Z\t\tsome_value_node\t20\t\tchng\t4\t\n"
                    ),
                    (
                        "site1/dirtylog",
                        concat!(
                            "2022-07-06T00:00:00.000Z\t\tsome_value_node\t20\t\tchng\t4\t\n",
                            "2022-07-08T00:00:00.000Z\t\tsome_value_node\t20\t\tchng\t4\t\n",
                        )
                    ),
                ],
            },
        ];

        for test_case in test_cases {
            run_test(
                test_case.name,
                test_case.steps,
                test_case.starting_files,
                test_case.expected_file_paths,
                |ccs, _ces, cer, _dirtylog_cmd_rx, _sync_cmd_rx, state| {
                    let (sender, receiver) = unbounded();
                    let task_state = DirtylogTaskTestState {
                        sender,
                        _sync_cmd_rx,
                    };
                    let dirtylog_task = tokio::spawn(dirtylog_task(ccs, cer, state, receiver));
                    (dirtylog_task, task_state)
                },
                |state| {
                    state.sender.close_channel();
                },
                &[]
            ).await?;
        }

        Ok(())
    }
}