Skip to content

Commit d229dbf

Browse files
committed
fix: debug sql lock error
1 parent c6c6948 commit d229dbf

File tree

3 files changed

+38
-21
lines changed

3 files changed

+38
-21
lines changed

packages/edge/infra/client/manager/src/ctx.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -975,8 +975,7 @@ impl Ctx {
975975
// Delete entries that aren't in our valid images table
976976
let deleted = sqlx::query(indoc!(
977977
"
978-
DELETE
979-
FROM images_cache
978+
DELETE FROM images_cache
980979
WHERE image_id NOT IN (
981980
SELECT image_id FROM __valid_images
982981
)

packages/edge/infra/client/manager/src/image_download_handler.rs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ impl ImageDownloadHandler {
123123
let (removed_count, removed_bytes) = if images_dir_size as u64 + image_download_size
124124
> ctx.config().images.max_cache_size()
125125
{
126+
tracing::info!(image_id=?image_config.id, ?images_dir_size, ?image_download_size, max=?ctx.config().images.max_cache_size(), "-------------------------");
127+
126128
// Fetch as many images as it takes to clear up enough space for this new image.
127129
// Ordered by LRU
128130
let rows = sqlx::query_as::<_, (Uuid, i64)>(indoc!(
@@ -148,10 +150,14 @@ impl ImageDownloadHandler {
148150
a.image_id IS NULL
149151
ORDER BY ic.last_used_ts
150152
)
151-
SELECT image_id, size
152-
FROM cumulative_sizes
153-
WHERE running_total - size < ?2
154-
ORDER BY last_used_ts
153+
DELETE FROM images_cache
154+
WHERE image_id IN (
155+
SELECT image_id
156+
FROM cumulative_sizes
157+
WHERE running_total - size < ?2
158+
ORDER BY last_used_ts
159+
)
160+
RETURNING image_id, size
155161
",
156162
))
157163
.bind(image_config.id)
@@ -171,24 +177,32 @@ impl ImageDownloadHandler {
171177
"no inactive images to delete to make space for new image, downloading anyway",
172178
);
173179
} else {
174-
tracing::debug!(count=?rows_len, "cache full, clearing LRU entries");
180+
tracing::debug!(image_id=?image_config.id, count=?rows_len, "cache full, clearing LRU entries");
175181
}
176182

183+
tracing::info!(image_id=?image_config.id, count=?rows_len, "foo -------------------------");
184+
177185
let mut total_removed_bytes = 0;
178186

179187
for (image_id, size) in rows {
188+
tracing::info!(image_id=?image_config.id, image_id2=?image_id, "bar -------------------------");
189+
180190
total_removed_bytes += size;
181191

182192
// NOTE: The sql query does not return the current image id so there is no chance
183193
// for a deadlock here
184194
// Acquire lock on entry
185195
let entry = self.downloads.entry_async(image_id).await;
186196

197+
tracing::info!(image_id=?image_config.id, image_id2=?image_id, "bar2 -------------------------");
198+
187199
match fs::remove_dir_all(ctx.image_path(image_id)).await {
188200
Err(e) if e.kind() == ErrorKind::NotFound => {}
189201
res => res.context("failed to delete image dir")?,
190202
}
191203

204+
tracing::info!(image_id=?image_config.id, image_id2=?image_id, "bar3 -------------------------");
205+
192206
// Remove entry and release lock
193207
if let Entry::Occupied(entry) = entry {
194208
let _ = entry.remove();
@@ -200,6 +214,8 @@ impl ImageDownloadHandler {
200214
(0, 0)
201215
};
202216

217+
tracing::info!(image_id=?image_config.id, "baz -------------------------");
218+
203219
metrics::IMAGE_CACHE_COUNT.set(cache_count + 1 - removed_count);
204220
metrics::IMAGE_CACHE_SIZE
205221
.set(images_dir_size + image_download_size as i64 - removed_bytes);
@@ -215,8 +231,12 @@ impl ImageDownloadHandler {
215231
.execute(&mut *tx)
216232
.await?;
217233

234+
tracing::info!(image_id=?image_config.id, "baz2 -------------------------");
235+
218236
tx.commit().await?;
219237

238+
tracing::info!(image_id=?image_config.id, "baz3 -------------------------");
239+
220240
// Release lock on sqlite pool
221241
drop(conn);
222242

@@ -237,7 +257,7 @@ impl ImageDownloadHandler {
237257
metrics::IMAGE_CACHE_SIZE.set(images_dir_size + image_size as i64 - removed_bytes);
238258

239259
// Update state to signify download completed successfully
240-
let foo = sqlx::query(indoc!(
260+
sqlx::query(indoc!(
241261
"
242262
UPDATE images_cache
243263
SET

packages/edge/infra/client/manager/src/utils/mod.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ use pegboard_config::Config;
1515
use sql::SqlitePoolExt;
1616
use sqlx::{
1717
migrate::MigrateDatabase,
18-
sqlite::{SqliteAutoVacuum, SqliteConnectOptions, SqlitePoolOptions, SqliteSynchronous},
19-
Executor, Sqlite, SqlitePool,
18+
sqlite::{
19+
SqliteAutoVacuum, SqliteConnectOptions, SqliteJournalMode, SqliteLockingMode,
20+
SqlitePoolOptions, SqliteSynchronous,
21+
},
22+
Sqlite, SqlitePool,
2023
};
2124
use tokio::{
2225
fs,
@@ -111,24 +114,19 @@ pub async fn init_sqlite_db(config: &Config) -> Result<SqlitePool> {
111114
async fn build_sqlite_pool(db_url: &str) -> Result<SqlitePool> {
112115
let opts = db_url
113116
.parse::<SqliteConnectOptions>()?
114-
// Set synchronous mode to NORMAL for performance and data safety balance
115-
.synchronous(SqliteSynchronous::Normal)
116117
// Set busy timeout to 5 seconds to avoid "database is locked" errors
117118
.busy_timeout(Duration::from_secs(5))
118119
// Enable foreign key constraint enforcement
119120
.foreign_keys(true)
120121
// Enable auto vacuuming and set it to incremental mode for gradual space reclaiming
121-
.auto_vacuum(SqliteAutoVacuum::Incremental);
122+
.auto_vacuum(SqliteAutoVacuum::Incremental)
123+
// Set synchronous mode to NORMAL for performance and data safety balance
124+
.synchronous(SqliteSynchronous::Normal)
125+
// Increases write performance
126+
.journal_mode(SqliteJournalMode::Wal)
127+
.locking_mode(SqliteLockingMode::Normal);
122128

123129
let pool = SqlitePoolOptions::new()
124-
.after_connect(|conn, _meta| {
125-
Box::pin(async move {
126-
// NOTE: sqlx doesn't seem to have a WAL2 option so we set it with a PRAGMA query
127-
conn.execute("PRAGMA journal_mode = WAL2").await?;
128-
129-
Ok(())
130-
})
131-
})
132130
// Open connection immediately on startup
133131
.min_connections(1)
134132
.connect_with(opts)

0 commit comments

Comments
 (0)