From 35e188c709d744a024ee587e8a1936435905cfec Mon Sep 17 00:00:00 2001 From: Afsal Thaj Date: Mon, 9 Dec 2024 15:08:02 +1100 Subject: [PATCH] Remove inmemory wrapper around redis --- Cargo.lock | 1 - golem-worker-service-base/Cargo.toml | 1 - .../src/gateway_execution/gateway_session.rs | 74 ------------------- .../src/gateway_middleware/mod.rs | 2 +- .../tests/api_gateway_end_to_end_tests.rs | 9 +-- .../tests/services_tests.rs | 34 +-------- 6 files changed, 4 insertions(+), 117 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4257a9430..96cba91e5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4540,7 +4540,6 @@ dependencies = [ "humantime-serde", "hyper 1.5.1", "lazy_static 1.5.0", - "log 0.4.22", "mime_guess", "nom 7.1.3", "openapiv3", diff --git a/golem-worker-service-base/Cargo.toml b/golem-worker-service-base/Cargo.toml index f69142df6..55d7ce6aa 100644 --- a/golem-worker-service-base/Cargo.toml +++ b/golem-worker-service-base/Cargo.toml @@ -84,7 +84,6 @@ tracing-subscriber = { workspace = true } url = { workspace = true } uuid = { workspace = true } wasm-wave = { workspace = true } -log = "0.4.22" [dev-dependencies] criterion = { version = "0.3", features = ["html_reports"] } diff --git a/golem-worker-service-base/src/gateway_execution/gateway_session.rs b/golem-worker-service-base/src/gateway_execution/gateway_session.rs index c11c08f03..c094f7458 100644 --- a/golem-worker-service-base/src/gateway_execution/gateway_session.rs +++ b/golem-worker-service-base/src/gateway_execution/gateway_session.rs @@ -17,14 +17,11 @@ use bincode::enc::Encoder; use bincode::error::EncodeError; use bytes::Bytes; use fred::interfaces::RedisResult; -use golem_common::cache::{BackgroundEvictionMode, Cache, FullCacheEvictionMode, SimpleCache}; use golem_common::redis::RedisPool; use golem_common::SafeDisplay; -use log::info; use std::collections::HashMap; use std::hash::Hash; use std::sync::Arc; -use std::time::Duration; use tracing::error; #[async_trait] @@ -206,74 +203,3 @@ impl GatewaySession for RedisGatewaySession { } } } - -pub struct GatewaySessionWithInMemoryCache { - backend: A, - cache: Cache<(SessionId, DataKey), (), DataValue, GatewaySessionError>, -} - -impl GatewaySessionWithInMemoryCache { - pub fn new( - inner: A, - in_memory_expiration_in_seconds: i64, - eviction_period_in_seconds: u64, - ) -> Self { - let cache = Cache::new( - Some(1024), - FullCacheEvictionMode::None, - BackgroundEvictionMode::OlderThan { - ttl: Duration::from_secs(in_memory_expiration_in_seconds as u64), - period: Duration::from_secs(eviction_period_in_seconds), - }, - "gateway_session_in_memory", - ); - - Self { - backend: inner, - cache, - } - } -} - -#[async_trait] -impl GatewaySession - for GatewaySessionWithInMemoryCache -{ - async fn insert( - &self, - session_id: SessionId, - data_key: DataKey, - data_value: DataValue, - ) -> Result<(), GatewaySessionError> { - info!("Inserting session data to the backend"); - - self.backend - .insert(session_id, data_key, data_value) - .await?; - - info!("Inserted session data into cache"); - - Ok(()) - } - - async fn get( - &self, - session_id: &SessionId, - data_key: &DataKey, - ) -> Result { - info!("Getting session data from cache"); - let result = self - .cache - .get_or_insert_simple(&(session_id.clone(), data_key.clone()), || { - let inner = self.backend.clone(); - let session_id = session_id.clone(); - let data_key = data_key.clone(); - - Box::pin(async move { inner.get(&session_id, &data_key).await }) - }) - .await?; - - info!("Got session data from cache"); - Ok(result) - } -} diff --git a/golem-worker-service-base/src/gateway_middleware/mod.rs b/golem-worker-service-base/src/gateway_middleware/mod.rs index e4f8f43ad..d52b5b972 100644 --- a/golem-worker-service-base/src/gateway_middleware/mod.rs +++ b/golem-worker-service-base/src/gateway_middleware/mod.rs @@ -51,7 +51,7 @@ impl HttpMiddlewares { HttpMiddleware::AddCorsHeaders(_) => {} HttpMiddleware::AuthenticateRequest(auth) => { let result = auth - .apply_http_auth(&http_request_details, &session_store, &identity_provider) + .apply_http_auth(http_request_details, &session_store, &identity_provider) .await?; match result { diff --git a/golem-worker-service-base/tests/api_gateway_end_to_end_tests.rs b/golem-worker-service-base/tests/api_gateway_end_to_end_tests.rs index a85edd1c7..d80e3fc7a 100644 --- a/golem-worker-service-base/tests/api_gateway_end_to_end_tests.rs +++ b/golem-worker-service-base/tests/api_gateway_end_to_end_tests.rs @@ -1619,8 +1619,7 @@ mod internal { use rib::RibResult; use golem_worker_service_base::gateway_execution::gateway_session::{ - DataKey, DataValue, GatewaySession, GatewaySessionError, GatewaySessionStore, - GatewaySessionWithInMemoryCache, SessionId, + DataKey, DataValue, GatewaySession, GatewaySessionError, GatewaySessionStore, SessionId, }; use serde_json::Value; use std::collections::HashMap; @@ -2008,11 +2007,7 @@ mod internal { } pub fn get_session_store() -> GatewaySessionStore { - Arc::new(GatewaySessionWithInMemoryCache::new( - TestSessionBackEnd::new(), - 60 * 60, - 60, - )) + Arc::new(TestSessionBackEnd::new()) } struct NoopTestSessionBackend; diff --git a/golem-worker-service-base/tests/services_tests.rs b/golem-worker-service-base/tests/services_tests.rs index 9c01ee169..e26a96fbc 100644 --- a/golem-worker-service-base/tests/services_tests.rs +++ b/golem-worker-service-base/tests/services_tests.rs @@ -48,7 +48,7 @@ use golem_worker_service_base::gateway_api_deployment::{ ApiDeploymentRequest, ApiSite, ApiSiteString, }; use golem_worker_service_base::gateway_execution::gateway_session::{ - DataKey, DataValue, GatewaySession, GatewaySessionError, GatewaySessionWithInMemoryCache, + DataKey, DataValue, GatewaySession, GatewaySessionError, RedisGatewaySession, SessionId, }; use golem_worker_service_base::gateway_security::{ @@ -216,17 +216,6 @@ pub async fn test_gateway_session_expiry() { Err(GatewaySessionError::MissingValue { .. }) )); - // Redis backed by in-memory cache should return value - let result = insert_and_get_with_redis_with_in_memory_cache( - SessionId("test2".to_string()), - DataKey::nonce(), - data_value.clone(), - &redis, - ) - .await - .expect("Expecting a value from redis cache backed by in-memory"); - - assert_eq!(result, data_value); } async fn insert_and_get_with_redis( @@ -249,27 +238,6 @@ async fn insert_and_get_with_redis( session_store.get(&session_id, &data_key).await } -async fn insert_and_get_with_redis_with_in_memory_cache( - session_id: SessionId, - data_key: DataKey, - data_value: DataValue, - redis: &RedisPool, -) -> Result { - let redis_session = RedisGatewaySession::new(redis.clone(), 60 * 60); - let redis_with_in_memory = Arc::new(GatewaySessionWithInMemoryCache::new( - redis_session.clone(), - 60 * 60, - 60, - )); - - redis_with_in_memory - .insert(session_id.clone(), data_key.clone(), data_value.clone()) - .await - .unwrap(); - - redis_with_in_memory.get(&session_id, &data_key).await -} - #[test] pub async fn test_with_sqlite_db() { let db = SqliteDb::default();