Skip to content

Commit d401dd9

Browse files
authored
feat: cache storage move lock file (#8686)
* feat: cache storage move lock file * feat: cache storage move lock file * feat: cache storage move lock file * feat: cache storage move lock file * feat: cache storage move lock file
1 parent 6ac8a38 commit d401dd9

File tree

13 files changed

+746
-173
lines changed

13 files changed

+746
-173
lines changed

crates/rspack_storage/src/pack/data/meta.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,26 @@ pub struct PackFileMeta {
1313
pub wrote: bool,
1414
}
1515

16+
#[derive(Debug, Default, Clone)]
17+
pub enum RootMetaFrom {
18+
#[default]
19+
New,
20+
File,
21+
}
22+
1623
#[derive(Debug, Default, Clone)]
1724
pub struct RootMeta {
18-
pub last_modified: u64,
25+
pub expire_time: u64,
1926
pub scopes: HashSet<String>,
27+
pub from: RootMetaFrom,
2028
}
2129

2230
impl RootMeta {
23-
pub fn new(scopes: HashSet<String>) -> Self {
31+
pub fn new(scopes: HashSet<String>, expire: u64) -> Self {
2432
Self {
2533
scopes,
26-
last_modified: current_time(),
34+
expire_time: current_time() + expire,
35+
from: RootMetaFrom::New,
2736
}
2837
}
2938
pub fn get_path(dir: &Utf8Path) -> Utf8PathBuf {

crates/rspack_storage/src/pack/data/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ mod options;
33
mod pack;
44
mod scope;
55

6-
pub use meta::{current_time, PackFileMeta, RootMeta, ScopeMeta};
6+
pub use meta::{current_time, PackFileMeta, RootMeta, RootMetaFrom, ScopeMeta};
77
pub use options::{PackOptions, RootOptions};
88
pub use pack::{Pack, PackContents, PackKeys};
99
pub use scope::{PackScope, RootMetaState};

crates/rspack_storage/src/pack/fs/mod.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,13 @@ impl PackFS for PackBridgeFS {
107107
}
108108

109109
async fn remove_file(&self, path: &Utf8Path) -> Result<()> {
110-
self
111-
.0
112-
.remove_file(path)
113-
.await
114-
.map_err(|e| PackFsError::from_fs_error(path, PackFsErrorOpt::Remove, e))?;
110+
if self.exists(path).await? {
111+
self
112+
.0
113+
.remove_file(path)
114+
.await
115+
.map_err(|e| PackFsError::from_fs_error(path, PackFsErrorOpt::Remove, e))?;
116+
}
115117
Ok(())
116118
}
117119

crates/rspack_storage/src/pack/manager/mod.rs

Lines changed: 34 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ impl ScopeManager {
6565
.filter(|(_, scope)| scope.loaded())
6666
.map(|(name, _)| name.clone())
6767
.collect::<HashSet<_>>(),
68+
self.root_options.expire,
6869
)));
6970
Ok(())
7071
}
@@ -99,25 +100,37 @@ impl ScopeManager {
99100
Ok(rx)
100101
}
101102

103+
async fn clear_scope(&self, name: &str) {
104+
self
105+
.scopes
106+
.lock()
107+
.await
108+
.get_mut(name)
109+
.expect("should have scope")
110+
.clear();
111+
}
112+
102113
pub async fn load(&self, name: &'static str) -> Result<StorageContent> {
114+
self
115+
.scopes
116+
.lock()
117+
.await
118+
.entry(name.to_string())
119+
.or_insert_with(|| PackScope::new(self.strategy.get_path(name), self.pack_options.clone()));
120+
121+
// only check lock file and root meta for the first time
103122
if matches!(*self.root_meta.lock().await, RootMetaState::Pending) {
104-
let loaded = self.strategy.read_root_meta().await?;
105-
if let Some(meta) = &loaded {
106-
for scope_name in meta.scopes.clone() {
107-
self
108-
.scopes
109-
.lock()
110-
.await
111-
.entry(scope_name.clone())
112-
.or_insert_with(|| {
113-
PackScope::new(
114-
self.strategy.get_path(&scope_name),
115-
self.pack_options.clone(),
116-
)
117-
});
123+
match self.strategy.before_load().await {
124+
Ok(()) => {
125+
let loaded = self.strategy.read_root_meta().await?;
126+
*self.root_meta.lock().await = RootMetaState::Value(loaded);
127+
}
128+
Err(err) => {
129+
*self.root_meta.lock().await = RootMetaState::Value(None);
130+
self.clear_scope(name).await;
131+
return Err(err);
118132
}
119133
}
120-
*self.root_meta.lock().await = RootMetaState::Value(loaded);
121134
}
122135

123136
match self.validate_scope(name).await {
@@ -131,37 +144,18 @@ impl ScopeManager {
131144
}
132145
// create empty scope if not exists
133146
ValidateResult::NotExists => {
134-
self
135-
.scopes
136-
.lock()
137-
.await
138-
.entry(name.to_string())
139-
.or_insert_with(|| {
140-
PackScope::empty(self.strategy.get_path(name), self.pack_options.clone())
141-
});
147+
self.clear_scope(name).await;
142148
Ok(vec![])
143149
}
144150
// clear scope if invalid
145151
ValidateResult::Invalid(_) => {
146-
self
147-
.scopes
148-
.lock()
149-
.await
150-
.get_mut(name)
151-
.expect("should have scope")
152-
.clear();
152+
self.clear_scope(name).await;
153153
Err(error!(validated.to_string()))
154154
}
155155
},
156156
Err(e) => {
157157
// clear scope if error
158-
self
159-
.scopes
160-
.lock()
161-
.await
162-
.get_mut(name)
163-
.expect("should have scope")
164-
.clear();
158+
self.clear_scope(name).await;
165159
Err(Error::from(e))
166160
}
167161
}
@@ -180,10 +174,7 @@ impl ScopeManager {
180174
};
181175

182176
// scope exists, validate it
183-
let validated = self
184-
.strategy
185-
.validate_root(root_meta, self.root_options.expire)
186-
.await?;
177+
let validated = self.strategy.validate_root(root_meta).await?;
187178
if validated.is_valid() {
188179
self.strategy.ensure_meta(scope).await?;
189180
let validated = self.strategy.validate_meta(scope).await?;
@@ -272,6 +263,8 @@ async fn save_scopes(
272263
.into_iter()
273264
.collect_vec();
274265

266+
strategy.write_root_meta(root_meta).await?;
267+
275268
join_all(
276269
scopes
277270
.values()
@@ -291,8 +284,6 @@ async fn save_scopes(
291284
.into_iter()
292285
.collect::<Result<Vec<_>>>()?;
293286

294-
strategy.write_root_meta(root_meta).await?;
295-
296287
for (_, scope) in scopes.iter_mut() {
297288
strategy.after_all(scope)?;
298289
}

crates/rspack_storage/src/pack/strategy/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ pub trait ScopeStrategy:
3131

3232
#[async_trait]
3333
pub trait RootStrategy {
34+
async fn before_load(&self) -> Result<()>;
3435
async fn read_root_meta(&self) -> Result<Option<RootMeta>>;
3536
async fn write_root_meta(&self, root_meta: &RootMeta) -> Result<()>;
36-
async fn validate_root(&self, root_meta: &RootMeta, expire: u64) -> Result<ValidateResult>;
37+
async fn validate_root(&self, root_meta: &RootMeta) -> Result<ValidateResult>;
3738
async fn clean_unused(
3839
&self,
3940
root_meta: &RootMeta,

0 commit comments

Comments
 (0)