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
…1832

Signed-off-by: Michael Burman <yak@iki.fi>
  • Loading branch information
burmanm committed Oct 7, 2019
1 parent fcc0adb commit ab94146
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
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
18 changes: 17 additions & 1 deletion plugin/storage/badger/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ type NamespaceConfig struct {
SyncWrites bool
MaintenanceInterval time.Duration
MetricsUpdateInterval time.Duration
Truncate bool
ReadOnly bool
}

const (
Expand All @@ -55,6 +57,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 +120,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 +132,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 +157,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 ab94146

Please sign in to comment.