|
| 1 | +package kine |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "database/sql" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "time" |
| 9 | + |
| 10 | + _ "github.com/mattn/go-sqlite3" |
| 11 | + "github.com/rs/zerolog/log" |
| 12 | +) |
| 13 | + |
| 14 | +// repairWALIfCorrupt runs a quick integrity check against the kine SQLite |
| 15 | +// database. If the check fails (or the DB cannot be opened at all) and |
| 16 | +// --db-wal-repair is enabled, the WAL artefacts (state.db-wal, state.db-shm) |
| 17 | +// are removed so that SQLite falls back to the last cleanly checkpointed state. |
| 18 | +// Without the flag, a fatal log is emitted with instructions for manual recovery |
| 19 | +// so that operators are never silently left in a broken boot loop. |
| 20 | +func (s *service) repairWALIfCorrupt() { |
| 21 | + dbPath := filepath.Join(s.databaseDir, "state.db") |
| 22 | + |
| 23 | + if _, err := os.Stat(dbPath); os.IsNotExist(err) { |
| 24 | + return |
| 25 | + } |
| 26 | + |
| 27 | + db, err := sql.Open("sqlite3", dbPath) |
| 28 | + if err != nil { |
| 29 | + s.handleCorruption(dbPath, "cannot open SQLite DB for integrity check: %v", err) |
| 30 | + return |
| 31 | + } |
| 32 | + defer db.Close() |
| 33 | + |
| 34 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 35 | + defer cancel() |
| 36 | + |
| 37 | + rows, err := db.QueryContext(ctx, "PRAGMA quick_check") |
| 38 | + if err != nil { |
| 39 | + s.handleCorruption(dbPath, "SQLite quick_check query failed: %v", err) |
| 40 | + return |
| 41 | + } |
| 42 | + defer rows.Close() |
| 43 | + |
| 44 | + if rows.Next() { |
| 45 | + var result string |
| 46 | + if rows.Scan(&result) == nil && result == "ok" { |
| 47 | + log.Debug().Str("component", "kine").Msg("SQLite integrity check passed") |
| 48 | + return |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + s.handleCorruption(dbPath, "SQLite integrity check failed") |
| 53 | +} |
| 54 | + |
| 55 | +func (s *service) handleCorruption(dbPath string, format string, args ...any) { |
| 56 | + if s.dbWALRepair { |
| 57 | + log.Warn().Str("component", "kine").Msgf(format+", removing WAL artefacts to recover from unclean shutdown", args...) |
| 58 | + removeWALArtefacts(dbPath) |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + log.Fatal().Str("component", "kine").Msgf( |
| 63 | + format+". The SQLite WAL artefacts may be corrupt after an unclean shutdown. "+ |
| 64 | + "Remove %s-wal and %s-shm manually, or restart with --db-wal-repair to remove them automatically.", |
| 65 | + append(args, dbPath, dbPath)..., |
| 66 | + ) |
| 67 | +} |
| 68 | + |
| 69 | +func removeWALArtefacts(dbPath string) { |
| 70 | + for _, suffix := range []string{"-wal", "-shm"} { |
| 71 | + path := dbPath + suffix |
| 72 | + if err := os.Remove(path); err != nil && !os.IsNotExist(err) { |
| 73 | + log.Warn().Str("component", "kine").Msgf("failed to remove %s: %v", path, err) |
| 74 | + } else if err == nil { |
| 75 | + log.Info().Str("component", "kine").Msgf("removed %s", path) |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments