-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a tool for converting boltdb->tsdb and benchmarking queries (#5430)
* exports NewChunkIndexIterator * starts building tsdb-bench * adds query benchmark for tsdb prototyping * lint
- Loading branch information
Showing
6 changed files
with
166 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"flag" | ||
"log" | ||
"strconv" | ||
|
||
"go.etcd.io/bbolt" | ||
"gopkg.in/yaml.v2" | ||
|
||
"github.com/grafana/loki/pkg/storage/chunk" | ||
"github.com/grafana/loki/pkg/storage/stores/shipper/compactor/retention" | ||
shipper_util "github.com/grafana/loki/pkg/storage/stores/shipper/util" | ||
"github.com/grafana/loki/pkg/storage/tsdb/index" | ||
) | ||
|
||
var ( | ||
source = flag.String("source", "", "the source boltdb file") | ||
dest = flag.String("dest", "", "the dest tsdb file") | ||
// Hardcode a periodconfig for convenience as the boltdb iterator needs one | ||
// NB: must match the index file you're reading from | ||
periodConfig = func() chunk.PeriodConfig { | ||
input := ` | ||
from: "2022-01-01" | ||
index: | ||
period: 24h | ||
prefix: loki_index_ | ||
object_store: gcs | ||
schema: v13 | ||
store: boltdb-shipper | ||
` | ||
var cfg chunk.PeriodConfig | ||
if err := yaml.Unmarshal([]byte(input), &cfg); err != nil { | ||
panic(err) | ||
} | ||
return cfg | ||
}() | ||
) | ||
|
||
func extractChecksumFromChunkID(b []byte) uint32 { | ||
i := bytes.LastIndexByte(b, ':') | ||
x, err := strconv.ParseUint(string(b[i+1:]), 16, 32) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return uint32(x) | ||
} | ||
|
||
func main() { | ||
flag.Parse() | ||
|
||
if source == nil || *source == "" { | ||
panic("source is required") | ||
} | ||
|
||
if dest == nil || *dest == "" { | ||
panic("dest is required") | ||
} | ||
|
||
db, err := shipper_util.SafeOpenBoltdbFile(*source) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
builder := index.NewBuilder() | ||
|
||
log.Println("Loading index into memory") | ||
|
||
// loads everything into memory. | ||
if err := db.View(func(t *bbolt.Tx) error { | ||
it, err := retention.NewChunkIndexIterator(t.Bucket([]byte("index")), periodConfig) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for it.Next() { | ||
if it.Err() != nil { | ||
return it.Err() | ||
} | ||
entry := it.Entry() | ||
builder.AddSeries(entry.Labels, []index.ChunkMeta{{ | ||
Checksum: extractChecksumFromChunkID(entry.ChunkID), | ||
MinTime: int64(entry.From), | ||
MaxTime: int64(entry.Through), | ||
Bytes: (3 << 20) / 4, // guess: 0.75mb, 1/2 of the max size | ||
Entries: 10000, // guess: 10k entries | ||
}}) | ||
} | ||
|
||
return nil | ||
}); err != nil { | ||
panic(err) | ||
} | ||
|
||
log.Println("writing index") | ||
if err := builder.Build(context.Background(), *dest); err != nil { | ||
panic(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"os" | ||
"testing" | ||
|
||
"github.com/prometheus/prometheus/model/labels" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/grafana/loki/pkg/storage/tsdb" | ||
"github.com/grafana/loki/pkg/storage/tsdb/index" | ||
) | ||
|
||
func TestExtractChecksum(t *testing.T) { | ||
x := rand.Uint32() | ||
s := fmt.Sprintf("a/b/c:d:e:%x", x) | ||
require.Equal(t, x, extractChecksumFromChunkID([]byte(s))) | ||
} | ||
|
||
// Requires LOKI_TSDB_PATH to be set or this will short-circuit | ||
func BenchmarkQuery(b *testing.B) { | ||
for _, bm := range []struct { | ||
name string | ||
matchers []*labels.Matcher | ||
}{ | ||
{ | ||
name: "match ns", | ||
matchers: []*labels.Matcher{labels.MustNewMatcher(labels.MatchEqual, "namespace", "loki-ops")}, | ||
}, | ||
{ | ||
name: "match ns regexp", | ||
matchers: []*labels.Matcher{labels.MustNewMatcher(labels.MatchEqual, "namespace", "loki-ops")}, | ||
}, | ||
} { | ||
indexPath := os.Getenv("LOKI_TSDB_PATH") | ||
if indexPath == "" { | ||
return | ||
} | ||
|
||
reader, err := index.NewFileReader(indexPath) | ||
if err != nil { | ||
panic(err) | ||
} | ||
b.Run(bm.name, func(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
p, _ := tsdb.PostingsForMatchers(reader, bm.matchers...) | ||
|
||
for p.Next() { | ||
} | ||
} | ||
}) | ||
} | ||
} |