-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule_cache.rs
More file actions
140 lines (129 loc) · 4.3 KB
/
Copy pathmodule_cache.rs
File metadata and controls
140 lines (129 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use std::sync::Arc;
use async_lru::async_lru::AsyncLru;
use async_trait::async_trait;
use common::{
document::ParsedDocument,
knobs::{
FUNRUN_CODE_CACHE_SIZE,
FUNRUN_MODULE_CACHE_SIZE,
FUNRUN_MODULE_MAX_CONCURRENCY,
},
runtime::Runtime,
};
use futures::FutureExt;
use isolate::environment::helpers::module_loader::get_module_and_prefetch;
use model::{
config::module_loader::ModuleLoader,
modules::{
module_versions::FullModuleSource,
types::ModuleMetadata,
},
source_packages::types::{
SourcePackage,
SourcePackageId,
},
};
use moka::sync::Cache;
use storage::Storage;
use sync_types::CanonicalizedModulePath;
use crate::record_module_sizes;
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub(crate) struct ModuleCacheKey {
instance_name: String,
module_path: CanonicalizedModulePath,
source_package_id: SourcePackageId,
}
#[derive(Clone)]
pub(crate) struct ModuleCache<RT: Runtime>(AsyncLru<RT, ModuleCacheKey, FullModuleSource>);
impl<RT: Runtime> ModuleCache<RT> {
pub(crate) fn new(rt: RT) -> Self {
Self(AsyncLru::new(
rt,
*FUNRUN_MODULE_CACHE_SIZE,
*FUNRUN_MODULE_MAX_CONCURRENCY,
"function_runner_module_cache",
))
}
}
#[derive(Clone)]
pub(crate) struct CodeCache(Arc<Cache<ModuleCacheKey, Arc<[u8]>>>);
impl CodeCache {
pub(crate) fn new() -> Self {
Self(Arc::new(
Cache::builder()
.max_capacity(*FUNRUN_CODE_CACHE_SIZE)
.weigher(|_, data: &Arc<[u8]>| u32::try_from(data.len()).unwrap_or(u32::MAX))
.build(),
))
}
}
pub(crate) struct FunctionRunnerModuleLoader<RT: Runtime> {
pub cache: ModuleCache<RT>,
pub code_cache: CodeCache,
pub instance_name: String,
pub modules_storage: Arc<dyn Storage>,
}
impl<RT: Runtime> FunctionRunnerModuleLoader<RT> {
fn cache_key(&self, module_metadata: &ModuleMetadata) -> ModuleCacheKey {
ModuleCacheKey {
instance_name: self.instance_name.clone(),
module_path: module_metadata.path.clone(),
source_package_id: module_metadata.source_package_id,
}
}
}
#[async_trait]
impl<RT: Runtime> ModuleLoader<RT> for FunctionRunnerModuleLoader<RT> {
#[fastrace::trace]
async fn get_module_with_metadata(
&self,
module_metadata: ParsedDocument<ModuleMetadata>,
source_package: ParsedDocument<SourcePackage>,
) -> anyhow::Result<Arc<FullModuleSource>> {
let instance_name = self.instance_name.clone();
let key = self.cache_key(&module_metadata);
let modules_storage = self.modules_storage.clone();
let result = self
.cache
.0
.get_and_prepopulate(
key.clone(),
async move {
let modules =
get_module_and_prefetch(modules_storage, module_metadata, source_package)
.await;
modules
.into_iter()
.map(move |((module_path, source_package_id), source)| {
(
ModuleCacheKey {
instance_name: instance_name.clone(),
module_path,
source_package_id,
},
source,
)
})
.collect()
}
.boxed(),
)
.await?;
record_module_sizes(
result.source.len(),
result.source_map.as_ref().map(|sm| sm.len()),
);
Ok(result)
}
}
impl<RT: Runtime> isolate::module_cache::ModuleCache<RT> for FunctionRunnerModuleLoader<RT> {
fn put_cached_code(&self, module_metadata: &ModuleMetadata, cached_data: Arc<[u8]>) {
self.code_cache
.0
.insert(self.cache_key(module_metadata), cached_data);
crate::metrics::record_code_cache_size(self.code_cache.0.weighted_size());
}
fn get_cached_code(&self, module_metadata: &ModuleMetadata) -> Option<Arc<[u8]>> {
self.code_cache.0.get(&self.cache_key(module_metadata))
}
}