lib/storage: protect from time drift during indexdb rotation

Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/248
This commit is contained in:
Aliaksandr Valialkin 2019-12-02 14:42:26 +02:00
parent cf85c567d1
commit 5a62415bec
2 changed files with 6 additions and 4 deletions

View file

@ -655,7 +655,10 @@ func nextRetentionDuration(retentionMonths int) time.Duration {
n -= n % retentionMonths
y := n / 12
m := time.Month((n % 12) + 1)
deadline := time.Date(y, m, 1, 0, 0, 0, 0, time.UTC)
// Schedule the deadline to +4 hours from the next retention period start.
// This should prevent from possible double deletion of indexdb
// due to time drift - see https://github.com/VictoriaMetrics/VictoriaMetrics/issues/248 .
deadline := time.Date(y, m, 1, 4, 0, 0, 0, time.UTC)
return deadline.Sub(t)
}

View file

@ -342,10 +342,9 @@ func TestMetricRowMarshalUnmarshal(t *testing.T) {
func TestNextRetentionDuration(t *testing.T) {
for retentionMonths := 1; retentionMonths < 360; retentionMonths++ {
currTime := time.Now().UTC()
d := nextRetentionDuration(retentionMonths)
if d < 0 {
if d <= 0 {
currTime := time.Now().UTC()
nextTime := time.Now().UTC().Add(d)
t.Fatalf("unexected retention duration for retentionMonths=%d; got %s; must be %s + %d months", retentionMonths, nextTime, currTime, retentionMonths)
}