chore: configurable delete retention interval (#10030)

Command line flag, as it also needs to be able to take effect during
migration.
This commit is contained in:
Jakob Borg
2025-04-03 09:55:19 +02:00
committed by GitHub
parent b88aea34b6
commit 8a2d8ebf81
5 changed files with 62 additions and 37 deletions
+14 -4
View File
@@ -17,10 +17,12 @@ import (
)
type DB struct {
sql *sqlx.DB
localDeviceIdx int64
updateLock sync.Mutex
updatePoints int
sql *sqlx.DB
localDeviceIdx int64
deleteRetention time.Duration
updateLock sync.Mutex
updatePoints int
statementsMut sync.RWMutex
statements map[string]*sqlx.Stmt
@@ -29,6 +31,14 @@ type DB struct {
var _ db.DB = (*DB)(nil)
type Option func(*DB)
func WithDeleteRetention(d time.Duration) Option {
return func(s *DB) {
s.deleteRetention = d
}
}
func (s *DB) Close() error {
s.updateLock.Lock()
s.statementsMut.Lock()
+12 -5
View File
@@ -21,7 +21,7 @@ import (
const maxDBConns = 128
func Open(path string) (*DB, error) {
func Open(path string, opts ...Option) (*DB, error) {
// Open the database with options to enable foreign keys and recursive
// triggers (needed for the delete+insert triggers on row replace).
sqlDB, err := sqlx.Open(dbDriver, "file:"+path+"?"+commonOptions)
@@ -36,7 +36,7 @@ func Open(path string) (*DB, error) {
// https://www.sqlite.org/pragma.html#pragma_optimize
return nil, wrap(err, "PRAGMA optimize")
}
return openCommon(sqlDB)
return openCommon(sqlDB, opts...)
}
// Open the database with options suitable for the migration inserts. This
@@ -73,7 +73,7 @@ func OpenTemp() (*DB, error) {
return Open(path)
}
func openCommon(sqlDB *sqlx.DB) (*DB, error) {
func openCommon(sqlDB *sqlx.DB, opts ...Option) (*DB, error) {
if _, err := sqlDB.Exec(`PRAGMA auto_vacuum = INCREMENTAL`); err != nil {
return nil, wrap(err, "PRAGMA auto_vacuum")
}
@@ -85,8 +85,15 @@ func openCommon(sqlDB *sqlx.DB) (*DB, error) {
}
db := &DB{
sql: sqlDB,
statements: make(map[string]*sqlx.Stmt),
sql: sqlDB,
deleteRetention: defaultDeleteRetention,
statements: make(map[string]*sqlx.Stmt),
}
for _, opt := range opts {
opt(db)
}
if db.deleteRetention > 0 && db.deleteRetention < minDeleteRetention {
db.deleteRetention = minDeleteRetention
}
if err := db.runScripts("sql/schema/*"); err != nil {
+11 -4
View File
@@ -15,9 +15,10 @@ import (
)
const (
internalMetaPrefix = "dbsvc"
lastMaintKey = "lastMaint"
MaxDeletedFileAge = 180 * 24 * time.Hour
internalMetaPrefix = "dbsvc"
lastMaintKey = "lastMaint"
defaultDeleteRetention = 180 * 24 * time.Hour
minDeleteRetention = 24 * time.Hour
)
type Service struct {
@@ -101,12 +102,18 @@ func (s *Service) periodic(ctx context.Context) error {
}
func (s *Service) garbageCollectOldDeletedLocked() error {
if s.sdb.deleteRetention <= 0 {
l.Debugln("Delete retention is infinite, skipping cleanup")
return nil
}
// Remove deleted files that are marked as not needed (we have processed
// them) and they were deleted more than MaxDeletedFileAge ago.
l.Debugln("Forgetting deleted files older than", s.sdb.deleteRetention)
res, err := s.sdb.stmt(`
DELETE FROM files
WHERE deleted AND modified < ? AND local_flags & {{.FlagLocalNeeded}} == 0
`).Exec(time.Now().Add(-MaxDeletedFileAge).UnixNano())
`).Exec(time.Now().Add(-s.sdb.deleteRetention).UnixNano())
if err != nil {
return wrap(err)
}