-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathstore_spec.go
289 lines (268 loc) · 8.73 KB
/
store_spec.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
// Copyright 2016 The Cockroach 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.
//
// Author: Bram Gruneir ([email protected])
package base
import (
"bytes"
"fmt"
"sort"
"strconv"
"strings"
"unicode"
"github.com/dustin/go-humanize"
"github.com/cockroachdb/cockroach/roachpb"
"github.com/cockroachdb/cockroach/util/humanizeutil"
)
// This file implements method receivers for members of server.Context struct
// -- 'Stores' and 'JoinList', which satisfies pflag's value interface
// MinimumStoreSize is the smallest size in bytes that a store can have. This
// number was originally based on config's defaultZoneConfig's RangeMaxBytes,
// which is extremely stable and to avoid adding the dependency on config here,
// it is just hard coded to 640MiB.
const MinimumStoreSize = 10 * 64 << 20
// StoreSpec contains the details that can be specified in the cli pertaining
// to the --store flag.
type StoreSpec struct {
Path string
SizeInBytes int64
SizePercent float64
InMemory bool
Attributes roachpb.Attributes
}
// String returns a fully parsable version of the store spec.
func (ss StoreSpec) String() string {
var buffer bytes.Buffer
if len(ss.Path) != 0 {
fmt.Fprintf(&buffer, "path=%s,", ss.Path)
}
if ss.InMemory {
fmt.Fprint(&buffer, "type=mem,")
}
if ss.SizeInBytes > 0 {
fmt.Fprintf(&buffer, "size=%s,", humanizeutil.IBytes(ss.SizeInBytes))
}
if ss.SizePercent > 0 {
fmt.Fprintf(&buffer, "size=%s%%,", humanize.Ftoa(ss.SizePercent))
}
if len(ss.Attributes.Attrs) > 0 {
fmt.Fprint(&buffer, "attrs=")
for i, attr := range ss.Attributes.Attrs {
if i != 0 {
fmt.Fprint(&buffer, ":")
}
fmt.Fprintf(&buffer, attr)
}
fmt.Fprintf(&buffer, ",")
}
// Trim the extra comma from the end if it exists.
if l := buffer.Len(); l > 0 {
buffer.Truncate(l - 1)
}
return buffer.String()
}
// newStoreSpec parses the string passed into a --store flag and returns a
// StoreSpec if it is correctly parsed.
// There are four possible fields that can be passed in, comma separated:
// - path=xxx The directory in which to the rocks db instance should be
// located, required unless using a in memory storage.
// - type=mem This specifies that the store is an in memory storage instead of
// an on disk one. mem is currently the only other type available.
// - size=xxx The optional maximum size of the storage. This can be in one of a
// few different formats.
// - 10000000000 -> 10000000000 bytes
// - 20GB -> 20000000000 bytes
// - 20GiB -> 21474836480 bytes
// - 0.02TiB -> 21474836480 bytes
// - 20% -> 20% of the available space
// - 0.2 -> 20% of the available space
// - attrs=xxx:yyy:zzz A colon separated list of optional attributes.
// Note that commas are forbidden within any field name or value.
func newStoreSpec(value string) (StoreSpec, error) {
if len(value) == 0 {
return StoreSpec{}, fmt.Errorf("no value specified")
}
var ss StoreSpec
used := make(map[string]struct{})
for _, split := range strings.Split(value, ",") {
if len(split) == 0 {
continue
}
subSplits := strings.SplitN(split, "=", 2)
var field string
var value string
if len(subSplits) == 1 {
field = "path"
value = subSplits[0]
} else {
field = strings.ToLower(subSplits[0])
value = subSplits[1]
}
if _, ok := used[field]; ok {
return StoreSpec{}, fmt.Errorf("%s field was used twice in store definition", field)
}
used[field] = struct{}{}
if len(field) == 0 {
continue
}
if len(value) == 0 {
return StoreSpec{}, fmt.Errorf("no value specified for %s", field)
}
switch field {
case "path":
if len(value) == 0 {
}
ss.Path = value
case "size":
if len(value) == 0 {
return StoreSpec{}, fmt.Errorf("no size specified")
}
if unicode.IsDigit(rune(value[len(value)-1])) &&
(strings.HasPrefix(value, "0.") || strings.HasPrefix(value, ".")) {
// Value is a percentage without % sign.
var err error
ss.SizePercent, err = strconv.ParseFloat(value, 64)
ss.SizePercent *= 100
if err != nil {
return StoreSpec{}, fmt.Errorf("could not parse store size (%s) %s", value, err)
}
if ss.SizePercent > 100 || ss.SizePercent < 1 {
return StoreSpec{}, fmt.Errorf("store size (%s) must be between 1%% and 100%%", value)
}
} else if strings.HasSuffix(value, "%") {
// Value is a percentage.
var err error
ss.SizePercent, err = strconv.ParseFloat(value[:len(value)-1], 64)
if err != nil {
return StoreSpec{}, fmt.Errorf("could not parse store size (%s) %s", value, err)
}
if ss.SizePercent > 100 || ss.SizePercent < 1 {
return StoreSpec{}, fmt.Errorf("store size (%s) must be between 1%% and 100%%", value)
}
} else {
var err error
ss.SizeInBytes, err = humanizeutil.ParseBytes(value)
if err != nil {
return StoreSpec{}, fmt.Errorf("could not parse store size (%s) %s", value, err)
}
if ss.SizeInBytes < MinimumStoreSize {
return StoreSpec{}, fmt.Errorf("store size (%s) must be larger than %s", value,
humanizeutil.IBytes(MinimumStoreSize))
}
}
case "attrs":
if len(value) == 0 {
return StoreSpec{}, fmt.Errorf("no attributes specified")
}
// Check to make sure there are no duplicate attributes.
attrMap := make(map[string]struct{})
for _, attribute := range strings.Split(value, ":") {
if _, ok := attrMap[attribute]; ok {
return StoreSpec{}, fmt.Errorf("duplicate attribute given for store: %s", attribute)
}
attrMap[attribute] = struct{}{}
}
for attribute := range attrMap {
ss.Attributes.Attrs = append(ss.Attributes.Attrs, attribute)
}
sort.Strings(ss.Attributes.Attrs)
case "type":
if value == "mem" {
ss.InMemory = true
} else {
return StoreSpec{}, fmt.Errorf("%s is not a valid store type", value)
}
default:
return StoreSpec{}, fmt.Errorf("%s is not a valid store field", field)
}
}
if ss.InMemory {
// Only in memory stores don't need a path and require a size.
if ss.Path != "" {
return StoreSpec{}, fmt.Errorf("path specified for in memory store")
}
if ss.SizePercent == 0 && ss.SizeInBytes == 0 {
return StoreSpec{}, fmt.Errorf("size must be specified for an in memory store")
}
} else if ss.Path == "" {
return StoreSpec{}, fmt.Errorf("no path specified")
}
return ss, nil
}
// StoreSpecList contains a slice of StoreSpecs that implements pflag's value
// interface.
type StoreSpecList struct {
Specs []StoreSpec
updated bool // updated is used to determine if specs only contain the default value.
}
// String returns a string representation of all the StoreSpecs. This is part
// of pflag's value interface.
func (ssl StoreSpecList) String() string {
var buffer bytes.Buffer
for _, ss := range ssl.Specs {
fmt.Fprintf(&buffer, "--store=%s ", ss)
}
// Trim the extra space from the end if it exists.
if l := buffer.Len(); l > 0 {
buffer.Truncate(l - 1)
}
return buffer.String()
}
// Type returns the underlying type in string form. This is part of pflag's
// value interface.
func (ssl *StoreSpecList) Type() string {
return "StoreSpec"
}
// Set adds a new value to the StoreSpecValue. It is the important part of
// pflag's value interface.
func (ssl *StoreSpecList) Set(value string) error {
spec, err := newStoreSpec(value)
if err != nil {
return err
}
if !ssl.updated {
ssl.Specs = []StoreSpec{spec}
ssl.updated = true
} else {
ssl.Specs = append(ssl.Specs, spec)
}
return nil
}
// JoinListType is a slice of strings that implements pflag's value
// interface.
type JoinListType []string
// String returns a string representation of all the JoinListType. This is part
// of pflag's value interface.
func (jls JoinListType) String() string {
var buffer bytes.Buffer
for _, jl := range jls {
fmt.Fprintf(&buffer, "--join=%s ", jl)
}
// Trim the extra space from the end if it exists.
if l := buffer.Len(); l > 0 {
buffer.Truncate(l - 1)
}
return buffer.String()
}
// Type returns the underlying type in string form. This is part of pflag's
// value interface.
func (jls *JoinListType) Type() string {
return "string"
}
// Set adds a new value to the JoinListType. It is the important part of
// pflag's value interface.
func (jls *JoinListType) Set(value string) error {
*jls = append(*jls, value)
return nil
}