Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify config validation errors #218

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ coverage.txt
gitversion.go
drummer-data
nodehost-data
.idea
22 changes: 15 additions & 7 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"time"

"github.com/cockroachdb/errors"

"github.com/lni/goutils/netutil"
"github.com/lni/goutils/stringutil"

Expand Down Expand Up @@ -474,7 +475,7 @@ type TargetValidator func(string) bool

// RaftAddressValidator is the validator used to validate user specified
// RaftAddress values.
type RaftAddressValidator func(string) bool
type RaftAddressValidator func(string) error

// LogDBFactory is the interface used for creating custom logdb modules.
type LogDBFactory interface {
Expand Down Expand Up @@ -566,11 +567,13 @@ func (c *NodeHostConfig) Validate() error {
return errors.New("gossip service not configured")
}
validate := c.GetRaftAddressValidator()
if !validate(c.RaftAddress) {
return errors.New("invalid NodeHost address")
if err := validate(c.RaftAddress); err != nil {
return errors.Wrapf(err, "invalid RaftAddress '%s'", c.RaftAddress)
}
if len(c.ListenAddress) > 0 && !validate(c.ListenAddress) {
return errors.New("invalid ListenAddress")
if len(c.ListenAddress) > 0 {
if err := validate(c.ListenAddress); err != nil {
return errors.Wrapf(err, "invalid ListenAddress '%s'", c.ListenAddress)
}
}
if !c.Gossip.IsEmpty() {
if err := c.Gossip.Validate(); err != nil {
Expand Down Expand Up @@ -735,9 +738,14 @@ func (c *NodeHostConfig) GetTargetValidator() TargetValidator {
// NodeHostConfig instance.
func (c *NodeHostConfig) GetRaftAddressValidator() RaftAddressValidator {
if c.Expert.TransportFactory != nil {
return c.Expert.TransportFactory.Validate
return func(value string) error {
if !c.Expert.TransportFactory.Validate(value) {
return errors.New("invalid value")
}
return nil
}
}
return stringutil.IsValidAddress
return stringutil.IsValidAddressErr
}

// IsValidAddress returns a boolean value indicating whether the input address
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ require (
)

go 1.14
replace github.com/lni/goutils v1.3.1-0.20210517080819-7f56813dc438 => github.com/vitalyisaev2/goutils v1.3.1-0.20211116144502-b2caaddb8815
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPU
github.com/valyala/histogram v1.0.1 h1:FzA7n2Tz/wKRMejgu3PV1vw3htAklTjjuoI6z3d4KDg=
github.com/valyala/histogram v1.0.1/go.mod h1:lQy0xA4wUz2+IUnf97SivorsJIp8FxsnRd6x25q7Mto=
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio=
github.com/vitalyisaev2/goutils v1.3.1-0.20211116144502-b2caaddb8815 h1:qPStcAla6uF0cITLeDaOx1+cg9p+BD3ThnkUAP70H10=
github.com/vitalyisaev2/goutils v1.3.1-0.20211116144502-b2caaddb8815/go.mod h1:wfcJxczc56vX0c1GHUDmt8/fWY/nZzZpErf7JjrHxho=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
Expand Down
15 changes: 8 additions & 7 deletions nodehost.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ import (
"time"

"github.com/cockroachdb/errors"

"github.com/lni/goutils/logutil"
"github.com/lni/goutils/syncutil"

Expand Down Expand Up @@ -279,14 +280,14 @@ var firstError = utils.FirstError
func NewNodeHost(nhConfig config.NodeHostConfig) (*NodeHost, error) {
logBuildTagsAndVersion()
if err := nhConfig.Validate(); err != nil {
return nil, err
return nil, errors.Wrap(err, "validate config")
}
if err := nhConfig.Prepare(); err != nil {
return nil, err
return nil, errors.Wrap(err, "prepare config")
}
env, err := server.NewEnv(nhConfig, nhConfig.Expert.FS)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "server new env")
}
nh := &NodeHost{
env: env,
Expand Down Expand Up @@ -322,16 +323,16 @@ func NewNodeHost(nhConfig config.NodeHostConfig) (*NodeHost, error) {
plog.Infof("DeploymentID set to %d", did)
if err := nh.createLogDB(); err != nil {
nh.Close()
return nil, err
return nil, errors.Wrap(err, "create LogDB")
}
if err := nh.loadNodeHostID(); err != nil {
nh.Close()
return nil, err
return nil, errors.Wrap(err, "load node host id")
}
plog.Infof("NodeHost ID: %s", nh.id.String())
if err := nh.createNodeRegistry(); err != nil {
nh.Close()
return nil, err
return nil, errors.Wrap(err, "create node registry")
}
errorInjection := false
if nhConfig.Expert.FS != nil {
Expand All @@ -342,7 +343,7 @@ func NewNodeHost(nhConfig config.NodeHostConfig) (*NodeHost, error) {
nh.nhConfig.NotifyCommit, errorInjection, nh.env, nh.mu.logdb)
if err := nh.createTransport(); err != nil {
nh.Close()
return nil, err
return nil, errors.Wrap(err, "create transport")
}
nh.stopper.RunWorker(func() {
nh.nodeMonitorMain()
Expand Down