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 5fa9525498

Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/2998
Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/3011
This commit is contained in:
Aliaksandr Valialkin 2022-10-21 00:52:11 +03:00
parent edf3b7be47
commit af648279ce
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1
2 changed files with 15 additions and 1 deletions

View file

@ -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 {

View file

@ -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