From 661b8ede5b004142116bee69f65c3ae1b1dcca41 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Wed, 9 Oct 2019 12:13:17 +0300 Subject: [PATCH] lib/storage: harden the check that the original items are sorted after mergeTagToMetricIDsRows fails to preserve sort order --- lib/storage/index_db.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/storage/index_db.go b/lib/storage/index_db.go index 69c2bf3e8..c5a0fd6df 100644 --- a/lib/storage/index_db.go +++ b/lib/storage/index_db.go @@ -2360,7 +2360,17 @@ func mergeTagToMetricIDsRows(data []byte, items [][]byte) ([]byte, [][]byte) { if err := checkItemsSorted(dstItems); err != nil { logger.Errorf("please report this error at https://github.com/VictoriaMetrics/VictoriaMetrics/issues : %s", err) dstData = append(dstData[:0], tmm.dataCopy...) - dstItems = append(dstItems[:0], tmm.itemsCopy...) + dstItems = dstItems[:0] + // tmm.itemsCopy can point to overwritten data, so it must be updated + // to point to real data from tmm.dataCopy. + buf := dstData + for _, item := range tmm.itemsCopy { + dstItems = append(dstItems, buf[:len(item)]) + buf = buf[len(item):] + } + if err := checkItemsSorted(dstItems); err != nil { + logger.Panicf("BUG: the original items weren't sorted: %s", err) + } } putTagToMetricIDsRowsMerger(tmm) return dstData, dstItems