talos_cohort_replicator 0.3.17

Talos Replicator for serial installation of statemaps and updating snapshot of cohort db.
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
// - Test updating decision in suffix.
//  -  Duplicate decisions
// - Test `prepare_offset_for_commit`

use ahash::{HashMap, HashMapExt};
use async_trait::async_trait;
use serde_json::{json, Value};
use talos_certifier::{
    errors::SystemServiceError,
    ports::{common::SharedPortTraits, errors::MessageReceiverError, MessageReciever},
    test_helpers::mocks::payload::{build_mock_outcome, MockCandidatePayload, MockChannelMessage},
    ChannelMessage,
};
use talos_common_utils::backpressure::config::BackPressureConfig;
use talos_suffix::{core::SuffixConfig, Suffix};
use tokio::{sync::mpsc, task::JoinHandle};

use crate::{
    core::Replicator,
    models::{ReplicatorCandidate, ReplicatorCandidateMessage},
    services::replicator_service::{ReplicatorService, ReplicatorServiceConfig},
    StatemapItem,
};

/// Mock receiver to mimic Kafka consumer to use in tests.
pub struct MockReciever {
    pub consumer: mpsc::Receiver<ChannelMessage<ReplicatorCandidateMessage>>,
    pub offset: i64,
}

#[async_trait]
impl MessageReciever for MockReciever {
    type Message = ChannelMessage<ReplicatorCandidateMessage>;

    async fn consume_message(&mut self) -> Result<Option<Self::Message>, MessageReceiverError> {
        let msg = self.consumer.recv().await.unwrap();

        Ok(Some(msg))
    }

    async fn subscribe(&self) -> Result<(), SystemServiceError> {
        Ok(())
    }

    fn commit(&self) -> Result<(), Box<SystemServiceError>> {
        Ok(())
    }
    fn commit_async(&self) -> Option<JoinHandle<Result<(), SystemServiceError>>> {
        None
    }
    fn update_offset_to_commit(&mut self, version: i64) -> Result<(), Box<SystemServiceError>> {
        self.offset = version;
        Ok(())
    }
    async fn update_savepoint_async(&mut self, _version: i64) -> Result<(), SystemServiceError> {
        Ok(())
    }

    async fn unsubscribe(&self) -> () {}
}

#[async_trait]
impl SharedPortTraits for MockReciever {
    async fn is_healthy(&self) -> bool {
        true
    }
    async fn shutdown(&self) -> bool {
        false
    }
}

//
struct MockReplicatorChannelMessage {}

impl MockReplicatorChannelMessage {
    fn build_channel_message(
        version: u64,
        suffix_head: u64,
        safepoint: Option<u64>,
        conflict_version: Option<u64>,
        headers: &HashMap<String, String>,
        statemaps: Option<Vec<(String, Value)>>,
    ) -> (ChannelMessage<ReplicatorCandidateMessage>, ChannelMessage<ReplicatorCandidateMessage>) {
        let outcome = build_mock_outcome(conflict_version, safepoint);

        let cm_1_statemaps = build_mock_statemaps(version, safepoint, statemaps.unwrap_or(vec![default_statemap_item(), default_statemap_item()]));
        let cm_1: ReplicatorCandidateMessage = MockCandidatePayload::new().add_statemap(&serde_json::to_value(cm_1_statemaps).unwrap()).build();

        let cm_1_channel_msg = MockChannelMessage::new(&cm_1, version);
        let c_cm_1_channel_msg = cm_1_channel_msg.build_candidate_channel_message(headers);
        let d_cm_1_channel_msg = cm_1_channel_msg.build_decision_channel_message(version, &outcome, suffix_head, headers);

        (c_cm_1_channel_msg, d_cm_1_channel_msg)
    }
}

fn default_statemap_item() -> (String, Value) {
    ("DefaultAction".to_owned(), json!({"some":"payload"}))
}

fn build_mock_statemaps(version: u64, safepoint: Option<u64>, statemap_item_fns: Vec<(String, Value)>) -> Vec<StatemapItem> {
    let mut statemaps = vec![];

    for item in statemap_item_fns {
        statemaps.push(StatemapItem::new(item.0, version, item.1, safepoint));
    }

    statemaps
}

// - Test inserting candidates to suffix
//      - When candidate version is below suffix head
//      - When candidate version > suffix head
#[tokio::test]
async fn test_replicator_service_inserting_candidates_to_suffix() {
    let (statemaps_tx, _) = mpsc::channel(50);
    let (replicator_feedback_tx, replicator_feedback_rx) = mpsc::channel(50);
    let (tx_message_channel, rx_message_channel) = mpsc::channel(10);

    let receiver = MockReciever {
        consumer: rx_message_channel,
        offset: 0,
    };

    let config = ReplicatorServiceConfig {
        commit_frequency_ms: 60_000, // 1 hour
        enable_stats: false,
        backpressure: BackPressureConfig::default(),
    };

    let suffix_config = SuffixConfig {
        capacity: 1_000,
        prune_start_threshold: Some(10),
        min_size_after_prune: None,
    };
    let suffix: Suffix<ReplicatorCandidate> = Suffix::with_config(suffix_config, None);

    let replicator = Replicator::new(receiver, suffix, None);

    let mut replicator_svc = ReplicatorService::new(statemaps_tx, replicator_feedback_rx, replicator, config);

    // ************* start of assertions *************** //
    let headers = HashMap::new();

    //******************************************************************************************************
    //*** Test when sending a version > suffix length while suffix is empty
    //******************************************************************************************************
    replicator_feedback_tx
        .send(crate::core::ReplicatorChannel::LastInstalledVersion(0))
        .await
        .unwrap();

    // Run the replicator service once to last installed version.
    replicator_svc.run_once().await.unwrap();

    assert_eq!(replicator_svc.replicator.suffix.meta.prune_index, None);
    assert_eq!(replicator_svc.replicator.suffix.messages.len(), 0);

    // ** candidate message with version 10.
    let vers_10 = 10;
    let (c_10, _) = MockReplicatorChannelMessage::build_channel_message(vers_10, replicator_svc.replicator.suffix.meta.head, Some(0), None, &headers, None);

    // Send the candidate message for version 10.
    tx_message_channel.send(c_10).await.unwrap();
    assert_eq!(tx_message_channel.max_capacity() - tx_message_channel.capacity(), 1);

    // Run the replicator service to process the candidate for version 10.
    replicator_svc.run_once().await.unwrap();
    // replicator_svc.run_once().await.unwrap();

    assert_eq!(replicator_svc.replicator.suffix.messages.len(), 1);

    // ** candidate message with version 6.
    let vers_6 = 6;
    let (c_6, _) = MockReplicatorChannelMessage::build_channel_message(vers_6, replicator_svc.replicator.suffix.meta.head, Some(0), None, &headers, None);

    // Send the candidate message for version 6.
    tx_message_channel.send(c_6).await.unwrap();
    // Run the replicator service to process the candidate for version 6.
    replicator_svc.run_once().await.unwrap();

    // Candidate version 6 will not be inserted as it is behind the head.
    assert_eq!(replicator_svc.replicator.suffix.meta.head, 10);
    assert_eq!(replicator_svc.replicator.suffix.messages.len(), 1);

    // ** candidate message with version 17.
    let vers_17 = 17;
    let (c_17, _) = MockReplicatorChannelMessage::build_channel_message(vers_17, replicator_svc.replicator.suffix.meta.head, Some(0), None, &headers, None);

    // Send the candidate message for version 17.
    tx_message_channel.send(c_17).await.unwrap();
    // Run the replicator service to process the candidate for version 1.
    replicator_svc.run_once().await.unwrap();

    // Candidate version 17 will be inserted.
    assert_eq!(replicator_svc.replicator.suffix.meta.head, 10);
    assert_eq!(replicator_svc.replicator.suffix.messages.len(), 8);
}

// - Test decisions coming through first
//      - When decision version is below suffix head
//      - When decision version > suffix head
#[tokio::test]
async fn test_replicator_service_receiving_decisions_first() {
    let (statemaps_tx, statemaps_rx) = mpsc::channel(50);
    let (replicator_feedback_tx, replicator_feedback_rx) = mpsc::channel(50);
    let (tx_message_channel, rx_message_channel) = mpsc::channel(10);

    let receiver = MockReciever {
        consumer: rx_message_channel,
        offset: 0,
    };

    let config = ReplicatorServiceConfig {
        commit_frequency_ms: 60_000, // 1 hour
        enable_stats: false,
        backpressure: BackPressureConfig::default(),
    };

    let suffix_config = SuffixConfig {
        capacity: 1_000,
        prune_start_threshold: Some(10),
        min_size_after_prune: None,
    };
    let suffix: Suffix<ReplicatorCandidate> = Suffix::with_config(suffix_config, None);

    let replicator = Replicator::new(receiver, suffix, None);

    let mut replicator_svc = ReplicatorService::new(statemaps_tx, replicator_feedback_rx, replicator, config);

    // ************* start of assertions *************** //
    let headers = HashMap::new();

    replicator_feedback_tx
        .send(crate::core::ReplicatorChannel::LastInstalledVersion(0))
        .await
        .unwrap();

    // Run the replicator service once to last installed version.
    replicator_svc.run_once().await.unwrap();

    assert_eq!(replicator_svc.replicator.suffix.meta.prune_index, None);
    assert_eq!(replicator_svc.replicator.suffix.messages.len(), 0);

    // ** candidate message with version 399.
    let vers_399 = 399;
    let (_, d_399) = MockReplicatorChannelMessage::build_channel_message(vers_399, replicator_svc.replicator.suffix.meta.head, Some(0), None, &headers, None);

    // Send the decision for candidate message for version 399.
    // This is the first message to receive
    tx_message_channel.send(d_399).await.unwrap();
    assert_eq!(tx_message_channel.max_capacity() - tx_message_channel.capacity(), 1);

    // Run the replicator service to process the decision for version 399.
    replicator_svc.run_once().await.unwrap();

    assert_eq!(replicator_svc.replicator.suffix.messages.len(), 0);

    // ** candidate message with version 402.
    let vers_402 = 402;
    let (c_402, d_402) =
        MockReplicatorChannelMessage::build_channel_message(vers_402, replicator_svc.replicator.suffix.meta.head, Some(0), None, &headers, None);

    // Send the candidate message for version 402.
    tx_message_channel.send(c_402).await.unwrap();
    assert_eq!(tx_message_channel.max_capacity() - tx_message_channel.capacity(), 1);

    // Run the replicator service to process the candidate for version 402.
    replicator_svc.run_once().await.unwrap();
    // replicator_svc.run_once().await.unwrap();

    assert_eq!(replicator_svc.replicator.suffix.messages.len(), 1);
    assert_eq!(statemaps_rx.max_capacity() - statemaps_rx.capacity(), 0);

    // ** candidate message with version 400.
    let vers_400 = 400;
    let (_, d_400) = MockReplicatorChannelMessage::build_channel_message(vers_400, replicator_svc.replicator.suffix.meta.head, Some(0), None, &headers, None);

    // Send the decision for candidate message for version 400.
    tx_message_channel.send(d_400).await.unwrap();

    // Run the replicator service to process the decision for version 400.
    replicator_svc.run_once().await.unwrap();

    assert_eq!(replicator_svc.replicator.suffix.messages.len(), 1);
    assert_eq!(statemaps_rx.max_capacity() - statemaps_rx.capacity(), 0);

    //** */
    // Send the candidate message for version 402.
    tx_message_channel.send(d_402).await.unwrap();

    // Run the replicator service to process the decision for version 402.
    replicator_svc.run_once().await.unwrap();

    assert_eq!(replicator_svc.replicator.suffix.messages.len(), 1);
    assert_eq!(replicator_svc.replicator.suffix.meta.head, 402);
    assert_eq!(statemaps_rx.max_capacity() - statemaps_rx.capacity(), 1);
}

// - Test pruning.
//      - When version received is < head -> no pruning.
//      - When version received is == head -> pruning till head.
//      - When version received is > the suffix length -> pruning the whole suffix.
//      - When version received is > head and < suffix length -> pruning till the provided index.
// - Test items send for install.
//      - Check if we are sending duplicates for install
//      - Check we take only till the items that are decided
//      - Check if we are picking only the ones that are really required.
#[tokio::test]
async fn test_replicator_service_pruning_suffix() {
    let (statemaps_tx, statemaps_rx) = mpsc::channel(50);
    let (replicator_feedback_tx, replicator_feedback_rx) = mpsc::channel(50);
    let (tx_message_channel, rx_message_channel) = mpsc::channel(10);

    let receiver = MockReciever {
        consumer: rx_message_channel,
        offset: 0,
    };

    let config = ReplicatorServiceConfig {
        commit_frequency_ms: 60_000, // 1 hour
        enable_stats: false,
        backpressure: BackPressureConfig::default(),
    };

    let suffix_config = SuffixConfig {
        capacity: 1_000,
        prune_start_threshold: Some(10),
        min_size_after_prune: None,
    };
    let suffix: Suffix<ReplicatorCandidate> = Suffix::with_config(suffix_config, None);

    let replicator = Replicator::new(receiver, suffix, None);

    let mut replicator_svc = ReplicatorService::new(statemaps_tx, replicator_feedback_rx, replicator, config);

    // ************* start of assertions *************** //

    assert_eq!(replicator_svc.replicator.suffix.meta.prune_index, None);
    assert_eq!(replicator_svc.replicator.suffix.meta.head, 0);

    //******************************************************************************************************
    //*** Test when sending a version > suffix length while suffix is empty
    //******************************************************************************************************
    replicator_feedback_tx
        .send(crate::core::ReplicatorChannel::LastInstalledVersion(20))
        .await
        .unwrap();

    // Run the replicator service once to last installed version.
    replicator_svc.run_once().await.unwrap();

    assert_eq!(replicator_svc.replicator.suffix.meta.prune_index, None);
    assert_eq!(replicator_svc.replicator.suffix.messages.len(), 0);

    let headers = HashMap::new();

    // ** candidate message with version 1.
    let vers_1 = 1;
    let (c_1, d_1) = MockReplicatorChannelMessage::build_channel_message(vers_1, replicator_svc.replicator.suffix.meta.head, Some(0), None, &headers, None);

    // Send the candidate message for version 1.
    tx_message_channel.send(c_1).await.unwrap();
    // Send the decision message for version 1.
    tx_message_channel.send(d_1).await.unwrap();
    // Run the replicator service to process the candidate for version 1.
    replicator_svc.run_once().await.unwrap();

    assert_eq!(replicator_svc.replicator.suffix.messages.len(), 1);
    // No items send for for install as decision is not processed by replicator service.
    assert_eq!(statemaps_rx.max_capacity() - statemaps_rx.capacity(), 0);

    // Run the replicator service to process the decision for version 1.
    replicator_svc.run_once().await.unwrap();
    assert_eq!(statemaps_rx.max_capacity() - statemaps_rx.capacity(), 1);

    // ** candidate message with version 4.
    let vers_4 = 4;
    let (c_4, d_4) = MockReplicatorChannelMessage::build_channel_message(vers_4, replicator_svc.replicator.suffix.meta.head, Some(0), None, &headers, None);

    // Send the candidate message for version 4.
    tx_message_channel.send(c_4).await.unwrap();
    // Send the decision message for version 4.
    tx_message_channel.send(d_4).await.unwrap();
    // Run the replicator service to process the candidate for version 4.
    replicator_svc.run_once().await.unwrap();

    assert_eq!(replicator_svc.replicator.suffix.messages.len(), 4);
    // No items send for for install as decision is not processed by replicator service.
    assert_eq!(statemaps_rx.max_capacity() - statemaps_rx.capacity(), 1);

    // Run the replicator service to process the decision for version 4.
    replicator_svc.run_once().await.unwrap();
    assert_eq!(statemaps_rx.max_capacity() - statemaps_rx.capacity(), 2);

    //******************************************************************************************************
    //*** Test when sending a version > suffix length - Prunes everything.
    //******************************************************************************************************
    replicator_feedback_tx
        .send(crate::core::ReplicatorChannel::LastInstalledVersion(20))
        .await
        .unwrap();

    // Run the replicator service once to prune based on last installed version.
    replicator_svc.run_once().await.unwrap();

    // Pruning doesn't happen as the item is not in suffix
    assert_eq!(replicator_svc.replicator.suffix.meta.head, 0);
    assert_eq!(replicator_svc.replicator.suffix.messages.len(), 0);

    // ** candidate message with version 6 with abort decision
    let vers_6 = 6;
    let (c_6, d_6) = MockReplicatorChannelMessage::build_channel_message(vers_6, replicator_svc.replicator.suffix.meta.head, None, None, &headers, None);

    // ** candidate message with version 12 with commit decision and safepoint 6
    let vers_12 = 12;
    let (c_12, d_12) = MockReplicatorChannelMessage::build_channel_message(vers_12, replicator_svc.replicator.suffix.meta.head, None, None, &headers, None);

    // Send the candidate message for version 6.
    tx_message_channel.send(c_6).await.unwrap();
    // Send the candidate message for version 12.
    tx_message_channel.send(c_12).await.unwrap();

    // Run the replicator service to process the candidate for version 6 and 12.
    replicator_svc.run_once().await.unwrap();
    replicator_svc.run_once().await.unwrap();

    // Send the decision message for version 12.
    tx_message_channel.send(d_12).await.unwrap();
    // Run the replicator service to process the decision for version 12.
    replicator_svc.run_once().await.unwrap();
    // Although version 12 is decided and ready to send, since version 6 hasn't been decided, the channel will have statemaps to install only for version 1 and 4.
    assert_eq!(replicator_svc.replicator.suffix.meta.head, 6);
    assert_eq!(statemaps_rx.max_capacity() - statemaps_rx.capacity(), 2);
    assert_eq!(replicator_svc.replicator.suffix.messages.len(), 7);
    assert_eq!(replicator_svc.replicator.last_installing, 4);

    // Send the decision message for version 12.
    tx_message_channel.send(d_6).await.unwrap();
    // Run the replicator service to process the decision for version 6.
    replicator_svc.run_once().await.unwrap();
    assert_eq!(replicator_svc.replicator.last_installing, 12);
    assert_eq!(statemaps_rx.max_capacity() - statemaps_rx.capacity(), 4);

    //***************************************************************************************
    //*** Test when sending a version which is below the head - Pruning will not happen
    //***************************************************************************************
    replicator_feedback_tx
        .send(crate::core::ReplicatorChannel::LastInstalledVersion(3))
        .await
        .unwrap();

    // Run the replicator service once to last installed version.
    replicator_svc.run_once().await.unwrap();

    assert_eq!(replicator_svc.replicator.suffix.meta.head, 6);
    assert_eq!(statemaps_rx.max_capacity() - statemaps_rx.capacity(), 4);
    assert_eq!(replicator_svc.replicator.suffix.messages.len(), 7);

    //***************************************************************************************
    //*** Test when sending a version which is equal to the head - Pruning will happen
    //***************************************************************************************
    replicator_feedback_tx
        .send(crate::core::ReplicatorChannel::LastInstalledVersion(6))
        .await
        .unwrap();

    // Run the replicator service once to last installed version.
    replicator_svc.run_once().await.unwrap();

    assert_eq!(statemaps_rx.max_capacity() - statemaps_rx.capacity(), 4);
    assert_eq!(replicator_svc.replicator.suffix.meta.head, 12);
    assert_eq!(replicator_svc.replicator.suffix.messages.len(), 1);

    // ** candidate message with version 15 with abort decision
    let vers_15 = 15;
    let (c_15, _d_15) = MockReplicatorChannelMessage::build_channel_message(vers_15, replicator_svc.replicator.suffix.meta.head, None, None, &headers, None);

    // ** candidate message with version 22 with commit decision and safepoint 6
    let vers_22 = 22;
    let (c_22, _d_22) = MockReplicatorChannelMessage::build_channel_message(vers_22, replicator_svc.replicator.suffix.meta.head, None, None, &headers, None);

    // // Send the candidate versions 15 adn 22.
    tx_message_channel.send(c_15).await.unwrap();
    tx_message_channel.send(c_22).await.unwrap();

    // Run the replicator service to process the candidates for version 15 and 22.
    replicator_svc.run_once().await.unwrap();
    replicator_svc.run_once().await.unwrap();

    assert_eq!(replicator_svc.replicator.suffix.meta.head, 12);
    assert_eq!(replicator_svc.replicator.suffix.messages.len(), 11);

    //*****************************************************************************************************************************
    //*** Test when sending an version which is between the head and suffix length - Pruning will happen
    //*****************************************************************************************************************************

    replicator_feedback_tx
        .send(crate::core::ReplicatorChannel::LastInstalledVersion(13))
        .await
        .unwrap();

    // Run the replicator service once to last installed version.
    replicator_svc.run_once().await.unwrap();

    // Pruning will not happen, as it is considered as invalid.
    assert_eq!(replicator_svc.replicator.suffix.meta.head, 15);
    assert_eq!(replicator_svc.replicator.suffix.messages.len(), 8);
}