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

feat: Add sampling store support to Badger #4834

Merged
Merged
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
7 changes: 7 additions & 0 deletions plugin/storage/badger/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
"github.com/jaegertracing/jaeger/pkg/metrics"
"github.com/jaegertracing/jaeger/plugin"
depStore "github.com/jaegertracing/jaeger/plugin/storage/badger/dependencystore"
badgerSampling "github.com/jaegertracing/jaeger/plugin/storage/badger/samplingstore"
badgerStore "github.com/jaegertracing/jaeger/plugin/storage/badger/spanstore"
"github.com/jaegertracing/jaeger/storage/dependencystore"
"github.com/jaegertracing/jaeger/storage/samplingstore"
"github.com/jaegertracing/jaeger/storage/spanstore"
)

Expand Down Expand Up @@ -170,6 +172,11 @@
return depStore.NewDependencyStore(sr), nil
}

// CreateSamplingStore implements storage.SamplingStoreFactory
func (f *Factory) CreateSamplingStore(maxBuckets int) (samplingstore.Store, error) {
return badgerSampling.NewSamplingStore(f.store), nil

Check warning on line 177 in plugin/storage/badger/factory.go

View check run for this annotation

Codecov / codecov/patch

plugin/storage/badger/factory.go#L176-L177

Added lines #L176 - L177 were not covered by tests
}

// Close Implements io.Closer and closes the underlying storage
func (f *Factory) Close() error {
close(f.maintenanceDone)
Expand Down
262 changes: 262 additions & 0 deletions plugin/storage/badger/samplingstore/storage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
// Copyright (c) 2023 The Jaeger 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.

package samplingstore

import (
"bytes"
"encoding/binary"
"encoding/json"
"fmt"
"time"

"github.com/dgraph-io/badger/v3"

"github.com/jaegertracing/jaeger/cmd/collector/app/sampling/model"
jaegermodel "github.com/jaegertracing/jaeger/model"
)

const (
throughputKeyPrefix byte = 0x08
probabilitiesKeyPrefix byte = 0x09
)

type SamplingStore struct {
store *badger.DB
}

type ProbabilitiesAndQPS struct {
Hostname string
Probabilities model.ServiceOperationProbabilities
QPS model.ServiceOperationQPS
}

func NewSamplingStore(db *badger.DB) *SamplingStore {
return &SamplingStore{
store: db,
}
}

func (s *SamplingStore) InsertThroughput(throughput []*model.Throughput) error {
startTime := jaegermodel.TimeAsEpochMicroseconds(time.Now())
entriesToStore := make([]*badger.Entry, 0)
entries, err := s.createThroughputEntry(throughput, startTime)
if err != nil {
return err
}

Check warning on line 57 in plugin/storage/badger/samplingstore/storage.go

View check run for this annotation

Codecov / codecov/patch

plugin/storage/badger/samplingstore/storage.go#L56-L57

Added lines #L56 - L57 were not covered by tests
entriesToStore = append(entriesToStore, entries)
err = s.store.Update(func(txn *badger.Txn) error {
for i := range entriesToStore {
err = txn.SetEntry(entriesToStore[i])
if err != nil {
return err
}

Check warning on line 64 in plugin/storage/badger/samplingstore/storage.go

View check run for this annotation

Codecov / codecov/patch

plugin/storage/badger/samplingstore/storage.go#L63-L64

Added lines #L63 - L64 were not covered by tests
}

return nil
})

return nil
}

func (s *SamplingStore) GetThroughput(start, end time.Time) ([]*model.Throughput, error) {
var retSlice []*model.Throughput
prefix := []byte{throughputKeyPrefix}

err := s.store.View(func(txn *badger.Txn) error {
opts := badger.DefaultIteratorOptions
it := txn.NewIterator(opts)
defer it.Close()

val := []byte{}
for it.Seek(prefix); it.ValidForPrefix(prefix); it.Next() {
item := it.Item()
k := item.Key()
startTime := k[1:9]
fmt.Printf("key=%s\n", k)
val, err := item.ValueCopy(val)
if err != nil {
return err
}

Check warning on line 91 in plugin/storage/badger/samplingstore/storage.go

View check run for this annotation

Codecov / codecov/patch

plugin/storage/badger/samplingstore/storage.go#L90-L91

Added lines #L90 - L91 were not covered by tests
t, err := initalStartTime(startTime)
if err != nil {
return err
}

Check warning on line 95 in plugin/storage/badger/samplingstore/storage.go

View check run for this annotation

Codecov / codecov/patch

plugin/storage/badger/samplingstore/storage.go#L94-L95

Added lines #L94 - L95 were not covered by tests
throughputs, err := decodeThroughtputValue(val)
if err != nil {
return err
}

Check warning on line 99 in plugin/storage/badger/samplingstore/storage.go

View check run for this annotation

Codecov / codecov/patch

plugin/storage/badger/samplingstore/storage.go#L98-L99

Added lines #L98 - L99 were not covered by tests

if t.After(start) && (t.Before(end) || t.Equal(end)) {
retSlice = append(retSlice, throughputs...)
}
}
return nil
})
if err != nil {
return nil, err
}

Check warning on line 109 in plugin/storage/badger/samplingstore/storage.go

View check run for this annotation

Codecov / codecov/patch

plugin/storage/badger/samplingstore/storage.go#L108-L109

Added lines #L108 - L109 were not covered by tests

return retSlice, nil
}

func (s *SamplingStore) InsertProbabilitiesAndQPS(hostname string,
probabilities model.ServiceOperationProbabilities,
qps model.ServiceOperationQPS,
) error {
startTime := jaegermodel.TimeAsEpochMicroseconds(time.Now())
entriesToStore := make([]*badger.Entry, 0)
entries, err := s.createProbabilitiesEntry(hostname, probabilities, qps, startTime)
if err != nil {
return err
}

Check warning on line 123 in plugin/storage/badger/samplingstore/storage.go

View check run for this annotation

Codecov / codecov/patch

plugin/storage/badger/samplingstore/storage.go#L122-L123

Added lines #L122 - L123 were not covered by tests
entriesToStore = append(entriesToStore, entries)
err = s.store.Update(func(txn *badger.Txn) error {
// Write the entries
for i := range entriesToStore {
err = txn.SetEntry(entriesToStore[i])
if err != nil {
return err
}

Check warning on line 131 in plugin/storage/badger/samplingstore/storage.go

View check run for this annotation

Codecov / codecov/patch

plugin/storage/badger/samplingstore/storage.go#L130-L131

Added lines #L130 - L131 were not covered by tests
}

return nil
})

return nil
}

// GetLatestProbabilities implements samplingstore.Reader#GetLatestProbabilities.
func (s *SamplingStore) GetLatestProbabilities() (model.ServiceOperationProbabilities, error) {
var retVal model.ServiceOperationProbabilities
var unMarshalProbabilities ProbabilitiesAndQPS
prefix := []byte{probabilitiesKeyPrefix}

err := s.store.View(func(txn *badger.Txn) error {
opts := badger.DefaultIteratorOptions
it := txn.NewIterator(opts)
defer it.Close()

val := []byte{}
for it.Seek(prefix); it.ValidForPrefix(prefix); it.Next() {
item := it.Item()
val, err := item.ValueCopy(val)
if err != nil {
return err
}

Check warning on line 157 in plugin/storage/badger/samplingstore/storage.go

View check run for this annotation

Codecov / codecov/patch

plugin/storage/badger/samplingstore/storage.go#L156-L157

Added lines #L156 - L157 were not covered by tests
unMarshalProbabilities, err = decodeProbabilitiesValue(val)
if err != nil {
return err
}

Check warning on line 161 in plugin/storage/badger/samplingstore/storage.go

View check run for this annotation

Codecov / codecov/patch

plugin/storage/badger/samplingstore/storage.go#L160-L161

Added lines #L160 - L161 were not covered by tests
retVal = unMarshalProbabilities.Probabilities
}
return nil
})
if err != nil {
return nil, err
}

Check warning on line 168 in plugin/storage/badger/samplingstore/storage.go

View check run for this annotation

Codecov / codecov/patch

plugin/storage/badger/samplingstore/storage.go#L167-L168

Added lines #L167 - L168 were not covered by tests
return retVal, nil
}

func (s *SamplingStore) createProbabilitiesEntry(hostname string, probabilities model.ServiceOperationProbabilities, qps model.ServiceOperationQPS, startTime uint64) (*badger.Entry, error) {
pK, pV, err := s.createProbabilitiesKV(hostname, probabilities, qps, startTime)
if err != nil {
return nil, err
}

Check warning on line 176 in plugin/storage/badger/samplingstore/storage.go

View check run for this annotation

Codecov / codecov/patch

plugin/storage/badger/samplingstore/storage.go#L175-L176

Added lines #L175 - L176 were not covered by tests

e := s.createBadgerEntry(pK, pV)

return e, nil
}

func (s *SamplingStore) createProbabilitiesKV(hostname string, probabilities model.ServiceOperationProbabilities, qps model.ServiceOperationQPS, startTime uint64) ([]byte, []byte, error) {
key := make([]byte, 16)
key[0] = probabilitiesKeyPrefix
pos := 1
binary.BigEndian.PutUint64(key[pos:], startTime)

var bb []byte
var err error
val := ProbabilitiesAndQPS{
Hostname: hostname,
Probabilities: probabilities,
QPS: qps,
}
bb, err = json.Marshal(val)
return key, bb, err
}

func (s *SamplingStore) createThroughputEntry(throughput []*model.Throughput, startTime uint64) (*badger.Entry, error) {
pK, pV, err := s.createThroughputKV(throughput, startTime)
if err != nil {
return nil, err
}

Check warning on line 204 in plugin/storage/badger/samplingstore/storage.go

View check run for this annotation

Codecov / codecov/patch

plugin/storage/badger/samplingstore/storage.go#L203-L204

Added lines #L203 - L204 were not covered by tests

e := s.createBadgerEntry(pK, pV)

return e, nil
}

func (s *SamplingStore) createBadgerEntry(key []byte, value []byte) *badger.Entry {
return &badger.Entry{
Key: key,
Value: value,
}
}

func (s *SamplingStore) createThroughputKV(throughput []*model.Throughput, startTime uint64) ([]byte, []byte, error) {
key := make([]byte, 16)
key[0] = throughputKeyPrefix
pos := 1
binary.BigEndian.PutUint64(key[pos:], startTime)

var bb []byte
var err error

bb, err = json.Marshal(throughput)
return key, bb, err
}

func decodeThroughtputValue(val []byte) ([]*model.Throughput, error) {
var throughput []*model.Throughput

err := json.Unmarshal(val, &throughput)
if err != nil {
return nil, err
}

Check warning on line 237 in plugin/storage/badger/samplingstore/storage.go

View check run for this annotation

Codecov / codecov/patch

plugin/storage/badger/samplingstore/storage.go#L236-L237

Added lines #L236 - L237 were not covered by tests
return throughput, err
}

func decodeProbabilitiesValue(val []byte) (ProbabilitiesAndQPS, error) {
var probabilities ProbabilitiesAndQPS

err := json.Unmarshal(val, &probabilities)
if err != nil {
return ProbabilitiesAndQPS{}, err
}
return probabilities, nil
}

func initalStartTime(timeBytes []byte) (time.Time, error) {
var usec int64

buf := bytes.NewReader(timeBytes)

if err := binary.Read(buf, binary.BigEndian, &usec); err != nil {
panic(nil)

Check warning on line 257 in plugin/storage/badger/samplingstore/storage.go

View check run for this annotation

Codecov / codecov/patch

plugin/storage/badger/samplingstore/storage.go#L257

Added line #L257 was not covered by tests
}

t := time.UnixMicro(usec)
return t, nil
}
Loading
Loading