-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfig.go
348 lines (292 loc) · 9.82 KB
/
config.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package raft
import (
"errors"
"fmt"
"net/url"
"os"
"os/user"
"path/filepath"
"reflect"
"strings"
"time"
"github.com/bbengfort/x/peers"
"github.com/fatih/structs"
"github.com/koding/multiconfig"
"github.com/rs/zerolog"
)
// Config uses the multiconfig loader and validators to store configuration
// values required to run Raft. Configuration can be stored as a JSON, TOML,
// or YAML file in the current working directory as raft.json, in the user's
// home directory as .raft.json or in /etc/raft.json (with the extension of
// the file format of choice). Configuration can also be added from the
// environment using environment variables prefixed with $RAFT_ and the all
// caps version of the configuration name.
type Config struct {
Name string `required:"false" json:"name,omitempty"` // unique name of the local replica, hostname by default
Seed int64 `required:"false" json:"seed,omitempty"` // random seed to initialize random generator
Tick string `default:"1s" validate:"duration" json:"tick"` // clock tick rate for timing (parseable duration)
Timeout string `default:"500ms" validate:"duration" json:"timeout"` // timeout to wait for responses (parseable duration)
Aggregate bool `default:"true" json:"aggregate"` // aggregate append entries from multiple concurrent clients
LogLevel string `default:"info" validate:"zerolog" json:"log_level"` // verbosity of logging, sets the zerolog log level
ConsoleLog bool `default:"false" json:"console_log"` // output human readable logs instead of JSON logs
Leader string `required:"false" json:"leader,omitempty"` // designated initial leader, if any
Peers []peers.Peer `json:"peers"` // definition of all hosts on the network
// Experimental configuration
// TODO: remove after benchmarks
Uptime string `required:"false" validate:"duration" json:"uptime"` // run for a time limit and then shutdown
Metrics string `requred:"false" json:"metrics"` // location to write benchmarks to disk
}
// Load the configuration from default values, then from a configuration file,
// and finally from the environment. Validate the configuration when loaded.
func (c *Config) Load() error {
loaders := []multiconfig.Loader{}
// Read default values defined via tag fields "default"
loaders = append(loaders, &multiconfig.TagLoader{})
// Find the config path and hte appropriate file loader
if path, err := c.GetPath(); err == nil {
if strings.HasSuffix(path, "toml") {
loaders = append(loaders, &multiconfig.TOMLLoader{Path: path})
}
if strings.HasSuffix(path, "json") {
loaders = append(loaders, &multiconfig.JSONLoader{Path: path})
}
if strings.HasSuffix(path, "yml") || strings.HasSuffix(path, "yaml") {
loaders = append(loaders, &multiconfig.YAMLLoader{Path: path})
}
}
// Load the environment variable loader
env := &multiconfig.EnvironmentLoader{Prefix: "RAFT", CamelCase: true}
loaders = append(loaders, env)
loader := multiconfig.MultiLoader(loaders...)
if err := loader.Load(c); err != nil {
return err
}
return c.Validate()
}
// Validate the loaded configuration using the multiconfig multi validator.
func (c *Config) Validate() error {
validators := multiconfig.MultiValidator(
&multiconfig.RequiredValidator{},
&ComplexValidator{},
)
return validators.Validate(c)
}
// Update the configuration from another configuration struct
func (c *Config) Update(o *Config) error {
if o == nil {
return nil
}
conf := structs.New(c)
// Then update the current config with values from the other config
for _, field := range structs.Fields(o) {
if !field.IsZero() {
updateField := conf.Field(field.Name())
updateField.Set(field.Value())
}
}
return c.Validate()
}
// GetName returns the name of the local host defined by the configuration or
// using the hostname by default.
func (c *Config) GetName() (name string, err error) {
if c.Name == "" {
if name, err = os.Hostname(); err != nil {
return "", errors.New("could not find unique name of localhost")
}
return name, nil
}
return c.Name, nil
}
// GetPeer returns the local peer configuration or an error if no peer is
// found in the configuration. If the name is not set on the configuration,
// the hostname is used.
func (c *Config) GetPeer() (peers.Peer, error) {
local, err := c.GetName()
if err != nil {
return peers.Peer{}, err
}
for _, peer := range c.Peers {
if peer.Name == local {
return peer, nil
}
}
return peers.Peer{}, fmt.Errorf("could not find peer for '%s'", local)
}
// IsLeader returns true if the local replica is the leader.
func (c *Config) IsLeader() bool {
if c.Leader != "" {
name, _ := c.GetName()
return name == c.Leader
}
return false
}
// GetRemotes returns all peer configurations for remote hosts on the network,
// e.g. by excluding the local peer configuration.
func (c *Config) GetRemotes() (remotes []peers.Peer, err error) {
var local string
if local, err = c.GetName(); err != nil {
return nil, err
}
remotes = make([]peers.Peer, 0, len(c.Peers)-1)
for _, peer := range c.Peers {
if local == peer.Name {
continue
}
remotes = append(remotes, peer)
}
return remotes, nil
}
// GetPath searches possible configuration paths returning the first path it
// finds; this path is used when loading the configuration from disk. An
// error is returned if no configuration file exists.
func (c *Config) GetPath() (string, error) {
// Prepare PATH list
paths := make([]string, 0, 3)
// Look in CWD directory first
if path, err := os.Getwd(); err == nil {
paths = append(paths, filepath.Join(path, "raft"))
}
// Look in user's home directory next
if user, err := user.Current(); err == nil {
paths = append(paths, filepath.Join(user.HomeDir, ".raft"))
}
// Finally look in etc for the global configuration
paths = append(paths, "/etc/raft")
for _, path := range paths {
for _, ext := range []string{".toml", ".json", ".yml", ".yaml"} {
fpath := path + ext
if _, err := os.Stat(fpath); !os.IsNotExist(err) {
return fpath, nil
}
}
}
return "", errors.New("no configuration file found")
}
// GetTick parses the tick duration and returns it.
func (c *Config) GetTick() (time.Duration, error) {
return time.ParseDuration(c.Tick)
}
// GetTimeout parses the timeout duration and returns it.
func (c *Config) GetTimeout() (time.Duration, error) {
return time.ParseDuration(c.Timeout)
}
// GetUptime parses the uptime duration and returns it.
func (c *Config) GetUptime() (time.Duration, error) {
return time.ParseDuration(c.Uptime)
}
// GetLogLevel returns the zerolog level
func (c *Config) GetLogLevel() zerolog.Level {
c.LogLevel = strings.TrimSpace(strings.ToLower(c.LogLevel))
switch c.LogLevel {
case "trace":
return zerolog.TraceLevel
case "debug":
return zerolog.DebugLevel
case "info":
return zerolog.InfoLevel
case "warn", "warning":
return zerolog.WarnLevel
case "error":
return zerolog.ErrorLevel
case "fatal":
return zerolog.FatalLevel
case "panic":
return zerolog.PanicLevel
default:
return zerolog.Disabled
}
}
//===========================================================================
// Validators
//===========================================================================
// ComplexValidator validates complex types that multiconfig doesn't understand
type ComplexValidator struct {
TagName string
}
// Validate implements the multiconfig.Validator interface.
func (v *ComplexValidator) Validate(s interface{}) error {
if v.TagName == "" {
v.TagName = "validate"
}
for _, field := range structs.Fields(s) {
if err := v.processField("", field); err != nil {
return err
}
}
return nil
}
func (v *ComplexValidator) processField(fieldName string, field *structs.Field) error {
fieldName += field.Name()
switch field.Kind() {
case reflect.Struct:
fieldName += "."
for _, f := range field.Fields() {
if err := v.processField(fieldName, f); err != nil {
return err
}
}
default:
if field.IsZero() {
return nil
}
switch strings.ToLower(field.Tag(v.TagName)) {
case "":
return nil
case "duration":
return v.processDurationField(fieldName, field)
case "url":
return v.processURLField(fieldName, field)
case "path":
return v.processPathField(fieldName, field)
case "uint":
return v.processUintField(fieldName, field)
case "zerolog":
return v.processLogLevelField(fieldName, field)
default:
return fmt.Errorf("cannot validate type '%s'", field.Tag(v.TagName))
}
}
return nil
}
func (v *ComplexValidator) processDurationField(fieldName string, field *structs.Field) error {
_, err := time.ParseDuration(field.Value().(string))
if err != nil {
return fmt.Errorf("could not validate %s: %s", fieldName, err.Error())
}
return nil
}
func (v *ComplexValidator) processURLField(fieldName string, field *structs.Field) error {
if _, err := url.Parse(field.Value().(string)); err != nil {
return fmt.Errorf("could not validate %s: %s", fieldName, err.Error())
}
return nil
}
func (v *ComplexValidator) processPathField(fieldName string, field *structs.Field) error {
// No path validation quite yet
return nil
}
func (v *ComplexValidator) processUintField(fieldName string, field *structs.Field) error {
val := field.Value().(int)
if val < 0 {
return fmt.Errorf("%s is less than zero", fieldName)
}
return nil
}
func (v *ComplexValidator) processLogLevelField(fieldName string, field *structs.Field) error {
val := field.Value().(string)
val = strings.TrimSpace(strings.ToLower(val))
var levels = map[string]struct{}{
"trace": {},
"debug": {},
"info": {},
"warn": {},
"warning": {},
"error": {},
"fatal": {},
"panic": {},
}
if _, ok := levels[val]; !ok {
return fmt.Errorf("unknown log level %q", val)
}
return nil
}