reductstore 1.20.8

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
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
// Copyright 2021-2026 ReductSoftware UG
// Licensed under the Apache License, Version 2.0

use crate::storage::block_manager::BlockRef;
use crate::storage::entry::{Entry, RecordReader};
use crate::storage::proto::{record, Record};
use log::debug;
use reduct_base::error::ReductError;
use reduct_base::{internal_server_error, not_found, too_early};

impl Entry {
    /// Starts a new record read.
    ///
    /// # Arguments
    ///
    /// * `time` - The timestamp of the record.
    ///
    /// # Returns
    ///
    /// * `RecordReader` - The record reader to read the record content in chunks.
    /// * `HTTPError` - The error if any.
    pub(crate) async fn begin_read(&self, time: u64) -> Result<RecordReader, ReductError> {
        debug!(
            "Reading record for ts={} in {}/{}",
            time, self.bucket_name, self.name
        );

        let (block_ref, record) = if let Some(block_ref) = {
            let bm = self.block_manager.read().await?;
            bm.find_cached_block(time)
        } {
            let block = block_ref.read().await?;
            if let Some(record) = block.get_record(time) {
                let record = record.clone();
                drop(block);
                (block_ref, record)
            } else {
                self.find_record_for_read(time).await?
            }
        } else {
            self.find_record_for_read(time).await?
        };

        if record.state == record::State::Started as i32 {
            return Err(too_early!(
                "Record with timestamp {} in {}/{} is still being written",
                time,
                self.bucket_name,
                self.name
            ));
        }

        if record.state == record::State::Errored as i32 {
            return Err(internal_server_error!(
                "Record with timestamp {} in {}/{} is broken",
                time,
                self.bucket_name,
                self.name
            ));
        }

        RecordReader::try_new(self.block_manager.clone(), block_ref, time, None, None).await
    }

    async fn find_record_for_read(&self, time: u64) -> Result<(BlockRef, Record), ReductError> {
        let mut bm = self.block_manager.write().await?;
        let block_ref = bm.find_block(time).await?;
        let block = block_ref.read().await?;
        let record = block
            .get_record(time)
            .ok_or_else(|| {
                not_found!(
                    "Record {} not found in block {}/{}/{}",
                    time,
                    self.bucket_name,
                    self.name,
                    block.block_id(),
                )
            })?
            .clone();

        drop(block);
        Ok((block_ref, record))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cfg::{Cfg, InstanceRole};
    use crate::core::file_cache::FILE_CACHE;
    use crate::storage::block_manager::DATA_FILE_EXT;
    use crate::storage::engine::MAX_IO_BUFFER_SIZE;
    use crate::storage::entry::tests::{
        entry, entry_settings, path, write_record, write_stub_record,
    };
    use crate::storage::entry::EntrySettings;
    use bytes::Bytes;
    use reduct_base::io::ReadRecord;
    use reduct_base::Labels;
    use rstest::rstest;
    use std::path::PathBuf;
    use std::sync::Arc;

    #[rstest]
    #[tokio::test(flavor = "multi_thread")]
    async fn test_begin_read_empty(#[future] entry: Arc<Entry>) {
        let entry = entry.await;
        let writer = entry.begin_read(1000).await;
        assert_eq!(
            writer.err(),
            Some(not_found!("Record 1000 not found in entry bucket/entry"))
        );
    }

    #[rstest]
    #[tokio::test(flavor = "multi_thread")]
    async fn test_begin_read_early(#[future] entry: Arc<Entry>) {
        let entry = entry.await;
        write_stub_record(&entry, 1000000).await;
        let writer = entry.begin_read(1000).await;
        assert_eq!(
            writer.err(),
            Some(not_found!("Record 1000 not found in entry bucket/entry"))
        );
    }

    #[rstest]
    #[tokio::test(flavor = "multi_thread")]
    async fn test_begin_read_late(#[future] entry: Arc<Entry>) {
        let entry = entry.await;
        write_stub_record(&entry, 1000000).await;
        let reader = entry.begin_read(2000000).await;
        assert_eq!(
            reader.err(),
            Some(not_found!(
                "Record 2000000 not found in block bucket/entry/1000000"
            ))
        );
    }

    #[rstest]
    #[tokio::test(flavor = "multi_thread")]
    async fn test_begin_read_broken(#[future] entry: Arc<Entry>) {
        let entry = entry.await;
        let mut sender = entry
            .clone()
            .begin_write(1000000, 10, "text/plain".to_string(), Labels::new())
            .await
            .unwrap();
        sender
            .send(Ok(Some(Bytes::from(vec![0; 50]))))
            .await
            .unwrap();
        sender.send(Ok(None)).await.unwrap();

        let reader = entry.begin_read(1000000).await;
        assert_eq!(
            reader.err(),
            Some(internal_server_error!(
                "Record with timestamp 1000000 in bucket/entry is broken"
            ))
        );
    }

    #[rstest]
    #[tokio::test(flavor = "multi_thread")]
    async fn test_begin_read_still_written(#[future] entry: Arc<Entry>) {
        let entry = entry.await;
        let mut sender = entry
            .clone()
            .begin_write(1000000, 10, "text/plain".to_string(), Labels::new())
            .await
            .unwrap();
        sender
            .send(Ok(Some(Bytes::from(vec![0; 5]))))
            .await
            .unwrap();

        let reader = entry.begin_read(1000000).await;
        assert_eq!(
            reader.err(),
            Some(too_early!(
                "Record with timestamp 1000000 in bucket/entry is still being written"
            ))
        );
    }

    #[rstest]
    #[tokio::test(flavor = "multi_thread")]
    async fn test_begin_read_not_found(#[future] entry: Arc<Entry>) {
        let entry = entry.await;
        write_stub_record(&entry, 1000000).await;
        write_stub_record(&entry, 3000000).await;

        let reader = entry.begin_read(2000000).await;
        assert_eq!(
            reader.err(),
            Some(not_found!(
                "Record 2000000 not found in block bucket/entry/1000000"
            ))
        );
    }

    #[rstest]
    #[tokio::test(flavor = "multi_thread")]
    async fn test_begin_read_not_found_in_cached_block(#[future] entry: Arc<Entry>) {
        let entry = entry.await;
        write_stub_record(&entry, 1000000).await;
        write_stub_record(&entry, 3000000).await;

        {
            let mut bm = entry.block_manager.write().await.unwrap();
            let _ = bm.find_block(1000000).await.unwrap();
        }
        let reader = entry.begin_read(2000000).await;
        assert_eq!(
            reader.err(),
            Some(not_found!(
                "Record 2000000 not found in block bucket/entry/1000000"
            ))
        );
    }

    #[rstest]
    #[tokio::test(flavor = "multi_thread")]
    async fn test_begin_read_missing_data_file(#[future] entry: Arc<Entry>) {
        let entry = entry.await;
        write_stub_record(&entry, 1000000).await;

        let data_path = {
            let bm = entry.block_manager.read().await.unwrap();
            let block_id = *bm.index().tree().first().unwrap();
            bm.path().join(format!("{}{}", block_id, DATA_FILE_EXT))
        };
        FILE_CACHE.remove(&data_path).await.unwrap();

        let reader = entry.begin_read(1000000).await;
        assert_eq!(
            reader.err(),
            Some(not_found!("Data block {} not found", data_path.display()))
        );
    }

    #[rstest]
    #[tokio::test(flavor = "multi_thread")]
    async fn test_begin_read_ignores_already_corrupted_block(#[future] entry: Arc<Entry>) {
        let entry = entry.await;
        write_stub_record(&entry, 1000000).await;

        entry
            .block_manager
            .write()
            .await
            .unwrap()
            .mark_block_corrupted(1000000)
            .await
            .unwrap();

        let reader = entry.begin_read(1000000).await;
        assert_eq!(
            reader.err(),
            Some(not_found!("Record 1000000 not found in entry bucket/entry"))
        );
    }

    #[rstest]
    #[tokio::test(flavor = "multi_thread")]
    async fn test_begin_read_missing_data_file_on_replica_returns_too_early(
        entry_settings: EntrySettings,
        path: PathBuf,
    ) {
        let entry = entry(entry_settings.clone(), path.clone()).await;
        write_stub_record(&entry, 1000000).await;
        entry
            .block_manager
            .write()
            .await
            .unwrap()
            .save_cache_on_disk()
            .await
            .unwrap();

        let data_path = {
            let bm = entry.block_manager.read().await.unwrap();
            let block_id = *bm.index().tree().first().unwrap();
            bm.path().join(format!("{}{}", block_id, DATA_FILE_EXT))
        };
        FILE_CACHE.remove(&data_path).await.unwrap();

        let cfg = Cfg {
            role: InstanceRole::Replica,
            ..Default::default()
        };
        let replica_entry = Entry::builder()
            .path(path.join("entry"))
            .name("entry")
            .bucket_name("bucket")
            .settings(entry_settings)
            .cfg(Arc::new(cfg))
            .usage_counters(Default::default())
            .restore()
            .await
            .unwrap()
            .unwrap();

        let reader = replica_entry.begin_read(1000000).await;
        assert_eq!(
            reader.err(),
            Some(too_early!(
                "Data block {} is not available on replica yet",
                data_path.display()
            ))
        );
    }

    #[rstest]
    #[tokio::test(flavor = "multi_thread")]
    async fn test_begin_read_ok1(#[future] entry: Arc<Entry>) {
        let entry = entry.await;
        write_stub_record(&entry, 1000000).await;
        let mut reader = entry.begin_read(1000000).await.unwrap();
        assert_eq!(reader.read_chunk().unwrap(), Ok(Bytes::from("0123456789")));
    }

    #[rstest]
    #[tokio::test(flavor = "multi_thread")]
    async fn test_read_chunk_eof_does_not_mark_block_corrupted(#[future] entry: Arc<Entry>) {
        let entry = entry.await;
        write_stub_record(&entry, 1000000).await;
        let mut reader = entry.begin_read(1000000).await.unwrap();

        let data_path = {
            let bm = entry.block_manager.read().await.unwrap();
            bm.path().join(format!("1000000{}", DATA_FILE_EXT))
        };
        FILE_CACHE
            .write_or_create(&data_path, std::io::SeekFrom::Start(0))
            .await
            .unwrap()
            .set_len(0)
            .unwrap();

        let err = reader.read_chunk().unwrap().err().unwrap();
        assert_eq!(
            err.status(),
            reduct_base::error::ErrorCode::InternalServerError
        );
        assert!(!entry
            .block_manager
            .read()
            .await
            .unwrap()
            .is_block_corrupted(1000000));
    }

    #[rstest]
    #[tokio::test(flavor = "multi_thread")]
    async fn test_begin_read_ok2(#[future] entry: Arc<Entry>) {
        let entry = entry.await;
        write_stub_record(&entry, 1000000).await;
        write_stub_record(&entry, 1010000).await;

        let mut reader = entry.begin_read(1010000).await.unwrap();
        assert_eq!(reader.read_chunk().unwrap(), Ok(Bytes::from("0123456789")));
    }

    #[rstest]
    #[tokio::test(flavor = "multi_thread")]
    async fn test_begin_read_uses_cached_block(#[future] entry: Arc<Entry>) {
        let entry = entry.await;
        write_stub_record(&entry, 1000000).await;

        {
            let mut bm = entry.block_manager.write().await.unwrap();
            let _ = bm.find_block(1000000).await.unwrap();
        }

        let mut reader = entry.begin_read(1000000).await.unwrap();
        assert_eq!(reader.read_chunk().unwrap(), Ok(Bytes::from("0123456789")));
    }

    #[rstest]
    #[tokio::test(flavor = "multi_thread")]
    async fn test_begin_read_ok_in_chunks(#[future] entry: Arc<Entry>) {
        let entry = entry.await;
        let mut data = vec![0; MAX_IO_BUFFER_SIZE + 1];
        data[0] = 1;
        data[MAX_IO_BUFFER_SIZE] = 2;

        write_record(&entry, 1000000, data.clone()).await;

        let mut reader = entry.begin_read(1000000).await.unwrap();
        assert_eq!(
            reader.read_chunk().unwrap().unwrap().to_vec(),
            data[0..MAX_IO_BUFFER_SIZE]
        );
        assert_eq!(
            reader.read_chunk().unwrap().unwrap().to_vec(),
            data[MAX_IO_BUFFER_SIZE..]
        );
        assert_eq!(reader.read_chunk(), None);
    }

    #[rstest]
    #[tokio::test(flavor = "multi_thread")]
    async fn test_search(path: PathBuf) {
        let entry = entry(
            EntrySettings {
                max_block_size: 10000,
                max_block_records: 5,
            },
            path,
        )
        .await;

        let step = 100000;
        for i in 0..10 {
            write_stub_record(&entry, i * step).await;
        }

        let reader = entry.begin_read(5 * step).await.unwrap();
        assert_eq!(reader.meta().timestamp(), 500000);
    }

    #[rstest]
    #[tokio::test(flavor = "multi_thread")]
    async fn test_begin_read_when_entry_is_deleted(entry_settings: EntrySettings, path: PathBuf) {
        let entry = entry(entry_settings.clone(), path.clone()).await;
        entry.mark_deleting().await.unwrap();

        let writer = entry.begin_read(1000).await;
        assert_eq!(
            writer.err(),
            Some(not_found!("Record 1000 not found in entry bucket/entry"))
        );
    }

    #[rstest]
    #[tokio::test(flavor = "multi_thread")]
    async fn test_begin_read(entry_settings: EntrySettings, path: PathBuf) {
        let entry = entry(entry_settings.clone(), path.clone()).await;

        write_stub_record(&entry, 1000000).await;
        let mut reader = entry.begin_read(1000000).await.unwrap();
        assert_eq!(reader.read_chunk().unwrap(), Ok(Bytes::from("0123456789")));
    }
}