This repository has been archived by the owner on Mar 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathconfig.go
246 lines (214 loc) · 7.24 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
package main
import (
"errors"
"fmt"
"net/url"
"os"
"path/filepath"
"strings"
"time"
"github.com/BurntSushi/toml"
"github.com/stripe/sequins/blocks"
)
const defaultSearchPath = "sequins.conf:/etc/sequins.conf"
var errNoConfig = errors.New("no config file found")
type sequinsConfig struct {
Source string `toml:"source"`
Bind string `toml:"bind"`
MaxParallelLoads int `toml:"max_parallel_loads"`
ThrottleLoads duration `toml:"throttle_loads"`
WriteBufferSize int `toml:"write_buffer_size"`
LocalStore string `toml:"local_store"`
RefreshPeriod duration `toml:"refresh_period"`
RequireSuccessFile bool `toml:"require_success_file"`
ContentType string `toml:"content_type"`
MaxDownloadBandwidthMBPerSecond int `toml:"max_download_bandwidth_mb_per_second"`
GoforitFlagJsonPath string `toml:"goforit_flag_json_path"`
Storage storageConfig `toml:"storage"`
S3 s3Config `toml:"s3"`
Sharding shardingConfig `toml:"sharding"`
ZK zkConfig `toml:"zk"`
Debug debugConfig `toml:"debug"`
Test testConfig `toml:"test"`
Datadog datadogConfig `toml:"datadog"`
}
type storageConfig struct {
Compression blocks.Compression `toml:"compression"`
BlockSize int `toml:"block_size"`
}
type s3Config struct {
Region string `toml:"region"`
AccessKeyId string `toml:"access_key_id"`
MaxRetries int `toml:"max_retries"`
SecretAccessKey string `toml:"secret_access_key"`
}
type shardingConfig struct {
Enabled bool `toml:"enabled"`
Replication int `toml:"replication"`
MinReplication int `toml:"min_replication"`
MaxReplication int `toml:"max_replication"`
TimeToConverge duration `toml:"time_to_converge"`
ProxyTimeout duration `toml:"proxy_timeout"`
ProxyStageTimeout duration `toml:"proxy_stage_timeout"`
ClusterName string `toml:"cluster_name"`
AdvertisedHostname string `toml:"advertised_hostname"`
ShardID string `toml:"shard_id"`
}
type zkConfig struct {
Servers []string `toml:"servers"`
ConnectTimeout duration `toml:"connect_timeout"`
SessionTimeout duration `toml:"session_timeout"`
}
type debugConfig struct {
Bind string `toml:"bind"`
Expvars bool `toml:"expvars"`
Pprof bool `toml:"pprof"`
RequestLogEnable bool `toml:"request_log_enable"`
RequestLogFile string `toml:"request_log_file"`
}
// testConfig has some options used in functional tests to slow sequins down
// and make it more observable.
type hangAfterRead struct {
Version string `toml:"db"`
File string `toml:"file"`
}
type testConfig struct {
UpgradeDelay duration `toml:"upgrade_delay"`
AllowLocalCluster bool `toml:"allow_local_cluster"`
VersionRemoveTimeout duration `toml:"version_remove_timeout"`
S3 s3Config `toml:"s3"`
Hang hangAfterRead `toml:"hang"`
}
type datadogConfig struct {
Url string `toml:"url"`
}
func defaultConfig() sequinsConfig {
return sequinsConfig{
Source: "",
Bind: "0.0.0.0:9599",
LocalStore: "/var/sequins/",
MaxParallelLoads: 0,
WriteBufferSize: 32 * 1024, // what io.Copy uses internally
RefreshPeriod: duration{time.Duration(0)},
RequireSuccessFile: false,
ContentType: "",
Storage: storageConfig{
Compression: blocks.SnappyCompression,
BlockSize: 4096,
},
S3: s3Config{
Region: "",
AccessKeyId: "",
MaxRetries: 3,
SecretAccessKey: "",
},
Sharding: shardingConfig{
Enabled: false,
Replication: 2,
MinReplication: 1,
MaxReplication: 0,
TimeToConverge: duration{10 * time.Second},
ProxyTimeout: duration{100 * time.Millisecond},
ProxyStageTimeout: duration{time.Duration(0)},
ClusterName: "sequins",
AdvertisedHostname: "",
ShardID: "",
},
ZK: zkConfig{
Servers: []string{"localhost:2181"},
ConnectTimeout: duration{1 * time.Second},
SessionTimeout: duration{10 * time.Second},
},
Debug: debugConfig{
Bind: "",
Expvars: true,
Pprof: false,
RequestLogEnable: false,
RequestLogFile: "stdout",
},
Test: testConfig{
UpgradeDelay: duration{time.Duration(0)},
VersionRemoveTimeout: duration{time.Duration(0)},
S3: s3Config{
Region: "",
AccessKeyId: "",
SecretAccessKey: "",
},
},
Datadog: datadogConfig{
Url: "localhost:8200",
},
}
}
func loadConfig(searchPath string) (sequinsConfig, error) {
if searchPath == "" {
searchPath = defaultSearchPath
}
config := defaultConfig()
paths := filepath.SplitList(searchPath)
for _, path := range paths {
md, err := toml.DecodeFile(path, &config)
if os.IsNotExist(err) {
continue
} else if err != nil {
return config, err
} else if len(md.Undecoded()) > 0 {
return config, fmt.Errorf("found unrecognized properties: %v", md.Undecoded())
}
return config, nil
}
return config, errNoConfig
}
func validateConfig(config sequinsConfig) (sequinsConfig, error) {
if !filepath.IsAbs(config.LocalStore) {
return config, fmt.Errorf("local store path must be absolute: %s", config.LocalStore)
}
if config.Source == "" {
return config, errors.New("source must be set")
}
parsed, err := url.Parse(config.Source)
if err != nil {
return config, fmt.Errorf("parsing source: %s", err)
}
if parsed.Scheme == "" || parsed.Scheme == "file" {
if parsed.Host != "" {
return config, fmt.Errorf("local source path is invalid (likely missing a '/'): %s", config.Source)
}
if !filepath.IsAbs(parsed.Path) {
return config, fmt.Errorf("local source path must be absolute: %s", config.Source)
}
if strings.HasPrefix(filepath.Dir(config.LocalStore), filepath.Clean(parsed.Path)) {
return config, fmt.Errorf("local store can't be within source root: %s", config.LocalStore)
}
if config.Sharding.Enabled && !config.Test.AllowLocalCluster {
return config, errors.New("you can't run sequins with sharding enabled on local paths")
}
}
switch config.Storage.Compression {
case blocks.SnappyCompression, blocks.NoCompression:
default:
return config, fmt.Errorf("unrecognized compression option: %s", config.Storage.Compression)
}
if config.Sharding.Replication <= 0 {
return config, fmt.Errorf("invalid replication factor: %d", config.Sharding.Replication)
}
if config.Sharding.MinReplication <= 0 {
return config, fmt.Errorf("invalid minimum replication factor: %d", config.Sharding.MinReplication)
}
if config.Sharding.MinReplication > config.Sharding.Replication {
return config, fmt.Errorf("invalid minimum replication factor greater than total replication factor: %d > %d",
config.Sharding.MinReplication, config.Sharding.Replication)
}
return config, nil
}
type duration struct {
time.Duration
}
func (d *duration) UnmarshalText(text []byte) error {
var err error
d.Duration, err = time.ParseDuration(string(text))
return err
}
func (d duration) MarshalText() ([]byte, error) {
return []byte(d.Duration.String()), nil
}