/// This task will flush journal data to the device when there is data that needs flushing, and
/// trigger compactions when short of journal space.  It will return after the terminate method
/// has been called, or an error is encountered with either flushing or compaction.
This task will flush journal data to the device when there is data that needs flushing, and
trigger compactions when short of journal space.  It will return after the terminate method
has been called, or an error is encountered with either flushing or compaction.
This task will flush journal data to the device when there is data that needs flushing and trigger compactions when short of journal space . It will return after the terminate method has been called or an error is encountered with either flushing or compaction .
pub async fn flush_task(&self) {
        // Clean up in case we're dropped.
        struct Defer<'a>(&'a Journal);
        impl Drop for Defer<'_> {
            fn drop(&mut self) {
                let mut inner = self.0.inner.lock().unwrap();
                inner.terminate = true;
                inner.compaction_running = false;
                if let Some(waker) = inner.sync_waker.take() {
                    waker.wake();
                }
                inner.reclaim_event = None;
            }
        }
        let _defer = Defer(self);
        let mut flush_fut = None;
        let mut compact_fut = None;
        poll_fn(|ctx| loop {
            {
                let mut inner = self.inner.lock().unwrap();
                if flush_fut.is_none() {
                    if let Some(handle) = self.handle.get() {
                        if let Some((offset, buf)) = inner.writer.take_buffer(handle) {
                            flush_fut = Some(self.flush(offset, buf).boxed());
                        }
                    }
                }
                if inner.terminate && flush_fut.is_none() && compact_fut.is_none() {
                    return Poll::Ready(());
                }
                // The / 2 is here because after compacting, we cannot reclaim the space until the
                // _next_ time we flush the device since the super-block is not guaranteed to
                // persist until then.
                if compact_fut.is_none()
                    && !inner.terminate
                    && !inner.disable_compactions
                    && self.objects.last_end_offset()
                        - inner.super_block.journal_checkpoint.file_offset
                        > RECLAIM_SIZE / 2
                {
                    compact_fut = Some(self.compact().boxed());
                    inner.compaction_running = true;
                }
                inner.flush_waker = Some(ctx.waker().clone());
            }
            let mut pending = true;
            if let Some(fut) = flush_fut.as_mut() {
                if let Poll::Ready(result) = fut.poll_unpin(ctx) {
                    if let Err(e) = result {
                        log::info!("Flush error: {:?}", e);
                        return Poll::Ready(());
                    }
                    flush_fut = None;
                    pending = false;
                }
            }
            if let Some(fut) = compact_fut.as_mut() {
                if let Poll::Ready(result) = fut.poll_unpin(ctx) {
                    if let Err(e) = result {
                        log::info!("Compaction error: {:?}", e);
                        return Poll::Ready(());
                    }
                    compact_fut = None;
                    let mut inner = self.inner.lock().unwrap();
                    inner.compaction_running = false;
                    inner.reclaim_event = None;
                    pending = false;
                }
            }
            if pending {
                return Poll::Pending;
            }
        })
        .await;
    }
pub async fn flush_task ( & self ) { // Clean up in case we're dropped. struct Defer < ' a > ( & ' a Journal ) ; impl Drop for Defer < ' _ > { fn drop ( & mut self ) { let mut inner = self . 0 . inner . lock ( ) . unwrap ( ) ; inner . terminate = true ; inner . compaction_running = false ; if let Some ( waker ) = inner . sync_waker . take ( ) { waker . wake ( ) ; } inner . reclaim_event = None ; } } let _defer = Defer ( self ) ; let mut flush_fut = None ; let mut compact_fut = None ; poll_fn ( | ctx | loop { { let mut inner = self . inner . lock ( ) . unwrap ( ) ; if flush_fut . is_none ( ) { if let Some ( handle ) = self . handle . get ( ) { if let Some ( ( offset , buf ) ) = inner . writer . take_buffer ( handle ) { flush_fut = Some ( self . flush ( offset , buf ) . boxed ( ) ) ; } } } if inner . terminate && flush_fut . is_none ( ) && compact_fut . is_none ( ) { return Poll :: Ready ( ( ) ) ; } // The / 2 is here because after compacting, we cannot reclaim the space until the // _next_ time we flush the device since the super-block is not guaranteed to // persist until then. if compact_fut . is_none ( ) && ! inner . terminate && ! inner . disable_compactions && self . objects . last_end_offset ( ) - inner . super_block . journal_checkpoint . file_offset > RECLAIM_SIZE / 2 { compact_fut = Some ( self . compact ( ) . boxed ( ) ) ; inner . compaction_running = true ; } inner . flush_waker = Some ( ctx . waker ( ) . clone ( ) ) ; } let mut pending = true ; if let Some ( fut ) = flush_fut . as_mut ( ) { if let Poll :: Ready ( result ) = fut . poll_unpin ( ctx ) { if let Err ( e ) = result { log :: info ! ( " " e ) ; return Poll :: Ready ( ( ) ) ; } flush_fut = None ; pending = false ; } } if let Some ( fut ) = compact_fut . as_mut ( ) { if let Poll :: Ready ( result ) = fut . poll_unpin ( ctx ) { if let Err ( e ) = result { log :: info ! ( " " e ) ; return Poll :: Ready ( ( ) ) ; } compact_fut = None ; let mut inner = self . inner . lock ( ) . unwrap ( ) ; inner . compaction_running = false ; inner . reclaim_event = None ; pending = false ; } } if pending { return Poll :: Pending ; } } ) . await ; }
This task will flush journal data to the device when there is data that needs flushing, and
trigger compactions when short of journal space.
This task will flush journal data to the device when there is data that needs flushing and trigger compactions when short of journal space .
&self