aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2026-09-13 08:49:16 -0500
committerGitHub <noreply@github.com>2026-09-13 08:49:16 -0500
commit92167f6f1657703959d6ea94e4af82169c5c5fba (patch)
treed3de16e660063c8a75688c57e312ba15e8831dde /src
parentf71b893e6b625b12906537166b332d550ed5f115 (diff)
parent50821750bb392ff06e51d6ef24fe5d9d95af10fc (diff)
downloadslskd-92167f6f1657703959d6ea94e4af82169c5c5fba.tar.gz
Merge pull request #1848 from slskd/checkpoint
Checkpoint share database after scan, vacuum and checkpoint backup after backup
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Shares/IShareRepository.cs5
-rw-r--r--src/slskd/Shares/ShareScanner.cs4
-rw-r--r--src/slskd/Shares/SqliteShareRepository.cs54
3 files changed, 58 insertions, 5 deletions
diff --git a/src/slskd/Shares/IShareRepository.cs b/src/slskd/Shares/IShareRepository.cs
index 3b57c16f..e9799663 100644
--- a/src/slskd/Shares/IShareRepository.cs
+++ b/src/slskd/Shares/IShareRepository.cs
@@ -53,6 +53,11 @@ namespace slskd.Shares
void BackupTo(IShareRepository repository);
/// <summary>
+ /// Checkpoints the current database, flushing the contents of the WAL (write ahead log) into the table(s).
+ /// </summary>
+ void Checkpoint();
+
+ /// <summary>
/// Counts the number of directories in the database.
/// </summary>
/// <param name="parentDirectory">The optional directory prefix used for counting subdirectories.</param>
diff --git a/src/slskd/Shares/ShareScanner.cs b/src/slskd/Shares/ShareScanner.cs
index 7c93d437..c36c0998 100644
--- a/src/slskd/Shares/ShareScanner.cs
+++ b/src/slskd/Shares/ShareScanner.cs
@@ -358,6 +358,10 @@ namespace slskd.Shares
Log.Debug("Vacuuming...");
repository.Vacuum();
Log.Debug("Vacuumed successfully");
+
+ Log.Debug("Checkpointing...");
+ repository.Checkpoint();
+ Log.Debug("Checkpointed successfully");
}
catch (OperationCanceledException)
{
diff --git a/src/slskd/Shares/SqliteShareRepository.cs b/src/slskd/Shares/SqliteShareRepository.cs
index ed594209..6d84308a 100644
--- a/src/slskd/Shares/SqliteShareRepository.cs
+++ b/src/slskd/Shares/SqliteShareRepository.cs
@@ -84,14 +84,58 @@ namespace slskd.Shares
/// <param name="repository">The destination repository.</param>
public void BackupTo(IShareRepository repository)
{
- using var sourceConn = GetConnection(ConnectionString);
- using var backupConn = GetConnection(repository.ConnectionString);
- sourceConn.BackupDatabase(backupConn);
+ using (var sourceConn = GetConnection(ConnectionString))
+ using (var backupConn = GetConnection(repository.ConnectionString))
+ {
+ sourceConn.BackupDatabase(backupConn);
+ }
+
+ using var backupRepository = new SqliteShareRepository(repository.ConnectionString);
Log.Debug("Vacuuming backup");
- using var cmd = new SqliteCommand("VACUUM", backupConn);
- cmd.ExecuteNonQuery();
+ backupRepository.Vacuum();
Log.Debug("Backup vacuumed successfully");
+
+ Log.Debug("Checkpointing backup");
+ backupRepository.Checkpoint();
+ Log.Debug("Backup checkpointed successfully");
+ }
+
+ /// <summary>
+ /// Checkpoints the current database, flushing the contents of the WAL (write ahead log) into the table(s).
+ /// </summary>
+ public void Checkpoint()
+ {
+ const int maxAttempts = 3;
+
+ using var conn = GetConnection();
+
+ for (var attempt = 1; attempt <= maxAttempts; attempt++)
+ {
+ using var cmd = new SqliteCommand("PRAGMA wal_checkpoint(TRUNCATE);", conn);
+ using var reader = cmd.ExecuteReader();
+
+ reader.Read();
+
+ var busy = reader.GetInt32(0); // another operation prevented the checkpoint (true/false)
+ var log = reader.GetInt32(1); // number of frames in the WAL at the start
+ var checkpointed = reader.GetInt32(2); // number of frames moved from the WAL to the db
+
+ if (busy == 0 && checkpointed == log)
+ {
+ Log.Debug("WAL checkpoint complete; {Log} frames in log, {Checkpointed} checkpointed", log, checkpointed);
+ return;
+ }
+
+ Log.Warning("WAL checkpoint attempt {Attempt} of {MaxAttempts} did not fully complete; {Log} frames in log, {Checkpointed} checkpointed", attempt, maxAttempts, log, checkpointed);
+
+ if (attempt < maxAttempts)
+ {
+ System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1));
+ }
+ }
+
+ throw new TimeoutException($"Failed to fully checkpoint the shares database after {maxAttempts} attempts");
}
/// <summary>