Skip to content

Commit 564b400

Browse files
committed
Merge branch 'cleanup-logical-snapshots' into 'master'
fix: cleanup logical snapshots See merge request postgres-ai/database-lab!1068
2 parents 115bf1c + 07403e7 commit 564b400

File tree

7 files changed

+20
-10
lines changed

7 files changed

+20
-10
lines changed

engine/internal/provision/mode_local_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (m mockFSManager) DestroySnapshot(_ string, _ thinclones.DestroyOptions) (e
8787
return nil
8888
}
8989

90-
func (m mockFSManager) CleanupSnapshots(_ int) ([]string, error) {
90+
func (m mockFSManager) CleanupSnapshots(_ int, _ models.RetrievalMode) ([]string, error) {
9191
return nil, nil
9292
}
9393

engine/internal/provision/pool/manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ type StateReporter interface {
4747
type Snapshotter interface {
4848
CreateSnapshot(poolSuffix, dataStateAt string) (snapshotName string, err error)
4949
DestroySnapshot(snapshotName string, options thinclones.DestroyOptions) (err error)
50-
CleanupSnapshots(retentionLimit int) ([]string, error)
50+
CleanupSnapshots(retentionLimit int, mode models.RetrievalMode) ([]string, error)
5151
SnapshotList() []resources.Snapshot
5252
RefreshSnapshotList()
5353
}

engine/internal/provision/thinclones/lvm/lvmanager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func (m *LVManager) DestroySnapshot(_ string, _ thinclones.DestroyOptions) error
106106
}
107107

108108
// CleanupSnapshots is not supported in LVM mode.
109-
func (m *LVManager) CleanupSnapshots(_ int) ([]string, error) {
109+
func (m *LVManager) CleanupSnapshots(_ int, _ models.RetrievalMode) ([]string, error) {
110110
log.Msg("Cleanup snapshots is not supported in LVM mode. Skip the operation.")
111111

112112
return nil, nil

engine/internal/provision/thinclones/zfs/branching.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (m *Manager) InitBranching() error {
7777
continue
7878
}
7979

80-
if err := m.SetRelation(leader.ID, follower.ID); err != nil {
80+
if err := m.SetRelation(follower.ID, leader.ID); err != nil {
8181
return fmt.Errorf("failed to set snapshot relations: %w", err)
8282
}
8383

engine/internal/provision/thinclones/zfs/zfs.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ func (m *Manager) checkDependentClones(snapshotName string) (string, error) {
578578
}
579579

580580
// CleanupSnapshots destroys old snapshots considering retention limit and related clones.
581-
func (m *Manager) CleanupSnapshots(retentionLimit int) ([]string, error) {
581+
func (m *Manager) CleanupSnapshots(retentionLimit int, mode models.RetrievalMode) ([]string, error) {
582582
clonesCmd := fmt.Sprintf("zfs list -S clones -o name,origin -H -r %s", m.config.Pool.Name)
583583

584584
clonesOutput, err := m.runner.Run(clonesCmd)
@@ -588,10 +588,16 @@ func (m *Manager) CleanupSnapshots(retentionLimit int) ([]string, error) {
588588

589589
busySnapshots := m.getBusySnapshotList(clonesOutput)
590590

591+
modeFilter := ""
592+
593+
if mode == models.Physical {
594+
modeFilter = "| grep _pre$"
595+
}
596+
591597
cleanupCmd := fmt.Sprintf(
592-
"zfs list -t snapshot -H -o name -s %s -s creation -r %s | grep -v clone | grep _pre$ | head -n -%d %s"+
598+
"zfs list -t snapshot -H -o name -s %s -s creation -r %s | grep -v clone %s | head -n -%d %s"+
593599
"| xargs -n1 --no-run-if-empty zfs destroy -R ",
594-
dataStateAtLabel, m.config.Pool.Name, retentionLimit, excludeBusySnapshots(busySnapshots))
600+
dataStateAtLabel, m.config.Pool.Name, modeFilter, retentionLimit, excludeBusySnapshots(busySnapshots))
595601

596602
out, err := m.runner.Run(cleanupCmd)
597603
if err != nil {
@@ -892,7 +898,9 @@ func (m *Manager) SnapshotList() []resources.Snapshot {
892898
// RefreshSnapshotList updates the list of snapshots.
893899
func (m *Manager) RefreshSnapshotList() {
894900
snapshots, err := m.getSnapshots()
895-
if err != nil {
901+
902+
var emptyPoolError *EmptyPoolError
903+
if err != nil && !errors.As(err, &emptyPoolError) {
896904
log.Err("failed to refresh snapshot list: ", err)
897905
return
898906
}

engine/internal/retrieval/engine/postgres/snapshot/logical.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"gitlab.com/postgres-ai/database-lab/v3/internal/telemetry"
3535
"gitlab.com/postgres-ai/database-lab/v3/pkg/config/global"
3636
"gitlab.com/postgres-ai/database-lab/v3/pkg/log"
37+
"gitlab.com/postgres-ai/database-lab/v3/pkg/models"
3738
"gitlab.com/postgres-ai/database-lab/v3/pkg/util"
3839
)
3940

@@ -151,7 +152,7 @@ func (s *LogicalInitial) Run(ctx context.Context) error {
151152

152153
log.Dbg("Cleaning up old snapshots from a dataset")
153154

154-
if _, err := s.cloneManager.CleanupSnapshots(0); err != nil {
155+
if _, err := s.cloneManager.CleanupSnapshots(0, models.Logical); err != nil {
155156
return errors.Wrap(err, "failed to destroy old snapshots")
156157
}
157158

engine/internal/retrieval/engine/postgres/snapshot/physical.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import (
4646
"gitlab.com/postgres-ai/database-lab/v3/internal/telemetry"
4747
"gitlab.com/postgres-ai/database-lab/v3/pkg/config/global"
4848
"gitlab.com/postgres-ai/database-lab/v3/pkg/log"
49+
"gitlab.com/postgres-ai/database-lab/v3/pkg/models"
4950
"gitlab.com/postgres-ai/database-lab/v3/pkg/util"
5051
"gitlab.com/postgres-ai/database-lab/v3/pkg/util/branching"
5152
)
@@ -1126,7 +1127,7 @@ func (p *PhysicalInitial) cleanupSnapshots(retentionLimit int) error {
11261127
default:
11271128
}
11281129

1129-
_, err := p.cloneManager.CleanupSnapshots(retentionLimit)
1130+
_, err := p.cloneManager.CleanupSnapshots(retentionLimit, models.Physical)
11301131
if err != nil {
11311132
return errors.Wrap(err, "failed to clean up snapshots")
11321133
}

0 commit comments

Comments
 (0)