Skip to content

Commit

Permalink
impl query result caching
Browse files Browse the repository at this point in the history
  • Loading branch information
Eshanatnight committed May 7, 2024
1 parent 8c99690 commit 1fae167
Show file tree
Hide file tree
Showing 4 changed files with 390 additions and 5 deletions.
9 changes: 5 additions & 4 deletions server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ build = "build.rs"

[dependencies]
### apache arrow/datafusion dependencies
# arrow = "51.0.0"
arrow-schema = { version = "51.0.0", features = ["serde"] }
arrow-array = { version = "51.0.0" }
arrow-json = "51.0.0"
arrow-ipc = "51.0.0"
arrow-select = "51.0.0"
datafusion = "37.1.0"
object_store = { version = "0.9.1", features = ["cloud", "aws"] }
object_store = { version = "0.9.1", features = ["cloud", "aws"] } # cannot update object_store as datafusion has not caught up
parquet = "51.0.0"

### LiveTail server deps
Expand Down Expand Up @@ -74,11 +75,11 @@ relative-path = { version = "1.7", features = ["serde"] }
reqwest = { version = "0.11.27", default_features = false, features = [
"rustls-tls",
"json",
] }
rustls = "0.22.4"
] } # cannot update cause rustls is not latest `see rustls`
rustls = "0.22.4" # cannot update to 0.23 actix has not caught up yet
rustls-pemfile = "2.1.2"
semver = "1.0"
serde = { version = "1.0", features = ["rc"] }
serde = { version = "1.0", features = ["rc", "derive"] }
serde_json = "1.0"
static-files = "0.2"
sysinfo = "0.30.11"
Expand Down
47 changes: 46 additions & 1 deletion server/src/handlers/http/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ use crate::event::error::EventError;
use crate::handlers::http::fetch_schema;

use crate::event::commit_schema;
use crate::localcache::CacheError;
use crate::metrics::QUERY_EXECUTE_TIME;
use crate::option::{Mode, CONFIG};
use crate::query::error::ExecuteError;
use crate::query::Query as LogicalQuery;
use crate::query::{TableScanVisitor, QUERY_SESSION};
use crate::querycache::QueryCacheManager;
use crate::rbac::role::{Action, Permission};
use crate::rbac::Users;
use crate::response::QueryResponse;
Expand All @@ -50,7 +52,7 @@ use crate::utils::actix::extract_session_key_from_req;
#[derive(Debug, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Query {
query: String,
pub query: String,
start_time: String,
end_time: String,
#[serde(default)]
Expand All @@ -72,6 +74,36 @@ pub async fn query(req: HttpRequest, query_request: Query) -> Result<impl Respon
// create a visitor to extract the table name
let mut visitor = TableScanVisitor::default();
let _ = raw_logical_plan.visit(&mut visitor);
let stream = visitor.top();
let query_cache_manager = QueryCacheManager::global(CONFIG.parseable.query_cache_size)
.await
.unwrap_or(None);

if let Some(query_cache_manager) = query_cache_manager {
let mut query_cache = query_cache_manager.get_cache(stream).await?;

let (start, end) = parse_human_time(&query_request.start_time, &query_request.end_time)?;
let key = format!(
"{}-{}-{}",
start.to_rfc3339(),
end.to_rfc3339(),
query_request.query.clone()
);

let file_path = query_cache.get_file(key);
if let Some(file_path) = file_path {
let (records, fields) = query_cache.get_cached_records(&file_path).await?;
let response = QueryResponse {
records,
fields,
fill_null: query_request.send_null,
with_fields: query_request.fields,
}
.to_http()?;

return Ok(response);
}
};

let tables = visitor.into_inner();

Expand Down Expand Up @@ -99,6 +131,19 @@ pub async fn query(req: HttpRequest, query_request: Query) -> Result<impl Respon
let time = Instant::now();

let (records, fields) = query.execute(table_name.clone()).await?;
// put the rbs to parquet
if let Some(query_cache_manager) = query_cache_manager {
query_cache_manager
.create_parquet_cache(
&table_name,
&records,
query.start.to_rfc3339(),
query.end.to_rfc3339(),
query_request.query,
)
.await?;
}

let response = QueryResponse {
records,
fields,
Expand Down
1 change: 1 addition & 0 deletions server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ mod migration;
mod oidc;
mod option;
mod query;
mod querycache;
mod rbac;
mod response;
mod static_schema;
Expand Down
Loading

0 comments on commit 1fae167

Please sign in to comment.