diff --git a/.gitignore b/.gitignore index a6e9fe8d5..00c77e6d0 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,4 @@ coverage.txt gitversion.go drummer-data nodehost-data +.idea diff --git a/config/config.go b/config/config.go index 430d4536e..a8e390ec6 100644 --- a/config/config.go +++ b/config/config.go @@ -27,6 +27,7 @@ import ( "time" "github.com/cockroachdb/errors" + "github.com/lni/goutils/netutil" "github.com/lni/goutils/stringutil" @@ -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 { @@ -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 { @@ -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 diff --git a/go.mod b/go.mod index 2e5fcf289..982415956 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index f9b91f275..5aed9f382 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/nodehost.go b/nodehost.go index bc46bece4..70a8681b7 100644 --- a/nodehost.go +++ b/nodehost.go @@ -65,6 +65,7 @@ import ( "time" "github.com/cockroachdb/errors" + "github.com/lni/goutils/logutil" "github.com/lni/goutils/syncutil" @@ -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, @@ -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 { @@ -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()