@@ -11,6 +11,7 @@ import (
11
11
"context"
12
12
"fmt"
13
13
"io"
14
+ "os"
14
15
"path"
15
16
"strings"
16
17
"sync"
@@ -37,6 +38,7 @@ import (
37
38
"gitlab.com/postgres-ai/database-lab/v3/internal/retrieval/engine/postgres/tools/activity"
38
39
"gitlab.com/postgres-ai/database-lab/v3/internal/retrieval/engine/postgres/tools/cont"
39
40
"gitlab.com/postgres-ai/database-lab/v3/internal/retrieval/engine/postgres/tools/defaults"
41
+ "gitlab.com/postgres-ai/database-lab/v3/internal/retrieval/engine/postgres/tools/fs"
40
42
"gitlab.com/postgres-ai/database-lab/v3/internal/retrieval/engine/postgres/tools/health"
41
43
"gitlab.com/postgres-ai/database-lab/v3/internal/retrieval/engine/postgres/tools/pgtool"
42
44
"gitlab.com/postgres-ai/database-lab/v3/internal/retrieval/engine/postgres/tools/query"
@@ -65,6 +67,9 @@ const (
65
67
// WAL parsing constants.
66
68
walNameLen = 24
67
69
pgVersion10 = 10
70
+
71
+ logDirName = "log"
72
+ defaultLogRetentionDays = 7
68
73
)
69
74
70
75
var defaultRecoveryCfg = map [string ]string {
@@ -351,6 +356,11 @@ func (p *PhysicalInitial) run(ctx context.Context) (err error) {
351
356
return errors .Wrapf (err , "failed to create \" pre\" clone %s" , cloneName )
352
357
}
353
358
359
+ cloneDataDir := path .Join (p .fsPool .ClonesDir (), cloneName , p .fsPool .DataSubDir )
360
+ if err := fs .CleanupLogsDir (cloneDataDir ); err != nil {
361
+ log .Warn ("Failed to clean up logs directory:" , err .Error ())
362
+ }
363
+
354
364
defer func () {
355
365
if err != nil {
356
366
if errDestroy := p .cloneManager .DestroyClone (cloneName ); errDestroy != nil {
@@ -361,7 +371,7 @@ func (p *PhysicalInitial) run(ctx context.Context) (err error) {
361
371
362
372
// Promotion.
363
373
if p .options .Promotion .Enabled {
364
- if err := p .promoteInstance (ctx , path . Join ( p . fsPool . ClonesDir (), cloneName , p . fsPool . DataSubDir ) , syState ); err != nil {
374
+ if err := p .promoteInstance (ctx , cloneDataDir , syState ); err != nil {
365
375
return errors .Wrap (err , "failed to promote instance" )
366
376
}
367
377
}
@@ -387,6 +397,45 @@ func (p *PhysicalInitial) run(ctx context.Context) (err error) {
387
397
388
398
p .tm .SendEvent (ctx , telemetry .SnapshotCreatedEvent , telemetry.SnapshotCreated {})
389
399
400
+ if err := p .cleanupOldLogs (); err != nil {
401
+ log .Warn ("cannot clean up old logs" , err .Error ())
402
+ }
403
+
404
+ return nil
405
+ }
406
+
407
+ func (p * PhysicalInitial ) cleanupOldLogs () error {
408
+ lastWeekTime := time .Now ().AddDate (0 , 0 , - 1 * defaultLogRetentionDays )
409
+
410
+ log .Dbg ("Cleaning up PGDATA logs older than" , lastWeekTime .Format (time .DateTime ))
411
+
412
+ logDir := path .Join (p .fsPool .DataDir (), logDirName )
413
+
414
+ dirEntries , err := os .ReadDir (logDir )
415
+ if err != nil {
416
+ return err
417
+ }
418
+
419
+ var fileCounter int
420
+
421
+ for _ , logFile := range dirEntries {
422
+ info , err := logFile .Info ()
423
+ if err != nil {
424
+ continue
425
+ }
426
+
427
+ if info .ModTime ().Before (lastWeekTime ) {
428
+ logFilename := path .Join (logDir , logFile .Name ())
429
+ if err := os .RemoveAll (logFilename ); err != nil {
430
+ log .Warn ("cannot remove old log file %s: %s" , logFilename , err .Error ())
431
+ }
432
+
433
+ fileCounter ++
434
+ }
435
+ }
436
+
437
+ log .Dbg ("Old PGDATA logs have been cleaned. Number of deleted files: " , fileCounter )
438
+
390
439
return nil
391
440
}
392
441
0 commit comments