From 99d67ac8ad2335c77f81d9a01ac275e6d4c08eef Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Fri, 21 Oct 2022 00:52:11 +0300 Subject: [PATCH] lib/storage: validate timestamps in the block only if they use encoding, which needs validation This reduces CPU usage when there is no sense in validating timestamps. This is a follow-up for 5fa952549893d90ef9d1074d4d61ab4134e7a010 Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/2998 Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/3011 --- lib/encoding/encoding.go | 14 ++++++++++++++ lib/storage/block.go | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/encoding/encoding.go b/lib/encoding/encoding.go index b53ee22842..89168529d5 100644 --- a/lib/encoding/encoding.go +++ b/lib/encoding/encoding.go @@ -42,6 +42,20 @@ const ( MarshalTypeNearestDelta = MarshalType(6) ) +// NeedsValidation returns true if mt may need additional validation for silent data corruption. +func (mt MarshalType) NeedsValidation() bool { + switch mt { + case MarshalTypeNearestDelta2, + MarshalTypeNearestDelta: + return true + default: + // Other types do not need additional validation, + // since they either already contain checksums (e.g. compressed data) + // or they are trivial and cannot be validated (e.g. const or delta const) + return false + } +} + // CheckMarshalType verifies whether the mt is valid. func CheckMarshalType(mt MarshalType) error { if mt < 0 || mt > 6 { diff --git a/lib/storage/block.go b/lib/storage/block.go index 84c7ee6265..6513c4c449 100644 --- a/lib/storage/block.go +++ b/lib/storage/block.go @@ -271,7 +271,7 @@ func (b *Block) UnmarshalData() error { if b.bh.PrecisionBits < 64 { // Recover timestamps order after lossy compression. encoding.EnsureNonDecreasingSequence(b.timestamps, b.bh.MinTimestamp, b.bh.MaxTimestamp) - } else { + } else if b.bh.TimestampsMarshalType.NeedsValidation() { // Ensure timestamps are in the range [MinTimestamp ... MaxTimestamps] and are ordered. if err := checkTimestampsBounds(b.timestamps, b.bh.MinTimestamp, b.bh.MaxTimestamp); err != nil { return err