From c137280718f4f4f288a01fe4c3b2f5159e4816f5 Mon Sep 17 00:00:00 2001 From: Michael Burman Date: Thu, 4 Jul 2019 17:38:41 +0300 Subject: [PATCH] Create directory paths automatically and make default dirs work in Windows also, fixes issue #1652 Signed-off-by: Michael Burman --- plugin/storage/badger/factory.go | 11 +++++++++++ plugin/storage/badger/options.go | 11 ++++++----- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/plugin/storage/badger/factory.go b/plugin/storage/badger/factory.go index 4204a4d15e4..758a94404af 100644 --- a/plugin/storage/badger/factory.go +++ b/plugin/storage/badger/factory.go @@ -96,6 +96,10 @@ func (f *Factory) Initialize(metricsFactory metrics.Factory, logger *zap.Logger) f.Options.primary.KeyDirectory = f.tmpDir f.Options.primary.ValueDirectory = f.tmpDir } else { + // Errors are ignored as they're catched in the Open call + initializeDir(f.Options.primary.KeyDirectory) + initializeDir(f.Options.primary.ValueDirectory) + opts.SyncWrites = f.Options.primary.SyncWrites opts.Dir = f.Options.primary.KeyDirectory opts.ValueDir = f.Options.primary.ValueDirectory @@ -121,6 +125,13 @@ func (f *Factory) Initialize(metricsFactory metrics.Factory, logger *zap.Logger) return nil } +// initializeDir makes the directory and parent directories if the path doesn't exists yet. +func initializeDir(path string) { + if _, err := os.Stat(path); err != nil && os.IsNotExist(err) { + os.MkdirAll(path, 0700) + } +} + // CreateSpanReader implements storage.Factory func (f *Factory) CreateSpanReader() (spanstore.Reader, error) { return badgerStore.NewTraceReader(f.store, f.cache), nil diff --git a/plugin/storage/badger/options.go b/plugin/storage/badger/options.go index 66c53f7b364..db3c957ca36 100644 --- a/plugin/storage/badger/options.go +++ b/plugin/storage/badger/options.go @@ -52,14 +52,15 @@ const ( suffixSpanstoreTTL = ".span-store-ttl" suffixSyncWrite = ".consistency" suffixMaintenanceInterval = ".maintenance-interval" - defaultValueDir = "/data/values" - defaultKeysDir = "/data/keys" + defaultDataDir = string(os.PathSeparator) + "data" + defaultValueDir = defaultDataDir + string(os.PathSeparator) + "values" + defaultKeysDir = defaultDataDir + string(os.PathSeparator) + "keys" ) // NewOptions creates a new Options struct. func NewOptions(primaryNamespace string, otherNamespaces ...string) *Options { - defaultDataDir := getCurrentExecutableDir() + defaultBadgerDataDir := getCurrentExecutableDir() options := &Options{ primary: &NamespaceConfig{ @@ -67,8 +68,8 @@ func NewOptions(primaryNamespace string, otherNamespaces ...string) *Options { SpanStoreTTL: defaultTTL, SyncWrites: false, // Performance over durability Ephemeral: true, // Default is ephemeral storage - ValueDirectory: defaultDataDir + defaultValueDir, - KeyDirectory: defaultDataDir + defaultKeysDir, + ValueDirectory: defaultBadgerDataDir + defaultValueDir, + KeyDirectory: defaultBadgerDataDir + defaultKeysDir, MaintenanceInterval: defaultMaintenanceInterval, }, }