Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: dont merge missed series if there is no overlap or has more points #5809

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion pkg/query-service/app/querier/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,21 @@ func mergeSerieses(cachedSeries, missedSeries []*v3.Series) []*v3.Series {
seriesesByLabels[labelsToString(series.Labels)] = series
continue
}
seriesesByLabels[labelsToString(series.Labels)].Points = append(seriesesByLabels[labelsToString(series.Labels)].Points, series.Points...)

series.SortPoints()
ls := len(series.Points)
// existing points are already sorted
cachedPoints := seriesesByLabels[labelsToString(series.Labels)].Points
lc := len(cachedPoints)

// if cacheSeries Start or End lies in missed series it means it can be merged
if (cachedPoints[0].Timestamp >= series.Points[0].Timestamp && cachedPoints[0].Timestamp <= series.Points[ls-1].Timestamp) ||
(cachedPoints[lc-1].Timestamp >= series.Points[0].Timestamp && cachedPoints[lc-1].Timestamp <= series.Points[ls-1].Timestamp) {

seriesesByLabels[labelsToString(series.Labels)].Points = append(seriesesByLabels[labelsToString(series.Labels)].Points, series.Points...)
} else {
seriesesByLabels[labelsToString(series.Labels)] = series
}
}
// Sort the points in each series by timestamp
for idx := range seriesesByLabels {
Expand Down
184 changes: 184 additions & 0 deletions pkg/query-service/app/querier/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,190 @@ import (
v3 "go.signoz.io/signoz/pkg/query-service/model/v3"
)

func TestMergeSerieses(t *testing.T) {

testCases := []struct {
name string
cachedSeries []*v3.Series
missedSeries []*v3.Series
resultSeries []*v3.Series
}{
{
name: "merge two series",
cachedSeries: []*v3.Series{
{
Labels: map[string]string{
"__name__": "http_server_requests_seconds_count",
},
Points: []v3.Point{
{Timestamp: 1675115596722, Value: 1},
{Timestamp: 1675115596723, Value: 2},
{Timestamp: 1675115596724, Value: 3},
},
},
},
missedSeries: []*v3.Series{
{
Labels: map[string]string{
"__name__": "http_server_requests_seconds_count",
},
Points: []v3.Point{
{Timestamp: 1675115596724, Value: 3},
{Timestamp: 1675115596725, Value: 4},
},
},
},
resultSeries: []*v3.Series{
{
Labels: map[string]string{
"__name__": "http_server_requests_seconds_count",
},
Points: []v3.Point{
{Timestamp: 1675115596722, Value: 1},
{Timestamp: 1675115596723, Value: 2},
{Timestamp: 1675115596724, Value: 3},
{Timestamp: 1675115596725, Value: 4},
},
},
},
},
{
name: "dont merge if start of missed is after end of cached",
cachedSeries: []*v3.Series{
{
Labels: map[string]string{
"__name__": "http_server_requests_seconds_count",
},
Points: []v3.Point{
{Timestamp: 1675115596722, Value: 1},
{Timestamp: 1675115596723, Value: 2},
{Timestamp: 1675115596724, Value: 3},
},
},
},
missedSeries: []*v3.Series{
{
Labels: map[string]string{
"__name__": "http_server_requests_seconds_count",
},
Points: []v3.Point{
{Timestamp: 1675115596726, Value: 5},
{Timestamp: 1675115596727, Value: 6},
},
},
},
resultSeries: []*v3.Series{
{
Labels: map[string]string{
"__name__": "http_server_requests_seconds_count",
},
Points: []v3.Point{
{Timestamp: 1675115596726, Value: 5},
{Timestamp: 1675115596727, Value: 6},
},
},
},
},
{
name: "dont merge if end of missed is before start of cached",
cachedSeries: []*v3.Series{
{
Labels: map[string]string{
"__name__": "http_server_requests_seconds_count",
},
Points: []v3.Point{
{Timestamp: 1675115596722, Value: 8},
{Timestamp: 1675115596723, Value: 9},
{Timestamp: 1675115596724, Value: 10},
},
},
},
missedSeries: []*v3.Series{
{
Labels: map[string]string{
"__name__": "http_server_requests_seconds_count",
},
Points: []v3.Point{
{Timestamp: 1675115596720, Value: 5},
{Timestamp: 1675115596721, Value: 6},
},
},
},
resultSeries: []*v3.Series{
{
Labels: map[string]string{
"__name__": "http_server_requests_seconds_count",
},
Points: []v3.Point{
{Timestamp: 1675115596720, Value: 5},
{Timestamp: 1675115596721, Value: 6},
},
},
},
},
{
name: "cache is a subset of missed series",
cachedSeries: []*v3.Series{
{
Labels: map[string]string{
"__name__": "http_server_requests_seconds_count",
},
Points: []v3.Point{
{Timestamp: 1675115596723, Value: 3},
{Timestamp: 1675115596724, Value: 4},
{Timestamp: 1675115596725, Value: 5},
},
},
},
missedSeries: []*v3.Series{
{
Labels: map[string]string{
"__name__": "http_server_requests_seconds_count",
},
Points: []v3.Point{
{Timestamp: 1675115596721, Value: 1},
{Timestamp: 1675115596722, Value: 2},
{Timestamp: 1675115596723, Value: 3},
{Timestamp: 1675115596725, Value: 5},
{Timestamp: 1675115596726, Value: 6},
},
},
},
resultSeries: []*v3.Series{
{
Labels: map[string]string{
"__name__": "http_server_requests_seconds_count",
},
Points: []v3.Point{
{Timestamp: 1675115596721, Value: 1},
{Timestamp: 1675115596722, Value: 2},
{Timestamp: 1675115596723, Value: 3},
{Timestamp: 1675115596724, Value: 4},
{Timestamp: 1675115596725, Value: 5},
{Timestamp: 1675115596726, Value: 6},
},
},
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
res := mergeSerieses(tc.cachedSeries, tc.missedSeries)
for sIdx, series := range tc.resultSeries {
if len(res[sIdx].Points) != len(series.Points) {
t.Errorf("expected %d, got %d", len(series.Points), len(res[sIdx].Points))
}
for pIdx, point := range series.Points {
if res[sIdx].Points[pIdx].Timestamp != point.Timestamp {
t.Errorf("expected %d, got %d", point.Timestamp, res[sIdx].Points[pIdx].Timestamp)
}
}
}
})
}
}

func TestFindMissingTimeRangesZeroFreshNess(t *testing.T) {
// There are five scenarios:
// 1. Cached time range is a subset of the requested time range
Expand Down
16 changes: 15 additions & 1 deletion pkg/query-service/app/querier/v2/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,21 @@ func mergeSerieses(cachedSeries, missedSeries []*v3.Series) []*v3.Series {
seriesesByLabels[labelsToString(series.Labels)] = series
continue
}
seriesesByLabels[labelsToString(series.Labels)].Points = append(seriesesByLabels[labelsToString(series.Labels)].Points, series.Points...)

series.SortPoints()
ls := len(series.Points)
// existing points are already sorted
cachedPoints := seriesesByLabels[labelsToString(series.Labels)].Points
lc := len(cachedPoints)

// if cacheSeries Start or End lies in missed series it means it can be merged
if (cachedPoints[0].Timestamp >= series.Points[0].Timestamp && cachedPoints[0].Timestamp <= series.Points[ls-1].Timestamp) ||
(cachedPoints[lc-1].Timestamp >= series.Points[0].Timestamp && cachedPoints[lc-1].Timestamp <= series.Points[ls-1].Timestamp) {

seriesesByLabels[labelsToString(series.Labels)].Points = append(seriesesByLabels[labelsToString(series.Labels)].Points, series.Points...)
} else {
seriesesByLabels[labelsToString(series.Labels)] = series
}
}

// Sort the points in each series by timestamp
Expand Down
Loading
Loading