Skip to content

Commit c7456e9

Browse files
committed
storageconfig: move WAL failover flag code to cli
Move the pflag-specific part to cli. Epic: none Release note: None
1 parent bd7e88a commit c7456e9

File tree

8 files changed

+66
-36
lines changed

8 files changed

+66
-36
lines changed

pkg/base/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ go_test(
7474
"//pkg/util/leaktest",
7575
"//pkg/util/log",
7676
"//pkg/util/uuid",
77-
"@com_github_cockroachdb_datadriven//:datadriven",
7877
"@com_github_cockroachdb_errors//:errors",
7978
"@com_github_davecgh_go_spew//spew",
8079
"@com_github_stretchr_testify//require",

pkg/base/config_test.go

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,15 @@
66
package base_test
77

88
import (
9-
"bytes"
109
"fmt"
1110
"math"
12-
"strings"
1311
"testing"
1412
"time"
1513

1614
"github.com/cockroachdb/cockroach/pkg/base"
17-
"github.com/cockroachdb/cockroach/pkg/storage/storageconfig"
1815
"github.com/cockroachdb/cockroach/pkg/testutils/datapathutils"
1916
"github.com/cockroachdb/cockroach/pkg/testutils/echotest"
2017
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
21-
"github.com/cockroachdb/datadriven"
2218
"github.com/davecgh/go-spew/spew"
2319
"github.com/stretchr/testify/require"
2420
)
@@ -211,20 +207,3 @@ func TestRaftMaxInflightBytes(t *testing.T) {
211207
})
212208
}
213209
}
214-
215-
func TestWALFailoverConfigRoundtrip(t *testing.T) {
216-
defer leaktest.AfterTest(t)()
217-
218-
datadriven.RunTest(t, datapathutils.TestDataPath(t, "wal-failover-config"), func(t *testing.T, d *datadriven.TestData) string {
219-
var buf bytes.Buffer
220-
for _, l := range strings.Split(d.Input, "\n") {
221-
var cfg storageconfig.WALFailover
222-
if err := cfg.Set(l); err != nil {
223-
fmt.Fprintf(&buf, "err: %s\n", err)
224-
continue
225-
}
226-
fmt.Fprintln(&buf, cfg.String())
227-
}
228-
return buf.String()
229-
})
230-
}

pkg/cli/flags.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ func init() {
529529
cliflagcfg.StringFlag(f, &deprecatedStorageEngine, cliflags.StorageEngine)
530530
_ = pf.MarkHidden(cliflags.StorageEngine.Name)
531531

532-
cliflagcfg.VarFlag(f, &serverCfg.StorageConfig.WALFailover, cliflags.WALFailover)
532+
cliflagcfg.VarFlag(f, &walFailoverWrapper{cfg: &serverCfg.StorageConfig.WALFailover}, cliflags.WALFailover)
533533
// TODO(storage): Consider combining the uri and cache manual settings.
534534
// Alternatively remove the ability to configure shared storage without
535535
// passing a bootstrap configuration file.

pkg/cli/flags_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@ import (
2929
"github.com/cockroachdb/cockroach/pkg/server/status"
3030
"github.com/cockroachdb/cockroach/pkg/storage/storageconfig"
3131
"github.com/cockroachdb/cockroach/pkg/testutils"
32+
"github.com/cockroachdb/cockroach/pkg/testutils/datapathutils"
3233
"github.com/cockroachdb/cockroach/pkg/testutils/skip"
3334
"github.com/cockroachdb/cockroach/pkg/util"
3435
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
3536
"github.com/cockroachdb/cockroach/pkg/util/log"
37+
"github.com/cockroachdb/datadriven"
3638
"github.com/pmezard/go-difflib/difflib"
3739
"github.com/spf13/cobra"
3840
"github.com/stretchr/testify/assert"
@@ -1863,3 +1865,20 @@ func TestParseEncryptionSpec(t *testing.T) {
18631865
}
18641866
}
18651867
}
1868+
1869+
func TestWALFailoverWrapperRoundtrip(t *testing.T) {
1870+
defer leaktest.AfterTest(t)()
1871+
1872+
datadriven.RunTest(t, datapathutils.TestDataPath(t, "wal-failover-config"), func(t *testing.T, d *datadriven.TestData) string {
1873+
var buf bytes.Buffer
1874+
for _, l := range strings.Split(d.Input, "\n") {
1875+
cfg := walFailoverWrapper{cfg: &storageconfig.WALFailover{}}
1876+
if err := cfg.Set(l); err != nil {
1877+
fmt.Fprintf(&buf, "err: %s\n", err)
1878+
continue
1879+
}
1880+
fmt.Fprintln(&buf, cfg.String())
1881+
}
1882+
return buf.String()
1883+
})
1884+
}

pkg/cli/flags_util.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,3 +706,28 @@ func getAbsoluteFSPath(fieldName string, p string) (string, error) {
706706
}
707707
return ret, nil
708708
}
709+
710+
// walFailoverWrapper implements pflag.Value for the wal failover flag.
711+
type walFailoverWrapper struct {
712+
cfg *storageconfig.WALFailover
713+
}
714+
715+
var _ pflag.Value = (*walFailoverWrapper)(nil)
716+
717+
// Type implements the pflag.Value interface.
718+
func (c *walFailoverWrapper) Type() string { return "string" }
719+
720+
// String implements the pflag.Value interface.
721+
func (c *walFailoverWrapper) String() string {
722+
return c.cfg.String()
723+
}
724+
725+
// Set implements the pflag.Value interface.
726+
func (c *walFailoverWrapper) Set(s string) error {
727+
cfg, err := storageconfig.ParseWALFailover(s)
728+
if err != nil {
729+
return err
730+
}
731+
*c.cfg = cfg
732+
return nil
733+
}
File renamed without changes.

pkg/storage/open_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ func TestWALFailover(t *testing.T) {
101101

102102
var cfg storageconfig.WALFailover
103103
if flagStr != "" {
104-
if err := cfg.Set(flagStr); err != nil {
104+
var err error
105+
cfg, err = storageconfig.ParseWALFailover(flagStr)
106+
if err != nil {
105107
return fmt.Sprintf("error parsing flag: %q", err)
106108
}
107109
}

pkg/storage/storageconfig/wal_failover.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ type ExternalPath struct {
4343
Encryption *EncryptionOptions
4444
}
4545

46+
// IsSet returns whether the path was provided.
47+
func (e ExternalPath) IsSet() bool { return e.Path != "" }
48+
4649
// WALFailover is the configuration for write-ahead log (WAL) failover, used
4750
// to temporarily write WALs to a separate location when disk
4851
// stalls are encountered.
@@ -62,10 +65,13 @@ type WALFailover struct {
6265
PrevPath ExternalPath
6366
}
6467

65-
// Type implements the pflag.Value interface.
66-
func (c *WALFailover) Type() string { return "string" }
67-
6868
// String implements fmt.Stringer.
69+
//
70+
// Representation:
71+
// - DefaultMode: ""
72+
// - Disabled: "disabled[,prev_path=<prev_path>]"
73+
// - AmongStores: "among-stores"
74+
// - ToExplicitPath: "path=<path>[,prev_path=<prev_path>]"
6975
func (c *WALFailover) String() string {
7076
return redact.StringWithoutMarkers(c)
7177
}
@@ -95,15 +101,18 @@ func (c *WALFailover) SafeFormat(p redact.SafePrinter, _ rune) {
95101
}
96102
}
97103

98-
// Set implements the pflag.Value interface.
99-
func (c *WALFailover) Set(s string) error {
104+
// ParseWALFailover parses a string in the format produced by String().
105+
//
106+
// Used to parse the --wal-failover command-line flag.
107+
func ParseWALFailover(s string) (WALFailover, error) {
108+
var c WALFailover
100109
switch {
101110
case strings.HasPrefix(s, "disabled"):
102111
c.Mode = WALFailoverDisabled
103112
var ok bool
104113
c.Path.Path, c.PrevPath.Path, ok = parseWALFailoverPathFields(strings.TrimPrefix(s, "disabled"))
105114
if !ok || c.Path.IsSet() {
106-
return errors.Newf("invalid disabled --wal-failover setting: %s "+
115+
return WALFailover{}, errors.Newf("invalid disabled --wal-failover setting: %s "+
107116
"expect disabled[,prev_path=<prev_path>]", s)
108117
}
109118
case s == "among-stores":
@@ -113,14 +122,14 @@ func (c *WALFailover) Set(s string) error {
113122
var ok bool
114123
c.Path.Path, c.PrevPath.Path, ok = parseWALFailoverPathFields(s)
115124
if !ok || !c.Path.IsSet() {
116-
return errors.Newf("invalid path --wal-failover setting: %s "+
125+
return WALFailover{}, errors.Newf("invalid path --wal-failover setting: %s "+
117126
"expect path=<path>[,prev_path=<prev_path>]", s)
118127
}
119128
default:
120-
return errors.Newf("invalid --wal-failover setting: %s "+
129+
return WALFailover{}, errors.Newf("invalid --wal-failover setting: %s "+
121130
"(possible values: disabled, among-stores, path=<path>)", s)
122131
}
123-
return nil
132+
return c, nil
124133
}
125134

126135
func parseWALFailoverPathFields(s string) (path, prevPath string, ok bool) {
@@ -144,6 +153,3 @@ func parseWALFailoverPathFields(s string) (path, prevPath string, ok bool) {
144153
prevPath = strings.TrimPrefix(s, ",prev_path=")
145154
return path, prevPath, true
146155
}
147-
148-
// IsSet returns whether or not the path was provided.
149-
func (e ExternalPath) IsSet() bool { return e.Path != "" }

0 commit comments

Comments
 (0)