aboutsummaryrefslogtreecommitdiff
path: root/src/api/s3/delete.rs
diff options
context:
space:
mode:
authorAlex <alex@adnab.me>2022-05-10 13:16:57 +0200
committerAlex <alex@adnab.me>2022-05-10 13:16:57 +0200
commit5768bf362262f78376af14517c4921941986192e (patch)
treeb4baf3051eade0f63649443278bb3a3f4c38ec25 /src/api/s3/delete.rs
parentdef78c5e6f5da37a0d17b5652c525fbeccbc2e86 (diff)
downloadgarage-5768bf362262f78376af14517c4921941986192e.tar.gz
garage-5768bf362262f78376af14517c4921941986192e.zip
First implementation of K2V (#293)
**Specification:** View spec at [this URL](https://git.deuxfleurs.fr/Deuxfleurs/garage/src/branch/k2v/doc/drafts/k2v-spec.md) - [x] Specify the structure of K2V triples - [x] Specify the DVVS format used for causality detection - [x] Specify the K2V index (just a counter of number of values per partition key) - [x] Specify single-item endpoints: ReadItem, InsertItem, DeleteItem - [x] Specify index endpoint: ReadIndex - [x] Specify multi-item endpoints: InsertBatch, ReadBatch, DeleteBatch - [x] Move to JSON objects instead of tuples - [x] Specify endpoints for polling for updates on single values (PollItem) **Implementation:** - [x] Table for K2V items, causal contexts - [x] Indexing mechanism and table for K2V index - [x] Make API handlers a bit more generic - [x] K2V API endpoint - [x] K2V API router - [x] ReadItem - [x] InsertItem - [x] DeleteItem - [x] PollItem - [x] ReadIndex - [x] InsertBatch - [x] ReadBatch - [x] DeleteBatch **Testing:** - [x] Just a simple Python script that does some requests to check visually that things are going right (does not contain parsing of results or assertions on returned values) - [x] Actual tests: - [x] Adapt testing framework - [x] Simple test with InsertItem + ReadItem - [x] Test with several Insert/Read/DeleteItem + ReadIndex - [x] Test all combinations of return formats for ReadItem - [x] Test with ReadBatch, InsertBatch, DeleteBatch - [x] Test with PollItem - [x] Test error codes - [ ] Fix most broken stuff - [x] test PollItem broken randomly - [x] when invalid causality tokens are given, errors should be 4xx not 5xx **Improvements:** - [x] Descending range queries - [x] Specify - [x] Implement - [x] Add test - [x] Batch updates to index counter - [x] Put K2V behind `k2v` feature flag Co-authored-by: Alex Auvolat <alex@adnab.me> Reviewed-on: https://git.deuxfleurs.fr/Deuxfleurs/garage/pulls/293 Co-authored-by: Alex <alex@adnab.me> Co-committed-by: Alex <alex@adnab.me>
Diffstat (limited to 'src/api/s3/delete.rs')
-rw-r--r--src/api/s3/delete.rs170
1 files changed, 170 insertions, 0 deletions
diff --git a/src/api/s3/delete.rs b/src/api/s3/delete.rs
new file mode 100644
index 00000000..1e3f1249
--- /dev/null
+++ b/src/api/s3/delete.rs
@@ -0,0 +1,170 @@
+use std::sync::Arc;
+
+use hyper::{Body, Request, Response, StatusCode};
+
+use garage_util::data::*;
+use garage_util::time::*;
+
+use garage_model::garage::Garage;
+use garage_model::s3::object_table::*;
+
+use crate::error::*;
+use crate::s3::xml as s3_xml;
+use crate::signature::verify_signed_content;
+
+async fn handle_delete_internal(
+ garage: &Garage,
+ bucket_id: Uuid,
+ key: &str,
+) -> Result<(Uuid, Uuid), Error> {
+ let object = garage
+ .object_table
+ .get(&bucket_id, &key.to_string())
+ .await?
+ .ok_or(Error::NoSuchKey)?; // No need to delete
+
+ let interesting_versions = object.versions().iter().filter(|v| {
+ !matches!(
+ v.state,
+ ObjectVersionState::Aborted
+ | ObjectVersionState::Complete(ObjectVersionData::DeleteMarker)
+ )
+ });
+
+ let mut version_to_delete = None;
+ let mut timestamp = now_msec();
+ for v in interesting_versions {
+ if v.timestamp + 1 > timestamp || version_to_delete.is_none() {
+ version_to_delete = Some(v.uuid);
+ }
+ timestamp = std::cmp::max(timestamp, v.timestamp + 1);
+ }
+
+ let deleted_version = version_to_delete.ok_or(Error::NoSuchKey)?;
+
+ let version_uuid = gen_uuid();
+
+ let object = Object::new(
+ bucket_id,
+ key.into(),
+ vec![ObjectVersion {
+ uuid: version_uuid,
+ timestamp,
+ state: ObjectVersionState::Complete(ObjectVersionData::DeleteMarker),
+ }],
+ );
+
+ garage.object_table.insert(&object).await?;
+
+ Ok((deleted_version, version_uuid))
+}
+
+pub async fn handle_delete(
+ garage: Arc<Garage>,
+ bucket_id: Uuid,
+ key: &str,
+) -> Result<Response<Body>, Error> {
+ let (_deleted_version, delete_marker_version) =
+ handle_delete_internal(&garage, bucket_id, key).await?;
+
+ Ok(Response::builder()
+ .header("x-amz-version-id", hex::encode(delete_marker_version))
+ .status(StatusCode::NO_CONTENT)
+ .body(Body::from(vec![]))
+ .unwrap())
+}
+
+pub async fn handle_delete_objects(
+ garage: Arc<Garage>,
+ bucket_id: Uuid,
+ req: Request<Body>,
+ content_sha256: Option<Hash>,
+) -> Result<Response<Body>, Error> {
+ let body = hyper::body::to_bytes(req.into_body()).await?;
+
+ if let Some(content_sha256) = content_sha256 {
+ verify_signed_content(content_sha256, &body[..])?;
+ }
+
+ let cmd_xml = roxmltree::Document::parse(std::str::from_utf8(&body)?)?;
+ let cmd = parse_delete_objects_xml(&cmd_xml).ok_or_bad_request("Invalid delete XML query")?;
+
+ let mut ret_deleted = Vec::new();
+ let mut ret_errors = Vec::new();
+
+ for obj in cmd.objects.iter() {
+ match handle_delete_internal(&garage, bucket_id, &obj.key).await {
+ Ok((deleted_version, delete_marker_version)) => {
+ if cmd.quiet {
+ continue;
+ }
+ ret_deleted.push(s3_xml::Deleted {
+ key: s3_xml::Value(obj.key.clone()),
+ version_id: s3_xml::Value(hex::encode(deleted_version)),
+ delete_marker_version_id: s3_xml::Value(hex::encode(delete_marker_version)),
+ });
+ }
+ Err(e) => {
+ ret_errors.push(s3_xml::DeleteError {
+ code: s3_xml::Value(e.aws_code().to_string()),
+ key: Some(s3_xml::Value(obj.key.clone())),
+ message: s3_xml::Value(format!("{}", e)),
+ version_id: None,
+ });
+ }
+ }
+ }
+
+ let xml = s3_xml::to_xml_with_header(&s3_xml::DeleteResult {
+ xmlns: (),
+ deleted: ret_deleted,
+ errors: ret_errors,
+ })?;
+
+ Ok(Response::builder()
+ .header("Content-Type", "application/xml")
+ .body(Body::from(xml))?)
+}
+
+struct DeleteRequest {
+ quiet: bool,
+ objects: Vec<DeleteObject>,
+}
+
+struct DeleteObject {
+ key: String,
+}
+
+fn parse_delete_objects_xml(xml: &roxmltree::Document) -> Option<DeleteRequest> {
+ let mut ret = DeleteRequest {
+ quiet: false,
+ objects: vec![],
+ };
+
+ let root = xml.root();
+ let delete = root.first_child()?;
+
+ if !delete.has_tag_name("Delete") {
+ return None;
+ }
+
+ for item in delete.children() {
+ if item.has_tag_name("Object") {
+ let key = item.children().find(|e| e.has_tag_name("Key"))?;
+ let key_str = key.text()?;
+ ret.objects.push(DeleteObject {
+ key: key_str.to_string(),
+ });
+ } else if item.has_tag_name("Quiet") {
+ if item.text()? == "true" {
+ ret.quiet = true;
+ } else {
+ ret.quiet = false;
+ }
+ } else {
+ return None;
+ }
+ }
+
+ Some(ret)
+}