2017-04-10 18:59:45 +00:00
|
|
|
// Copyright 2017 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-11-30 14:34:49 +00:00
|
|
|
package index
|
2017-03-06 16:36:03 +00:00
|
|
|
|
|
|
|
import (
|
Reduce memory used by postings offset table.
Rather than keeping the offset of each postings list, instead
keep the nth offset of the offset of the posting list. As postings
list offsets have always been sorted, we can then get to the closest
entry before the one we want an iterate forwards.
I haven't done much tuning on the 32 number, it was chosen to try
not to read through more than a 4k page of data.
Switch to a bulk interface for fetching postings. Use it to avoid having
to re-read parts of the posting offset table when querying lots of it.
For a index with what BenchmarkHeadPostingForMatchers uses RAM
for r.postings drops from 3.79MB to 80.19kB or about 48x.
Bytes allocated go down by 30%, and suprisingly CPU usage drops by
4-6% for typical queries too.
benchmark old ns/op new ns/op delta
BenchmarkPostingsForMatchers/Block/n="1"-4 35231 36673 +4.09%
BenchmarkPostingsForMatchers/Block/n="1",j="foo"-4 563380 540627 -4.04%
BenchmarkPostingsForMatchers/Block/j="foo",n="1"-4 536782 534186 -0.48%
BenchmarkPostingsForMatchers/Block/n="1",j!="foo"-4 533990 541550 +1.42%
BenchmarkPostingsForMatchers/Block/i=~".*"-4 113374598 117969608 +4.05%
BenchmarkPostingsForMatchers/Block/i=~".+"-4 146329884 139651442 -4.56%
BenchmarkPostingsForMatchers/Block/i=~""-4 50346510 44961127 -10.70%
BenchmarkPostingsForMatchers/Block/i!=""-4 41261550 35356165 -14.31%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",j="foo"-4 112544418 116904010 +3.87%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",i!="2",j="foo"-4 112487086 116864918 +3.89%
BenchmarkPostingsForMatchers/Block/n="1",i!=""-4 41094758 35457904 -13.72%
BenchmarkPostingsForMatchers/Block/n="1",i!="",j="foo"-4 41906372 36151473 -13.73%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",j="foo"-4 147262414 140424800 -4.64%
BenchmarkPostingsForMatchers/Block/n="1",i=~"1.+",j="foo"-4 28615629 27872072 -2.60%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!="2",j="foo"-4 147117177 140462403 -4.52%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!~"2.*",j="foo"-4 175096826 167902298 -4.11%
benchmark old allocs new allocs delta
BenchmarkPostingsForMatchers/Block/n="1"-4 4 6 +50.00%
BenchmarkPostingsForMatchers/Block/n="1",j="foo"-4 7 11 +57.14%
BenchmarkPostingsForMatchers/Block/j="foo",n="1"-4 7 11 +57.14%
BenchmarkPostingsForMatchers/Block/n="1",j!="foo"-4 15 17 +13.33%
BenchmarkPostingsForMatchers/Block/i=~".*"-4 100010 100012 +0.00%
BenchmarkPostingsForMatchers/Block/i=~".+"-4 200069 200040 -0.01%
BenchmarkPostingsForMatchers/Block/i=~""-4 200072 200045 -0.01%
BenchmarkPostingsForMatchers/Block/i!=""-4 200070 200041 -0.01%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",j="foo"-4 100013 100017 +0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",i!="2",j="foo"-4 100017 100023 +0.01%
BenchmarkPostingsForMatchers/Block/n="1",i!=""-4 200073 200046 -0.01%
BenchmarkPostingsForMatchers/Block/n="1",i!="",j="foo"-4 200075 200050 -0.01%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",j="foo"-4 200074 200049 -0.01%
BenchmarkPostingsForMatchers/Block/n="1",i=~"1.+",j="foo"-4 111165 111150 -0.01%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!="2",j="foo"-4 200078 200055 -0.01%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!~"2.*",j="foo"-4 311282 311238 -0.01%
benchmark old bytes new bytes delta
BenchmarkPostingsForMatchers/Block/n="1"-4 264 296 +12.12%
BenchmarkPostingsForMatchers/Block/n="1",j="foo"-4 360 424 +17.78%
BenchmarkPostingsForMatchers/Block/j="foo",n="1"-4 360 424 +17.78%
BenchmarkPostingsForMatchers/Block/n="1",j!="foo"-4 520 552 +6.15%
BenchmarkPostingsForMatchers/Block/i=~".*"-4 1600461 1600482 +0.00%
BenchmarkPostingsForMatchers/Block/i=~".+"-4 24900801 17259077 -30.69%
BenchmarkPostingsForMatchers/Block/i=~""-4 24900836 17259151 -30.69%
BenchmarkPostingsForMatchers/Block/i!=""-4 24900760 17259048 -30.69%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",j="foo"-4 1600557 1600621 +0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",i!="2",j="foo"-4 1600717 1600813 +0.01%
BenchmarkPostingsForMatchers/Block/n="1",i!=""-4 24900856 17259176 -30.69%
BenchmarkPostingsForMatchers/Block/n="1",i!="",j="foo"-4 24900952 17259304 -30.69%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",j="foo"-4 24900993 17259333 -30.69%
BenchmarkPostingsForMatchers/Block/n="1",i=~"1.+",j="foo"-4 3788311 3142630 -17.04%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!="2",j="foo"-4 24901137 17259509 -30.69%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!~"2.*",j="foo"-4 28693086 20405680 -28.88%
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
2019-12-05 18:27:40 +00:00
|
|
|
"fmt"
|
2017-03-06 16:36:03 +00:00
|
|
|
"io/ioutil"
|
2017-03-14 14:24:08 +00:00
|
|
|
"math/rand"
|
2017-03-06 16:36:03 +00:00
|
|
|
"os"
|
2017-03-08 15:54:13 +00:00
|
|
|
"path/filepath"
|
2017-03-14 14:24:08 +00:00
|
|
|
"sort"
|
2017-03-07 10:29:20 +00:00
|
|
|
"testing"
|
2017-03-06 16:36:03 +00:00
|
|
|
|
2017-03-29 23:18:41 +00:00
|
|
|
"github.com/pkg/errors"
|
2019-11-18 19:53:33 +00:00
|
|
|
"github.com/prometheus/prometheus/pkg/labels"
|
2019-08-13 08:34:14 +00:00
|
|
|
"github.com/prometheus/prometheus/tsdb/chunkenc"
|
|
|
|
"github.com/prometheus/prometheus/tsdb/chunks"
|
|
|
|
"github.com/prometheus/prometheus/tsdb/encoding"
|
2019-08-14 09:07:02 +00:00
|
|
|
"github.com/prometheus/prometheus/util/testutil"
|
2017-03-06 16:36:03 +00:00
|
|
|
)
|
|
|
|
|
2017-03-29 23:18:41 +00:00
|
|
|
type series struct {
|
|
|
|
l labels.Labels
|
2017-11-30 14:34:49 +00:00
|
|
|
chunks []chunks.Meta
|
2017-03-07 10:29:20 +00:00
|
|
|
}
|
|
|
|
|
2017-03-29 23:18:41 +00:00
|
|
|
type mockIndex struct {
|
2017-09-04 14:08:38 +00:00
|
|
|
series map[uint64]series
|
2017-03-29 23:18:41 +00:00
|
|
|
labelIndex map[string][]string
|
2017-11-30 14:34:49 +00:00
|
|
|
postings map[labels.Label][]uint64
|
2017-08-05 11:31:48 +00:00
|
|
|
symbols map[string]struct{}
|
2017-03-07 10:29:20 +00:00
|
|
|
}
|
|
|
|
|
2017-03-29 23:18:41 +00:00
|
|
|
func newMockIndex() mockIndex {
|
2017-10-09 13:21:46 +00:00
|
|
|
ix := mockIndex{
|
2017-09-04 14:08:38 +00:00
|
|
|
series: make(map[uint64]series),
|
2017-03-29 23:18:41 +00:00
|
|
|
labelIndex: make(map[string][]string),
|
2017-11-30 14:34:49 +00:00
|
|
|
postings: make(map[labels.Label][]uint64),
|
2017-08-05 11:31:48 +00:00
|
|
|
symbols: make(map[string]struct{}),
|
2017-03-29 23:18:41 +00:00
|
|
|
}
|
2017-10-09 13:21:46 +00:00
|
|
|
return ix
|
2017-03-29 23:18:41 +00:00
|
|
|
}
|
|
|
|
|
2017-08-05 11:31:48 +00:00
|
|
|
func (m mockIndex) Symbols() (map[string]struct{}, error) {
|
|
|
|
return m.symbols, nil
|
|
|
|
}
|
|
|
|
|
2017-11-30 14:34:49 +00:00
|
|
|
func (m mockIndex) AddSeries(ref uint64, l labels.Labels, chunks ...chunks.Meta) error {
|
2017-03-29 23:18:41 +00:00
|
|
|
if _, ok := m.series[ref]; ok {
|
|
|
|
return errors.Errorf("series with reference %d already added", ref)
|
|
|
|
}
|
2017-08-05 11:31:48 +00:00
|
|
|
for _, lbl := range l {
|
|
|
|
m.symbols[lbl.Name] = struct{}{}
|
|
|
|
m.symbols[lbl.Value] = struct{}{}
|
|
|
|
}
|
2017-03-29 23:18:41 +00:00
|
|
|
|
2017-04-24 15:10:12 +00:00
|
|
|
s := series{l: l}
|
|
|
|
// Actual chunk data is not stored in the index.
|
|
|
|
for _, c := range chunks {
|
2017-08-06 18:41:24 +00:00
|
|
|
c.Chunk = nil
|
|
|
|
s.chunks = append(s.chunks, c)
|
2017-03-29 23:18:41 +00:00
|
|
|
}
|
2017-04-24 15:10:12 +00:00
|
|
|
m.series[ref] = s
|
2017-03-29 23:18:41 +00:00
|
|
|
|
|
|
|
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 Postings) error {
|
2017-11-30 14:34:49 +00:00
|
|
|
l := labels.Label{Name: name, Value: value}
|
|
|
|
if _, ok := m.postings[l]; ok {
|
|
|
|
return errors.Errorf("postings for %s already added", l)
|
2017-03-29 23:18:41 +00:00
|
|
|
}
|
2017-11-30 14:34:49 +00:00
|
|
|
ep, err := ExpandPostings(it)
|
2017-08-05 11:31:48 +00:00
|
|
|
if err != nil {
|
2017-03-29 23:18:41 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-11-30 14:34:49 +00:00
|
|
|
m.postings[l] = ep
|
|
|
|
return nil
|
2017-03-29 23:18:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m mockIndex) Close() error {
|
|
|
|
return nil
|
2017-03-07 10:29:20 +00:00
|
|
|
}
|
|
|
|
|
2017-03-29 23:18:41 +00:00
|
|
|
func (m mockIndex) LabelValues(names ...string) (StringTuples, error) {
|
|
|
|
// TODO support composite indexes
|
|
|
|
if len(names) != 1 {
|
|
|
|
return nil, errors.New("composite indexes not supported yet")
|
|
|
|
}
|
|
|
|
|
2017-11-30 14:34:49 +00:00
|
|
|
return NewStringTuples(m.labelIndex[names[0]], 1)
|
2017-03-07 10:29:20 +00:00
|
|
|
}
|
|
|
|
|
Reduce memory used by postings offset table.
Rather than keeping the offset of each postings list, instead
keep the nth offset of the offset of the posting list. As postings
list offsets have always been sorted, we can then get to the closest
entry before the one we want an iterate forwards.
I haven't done much tuning on the 32 number, it was chosen to try
not to read through more than a 4k page of data.
Switch to a bulk interface for fetching postings. Use it to avoid having
to re-read parts of the posting offset table when querying lots of it.
For a index with what BenchmarkHeadPostingForMatchers uses RAM
for r.postings drops from 3.79MB to 80.19kB or about 48x.
Bytes allocated go down by 30%, and suprisingly CPU usage drops by
4-6% for typical queries too.
benchmark old ns/op new ns/op delta
BenchmarkPostingsForMatchers/Block/n="1"-4 35231 36673 +4.09%
BenchmarkPostingsForMatchers/Block/n="1",j="foo"-4 563380 540627 -4.04%
BenchmarkPostingsForMatchers/Block/j="foo",n="1"-4 536782 534186 -0.48%
BenchmarkPostingsForMatchers/Block/n="1",j!="foo"-4 533990 541550 +1.42%
BenchmarkPostingsForMatchers/Block/i=~".*"-4 113374598 117969608 +4.05%
BenchmarkPostingsForMatchers/Block/i=~".+"-4 146329884 139651442 -4.56%
BenchmarkPostingsForMatchers/Block/i=~""-4 50346510 44961127 -10.70%
BenchmarkPostingsForMatchers/Block/i!=""-4 41261550 35356165 -14.31%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",j="foo"-4 112544418 116904010 +3.87%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",i!="2",j="foo"-4 112487086 116864918 +3.89%
BenchmarkPostingsForMatchers/Block/n="1",i!=""-4 41094758 35457904 -13.72%
BenchmarkPostingsForMatchers/Block/n="1",i!="",j="foo"-4 41906372 36151473 -13.73%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",j="foo"-4 147262414 140424800 -4.64%
BenchmarkPostingsForMatchers/Block/n="1",i=~"1.+",j="foo"-4 28615629 27872072 -2.60%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!="2",j="foo"-4 147117177 140462403 -4.52%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!~"2.*",j="foo"-4 175096826 167902298 -4.11%
benchmark old allocs new allocs delta
BenchmarkPostingsForMatchers/Block/n="1"-4 4 6 +50.00%
BenchmarkPostingsForMatchers/Block/n="1",j="foo"-4 7 11 +57.14%
BenchmarkPostingsForMatchers/Block/j="foo",n="1"-4 7 11 +57.14%
BenchmarkPostingsForMatchers/Block/n="1",j!="foo"-4 15 17 +13.33%
BenchmarkPostingsForMatchers/Block/i=~".*"-4 100010 100012 +0.00%
BenchmarkPostingsForMatchers/Block/i=~".+"-4 200069 200040 -0.01%
BenchmarkPostingsForMatchers/Block/i=~""-4 200072 200045 -0.01%
BenchmarkPostingsForMatchers/Block/i!=""-4 200070 200041 -0.01%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",j="foo"-4 100013 100017 +0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",i!="2",j="foo"-4 100017 100023 +0.01%
BenchmarkPostingsForMatchers/Block/n="1",i!=""-4 200073 200046 -0.01%
BenchmarkPostingsForMatchers/Block/n="1",i!="",j="foo"-4 200075 200050 -0.01%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",j="foo"-4 200074 200049 -0.01%
BenchmarkPostingsForMatchers/Block/n="1",i=~"1.+",j="foo"-4 111165 111150 -0.01%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!="2",j="foo"-4 200078 200055 -0.01%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!~"2.*",j="foo"-4 311282 311238 -0.01%
benchmark old bytes new bytes delta
BenchmarkPostingsForMatchers/Block/n="1"-4 264 296 +12.12%
BenchmarkPostingsForMatchers/Block/n="1",j="foo"-4 360 424 +17.78%
BenchmarkPostingsForMatchers/Block/j="foo",n="1"-4 360 424 +17.78%
BenchmarkPostingsForMatchers/Block/n="1",j!="foo"-4 520 552 +6.15%
BenchmarkPostingsForMatchers/Block/i=~".*"-4 1600461 1600482 +0.00%
BenchmarkPostingsForMatchers/Block/i=~".+"-4 24900801 17259077 -30.69%
BenchmarkPostingsForMatchers/Block/i=~""-4 24900836 17259151 -30.69%
BenchmarkPostingsForMatchers/Block/i!=""-4 24900760 17259048 -30.69%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",j="foo"-4 1600557 1600621 +0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",i!="2",j="foo"-4 1600717 1600813 +0.01%
BenchmarkPostingsForMatchers/Block/n="1",i!=""-4 24900856 17259176 -30.69%
BenchmarkPostingsForMatchers/Block/n="1",i!="",j="foo"-4 24900952 17259304 -30.69%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",j="foo"-4 24900993 17259333 -30.69%
BenchmarkPostingsForMatchers/Block/n="1",i=~"1.+",j="foo"-4 3788311 3142630 -17.04%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!="2",j="foo"-4 24901137 17259509 -30.69%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!~"2.*",j="foo"-4 28693086 20405680 -28.88%
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
2019-12-05 18:27:40 +00:00
|
|
|
func (m mockIndex) Postings(name string, values ...string) (Postings, error) {
|
|
|
|
p := []Postings{}
|
|
|
|
for _, value := range values {
|
|
|
|
l := labels.Label{Name: name, Value: value}
|
|
|
|
p = append(p, NewListPostings(m.postings[l]))
|
|
|
|
}
|
|
|
|
return Merge(p...), nil
|
2017-08-05 11:31:48 +00:00
|
|
|
}
|
2017-03-29 23:18:41 +00:00
|
|
|
|
2017-08-05 11:31:48 +00:00
|
|
|
func (m mockIndex) SortedPostings(p Postings) Postings {
|
2017-11-30 14:34:49 +00:00
|
|
|
ep, err := ExpandPostings(p)
|
2017-08-05 11:31:48 +00:00
|
|
|
if err != nil {
|
2017-11-30 14:34:49 +00:00
|
|
|
return ErrPostings(errors.Wrap(err, "expand postings"))
|
2017-03-29 23:18:41 +00:00
|
|
|
}
|
|
|
|
|
2017-08-05 11:31:48 +00:00
|
|
|
sort.Slice(ep, func(i, j int) bool {
|
|
|
|
return labels.Compare(m.series[ep[i]].l, m.series[ep[j]].l) < 0
|
|
|
|
})
|
2017-11-30 14:34:49 +00:00
|
|
|
return NewListPostings(ep)
|
2017-03-07 10:29:20 +00:00
|
|
|
}
|
|
|
|
|
2017-11-30 14:34:49 +00:00
|
|
|
func (m mockIndex) Series(ref uint64, lset *labels.Labels, chks *[]chunks.Meta) error {
|
2017-03-29 23:18:41 +00:00
|
|
|
s, ok := m.series[ref]
|
|
|
|
if !ok {
|
2017-11-30 14:34:49 +00:00
|
|
|
return errors.New("not found")
|
2017-03-29 23:18:41 +00:00
|
|
|
}
|
2017-08-05 11:31:48 +00:00
|
|
|
*lset = append((*lset)[:0], s.l...)
|
|
|
|
*chks = append((*chks)[:0], s.chunks...)
|
2017-03-29 23:18:41 +00:00
|
|
|
|
2017-08-05 11:31:48 +00:00
|
|
|
return nil
|
2017-03-29 23:18:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m mockIndex) LabelIndices() ([][]string, error) {
|
|
|
|
res := make([][]string, 0, len(m.labelIndex))
|
|
|
|
for k := range m.labelIndex {
|
|
|
|
res = append(res, []string{k})
|
|
|
|
}
|
|
|
|
return res, nil
|
2017-03-07 10:29:20 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 15:54:13 +00:00
|
|
|
func TestIndexRW_Create_Open(t *testing.T) {
|
|
|
|
dir, err := ioutil.TempDir("", "test_index_create")
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2019-03-19 13:31:57 +00:00
|
|
|
defer func() {
|
|
|
|
testutil.Ok(t, os.RemoveAll(dir))
|
|
|
|
}()
|
2017-03-06 16:36:03 +00:00
|
|
|
|
2019-01-29 08:32:32 +00:00
|
|
|
fn := filepath.Join(dir, indexFilename)
|
2017-11-30 14:34:49 +00:00
|
|
|
|
2017-03-08 15:54:13 +00:00
|
|
|
// An empty index must still result in a readable file.
|
2017-11-30 14:34:49 +00:00
|
|
|
iw, err := NewWriter(fn)
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Ok(t, iw.Close())
|
2017-03-06 16:36:03 +00:00
|
|
|
|
2018-02-09 12:11:03 +00:00
|
|
|
ir, err := NewFileReader(fn)
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Ok(t, ir.Close())
|
2017-03-06 16:36:03 +00:00
|
|
|
|
2017-03-08 15:54:13 +00:00
|
|
|
// Modify magic header must cause open to fail.
|
2017-11-30 14:34:49 +00:00
|
|
|
f, err := os.OpenFile(fn, os.O_WRONLY, 0666)
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2017-03-08 15:54:13 +00:00
|
|
|
_, err = f.WriteAt([]byte{0, 0}, 0)
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2019-03-19 13:31:57 +00:00
|
|
|
f.Close()
|
2017-03-06 16:36:03 +00:00
|
|
|
|
2018-02-09 12:11:03 +00:00
|
|
|
_, err = NewFileReader(dir)
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.NotOk(t, err)
|
2017-03-08 15:54:13 +00:00
|
|
|
}
|
2017-03-06 16:36:03 +00:00
|
|
|
|
2017-03-09 08:39:30 +00:00
|
|
|
func TestIndexRW_Postings(t *testing.T) {
|
|
|
|
dir, err := ioutil.TempDir("", "test_index_postings")
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2019-03-19 13:31:57 +00:00
|
|
|
defer func() {
|
|
|
|
testutil.Ok(t, os.RemoveAll(dir))
|
|
|
|
}()
|
2017-03-09 08:39:30 +00:00
|
|
|
|
2019-01-29 08:32:32 +00:00
|
|
|
fn := filepath.Join(dir, indexFilename)
|
2017-11-30 14:34:49 +00:00
|
|
|
|
|
|
|
iw, err := NewWriter(fn)
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2017-03-09 08:39:30 +00:00
|
|
|
|
|
|
|
series := []labels.Labels{
|
|
|
|
labels.FromStrings("a", "1", "b", "1"),
|
|
|
|
labels.FromStrings("a", "1", "b", "2"),
|
|
|
|
labels.FromStrings("a", "1", "b", "3"),
|
|
|
|
labels.FromStrings("a", "1", "b", "4"),
|
|
|
|
}
|
|
|
|
|
2017-08-05 11:31:48 +00:00
|
|
|
err = iw.AddSymbols(map[string]struct{}{
|
2018-05-07 12:39:54 +00:00
|
|
|
"a": {},
|
|
|
|
"b": {},
|
|
|
|
"1": {},
|
|
|
|
"2": {},
|
|
|
|
"3": {},
|
|
|
|
"4": {},
|
2017-08-05 11:31:48 +00:00
|
|
|
})
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2017-08-05 11:31:48 +00:00
|
|
|
|
2017-03-09 08:39:30 +00:00
|
|
|
// Postings lists are only written if a series with the respective
|
|
|
|
// reference was added before.
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, iw.AddSeries(1, series[0]))
|
|
|
|
testutil.Ok(t, iw.AddSeries(2, series[1]))
|
|
|
|
testutil.Ok(t, iw.AddSeries(3, series[2]))
|
|
|
|
testutil.Ok(t, iw.AddSeries(4, series[3]))
|
2017-03-09 08:39:30 +00:00
|
|
|
|
2019-03-21 16:23:00 +00:00
|
|
|
err = iw.WritePostings("a", "1", newListPostings(1, 2, 3, 4))
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2017-03-09 08:39:30 +00:00
|
|
|
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, iw.Close())
|
2017-03-09 08:39:30 +00:00
|
|
|
|
2018-02-09 12:11:03 +00:00
|
|
|
ir, err := NewFileReader(fn)
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2017-03-09 08:39:30 +00:00
|
|
|
|
|
|
|
p, err := ir.Postings("a", "1")
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2017-03-09 08:39:30 +00:00
|
|
|
|
2017-08-05 11:31:48 +00:00
|
|
|
var l labels.Labels
|
2017-11-30 14:34:49 +00:00
|
|
|
var c []chunks.Meta
|
2017-08-05 11:31:48 +00:00
|
|
|
|
2017-03-09 08:39:30 +00:00
|
|
|
for i := 0; p.Next(); i++ {
|
2017-08-05 11:31:48 +00:00
|
|
|
err := ir.Series(p.At(), &l, &c)
|
2017-03-09 08:39:30 +00:00
|
|
|
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Equals(t, 0, len(c))
|
|
|
|
testutil.Equals(t, series[i], l)
|
2017-03-09 08:39:30 +00:00
|
|
|
}
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, p.Err())
|
2017-03-09 08:39:30 +00:00
|
|
|
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, ir.Close())
|
2017-03-09 08:39:30 +00:00
|
|
|
}
|
|
|
|
|
Reduce memory used by postings offset table.
Rather than keeping the offset of each postings list, instead
keep the nth offset of the offset of the posting list. As postings
list offsets have always been sorted, we can then get to the closest
entry before the one we want an iterate forwards.
I haven't done much tuning on the 32 number, it was chosen to try
not to read through more than a 4k page of data.
Switch to a bulk interface for fetching postings. Use it to avoid having
to re-read parts of the posting offset table when querying lots of it.
For a index with what BenchmarkHeadPostingForMatchers uses RAM
for r.postings drops from 3.79MB to 80.19kB or about 48x.
Bytes allocated go down by 30%, and suprisingly CPU usage drops by
4-6% for typical queries too.
benchmark old ns/op new ns/op delta
BenchmarkPostingsForMatchers/Block/n="1"-4 35231 36673 +4.09%
BenchmarkPostingsForMatchers/Block/n="1",j="foo"-4 563380 540627 -4.04%
BenchmarkPostingsForMatchers/Block/j="foo",n="1"-4 536782 534186 -0.48%
BenchmarkPostingsForMatchers/Block/n="1",j!="foo"-4 533990 541550 +1.42%
BenchmarkPostingsForMatchers/Block/i=~".*"-4 113374598 117969608 +4.05%
BenchmarkPostingsForMatchers/Block/i=~".+"-4 146329884 139651442 -4.56%
BenchmarkPostingsForMatchers/Block/i=~""-4 50346510 44961127 -10.70%
BenchmarkPostingsForMatchers/Block/i!=""-4 41261550 35356165 -14.31%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",j="foo"-4 112544418 116904010 +3.87%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",i!="2",j="foo"-4 112487086 116864918 +3.89%
BenchmarkPostingsForMatchers/Block/n="1",i!=""-4 41094758 35457904 -13.72%
BenchmarkPostingsForMatchers/Block/n="1",i!="",j="foo"-4 41906372 36151473 -13.73%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",j="foo"-4 147262414 140424800 -4.64%
BenchmarkPostingsForMatchers/Block/n="1",i=~"1.+",j="foo"-4 28615629 27872072 -2.60%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!="2",j="foo"-4 147117177 140462403 -4.52%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!~"2.*",j="foo"-4 175096826 167902298 -4.11%
benchmark old allocs new allocs delta
BenchmarkPostingsForMatchers/Block/n="1"-4 4 6 +50.00%
BenchmarkPostingsForMatchers/Block/n="1",j="foo"-4 7 11 +57.14%
BenchmarkPostingsForMatchers/Block/j="foo",n="1"-4 7 11 +57.14%
BenchmarkPostingsForMatchers/Block/n="1",j!="foo"-4 15 17 +13.33%
BenchmarkPostingsForMatchers/Block/i=~".*"-4 100010 100012 +0.00%
BenchmarkPostingsForMatchers/Block/i=~".+"-4 200069 200040 -0.01%
BenchmarkPostingsForMatchers/Block/i=~""-4 200072 200045 -0.01%
BenchmarkPostingsForMatchers/Block/i!=""-4 200070 200041 -0.01%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",j="foo"-4 100013 100017 +0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",i!="2",j="foo"-4 100017 100023 +0.01%
BenchmarkPostingsForMatchers/Block/n="1",i!=""-4 200073 200046 -0.01%
BenchmarkPostingsForMatchers/Block/n="1",i!="",j="foo"-4 200075 200050 -0.01%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",j="foo"-4 200074 200049 -0.01%
BenchmarkPostingsForMatchers/Block/n="1",i=~"1.+",j="foo"-4 111165 111150 -0.01%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!="2",j="foo"-4 200078 200055 -0.01%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!~"2.*",j="foo"-4 311282 311238 -0.01%
benchmark old bytes new bytes delta
BenchmarkPostingsForMatchers/Block/n="1"-4 264 296 +12.12%
BenchmarkPostingsForMatchers/Block/n="1",j="foo"-4 360 424 +17.78%
BenchmarkPostingsForMatchers/Block/j="foo",n="1"-4 360 424 +17.78%
BenchmarkPostingsForMatchers/Block/n="1",j!="foo"-4 520 552 +6.15%
BenchmarkPostingsForMatchers/Block/i=~".*"-4 1600461 1600482 +0.00%
BenchmarkPostingsForMatchers/Block/i=~".+"-4 24900801 17259077 -30.69%
BenchmarkPostingsForMatchers/Block/i=~""-4 24900836 17259151 -30.69%
BenchmarkPostingsForMatchers/Block/i!=""-4 24900760 17259048 -30.69%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",j="foo"-4 1600557 1600621 +0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",i!="2",j="foo"-4 1600717 1600813 +0.01%
BenchmarkPostingsForMatchers/Block/n="1",i!=""-4 24900856 17259176 -30.69%
BenchmarkPostingsForMatchers/Block/n="1",i!="",j="foo"-4 24900952 17259304 -30.69%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",j="foo"-4 24900993 17259333 -30.69%
BenchmarkPostingsForMatchers/Block/n="1",i=~"1.+",j="foo"-4 3788311 3142630 -17.04%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!="2",j="foo"-4 24901137 17259509 -30.69%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!~"2.*",j="foo"-4 28693086 20405680 -28.88%
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
2019-12-05 18:27:40 +00:00
|
|
|
func TestPostingsMany(t *testing.T) {
|
|
|
|
dir, err := ioutil.TempDir("", "test_postings_many")
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
defer func() {
|
|
|
|
testutil.Ok(t, os.RemoveAll(dir))
|
|
|
|
}()
|
|
|
|
|
|
|
|
fn := filepath.Join(dir, indexFilename)
|
|
|
|
|
|
|
|
iw, err := NewWriter(fn)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
|
|
|
|
// Create a label in the index which has 999 values.
|
|
|
|
symbols := map[string]struct{}{}
|
|
|
|
series := []labels.Labels{}
|
|
|
|
for i := 1; i < 1000; i++ {
|
|
|
|
v := fmt.Sprintf("%03d", i)
|
|
|
|
series = append(series, labels.FromStrings("i", v, "foo", "bar"))
|
|
|
|
symbols[v] = struct{}{}
|
|
|
|
}
|
|
|
|
symbols["i"] = struct{}{}
|
|
|
|
symbols["foo"] = struct{}{}
|
|
|
|
symbols["bar"] = struct{}{}
|
|
|
|
testutil.Ok(t, iw.AddSymbols(symbols))
|
|
|
|
|
|
|
|
for i, s := range series {
|
|
|
|
testutil.Ok(t, iw.AddSeries(uint64(i), s))
|
|
|
|
}
|
|
|
|
for i, s := range series {
|
|
|
|
testutil.Ok(t, iw.WritePostings("i", s.Get("i"), newListPostings(uint64(i))))
|
|
|
|
}
|
|
|
|
testutil.Ok(t, iw.Close())
|
|
|
|
|
|
|
|
ir, err := NewFileReader(fn)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
in []string
|
|
|
|
}{
|
|
|
|
// Simple cases, everything is present.
|
|
|
|
{in: []string{"002"}},
|
|
|
|
{in: []string{"031", "032", "033"}},
|
|
|
|
{in: []string{"032", "033"}},
|
|
|
|
{in: []string{"127", "128"}},
|
|
|
|
{in: []string{"127", "128", "129"}},
|
|
|
|
{in: []string{"127", "129"}},
|
|
|
|
{in: []string{"128", "129"}},
|
|
|
|
{in: []string{"998", "999"}},
|
|
|
|
{in: []string{"999"}},
|
|
|
|
// Before actual values.
|
|
|
|
{in: []string{"000"}},
|
|
|
|
{in: []string{"000", "001"}},
|
|
|
|
{in: []string{"000", "002"}},
|
|
|
|
// After actual values.
|
|
|
|
{in: []string{"999a"}},
|
|
|
|
{in: []string{"999", "999a"}},
|
|
|
|
{in: []string{"998", "999", "999a"}},
|
|
|
|
// In the middle of actual values.
|
|
|
|
{in: []string{"126a", "127", "128"}},
|
|
|
|
{in: []string{"127", "127a", "128"}},
|
|
|
|
{in: []string{"127", "127a", "128", "128a", "129"}},
|
|
|
|
{in: []string{"127", "128a", "129"}},
|
|
|
|
{in: []string{"128", "128a", "129"}},
|
|
|
|
{in: []string{"128", "129", "129a"}},
|
|
|
|
{in: []string{"126a", "126b", "127", "127a", "127b", "128", "128a", "128b", "129", "129a", "129b"}},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
it, err := ir.Postings("i", c.in...)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
|
|
|
|
got := []string{}
|
|
|
|
var lbls labels.Labels
|
|
|
|
var metas []chunks.Meta
|
|
|
|
for it.Next() {
|
|
|
|
testutil.Ok(t, ir.Series(it.At(), &lbls, &metas))
|
|
|
|
got = append(got, lbls.Get("i"))
|
|
|
|
}
|
|
|
|
testutil.Ok(t, it.Err())
|
|
|
|
exp := []string{}
|
|
|
|
for _, e := range c.in {
|
|
|
|
if _, ok := symbols[e]; ok && e != "l" {
|
|
|
|
exp = append(exp, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
testutil.Equals(t, exp, got, fmt.Sprintf("input: %v", c.in))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-03-14 14:24:08 +00:00
|
|
|
func TestPersistence_index_e2e(t *testing.T) {
|
|
|
|
dir, err := ioutil.TempDir("", "test_persistence_e2e")
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2019-03-19 13:31:57 +00:00
|
|
|
defer func() {
|
|
|
|
testutil.Ok(t, os.RemoveAll(dir))
|
|
|
|
}()
|
2017-03-14 14:24:08 +00:00
|
|
|
|
2018-10-25 09:32:57 +00:00
|
|
|
lbls, err := labels.ReadLabels(filepath.Join("..", "testdata", "20kseries.json"), 20000)
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2017-03-14 14:24:08 +00:00
|
|
|
|
2017-08-05 11:31:48 +00:00
|
|
|
// Sort labels as the index writer expects series in sorted order.
|
|
|
|
sort.Sort(labels.Slice(lbls))
|
|
|
|
|
|
|
|
symbols := map[string]struct{}{}
|
|
|
|
for _, lset := range lbls {
|
|
|
|
for _, l := range lset {
|
|
|
|
symbols[l.Name] = struct{}{}
|
|
|
|
symbols[l.Value] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-14 14:24:08 +00:00
|
|
|
var input indexWriterSeriesSlice
|
|
|
|
|
|
|
|
// Generate ChunkMetas for every label set.
|
|
|
|
for i, lset := range lbls {
|
2017-11-30 14:34:49 +00:00
|
|
|
var metas []chunks.Meta
|
2017-03-14 14:24:08 +00:00
|
|
|
|
|
|
|
for j := 0; j <= (i % 20); j++ {
|
2017-11-30 14:34:49 +00:00
|
|
|
metas = append(metas, chunks.Meta{
|
2017-03-14 14:24:08 +00:00
|
|
|
MinTime: int64(j * 10000),
|
|
|
|
MaxTime: int64((j + 1) * 10000),
|
|
|
|
Ref: rand.Uint64(),
|
2017-11-30 14:34:49 +00:00
|
|
|
Chunk: chunkenc.NewXORChunk(),
|
2017-03-14 14:24:08 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
input = append(input, &indexWriterSeries{
|
|
|
|
labels: lset,
|
|
|
|
chunks: metas,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-01-29 08:32:32 +00:00
|
|
|
iw, err := NewWriter(filepath.Join(dir, indexFilename))
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2017-03-14 14:24:08 +00:00
|
|
|
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, iw.AddSymbols(symbols))
|
2017-08-05 11:31:48 +00:00
|
|
|
|
2017-03-14 14:24:08 +00:00
|
|
|
// Population procedure as done by compaction.
|
|
|
|
var (
|
2017-11-30 14:34:49 +00:00
|
|
|
postings = NewMemPostings()
|
|
|
|
values = map[string]map[string]struct{}{}
|
2017-03-14 14:24:08 +00:00
|
|
|
)
|
|
|
|
|
2017-03-29 23:18:41 +00:00
|
|
|
mi := newMockIndex()
|
|
|
|
|
2017-03-14 14:24:08 +00:00
|
|
|
for i, s := range input {
|
2017-09-04 14:08:38 +00:00
|
|
|
err = iw.AddSeries(uint64(i), s.labels, s.chunks...)
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2019-04-25 10:07:04 +00:00
|
|
|
testutil.Ok(t, mi.AddSeries(uint64(i), s.labels, s.chunks...))
|
2017-03-14 14:24:08 +00:00
|
|
|
|
|
|
|
for _, l := range s.labels {
|
|
|
|
valset, ok := values[l.Name]
|
|
|
|
if !ok {
|
2017-11-30 14:34:49 +00:00
|
|
|
valset = map[string]struct{}{}
|
2017-03-14 14:24:08 +00:00
|
|
|
values[l.Name] = valset
|
|
|
|
}
|
2017-11-30 14:34:49 +00:00
|
|
|
valset[l.Value] = struct{}{}
|
2017-03-14 14:24:08 +00:00
|
|
|
}
|
2017-11-30 14:34:49 +00:00
|
|
|
postings.Add(uint64(i), s.labels)
|
2017-03-14 14:24:08 +00:00
|
|
|
}
|
2017-04-08 15:42:04 +00:00
|
|
|
|
|
|
|
for k, v := range values {
|
2017-12-21 10:55:58 +00:00
|
|
|
var vals []string
|
|
|
|
for e := range v {
|
|
|
|
vals = append(vals, e)
|
|
|
|
}
|
|
|
|
sort.Strings(vals)
|
2017-04-08 15:42:04 +00:00
|
|
|
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, iw.WriteLabelIndex([]string{k}, vals))
|
|
|
|
testutil.Ok(t, mi.WriteLabelIndex([]string{k}, vals))
|
2017-04-08 15:42:04 +00:00
|
|
|
}
|
|
|
|
|
2017-09-04 14:08:38 +00:00
|
|
|
all := make([]uint64, len(lbls))
|
2017-03-14 14:24:08 +00:00
|
|
|
for i := range all {
|
2017-09-04 14:08:38 +00:00
|
|
|
all[i] = uint64(i)
|
2017-03-14 14:24:08 +00:00
|
|
|
}
|
2019-03-21 16:23:00 +00:00
|
|
|
err = iw.WritePostings("", "", newListPostings(all...))
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2019-04-25 10:07:04 +00:00
|
|
|
testutil.Ok(t, mi.WritePostings("", "", newListPostings(all...)))
|
2017-03-29 23:18:41 +00:00
|
|
|
|
Reduce memory used by postings offset table.
Rather than keeping the offset of each postings list, instead
keep the nth offset of the offset of the posting list. As postings
list offsets have always been sorted, we can then get to the closest
entry before the one we want an iterate forwards.
I haven't done much tuning on the 32 number, it was chosen to try
not to read through more than a 4k page of data.
Switch to a bulk interface for fetching postings. Use it to avoid having
to re-read parts of the posting offset table when querying lots of it.
For a index with what BenchmarkHeadPostingForMatchers uses RAM
for r.postings drops from 3.79MB to 80.19kB or about 48x.
Bytes allocated go down by 30%, and suprisingly CPU usage drops by
4-6% for typical queries too.
benchmark old ns/op new ns/op delta
BenchmarkPostingsForMatchers/Block/n="1"-4 35231 36673 +4.09%
BenchmarkPostingsForMatchers/Block/n="1",j="foo"-4 563380 540627 -4.04%
BenchmarkPostingsForMatchers/Block/j="foo",n="1"-4 536782 534186 -0.48%
BenchmarkPostingsForMatchers/Block/n="1",j!="foo"-4 533990 541550 +1.42%
BenchmarkPostingsForMatchers/Block/i=~".*"-4 113374598 117969608 +4.05%
BenchmarkPostingsForMatchers/Block/i=~".+"-4 146329884 139651442 -4.56%
BenchmarkPostingsForMatchers/Block/i=~""-4 50346510 44961127 -10.70%
BenchmarkPostingsForMatchers/Block/i!=""-4 41261550 35356165 -14.31%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",j="foo"-4 112544418 116904010 +3.87%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",i!="2",j="foo"-4 112487086 116864918 +3.89%
BenchmarkPostingsForMatchers/Block/n="1",i!=""-4 41094758 35457904 -13.72%
BenchmarkPostingsForMatchers/Block/n="1",i!="",j="foo"-4 41906372 36151473 -13.73%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",j="foo"-4 147262414 140424800 -4.64%
BenchmarkPostingsForMatchers/Block/n="1",i=~"1.+",j="foo"-4 28615629 27872072 -2.60%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!="2",j="foo"-4 147117177 140462403 -4.52%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!~"2.*",j="foo"-4 175096826 167902298 -4.11%
benchmark old allocs new allocs delta
BenchmarkPostingsForMatchers/Block/n="1"-4 4 6 +50.00%
BenchmarkPostingsForMatchers/Block/n="1",j="foo"-4 7 11 +57.14%
BenchmarkPostingsForMatchers/Block/j="foo",n="1"-4 7 11 +57.14%
BenchmarkPostingsForMatchers/Block/n="1",j!="foo"-4 15 17 +13.33%
BenchmarkPostingsForMatchers/Block/i=~".*"-4 100010 100012 +0.00%
BenchmarkPostingsForMatchers/Block/i=~".+"-4 200069 200040 -0.01%
BenchmarkPostingsForMatchers/Block/i=~""-4 200072 200045 -0.01%
BenchmarkPostingsForMatchers/Block/i!=""-4 200070 200041 -0.01%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",j="foo"-4 100013 100017 +0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",i!="2",j="foo"-4 100017 100023 +0.01%
BenchmarkPostingsForMatchers/Block/n="1",i!=""-4 200073 200046 -0.01%
BenchmarkPostingsForMatchers/Block/n="1",i!="",j="foo"-4 200075 200050 -0.01%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",j="foo"-4 200074 200049 -0.01%
BenchmarkPostingsForMatchers/Block/n="1",i=~"1.+",j="foo"-4 111165 111150 -0.01%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!="2",j="foo"-4 200078 200055 -0.01%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!~"2.*",j="foo"-4 311282 311238 -0.01%
benchmark old bytes new bytes delta
BenchmarkPostingsForMatchers/Block/n="1"-4 264 296 +12.12%
BenchmarkPostingsForMatchers/Block/n="1",j="foo"-4 360 424 +17.78%
BenchmarkPostingsForMatchers/Block/j="foo",n="1"-4 360 424 +17.78%
BenchmarkPostingsForMatchers/Block/n="1",j!="foo"-4 520 552 +6.15%
BenchmarkPostingsForMatchers/Block/i=~".*"-4 1600461 1600482 +0.00%
BenchmarkPostingsForMatchers/Block/i=~".+"-4 24900801 17259077 -30.69%
BenchmarkPostingsForMatchers/Block/i=~""-4 24900836 17259151 -30.69%
BenchmarkPostingsForMatchers/Block/i!=""-4 24900760 17259048 -30.69%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",j="foo"-4 1600557 1600621 +0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",i!="2",j="foo"-4 1600717 1600813 +0.01%
BenchmarkPostingsForMatchers/Block/n="1",i!=""-4 24900856 17259176 -30.69%
BenchmarkPostingsForMatchers/Block/n="1",i!="",j="foo"-4 24900952 17259304 -30.69%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",j="foo"-4 24900993 17259333 -30.69%
BenchmarkPostingsForMatchers/Block/n="1",i=~"1.+",j="foo"-4 3788311 3142630 -17.04%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!="2",j="foo"-4 24901137 17259509 -30.69%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!~"2.*",j="foo"-4 28693086 20405680 -28.88%
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
2019-12-05 18:27:40 +00:00
|
|
|
for _, l := range postings.SortedKeys() {
|
|
|
|
err := iw.WritePostings(l.Name, l.Value, postings.Get(l.Name, l.Value))
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
mi.WritePostings(l.Name, l.Value, postings.Get(l.Name, l.Value))
|
2017-03-29 23:18:41 +00:00
|
|
|
}
|
2017-03-14 14:24:08 +00:00
|
|
|
|
|
|
|
err = iw.Close()
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2017-03-14 14:24:08 +00:00
|
|
|
|
2019-01-29 08:32:32 +00:00
|
|
|
ir, err := NewFileReader(filepath.Join(dir, indexFilename))
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2017-03-14 14:24:08 +00:00
|
|
|
|
2017-11-30 14:34:49 +00:00
|
|
|
for p := range mi.postings {
|
2017-09-05 09:45:18 +00:00
|
|
|
gotp, err := ir.Postings(p.Name, p.Value)
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2017-03-14 14:24:08 +00:00
|
|
|
|
2017-09-05 09:45:18 +00:00
|
|
|
expp, err := mi.Postings(p.Name, p.Value)
|
2019-01-02 16:48:42 +00:00
|
|
|
testutil.Ok(t, err)
|
2017-08-05 11:31:48 +00:00
|
|
|
|
|
|
|
var lset, explset labels.Labels
|
2017-11-30 14:34:49 +00:00
|
|
|
var chks, expchks []chunks.Meta
|
2017-03-14 14:24:08 +00:00
|
|
|
|
2017-03-29 23:18:41 +00:00
|
|
|
for gotp.Next() {
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Assert(t, expp.Next() == true, "")
|
2017-03-14 14:24:08 +00:00
|
|
|
|
2017-03-29 23:18:41 +00:00
|
|
|
ref := gotp.At()
|
2017-03-14 14:24:08 +00:00
|
|
|
|
2017-08-05 11:31:48 +00:00
|
|
|
err := ir.Series(ref, &lset, &chks)
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2017-03-14 14:24:08 +00:00
|
|
|
|
2017-08-05 11:31:48 +00:00
|
|
|
err = mi.Series(expp.At(), &explset, &expchks)
|
2019-01-02 16:48:42 +00:00
|
|
|
testutil.Ok(t, err)
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Equals(t, explset, lset)
|
|
|
|
testutil.Equals(t, expchks, chks)
|
2017-03-29 23:18:41 +00:00
|
|
|
}
|
Reduce memory used by postings offset table.
Rather than keeping the offset of each postings list, instead
keep the nth offset of the offset of the posting list. As postings
list offsets have always been sorted, we can then get to the closest
entry before the one we want an iterate forwards.
I haven't done much tuning on the 32 number, it was chosen to try
not to read through more than a 4k page of data.
Switch to a bulk interface for fetching postings. Use it to avoid having
to re-read parts of the posting offset table when querying lots of it.
For a index with what BenchmarkHeadPostingForMatchers uses RAM
for r.postings drops from 3.79MB to 80.19kB or about 48x.
Bytes allocated go down by 30%, and suprisingly CPU usage drops by
4-6% for typical queries too.
benchmark old ns/op new ns/op delta
BenchmarkPostingsForMatchers/Block/n="1"-4 35231 36673 +4.09%
BenchmarkPostingsForMatchers/Block/n="1",j="foo"-4 563380 540627 -4.04%
BenchmarkPostingsForMatchers/Block/j="foo",n="1"-4 536782 534186 -0.48%
BenchmarkPostingsForMatchers/Block/n="1",j!="foo"-4 533990 541550 +1.42%
BenchmarkPostingsForMatchers/Block/i=~".*"-4 113374598 117969608 +4.05%
BenchmarkPostingsForMatchers/Block/i=~".+"-4 146329884 139651442 -4.56%
BenchmarkPostingsForMatchers/Block/i=~""-4 50346510 44961127 -10.70%
BenchmarkPostingsForMatchers/Block/i!=""-4 41261550 35356165 -14.31%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",j="foo"-4 112544418 116904010 +3.87%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",i!="2",j="foo"-4 112487086 116864918 +3.89%
BenchmarkPostingsForMatchers/Block/n="1",i!=""-4 41094758 35457904 -13.72%
BenchmarkPostingsForMatchers/Block/n="1",i!="",j="foo"-4 41906372 36151473 -13.73%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",j="foo"-4 147262414 140424800 -4.64%
BenchmarkPostingsForMatchers/Block/n="1",i=~"1.+",j="foo"-4 28615629 27872072 -2.60%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!="2",j="foo"-4 147117177 140462403 -4.52%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!~"2.*",j="foo"-4 175096826 167902298 -4.11%
benchmark old allocs new allocs delta
BenchmarkPostingsForMatchers/Block/n="1"-4 4 6 +50.00%
BenchmarkPostingsForMatchers/Block/n="1",j="foo"-4 7 11 +57.14%
BenchmarkPostingsForMatchers/Block/j="foo",n="1"-4 7 11 +57.14%
BenchmarkPostingsForMatchers/Block/n="1",j!="foo"-4 15 17 +13.33%
BenchmarkPostingsForMatchers/Block/i=~".*"-4 100010 100012 +0.00%
BenchmarkPostingsForMatchers/Block/i=~".+"-4 200069 200040 -0.01%
BenchmarkPostingsForMatchers/Block/i=~""-4 200072 200045 -0.01%
BenchmarkPostingsForMatchers/Block/i!=""-4 200070 200041 -0.01%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",j="foo"-4 100013 100017 +0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",i!="2",j="foo"-4 100017 100023 +0.01%
BenchmarkPostingsForMatchers/Block/n="1",i!=""-4 200073 200046 -0.01%
BenchmarkPostingsForMatchers/Block/n="1",i!="",j="foo"-4 200075 200050 -0.01%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",j="foo"-4 200074 200049 -0.01%
BenchmarkPostingsForMatchers/Block/n="1",i=~"1.+",j="foo"-4 111165 111150 -0.01%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!="2",j="foo"-4 200078 200055 -0.01%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!~"2.*",j="foo"-4 311282 311238 -0.01%
benchmark old bytes new bytes delta
BenchmarkPostingsForMatchers/Block/n="1"-4 264 296 +12.12%
BenchmarkPostingsForMatchers/Block/n="1",j="foo"-4 360 424 +17.78%
BenchmarkPostingsForMatchers/Block/j="foo",n="1"-4 360 424 +17.78%
BenchmarkPostingsForMatchers/Block/n="1",j!="foo"-4 520 552 +6.15%
BenchmarkPostingsForMatchers/Block/i=~".*"-4 1600461 1600482 +0.00%
BenchmarkPostingsForMatchers/Block/i=~".+"-4 24900801 17259077 -30.69%
BenchmarkPostingsForMatchers/Block/i=~""-4 24900836 17259151 -30.69%
BenchmarkPostingsForMatchers/Block/i!=""-4 24900760 17259048 -30.69%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",j="foo"-4 1600557 1600621 +0.00%
BenchmarkPostingsForMatchers/Block/n="1",i=~".*",i!="2",j="foo"-4 1600717 1600813 +0.01%
BenchmarkPostingsForMatchers/Block/n="1",i!=""-4 24900856 17259176 -30.69%
BenchmarkPostingsForMatchers/Block/n="1",i!="",j="foo"-4 24900952 17259304 -30.69%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",j="foo"-4 24900993 17259333 -30.69%
BenchmarkPostingsForMatchers/Block/n="1",i=~"1.+",j="foo"-4 3788311 3142630 -17.04%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!="2",j="foo"-4 24901137 17259509 -30.69%
BenchmarkPostingsForMatchers/Block/n="1",i=~".+",i!~"2.*",j="foo"-4 28693086 20405680 -28.88%
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
2019-12-05 18:27:40 +00:00
|
|
|
testutil.Assert(t, expp.Next() == false, "Unexpected Next() for "+p.Name+" "+p.Value)
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, gotp.Err())
|
2017-03-14 14:24:08 +00:00
|
|
|
}
|
|
|
|
|
2017-04-08 15:42:04 +00:00
|
|
|
for k, v := range mi.labelIndex {
|
2017-12-21 10:55:58 +00:00
|
|
|
tplsExp, err := NewStringTuples(v, 1)
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2017-04-08 15:42:04 +00:00
|
|
|
|
|
|
|
tplsRes, err := ir.LabelValues(k)
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2017-03-14 14:24:08 +00:00
|
|
|
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Equals(t, tplsExp.Len(), tplsRes.Len())
|
2017-04-08 15:42:04 +00:00
|
|
|
for i := 0; i < tplsExp.Len(); i++ {
|
|
|
|
strsExp, err := tplsExp.At(i)
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2017-04-08 15:42:04 +00:00
|
|
|
|
|
|
|
strsRes, err := tplsRes.At(i)
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2017-04-08 15:42:04 +00:00
|
|
|
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Equals(t, strsExp, strsRes)
|
2017-04-08 15:42:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-11 17:31:26 +00:00
|
|
|
gotSymbols, err := ir.Symbols()
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
|
|
|
|
testutil.Equals(t, len(mi.symbols), len(gotSymbols))
|
|
|
|
for s := range mi.symbols {
|
|
|
|
_, ok := gotSymbols[s]
|
|
|
|
testutil.Assert(t, ok, "")
|
|
|
|
}
|
|
|
|
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, ir.Close())
|
2017-03-14 14:24:08 +00:00
|
|
|
}
|
2018-06-25 09:25:22 +00:00
|
|
|
|
2019-01-11 17:31:26 +00:00
|
|
|
func TestDecbufUvariantWithInvalidBuffer(t *testing.T) {
|
2018-06-25 09:25:22 +00:00
|
|
|
b := realByteSlice([]byte{0x81, 0x81, 0x81, 0x81, 0x81, 0x81})
|
|
|
|
|
2019-02-22 17:11:11 +00:00
|
|
|
db := encoding.NewDecbufUvarintAt(b, 0, castagnoliTable)
|
|
|
|
testutil.NotOk(t, db.Err())
|
2018-06-25 09:25:22 +00:00
|
|
|
}
|
2019-01-11 17:31:26 +00:00
|
|
|
|
|
|
|
func TestReaderWithInvalidBuffer(t *testing.T) {
|
|
|
|
b := realByteSlice([]byte{0x81, 0x81, 0x81, 0x81, 0x81, 0x81})
|
|
|
|
|
|
|
|
_, err := NewReader(b)
|
|
|
|
testutil.NotOk(t, err)
|
|
|
|
}
|
2019-04-03 08:34:04 +00:00
|
|
|
|
|
|
|
// TestNewFileReaderErrorNoOpenFiles ensures that in case of an error no file remains open.
|
|
|
|
func TestNewFileReaderErrorNoOpenFiles(t *testing.T) {
|
|
|
|
dir := testutil.NewTemporaryDirectory("block", t)
|
|
|
|
|
|
|
|
idxName := filepath.Join(dir.Path(), "index")
|
|
|
|
err := ioutil.WriteFile(idxName, []byte("corrupted contents"), 0644)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
|
|
|
|
_, err = NewFileReader(idxName)
|
|
|
|
testutil.NotOk(t, err)
|
|
|
|
|
|
|
|
// dir.Close will fail on Win if idxName fd is not closed on error path.
|
|
|
|
dir.Close()
|
|
|
|
}
|