-
Notifications
You must be signed in to change notification settings - Fork 447
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
support compressed data #4506
Merged
Merged
support compressed data #4506
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Copyright (C) 2024 Quickwit, Inc. | ||
// | ||
// Quickwit is offered under the AGPL v3.0 and as commercial software. | ||
// For commercial licensing, contact us at [email protected]. | ||
// | ||
// AGPL: | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as | ||
// published by the Free Software Foundation, either version 3 of the | ||
// License, or (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
use std::io::Read; | ||
|
||
use bytes::Bytes; | ||
use flate2::read::GzDecoder; | ||
use thiserror::Error; | ||
use tokio::task; | ||
use warp::reject::Reject; | ||
use warp::Filter; | ||
|
||
/// There are two ways to decompress the body: | ||
/// - Stream the body through an async decompressor | ||
/// - Fetch the body and then decompress the bytes | ||
/// | ||
/// The first approach lowers the latency, while the second approach is more CPU efficient. | ||
/// Ingesting data is usually CPU bound and there is considerable latency until the data is | ||
/// searchable, so the second approach is more suitable for this use case. | ||
async fn decompress_body(encoding: Option<String>, body: Bytes) -> Result<Bytes, warp::Rejection> { | ||
match encoding.as_deref() { | ||
Some("gzip" | "x-gzip") => { | ||
let decompressed = task::spawn_blocking(move || { | ||
let mut decompressed = Vec::new(); | ||
let mut decoder = GzDecoder::new(body.as_ref()); | ||
decoder | ||
.read_to_end(&mut decompressed) | ||
.map_err(|_| warp::reject::custom(CorruptedData))?; | ||
Result::<_, warp::Rejection>::Ok(Bytes::from(decompressed)) | ||
}) | ||
.await | ||
.map_err(|_| warp::reject::custom(CorruptedData))??; | ||
Ok(decompressed) | ||
} | ||
Some("zstd") => { | ||
let decompressed = task::spawn_blocking(move || { | ||
zstd::decode_all(body.as_ref()) | ||
.map(Bytes::from) | ||
.map_err(|_| warp::reject::custom(CorruptedData)) | ||
}) | ||
.await | ||
.map_err(|_| warp::reject::custom(CorruptedData))??; | ||
Ok(decompressed) | ||
} | ||
Some(encoding) => Err(warp::reject::custom(UnsupportedEncoding( | ||
encoding.to_string(), | ||
))), | ||
_ => Ok(body), | ||
} | ||
} | ||
|
||
#[derive(Debug, Error)] | ||
#[error("Error while decompressing the data")] | ||
pub(crate) struct CorruptedData; | ||
|
||
impl Reject for CorruptedData {} | ||
|
||
#[derive(Debug, Error)] | ||
#[error("Unsupported Content-Encoding {}. Supported encodings are 'gzip' and 'zstd'", self.0)] | ||
pub(crate) struct UnsupportedEncoding(String); | ||
|
||
impl Reject for UnsupportedEncoding {} | ||
|
||
/// Custom filter for optional decompression | ||
pub(crate) fn get_body_bytes() -> impl Filter<Extract = (Bytes,), Error = warp::Rejection> + Clone { | ||
warp::header::optional("content-encoding") | ||
.and(warp::body::bytes()) | ||
.and_then(|encoding: Option<String>, body: Bytes| async move { | ||
decompress_body(encoding, body).await | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we improve this and make sure we return a 400 or a 406 with an error message for these errors. Right now all of them end up as a 500.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I update the handling to return 415
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not the part where the header is there but the file is invalid ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated to also cover that case
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thank you!