From 761af08889f3c491beafe8e89c188d424dc43493 Mon Sep 17 00:00:00 2001 From: Tobias Klein Date: Tue, 29 Aug 2017 18:30:06 +0200 Subject: [PATCH 1/5] fix: bucker bucker "always" does not return a unique id in case of exact same timestamps --- internal/restic/snapshot_policy.go | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/internal/restic/snapshot_policy.go b/internal/restic/snapshot_policy.go index 0775f4024..156749975 100644 --- a/internal/restic/snapshot_policy.go +++ b/internal/restic/snapshot_policy.go @@ -34,34 +34,34 @@ func (e ExpirePolicy) Empty() bool { } // ymdh returns an integer in the form YYYYMMDDHH. -func ymdh(d time.Time) int { +func ymdh(d time.Time, _ int) int { return d.Year()*1000000 + int(d.Month())*10000 + d.Day()*100 + d.Hour() } // ymd returns an integer in the form YYYYMMDD. -func ymd(d time.Time) int { +func ymd(d time.Time, _ int) int { return d.Year()*10000 + int(d.Month())*100 + d.Day() } // yw returns an integer in the form YYYYWW, where WW is the week number. -func yw(d time.Time) int { +func yw(d time.Time, _ int) int { year, week := d.ISOWeek() return year*100 + week } // ym returns an integer in the form YYYYMM. -func ym(d time.Time) int { +func ym(d time.Time, _ int) int { return d.Year()*100 + int(d.Month()) } // y returns the year of d. -func y(d time.Time) int { +func y(d time.Time, _ int) int { return d.Year() } // always returns a unique number for d. -func always(d time.Time) int { - return int(d.UnixNano()) +func always(d time.Time, nr int) int { + return nr } // ApplyPolicy returns the snapshots from list that are to be kept and removed @@ -79,7 +79,7 @@ func ApplyPolicy(list Snapshots, p ExpirePolicy) (keep, remove Snapshots) { var buckets = [6]struct { Count int - bucker func(d time.Time) int + bucker func(d time.Time, nr int) int Last int }{ {p.Last, always, -1}, @@ -90,7 +90,9 @@ func ApplyPolicy(list Snapshots, p ExpirePolicy) (keep, remove Snapshots) { {p.Yearly, y, -1}, } + var nr int for _, cur := range list { + nr = nr + 1 var keepSnap bool // Tags are handled specially as they are not counted. @@ -103,7 +105,7 @@ func ApplyPolicy(list Snapshots, p ExpirePolicy) (keep, remove Snapshots) { // Now update the other buckets and see if they have some counts left. for i, b := range buckets { if b.Count > 0 { - val := b.bucker(cur.Time) + val := b.bucker(cur.Time, nr) if val != b.Last { keepSnap = true buckets[i].Last = val From 2d73a273af9e4e7c56c4ea8996507451d543cf1b Mon Sep 17 00:00:00 2001 From: Tobias Klein Date: Sun, 3 Sep 2017 17:06:41 +0200 Subject: [PATCH 2/5] saving a variable --- internal/restic/snapshot_policy.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/internal/restic/snapshot_policy.go b/internal/restic/snapshot_policy.go index 156749975..448667943 100644 --- a/internal/restic/snapshot_policy.go +++ b/internal/restic/snapshot_policy.go @@ -90,8 +90,7 @@ func ApplyPolicy(list Snapshots, p ExpirePolicy) (keep, remove Snapshots) { {p.Yearly, y, -1}, } - var nr int - for _, cur := range list { + for nr, cur := range list { nr = nr + 1 var keepSnap bool From a677f1139ae2ad858fdbd0b906334438e84cf7a8 Mon Sep 17 00:00:00 2001 From: Tobias Klein Date: Sun, 10 Sep 2017 10:41:07 +0200 Subject: [PATCH 3/5] removed unnacessary line --- internal/restic/snapshot_policy.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/restic/snapshot_policy.go b/internal/restic/snapshot_policy.go index 448667943..929da93ed 100644 --- a/internal/restic/snapshot_policy.go +++ b/internal/restic/snapshot_policy.go @@ -91,7 +91,6 @@ func ApplyPolicy(list Snapshots, p ExpirePolicy) (keep, remove Snapshots) { } for nr, cur := range list { - nr = nr + 1 var keepSnap bool // Tags are handled specially as they are not counted. From 9924c311c9f8db03f674ac34c4b80b3d69f4f846 Mon Sep 17 00:00:00 2001 From: Tobias Klein Date: Sun, 10 Sep 2017 12:23:28 +0200 Subject: [PATCH 4/5] added test cases --- internal/restic/snapshot_policy_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/internal/restic/snapshot_policy_test.go b/internal/restic/snapshot_policy_test.go index e0cdeeeba..69111fab1 100644 --- a/internal/restic/snapshot_policy_test.go +++ b/internal/restic/snapshot_policy_test.go @@ -114,6 +114,10 @@ var testExpireSnapshots = restic.Snapshots{ {Time: parseTimeUTC("2015-10-11 10:20:30")}, {Time: parseTimeUTC("2015-10-20 10:20:30")}, {Time: parseTimeUTC("2015-10-22 10:20:30")}, + {Time: parseTimeUTC("2015-10-22 10:20:30")}, + {Time: parseTimeUTC("2015-10-22 10:20:30"), Tags: []string{"foo", "bar"}}, + {Time: parseTimeUTC("2015-10-22 10:20:30"), Tags: []string{"foo", "bar"}}, + {Time: parseTimeUTC("2015-10-22 10:20:30"), Tags: []string{"foo", "bar"}, Paths: []string{"path1", "path2"}}, {Time: parseTimeUTC("2015-11-08 10:20:30")}, {Time: parseTimeUTC("2015-11-10 10:20:30")}, {Time: parseTimeUTC("2015-11-12 10:20:30")}, From 9fa909ccd618e417010da557299f6c4499eed6c8 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Mon, 11 Sep 2017 17:52:22 +0200 Subject: [PATCH 5/5] Update golden files --- .../restic/testdata/policy_keep_snapshots_0 | 35 ++++++++++++ .../restic/testdata/policy_keep_snapshots_18 | 30 ++++++++++ .../restic/testdata/policy_keep_snapshots_19 | 30 ++++++++++ .../restic/testdata/policy_keep_snapshots_20 | 30 ++++++++++ .../restic/testdata/policy_keep_snapshots_3 | 55 ++++++++++++------- .../restic/testdata/policy_keep_snapshots_4 | 35 ++++++++++++ 6 files changed, 195 insertions(+), 20 deletions(-) diff --git a/internal/restic/testdata/policy_keep_snapshots_0 b/internal/restic/testdata/policy_keep_snapshots_0 index 159d55d0c..875f0e44e 100644 --- a/internal/restic/testdata/policy_keep_snapshots_0 +++ b/internal/restic/testdata/policy_keep_snapshots_0 @@ -149,6 +149,41 @@ "tree": null, "paths": null }, + { + "time": "2015-10-22T10:20:30Z", + "tree": null, + "paths": [ + "path1", + "path2" + ], + "tags": [ + "foo", + "bar" + ] + }, + { + "time": "2015-10-22T10:20:30Z", + "tree": null, + "paths": null, + "tags": [ + "foo", + "bar" + ] + }, + { + "time": "2015-10-22T10:20:30Z", + "tree": null, + "paths": null + }, + { + "time": "2015-10-22T10:20:30Z", + "tree": null, + "paths": null, + "tags": [ + "foo", + "bar" + ] + }, { "time": "2015-10-20T10:20:30Z", "tree": null, diff --git a/internal/restic/testdata/policy_keep_snapshots_18 b/internal/restic/testdata/policy_keep_snapshots_18 index 3ca6727a0..de7cea914 100644 --- a/internal/restic/testdata/policy_keep_snapshots_18 +++ b/internal/restic/testdata/policy_keep_snapshots_18 @@ -1,4 +1,34 @@ [ + { + "time": "2015-10-22T10:20:30Z", + "tree": null, + "paths": [ + "path1", + "path2" + ], + "tags": [ + "foo", + "bar" + ] + }, + { + "time": "2015-10-22T10:20:30Z", + "tree": null, + "paths": null, + "tags": [ + "foo", + "bar" + ] + }, + { + "time": "2015-10-22T10:20:30Z", + "tree": null, + "paths": null, + "tags": [ + "foo", + "bar" + ] + }, { "time": "2014-11-15T10:20:30Z", "tree": null, diff --git a/internal/restic/testdata/policy_keep_snapshots_19 b/internal/restic/testdata/policy_keep_snapshots_19 index af86beb84..5e0cb619b 100644 --- a/internal/restic/testdata/policy_keep_snapshots_19 +++ b/internal/restic/testdata/policy_keep_snapshots_19 @@ -1,4 +1,34 @@ [ + { + "time": "2015-10-22T10:20:30Z", + "tree": null, + "paths": [ + "path1", + "path2" + ], + "tags": [ + "foo", + "bar" + ] + }, + { + "time": "2015-10-22T10:20:30Z", + "tree": null, + "paths": null, + "tags": [ + "foo", + "bar" + ] + }, + { + "time": "2015-10-22T10:20:30Z", + "tree": null, + "paths": null, + "tags": [ + "foo", + "bar" + ] + }, { "time": "2014-11-15T10:20:30Z", "tree": null, diff --git a/internal/restic/testdata/policy_keep_snapshots_20 b/internal/restic/testdata/policy_keep_snapshots_20 index 004b12156..51101c49b 100644 --- a/internal/restic/testdata/policy_keep_snapshots_20 +++ b/internal/restic/testdata/policy_keep_snapshots_20 @@ -1,4 +1,34 @@ [ + { + "time": "2015-10-22T10:20:30Z", + "tree": null, + "paths": [ + "path1", + "path2" + ], + "tags": [ + "foo", + "bar" + ] + }, + { + "time": "2015-10-22T10:20:30Z", + "tree": null, + "paths": null, + "tags": [ + "foo", + "bar" + ] + }, + { + "time": "2015-10-22T10:20:30Z", + "tree": null, + "paths": null, + "tags": [ + "foo", + "bar" + ] + }, { "time": "2014-11-15T10:20:30Z", "tree": null, diff --git a/internal/restic/testdata/policy_keep_snapshots_3 b/internal/restic/testdata/policy_keep_snapshots_3 index 159d55d0c..4c2846221 100644 --- a/internal/restic/testdata/policy_keep_snapshots_3 +++ b/internal/restic/testdata/policy_keep_snapshots_3 @@ -149,6 +149,41 @@ "tree": null, "paths": null }, + { + "time": "2015-10-22T10:20:30Z", + "tree": null, + "paths": [ + "path1", + "path2" + ], + "tags": [ + "foo", + "bar" + ] + }, + { + "time": "2015-10-22T10:20:30Z", + "tree": null, + "paths": null, + "tags": [ + "foo", + "bar" + ] + }, + { + "time": "2015-10-22T10:20:30Z", + "tree": null, + "paths": null + }, + { + "time": "2015-10-22T10:20:30Z", + "tree": null, + "paths": null, + "tags": [ + "foo", + "bar" + ] + }, { "time": "2015-10-20T10:20:30Z", "tree": null, @@ -522,25 +557,5 @@ "time": "2014-08-13T10:20:30.1Z", "tree": null, "paths": null - }, - { - "time": "2014-08-13T10:20:30Z", - "tree": null, - "paths": null - }, - { - "time": "2014-08-12T10:20:30Z", - "tree": null, - "paths": null - }, - { - "time": "2014-08-10T10:20:30Z", - "tree": null, - "paths": null - }, - { - "time": "2014-08-08T10:20:30Z", - "tree": null, - "paths": null } ] \ No newline at end of file diff --git a/internal/restic/testdata/policy_keep_snapshots_4 b/internal/restic/testdata/policy_keep_snapshots_4 index 159d55d0c..875f0e44e 100644 --- a/internal/restic/testdata/policy_keep_snapshots_4 +++ b/internal/restic/testdata/policy_keep_snapshots_4 @@ -149,6 +149,41 @@ "tree": null, "paths": null }, + { + "time": "2015-10-22T10:20:30Z", + "tree": null, + "paths": [ + "path1", + "path2" + ], + "tags": [ + "foo", + "bar" + ] + }, + { + "time": "2015-10-22T10:20:30Z", + "tree": null, + "paths": null, + "tags": [ + "foo", + "bar" + ] + }, + { + "time": "2015-10-22T10:20:30Z", + "tree": null, + "paths": null + }, + { + "time": "2015-10-22T10:20:30Z", + "tree": null, + "paths": null, + "tags": [ + "foo", + "bar" + ] + }, { "time": "2015-10-20T10:20:30Z", "tree": null,