(
repository_root: &Path,
path: &Path,
started: Instant,
wait: Duration,
)
| 112 | } |
| 113 | |
| 114 | fn compact_waiting( |
| 115 | repository_root: &Path, |
| 116 | path: &Path, |
| 117 | started: Instant, |
| 118 | wait: Duration, |
| 119 | ) -> Result<CompactDatabaseResponse, Status> { |
| 120 | loop { |
| 121 | match compact_repository(repository_root, path) { |
| 122 | Ok(report) => return Ok(report), |
| 123 | Err(error) |
| 124 | if matches!( |
| 125 | error.downcast_ref::<redb::DatabaseError>(), |
| 126 | Some(redb::DatabaseError::DatabaseAlreadyOpen) |
| 127 | ) || error |
| 128 | .downcast_ref::<atomic_repository::RepositoryError>() |
| 129 | .is_some_and(|error| { |
| 130 | matches!(error, atomic_repository::RepositoryError::DatabaseBusy) |
| 131 | || error.is_lock_contended() |
| 132 | }) => |
| 133 | { |
| 134 | let remaining = wait.saturating_sub(started.elapsed()); |
| 135 | if remaining.is_zero() { |
| 136 | return Err(Status::unavailable(format!( |
| 137 | "database compaction timed out waiting for {}; close other database readers/writers and retry", |
| 138 | path.display() |
| 139 | ))); |
| 140 | } |
| 141 | std::thread::sleep(remaining.min(Duration::from_millis(25))); |
| 142 | } |
| 143 | Err(error) => { |
| 144 | return Err(Status::failed_precondition(format!( |
| 145 | "cannot compact {}: {error:#}", |
| 146 | path.display() |
| 147 | ))) |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | fn compact_repository(root: &Path, path: &Path) -> anyhow::Result<CompactDatabaseResponse> { |
| 154 | // Inspect before creating a lock file or opening redb writable. Then hold |
no test coverage detected