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
use std::cmp;
use std::fmt;
use std::future::Future;
use std::io::Read;
use std::io;
use std::marker::PhantomData;
use std::mem;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::vec::IntoIter;

use blocking_permit::{
    blocking_permit_future, SyncBlockingPermitFuture,
    dispatch_rx, Dispatched,
    Cleaver, Splittable, YieldStream,
};
use bytes::{Buf, BufMut, Bytes, BytesMut};
use futures_core::stream::Stream;
use olio::fs::rc::ReadSlice;
use tao_log::{debug, warn};

use body_image::{BodyImage, ExplodedImage};

use crate::{
    Blocking, BlockingArbiter, LenientArbiter, StatefulArbiter,
    FutioTunables, StreamWrapper, UniBodyBuf
};

#[cfg(feature = "mmap")]
use {
    memmap::Mmap,
    olio::mem::{MemAdvice, MemHandle},

    body_image::_mem_handle_ext::MemHandleExt,

    crate::MemMapBuf,
};

/// Trait qualifying `Stream` Item-type buffer requirments.
pub trait OutputBuf: Buf + 'static + From<Bytes> + Send + Sync + Unpin {
    /// Convert from a `MemHandle<Mmap>`
    #[cfg(feature = "mmap")]
    fn from_mmap(mmap: MemHandle<Mmap>) -> Result<Self, io::Error>;
}

impl OutputBuf for Bytes {
    #[cfg(feature = "mmap")]
    fn from_mmap(mmap: MemHandle<Mmap>) -> Result<Self, io::Error> {
        match mmap.tmp_advise(
            MemAdvice::Sequential,
            || Ok(Bytes::copy_from_slice(&mmap[..])))
        {
            Ok(b) => {
                debug!("MemMap copied to Bytes (len: {})", b.len());
                Ok(b)
            }
            Err(e) => Err(e),
        }

    }
}

impl OutputBuf for UniBodyBuf {
    #[cfg(feature = "mmap")]
    fn from_mmap(mmap: MemHandle<Mmap>) -> Result<Self, io::Error> {
        let buf = MemMapBuf::new(mmap);
        buf.advise_sequential()?;
        let _b = buf.chunk()[0];
        debug!("MemMap prepared for sequential read (len: {})", buf.remaining());
        Ok(UniBodyBuf::from_mmap(buf))
    }

}

/// Adaptor for `BodyImage`, implementing the `futures::Stream` and
/// `http_body::Body` traits.
///
/// The `http_body::Body` trait makes this usable with hyper as the `B`
/// body type of `http::Request<B>` (client) or `http::Response<B>`
/// (server).
///
/// The stream `Item` type is an `OutputBuf`, implemented here for `Bytes` or
/// [`UniBodyBuf`](crate::UniBodyBuf). The later provides zero-copy `MemMap`
/// support (*mmap feature only).
///
/// `Tunables::buffer_size_fs` is used for reading the body when in `FsRead`
/// state.
///
/// See also [`YieldBodyImage`], [`SplitBodyImage`], [`DispatchBodyImage`] and
/// [`PermitBodyImage`] which provide additional coordination of blocking
/// operations.
#[must_use = "streams do nothing unless polled"]
#[derive(Debug)]
pub struct AsyncBodyImage<B, BA=LenientArbiter>
    where B: OutputBuf,
          BA: BlockingArbiter + Default + Unpin
{
    state: AsyncBodyState,
    len: u64,
    consumed: u64,
    tune: FutioTunables,
    arbiter: BA,
    buf_type: PhantomData<fn() -> B>,
}

impl<B, BA> AsyncBodyImage<B, BA>
    where B: OutputBuf,
          BA: BlockingArbiter + Default + Unpin
{
    /// Wrap by consuming the `BodyImage` instance.
    ///
    /// *Note*: `BodyImage` and `FutioTunables` are `Clone` (inexpensive), so
    /// that can be done beforehand to preserve owned copies.
    pub fn new(body: BodyImage, tune: FutioTunables) -> Self {
        let len = body.len();
        match body.explode() {
            ExplodedImage::Ram(v) => {
                AsyncBodyImage {
                    state: AsyncBodyState::Ram(v.into_iter()),
                    len,
                    consumed: 0,
                    tune,
                    arbiter: BA::default(),
                    buf_type: PhantomData,
                }
            }
            ExplodedImage::FsRead(rs) => {
                AsyncBodyImage {
                    state: AsyncBodyState::File(rs),
                    len,
                    consumed: 0,
                    tune,
                    arbiter: BA::default(),
                    buf_type: PhantomData,
                }
            }
            #[cfg(feature = "mmap")]
            ExplodedImage::MemMap(mmap) => {
                AsyncBodyImage {
                    state: AsyncBodyState::MemMap(Some(mmap)),
                    len,
                    consumed: 0,
                    tune,
                    arbiter: BA::default(),
                    buf_type: PhantomData,
                }
            }
        }
    }

    fn poll_impl(&mut self) -> Poll<Option<<Self as Stream>::Item>> {
        let avail = self.len - self.consumed;
        if avail == 0 {
            return Poll::Ready(None);
        }

        // All states besides Ram require blocking
        match self.state {
            AsyncBodyState::Ram(_) => {}
            _ => {
                if !self.arbiter.can_block() {
                    return Poll::Pending;
                }
            }
        }

        match self.state {
            AsyncBodyState::Ram(ref mut iter) => {
                match iter.next() {
                    Some(b) => {
                        self.consumed += b.len() as u64;
                        Poll::Ready(Some(Ok(b.into())))
                    }
                    None => Poll::Ready(None),
                }
            }
            AsyncBodyState::File(ref mut rs) => {
                let rlen = cmp::min(
                    self.tune.image().buffer_size_fs() as u64,
                    avail
                ) as usize;
                let mut buf = BytesMut::with_capacity(rlen);
                let b = unsafe { &mut *(
                    buf.chunk_mut() as *mut _
                        as *mut [mem::MaybeUninit<u8>]
                        as *mut [u8]
                )};
                loop {
                    match rs.read(&mut b[..rlen]) {
                        Ok(0) => break Poll::Ready(None),
                        Ok(len) => {
                            unsafe { buf.advance_mut(len); }
                            debug!("read chunk (len: {})", len);
                            self.consumed += len as u64;
                            break Poll::Ready(Some(Ok(buf.freeze().into())));
                        }
                        Err(e) => {
                            if e.kind() == io::ErrorKind::Interrupted {
                                warn!("AsyncBodyImage: read interrupted");
                            } else {
                                break Poll::Ready(Some(Err(e)));
                            }
                        }
                    }
                }
            }
            #[cfg(feature = "mmap")]
            AsyncBodyState::MemMap(ref mut ob) => {
                if let Some(mb) = ob.take() {
                    match B::from_mmap(mb) {
                        Ok(buf) => {
                            self.consumed += buf.remaining() as u64;
                            Poll::Ready(Some(Ok(buf)))
                        }
                        Err(e) => Poll::Ready(Some(Err(e))),
                    }
                } else {
                    Poll::Ready(None)
                }
            }
        }
    }
}

impl<B, BA> StreamWrapper for AsyncBodyImage<B, BA>
    where B: OutputBuf,
          BA: BlockingArbiter + Default + Unpin
{
    fn new(body: BodyImage, tune: FutioTunables) -> Self {
        AsyncBodyImage::new(body, tune)
    }
}

/// Extends [`AsyncBodyImage`] by splitting buffers and yielding.
///
/// Extends [`AsyncBodyImage`] by splitting stream item buffers to a maximum
/// [`FutioTunables::stream_item_size`] *and* yielding after each each
/// item. This may be effective when the underlying `AsyncBodyImage` contains a
/// vary large contiguous memory region, e.g. after it was gathered or memory
/// mapped, which could cause a large delay when subsequently processed.
pub type SplitBodyImage<B> =
    YieldStream<Cleaver<B, io::Error, AsyncBodyImage<B>>,
                Result<B, io::Error>>;

impl<B> StreamWrapper for SplitBodyImage<B>
    where B: OutputBuf + Splittable
{
    fn new(body: BodyImage, tune: FutioTunables) -> Self {
        let max = tune.stream_item_size();
        YieldStream::new(
            Cleaver::new(AsyncBodyImage::new(body, tune), max)
        )
    }
}

/// Extends [`AsyncBodyImage`] by periodically yielding.
///
/// Extends [`AsyncBodyImage`] by always yielding from `Stream::poll_next` (by
/// returning `Poll::Pending`) immediately after it has returned
/// `Poll::Ready(Some(_))`. This may be effective in some settings since the
/// underlying `AsyncBodyImage` may use blocking reads (directly, without other
/// coordination) and some use cases (e.g. `Stream::fold`) do not otherwise
/// yield.
pub type YieldBodyImage<B> =
    YieldStream<AsyncBodyImage<B>, Result<B, io::Error>>;

impl<B> StreamWrapper for YieldBodyImage<B>
    where B: OutputBuf
{
    fn new(body: BodyImage, tune: FutioTunables) -> Self {
        YieldStream::new(AsyncBodyImage::new(body, tune))
    }
}

enum AsyncBodyState {
    Ram(IntoIter<Bytes>),
    File(ReadSlice),
    #[cfg(feature = "mmap")]
    MemMap(Option<MemHandle<Mmap>>),
}

impl fmt::Debug for AsyncBodyState {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {
            AsyncBodyState::Ram(_) => {
                // Avoids showing all buffers as u8 lists
                write!(f, "Ram(IntoIter<Bytes>)")
            }
            AsyncBodyState::File(ref rs) => {
                f.debug_struct("File")
                    .field("rs", rs)
                    .finish()
            }
            #[cfg(feature = "mmap")]
            AsyncBodyState::MemMap(ref ob) => {
                f.debug_tuple("MemMap")
                    .field(ob)
                    .finish()
            }
        }
    }
}

impl<B, BA> Stream for AsyncBodyImage<B, BA>
    where B: OutputBuf,
          BA: BlockingArbiter + Default + Unpin
{
    type Item = Result<B, io::Error>;

    fn poll_next(self: Pin<&mut Self>, _cx: &mut Context<'_>)
        -> Poll<Option<Self::Item>>
    {
        self.get_mut().poll_impl()
    }
}

impl<B, BA> http_body::Body for AsyncBodyImage<B, BA>
    where B: OutputBuf,
          BA: BlockingArbiter + Default + Unpin
{
    type Data = B;
    type Error = io::Error;

    fn poll_data(self: Pin<&mut Self>, cx: &mut Context<'_>)
        -> Poll<Option<Result<Self::Data, Self::Error>>>
    {
        self.poll_next(cx)
    }

    fn poll_trailers(self: Pin<&mut Self>, _cx: &mut Context<'_>)
        -> Poll<Result<Option<http::HeaderMap>, Self::Error>>
    {
        Poll::Ready(Ok(None))
    }

    fn size_hint(&self) -> http_body::SizeHint {
        http_body::SizeHint::with_exact(self.len)
    }

    fn is_end_stream(&self) -> bool {
        self.consumed >= self.len
    }
}

/// Extends [`AsyncBodyImage`] by acquiring a blocking permit before performing
/// any blocking file read operations.
///
/// The total number of concurrent blocking operations is constrained by the
/// `Semaphore` referenced in
/// [`BlockingPolicy::Permit`](crate::BlockingPolicy::Permit) from
/// [`FutioTunables::blocking_policy`], which is required.
#[must_use = "streams do nothing unless polled"]
#[derive(Debug)]
pub struct PermitBodyImage<B>
    where B: OutputBuf
{
    image: AsyncBodyImage<B, StatefulArbiter>,
    permit: Option<SyncBlockingPermitFuture<'static>>
}

impl<B> StreamWrapper for PermitBodyImage<B>
    where B: OutputBuf
{
    fn new(body: BodyImage, tune: FutioTunables) -> Self {
        PermitBodyImage {
            image: AsyncBodyImage::new(body, tune),
            permit: None
        }
    }
}

impl<B> Stream for PermitBodyImage<B>
    where B: OutputBuf
{
    type Item = Result<B, io::Error>;

    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>)
        -> Poll<Option<Self::Item>>
    {
        let this = self.get_mut();
        let permit = if let Some(ref mut pf) = this.permit {
            let pf = Pin::new(pf);
            match pf.poll(cx) {
                Poll::Pending => return Poll::Pending,
                Poll::Ready(Ok(p)) => {
                    this.permit = None;
                    Some(p)
                }
                Poll::Ready(Err(e)) => return Poll::Ready(Some(Err(e.into()))),
            }
        } else {
            None
        };

        if let Some(p) = permit {
            this.image.arbiter.set(Blocking::Once);
            let image = Pin::new(&mut this.image);
            let res = p.run(|| image.poll_next(cx));
            debug_assert_eq!(this.image.arbiter.state(), Blocking::Void);
            res
        } else {
            let res = {
                let image = Pin::new(&mut this.image);
                image.poll_next(cx)
            };

            if res.is_pending()
                && this.image.arbiter.state() == Blocking::Pending
            {
                this.permit = Some(blocking_permit_future(
                    this.image.tune.blocking_semaphore()
                        .expect("blocking semaphore required!")
                ).make_sync());

                // Recurse for correct waking
                return Pin::new(this).poll_next(cx);
            }
            res
        }
    }
}

impl<B> http_body::Body for PermitBodyImage<B>
    where B: OutputBuf,
{
    type Data = B;
    type Error = io::Error;

    fn poll_data(self: Pin<&mut Self>, cx: &mut Context<'_>)
        -> Poll<Option<Result<Self::Data, Self::Error>>>
    {
        self.poll_next(cx)
    }

    fn poll_trailers(self: Pin<&mut Self>, _cx: &mut Context<'_>)
        -> Poll<Result<Option<http::HeaderMap>, Self::Error>>
    {
        Poll::Ready(Ok(None))
    }

    fn size_hint(&self) -> http_body::SizeHint {
        http_body::Body::size_hint(&self.image)
    }

    fn is_end_stream(&self) -> bool {
        http_body::Body::is_end_stream(&self.image)
    }
}

/// Extends [`AsyncBodyImage`] by further dispatching any blocking file read
/// operations to a [`blocking_permit::DispatchPool`] registered with the
/// current thread.
///
/// The implementation will panic if a `DispatchPool` is not registered. Note
/// that each instance will have, at most, 1 pending dispatched read operation
/// `Future`, that it drives to completion before making new reads.
#[must_use = "streams do nothing unless polled"]
#[derive(Debug)]
pub struct DispatchBodyImage<B>
    where B: OutputBuf
{
    state: DispatchState<B>,
    len: u64,
}

type DispatchReturn<B> = (
    Poll<Option<Result<B, io::Error>>>,
    AsyncBodyImage<B, StatefulArbiter>
);

#[derive(Debug)]
enum DispatchState<B>
    where B: OutputBuf
{
    Image(Option<AsyncBodyImage<B, StatefulArbiter>>),
    Dispatch(Dispatched<DispatchReturn<B>>),
}

impl<B> StreamWrapper for DispatchBodyImage<B>
    where B: OutputBuf
{
    fn new(body: BodyImage, tune: FutioTunables) -> Self {
        let len = body.len();
        DispatchBodyImage {
            state: DispatchState::Image(Some(AsyncBodyImage::new(body, tune))),
            len,
        }
    }
}

impl<B> Stream for DispatchBodyImage<B>
    where B: OutputBuf
{
    type Item = Result<B, io::Error>;

    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>)
        -> Poll<Option<Self::Item>>
    {
        let this = self.get_mut();
        match this.state {
            DispatchState::Image(ref mut obo) => {
                let ob = obo.as_mut().unwrap();
                let res = ob.poll_impl();
                if res.is_pending() && ob.arbiter.state() == Blocking::Pending {
                    ob.arbiter.set(Blocking::Once);
                    let mut ob = obo.take().unwrap();
                    this.state = DispatchState::Dispatch(dispatch_rx(move || {
                        (ob.poll_impl(), ob)
                    }).unwrap());

                    // Recurse for correct waking
                    return Pin::new(this).poll_next(cx);
                }
                res
            }
            DispatchState::Dispatch(ref mut db) => {
                let (res, ob) = match Pin::new(&mut *db).poll(cx) {
                    Poll::Pending => return Poll::Pending,
                    Poll::Ready(Err(e)) => return Poll::Ready(Some(Err(e.into()))),
                    Poll::Ready(Ok((res, ob))) => (res, ob),
                };
                debug_assert_eq!(ob.arbiter.state(), Blocking::Void);
                this.state = DispatchState::Image(Some(ob));
                res
            }
        }
    }
}

impl<B> http_body::Body for DispatchBodyImage<B>
    where B: OutputBuf,
{
    type Data = B;
    type Error = io::Error;

    fn poll_data(self: Pin<&mut Self>, cx: &mut Context<'_>)
        -> Poll<Option<Result<Self::Data, Self::Error>>>
    {
        self.poll_next(cx)
    }

    fn poll_trailers(self: Pin<&mut Self>, _cx: &mut Context<'_>)
        -> Poll<Result<Option<http::HeaderMap>, Self::Error>>
    {
        Poll::Ready(Ok(None))
    }

    fn size_hint(&self) -> http_body::SizeHint {
        http_body::SizeHint::with_exact(self.len)
    }

    fn is_end_stream(&self) -> bool {
        match self.state {
            DispatchState::Image(ref obi) => {
                http_body::Body::is_end_stream(obi.as_ref().unwrap())
            }
            DispatchState::Dispatch(_) => {
                false
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn is_send<T: Send>() -> bool { true }
    fn is_sync<T: Sync>() -> bool { true }

    #[test]
    fn test_send_sync() {
        // In order for AsyncBodyImage to work with hyper::Body::wrap_stream,
        // it must be both Sync and Send
        assert!(is_send::<AsyncBodyImage<Bytes>>());
        assert!(is_sync::<AsyncBodyImage<Bytes>>());

        assert!(is_send::<DispatchBodyImage<Bytes>>());
        assert!(is_sync::<DispatchBodyImage<Bytes>>());

        assert!(is_send::<PermitBodyImage<Bytes>>());
        assert!(is_sync::<PermitBodyImage<Bytes>>());
    }
}