Spaces:
Build error
Build error
File size: 18,634 Bytes
d8435ba |
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 585 586 |
use std::path::Path;
use actix_multipart::form::tempfile::TempFile;
use actix_multipart::form::MultipartForm;
use actix_web::{delete, get, post, put, web, Responder, Result};
use actix_web_validator as valid;
use collection::common::file_utils::move_file;
use collection::common::sha_256::{hash_file, hashes_equal};
use collection::common::snapshot_stream::SnapshotStream;
use collection::operations::snapshot_ops::{
ShardSnapshotRecover, SnapshotPriority, SnapshotRecover,
};
use collection::operations::verification::new_unchecked_verification_pass;
use collection::shards::shard::ShardId;
use futures::{FutureExt as _, TryFutureExt as _};
use reqwest::Url;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use storage::content_manager::errors::StorageError;
use storage::content_manager::snapshots::recover::do_recover_from_snapshot;
use storage::content_manager::snapshots::{
do_create_full_snapshot, do_delete_collection_snapshot, do_delete_full_snapshot,
do_list_full_snapshots,
};
use storage::content_manager::toc::TableOfContent;
use storage::dispatcher::Dispatcher;
use storage::rbac::{Access, AccessRequirements};
use uuid::Uuid;
use validator::Validate;
use super::{CollectionPath, StrictCollectionPath};
use crate::actix::auth::ActixAccess;
use crate::actix::helpers::{self, HttpError};
use crate::common;
use crate::common::collections::*;
use crate::common::http_client::HttpClient;
#[derive(Deserialize, Serialize, JsonSchema, Validate)]
pub struct SnapshotUploadingParam {
pub wait: Option<bool>,
pub priority: Option<SnapshotPriority>,
/// Optional SHA256 checksum to verify snapshot integrity before recovery.
#[serde(default)]
#[validate(custom(function = "::common::validation::validate_sha256_hash"))]
pub checksum: Option<String>,
}
#[derive(Deserialize, Serialize, JsonSchema, Validate)]
pub struct SnapshottingParam {
pub wait: Option<bool>,
}
#[derive(MultipartForm)]
pub struct SnapshottingForm {
snapshot: TempFile,
}
// Actix specific code
pub async fn do_get_full_snapshot(
toc: &TableOfContent,
access: Access,
snapshot_name: &str,
) -> Result<SnapshotStream, HttpError> {
access.check_global_access(AccessRequirements::new())?;
let snapshots_storage_manager = toc.get_snapshots_storage_manager()?;
let snapshot_path =
snapshots_storage_manager.get_full_snapshot_path(toc.snapshots_path(), snapshot_name)?;
let snapshot_stream = snapshots_storage_manager
.get_snapshot_stream(&snapshot_path)
.await?;
Ok(snapshot_stream)
}
pub async fn do_save_uploaded_snapshot(
toc: &TableOfContent,
collection_name: &str,
snapshot: TempFile,
) -> Result<Url, StorageError> {
let filename = snapshot
.file_name
// Sanitize the file name:
// - only take the top level path (no directories such as ../)
// - require the file name to be valid UTF-8
.and_then(|x| {
Path::new(&x)
.file_name()
.map(|filename| filename.to_owned())
})
.and_then(|x| x.to_str().map(|x| x.to_owned()))
.unwrap_or_else(|| Uuid::new_v4().to_string());
let collection_snapshot_path = toc.snapshots_path_for_collection(collection_name);
if !collection_snapshot_path.exists() {
log::debug!(
"Creating missing collection snapshots directory for {}",
collection_name
);
toc.create_snapshots_path(collection_name).await?;
}
let path = collection_snapshot_path.join(filename);
move_file(snapshot.file.path(), &path).await?;
let absolute_path = path.canonicalize()?;
let snapshot_location = Url::from_file_path(&absolute_path).map_err(|_| {
StorageError::service_error(format!(
"Failed to convert path to URL: {}",
absolute_path.display()
))
})?;
Ok(snapshot_location)
}
// Actix specific code
pub async fn do_get_snapshot(
toc: &TableOfContent,
access: Access,
collection_name: &str,
snapshot_name: &str,
) -> Result<SnapshotStream, HttpError> {
let collection_pass =
access.check_collection_access(collection_name, AccessRequirements::new().whole())?;
let collection: tokio::sync::RwLockReadGuard<collection::collection::Collection> =
toc.get_collection(&collection_pass).await?;
let snapshot_storage_manager = collection.get_snapshots_storage_manager()?;
let snapshot_path =
snapshot_storage_manager.get_snapshot_path(collection.snapshots_path(), snapshot_name)?;
let snapshot_stream = snapshot_storage_manager
.get_snapshot_stream(&snapshot_path)
.await?;
Ok(snapshot_stream)
}
#[get("/collections/{name}/snapshots")]
async fn list_snapshots(
dispatcher: web::Data<Dispatcher>,
path: web::Path<String>,
ActixAccess(access): ActixAccess,
) -> impl Responder {
// Nothing to verify.
let pass = new_unchecked_verification_pass();
helpers::time(do_list_snapshots(
dispatcher.toc(&access, &pass),
access,
&path,
))
.await
}
#[post("/collections/{name}/snapshots")]
async fn create_snapshot(
dispatcher: web::Data<Dispatcher>,
path: web::Path<String>,
params: valid::Query<SnapshottingParam>,
ActixAccess(access): ActixAccess,
) -> impl Responder {
// Nothing to verify.
let pass = new_unchecked_verification_pass();
let collection_name = path.into_inner();
let future = async move {
do_create_snapshot(
dispatcher.toc(&access, &pass).clone(),
access,
&collection_name,
)
.await
};
helpers::time_or_accept(future, params.wait.unwrap_or(true)).await
}
#[post("/collections/{name}/snapshots/upload")]
async fn upload_snapshot(
dispatcher: web::Data<Dispatcher>,
http_client: web::Data<HttpClient>,
collection: valid::Path<StrictCollectionPath>,
MultipartForm(form): MultipartForm<SnapshottingForm>,
params: valid::Query<SnapshotUploadingParam>,
ActixAccess(access): ActixAccess,
) -> impl Responder {
let wait = params.wait;
// Nothing to verify.
let pass = new_unchecked_verification_pass();
let future = async move {
let snapshot = form.snapshot;
access.check_global_access(AccessRequirements::new().manage())?;
if let Some(checksum) = ¶ms.checksum {
let snapshot_checksum = hash_file(snapshot.file.path()).await?;
if !hashes_equal(snapshot_checksum.as_str(), checksum.as_str()) {
return Err(StorageError::checksum_mismatch(snapshot_checksum, checksum));
}
}
let snapshot_location =
do_save_uploaded_snapshot(dispatcher.toc(&access, &pass), &collection.name, snapshot)
.await?;
// Snapshot is a local file, we do not need an API key for that
let http_client = http_client.client(None)?;
let snapshot_recover = SnapshotRecover {
location: snapshot_location,
priority: params.priority,
checksum: None,
api_key: None,
};
do_recover_from_snapshot(
dispatcher.get_ref(),
&collection.name,
snapshot_recover,
access,
http_client,
)
.await
};
helpers::time_or_accept(future, wait.unwrap_or(true)).await
}
#[put("/collections/{name}/snapshots/recover")]
async fn recover_from_snapshot(
dispatcher: web::Data<Dispatcher>,
http_client: web::Data<HttpClient>,
collection: valid::Path<CollectionPath>,
request: valid::Json<SnapshotRecover>,
params: valid::Query<SnapshottingParam>,
ActixAccess(access): ActixAccess,
) -> impl Responder {
let future = async move {
let snapshot_recover = request.into_inner();
let http_client = http_client.client(snapshot_recover.api_key.as_deref())?;
do_recover_from_snapshot(
dispatcher.get_ref(),
&collection.name,
snapshot_recover,
access,
http_client,
)
.await
};
helpers::time_or_accept(future, params.wait.unwrap_or(true)).await
}
#[get("/collections/{name}/snapshots/{snapshot_name}")]
async fn get_snapshot(
dispatcher: web::Data<Dispatcher>,
path: web::Path<(String, String)>,
ActixAccess(access): ActixAccess,
) -> impl Responder {
// Nothing to verify.
let pass = new_unchecked_verification_pass();
let (collection_name, snapshot_name) = path.into_inner();
do_get_snapshot(
dispatcher.toc(&access, &pass),
access,
&collection_name,
&snapshot_name,
)
.await
}
#[get("/snapshots")]
async fn list_full_snapshots(
dispatcher: web::Data<Dispatcher>,
ActixAccess(access): ActixAccess,
) -> impl Responder {
// nothing to verify.
let pass = new_unchecked_verification_pass();
helpers::time(do_list_full_snapshots(
dispatcher.toc(&access, &pass),
access,
))
.await
}
#[post("/snapshots")]
async fn create_full_snapshot(
dispatcher: web::Data<Dispatcher>,
params: valid::Query<SnapshottingParam>,
ActixAccess(access): ActixAccess,
) -> impl Responder {
let future = async move { do_create_full_snapshot(dispatcher.get_ref(), access).await };
helpers::time_or_accept(future, params.wait.unwrap_or(true)).await
}
#[get("/snapshots/{snapshot_name}")]
async fn get_full_snapshot(
dispatcher: web::Data<Dispatcher>,
path: web::Path<String>,
ActixAccess(access): ActixAccess,
) -> impl Responder {
// nothing to verify.
let pass = new_unchecked_verification_pass();
let snapshot_name = path.into_inner();
do_get_full_snapshot(dispatcher.toc(&access, &pass), access, &snapshot_name).await
}
#[delete("/snapshots/{snapshot_name}")]
async fn delete_full_snapshot(
dispatcher: web::Data<Dispatcher>,
path: web::Path<String>,
params: valid::Query<SnapshottingParam>,
ActixAccess(access): ActixAccess,
) -> impl Responder {
let future = async move {
let snapshot_name = path.into_inner();
do_delete_full_snapshot(dispatcher.get_ref(), access, &snapshot_name).await
};
helpers::time_or_accept(future, params.wait.unwrap_or(true)).await
}
#[delete("/collections/{name}/snapshots/{snapshot_name}")]
async fn delete_collection_snapshot(
dispatcher: web::Data<Dispatcher>,
path: web::Path<(String, String)>,
params: valid::Query<SnapshottingParam>,
ActixAccess(access): ActixAccess,
) -> impl Responder {
let future = async move {
let (collection_name, snapshot_name) = path.into_inner();
do_delete_collection_snapshot(
dispatcher.get_ref(),
access,
&collection_name,
&snapshot_name,
)
.await
};
helpers::time_or_accept(future, params.wait.unwrap_or(true)).await
}
#[get("/collections/{collection}/shards/{shard}/snapshots")]
async fn list_shard_snapshots(
dispatcher: web::Data<Dispatcher>,
path: web::Path<(String, ShardId)>,
ActixAccess(access): ActixAccess,
) -> impl Responder {
// nothing to verify.
let pass = new_unchecked_verification_pass();
let (collection, shard) = path.into_inner();
let future = common::snapshots::list_shard_snapshots(
dispatcher.toc(&access, &pass).clone(),
access,
collection,
shard,
)
.map_err(Into::into);
helpers::time(future).await
}
#[post("/collections/{collection}/shards/{shard}/snapshots")]
async fn create_shard_snapshot(
dispatcher: web::Data<Dispatcher>,
path: web::Path<(String, ShardId)>,
query: web::Query<SnapshottingParam>,
ActixAccess(access): ActixAccess,
) -> impl Responder {
// nothing to verify.
let pass = new_unchecked_verification_pass();
let (collection, shard) = path.into_inner();
let future = common::snapshots::create_shard_snapshot(
dispatcher.toc(&access, &pass).clone(),
access,
collection,
shard,
);
helpers::time_or_accept(future, query.wait.unwrap_or(true)).await
}
#[get("/collections/{collection}/shards/{shard}/snapshot")]
async fn stream_shard_snapshot(
dispatcher: web::Data<Dispatcher>,
path: web::Path<(String, ShardId)>,
ActixAccess(access): ActixAccess,
) -> Result<SnapshotStream, HttpError> {
// nothing to verify.
let pass = new_unchecked_verification_pass();
let (collection, shard) = path.into_inner();
Ok(common::snapshots::stream_shard_snapshot(
dispatcher.toc(&access, &pass).clone(),
access,
collection,
shard,
)
.await?)
}
// TODO: `PUT` (same as `recover_from_snapshot`) or `POST`!?
#[put("/collections/{collection}/shards/{shard}/snapshots/recover")]
async fn recover_shard_snapshot(
dispatcher: web::Data<Dispatcher>,
http_client: web::Data<HttpClient>,
path: web::Path<(String, ShardId)>,
query: web::Query<SnapshottingParam>,
web::Json(request): web::Json<ShardSnapshotRecover>,
ActixAccess(access): ActixAccess,
) -> impl Responder {
// nothing to verify.
let pass = new_unchecked_verification_pass();
let future = async move {
let (collection, shard) = path.into_inner();
common::snapshots::recover_shard_snapshot(
dispatcher.toc(&access, &pass).clone(),
access,
collection,
shard,
request.location,
request.priority.unwrap_or_default(),
request.checksum,
http_client.as_ref().clone(),
request.api_key,
)
.await?;
Ok(true)
};
helpers::time_or_accept(future, query.wait.unwrap_or(true)).await
}
// TODO: `POST` (same as `upload_snapshot`) or `PUT`!?
#[post("/collections/{collection}/shards/{shard}/snapshots/upload")]
async fn upload_shard_snapshot(
dispatcher: web::Data<Dispatcher>,
path: web::Path<(String, ShardId)>,
query: web::Query<SnapshotUploadingParam>,
MultipartForm(form): MultipartForm<SnapshottingForm>,
ActixAccess(access): ActixAccess,
) -> impl Responder {
// nothing to verify.
let pass = new_unchecked_verification_pass();
let (collection, shard) = path.into_inner();
let SnapshotUploadingParam {
wait,
priority,
checksum,
} = query.into_inner();
// - `recover_shard_snapshot_impl` is *not* cancel safe
// - but the task is *spawned* on the runtime and won't be cancelled, if request is cancelled
let future = cancel::future::spawn_cancel_on_drop(move |cancel| async move {
// TODO: Run this check before the multipart blob is uploaded
let collection_pass = access
.check_global_access(AccessRequirements::new().manage())?
.issue_pass(&collection);
if let Some(checksum) = checksum {
let snapshot_checksum = hash_file(form.snapshot.file.path()).await?;
if !hashes_equal(snapshot_checksum.as_str(), checksum.as_str()) {
return Err(StorageError::checksum_mismatch(snapshot_checksum, checksum));
}
}
let future = async {
let collection = dispatcher
.toc(&access, &pass)
.get_collection(&collection_pass)
.await?;
collection.assert_shard_exists(shard).await?;
Result::<_, StorageError>::Ok(collection)
};
let collection = cancel::future::cancel_on_token(cancel.clone(), future).await??;
// `recover_shard_snapshot_impl` is *not* cancel safe
common::snapshots::recover_shard_snapshot_impl(
dispatcher.toc(&access, &pass),
&collection,
shard,
form.snapshot.file.path(),
priority.unwrap_or_default(),
cancel,
)
.await?;
Ok(())
})
.map(|x| x.map_err(Into::into).and_then(|x| x));
helpers::time_or_accept(future, wait.unwrap_or(true)).await
}
#[get("/collections/{collection}/shards/{shard}/snapshots/{snapshot}")]
async fn download_shard_snapshot(
dispatcher: web::Data<Dispatcher>,
path: web::Path<(String, ShardId, String)>,
ActixAccess(access): ActixAccess,
) -> Result<impl Responder, HttpError> {
// nothing to verify.
let pass = new_unchecked_verification_pass();
let (collection, shard, snapshot) = path.into_inner();
let collection_pass =
access.check_collection_access(&collection, AccessRequirements::new().whole())?;
let collection = dispatcher
.toc(&access, &pass)
.get_collection(&collection_pass)
.await?;
let snapshots_storage_manager = collection.get_snapshots_storage_manager()?;
let snapshot_path = collection
.shards_holder()
.read()
.await
.get_shard_snapshot_path(collection.snapshots_path(), shard, &snapshot)
.await?;
let snapshot_stream = snapshots_storage_manager
.get_snapshot_stream(&snapshot_path)
.await?;
Ok(snapshot_stream)
}
#[delete("/collections/{collection}/shards/{shard}/snapshots/{snapshot}")]
async fn delete_shard_snapshot(
dispatcher: web::Data<Dispatcher>,
path: web::Path<(String, ShardId, String)>,
query: web::Query<SnapshottingParam>,
ActixAccess(access): ActixAccess,
) -> impl Responder {
// nothing to verify.
let pass = new_unchecked_verification_pass();
let (collection, shard, snapshot) = path.into_inner();
let future = common::snapshots::delete_shard_snapshot(
dispatcher.toc(&access, &pass).clone(),
access,
collection,
shard,
snapshot,
)
.map_ok(|_| true)
.map_err(Into::into);
helpers::time_or_accept(future, query.wait.unwrap_or(true)).await
}
// Configure services
pub fn config_snapshots_api(cfg: &mut web::ServiceConfig) {
cfg.service(list_snapshots)
.service(create_snapshot)
.service(upload_snapshot)
.service(recover_from_snapshot)
.service(get_snapshot)
.service(list_full_snapshots)
.service(create_full_snapshot)
.service(get_full_snapshot)
.service(delete_full_snapshot)
.service(delete_collection_snapshot)
.service(list_shard_snapshots)
.service(create_shard_snapshot)
.service(stream_shard_snapshot)
.service(recover_shard_snapshot)
.service(upload_shard_snapshot)
.service(download_shard_snapshot)
.service(delete_shard_snapshot);
}
|