-
Notifications
You must be signed in to change notification settings - Fork 451
/
fs.go
322 lines (266 loc) · 10.6 KB
/
fs.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
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package config
import (
"fmt"
"os"
)
const (
// DefaultNewFileMode is the default new file mode.
DefaultNewFileMode = os.FileMode(0666)
// DefaultNewDirectoryMode is the default new directory mode.
DefaultNewDirectoryMode = os.FileMode(0755)
defaultFilePathPrefix = "/var/lib/m3db"
defaultWriteBufferSize = 65536
defaultDataReadBufferSize = 65536
defaultInfoReadBufferSize = 128
defaultSeekReadBufferSize = 4096
defaultThroughputLimitMbps = 1000.0
defaultThroughputCheckEvery = 128
defaultForceIndexSummariesMmapMemory = false
defaultForceBloomFilterMmapMemory = false
defaultBloomFilterFalsePositivePercent = 0.02
)
// DefaultMmapConfiguration is the default mmap configuration.
func DefaultMmapConfiguration() MmapConfiguration {
return MmapConfiguration{
HugeTLB: MmapHugeTLBConfiguration{
Enabled: true, // Enable when on a platform that supports
Threshold: 2 << 14, // 32kb and above mappings use huge pages
},
}
}
// FilesystemConfiguration is the filesystem configuration.
type FilesystemConfiguration struct {
// File path prefix for reading/writing TSDB files
FilePathPrefix *string `yaml:"filePathPrefix"`
// Write buffer size
WriteBufferSize *int `yaml:"writeBufferSize"`
// Data read buffer size
DataReadBufferSize *int `yaml:"dataReadBufferSize"`
// Info metadata file read buffer size
InfoReadBufferSize *int `yaml:"infoReadBufferSize"`
// Seek data read buffer size
SeekReadBufferSize *int `yaml:"seekReadBufferSize"`
// Disk flush throughput limit in Mb/s
ThroughputLimitMbps *float64 `yaml:"throughputLimitMbps"`
// Disk flush throughput check interval
ThroughputCheckEvery *int `yaml:"throughputCheckEvery"`
// NewFileMode is the new file permissions mode to use when
// creating files - specify as three digits, e.g. 666.
NewFileMode *string `yaml:"newFileMode"`
// NewDirectoryMode is the new file permissions mode to use when
// creating directories - specify as three digits, e.g. 755.
NewDirectoryMode *string `yaml:"newDirectoryMode"`
// Mmap is the mmap options which features are primarily platform dependent
Mmap *MmapConfiguration `yaml:"mmap"`
// ForceIndexSummariesMmapMemory forces the mmap that stores the index lookup bytes
// to be an anonymous region in memory as opposed to a file-based mmap.
ForceIndexSummariesMmapMemory *bool `yaml:"force_index_summaries_mmap_memory"`
// ForceBloomFilterMmapMemory forces the mmap that stores the index lookup bytes
// to be an anonymous region in memory as opposed to a file-based mmap.
ForceBloomFilterMmapMemory *bool `yaml:"force_bloom_filter_mmap_memory"`
// BloomFilterFalsePositivePercent controls the target false positive percentage
// for the bloom filters for the fileset files.
BloomFilterFalsePositivePercent *float64 `yaml:"bloomFilterFalsePositivePercent"`
}
// Validate validates the Filesystem configuration. We use this method to validate
// fields where the validator package falls short.
func (f FilesystemConfiguration) Validate() error {
if f.WriteBufferSize != nil && *f.WriteBufferSize < 1 {
return fmt.Errorf(
"fs writeBufferSize is set to: %d, but must be at least 1",
*f.WriteBufferSize)
}
if f.DataReadBufferSize != nil && *f.DataReadBufferSize < 1 {
return fmt.Errorf(
"fs dataReadBufferSize is set to: %d, but must be at least 1",
*f.DataReadBufferSize)
}
if f.InfoReadBufferSize != nil && *f.InfoReadBufferSize < 1 {
return fmt.Errorf(
"fs infoReadBufferSize is set to: %d, but must be at least 1",
*f.InfoReadBufferSize)
}
if f.SeekReadBufferSize != nil && *f.SeekReadBufferSize < 1 {
return fmt.Errorf(
"fs seekReadBufferSize is set to: %d, but must be at least 1",
*f.SeekReadBufferSize)
}
if f.ThroughputLimitMbps != nil && *f.ThroughputLimitMbps < 1 {
return fmt.Errorf(
"fs throughputLimitMbps is set to: %f, but must be at least 1",
*f.ThroughputLimitMbps)
}
if f.ThroughputCheckEvery != nil && *f.ThroughputCheckEvery < 1 {
return fmt.Errorf(
"fs throughputCheckEvery is set to: %d, but must be at least 1",
*f.ThroughputCheckEvery)
}
if f.BloomFilterFalsePositivePercent != nil &&
(*f.BloomFilterFalsePositivePercent < 0 || *f.BloomFilterFalsePositivePercent > 1) {
return fmt.Errorf(
"fs bloomFilterFalsePositivePercent is set to: %f, but must be between 0.0 and 1.0",
*f.BloomFilterFalsePositivePercent)
}
return nil
}
// FilePathPrefixOrDefault returns the configured file path prefix if configured, or a
// default value otherwise.
func (f FilesystemConfiguration) FilePathPrefixOrDefault() string {
if f.FilePathPrefix != nil {
return *f.FilePathPrefix
}
return defaultFilePathPrefix
}
// WriteBufferSizeOrDefault returns the configured write buffer size if configured, or a
// default value otherwise.
func (f FilesystemConfiguration) WriteBufferSizeOrDefault() int {
if f.WriteBufferSize != nil {
return *f.WriteBufferSize
}
return defaultWriteBufferSize
}
// DataReadBufferSizeOrDefault returns the configured data read buffer size if configured, or a
// default value otherwise.
func (f FilesystemConfiguration) DataReadBufferSizeOrDefault() int {
if f.DataReadBufferSize != nil {
return *f.DataReadBufferSize
}
return defaultDataReadBufferSize
}
// InfoReadBufferSizeOrDefault returns the configured info read buffer size if configured, or a
// default value otherwise.
func (f FilesystemConfiguration) InfoReadBufferSizeOrDefault() int {
if f.InfoReadBufferSize != nil {
return *f.InfoReadBufferSize
}
return defaultInfoReadBufferSize
}
// SeekReadBufferSizeOrDefault returns the configured seek read buffer size if configured, or a
// default value otherwise.
func (f FilesystemConfiguration) SeekReadBufferSizeOrDefault() int {
if f.SeekReadBufferSize != nil {
return *f.SeekReadBufferSize
}
return defaultSeekReadBufferSize
}
// ThroughputLimitMbpsOrDefault returns the configured throughput limit mbps if configured, or a
// default value otherwise.
func (f FilesystemConfiguration) ThroughputLimitMbpsOrDefault() float64 {
if f.ThroughputLimitMbps != nil {
return *f.ThroughputLimitMbps
}
return defaultThroughputLimitMbps
}
// ThroughputCheckEveryOrDefault returns the configured throughput check every value if configured, or a
// default value otherwise.
func (f FilesystemConfiguration) ThroughputCheckEveryOrDefault() int {
if f.ThroughputCheckEvery != nil {
return *f.ThroughputCheckEvery
}
return defaultThroughputCheckEvery
}
// MmapConfigurationOrDefault returns the configured mmap configuration if configured, or a
// default value otherwise.
func (f FilesystemConfiguration) MmapConfigurationOrDefault() MmapConfiguration {
if f.Mmap == nil {
return DefaultMmapConfiguration()
}
return *f.Mmap
}
// ForceIndexSummariesMmapMemoryOrDefault returns the configured value for forcing the summaries
// mmaps into anonymous region in memory if configured, or a default value otherwise.
func (f FilesystemConfiguration) ForceIndexSummariesMmapMemoryOrDefault() bool {
if f.ForceIndexSummariesMmapMemory != nil {
return *f.ForceIndexSummariesMmapMemory
}
return defaultForceIndexSummariesMmapMemory
}
// ForceBloomFilterMmapMemoryOrDefault returns the configured value for forcing the bloom
// filter mmaps into anonymous region in memory if configured, or a default value otherwise.
func (f FilesystemConfiguration) ForceBloomFilterMmapMemoryOrDefault() bool {
if f.ForceBloomFilterMmapMemory != nil {
return *f.ForceBloomFilterMmapMemory
}
return defaultForceBloomFilterMmapMemory
}
// BloomFilterFalsePositivePercentOrDefault returns the configured value for the target
// false positive percent for the bloom filter for the fileset files if configured, or a default
// value otherwise
func (f FilesystemConfiguration) BloomFilterFalsePositivePercentOrDefault() float64 {
if f.BloomFilterFalsePositivePercent != nil {
return *f.BloomFilterFalsePositivePercent
}
return defaultBloomFilterFalsePositivePercent
}
// MmapConfiguration is the mmap configuration.
type MmapConfiguration struct {
// HugeTLB is the huge pages configuration which will only take affect
// on platforms that support it, currently just linux
HugeTLB MmapHugeTLBConfiguration `yaml:"hugeTLB"`
}
// MmapHugeTLBConfiguration is the mmap huge TLB configuration.
type MmapHugeTLBConfiguration struct {
// Enabled if true or disabled if false
Enabled bool `yaml:"enabled"`
// Threshold is the threshold on which to use the huge TLB flag if enabled
Threshold int64 `yaml:"threshold"`
}
// ParseNewFileMode parses the specified new file mode.
func (f FilesystemConfiguration) ParseNewFileMode() (os.FileMode, error) {
if f.NewFileMode == nil {
return DefaultNewFileMode, nil
}
str := *f.NewFileMode
if len(str) != 3 {
return 0, fmt.Errorf("file mode must be 3 chars long")
}
str = "0" + str
var v uint32
n, err := fmt.Sscanf(str, "%o", &v)
if err != nil {
return 0, fmt.Errorf("unable to parse: %v", err)
}
if n != 1 {
return 0, fmt.Errorf("no value to parse")
}
return os.FileMode(v), nil
}
// ParseNewDirectoryMode parses the specified new directory mode.
func (f FilesystemConfiguration) ParseNewDirectoryMode() (os.FileMode, error) {
if f.NewDirectoryMode == nil {
return DefaultNewDirectoryMode, nil
}
str := *f.NewDirectoryMode
if len(str) != 3 {
return 0, fmt.Errorf("file mode must be 3 chars long")
}
str = "0" + str
var v uint32
n, err := fmt.Sscanf(str, "%o", &v)
if err != nil {
return 0, fmt.Errorf("unable to parse: %v", err)
}
if n != 1 {
return 0, fmt.Errorf("no value to parse")
}
return os.ModeDir | os.FileMode(v), nil
}