mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-11-21 14:44:00 +00:00
lib/storage: remove duplicate MetricIDs in tag->metricIDs
items before writing them into inverted index
This commit is contained in:
parent
adc18c3ee6
commit
4468f9f966
2 changed files with 46 additions and 0 deletions
|
@ -2391,7 +2391,9 @@ func (tmm *tagToMetricIDsRowsMerger) flushPendingMetricIDs(dstData []byte, dstIt
|
|||
}
|
||||
// Use sort.Sort instead of sort.Slice in order to reduce memory allocations.
|
||||
sort.Sort(&tmm.pendingMetricIDs)
|
||||
tmm.pendingMetricIDs = removeDuplicateMetricIDs(tmm.pendingMetricIDs)
|
||||
|
||||
// Marshal pendingMetricIDs
|
||||
dstDataLen := len(dstData)
|
||||
dstData = marshalCommonPrefix(dstData, nsPrefixTagToMetricIDs, mp.AccountID, mp.ProjectID)
|
||||
dstData = mp.Tag.Marshal(dstData)
|
||||
|
@ -2403,6 +2405,33 @@ func (tmm *tagToMetricIDsRowsMerger) flushPendingMetricIDs(dstData []byte, dstIt
|
|||
return dstData, dstItems
|
||||
}
|
||||
|
||||
func removeDuplicateMetricIDs(sortedMetricIDs []uint64) []uint64 {
|
||||
if len(sortedMetricIDs) < 2 {
|
||||
return sortedMetricIDs
|
||||
}
|
||||
prevMetricID := sortedMetricIDs[0]
|
||||
hasDuplicates := false
|
||||
for _, metricID := range sortedMetricIDs[1:] {
|
||||
if prevMetricID == metricID {
|
||||
hasDuplicates = true
|
||||
}
|
||||
prevMetricID = metricID
|
||||
}
|
||||
if !hasDuplicates {
|
||||
return sortedMetricIDs
|
||||
}
|
||||
dstMetricIDs := sortedMetricIDs[:1]
|
||||
prevMetricID = sortedMetricIDs[0]
|
||||
for _, metricID := range sortedMetricIDs[1:] {
|
||||
if prevMetricID == metricID {
|
||||
continue
|
||||
}
|
||||
dstMetricIDs = append(dstMetricIDs, metricID)
|
||||
prevMetricID = metricID
|
||||
}
|
||||
return dstMetricIDs
|
||||
}
|
||||
|
||||
func getTagToMetricIDsRowsMerger() *tagToMetricIDsRowsMerger {
|
||||
v := tmmPool.Get()
|
||||
if v == nil {
|
||||
|
|
|
@ -15,6 +15,23 @@ import (
|
|||
"github.com/VictoriaMetrics/VictoriaMetrics/lib/workingsetcache"
|
||||
)
|
||||
|
||||
func TestRemoveDuplicateMetricIDs(t *testing.T) {
|
||||
f := func(metricIDs, expectedMetricIDs []uint64) {
|
||||
a := removeDuplicateMetricIDs(metricIDs)
|
||||
if !reflect.DeepEqual(a, expectedMetricIDs) {
|
||||
t.Fatalf("unexpected result from removeDuplicateMetricIDs:\ngot\n%d\nwant\n%d", a, expectedMetricIDs)
|
||||
}
|
||||
}
|
||||
f(nil, nil)
|
||||
f([]uint64{123}, []uint64{123})
|
||||
f([]uint64{123, 123}, []uint64{123})
|
||||
f([]uint64{123, 123, 123}, []uint64{123})
|
||||
f([]uint64{0, 1, 1, 2}, []uint64{0, 1, 2})
|
||||
f([]uint64{0, 0, 0, 1, 1, 2}, []uint64{0, 1, 2})
|
||||
f([]uint64{0, 1, 1, 2, 2}, []uint64{0, 1, 2})
|
||||
f([]uint64{0, 1, 2, 2}, []uint64{0, 1, 2})
|
||||
}
|
||||
|
||||
func TestMarshalUnmarshalTSIDs(t *testing.T) {
|
||||
f := func(tsids []TSID) {
|
||||
t.Helper()
|
||||
|
|
Loading…
Reference in a new issue