iroh_blobs/api/blobs/
reader.rs

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
use std::{
    io::{self, ErrorKind, SeekFrom},
    pin::Pin,
    task::{Context, Poll},
};

use n0_future::StreamExt;

use crate::{
    api::{
        blobs::{Blobs, ReaderOptions},
        proto::ExportRangesItem,
    },
    Hash,
};

/// A reader for blobs that implements `AsyncRead` and `AsyncSeek`.
#[derive(Debug)]
pub struct BlobReader {
    blobs: Blobs,
    options: ReaderOptions,
    state: ReaderState,
}

#[derive(Default, derive_more::Debug)]
enum ReaderState {
    Idle {
        position: u64,
    },
    Seeking {
        position: u64,
    },
    Reading {
        position: u64,
        #[debug(skip)]
        op: n0_future::boxed::BoxStream<ExportRangesItem>,
    },
    #[default]
    Poisoned,
}

impl BlobReader {
    pub(super) fn new(blobs: Blobs, options: ReaderOptions) -> Self {
        Self {
            blobs,
            options,
            state: ReaderState::Idle { position: 0 },
        }
    }

    pub fn hash(&self) -> &Hash {
        &self.options.hash
    }
}

impl tokio::io::AsyncRead for BlobReader {
    fn poll_read(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &mut tokio::io::ReadBuf<'_>,
    ) -> Poll<io::Result<()>> {
        let this = self.get_mut();
        let mut position1 = None;
        loop {
            let guard = &mut this.state;
            match std::mem::take(guard) {
                ReaderState::Idle { position } => {
                    // todo: read until next page boundary instead of fixed size
                    let len = buf.remaining() as u64;
                    let end = position.checked_add(len).ok_or_else(|| {
                        io::Error::new(ErrorKind::InvalidInput, "Position overflow when reading")
                    })?;
                    // start the export op for the entire size of the buffer, and convert to a stream
                    let stream = this
                        .blobs
                        .export_ranges(this.options.hash, position..end)
                        .stream();
                    position1 = Some(position);
                    *guard = ReaderState::Reading {
                        position,
                        op: Box::pin(stream),
                    };
                }
                ReaderState::Reading { position, mut op } => {
                    let position1 = position1.get_or_insert(position);
                    match op.poll_next(cx) {
                        Poll::Ready(Some(ExportRangesItem::Size(_))) => {
                            *guard = ReaderState::Reading { position, op };
                        }
                        Poll::Ready(Some(ExportRangesItem::Data(data))) => {
                            if data.offset != *position1 {
                                break Poll::Ready(Err(io::Error::other(
                                    "Data offset does not match expected position",
                                )));
                            }
                            buf.put_slice(&data.data);
                            // update just local position1, not the position in the state.
                            *position1 =
                                position1
                                    .checked_add(data.data.len() as u64)
                                    .ok_or_else(|| {
                                        io::Error::new(ErrorKind::InvalidInput, "Position overflow")
                                    })?;
                            *guard = ReaderState::Reading { position, op };
                        }
                        Poll::Ready(Some(ExportRangesItem::Error(err))) => {
                            *guard = ReaderState::Idle { position };
                            break Poll::Ready(Err(io::Error::other(format!(
                                "Error reading data: {err}"
                            ))));
                        }
                        Poll::Ready(None) => {
                            // done with the stream, go back in idle.
                            *guard = ReaderState::Idle {
                                position: *position1,
                            };
                            break Poll::Ready(Ok(()));
                        }
                        Poll::Pending => {
                            break if position != *position1 {
                                // we read some data so we need to abort the op.
                                //
                                // we can't be sure we won't be called with the same buf size next time.
                                *guard = ReaderState::Idle {
                                    position: *position1,
                                };
                                Poll::Ready(Ok(()))
                            } else {
                                // nothing was read yet, we remain in the reading state
                                //
                                // we make an assumption here that the next call will be with the same buf size.
                                *guard = ReaderState::Reading {
                                    position: *position1,
                                    op,
                                };
                                Poll::Pending
                            };
                        }
                    }
                }
                state @ ReaderState::Seeking { .. } => {
                    // should I try to recover from this or just keep it poisoned?
                    this.state = state;
                    break Poll::Ready(Err(io::Error::other("Can't read while seeking")));
                }
                ReaderState::Poisoned => {
                    break Poll::Ready(Err(io::Error::other("Reader is poisoned")));
                }
            };
        }
    }
}

impl tokio::io::AsyncSeek for BlobReader {
    fn start_seek(
        self: std::pin::Pin<&mut Self>,
        seek_from: tokio::io::SeekFrom,
    ) -> io::Result<()> {
        let this = self.get_mut();
        let guard = &mut this.state;
        match std::mem::take(guard) {
            ReaderState::Idle { position } => {
                let position1 = match seek_from {
                    SeekFrom::Start(pos) => pos,
                    SeekFrom::Current(offset) => {
                        position.checked_add_signed(offset).ok_or_else(|| {
                            io::Error::new(
                                ErrorKind::InvalidInput,
                                "Position overflow when seeking",
                            )
                        })?
                    }
                    SeekFrom::End(_offset) => {
                        // todo: support seeking from end if we know the size
                        return Err(io::Error::new(
                            ErrorKind::InvalidInput,
                            "Seeking from end is not supported yet",
                        ))?;
                    }
                };
                *guard = ReaderState::Seeking {
                    position: position1,
                };
                Ok(())
            }
            ReaderState::Reading { .. } => Err(io::Error::other("Can't seek while reading")),
            ReaderState::Seeking { .. } => Err(io::Error::other("Already seeking")),
            ReaderState::Poisoned => Err(io::Error::other("Reader is poisoned")),
        }
    }

    fn poll_complete(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<u64>> {
        let this = self.get_mut();
        let guard = &mut this.state;
        Poll::Ready(match std::mem::take(guard) {
            ReaderState::Seeking { position } => {
                *guard = ReaderState::Idle { position };
                Ok(position)
            }
            ReaderState::Idle { position } => {
                // seek calls poll_complete just in case, to finish a pending seek operation
                // before the next seek operation. So it is poll_complete/start_seek/poll_complete
                *guard = ReaderState::Idle { position };
                Ok(position)
            }
            state @ ReaderState::Reading { .. } => {
                // should I try to recover from this or just keep it poisoned?
                *guard = state;
                Err(io::Error::other("Can't seek while reading"))
            }
            ReaderState::Poisoned => Err(io::Error::other("Reader is poisoned")),
        })
    }
}

#[cfg(test)]
mod tests {
    use bao_tree::ChunkRanges;
    use testresult::TestResult;
    use tokio::io::{AsyncReadExt, AsyncSeekExt};

    use super::*;
    use crate::{
        store::{
            fs::{
                tests::{create_n0_bao, test_data, INTERESTING_SIZES},
                FsStore,
            },
            mem::MemStore,
        },
        util::ChunkRangesExt,
    };

    async fn reader_smoke(blobs: &Blobs) -> TestResult<()> {
        for size in INTERESTING_SIZES {
            let data = test_data(size);
            let tag = blobs.add_bytes(data.clone()).await?;
            // read all
            {
                let mut reader = blobs.reader(tag.hash);
                let mut buf = Vec::new();
                reader.read_to_end(&mut buf).await?;
                assert_eq!(buf, data);
                let pos = reader.stream_position().await?;
                assert_eq!(pos, data.len() as u64);
            }
            // seek to mid and read all
            {
                let mut reader = blobs.reader(tag.hash);
                let mid = size / 2;
                reader.seek(SeekFrom::Start(mid as u64)).await?;
                let mut buf = Vec::new();
                reader.read_to_end(&mut buf).await?;
                assert_eq!(buf, data[mid..].to_vec());
                let pos = reader.stream_position().await?;
                assert_eq!(pos, data.len() as u64);
            }
        }
        Ok(())
    }

    async fn reader_partial(blobs: &Blobs) -> TestResult<()> {
        for size in INTERESTING_SIZES {
            let data = test_data(size);
            let ranges = ChunkRanges::chunk(0);
            let (hash, bao) = create_n0_bao(&data, &ranges)?;
            println!("importing {} bytes", bao.len());
            blobs.import_bao_bytes(hash, ranges.clone(), bao).await?;
            // read the first chunk or the entire blob, whatever is smaller
            // this should work!
            {
                let mut reader = blobs.reader(hash);
                let valid = size.min(1024);
                let mut buf = vec![0u8; valid];
                reader.read_exact(&mut buf).await?;
                assert_eq!(buf, data[..valid]);
                let pos = reader.stream_position().await?;
                assert_eq!(pos, valid as u64);
            }
            if size > 1024 {
                // read the part we don't have - should immediately return an error
                {
                    let mut reader = blobs.reader(hash);
                    let mut rest = vec![0u8; size - 1024];
                    reader.seek(SeekFrom::Start(1024)).await?;
                    let res = reader.read_exact(&mut rest).await;
                    assert!(res.is_err());
                }
                // read crossing the end of the blob - should return an error despite
                // the first bytes being valid.
                // A read that fails should not update the stream position.
                {
                    let mut reader = blobs.reader(hash);
                    let mut buf = vec![0u8; size];
                    let res = reader.read(&mut buf).await;
                    assert!(res.is_err());
                    let pos = reader.stream_position().await?;
                    assert_eq!(pos, 0);
                }
            }
        }
        Ok(())
    }

    #[tokio::test]
    async fn reader_partial_fs() -> TestResult<()> {
        let testdir = tempfile::tempdir()?;
        let store = FsStore::load(testdir.path().to_owned()).await?;
        reader_partial(store.blobs()).await?;
        Ok(())
    }

    #[tokio::test]
    async fn reader_partial_memory() -> TestResult<()> {
        let store = MemStore::new();
        reader_partial(store.blobs()).await?;
        Ok(())
    }

    #[tokio::test]
    async fn reader_smoke_fs() -> TestResult<()> {
        let testdir = tempfile::tempdir()?;
        let store = FsStore::load(testdir.path().to_owned()).await?;
        reader_smoke(store.blobs()).await?;
        Ok(())
    }

    #[tokio::test]
    async fn reader_smoke_memory() -> TestResult<()> {
        let store = MemStore::new();
        reader_smoke(store.blobs()).await?;
        Ok(())
    }
}