Remove last vestiges of never used composite index code.

Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
This commit is contained in:
Brian Brazil 2020-01-01 11:21:42 +00:00
parent f9fef91b59
commit e9ede51753
6 changed files with 70 additions and 109 deletions

View File

@ -63,7 +63,7 @@ type IndexReader interface {
Symbols() index.StringIter Symbols() index.StringIter
// LabelValues returns sorted possible label values. // LabelValues returns sorted possible label values.
LabelValues(names ...string) (index.StringTuples, error) LabelValues(name string) (index.StringTuples, error)
// Postings returns the postings list iterator for the label pairs. // Postings returns the postings list iterator for the label pairs.
// The Postings here contain the offsets to the series inside the index. // The Postings here contain the offsets to the series inside the index.
@ -433,8 +433,8 @@ func (r blockIndexReader) Symbols() index.StringIter {
return r.ir.Symbols() return r.ir.Symbols()
} }
func (r blockIndexReader) LabelValues(names ...string) (index.StringTuples, error) { func (r blockIndexReader) LabelValues(name string) (index.StringTuples, error) {
st, err := r.ir.LabelValues(names...) st, err := r.ir.LabelValues(name)
return st, errors.Wrapf(err, "block: %s", r.b.Meta().ULID) return st, errors.Wrapf(err, "block: %s", r.b.Meta().ULID)
} }

View File

@ -31,7 +31,6 @@ import (
"github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/tsdb/chunkenc" "github.com/prometheus/prometheus/tsdb/chunkenc"
"github.com/prometheus/prometheus/tsdb/chunks" "github.com/prometheus/prometheus/tsdb/chunks"
"github.com/prometheus/prometheus/tsdb/encoding"
"github.com/prometheus/prometheus/tsdb/index" "github.com/prometheus/prometheus/tsdb/index"
"github.com/prometheus/prometheus/tsdb/record" "github.com/prometheus/prometheus/tsdb/record"
"github.com/prometheus/prometheus/tsdb/tombstones" "github.com/prometheus/prometheus/tsdb/tombstones"
@ -1353,20 +1352,16 @@ func (h *headIndexReader) Symbols() index.StringIter {
} }
// LabelValues returns the possible label values // LabelValues returns the possible label values
func (h *headIndexReader) LabelValues(names ...string) (index.StringTuples, error) { func (h *headIndexReader) LabelValues(name string) (index.StringTuples, error) {
if len(names) != 1 {
return nil, encoding.ErrInvalidSize
}
h.head.symMtx.RLock() h.head.symMtx.RLock()
sl := make([]string, 0, len(h.head.values[names[0]])) sl := make([]string, 0, len(h.head.values[name]))
for s := range h.head.values[names[0]] { for s := range h.head.values[name] {
sl = append(sl, s) sl = append(sl, s)
} }
h.head.symMtx.RUnlock() h.head.symMtx.RUnlock()
sort.Strings(sl) sort.Strings(sl)
return index.NewStringTuples(sl, len(names)) return index.NewStringTuples(sl)
} }
// LabelNames returns all the unique label names present in the head. // LabelNames returns all the unique label names present in the head.

View File

@ -26,7 +26,6 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"sort" "sort"
"strings"
"unsafe" "unsafe"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -999,7 +998,7 @@ type StringTuples interface {
// Total number of tuples in the list. // Total number of tuples in the list.
Len() int Len() int
// At returns the tuple at position i. // At returns the tuple at position i.
At(i int) ([]string, error) At(i int) (string, error)
} }
// StringIter iterates over a sorted list of strings. // StringIter iterates over a sorted list of strings.
@ -1422,15 +1421,12 @@ func (r *Reader) SymbolTableSize() uint64 {
return uint64(r.symbols.Size()) return uint64(r.symbols.Size())
} }
// LabelValues returns value tuples that exist for the given label name tuples. // LabelValues returns value tuples that exist for the given label name.
// It is not safe to use the return value beyond the lifetime of the byte slice // It is not safe to use the return value beyond the lifetime of the byte slice
// passed into the Reader. // passed into the Reader.
func (r *Reader) LabelValues(names ...string) (StringTuples, error) { func (r *Reader) LabelValues(name string) (StringTuples, error) {
if len(names) != 1 {
return nil, errors.Errorf("only one label name supported")
}
if r.version == FormatV1 { if r.version == FormatV1 {
e, ok := r.postingsV1[names[0]] e, ok := r.postingsV1[name]
if !ok { if !ok {
return emptyStringTuples{}, nil return emptyStringTuples{}, nil
} }
@ -1442,7 +1438,7 @@ func (r *Reader) LabelValues(names ...string) (StringTuples, error) {
return NewStringTuples(values, 1) return NewStringTuples(values, 1)
} }
e, ok := r.postings[names[0]] e, ok := r.postings[name]
if !ok { if !ok {
return emptyStringTuples{}, nil return emptyStringTuples{}, nil
} }
@ -1477,13 +1473,13 @@ func (r *Reader) LabelValues(names ...string) (StringTuples, error) {
if d.Err() != nil { if d.Err() != nil {
return nil, errors.Wrap(d.Err(), "get postings offset entry") return nil, errors.Wrap(d.Err(), "get postings offset entry")
} }
return NewStringTuples(values, 1) return NewStringTuples(values)
} }
type emptyStringTuples struct{} type emptyStringTuples struct{}
func (emptyStringTuples) At(i int) ([]string, error) { return nil, nil } func (emptyStringTuples) At(i int) (string, error) { return "", nil }
func (emptyStringTuples) Len() int { return 0 } func (emptyStringTuples) Len() int { return 0 }
// Series reads the series with the given ID and writes its labels and chunks into lbls and chks. // Series reads the series with the given ID and writes its labels and chunks into lbls and chks.
func (r *Reader) Series(id uint64, lbls *labels.Labels, chks *[]chunks.Meta) error { func (r *Reader) Series(id uint64, lbls *labels.Labels, chks *[]chunks.Meta) error {
@ -1626,47 +1622,24 @@ func (r *Reader) LabelNames() ([]string, error) {
} }
type stringTuples struct { type stringTuples struct {
length int // tuple length
entries []string // flattened tuple entries entries []string // flattened tuple entries
swapBuf []string
} }
func NewStringTuples(entries []string, length int) (*stringTuples, error) { func NewStringTuples(entries []string) (*stringTuples, error) {
if len(entries)%length != 0 {
return nil, errors.Wrap(encoding.ErrInvalidSize, "string tuple list")
}
return &stringTuples{ return &stringTuples{
entries: entries, entries: entries,
length: length,
}, nil }, nil
} }
func (t *stringTuples) Len() int { return len(t.entries) / t.length } func (t *stringTuples) Len() int { return len(t.entries) }
func (t *stringTuples) At(i int) ([]string, error) { return t.entries[i : i+t.length], nil } func (t *stringTuples) At(i int) (string, error) { return t.entries[i], nil }
func (t *stringTuples) Swap(i, j int) { func (t *stringTuples) Swap(i, j int) {
if t.swapBuf == nil { t.entries[i], t.entries[j] = t.entries[j], t.entries[i]
t.swapBuf = make([]string, t.length)
}
copy(t.swapBuf, t.entries[i:i+t.length])
for k := 0; k < t.length; k++ {
t.entries[i+k] = t.entries[j+k]
t.entries[j+k] = t.swapBuf[k]
}
} }
func (t *stringTuples) Less(i, j int) bool { func (t *stringTuples) Less(i, j int) bool {
for k := 0; k < t.length; k++ { return t.entries[i] < t.entries[j]
d := strings.Compare(t.entries[i+k], t.entries[j+k])
if d < 0 {
return true
}
if d > 0 {
return false
}
}
return false
} }
// NewStringListIterator returns a StringIter for the given sorted list of strings. // NewStringListIterator returns a StringIter for the given sorted list of strings.

View File

@ -37,18 +37,16 @@ type series struct {
} }
type mockIndex struct { type mockIndex struct {
series map[uint64]series series map[uint64]series
labelIndex map[string][]string postings map[labels.Label][]uint64
postings map[labels.Label][]uint64 symbols map[string]struct{}
symbols map[string]struct{}
} }
func newMockIndex() mockIndex { func newMockIndex() mockIndex {
ix := mockIndex{ ix := mockIndex{
series: make(map[uint64]series), series: make(map[uint64]series),
labelIndex: make(map[string][]string), postings: make(map[labels.Label][]uint64),
postings: make(map[labels.Label][]uint64), symbols: make(map[string]struct{}),
symbols: make(map[string]struct{}),
} }
ix.postings[allPostingsKey] = []uint64{} ix.postings[allPostingsKey] = []uint64{}
return ix return ix
@ -87,13 +85,14 @@ func (m mockIndex) Close() error {
return nil return nil
} }
func (m mockIndex) LabelValues(names ...string) (StringTuples, error) { func (m mockIndex) LabelValues(name string) (StringTuples, error) {
// TODO support composite indexes values := []string{}
if len(names) != 1 { for l := range m.postings {
return nil, errors.New("composite indexes not supported yet") if l.Name == name {
values = append(values, l.Value)
}
} }
return NewStringTuples(values)
return NewStringTuples(m.labelIndex[names[0]], 1)
} }
func (m mockIndex) Postings(name string, values ...string) (Postings, error) { func (m mockIndex) Postings(name string, values ...string) (Postings, error) {
@ -446,8 +445,13 @@ func TestPersistence_index_e2e(t *testing.T) {
testutil.Ok(t, gotp.Err()) testutil.Ok(t, gotp.Err())
} }
for k, v := range mi.labelIndex { labelPairs := map[string][]string{}
tplsExp, err := NewStringTuples(v, 1) for l := range mi.postings {
labelPairs[l.Name] = append(labelPairs[l.Name], l.Value)
}
for k, v := range labelPairs {
sort.Strings(v)
tplsExp, err := NewStringTuples(v)
testutil.Ok(t, err) testutil.Ok(t, err)
tplsRes, err := ir.LabelValues(k) tplsRes, err := ir.LabelValues(k)

View File

@ -225,11 +225,11 @@ func (q *blockQuerier) LabelValues(name string) ([]string, error) {
res := make([]string, 0, tpls.Len()) res := make([]string, 0, tpls.Len())
for i := 0; i < tpls.Len(); i++ { for i := 0; i < tpls.Len(); i++ {
vals, err := tpls.At(i) val, err := tpls.At(i)
if err != nil { if err != nil {
return nil, err return nil, err
} }
res = append(res, vals[0]) res = append(res, val)
} }
return res, nil return res, nil
} }
@ -419,12 +419,12 @@ func postingsForMatcher(ix IndexReader, m *labels.Matcher) (index.Postings, erro
var res []string var res []string
for i := 0; i < tpls.Len(); i++ { for i := 0; i < tpls.Len(); i++ {
vals, err := tpls.At(i) val, err := tpls.At(i)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if m.Matches(vals[0]) { if m.Matches(val) {
res = append(res, vals[0]) res = append(res, val)
} }
} }
@ -444,13 +444,13 @@ func inversePostingsForMatcher(ix IndexReader, m *labels.Matcher) (index.Posting
var res []string var res []string
for i := 0; i < tpls.Len(); i++ { for i := 0; i < tpls.Len(); i++ {
vals, err := tpls.At(i) val, err := tpls.At(i)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if !m.Matches(vals[0]) { if !m.Matches(val) {
res = append(res, vals[0]) res = append(res, val)
} }
} }

View File

@ -250,10 +250,6 @@ func createIdxChkReaders(t *testing.T, tc []seriesSamples) (IndexReader, ChunkRe
} }
} }
for l, vs := range lblIdx {
testutil.Ok(t, mi.WriteLabelIndex([]string{l}, vs.slice()))
}
testutil.Ok(t, postings.Iter(func(l labels.Label, p index.Postings) error { testutil.Ok(t, postings.Iter(func(l labels.Label, p index.Postings) error {
return mi.WritePostings(l.Name, l.Value, p) return mi.WritePostings(l.Name, l.Value, p)
})) }))
@ -1300,18 +1296,16 @@ type series struct {
} }
type mockIndex struct { type mockIndex struct {
series map[uint64]series series map[uint64]series
labelIndex map[string][]string postings map[labels.Label][]uint64
postings map[labels.Label][]uint64 symbols map[string]struct{}
symbols map[string]struct{}
} }
func newMockIndex() mockIndex { func newMockIndex() mockIndex {
ix := mockIndex{ ix := mockIndex{
series: make(map[uint64]series), series: make(map[uint64]series),
labelIndex: make(map[string][]string), postings: make(map[labels.Label][]uint64),
postings: make(map[labels.Label][]uint64), symbols: make(map[string]struct{}),
symbols: make(map[string]struct{}),
} }
return ix return ix
} }
@ -1345,16 +1339,6 @@ func (m *mockIndex) AddSeries(ref uint64, l labels.Labels, chunks ...chunks.Meta
return nil return nil
} }
func (m mockIndex) WriteLabelIndex(names []string, values []string) error {
// TODO support composite indexes
if len(names) != 1 {
return errors.New("composite indexes not supported yet")
}
sort.Strings(values)
m.labelIndex[names[0]] = values
return nil
}
func (m mockIndex) WritePostings(name, value string, it index.Postings) error { func (m mockIndex) WritePostings(name, value string, it index.Postings) error {
l := labels.Label{Name: name, Value: value} l := labels.Label{Name: name, Value: value}
if _, ok := m.postings[l]; ok { if _, ok := m.postings[l]; ok {
@ -1372,13 +1356,14 @@ func (m mockIndex) Close() error {
return nil return nil
} }
func (m mockIndex) LabelValues(names ...string) (index.StringTuples, error) { func (m mockIndex) LabelValues(name string) (index.StringTuples, error) {
// TODO support composite indexes values := []string{}
if len(names) != 1 { for l := range m.postings {
return nil, errors.New("composite indexes not supported yet") if l.Name == name {
values = append(values, l.Value)
}
} }
return index.NewStringTuples(values)
return index.NewStringTuples(m.labelIndex[names[0]], 1)
} }
func (m mockIndex) Postings(name string, values ...string) (index.Postings, error) { func (m mockIndex) Postings(name string, values ...string) (index.Postings, error) {
@ -1414,12 +1399,16 @@ func (m mockIndex) Series(ref uint64, lset *labels.Labels, chks *[]chunks.Meta)
} }
func (m mockIndex) LabelNames() ([]string, error) { func (m mockIndex) LabelNames() ([]string, error) {
labelNames := make([]string, 0, len(m.labelIndex)) names := map[string]struct{}{}
for name := range m.labelIndex { for l := range m.postings {
labelNames = append(labelNames, name) names[l.Name] = struct{}{}
} }
sort.Strings(labelNames) l := make([]string, 0, len(names))
return labelNames, nil for name := range names {
l = append(l, name)
}
sort.Strings(l)
return l, nil
} }
type mockSeries struct { type mockSeries struct {