Skip to content

Commit

Permalink
Add Truncate and ReadOnly options to Badger storage backend, closes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
burmanm committed Oct 7, 2019
1 parent fcc0adb commit 7b6d741
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
4 changes: 4 additions & 0 deletions plugin/storage/badger/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ func (f *Factory) Initialize(metricsFactory metrics.Factory, logger *zap.Logger)
opts.SyncWrites = f.Options.primary.SyncWrites
opts.Dir = f.Options.primary.KeyDirectory
opts.ValueDir = f.Options.primary.ValueDirectory

// These options make no sense with ephemeral data
opts.Truncate = f.Options.primary.Truncate
opts.ReadOnly = f.Options.primary.ReadOnly
}

store, err := badger.Open(opts)
Expand Down
24 changes: 21 additions & 3 deletions plugin/storage/badger/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,18 @@ type Options struct {

// NamespaceConfig is badger's internal configuration data
type NamespaceConfig struct {
namespace string
SpanStoreTTL time.Duration
namespace string
SpanStoreTTL time.Duration

// Defining ValueDirectory and KeyDirectory will make Ephemeral false
ValueDirectory string
KeyDirectory string
Ephemeral bool // Setting this to true will ignore ValueDirectory and KeyDirectory
SyncWrites bool
MaintenanceInterval time.Duration
MetricsUpdateInterval time.Duration
Truncate bool
ReadOnly bool
}

const (
Expand All @@ -55,6 +59,8 @@ const (
suffixSyncWrite = ".consistency"
suffixMaintenanceInterval = ".maintenance-interval"
suffixMetricsInterval = ".metrics-update-interval" // Intended only for testing purposes
suffixTruncate = ".truncate"
suffixReadOnly = ".read-only"
defaultDataDir = string(os.PathSeparator) + "data"
defaultValueDir = defaultDataDir + string(os.PathSeparator) + "values"
defaultKeysDir = defaultDataDir + string(os.PathSeparator) + "keys"
Expand Down Expand Up @@ -116,7 +122,7 @@ func addFlags(flagSet *flag.FlagSet, nsConfig *NamespaceConfig) {
flagSet.Bool(
nsConfig.namespace+suffixSyncWrite,
nsConfig.SyncWrites,
"If all writes should be synced immediately. This can impact write performance.",
"If all writes should be synced immediately to physical disk. This will impact write performance.",
)
flagSet.Duration(
nsConfig.namespace+suffixMaintenanceInterval,
Expand All @@ -128,6 +134,16 @@ func addFlags(flagSet *flag.FlagSet, nsConfig *NamespaceConfig) {
nsConfig.MetricsUpdateInterval,
"How often the badger metrics are collected by Jaeger. Format is time.Duration (https://golang.org/pkg/time/#Duration)",
)
flagSet.Bool(
nsConfig.namespace+suffixTruncate,
nsConfig.Truncate,
"If write-ahead-log should be truncated on restart. this will cause data loss.",
)
flagSet.Bool(
nsConfig.namespace+suffixReadOnly,
nsConfig.ReadOnly,
"Allows to open badger database in read only mode. Multiple instances can open same database in read-only mode. Values still in the write-ahead-log must be replayed before opening.",
)
}

// InitFromViper initializes Options with properties from viper
Expand All @@ -143,6 +159,8 @@ func initFromViper(cfg *NamespaceConfig, v *viper.Viper) {
cfg.SpanStoreTTL = v.GetDuration(cfg.namespace + suffixSpanstoreTTL)
cfg.MaintenanceInterval = v.GetDuration(cfg.namespace + suffixMaintenanceInterval)
cfg.MetricsUpdateInterval = v.GetDuration(cfg.namespace + suffixMetricsInterval)
cfg.Truncate = v.GetBool(cfg.namespace + suffixTruncate)
cfg.ReadOnly = v.GetBool(cfg.namespace + suffixReadOnly)
}

// GetPrimary returns the primary namespace configuration
Expand Down
15 changes: 15 additions & 0 deletions plugin/storage/badger/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,19 @@ func TestParseOptions(t *testing.T) {
assert.Equal(t, time.Duration(168*time.Hour), opts.GetPrimary().SpanStoreTTL)
assert.Equal(t, "/var/lib/badger", opts.GetPrimary().KeyDirectory)
assert.Equal(t, "/mnt/slow/badger", opts.GetPrimary().ValueDirectory)
assert.False(t, opts.GetPrimary().ReadOnly)
assert.False(t, opts.GetPrimary().Truncate)
}

func TestTruncateAndReadOnlyOptions(t *testing.T) {
opts := NewOptions("badger")
v, command := config.Viperize(opts.AddFlags)
command.ParseFlags([]string{
"--badger.truncate=true",
"--badger.read-only=true",
})
opts.InitFromViper(v)

assert.True(t, opts.GetPrimary().ReadOnly)
assert.True(t, opts.GetPrimary().Truncate)
}

0 comments on commit 7b6d741

Please sign in to comment.