| 1 | #![forbid(unsafe_code)] |
| 2 | |
| 3 | //! 개발용 단일 GZip 파일 작업을 순서대로 연결함. |
| 4 | //! OS worker 격리나 제품 게시 완료를 증명하지 않음. |
| 5 | |
| 6 | mod types; |
| 7 | pub use types::*; |
| 8 | pub use zipelo_file_publication as publication; |
| 9 | pub use zipelo_file_snapshot as snapshot; |
| 10 | pub use zipelo_memory_gzip_reader as gzip; |
| 11 | |
| 12 | use std::sync::{ |
| 13 | Arc, |
| 14 | atomic::{AtomicBool, Ordering}, |
| 15 | }; |
| 16 | |
| 17 | // native commit gate를 먼저 결정하고 앞 단계의 협력적 취소에 전달함. |
| 18 | #[derive(Clone)] |
| 19 | pub struct Cancellation { |
| 20 | native: publication::Cancellation, |
| 21 | accepted: Arc<AtomicBool>, |
| 22 | } |
| 23 | |
| 24 | impl Cancellation { |
| 25 | pub fn request(&self) -> publication::CancelDecision { |
| 26 | let decision = self.native.request(); |
| 27 | if matches!( |
| 28 | decision, |
| 29 | publication::CancelDecision::Requested | publication::CancelDecision::AlreadyRequested |
| 30 | ) { |
| 31 | self.accepted.store(true, Ordering::SeqCst); |
| 32 | } |
| 33 | decision |
| 34 | } |
| 35 | |
| 36 | fn observe<E>( |
| 37 | &self, |
| 38 | checkpoint: Checkpoint, |
| 39 | observer: &mut impl FnMut(Checkpoint) -> Result<(), Stop<E>>, |
| 40 | ) -> Result<(), Stop<E>> { |
| 41 | // callback 원인을 취소 확인보다 먼저 보존함. |
| 42 | observer(checkpoint)?; |
| 43 | if self.accepted.load(Ordering::SeqCst) { |
| 44 | return Err(Stop::Cancelled); |
| 45 | } |
| 46 | Ok(()) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | pub struct FileJob { |
| 51 | publication: publication::Publication, |
| 52 | cancellation: Cancellation, |
| 53 | } |
| 54 | |
| 55 | impl FileJob { |
| 56 | pub fn new() -> (Self, Cancellation) { |
| 57 | let (publication, native) = publication::Publication::new(); |
| 58 | let cancellation = Cancellation { |
| 59 | native, |
| 60 | accepted: Arc::new(AtomicBool::new(false)), |
| 61 | }; |
| 62 | ( |
| 63 | Self { |
| 64 | publication, |
| 65 | cancellation: cancellation.clone(), |
| 66 | }, |
| 67 | cancellation, |
| 68 | ) |
| 69 | } |
| 70 | |
| 71 | // 한 번 소비하며 실패와 불명확한 게시를 자동 재시도하지 않음. |
| 72 | pub fn run<E>( |
| 73 | self, |
| 74 | request: Request<'_>, |
| 75 | mut observer: impl FnMut(Checkpoint) -> Result<(), Stop<E>>, |
| 76 | ) -> Report<E> { |
| 77 | let mut usage = Usage::default(); |
| 78 | let captured = snapshot::capture( |
| 79 | snapshot::Request { |
| 80 | path: request.input, |
| 81 | limits: request.limits.snapshot, |
| 82 | }, |
| 83 | |checkpoint| { |
| 84 | usage.snapshot = checkpoint.usage; |
| 85 | self.cancellation |
| 86 | .observe( |
| 87 | Checkpoint { |
| 88 | event: Event::Snapshot(checkpoint), |
| 89 | usage, |
| 90 | }, |
| 91 | &mut observer, |
| 92 | ) |
| 93 | .map_err(|stop| match stop { |
| 94 | Stop::Cancelled => snapshot::Stop::Cancelled, |
| 95 | Stop::Callback(error) => snapshot::Stop::Callback(error), |
| 96 | }) |
| 97 | }, |
| 98 | ); |
| 99 | let captured = match captured { |
| 100 | Ok(value) => value, |
| 101 | Err(failure) => { |
| 102 | usage.snapshot = failure.usage; |
| 103 | return Report { |
| 104 | outcome: Outcome::SnapshotFailed(failure), |
| 105 | usage, |
| 106 | snapshot: None, |
| 107 | decoded: None, |
| 108 | }; |
| 109 | } |
| 110 | }; |
| 111 | usage.snapshot = captured.usage(); |
| 112 | let source = SnapshotObservation::from_capture(&captured); |
| 113 | let mut budget = match gzip::Budget::new(request.limits.gzip) { |
| 114 | Ok(value) => value, |
| 115 | Err(failure) => { |
| 116 | return Report { |
| 117 | outcome: Outcome::DecodeFailed(gzip::Error::Reader(failure)), |
| 118 | usage, |
| 119 | snapshot: Some(source), |
| 120 | decoded: None, |
| 121 | }; |
| 122 | } |
| 123 | }; |
| 124 | // 검증된 snapshot의 소유 bytes만 넘기며 원래 pathname을 다시 열지 않음. |
| 125 | let decoded = gzip::read( |
| 126 | gzip::Request { |
| 127 | input: captured.into_bytes().into_boxed_slice(), |
| 128 | selected: true, |
| 129 | }, |
| 130 | &mut budget, |
| 131 | |checkpoint| { |
| 132 | usage.gzip = checkpoint.usage; |
| 133 | self.cancellation |
| 134 | .observe( |
| 135 | Checkpoint { |
| 136 | event: Event::Decode(checkpoint), |
| 137 | usage, |
| 138 | }, |
| 139 | &mut observer, |
| 140 | ) |
| 141 | .map_err(|stop| match stop { |
| 142 | Stop::Cancelled => gzip::Stop::Cancelled, |
| 143 | Stop::Callback(error) => gzip::Stop::Callback(error), |
| 144 | }) |
| 145 | }, |
| 146 | ); |
| 147 | // 마지막 checkpoint 뒤의 실패 과금도 원래 Budget에서 회수함. |
| 148 | usage.gzip = budget.usage(); |
| 149 | let decoded = match decoded { |
| 150 | Ok(value) => value, |
| 151 | Err(error) => { |
| 152 | return Report { |
| 153 | outcome: Outcome::DecodeFailed(error), |
| 154 | usage, |
| 155 | snapshot: Some(source), |
| 156 | decoded: None, |
| 157 | }; |
| 158 | } |
| 159 | }; |
| 160 | let Some(bytes) = decoded.bytes else { |
| 161 | return Report { |
| 162 | outcome: Outcome::AdapterFailed(AdapterFailure::MissingSelectedOutput), |
| 163 | usage, |
| 164 | snapshot: Some(source), |
| 165 | decoded: None, |
| 166 | }; |
| 167 | }; |
| 168 | if bytes.len() as u64 != decoded.boundary.inflated_bytes { |
| 169 | return Report { |
| 170 | outcome: Outcome::AdapterFailed(AdapterFailure::SelectedLengthMismatch { |
| 171 | expected: decoded.boundary.inflated_bytes, |
| 172 | actual: bytes.len() as u64, |
| 173 | }), |
| 174 | usage, |
| 175 | snapshot: Some(source), |
| 176 | decoded: None, |
| 177 | }; |
| 178 | } |
| 179 | let memory = GzipObservation::from_decode(decoded.boundary); |
| 180 | let native = self.publication.publish( |
| 181 | publication::Request { |
| 182 | parent: request.output_parent, |
| 183 | leaf: request.output_leaf, |
| 184 | bytes: &bytes, |
| 185 | limits: request.limits.publication, |
| 186 | }, |
| 187 | |checkpoint| { |
| 188 | usage.publication = checkpoint.usage; |
| 189 | self.cancellation |
| 190 | .observe( |
| 191 | Checkpoint { |
| 192 | event: Event::Publication(checkpoint), |
| 193 | usage, |
| 194 | }, |
| 195 | &mut observer, |
| 196 | ) |
| 197 | .map_err(|stop| match stop { |
| 198 | Stop::Cancelled => publication::Stop::Cancelled, |
| 199 | Stop::Callback(error) => publication::Stop::Callback(error), |
| 200 | }) |
| 201 | }, |
| 202 | ); |
| 203 | usage.publication = native.usage; |
| 204 | // commit 뒤에는 callback, 취소 판정, 재시도와 rollback을 추가하지 않음. |
| 205 | Report { |
| 206 | outcome: Outcome::Publication(native), |
| 207 | usage, |
| 208 | snapshot: Some(source), |
| 209 | decoded: Some(memory), |
| 210 | } |
| 211 | } |
| 212 | } |