-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLite.Maintenance.Execution.cs
More file actions
458 lines (425 loc) · 18.1 KB
/
Copy pathSQLite.Maintenance.Execution.cs
File metadata and controls
458 lines (425 loc) · 18.1 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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
using System.Diagnostics;
using Microsoft.Data.Sqlite;
using SQLitePCL;
namespace DBAClientX;
public partial class SQLite
{
private const int MaximumBackupPagesPerStep = 4096;
/// <summary>
/// Runs an SQLite integrity check on a dedicated thread so the provider's synchronous execution cannot block
/// an asynchronous caller or scheduler.
/// </summary>
/// <param name="database">Source SQLite database path.</param>
/// <param name="fullCheck">When true, uses <c>PRAGMA integrity_check</c>; otherwise uses <c>PRAGMA quick_check</c>.</param>
/// <param name="maxIssues">Maximum number of integrity issues returned by SQLite.</param>
/// <param name="busyTimeoutMs">Optional busy timeout in milliseconds.</param>
/// <param name="cancellationToken">Token used to interrupt the native SQLite command.</param>
/// <returns>A task containing the integrity result.</returns>
public virtual Task<SqliteIntegrityCheckResult> CheckIntegrityAsync(
string database,
bool fullCheck = false,
int maxIssues = 10,
int? busyTimeoutMs = null,
CancellationToken cancellationToken = default)
{
ValidateDatabasePath(database);
if (maxIssues <= 0)
{
throw new ArgumentOutOfRangeException(nameof(maxIssues), "Maximum issue count must be positive.");
}
EnsureNoActiveTransaction();
return RunDedicatedMaintenanceAsync(
() => CheckIntegrityCore(database, fullCheck, maxIssues, busyTimeoutMs, cancellationToken),
cancellationToken);
}
/// <summary>
/// Copies an SQLite database incrementally on a dedicated thread using SQLite's online backup API.
/// </summary>
/// <param name="sourceDatabase">Source SQLite database path.</param>
/// <param name="destinationDatabase">Destination SQLite database path.</param>
/// <param name="options">Backup behavior options.</param>
/// <param name="progress">Optional page-based progress observer.</param>
/// <param name="cancellationToken">Token used to stop between backup steps and interrupt native work.</param>
/// <returns>A task containing the completed backup details.</returns>
public virtual Task<SqliteBackupResult> BackupDatabaseIncrementalAsync(
string sourceDatabase,
string destinationDatabase,
SqliteBackupOptions? options = null,
IProgress<SqliteBackupProgress>? progress = null,
CancellationToken cancellationToken = default)
{
ValidateDatabasePath(sourceDatabase);
ValidateDatabasePath(destinationDatabase);
EnsureNoActiveTransaction();
SqliteBackupOptions effectiveOptions = SnapshotBackupOptions(options);
ValidateBackupOptions(effectiveOptions);
return RunDedicatedMaintenanceAsync(
() => BackupDatabaseIncrementalCore(
sourceDatabase,
destinationDatabase,
effectiveOptions,
progress,
cancellationToken),
cancellationToken);
}
private SqliteIntegrityCheckResult CheckIntegrityCore(
string database,
bool fullCheck,
int maxIssues,
int? busyTimeoutMs,
CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
if (!File.Exists(database))
{
throw new FileNotFoundException($"SQLite database file does not exist: {database}", database);
}
var stopwatch = Stopwatch.StartNew();
using var connection = new SqliteConnection(BuildOperationalConnectionString(database, readOnly: true));
connection.Open();
ApplyBusyTimeout(connection, busyTimeoutMs);
using CancellationTokenRegistration registration = cancellationToken.Register(
static state => raw.sqlite3_interrupt(((SqliteConnection)state!).Handle),
connection);
using var command = connection.CreateCommand();
command.CommandText = fullCheck
? $"PRAGMA integrity_check({maxIssues});"
: $"PRAGMA quick_check({maxIssues});";
if (CommandTimeout > 0)
{
command.CommandTimeout = CommandTimeout;
}
var issues = new List<string>();
try
{
using var reader = command.ExecuteReader();
while (reader.Read())
{
cancellationToken.ThrowIfCancellationRequested();
string value = reader.IsDBNull(0) ? string.Empty : reader.GetString(0).Trim();
if (value.Length > 0 && !string.Equals(value, "ok", StringComparison.OrdinalIgnoreCase))
{
issues.Add(value);
}
}
}
catch (SqliteException ex) when (
cancellationToken.IsCancellationRequested &&
IsProviderCancellationException(ex))
{
throw CreateCallerCancellationException(ex, cancellationToken);
}
stopwatch.Stop();
return new SqliteIntegrityCheckResult
{
IsHealthy = issues.Count == 0,
IsFullCheck = fullCheck,
Issues = issues,
Elapsed = stopwatch.Elapsed
};
}
private SqliteBackupResult BackupDatabaseIncrementalCore(
string sourceDatabase,
string destinationDatabase,
SqliteBackupOptions options,
IProgress<SqliteBackupProgress>? progress,
CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
string sourcePath = Path.GetFullPath(sourceDatabase);
string destinationPath = Path.GetFullPath(destinationDatabase);
if (string.Equals(sourcePath, destinationPath, StringComparison.OrdinalIgnoreCase))
{
throw new ArgumentException("Source and destination database paths must be different.", nameof(destinationDatabase));
}
if (!File.Exists(sourcePath))
{
throw new FileNotFoundException($"SQLite database file does not exist: {sourcePath}", sourcePath);
}
string? destinationDirectory = Path.GetDirectoryName(destinationPath);
if (!string.IsNullOrWhiteSpace(destinationDirectory))
{
Directory.CreateDirectory(destinationDirectory);
}
bool destinationExisted = File.Exists(destinationPath);
if (destinationExisted)
{
if (!options.OverwriteDestination)
{
throw new IOException($"SQLite backup destination already exists: {destinationPath}");
}
}
string workingPath = destinationExisted
? $"{destinationPath}.{Guid.NewGuid():N}.partial"
: destinationPath;
var stopwatch = Stopwatch.StartNew();
bool completed = false;
int totalPages = 0;
try
{
{
using var source = new SqliteConnection(BuildOperationalConnectionString(sourcePath, readOnly: true));
using var destination = new SqliteConnection(BuildOperationalConnectionString(workingPath));
source.Open();
destination.Open();
using CancellationTokenRegistration sourceRegistration = cancellationToken.Register(
static state => raw.sqlite3_interrupt(((SqliteConnection)state!).Handle),
source);
using CancellationTokenRegistration destinationRegistration = cancellationToken.Register(
static state => raw.sqlite3_interrupt(((SqliteConnection)state!).Handle),
destination);
sqlite3_backup? backup = raw.sqlite3_backup_init(destination.Handle, "main", source.Handle, "main");
if (backup == null || backup.IsInvalid)
{
string message = raw.sqlite3_errmsg(destination.Handle).utf8_to_string();
throw new DbaQueryExecutionException("Failed to initialize SQLite online backup.", "SQLite online backup", new InvalidOperationException(message));
}
int resultCode = raw.SQLITE_OK;
int remainingPages = 0;
TimeSpan cumulativeBusyDuration = TimeSpan.Zero;
Exception? backupFailure = null;
try
{
while (resultCode != raw.SQLITE_DONE)
{
cancellationToken.ThrowIfCancellationRequested();
var stepStopwatch = Stopwatch.StartNew();
resultCode = raw.sqlite3_backup_step(backup, options.PagesPerStep);
stepStopwatch.Stop();
totalPages = raw.sqlite3_backup_pagecount(backup);
remainingPages = raw.sqlite3_backup_remaining(backup);
ReportBackupProgress(progress, totalPages, remainingPages, stopwatch.Elapsed);
if (resultCode == raw.SQLITE_DONE)
{
break;
}
if (resultCode != raw.SQLITE_OK && resultCode != raw.SQLITE_BUSY && resultCode != raw.SQLITE_LOCKED)
{
string message = raw.sqlite3_errmsg(destination.Handle).utf8_to_string();
throw new DbaQueryExecutionException(
$"SQLite online backup failed with result code {resultCode}.",
"SQLite online backup",
new InvalidOperationException(message));
}
bool isBusy = resultCode == raw.SQLITE_BUSY || resultCode == raw.SQLITE_LOCKED;
if (isBusy)
{
cumulativeBusyDuration += stepStopwatch.Elapsed;
ThrowIfBusyRetryTimeoutExceeded(cumulativeBusyDuration, options.BusyRetryTimeout);
}
TimeSpan delay = isBusy
? options.BusyRetryDelay
: options.StepDelay;
if (isBusy && delay > TimeSpan.Zero)
{
var delayStopwatch = Stopwatch.StartNew();
WaitWithCancellation(delay, cancellationToken);
delayStopwatch.Stop();
cumulativeBusyDuration += delayStopwatch.Elapsed;
ThrowIfBusyRetryTimeoutExceeded(cumulativeBusyDuration, options.BusyRetryTimeout);
}
else
{
WaitWithCancellation(delay, cancellationToken);
}
}
}
catch (Exception exception)
{
backupFailure = exception;
throw;
}
finally
{
int finishCode = raw.sqlite3_backup_finish(backup);
if (backupFailure == null && resultCode == raw.SQLITE_DONE && finishCode != raw.SQLITE_OK)
{
string message = raw.sqlite3_errmsg(destination.Handle).utf8_to_string();
throw new DbaQueryExecutionException(
$"SQLite online backup finalization failed with result code {finishCode}.",
"SQLite online backup",
new InvalidOperationException(message));
}
}
}
if (destinationExisted)
{
ReplaceBackupDestination(workingPath, destinationPath);
}
stopwatch.Stop();
completed = true;
ReportBackupProgress(progress, totalPages, 0, stopwatch.Elapsed);
return new SqliteBackupResult
{
SourceDatabase = sourcePath,
DestinationDatabase = destinationPath,
CopiedPages = totalPages,
DestinationLengthBytes = new FileInfo(destinationPath).Length,
Elapsed = stopwatch.Elapsed
};
}
catch (SqliteException ex) when (
cancellationToken.IsCancellationRequested &&
IsProviderCancellationException(ex))
{
throw CreateCallerCancellationException(ex, cancellationToken);
}
finally
{
if (!completed && options.DeleteDestinationOnFailure)
{
TryDeleteBackupDestination(workingPath);
}
}
}
private static Task<T> RunDedicatedMaintenanceAsync<T>(Func<T> operation, CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled<T>(cancellationToken);
}
return Task.Factory.StartNew(
operation,
CancellationToken.None,
TaskCreationOptions.DenyChildAttach | TaskCreationOptions.LongRunning,
TaskScheduler.Default);
}
private static void ValidateBackupOptions(SqliteBackupOptions options)
{
if (options.PagesPerStep <= 0)
{
throw new ArgumentOutOfRangeException(nameof(options.PagesPerStep), "Pages per step must be positive.");
}
if (options.PagesPerStep > MaximumBackupPagesPerStep)
{
throw new ArgumentOutOfRangeException(
nameof(options.PagesPerStep),
$"Pages per step cannot exceed {MaximumBackupPagesPerStep} so cancellation remains responsive.");
}
if (options.StepDelay < TimeSpan.Zero)
{
throw new ArgumentOutOfRangeException(nameof(options.StepDelay), "Step delay cannot be negative.");
}
if (options.BusyRetryDelay < TimeSpan.Zero)
{
throw new ArgumentOutOfRangeException(nameof(options.BusyRetryDelay), "Busy retry delay cannot be negative.");
}
if (options.BusyRetryTimeout < TimeSpan.Zero)
{
throw new ArgumentOutOfRangeException(nameof(options.BusyRetryTimeout), "Busy retry timeout cannot be negative.");
}
}
internal static SqliteBackupOptions SnapshotBackupOptions(SqliteBackupOptions? options)
{
SqliteBackupOptions source = options ?? new SqliteBackupOptions();
return new SqliteBackupOptions
{
PagesPerStep = source.PagesPerStep,
StepDelay = source.StepDelay,
BusyRetryDelay = source.BusyRetryDelay,
BusyRetryTimeout = source.BusyRetryTimeout,
OverwriteDestination = source.OverwriteDestination,
DeleteDestinationOnFailure = source.DeleteDestinationOnFailure
};
}
private static void ReplaceBackupDestination(string workingPath, string destinationPath)
{
var quarantinedSidecars = new List<KeyValuePair<string, string>>();
bool replaced = false;
try
{
foreach (string suffix in new[] { "-wal", "-shm", "-journal" })
{
string sidecarPath = destinationPath + suffix;
if (!File.Exists(sidecarPath))
{
continue;
}
string quarantinePath = $"{destinationPath}.{Guid.NewGuid():N}.stale{suffix}";
File.Move(sidecarPath, quarantinePath);
quarantinedSidecars.Add(new KeyValuePair<string, string>(sidecarPath, quarantinePath));
}
File.Replace(workingPath, destinationPath, null, ignoreMetadataErrors: true);
replaced = true;
}
finally
{
foreach (KeyValuePair<string, string> sidecar in quarantinedSidecars)
{
try
{
if (replaced)
{
File.Delete(sidecar.Value);
}
else if (!File.Exists(sidecar.Key))
{
File.Move(sidecar.Value, sidecar.Key);
}
}
catch
{
// A quarantined sidecar cannot affect the promoted backup; failed restoration is surfaced by
// the original replacement exception while cleanup remains best effort.
}
}
}
}
private static void ThrowIfBusyRetryTimeoutExceeded(TimeSpan elapsed, TimeSpan timeout)
{
if (timeout > TimeSpan.Zero && elapsed >= timeout)
{
throw new TimeoutException($"SQLite online backup cumulative busy or locked time reached {elapsed:g}.");
}
}
private static void ReportBackupProgress(
IProgress<SqliteBackupProgress>? progress,
int totalPages,
int remainingPages,
TimeSpan elapsed)
{
if (progress == null)
{
return;
}
int copiedPages = Math.Max(0, totalPages - remainingPages);
double percentage = totalPages > 0 ? copiedPages * 100d / totalPages : 0d;
progress.Report(new SqliteBackupProgress
{
TotalPages = totalPages,
CopiedPages = copiedPages,
RemainingPages = Math.Max(0, remainingPages),
PercentComplete = percentage,
Elapsed = elapsed
});
}
private static void WaitWithCancellation(TimeSpan delay, CancellationToken cancellationToken)
{
if (delay <= TimeSpan.Zero)
{
return;
}
if (cancellationToken.WaitHandle.WaitOne(delay))
{
cancellationToken.ThrowIfCancellationRequested();
}
}
private static void TryDeleteBackupDestination(string path)
{
try
{
foreach (string suffix in new[] { string.Empty, "-wal", "-shm" })
{
string candidate = path + suffix;
if (File.Exists(candidate))
{
File.Delete(candidate);
}
}
}
catch
{
// The caller receives the original backup failure; cleanup is best effort.
}
}
}