From 3de3e2b68862e405a8e2c779cc6420ac362b8fae Mon Sep 17 00:00:00 2001 From: Christian Schwarz Date: Thu, 10 Oct 2019 13:34:33 +0200 Subject: [PATCH] WIP: draft incomplete keep rule for keeping most recent common ancestor --- config/samples/local.yml | 1 + pruning/keep_most_recent_common_ancestor.go | 29 +++++++++++++++++ .../keep_most_recent_common_ancestor_test.go | 31 +++++++++++++++++++ pruning/pruning.go | 1 + pruning/pruning_test.go | 1 + 5 files changed, 63 insertions(+) create mode 100644 pruning/keep_most_recent_common_ancestor.go create mode 100644 pruning/keep_most_recent_common_ancestor_test.go diff --git a/config/samples/local.yml b/config/samples/local.yml index fbf7861..05baf9e 100644 --- a/config/samples/local.yml +++ b/config/samples/local.yml @@ -26,6 +26,7 @@ jobs: - type: last_n count: 10 keep_receiver: + - type: most_recent_common_snapshot - type: grid grid: 1x1h(keep=all) | 24x1h | 35x1d | 6x30d regex: "zrepl_.*" \ No newline at end of file diff --git a/pruning/keep_most_recent_common_ancestor.go b/pruning/keep_most_recent_common_ancestor.go new file mode 100644 index 0000000..da73e54 --- /dev/null +++ b/pruning/keep_most_recent_common_ancestor.go @@ -0,0 +1,29 @@ +package pruning + +import "sort" + +type KeepMostRecentCommonAncestor struct { + _opaque struct{} +} + +func NewKeepMostRecentCommonAncestor() *KeepMostRecentCommonAncestor { + return &KeepMostRecentCommonAncestor{} +} + +func (k *KeepMostRecentCommonAncestor) KeepRule(snaps []Snapshot) (destroyList []Snapshot) { + var bothSides []Snapshot + for _, s := range snaps { + if s.PresentOnBothSides() { + bothSides = append(bothSides, s) + } + } + if len(bothSides) == 0 { + return []Snapshot{} + } + + sort.Slice(bothSides, func(i, j int) bool { + return bothSides[i].Date().After(bothSides[j].Date()) + }) + + return []Snapshot{bothSides[0]} +} diff --git a/pruning/keep_most_recent_common_ancestor_test.go b/pruning/keep_most_recent_common_ancestor_test.go new file mode 100644 index 0000000..daeb546 --- /dev/null +++ b/pruning/keep_most_recent_common_ancestor_test.go @@ -0,0 +1,31 @@ +package pruning + +import "testing" + +func TestKeppMostRecentCommonAncestor(t *testing.T) { + + o := func(minutes int) time.Time { + return time.Unix(123, 0).Add(time.Duration(minutes) * time.Minute) + } + + tcs := map[string]testCase{ + "empty": { + inputs: []Snapshot{}, + rules: []KeepRule{NewKeepMostRecentCommonAncestor()}, + expDestroy: map[string]bool{}, + }, + "nil": { + inputs: nil, + rules: []KeepRule{NewKeepMostRecentCommonAncestor()}, + expDestroy: map[string]bool{}, + }, + "": { + inputs: []Snapshot{ + stubSnap{name: "s1", date: o(5), bothSides: } + }, + } + } + + testTable(tcs, t) + +} diff --git a/pruning/pruning.go b/pruning/pruning.go index 9f36581..f7db2c2 100644 --- a/pruning/pruning.go +++ b/pruning/pruning.go @@ -17,6 +17,7 @@ type Snapshot interface { Name() string Replicated() bool Date() time.Time + PresentOnBothSides() bool } // The returned snapshot list is guaranteed to only contains elements of input parameter snaps diff --git a/pruning/pruning_test.go b/pruning/pruning_test.go index 2755dde..efd6f54 100644 --- a/pruning/pruning_test.go +++ b/pruning/pruning_test.go @@ -9,6 +9,7 @@ type stubSnap struct { name string replicated bool date time.Time + bothSides bool } func (s stubSnap) Name() string { return s.name }