Skip to content

Commit

Permalink
Reduce visibility of evaluator details
Browse files Browse the repository at this point in the history
  • Loading branch information
afsalthaj committed Jul 1, 2024
1 parent 4e8edc0 commit 18fbd6f
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 66 deletions.
111 changes: 53 additions & 58 deletions golem-worker-service-base/src/evaluator/component_elements.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
use crate::evaluator::component_metadata_fetch::{ComponentMetadataFetch, MetadataFetchError};
use crate::evaluator::{Function, FQN};
use async_trait::async_trait;
use golem_common::cache::{BackgroundEvictionMode, Cache, SimpleCache};
use golem_common::model::ComponentId;
use golem_service_base::model::ComponentMetadata;
use rib::ParsedFunctionName;

use std::sync::Arc;
#[derive(PartialEq, Debug, Clone)]
pub struct ComponentElements {
pub functions: Vec<Function>,
Expand Down Expand Up @@ -54,71 +59,61 @@ impl ComponentElements {
}
}

pub mod cached {
use crate::evaluator::component_elements::ComponentElements;
use crate::evaluator::component_metadata_fetch::{ComponentMetadataFetch, MetadataFetchError};
use async_trait::async_trait;
use golem_common::cache::{BackgroundEvictionMode, Cache, SimpleCache};
use golem_common::model::ComponentId;

use std::sync::Arc;

// The logic shouldn't be visible outside the crate
pub(crate) struct DefaultComponentElementsFetch {
component_metadata_fetch: Arc<dyn ComponentMetadataFetch + Sync + Send>,
component_elements_cache: Cache<ComponentId, (), ComponentElements, MetadataFetchError>,
}
// The logic shouldn't be visible outside the crate
pub(crate) struct DefaultComponentElementsFetch {
component_metadata_fetch: Arc<dyn ComponentMetadataFetch + Sync + Send>,
component_elements_cache: Cache<ComponentId, (), ComponentElements, MetadataFetchError>,
}

impl DefaultComponentElementsFetch {
pub(crate) fn new(metadata_fetcher: Arc<dyn ComponentMetadataFetch + Sync + Send>) -> Self {
DefaultComponentElementsFetch {
component_metadata_fetch: metadata_fetcher,
component_elements_cache: Cache::new(
Some(10000),
golem_common::cache::FullCacheEvictionMode::LeastRecentlyUsed(1),
BackgroundEvictionMode::None,
"worker_gateway",
),
}
impl DefaultComponentElementsFetch {
pub(crate) fn new(metadata_fetcher: Arc<dyn ComponentMetadataFetch + Sync + Send>) -> Self {
DefaultComponentElementsFetch {
component_metadata_fetch: metadata_fetcher,
component_elements_cache: Cache::new(
Some(10000),
golem_common::cache::FullCacheEvictionMode::LeastRecentlyUsed(1),
BackgroundEvictionMode::None,
"worker_gateway",
),
}
}
}

// A service that will give richer data
// compared to ComponentMetadataFetch service
// which is required for the evaluator
#[async_trait]
pub(crate) trait ComponentElementsFetch {
async fn get_component_elements(
&self,
component_id: ComponentId,
) -> Result<ComponentElements, MetadataFetchError>;
// A service that will give richer data
// compared to ComponentMetadataFetch service
// which is required for the evaluator
#[async_trait]
pub(crate) trait ComponentElementsFetch {
async fn get_component_elements(
&self,
component_id: ComponentId,
) -> Result<ComponentElements, MetadataFetchError>;

fn invalidate_cached_component_elements(&self, component_id: &ComponentId);
}
fn invalidate_cached_component_elements(&self, component_id: &ComponentId);
}

#[async_trait]
impl ComponentElementsFetch for DefaultComponentElementsFetch {
async fn get_component_elements(
&self,
component_id: ComponentId,
) -> Result<ComponentElements, MetadataFetchError> {
self.component_elements_cache
.get_or_insert_simple(&component_id.clone(), || {
let metadata_fetcher = self.component_metadata_fetch.clone();
Box::pin(async move {
let component_metadata = metadata_fetcher
.get_component_metadata(&component_id)
.await?;
Ok(ComponentElements::from_component_metadata(
component_metadata,
))
})
#[async_trait]
impl ComponentElementsFetch for DefaultComponentElementsFetch {
async fn get_component_elements(
&self,
component_id: ComponentId,
) -> Result<ComponentElements, MetadataFetchError> {
self.component_elements_cache
.get_or_insert_simple(&component_id.clone(), || {
let metadata_fetcher = self.component_metadata_fetch.clone();
Box::pin(async move {
let component_metadata = metadata_fetcher
.get_component_metadata(&component_id)
.await?;
Ok(ComponentElements::from_component_metadata(
component_metadata,
))
})
.await
}
})
.await
}

fn invalidate_cached_component_elements(&self, component_id: &ComponentId) {
self.component_elements_cache.remove(component_id);
}
fn invalidate_cached_component_elements(&self, component_id: &ComponentId) {
self.component_elements_cache.remove(component_id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ use golem_common::model::ComponentId;
use golem_service_base::model::ComponentMetadata;
use std::fmt::Display;

// Service to fetch the component metadata given a component-id
// This is different to ComponentMetadataFetch which gives richer data called ComponentElements
// that's more useful to evaluator.
// Outside modules/crates should use this service, while ComponentElementsFetch is visible only to the base crate
#[async_trait]
pub trait ComponentMetadataFetch {
async fn get_component_metadata(
Expand Down
17 changes: 9 additions & 8 deletions golem-worker-service-base/src/evaluator/mod.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
use async_trait::async_trait;
pub(crate) use component_elements::cached::*;
pub use component_elements::*;
use std::sync::Arc;

pub use component_metadata_fetch::*;
pub use evaluator_context::*;

use std::sync::Arc;
mod component_metadata_fetch;
mod evaluator_context;
// Component Elements shouldn't be visible outside this crate
pub(crate) use component_elements::*;
pub(crate) use evaluator_context::*;
pub(crate) mod getter;
mod math_op_evaluator;
pub(crate) mod path;
mod pattern_match_evaluator;

mod component_elements;
mod component_metadata_fetch;
mod evaluator_context;
mod math_op_evaluator;
mod pattern_match_evaluator;

use golem_wasm_ast::analysis::AnalysedType;
use golem_wasm_rpc::json::get_json_from_typed_value;
Expand Down

0 comments on commit 18fbd6f

Please sign in to comment.