From 9907a33ed56093294fef0dbfcd25f1e9f5a143b9 Mon Sep 17 00:00:00 2001 From: Nitish Tiwari Date: Wed, 3 Jan 2024 13:19:37 +0530 Subject: [PATCH] fix: ensure checking the global cache before enabling stream cache (#592) * fix: ensure checking the global cache status before enabling stream cache This PR adds a check to ensure global cache is enabled in the enable stream cache API. We don't allow enabling stream cache because it is misleading to enable cache if global cache is not enabled / configured. * enhanced http response message for enabling/disabling cache --------- Co-authored-by: Nikhil Sinha <131262146+nikhilsinhacloudsurfex@users.noreply.github.com> --- .gitignore | 3 ++- server/src/handlers/http/logstream.rs | 11 ++++++++++- server/src/metadata.rs | 2 +- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index c759f2831..c3994ec35 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,5 @@ env-file parseable parseable_* parseable-env-secret -cache +cache* + diff --git a/server/src/handlers/http/logstream.rs b/server/src/handlers/http/logstream.rs index 97ac52056..7fdfe61c3 100644 --- a/server/src/handlers/http/logstream.rs +++ b/server/src/handlers/http/logstream.rs @@ -241,6 +241,10 @@ pub async fn put_enable_cache( let stream_name: String = req.match_info().get("logstream").unwrap().parse().unwrap(); let storage = CONFIG.storage().get_object_store(); + if CONFIG.parseable.local_cache_path.is_none() { + return Err(StreamError::CacheNotEnabled(stream_name)); + } + let mut stream_metadata = storage.get_stream_metadata(&stream_name).await?; stream_metadata.cache_enabled = enable_cache; storage @@ -249,7 +253,7 @@ pub async fn put_enable_cache( STREAM_INFO.set_stream_cache(&stream_name, enable_cache)?; Ok(( - format!("Cache setting updated for log stream {stream_name}"), + format!("Cache set to {enable_cache} for log stream {stream_name}"), StatusCode::OK, )) } @@ -336,6 +340,10 @@ pub mod error { CreateStream(#[from] CreateStreamError), #[error("Log stream {0} does not exist")] StreamNotFound(String), + #[error( + "Caching not enabled at Parseable server config. Can't enable cache for stream {0}" + )] + CacheNotEnabled(String), #[error("Log stream is not initialized, send an event to this logstream and try again")] UninitializedLogstream, #[error("Storage Error {0}")] @@ -370,6 +378,7 @@ pub mod error { StreamError::CreateStream(CreateStreamError::Storage { .. }) => { StatusCode::INTERNAL_SERVER_ERROR } + StreamError::CacheNotEnabled(_) => StatusCode::BAD_REQUEST, StreamError::StreamNotFound(_) => StatusCode::NOT_FOUND, StreamError::Custom { status, .. } => *status, StreamError::UninitializedLogstream => StatusCode::METHOD_NOT_ALLOWED, diff --git a/server/src/metadata.rs b/server/src/metadata.rs index 5bdfdb515..e7fb32614 100644 --- a/server/src/metadata.rs +++ b/server/src/metadata.rs @@ -220,7 +220,7 @@ pub mod error { #[derive(Debug, thiserror::Error)] pub enum MetadataError { - #[error("Metadata for stream {0} not found. Maybe the stream does not exist")] + #[error("Metadata for stream {0} not found. Please create the stream and try again")] StreamMetaNotFound(String), }