Skip to content

Commit a138fbd

Browse files
craig[bot]RaduBerinde
andcommitted
Merge #148338
148338: storageconfig: cleanup and move StoreSpec r=RaduBerinde a=RaduBerinde #### storageconfig: clean up SizeSpec This change renames SizeSpec to Size (everything in `storageconfig` is a "spec"); cleans up the interface of Size; and separates the pflag-specific code to a wrapper inside the `cli` package. Epic: none Release note: None #### storageconfig: move provisioned rate Move `base.ProvisionedRateSpec` to `storageconfig.ProvisionedRate`. Epic: none Release note: None #### storageconfig: move StoreSpec Epic: none Release note: None Co-authored-by: Radu Berinde <[email protected]>
2 parents 34e9bca + 4744a80 commit a138fbd

32 files changed

+326
-341
lines changed

pkg/acceptance/cluster/dockercluster.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,9 +485,9 @@ func (l *DockerCluster) startNode(ctx context.Context, node *testNode, singleNod
485485
for _, store := range node.stores {
486486
storeSpec := base.StoreSpec{
487487
Path: store.dir,
488-
Size: storageconfig.SizeSpec{Capacity: int64(store.config.MaxRanges) * maxRangeBytes},
488+
Size: storageconfig.Size{Bytes: int64(store.config.MaxRanges) * maxRangeBytes},
489489
}
490-
cmd = append(cmd, fmt.Sprintf("--store=%s", storeSpec))
490+
cmd = append(cmd, fmt.Sprintf("--store=%s", base.StoreSpecCmdLineString(storeSpec)))
491491
}
492492
// Append --join flag for all nodes.
493493
if !singleNode {

pkg/base/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ go_test(
6363
data = glob(["testdata/**"]),
6464
embed = [":base"],
6565
deps = [
66-
"//pkg/roachpb",
6766
"//pkg/security/securityassets",
6867
"//pkg/security/securitytest",
6968
"//pkg/storage/storageconfig",

pkg/base/store_spec.go

Lines changed: 41 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"unicode"
1616

1717
"github.com/cockroachdb/cockroach/pkg/cli/cliflags"
18-
"github.com/cockroachdb/cockroach/pkg/roachpb"
1918
"github.com/cockroachdb/cockroach/pkg/storage/storageconfig"
2019
"github.com/cockroachdb/cockroach/pkg/util/humanizeutil"
2120
"github.com/cockroachdb/errors"
@@ -50,72 +49,45 @@ func GetAbsoluteFSPath(fieldName string, p string) (string, error) {
5049
return ret, nil
5150
}
5251

53-
// ProvisionedRateSpec is an optional part of the StoreSpec.
54-
type ProvisionedRateSpec struct {
55-
// ProvisionedBandwidth is the bandwidth provisioned for this store in bytes/s.
56-
ProvisionedBandwidth int64
57-
}
58-
59-
func newStoreProvisionedRateSpec(
52+
func parseStoreProvisionedRate(
6053
field redact.SafeString, value string,
61-
) (ProvisionedRateSpec, error) {
54+
) (storageconfig.ProvisionedRate, error) {
6255
split := strings.Split(value, "=")
6356
if len(split) != 2 {
64-
return ProvisionedRateSpec{}, errors.Errorf("%s field has invalid value %s", field, value)
57+
return storageconfig.ProvisionedRate{}, errors.Errorf("%s field has invalid value %s", field, value)
6558
}
6659
subField := split[0]
6760
subValue := split[1]
6861
if subField != "bandwidth" {
69-
return ProvisionedRateSpec{}, errors.Errorf("%s field does not have bandwidth sub-field", field)
62+
return storageconfig.ProvisionedRate{}, errors.Errorf("%s field does not have bandwidth sub-field", field)
7063
}
7164
if len(subValue) == 0 {
72-
return ProvisionedRateSpec{}, errors.Errorf("%s field has no value specified for bandwidth", field)
65+
return storageconfig.ProvisionedRate{}, errors.Errorf("%s field has no value specified for bandwidth", field)
7366
}
7467
if len(subValue) <= 2 || subValue[len(subValue)-2:] != "/s" {
75-
return ProvisionedRateSpec{},
68+
return storageconfig.ProvisionedRate{},
7669
errors.Errorf("%s field does not have bandwidth sub-field %s ending in /s",
7770
field, subValue)
7871
}
7972
bandwidthString := subValue[:len(subValue)-2]
8073
bandwidth, err := humanizeutil.ParseBytes(bandwidthString)
8174
if err != nil {
82-
return ProvisionedRateSpec{},
75+
return storageconfig.ProvisionedRate{},
8376
errors.Wrapf(err, "could not parse bandwidth in field %s", field)
8477
}
8578
if bandwidth == 0 {
86-
return ProvisionedRateSpec{},
79+
return storageconfig.ProvisionedRate{},
8780
errors.Errorf("%s field is trying to set bandwidth to 0", field)
8881
}
89-
return ProvisionedRateSpec{ProvisionedBandwidth: bandwidth}, nil
82+
return storageconfig.ProvisionedRate{ProvisionedBandwidth: bandwidth}, nil
9083
}
9184

9285
// StoreSpec contains the details that can be specified in the cli pertaining
9386
// to the --store flag.
94-
type StoreSpec struct {
95-
Path string
96-
Size storageconfig.SizeSpec
97-
BallastSize *storageconfig.SizeSpec
98-
InMemory bool
99-
Attributes roachpb.Attributes
100-
// StickyVFSID is a unique identifier associated with a given store which
101-
// will preserve the in-memory virtual file system (VFS) even after the
102-
// storage engine has been closed. This only applies to in-memory storage
103-
// engine.
104-
StickyVFSID string
105-
// PebbleOptions contains Pebble-specific options in the same format as a
106-
// Pebble OPTIONS file. For example:
107-
// [Options]
108-
// delete_range_flush_delay=2s
109-
// flush_split_bytes=4096
110-
PebbleOptions string
111-
// EncryptionOptions is set if encryption is enabled.
112-
EncryptionOptions *storageconfig.EncryptionOptions
113-
// ProvisionedRateSpec is optional.
114-
ProvisionedRateSpec ProvisionedRateSpec
115-
}
87+
type StoreSpec = storageconfig.Store
11688

117-
// String returns a fully parsable version of the store spec.
118-
func (ss StoreSpec) String() string {
89+
// StoreSpecCmdLineString returns a fully parsable version of the store spec.
90+
func StoreSpecCmdLineString(ss storageconfig.Store) string {
11991
// TODO(jackson): Implement redact.SafeFormatter
12092
var buffer bytes.Buffer
12193
if len(ss.Path) != 0 {
@@ -124,23 +96,23 @@ func (ss StoreSpec) String() string {
12496
if ss.InMemory {
12597
fmt.Fprint(&buffer, "type=mem,")
12698
}
127-
if ss.Size.Capacity > 0 {
128-
fmt.Fprintf(&buffer, "size=%s,", humanizeutil.IBytes(ss.Size.Capacity))
99+
if ss.Size.Bytes > 0 {
100+
fmt.Fprintf(&buffer, "size=%s,", humanizeutil.IBytes(ss.Size.Bytes))
129101
}
130102
if ss.Size.Percent > 0 {
131103
fmt.Fprintf(&buffer, "size=%s%%,", humanize.Ftoa(ss.Size.Percent))
132104
}
133105
if ss.BallastSize != nil {
134-
if ss.BallastSize.Capacity > 0 {
135-
fmt.Fprintf(&buffer, "ballast-size=%s,", humanizeutil.IBytes(ss.BallastSize.Capacity))
106+
if ss.BallastSize.Bytes > 0 {
107+
fmt.Fprintf(&buffer, "ballast-size=%s,", humanizeutil.IBytes(ss.BallastSize.Bytes))
136108
}
137109
if ss.BallastSize.Percent > 0 {
138110
fmt.Fprintf(&buffer, "ballast-size=%s%%,", humanize.Ftoa(ss.BallastSize.Percent))
139111
}
140112
}
141-
if len(ss.Attributes.Attrs) > 0 {
113+
if len(ss.Attributes) > 0 {
142114
fmt.Fprint(&buffer, "attrs=")
143-
for i, attr := range ss.Attributes.Attrs {
115+
for i, attr := range ss.Attributes {
144116
if i != 0 {
145117
fmt.Fprint(&buffer, ":")
146118
}
@@ -154,9 +126,9 @@ func (ss StoreSpec) String() string {
154126
fmt.Fprint(&buffer, optsStr)
155127
fmt.Fprint(&buffer, ",")
156128
}
157-
if ss.ProvisionedRateSpec.ProvisionedBandwidth > 0 {
129+
if ss.ProvisionedRate.ProvisionedBandwidth > 0 {
158130
fmt.Fprintf(&buffer, "provisioned-rate=bandwidth=%s/s,",
159-
humanizeutil.IBytes(ss.ProvisionedRateSpec.ProvisionedBandwidth))
131+
humanizeutil.IBytes(ss.ProvisionedRate.ProvisionedBandwidth))
160132
}
161133
// Trim the extra comma from the end if it exists.
162134
if l := buffer.Len(); l > 0 {
@@ -165,11 +137,6 @@ func (ss StoreSpec) String() string {
165137
return buffer.String()
166138
}
167139

168-
// IsEncrypted returns whether the StoreSpec has encryption enabled.
169-
func (ss StoreSpec) IsEncrypted() bool {
170-
return ss.EncryptionOptions != nil
171-
}
172-
173140
// NewStoreSpec parses the string passed into a --store flag and returns a
174141
// StoreSpec if it is correctly parsed.
175142
// There are five possible fields that can be passed in, comma separated:
@@ -229,30 +196,22 @@ func NewStoreSpec(value string) (StoreSpec, error) {
229196
ss.Path = value
230197
case "size":
231198
var err error
232-
var minBytesAllowed int64 = MinimumStoreSize
233-
var minPercent float64 = 1
234-
var maxPercent float64 = 100
235-
ss.Size, err = storageconfig.NewSizeSpec(
236-
"store",
237-
value,
238-
&storageconfig.IntInterval{Min: &minBytesAllowed},
239-
&storageconfig.FloatInterval{Min: &minPercent, Max: &maxPercent},
240-
)
199+
constraints := storageconfig.SizeSpecConstraints{
200+
MinBytes: MinimumStoreSize,
201+
MinPercent: 1,
202+
MaxPercent: 100,
203+
}
204+
ss.Size, err = storageconfig.ParseSizeSpec(value, constraints)
241205
if err != nil {
242206
return StoreSpec{}, err
243207
}
244208
case "ballast-size":
245-
var minBytesAllowed int64
246-
var minPercent float64 = 0
247-
var maxPercent float64 = 50
248-
ballastSize, err := storageconfig.NewSizeSpec(
249-
"ballast",
250-
value,
251-
&storageconfig.IntInterval{Min: &minBytesAllowed},
252-
&storageconfig.FloatInterval{Min: &minPercent, Max: &maxPercent},
253-
)
209+
constraints := storageconfig.SizeSpecConstraints{
210+
MaxPercent: 50,
211+
}
212+
ballastSize, err := storageconfig.ParseSizeSpec(value, constraints)
254213
if err != nil {
255-
return StoreSpec{}, err
214+
return StoreSpec{}, errors.Wrap(err, "ballast")
256215
}
257216
ss.BallastSize = &ballastSize
258217
case "attrs":
@@ -265,9 +224,9 @@ func NewStoreSpec(value string) (StoreSpec, error) {
265224
attrMap[attribute] = struct{}{}
266225
}
267226
for attribute := range attrMap {
268-
ss.Attributes.Attrs = append(ss.Attributes.Attrs, attribute)
227+
ss.Attributes = append(ss.Attributes, attribute)
269228
}
270-
sort.Strings(ss.Attributes.Attrs)
229+
sort.Strings(ss.Attributes)
271230
case "type":
272231
if value == "mem" {
273232
ss.InMemory = true
@@ -311,11 +270,11 @@ func NewStoreSpec(value string) (StoreSpec, error) {
311270
}
312271
ss.PebbleOptions = buf.String()
313272
case "provisioned-rate":
314-
rateSpec, err := newStoreProvisionedRateSpec("provisioned-rate", value)
273+
rateSpec, err := parseStoreProvisionedRate("provisioned-rate", value)
315274
if err != nil {
316275
return StoreSpec{}, err
317276
}
318-
ss.ProvisionedRateSpec = rateSpec
277+
ss.ProvisionedRate = rateSpec
319278

320279
default:
321280
return StoreSpec{}, fmt.Errorf("%s is not a valid store field", field)
@@ -326,7 +285,7 @@ func NewStoreSpec(value string) (StoreSpec, error) {
326285
if ss.Path != "" {
327286
return StoreSpec{}, fmt.Errorf("path specified for in memory store")
328287
}
329-
if ss.Size.Percent == 0 && ss.Size.Capacity == 0 {
288+
if ss.Size.Percent == 0 && ss.Size.Bytes == 0 {
330289
return StoreSpec{}, fmt.Errorf("size must be specified for an in memory store")
331290
}
332291
if ss.BallastSize != nil {
@@ -352,7 +311,7 @@ var _ pflag.Value = &StoreSpecList{}
352311
func (ssl StoreSpecList) String() string {
353312
var buffer bytes.Buffer
354313
for _, ss := range ssl.Specs {
355-
fmt.Fprintf(&buffer, "--%s=%s ", cliflags.Store.Name, ss)
314+
fmt.Fprintf(&buffer, "--%s=%s ", cliflags.Store.Name, StoreSpecCmdLineString(ss))
356315
}
357316
// Trim the extra space from the end if it exists.
358317
if l := buffer.Len(); l > 0 {
@@ -397,7 +356,10 @@ func (ssl StoreSpecList) PriorCriticalAlertError() (err error) {
397356
err = errors.WithDetailf(err, "%v", newErr)
398357
}
399358
for _, ss := range ssl.Specs {
400-
path := ss.PreventedStartupFile()
359+
if ss.InMemory {
360+
continue
361+
}
362+
path := PreventedStartupFile(filepath.Join(ss.Path, AuxiliaryDir))
401363
if path == "" {
402364
continue
403365
}
@@ -413,16 +375,6 @@ func (ssl StoreSpecList) PriorCriticalAlertError() (err error) {
413375
return err
414376
}
415377

416-
// PreventedStartupFile returns the path to a file which, if it exists, should
417-
// prevent the server from starting up. Returns an empty string for in-memory
418-
// engines.
419-
func (ss StoreSpec) PreventedStartupFile() string {
420-
if ss.InMemory {
421-
return ""
422-
}
423-
return PreventedStartupFile(filepath.Join(ss.Path, AuxiliaryDir))
424-
}
425-
426378
// Type returns the underlying type in string form. This is part of pflag's
427379
// value interface.
428380
func (ssl *StoreSpecList) Type() string {

0 commit comments

Comments
 (0)