-
Notifications
You must be signed in to change notification settings - Fork 369
Expand file tree
/
Copy pathincremental_state_db.rs
More file actions
387 lines (345 loc) · 13.9 KB
/
Copy pathincremental_state_db.rs
File metadata and controls
387 lines (345 loc) · 13.9 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is dual-licensed under either the MIT license found in the
* LICENSE-MIT file in the root directory of this source tree or the Apache
* License, Version 2.0 found in the LICENSE-APACHE file in the root directory
* of this source tree. You may select, at your option, one of the
* above-listed licenses.
*/
use std::sync::Arc;
use buck2_common::sqlite::sqlite_db::SqliteDb;
use buck2_common::sqlite::sqlite_db::SqliteIdentity;
use buck2_common::sqlite::sqlite_db::SqliteTable;
use buck2_common::sqlite::sqlite_db::SqliteTables;
use buck2_core::fs::project::ProjectRoot;
use buck2_core::fs::project_rel_path::ProjectRelativePath;
use buck2_core::fs::project_rel_path::ProjectRelativePathBuf;
use buck2_core::soft_error;
use buck2_execute::execute::blocking::BlockingExecutor;
use buck2_fs::paths::abs_norm_path::AbsNormPath;
use buck2_fs::paths::abs_norm_path::AbsNormPathBuf;
use buck2_hash::BuckDashMap;
use buck2_hash::StdBuckHashMap;
use chrono::DateTime;
use chrono::Utc;
use dupe::Dupe;
use crate::incremental_actions_helper::IncrementalPathMap;
use crate::materializers::deferred::artifact_tree::ArtifactMetadata;
use crate::sqlite::tables::incremental_state_table::IncrementalStateSqliteTable;
/// Hand-maintained schema version for the incremental state sqlite db.
/// PLEASE bump this version if you are making a breaking change to the
/// incremental state sqlite db schema!
///
/// If you forget to bump this version,
/// then you can fix forward by bumping the `buck2.sqlite_incremental_state_version`
/// buckconfig in the project root's .buckconfig.
pub const INCREMENTAL_DB_SCHEMA_VERSION: u64 = 0;
pub(crate) type IncrementalState = BuckDashMap<String, Arc<IncrementalPathMap>>;
pub struct IncrementalDbState {
pub db: Option<IncrementalStateSqliteDb>,
pub state: IncrementalState,
}
impl IncrementalDbState {
pub fn db_disabled() -> Self {
Self {
db: None,
state: BuckDashMap::default(),
}
}
}
impl IncrementalDbState {
pub(crate) fn get(&self, key: &str) -> Option<Arc<IncrementalPathMap>> {
self.state.get(key).map(|s| s.dupe())
}
pub(crate) fn insert(&self, key: &str, value: IncrementalPathMap) {
match &self.db {
Some(db) => {
// (run_action_key, short_path) combination is the primary key so it should replace the current entry,
// but let's delete it beforehand just in case so it doesn't keep on inserting more entries. We don't need
// to do this without db because a map will automatically rewrite the current entry
self.delete(key);
self.state.insert(key.to_owned(), value.clone().into());
if let Err(e) = db.incremental_state_table().insert(key.to_owned(), value) {
let _unused = soft_error!(
"insert_to_incremental_db",
buck2_error::buck2_error!(
buck2_error::ErrorTag::Tier0,
"Failed to insert {} into sqlite db. {}",
key, e
),
quiet: true
);
};
}
None => {
self.state.insert(key.to_owned(), value.clone().into());
}
}
}
pub(crate) fn delete(&self, key: &str) {
if let Some(db) = &self.db
&& let Err(e) = db.incremental_state_table().delete(key.to_owned())
{
// This should be converted into a real error later, marking as a soft error for now as the row not existing
// might show up as an error but doesn't actually matter in reality.
let _unused = soft_error!(
"delete_from_incremental_db",
buck2_error::buck2_error!(
buck2_error::ErrorTag::Tier0,
"Failed to remove incremental path map from sqlite db. {}",
e
),
quiet: true
);
}
self.state.remove(key);
}
}
#[derive(Debug)]
pub struct IncrementalStateEntry {
pub path: ProjectRelativePathBuf,
pub metadata: ArtifactMetadata,
pub last_access_time: DateTime<Utc>,
}
/// Concrete implementation of SqliteTable for IncrementalStateSqliteTable
impl SqliteTable for IncrementalStateSqliteTable {
fn create_table(&self) -> buck2_error::Result<()> {
IncrementalStateSqliteTable::create_table(self)
}
}
/// DB that opens the sqlite connection to the incremental state db on disk and
/// holds all the sqlite tables we need for storing/querying incremental state
pub struct IncrementalStateSqliteDb {
tables: SqliteTables<IncrementalStateSqliteTable>,
/// A unique ID identifying this particular instance of the database. This will reset when we
/// recreate it.
identity: SqliteIdentity,
}
impl SqliteDb for IncrementalStateSqliteDb {
type StateType = IncrementalState;
type TableType = IncrementalStateSqliteTable;
fn new(tables: SqliteTables<Self::TableType>) -> buck2_error::Result<Self> {
let identity = tables.get_identity()?;
Ok(Self { tables, identity })
}
fn open_tables(path: &AbsNormPath) -> buck2_error::Result<SqliteTables<Self::TableType>> {
let connection = SqliteTables::<Self::TableType>::create_connection(path)?;
let incremental_state_table = IncrementalStateSqliteTable::new(connection.dupe());
Ok(SqliteTables::new(incremental_state_table, connection))
}
fn identity(&self) -> &SqliteIdentity {
&self.identity
}
}
impl IncrementalStateSqliteDb {
/// Given path to the sqlite DB, attempts to read `IncrementalState` from the DB. If we encounter
/// any failure along the way, such as if the DB path does not exist, the sqlite read fails,
/// or the DB has a different set of versions than the versions this buck2 expects, we
/// throw away the existing DB and initialize a new DB. Returns (1) the connected sqlite DB and
/// (2) the `IncrementalState` if loading was successful or the load error.
pub async fn initialize(
incremental_state_dir: AbsNormPathBuf,
versions: StdBuckHashMap<String, String>,
current_instance_metadata: StdBuckHashMap<String, String>,
io_executor: Arc<dyn BlockingExecutor>,
reject_identity: Option<&SqliteIdentity>,
) -> buck2_error::Result<IncrementalDbState> {
io_executor
.execute_io_inline(|| {
Self::initialize_incremental_sqlite_db(
incremental_state_dir,
versions,
current_instance_metadata,
reject_identity,
)
})
.await
}
fn initialize_incremental_sqlite_db(
incremental_state_dir: AbsNormPathBuf,
versions: StdBuckHashMap<String, String>,
current_instance_metadata: StdBuckHashMap<String, String>,
reject_identity: Option<&SqliteIdentity>,
) -> buck2_error::Result<IncrementalDbState> {
let reject_identity = reject_identity.cloned();
let (db, state) = match Self::get_sqlite_db(
&incremental_state_dir,
&versions,
current_instance_metadata.clone(),
reject_identity.as_ref(),
) {
Ok(db) => match db.tables.domain_table.read_incremental_state() {
Ok(state) => (db, Ok(state)),
Err(e) => {
let db = Self::create_sqlite_db(
incremental_state_dir,
versions,
current_instance_metadata,
)?;
(db, Err(e))
}
},
Err(e) => {
let db = Self::create_sqlite_db(
incremental_state_dir,
versions,
current_instance_metadata,
)?;
(db, Err(e))
}
};
match state {
Ok(state) => Ok(IncrementalDbState {
db: Some(db),
state,
}),
Err(e) => {
tracing::debug!(
"Failed to read/load incremental state. Build will continue as if the state is empty. {}",
e
);
Ok(IncrementalDbState {
db: Some(db),
state: BuckDashMap::default(),
})
}
}
}
pub(crate) fn incremental_state_table(&self) -> &IncrementalStateSqliteTable {
&self.tables.domain_table
}
}
#[allow(unused)] // Used by test modules
pub(crate) fn testing_incremental_state_sqlite_db(
fs: &ProjectRoot,
versions: StdBuckHashMap<String, String>,
metadata: StdBuckHashMap<String, String>,
reject_identity: Option<&SqliteIdentity>,
) -> buck2_error::Result<IncrementalDbState> {
IncrementalStateSqliteDb::initialize_incremental_sqlite_db(
fs.resolve(ProjectRelativePath::unchecked_new(
"buck-out/v2/cache/incremental_state",
)),
versions,
metadata,
reject_identity,
)
}
#[cfg(test)]
mod tests {
use buck2_core::fs::project::ProjectRootTemp;
use buck2_events::daemon_id::DaemonId;
use starlark_map::small_map::SmallMap;
use super::*;
use crate::incremental_actions_helper::IncrementalPathMap;
#[test]
fn test_initialize_incremental_sqlite_db() -> buck2_error::Result<()> {
fn testing_metadatas() -> Vec<StdBuckHashMap<String, String>> {
let metadata = buck2_events::metadata::collect(&DaemonId::new());
let mut metadatas = vec![metadata; 5];
for (i, metadata) in metadatas.iter_mut().enumerate() {
metadata.insert("version".to_owned(), i.to_string());
}
metadatas
}
fn assert_metadata_matches(
mut have: StdBuckHashMap<String, String>,
want: &StdBuckHashMap<String, String>,
) {
// Remove the key we inject (and check it's there).
have.remove("timestamp_on_initialization").unwrap();
assert_eq!(have, *want);
}
let fs = ProjectRootTemp::new()?;
let run_action_key = "test_action".to_owned();
let mut mapping = SmallMap::new();
mapping.insert(
buck2_fs::paths::forward_rel_path::ForwardRelativePathBuf::unchecked_new(
"test_file".to_owned(),
),
ProjectRelativePathBuf::unchecked_new("buck-out/content_hash/test_file".to_owned()),
);
let incremental_path_map = IncrementalPathMap::new(mapping);
let metadatas = testing_metadatas();
let v0 = StdBuckHashMap::from([("version".to_owned(), "0".to_owned())]);
let v1 = StdBuckHashMap::from([("version".to_owned(), "1".to_owned())]);
// Initialize with non-existent DB (should create new DB)
{
let IncrementalDbState { db, state } = testing_incremental_state_sqlite_db(
fs.path(),
v0.clone(),
metadatas[0].clone(),
None,
)
.unwrap();
let db = db.unwrap();
assert!(state.is_empty());
assert_metadata_matches(db.tables.created_by_table.read_all()?, &metadatas[0]);
db.incremental_state_table()
.insert(run_action_key.clone(), incremental_path_map.clone())
.unwrap();
}
// Load existing DB with same version (should load successfully)
{
let IncrementalDbState { db, state } =
testing_incremental_state_sqlite_db(fs.path(), v0, metadatas[1].clone(), None)
.unwrap();
assert_eq!(state.len(), 1);
assert!(state.contains_key(&run_action_key));
let action_state = state.get(&run_action_key).unwrap();
assert_eq!(action_state.iter().count(), 1);
assert_metadata_matches(
db.unwrap().tables.created_by_table.read_all()?,
&metadatas[0],
);
}
// Load with different version (should recreate DB)
{
let IncrementalDbState { db, state } = testing_incremental_state_sqlite_db(
fs.path(),
v1.clone(),
metadatas[2].clone(),
None,
)
.unwrap();
let db = db.unwrap();
assert!(state.is_empty());
assert_metadata_matches(db.tables.created_by_table.read_all()?, &metadatas[2]);
db.incremental_state_table()
.insert(run_action_key.clone(), incremental_path_map.clone())
.unwrap();
}
// Load existing DB with same version again (should load successfully)
let identity = {
let IncrementalDbState { db, state } = testing_incremental_state_sqlite_db(
fs.path(),
v1.clone(),
metadatas[3].clone(),
None,
)
.unwrap();
let db = db.unwrap();
assert_eq!(state.len(), 1);
assert!(state.contains_key(&run_action_key));
assert_metadata_matches(db.tables.created_by_table.read_all()?, &metadatas[2]);
db.identity().clone()
};
// Reject specific identity (should recreate DB)
{
let IncrementalDbState { db, state } = testing_incremental_state_sqlite_db(
fs.path(),
v1,
metadatas[4].clone(),
Some(&identity),
)
.unwrap();
assert!(state.is_empty());
assert_metadata_matches(
db.unwrap().tables.created_by_table.read_all()?,
&metadatas[4],
);
}
Ok(())
}
}