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: bump badger to v4 #14427

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
699 changes: 626 additions & 73 deletions NOTICE.txt

Large diffs are not rendered by default.

13 changes: 6 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.23.0

require (
github.com/cespare/xxhash/v2 v2.3.0
github.com/dgraph-io/badger/v2 v2.2007.4
github.com/dgraph-io/badger/v4 v4.3.1
github.com/dustin/go-humanize v1.0.1
github.com/elastic/apm-aggregation v1.1.0
github.com/elastic/apm-data v1.13.1
Expand Down Expand Up @@ -60,13 +60,11 @@ require (
require (
github.com/DataDog/zstd v1.4.5 // indirect
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/OneOfOne/xxhash v1.2.8 // indirect
github.com/Shopify/sarama v1.38.1 // indirect
github.com/apache/thrift v0.20.0 // indirect
github.com/armon/go-radix v1.0.0 // indirect
github.com/axiomhq/hyperloglog v0.2.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash v1.1.0 // indirect
github.com/cockroachdb/errors v1.11.3 // indirect
github.com/cockroachdb/fifo v0.0.0-20240606204812-0bbfbd93a7ce // indirect
github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b // indirect
Expand All @@ -75,8 +73,7 @@ require (
github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 // indirect
github.com/containerd/errdefs v0.1.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dgraph-io/ristretto v0.1.1 // indirect
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 // indirect
github.com/dgraph-io/ristretto v1.0.0 // indirect
github.com/dgryski/go-metro v0.0.0-20180109044635-280f6062b5bc // indirect
github.com/dlclark/regexp2 v1.8.1 // indirect
github.com/docker/distribution v2.8.3+incompatible // indirect
Expand All @@ -100,10 +97,11 @@ require (
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/gofrs/uuid/v5 v5.2.0 // indirect
github.com/gogo/googleapis v1.4.1 // indirect
github.com/golang/glog v1.2.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/gomodule/redigo v1.8.9 // indirect
github.com/google/flatbuffers v24.3.25+incompatible // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/h2non/filetype v1.1.3 // indirect
github.com/hashicorp/go-uuid v1.0.3 // indirect
Expand All @@ -116,7 +114,7 @@ require (
github.com/jcmturner/rpc/v2 v2.0.3 // indirect
github.com/joeshaw/multierror v0.0.0-20140124173710-69b34d4ec901 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/klauspost/compress v1.17.10 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/lufia/plan9stats v0.0.0-20220913051719-115f729f3c8c // indirect
Expand Down Expand Up @@ -149,6 +147,7 @@ require (
github.com/yusufpapurcu/wmi v1.2.4 // indirect
go.elastic.co/apm/module/apmzap/v2 v2.6.0 // indirect
go.elastic.co/ecszap v1.0.2 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/collector/semconv v0.109.0 // indirect
go.opentelemetry.io/otel/sdk v1.30.0 // indirect
go.opentelemetry.io/otel/trace v1.30.0 // indirect
Expand Down
122 changes: 75 additions & 47 deletions go.sum

Large diffs are not rendered by default.

24 changes: 22 additions & 2 deletions x-pack/apm-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (
"fmt"
"os"
"sync"
"time"

"github.com/dgraph-io/badger/v2"
"github.com/dgraph-io/badger/v4"
"github.com/gofrs/uuid"
"golang.org/x/sync/errgroup"

Expand All @@ -33,7 +34,8 @@ import (
)

const (
tailSamplingStorageDir = "tail_sampling"
oldTailSamplingStorageDir = "tail_sampling"
tailSamplingStorageDir = "tail_sampling_v4"
)

var (
Expand Down Expand Up @@ -117,6 +119,24 @@ func newTailSamplingProcessor(args beater.ServerParams) (*sampling.Processor, er
return nil, fmt.Errorf("failed to create Elasticsearch client for tail-sampling: %w", err)
}

oldStorageDir := paths.Resolve(paths.Data, oldTailSamplingStorageDir)
if entries, err := os.ReadDir(oldStorageDir); err == nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to log the error if the syscall to open a directory has failed here? i.e. specifically checking for err != nil && err != io.EOF

var newest time.Time
for _, de := range entries {
if i, err := de.Info(); err == nil {
if t := i.ModTime(); t.After(newest) {
newest = t
}
}
}

if newest.IsZero() || time.Since(newest) > tailSamplingConfig.TTL {
if err := os.RemoveAll(oldStorageDir); err != nil {
args.Logger.Warnf("failed to remove old tail sampling storage dir: %v", err)
}
}
}

storageDir := paths.Resolve(paths.Data, tailSamplingStorageDir)
badgerDB, err = getBadgerDB(storageDir)
if err != nil {
Expand Down
22 changes: 22 additions & 0 deletions x-pack/apm-server/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package main

import (
"context"
"os"
"testing"

"github.com/pkg/errors"
Expand Down Expand Up @@ -67,3 +68,24 @@ func TestMonitoring(t *testing.T) {
assert.NotEqual(t, monitoring.MakeFlatSnapshot(), tailSamplingMonitoringSnapshot)
}
}

func TestSamplingOldDir(t *testing.T) {
home := t.TempDir()
err := paths.InitPaths(&paths.Path{Home: home})
require.NoError(t, err)

oldDir := paths.Resolve(paths.Data, oldTailSamplingStorageDir)
os.Mkdir(oldDir, 0600)

cfg := config.DefaultConfig()
cfg.Sampling.Tail.Policies = []config.TailSamplingPolicy{{SampleRate: 0.1}}
_, err = newTailSamplingProcessor(beater.ServerParams{
Config: cfg,
NewElasticsearchClient: elasticsearch.NewClient,
BatchProcessor: modelpb.ProcessBatchFunc(func(context.Context, *modelpb.Batch) error { return nil }),
Namespace: "default",
})
require.NoError(t, err)

require.NoDirExists(t, oldDir)
}
2 changes: 1 addition & 1 deletion x-pack/apm-server/sampling/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package sampling
import (
"time"

"github.com/dgraph-io/badger/v2"
"github.com/dgraph-io/badger/v4"
"github.com/pkg/errors"

"github.com/elastic/apm-data/model/modelpb"
Expand Down
2 changes: 1 addition & 1 deletion x-pack/apm-server/sampling/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package sampling_test
import (
"testing"

"github.com/dgraph-io/badger/v2"
"github.com/dgraph-io/badger/v4"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down
5 changes: 2 additions & 3 deletions x-pack/apm-server/sampling/eventstorage/badger.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
package eventstorage

import (
"github.com/dgraph-io/badger/v2"
"github.com/dgraph-io/badger/v4"

"github.com/elastic/apm-server/internal/logs"
"github.com/elastic/elastic-agent-libs/logp"
Expand Down Expand Up @@ -34,11 +34,10 @@ func OpenBadger(storageDir string, valueLogFileSize int64) (*badger.DB, error) {
const tableLimit = 4
badgerOpts := badger.DefaultOptions(storageDir).
WithLogger(&LogpAdaptor{Logger: logger}).
WithTruncate(true). // Truncate unreadable files which cannot be read.
WithNumMemtables(tableLimit). // in-memory tables.
WithNumLevelZeroTables(tableLimit). // L0 tables.
WithNumLevelZeroTablesStall(tableLimit * 3). // Maintain the default 1-to-3 ratio before stalling.
WithMaxTableSize(int64(16 << 20)). // Max LSM table or file size.
WithBaseTableSize(int64(16 << 20)). // Max LSM table or file size.
WithValueLogFileSize(valueLogFileSize) // vlog file size.

return badger.Open(badgerOpts)
Expand Down
2 changes: 1 addition & 1 deletion x-pack/apm-server/sampling/eventstorage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"sync/atomic"
"time"

"github.com/dgraph-io/badger/v2"
"github.com/dgraph-io/badger/v4"

"github.com/elastic/apm-data/model/modelpb"
)
Expand Down
2 changes: 1 addition & 1 deletion x-pack/apm-server/sampling/eventstorage/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"testing"
"time"

"github.com/dgraph-io/badger/v2"
"github.com/dgraph-io/badger/v4"
"github.com/gofrs/uuid"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"testing"
"time"

"github.com/dgraph-io/badger/v2"
"github.com/dgraph-io/badger/v4"
"github.com/gofrs/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down
2 changes: 1 addition & 1 deletion x-pack/apm-server/sampling/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"sync/atomic"
"time"

"github.com/dgraph-io/badger/v2"
"github.com/dgraph-io/badger/v4"
"github.com/pkg/errors"
"golang.org/x/sync/errgroup"

Expand Down
8 changes: 2 additions & 6 deletions x-pack/apm-server/sampling/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -597,12 +597,6 @@ func TestStorageGC(t *testing.T) {
config.TTL = 10 * time.Millisecond
config.FlushInterval = 10 * time.Millisecond

// Create a new badger DB with smaller value log files so we can test GC.
config.DB.Close()
badgerDB, err := eventstorage.OpenBadger(config.StorageDir, 1024*1024)
require.NoError(t, err)
t.Cleanup(func() { badgerDB.Close() })
config.DB = badgerDB
config.Storage = eventstorage.
New(config.DB, eventstorage.ProtobufCodec{}).
NewShardedReadWriter()
Expand All @@ -622,6 +616,8 @@ func TestStorageGC(t *testing.T) {
Span: &modelpb.Span{
Type: "type",
Id: traceID,
// inflate the size of the span so the entry is written to the vlog
Name: strings.Repeat("foo", 1024*1024),
},
}}
err := processor.ProcessBatch(context.Background(), &batch)
Expand Down
Loading