From e3277918e46875f3838375d9944f351d1c9ab7ab Mon Sep 17 00:00:00 2001
From: Aliaksandr Valialkin <valyala@victoriametrics.com>
Date: Thu, 20 Jan 2022 20:36:33 +0200
Subject: [PATCH] lib/storage: verify that blocks in a single part are sorted
 by TSID when reading sequential blocks from the part

This may help narrowing down the issue https://github.com/VictoriaMetrics/VictoriaMetrics/issues/2082
---
 lib/storage/block_stream_reader.go | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/lib/storage/block_stream_reader.go b/lib/storage/block_stream_reader.go
index f4aa6fab23..72b1966c7f 100644
--- a/lib/storage/block_stream_reader.go
+++ b/lib/storage/block_stream_reader.go
@@ -175,7 +175,7 @@ func (bsr *blockStreamReader) InitFromFilePart(path string) error {
 		timestampsFile.MustClose()
 		valuesFile.MustClose()
 		indexFile.MustClose()
-		return fmt.Errorf("cannot unmarshal metaindex rows from inmemoryPart: %w", err)
+		return fmt.Errorf("cannot unmarshal metaindex rows from file part %q: %w", metaindexPath, err)
 	}
 
 	bsr.path = path
@@ -213,16 +213,19 @@ func (bsr *blockStreamReader) NextBlock() bool {
 	if bsr.err != nil {
 		return false
 	}
-
+	tsidPrev := bsr.Block.bh.TSID
 	bsr.Block.Reset()
-
 	err := bsr.readBlock()
 	if err == nil {
-		if bsr.Block.bh.RowsCount > 0 {
-			return true
+		if bsr.Block.bh.TSID.Less(&tsidPrev) {
+			bsr.err = fmt.Errorf("possible data corruption: the next TSID=%v is smaller than the previous TSID=%v", &bsr.Block.bh.TSID, &tsidPrev)
+			return false
 		}
-		bsr.err = fmt.Errorf("invalid block read with zero rows; block=%+v", &bsr.Block)
-		return false
+		if bsr.Block.bh.RowsCount == 0 {
+			bsr.err = fmt.Errorf("invalid block read with zero rows; block=%+v", &bsr.Block)
+			return false
+		}
+		return true
 	}
 	if err == io.EOF {
 		bsr.err = io.EOF