This repository has been archived by the owner on Sep 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
140 lines (123 loc) · 4.49 KB
/
options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright (c) 2018 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 main
import (
"flag"
"os"
"path/filepath"
"time"
"github.com/spf13/viper"
)
// Options store storage plugin related configs
type Options struct {
primary *NamespaceConfig
// This storage plugin does not support additional namespaces
}
// NamespaceConfig is badger's internal configuration data
type NamespaceConfig struct {
namespace string
SpanStoreTTL time.Duration
ValueDirectory string
KeyDirectory string
Ephemeral bool // Setting this to true will ignore ValueDirectory and KeyDirectory
SyncWrites bool
MaintenanceInterval time.Duration
}
const (
defaultMaintenanceInterval time.Duration = 5 * time.Minute
defaultTTL time.Duration = time.Hour * 72
)
const (
suffixKeyDirectory = ".directory-key"
suffixValueDirectory = ".directory-value"
suffixEphemeral = ".ephemeral"
suffixSpanstoreTTL = ".span-store-ttl"
suffixSyncWrite = ".consistency"
suffixMaintenanceInterval = ".maintenance-interval"
defaultValueDir = "/data/values"
defaultKeysDir = "/data/keys"
)
// NewOptions creates a new Options struct.
func NewOptions(primaryNamespace string, otherNamespaces ...string) *Options {
defaultDataDir := getCurrentExecutableDir()
options := &Options{
primary: &NamespaceConfig{
namespace: primaryNamespace,
SpanStoreTTL: defaultTTL,
SyncWrites: false, // Performance over durability
Ephemeral: true, // Default is ephemeral storage
ValueDirectory: defaultDataDir + defaultValueDir,
KeyDirectory: defaultDataDir + defaultKeysDir,
MaintenanceInterval: defaultMaintenanceInterval,
},
}
return options
}
func getCurrentExecutableDir() string {
// We ignore the error, this will fail later when trying to start the store
exec, _ := os.Executable()
return filepath.Dir(exec)
}
// AddFlags adds flags for Options
func (opt *Options) AddFlags(flagSet *flag.FlagSet) {
addFlags(flagSet, opt.primary)
}
func addFlags(flagSet *flag.FlagSet, nsConfig *NamespaceConfig) {
flagSet.Bool(
nsConfig.namespace+suffixEphemeral,
nsConfig.Ephemeral,
"Mark this storage ephemeral, data is stored in tmpfs.",
)
flagSet.Duration(
nsConfig.namespace+suffixSpanstoreTTL,
nsConfig.SpanStoreTTL,
"How long to store the data. Format is time.Duration (https://golang.org/pkg/time/#Duration)",
)
flagSet.String(
nsConfig.namespace+suffixKeyDirectory,
nsConfig.KeyDirectory,
"Path to store the keys (indexes), this directory should reside in SSD disk. Set ephemeral to false if you want to define this setting.",
)
flagSet.String(
nsConfig.namespace+suffixValueDirectory,
nsConfig.ValueDirectory,
"Path to store the values (spans). Set ephemeral to false if you want to define this setting.",
)
flagSet.Bool(
nsConfig.namespace+suffixSyncWrite,
nsConfig.SyncWrites,
"If all writes should be synced immediately. This will greatly reduce write performance.",
)
flagSet.Duration(
nsConfig.namespace+suffixMaintenanceInterval,
nsConfig.MaintenanceInterval,
"How often the maintenance thread for values is ran. Format is time.Duration (https://golang.org/pkg/time/#Duration)",
)
}
// InitFromViper initializes Options with properties from viper
func (opt *Options) InitFromViper(v *viper.Viper) {
initFromViper(opt.primary, v)
}
func initFromViper(cfg *NamespaceConfig, v *viper.Viper) {
cfg.Ephemeral = v.GetBool(cfg.namespace + suffixEphemeral)
cfg.KeyDirectory = v.GetString(cfg.namespace + suffixKeyDirectory)
cfg.ValueDirectory = v.GetString(cfg.namespace + suffixValueDirectory)
cfg.SyncWrites = v.GetBool(cfg.namespace + suffixSyncWrite)
cfg.SpanStoreTTL = v.GetDuration(cfg.namespace + suffixSpanstoreTTL)
cfg.MaintenanceInterval = v.GetDuration(cfg.namespace + suffixMaintenanceInterval)
}
// GetPrimary returns the primary namespace configuration
func (opt *Options) GetPrimary() *NamespaceConfig {
return opt.primary
}