reductstore 1.20.7

ReductStore is a time series database designed specifically for storing and managing large amounts of blob data.
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
// Copyright 2021-2026 ReductSoftware UG
// Licensed under the Apache License, Version 2.0

use crate::core::sync::AsyncRwLock;
use crate::storage::block_manager::BlockManager;
use crate::storage::entry::{Entry, RecordQueryStats};
use log::warn;
use reduct_base::error::{ErrorCode, ReductError};
use reduct_base::io::ReadRecord;
use reduct_base::msg::entry_api::QueryEntry;
use reduct_base::not_found;
use std::collections::{BTreeMap, BTreeSet};
use std::sync::Arc;

struct RemoveRecordsResult {
    errors: BTreeMap<u64, ReductError>,
    block_ids: Vec<u64>,
}

impl Entry {
    /// Remove multiple records.
    ///
    /// The method removes multiple records. The records are identified by their timestamps
    /// and batched by the block they belong to
    ///
    /// # Arguments
    ///
    /// * `timestamps` - A vector of timestamps of the records to remove.
    ///
    /// # Returns
    ///
    /// A map of timestamps to the result of the remove operation. The result is either a vector of labels
    /// or an error if the record was not found.
    pub async fn remove_records(
        self: Arc<Self>,
        timestamps: Vec<u64>,
    ) -> Result<BTreeMap<u64, ReductError>, ReductError> {
        self.ensure_not_deleting().await?;
        let block_manager = self.block_manager.clone();
        Ok(
            Self::inner_remove_records(timestamps, block_manager, &self.bucket_name, &self.name)
                .await?
                .errors,
        )
    }

    /// Query and remove multiple records over a range of timestamps.
    ///
    /// # Arguments
    ///
    /// * `start` - The start timestamp of the query.
    /// * `end` - The end timestamp of the query.
    /// * `options` - The query options.
    ///
    /// # Returns
    ///
    /// The number of records removed.
    ///
    /// # Errors
    ///
    /// * If the query fails.
    pub async fn query_remove_records(&self, options: QueryEntry) -> Result<u64, ReductError> {
        Ok(self.query_remove_records_with_stats(options).await?.records)
    }

    pub(crate) async fn query_remove_records_with_stats(
        &self,
        mut options: QueryEntry,
    ) -> Result<RecordQueryStats, ReductError> {
        self.ensure_not_deleting().await?;
        options.continuous = None; // force non-continuous query

        let rx = async || {
            // io defaults isn't used in remove queries
            let query_id = self.query(options).await?;
            self.get_query_receiver(query_id).await
        };

        let rx = match rx().await {
            Ok((rx, _)) => rx,
            Err(e) => return Err(e).into(),
        };

        let block_manager = self.block_manager.clone();
        let max_block_records = self.settings().await?.max_block_records; // max records per block

        // Loop until the query is done
        let mut continue_query = true;
        let mut stats = RecordQueryStats::default();
        let mut affected_blocks = BTreeSet::new();

        while continue_query {
            let mut records_to_remove = vec![];
            records_to_remove.reserve(max_block_records as usize);

            // Receive a batch of records to remove
            while records_to_remove.len() < max_block_records as usize && continue_query {
                let result = &mut rx.upgrade()?.write().await?.recv().await;
                match result {
                    Some(Ok(rec)) => {
                        records_to_remove.push(rec.meta().timestamp());
                    }
                    Some(Err(ReductError {
                        status: ErrorCode::NoContent,
                        ..
                    })) => {
                        continue_query = false;
                    }
                    None => {
                        continue_query = false;
                    }
                    Some(Err(e)) => return Err(e.clone()),
                }
            }

            // Send the records to remove
            self.ensure_not_deleting().await?;
            stats.records += records_to_remove.len() as u64;
            let copy_block_manager = block_manager.clone();

            match Self::inner_remove_records(
                records_to_remove,
                copy_block_manager,
                &self.bucket_name,
                &self.name,
            )
            .await
            {
                Ok(result) => {
                    affected_blocks.extend(result.block_ids);
                    for (timestamp, error) in result.errors {
                        // TODO: send the error to the client
                        warn!(
                            "Failed to remove record with timestamp {}: {}",
                            timestamp, error
                        );

                        stats.records -= 1;
                    }
                }
                Err(e) => return Err(e),
            }
        }

        stats.blocks = affected_blocks.len() as u64;
        Ok(stats)
    }

    /// Query and count multiple records over a range of timestamps.
    ///
    /// # Arguments
    ///
    /// * `options` - The query options.
    ///
    /// # Returns
    ///
    /// The number of matched records.
    ///
    /// # Errors
    ///
    /// * If the query fails.
    #[allow(dead_code)]
    pub async fn query_count_records(&self, options: QueryEntry) -> Result<u64, ReductError> {
        Ok(self.query_count_records_with_stats(options).await?.records)
    }

    pub(crate) async fn query_count_records_with_stats(
        &self,
        mut options: QueryEntry,
    ) -> Result<RecordQueryStats, ReductError> {
        options.continuous = None; // force non-continuous query

        let rx = async || {
            // io defaults isn't used in count queries
            let query_id = self.query(options).await?;
            self.get_query_receiver(query_id).await
        };

        let rx = match rx().await {
            Ok((rx, _)) => rx,
            Err(e) => return Err(e).into(),
        };

        let mut continue_query = true;
        let mut stats = RecordQueryStats::default();
        let mut affected_blocks = BTreeSet::new();

        while continue_query {
            let result = &mut rx.upgrade()?.write().await?.recv().await;
            match result {
                Some(Ok(rec)) => {
                    stats.records += 1;
                    let block_ref = self
                        .block_manager
                        .write()
                        .await?
                        .find_block(rec.meta().timestamp())
                        .await?;
                    affected_blocks.insert(block_ref.read().await?.block_id());
                }
                Some(Err(ReductError {
                    status: ErrorCode::NoContent,
                    ..
                })) => {
                    continue_query = false;
                }
                None => {
                    continue_query = false;
                }
                Some(Err(e)) => return Err(e.clone()),
            }
        }

        stats.blocks = affected_blocks.len() as u64;
        Ok(stats)
    }

    async fn inner_remove_records(
        timestamps: Vec<u64>,
        block_manager: Arc<AsyncRwLock<BlockManager>>,
        bucket_name: &str,
        entry_name: &str,
    ) -> Result<RemoveRecordsResult, ReductError> {
        let mut error_map = BTreeMap::new();
        let mut records_per_block = BTreeMap::new();

        {
            for time in timestamps {
                // Find the block that contains the record
                // TODO: Try to avoid the lookup for each record
                match block_manager.write().await?.find_block(time).await {
                    Ok(block_ref) => {
                        // Check if the record exists
                        let block = block_ref.read().await?;
                        if let Some(_) = block.get_record(time) {
                            records_per_block
                                .entry(block.block_id())
                                .or_insert_with(Vec::new)
                                .push(time);
                        } else {
                            error_map.insert(
                                time,
                                not_found!(
                                    "Record {} not found in entry {}/{}",
                                    time,
                                    bucket_name,
                                    entry_name
                                ),
                            );
                        }
                    }
                    Err(e) => {
                        error_map.insert(time, e);
                    }
                }
            }
        }

        // Remove the records
        let mut handlers = vec![];
        let mut block_ids = vec![];
        for (block_id, timestamps) in records_per_block {
            block_ids.push(block_id);
            let local_block_manager = block_manager.clone();
            let handler = tokio::spawn(async move {
                // TODO: we don't parallelize the removal of records in different blocks
                let mut bm = local_block_manager.write().await?;
                bm.remove_records(block_id, timestamps).await?;
                Ok::<(), ReductError>(())
            });
            handlers.push(handler);
        }

        for handler in handlers {
            handler.await.unwrap()?;
        }

        Ok(RemoveRecordsResult {
            errors: error_map,
            block_ids,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::sync::{
        reset_rwlock_config, set_rwlock_failure_action, set_rwlock_timeout, RwLockFailureAction,
    };
    use crate::storage::entry::tests::{entry, write_stub_record};
    use crate::storage::entry::EntrySettings;
    use rstest::{fixture, rstest};
    use serial_test::serial;
    use std::sync::Arc;
    use std::time::Duration;

    #[rstest]
    #[tokio::test]
    #[serial]
    async fn test_remove_records(#[future] entry_with_data: Arc<Entry>) {
        let entry_with_data = entry_with_data.await;

        let timestamps = vec![0, 2, 4, 5];
        let error_map = entry_with_data
            .clone()
            .remove_records(timestamps)
            .await
            .unwrap();

        assert_eq!(error_map.len(), 2, "Only two records are not found");
        assert_eq!(
            error_map[&0],
            not_found!("Record 0 not found in entry bucket/entry")
        );
        assert_eq!(
            error_map[&5],
            not_found!("Record 5 not found in entry bucket/entry")
        );

        // check existing records
        assert!(entry_with_data.begin_read(1).await.is_ok());
        assert!(entry_with_data.begin_read(3).await.is_ok());

        // check removed records
        assert_eq!(
            entry_with_data.begin_read(2).await.err().unwrap(),
            not_found!("Record 2 not found in block bucket/entry/1")
        );
        assert_eq!(
            entry_with_data.begin_read(4).await.err().unwrap(),
            not_found!("Record 4 not found in block bucket/entry/3")
        );
    }

    #[rstest]
    #[tokio::test]
    #[serial]
    async fn test_query_remove_records(#[future] entry_with_data: Arc<Entry>) {
        let entry_with_data = entry_with_data.await;

        let params = QueryEntry {
            start: Some(2),
            stop: Some(4),
            ..Default::default()
        };

        let removed_records = entry_with_data.query_remove_records(params).await.unwrap();

        assert_eq!(removed_records, 2);

        // check removed records
        assert_eq!(
            entry_with_data.begin_read(2).await.err().unwrap(),
            not_found!("Record 2 not found in block bucket/entry/1")
        );
        assert_eq!(
            entry_with_data.begin_read(3).await.err().unwrap(),
            not_found!("Record 3 not found in block bucket/entry/3")
        );
    }

    #[rstest]
    #[tokio::test]
    #[serial]
    async fn test_query_count_records(#[future] entry_with_data: Arc<Entry>) {
        let entry_with_data = entry_with_data.await;

        let params = QueryEntry {
            start: Some(2),
            stop: Some(4),
            ..Default::default()
        };

        let counted_records = entry_with_data.query_count_records(params).await.unwrap();

        assert_eq!(counted_records, 2);

        assert!(entry_with_data.begin_read(2).await.is_ok());
        assert!(entry_with_data.begin_read(3).await.is_ok());
    }

    // TODO: replace with multiple add/remove on RwLock
    #[fixture]
    async fn entry_with_data(#[future] entry: Arc<Entry>) -> Arc<Entry> {
        let entry = entry.await;
        struct ResetGuard;
        impl Drop for ResetGuard {
            fn drop(&mut self) {
                reset_rwlock_config();
            }
        }
        let _reset = ResetGuard;
        set_rwlock_failure_action(RwLockFailureAction::Error);
        set_rwlock_timeout(Duration::from_secs(10));

        entry
            .set_settings(EntrySettings {
                max_block_records: 2,
                ..entry.settings().await.unwrap()
            })
            .await
            .unwrap();

        write_stub_record(&entry, 1).await;
        write_stub_record(&entry, 2).await;
        write_stub_record(&entry, 3).await;
        write_stub_record(&entry, 4).await;
        entry
    }
}