| 1 | #![forbid(unsafe_code)] |
| 2 | |
| 3 | use std::sync::{ |
| 4 | Arc, |
| 5 | atomic::{AtomicBool, AtomicU64, Ordering}, |
| 6 | }; |
| 7 | |
| 8 | pub use {gzip, ustar, zip_create}; |
| 9 | |
| 10 | static NEXT_OPERATION: AtomicU64 = AtomicU64::new(1); |
| 11 | |
| 12 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] |
| 13 | pub struct OperationId(u64); |
| 14 | |
| 15 | impl OperationId { |
| 16 | pub fn value(self) -> u64 { |
| 17 | self.0 |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 22 | pub enum Policy { |
| 23 | GzipSingleMemberV1, |
| 24 | UstarLinuxV1, |
| 25 | ZipAsciiCreateV1, |
| 26 | } |
| 27 | |
| 28 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 29 | pub enum AdapterFailure { |
| 30 | OperationIdExhausted, |
| 31 | SequenceExhausted, |
| 32 | } |
| 33 | |
| 34 | #[derive(Clone, Debug)] |
| 35 | pub struct Cancellation { |
| 36 | operation: OperationId, |
| 37 | requested: Arc<AtomicBool>, |
| 38 | } |
| 39 | |
| 40 | impl Cancellation { |
| 41 | pub fn operation(&self) -> OperationId { |
| 42 | self.operation |
| 43 | } |
| 44 | pub fn request(&self) -> bool { |
| 45 | !self.requested.swap(true, Ordering::SeqCst) |
| 46 | } |
| 47 | pub fn is_requested(&self) -> bool { |
| 48 | self.requested.load(Ordering::SeqCst) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 53 | pub struct Stamp { |
| 54 | operation: OperationId, |
| 55 | sequence: u64, |
| 56 | } |
| 57 | |
| 58 | impl Stamp { |
| 59 | pub fn operation(self) -> OperationId { |
| 60 | self.operation |
| 61 | } |
| 62 | pub fn sequence(self) -> u64 { |
| 63 | self.sequence |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 68 | pub enum Metric { |
| 69 | GzipCumulativeProcessedBytes, |
| 70 | UstarInputOffset { pass: u8 }, |
| 71 | UstarEntryIndex { pass: u8 }, |
| 72 | ZipInputOffset { entry: usize }, |
| 73 | ZipOutputExtent, |
| 74 | } |
| 75 | |
| 76 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 77 | pub struct Progress { |
| 78 | pub metric: Metric, |
| 79 | pub value: u64, |
| 80 | pub total: Option<u64>, |
| 81 | } |
| 82 | |
| 83 | #[derive(Debug)] |
| 84 | pub struct Event<C> { |
| 85 | stamp: Stamp, |
| 86 | pub policy: Policy, |
| 87 | pub checkpoint: C, |
| 88 | pub progress: Option<Progress>, |
| 89 | } |
| 90 | |
| 91 | impl<C> Event<C> { |
| 92 | pub fn stamp(&self) -> Stamp { |
| 93 | self.stamp |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | #[derive(Debug)] |
| 98 | pub struct MemoryValidated<T> { |
| 99 | policy: Policy, |
| 100 | value: T, |
| 101 | } |
| 102 | |
| 103 | impl<T> MemoryValidated<T> { |
| 104 | pub fn policy(&self) -> Policy { |
| 105 | self.policy |
| 106 | } |
| 107 | pub fn value(&self) -> &T { |
| 108 | &self.value |
| 109 | } |
| 110 | pub fn into_inner(self) -> T { |
| 111 | self.value |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | #[derive(Debug)] |
| 116 | pub struct Failure<E> { |
| 117 | pub native: Option<E>, |
| 118 | pub adapter: Option<AdapterFailure>, |
| 119 | } |
| 120 | |
| 121 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 122 | pub enum ProcessCleanup { |
| 123 | NotApplicable, |
| 124 | } |
| 125 | |
| 126 | #[derive(Debug)] |
| 127 | pub struct Terminal<T, E> { |
| 128 | stamp: Stamp, |
| 129 | policy: Policy, |
| 130 | cancellation_requested: bool, |
| 131 | cancellation_forwarded: bool, |
| 132 | result: Result<MemoryValidated<T>, Failure<E>>, |
| 133 | } |
| 134 | |
| 135 | impl<T, E> Terminal<T, E> { |
| 136 | pub fn stamp(&self) -> Stamp { |
| 137 | self.stamp |
| 138 | } |
| 139 | pub fn policy(&self) -> Policy { |
| 140 | self.policy |
| 141 | } |
| 142 | pub fn cancellation_requested(&self) -> bool { |
| 143 | self.cancellation_requested |
| 144 | } |
| 145 | pub fn cancellation_forwarded(&self) -> bool { |
| 146 | self.cancellation_forwarded |
| 147 | } |
| 148 | pub fn process_cleanup(&self) -> ProcessCleanup { |
| 149 | ProcessCleanup::NotApplicable |
| 150 | } |
| 151 | pub fn result(&self) -> Result<&MemoryValidated<T>, &Failure<E>> { |
| 152 | self.result.as_ref() |
| 153 | } |
| 154 | pub fn into_result(self) -> Result<MemoryValidated<T>, Failure<E>> { |
| 155 | self.result |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | pub struct Operation { |
| 160 | cancellation: Cancellation, |
| 161 | next_sequence: u64, |
| 162 | fault: Option<AdapterFailure>, |
| 163 | cancellation_forwarded: bool, |
| 164 | } |
| 165 | |
| 166 | impl Operation { |
| 167 | pub fn start() -> Result<(Self, Cancellation), AdapterFailure> { |
| 168 | let value = NEXT_OPERATION |
| 169 | .try_update(Ordering::SeqCst, Ordering::SeqCst, |v| v.checked_add(1)) |
| 170 | .map_err(|_| AdapterFailure::OperationIdExhausted)?; |
| 171 | let cancellation = Cancellation { |
| 172 | operation: OperationId(value), |
| 173 | requested: Arc::new(AtomicBool::new(false)), |
| 174 | }; |
| 175 | Ok(( |
| 176 | Self { |
| 177 | cancellation: cancellation.clone(), |
| 178 | next_sequence: 0, |
| 179 | fault: None, |
| 180 | cancellation_forwarded: false, |
| 181 | }, |
| 182 | cancellation, |
| 183 | )) |
| 184 | } |
| 185 | |
| 186 | pub fn id(&self) -> OperationId { |
| 187 | self.cancellation.operation |
| 188 | } |
| 189 | |
| 190 | fn event<C>( |
| 191 | &mut self, |
| 192 | policy: Policy, |
| 193 | checkpoint: C, |
| 194 | progress: Option<Progress>, |
| 195 | ) -> Option<Event<C>> { |
| 196 | // 마지막 순번은 terminal 반환에 남겨 둠. |
| 197 | let Some(next) = self.next_sequence.checked_add(1) else { |
| 198 | self.fault = Some(AdapterFailure::SequenceExhausted); |
| 199 | return None; |
| 200 | }; |
| 201 | let stamp = Stamp { |
| 202 | operation: self.id(), |
| 203 | sequence: self.next_sequence, |
| 204 | }; |
| 205 | self.next_sequence = next; |
| 206 | Some(Event { |
| 207 | stamp, |
| 208 | policy, |
| 209 | checkpoint, |
| 210 | progress, |
| 211 | }) |
| 212 | } |
| 213 | |
| 214 | fn cancelled(&mut self) -> bool { |
| 215 | if self.cancellation.is_requested() { |
| 216 | self.cancellation_forwarded = true; |
| 217 | true |
| 218 | } else { |
| 219 | false |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | fn finish<T, E>(self, policy: Policy, result: Result<T, E>) -> Terminal<T, E> { |
| 224 | let result = match (result, self.fault) { |
| 225 | (Ok(value), None) => Ok(MemoryValidated { policy, value }), |
| 226 | (Ok(_), Some(fault)) => Err(Failure { |
| 227 | native: None, |
| 228 | adapter: Some(fault), |
| 229 | }), |
| 230 | (Err(error), fault) => Err(Failure { |
| 231 | native: Some(error), |
| 232 | adapter: fault, |
| 233 | }), |
| 234 | }; |
| 235 | Terminal { |
| 236 | stamp: Stamp { |
| 237 | operation: self.id(), |
| 238 | sequence: self.next_sequence, |
| 239 | }, |
| 240 | policy, |
| 241 | cancellation_requested: self.cancellation.is_requested(), |
| 242 | cancellation_forwarded: self.cancellation_forwarded, |
| 243 | result, |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | pub fn gzip<E>( |
| 248 | mut self, |
| 249 | request: gzip::Request, |
| 250 | budget: &mut gzip::Budget, |
| 251 | mut observe: impl FnMut(Event<gzip::Checkpoint>) -> Result<(), gzip::Stop<E>>, |
| 252 | ) -> Terminal<gzip::Completed, gzip::Error<E>> { |
| 253 | let policy = Policy::GzipSingleMemberV1; |
| 254 | let result = gzip::read(request, budget, |checkpoint| { |
| 255 | let progress = Some(Progress { |
| 256 | metric: Metric::GzipCumulativeProcessedBytes, |
| 257 | value: checkpoint.usage.processed, |
| 258 | total: None, |
| 259 | }); |
| 260 | let event = self |
| 261 | .event(policy, checkpoint, progress) |
| 262 | .ok_or(gzip::Stop::Cancelled)?; |
| 263 | observe(event)?; |
| 264 | if self.cancelled() { |
| 265 | Err(gzip::Stop::Cancelled) |
| 266 | } else { |
| 267 | Ok(()) |
| 268 | } |
| 269 | }); |
| 270 | self.finish(policy, result) |
| 271 | } |
| 272 | |
| 273 | pub fn ustar<E>( |
| 274 | mut self, |
| 275 | request: ustar::Request, |
| 276 | select: impl FnOnce(&[ustar::Entry]) -> Result<Vec<String>, E>, |
| 277 | mut observe: impl FnMut(Event<ustar::Checkpoint>) -> Result<(), ustar::Stop>, |
| 278 | ) -> Terminal<ustar::Completed, ustar::Error<E>> { |
| 279 | let policy = Policy::UstarLinuxV1; |
| 280 | let result = ustar::read(request, select, |checkpoint| { |
| 281 | let progress = match checkpoint { |
| 282 | ustar::Checkpoint::Read { pass, offset } => Some(Progress { |
| 283 | metric: Metric::UstarInputOffset { pass }, |
| 284 | value: offset as u64, |
| 285 | total: None, |
| 286 | }), |
| 287 | ustar::Checkpoint::Entry { pass, index } => Some(Progress { |
| 288 | metric: Metric::UstarEntryIndex { pass }, |
| 289 | value: index as u64, |
| 290 | total: None, |
| 291 | }), |
| 292 | _ => None, |
| 293 | }; |
| 294 | let event = self |
| 295 | .event(policy, checkpoint, progress) |
| 296 | .ok_or(ustar::Stop::Cancelled)?; |
| 297 | observe(event)?; |
| 298 | if self.cancelled() { |
| 299 | Err(ustar::Stop::Cancelled) |
| 300 | } else { |
| 301 | Ok(()) |
| 302 | } |
| 303 | }); |
| 304 | self.finish(policy, result) |
| 305 | } |
| 306 | |
| 307 | pub fn create_zip( |
| 308 | mut self, |
| 309 | request: zip_create::Request, |
| 310 | mut observe: impl FnMut(Event<zip_create::Checkpoint>) -> Result<(), zip_create::Stop>, |
| 311 | ) -> Terminal<Vec<u8>, zip_create::Failure> { |
| 312 | let policy = Policy::ZipAsciiCreateV1; |
| 313 | let result = zip_create::create(request, |checkpoint| { |
| 314 | let progress = match checkpoint { |
| 315 | zip_create::Checkpoint::Input { index, offset, .. } => Some(Progress { |
| 316 | metric: Metric::ZipInputOffset { entry: index }, |
| 317 | value: offset as u64, |
| 318 | total: None, |
| 319 | }), |
| 320 | zip_create::Checkpoint::Write { extent, .. } => Some(Progress { |
| 321 | metric: Metric::ZipOutputExtent, |
| 322 | value: extent, |
| 323 | total: None, |
| 324 | }), |
| 325 | _ => None, |
| 326 | }; |
| 327 | let event = self |
| 328 | .event(policy, checkpoint, progress) |
| 329 | .ok_or(zip_create::Stop::Cancelled)?; |
| 330 | observe(event)?; |
| 331 | if self.cancelled() { |
| 332 | Err(zip_create::Stop::Cancelled) |
| 333 | } else { |
| 334 | Ok(()) |
| 335 | } |
| 336 | }); |
| 337 | self.finish(policy, result) |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 342 | pub enum Delivery { |
| 343 | Accepted, |
| 344 | IgnoredStale, |
| 345 | } |
| 346 | |
| 347 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 348 | pub enum DeliveryError { |
| 349 | Sequence { expected: u64, received: u64 }, |
| 350 | AlreadyTerminal, |
| 351 | Faulted, |
| 352 | } |
| 353 | |
| 354 | pub struct Receiver { |
| 355 | operation: OperationId, |
| 356 | next_sequence: u64, |
| 357 | terminal: bool, |
| 358 | faulted: bool, |
| 359 | } |
| 360 | |
| 361 | impl Receiver { |
| 362 | pub fn new(operation: OperationId) -> Self { |
| 363 | Self { |
| 364 | operation, |
| 365 | next_sequence: 0, |
| 366 | terminal: false, |
| 367 | faulted: false, |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | pub fn progress<C>(&mut self, event: &Event<C>) -> Result<Delivery, DeliveryError> { |
| 372 | self.accept(event.stamp, false) |
| 373 | } |
| 374 | |
| 375 | pub fn terminal<T, E>(&mut self, event: &Terminal<T, E>) -> Result<Delivery, DeliveryError> { |
| 376 | self.accept(event.stamp, true) |
| 377 | } |
| 378 | |
| 379 | fn accept(&mut self, stamp: Stamp, terminal: bool) -> Result<Delivery, DeliveryError> { |
| 380 | // 이전 작업 이벤트는 현재 순번이나 종료 상태를 바꾸지 않음. |
| 381 | if stamp.operation != self.operation { |
| 382 | return Ok(Delivery::IgnoredStale); |
| 383 | } |
| 384 | if self.faulted { |
| 385 | return Err(DeliveryError::Faulted); |
| 386 | } |
| 387 | if self.terminal { |
| 388 | return Err(DeliveryError::AlreadyTerminal); |
| 389 | } |
| 390 | if stamp.sequence != self.next_sequence || (!terminal && stamp.sequence == u64::MAX) { |
| 391 | self.faulted = true; |
| 392 | return Err(DeliveryError::Sequence { |
| 393 | expected: self.next_sequence, |
| 394 | received: stamp.sequence, |
| 395 | }); |
| 396 | } |
| 397 | if terminal { |
| 398 | self.terminal = true; |
| 399 | } else { |
| 400 | self.next_sequence = self |
| 401 | .next_sequence |
| 402 | .checked_add(1) |
| 403 | .ok_or(DeliveryError::Faulted)?; |
| 404 | } |
| 405 | Ok(Delivery::Accepted) |
| 406 | } |
| 407 | } |