From 3ddaafa729a96c1ebb6c26cfb88150a4e66f896e Mon Sep 17 00:00:00 2001 From: Artem Fetishev <149964189+rtm0@users.noreply.github.com> Date: Thu, 21 Nov 2024 10:44:21 +0100 Subject: [PATCH] lib/storage: confirm that changing retention period can cause previous indexDB deletion (#7569) ### Describe Your Changes Add test cases proving that it is possible to lose indexDB after changing the retention period. See #7609 ### Checklist The following checks are **mandatory**: - [x ] My change adheres [VictoriaMetrics contributing guidelines](https://docs.victoriametrics.com/contributing/). --------- Signed-off-by: Artem Fetishev Co-authored-by: Roman Khavronenko (cherry picked from commit 3383589fd1fd25c880ccd581d06e9451be5613a9) --- lib/storage/storage_test.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/lib/storage/storage_test.go b/lib/storage/storage_test.go index d1ead9084..d9fc824f7 100644 --- a/lib/storage/storage_test.go +++ b/lib/storage/storage_test.go @@ -573,6 +573,37 @@ func TestNextRetentionDeadlineSeconds(t *testing.T) { f("2023-07-22T12:44:35Z", 24*time.Hour, 37*time.Hour, "2023-07-22T15:00:00Z") f("2023-07-22T14:44:35Z", 24*time.Hour, 37*time.Hour, "2023-07-22T15:00:00Z") f("2023-07-22T15:44:35Z", 24*time.Hour, 37*time.Hour, "2023-07-23T15:00:00Z") + + // The test cases below confirm that it is possible to pick a retention + // period such that the previous IndexDB may be removed earlier than it should be. + // See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/7609 + + // Cluster is configured with 12 month retentionPeriod on 2023-01-01. + f("2023-01-01T00:00:00Z", 365*24*time.Hour, 0, "2023-12-19T04:00:00Z") + + // Restarts during that period do not change the retention deadline: + f("2023-03-01T00:00:00Z", 365*24*time.Hour, 0, "2023-12-19T04:00:00Z") + f("2023-06-01T00:00:00Z", 365*24*time.Hour, 0, "2023-12-19T04:00:00Z") + f("2023-09-01T00:00:00Z", 365*24*time.Hour, 0, "2023-12-19T04:00:00Z") + f("2023-12-01T00:00:00Z", 365*24*time.Hour, 0, "2023-12-19T04:00:00Z") + f("2023-12-19T03:59:59Z", 365*24*time.Hour, 0, "2023-12-19T04:00:00Z") + + // At 2023-12-19T04:00:00Z the rotation occurs. New deadline is + // 2024-12-18T04:00:00Z. Restarts during that period do not change the + // new deadline: + f("2023-12-19T04:00:01Z", 365*24*time.Hour, 0, "2024-12-18T04:00:00Z") + f("2024-01-01T00:00:00Z", 365*24*time.Hour, 0, "2024-12-18T04:00:00Z") + f("2024-03-01T00:00:00Z", 365*24*time.Hour, 0, "2024-12-18T04:00:00Z") + f("2024-04-29T00:00:00Z", 365*24*time.Hour, 0, "2024-12-18T04:00:00Z") + + // Now restart again but with the new retention period of 451d and the + // rotation time becomes 2024-05-01T04:00:00Z. + // + // At 2024-05-01T04:00:00Z, a new IndexDB is created and the current + // IndexDB (currently applicable to only ~4 months of data) becomes the + // previous IndexDB. The preceding IndexDB is deleted despite possibly + // being related to ~8 months of data that is still within retention. + f("2024-04-29T00:00:00Z", 451*24*time.Hour, 0, "2024-05-01T04:00:00Z") } func TestStorageOpenClose(t *testing.T) {