Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
Update tests to detect labels issue
Browse files Browse the repository at this point in the history
Our labels were too well behaved; the order in which we queried them
was always the order they were stored. This adds another label to
ensure that there's some noise in how we query.
  • Loading branch information
JLockerman committed Aug 7, 2020
1 parent 1f8cf32 commit 6693256
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
5 changes: 3 additions & 2 deletions pkg/pgmodel/end_to_end_tests/query_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1131,6 +1131,7 @@ func generateLargeTimeseries() []prompb.TimeSeries {
metrics := []prompb.TimeSeries{
{
Labels: []prompb.Label{
{Name: "aaa", Value: "000"},
{Name: MetricNameLabelName, Value: "metric_1"},
{Name: "foo", Value: "bar"},
{Name: "instance", Value: "1"},
Expand Down Expand Up @@ -1214,7 +1215,7 @@ func TestPushdown(t *testing.T) {
res: promql.Result{
Value: promql.Matrix{promql.Series{
Points: []promql.Point{{V: 20, T: startTime + 300000}, {V: 20, T: startTime + 330000}},
Metric: labels.FromStrings("foo", "bar", "instance", "1")},
Metric: labels.FromStrings("foo", "bar", "instance", "1", "aaa", "000")},
},
},
},
Expand All @@ -1225,7 +1226,7 @@ func TestPushdown(t *testing.T) {
res: promql.Result{
Value: promql.Vector{promql.Sample{
Point: promql.Point{V: 20, T: startTime + 300*1000},
Metric: labels.FromStrings("foo", "bar", "instance", "1")},
Metric: labels.FromStrings("foo", "bar", "instance", "1", "aaa", "000")},
},
},
},
Expand Down
26 changes: 16 additions & 10 deletions pkg/pgmodel/sql_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,12 @@ func (q *pgxQuerier) getLabelsForIds(ids []int64) (lls labels.Labels, err error)
numHits := q.labels.GetValues(keys, values)

if numHits < len(ids) {
err = q.fetchMissingLabels(keys[numHits:], ids[numHits:], values[numHits:])
var numFetches int
numFetches, err = q.fetchMissingLabels(keys[numHits:], ids[numHits:], values[numHits:])
if err != nil {
return
}
values = values[:numHits+numFetches]
}

lls = make([]labels.Label, 0, len(values))
Expand All @@ -222,13 +224,13 @@ func (q *pgxQuerier) getLabelsForIds(ids []int64) (lls labels.Labels, err error)
return
}

func (q *pgxQuerier) fetchMissingLabels(misses []interface{}, missedIds []int64, newLabels []interface{}) error {
func (q *pgxQuerier) fetchMissingLabels(misses []interface{}, missedIds []int64, newLabels []interface{}) (numNewLabels int, err error) {
for i := range misses {
missedIds[i] = misses[i].(int64)
}
rows, err := q.conn.Query(context.Background(), GetLabelsSQL, missedIds)
if err != nil {
return err
return 0, err
}
defer rows.Close()

Expand All @@ -238,28 +240,32 @@ func (q *pgxQuerier) fetchMissingLabels(misses []interface{}, missedIds []int64,
var vals []string
err = rows.Scan(&ids, &keys, &vals)
if err != nil {
return err
return 0, err
}
if len(ids) != len(keys) {
return fmt.Errorf("query returned a mismatch in ids and keys: %d, %d", len(ids), len(keys))
return 0, fmt.Errorf("query returned a mismatch in ids and keys: %d, %d", len(ids), len(keys))
}
if len(keys) != len(vals) {
return fmt.Errorf("query returned a mismatch in timestamps and values: %d, %d", len(keys), len(vals))
return 0, fmt.Errorf("query returned a mismatch in timestamps and values: %d, %d", len(keys), len(vals))
}
if len(keys) != len(misses) {
return fmt.Errorf("query returned wrong number of labels: %d, %d", len(misses), len(keys))
if len(keys) > len(misses) {
return 0, fmt.Errorf("query returned wrong number of labels: %d, %d", len(misses), len(keys))
}

for i := range misses {
numNewLabels = len(keys)
misses = misses[:len(keys)]
newLabels = newLabels[:len(keys)]
for i := range newLabels {
misses[i] = ids[i]
newLabels[i] = labels.Label{Name: keys[i], Value: vals[i]}
}

numInserted := q.labels.InsertBatch(misses, newLabels)
if numInserted < len(misses) {
log.Warn("msg", "labels cache starving, may need to increase size")
}
}
return nil
return numNewLabels, nil
}

func (q *pgxQuerier) getResultRows(startTimestamp int64, endTimestamp int64, hints *storage.SelectHints, path []parser.Node, matchers []*labels.Matcher) ([]pgx.Rows, parser.Node, error) {
Expand Down

0 comments on commit 6693256

Please sign in to comment.