Skip to content

Commit

Permalink
fix: enable errorlint in server directory (part 1)
Browse files Browse the repository at this point in the history
Signed-off-by: Matthieu MOREL <[email protected]>
  • Loading branch information
mmorel-35 committed Oct 22, 2024
1 parent bb381d4 commit add6d6f
Show file tree
Hide file tree
Showing 18 changed files with 67 additions and 55 deletions.
6 changes: 3 additions & 3 deletions server/etcdmain/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ func newConfig() *config {

func (cfg *config) parse(arguments []string) error {
perr := cfg.cf.flagSet.Parse(arguments)
switch perr {
case nil:
case flag.ErrHelp:
switch {
case errors.Is(perr, nil):
case errors.Is(perr, flag.ErrHelp):
fmt.Println(flagsline)
os.Exit(0)
default:
Expand Down
14 changes: 8 additions & 6 deletions server/etcdmain/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package etcdmain

import (
errorspkg "errors"
"fmt"
"os"
"runtime"
Expand Down Expand Up @@ -63,8 +64,8 @@ func startEtcdOrProxyV2(args []string) {
lg.Info("Running: ", zap.Strings("args", args))
if err != nil {
lg.Warn("failed to verify flags", zap.Error(err))
switch err {
case embed.ErrUnsetAdvertiseClientURLsFlag:
switch {
case errorspkg.Is(err, embed.ErrUnsetAdvertiseClientURLsFlag):
lg.Warn("advertise client URLs are not set", zap.Error(err))
}
os.Exit(1)
Expand Down Expand Up @@ -129,9 +130,10 @@ func startEtcdOrProxyV2(args []string) {
}

if err != nil {
if derr, ok := err.(*errors.DiscoveryError); ok {
switch derr.Err {
case v2discovery.ErrDuplicateID:
var derr *errors.DiscoveryError
if errorspkg.As(err, &derr) {
switch {
case errorspkg.Is(derr.Err, v2discovery.ErrDuplicateID):
lg.Warn(
"member has been registered with discovery service",
zap.String("name", cfg.ec.Name),
Expand All @@ -145,7 +147,7 @@ func startEtcdOrProxyV2(args []string) {
lg.Warn("check data dir if previous bootstrap succeeded")
lg.Warn("or use a new discovery token if previous bootstrap failed")

case v2discovery.ErrDuplicateName:
case errorspkg.Is(derr.Err, v2discovery.ErrDuplicateName):
lg.Warn(
"member with duplicated name has already been registered",
zap.String("discovery-token", cfg.ec.Durl),
Expand Down
9 changes: 5 additions & 4 deletions server/etcdserver/api/etcdhttp/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package etcdhttp

import (
"encoding/json"
errorspkg "errors"
"fmt"
"net/http"
"strconv"
Expand Down Expand Up @@ -138,12 +139,12 @@ func (h *peerMemberPromoteHandler) ServeHTTP(w http.ResponseWriter, r *http.Requ

resp, err := h.server.PromoteMember(r.Context(), id)
if err != nil {
switch err {
case membership.ErrIDNotFound:
switch {
case errorspkg.Is(err, membership.ErrIDNotFound):
http.Error(w, err.Error(), http.StatusNotFound)
case membership.ErrMemberNotLearner:
case errorspkg.Is(err, membership.ErrMemberNotLearner):
http.Error(w, err.Error(), http.StatusPreconditionFailed)
case errors.ErrLearnerNotReady:
case errorspkg.Is(err, errors.ErrLearnerNotReady):
http.Error(w, err.Error(), http.StatusPreconditionFailed)
default:
writeError(h.lg, w, r, err)
Expand Down
10 changes: 7 additions & 3 deletions server/etcdserver/api/etcdhttp/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package etcdhttp

import (
errorspkg "errors"
"net/http"

"go.uber.org/zap"
Expand Down Expand Up @@ -57,9 +58,12 @@ func writeError(lg *zap.Logger, w http.ResponseWriter, r *http.Request, err erro
}

default:
switch err {
case errors.ErrTimeoutDueToLeaderFail, errors.ErrTimeoutDueToConnectionLost, errors.ErrNotEnoughStartedMembers,
errors.ErrUnhealthy:
switch {
case
errorspkg.Is(err, errors.ErrTimeoutDueToLeaderFail),
errorspkg.Is(err, errors.ErrTimeoutDueToConnectionLost),
errorspkg.Is(err, errors.ErrNotEnoughStartedMembers),
errorspkg.Is(err, errors.ErrUnhealthy):
if lg != nil {
lg.Warn(
"v2 response error",
Expand Down
2 changes: 1 addition & 1 deletion server/etcdserver/api/membership/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@ func ValidateClusterAndAssignIDs(lg *zap.Logger, local *RaftCluster, existing *R
}
}
if !ok {
return fmt.Errorf("PeerURLs: no match found for existing member (%v, %v), last resolver error (%v)", ems[i].ID, ems[i].PeerURLs, err)
return fmt.Errorf("PeerURLs: no match found for existing member (%v, %v), last resolver error (%w)", ems[i].ID, ems[i].PeerURLs, err)
}
}
local.members = make(map[types.ID]*Member)
Expand Down
4 changes: 2 additions & 2 deletions server/etcdserver/api/membership/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ var (
)

func isKeyNotFound(err error) bool {
e, ok := err.(*v2error.Error)
return ok && e.ErrorCode == v2error.EcodeKeyNotFound
var e *v2error.Error
return errors.As(err, &e) && e.ErrorCode == v2error.EcodeKeyNotFound
}
4 changes: 2 additions & 2 deletions server/etcdserver/api/membership/storev2.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,14 @@ func nodeToMember(lg *zap.Logger, n *v2store.NodeExtern) (*Member, error) {
}
if data := attrs[raftAttrKey]; data != nil {
if err := json.Unmarshal(data, &m.RaftAttributes); err != nil {
return nil, fmt.Errorf("unmarshal raftAttributes error: %v", err)
return nil, fmt.Errorf("unmarshal raftAttributes error: %w", err)
}
} else {
return nil, fmt.Errorf("raftAttributes key doesn't exist")
}
if data := attrs[attrKey]; data != nil {
if err := json.Unmarshal(data, &m.Attributes); err != nil {
return m, fmt.Errorf("unmarshal attributes error: %v", err)
return m, fmt.Errorf("unmarshal attributes error: %w", err)
}
}
return m, nil
Expand Down
2 changes: 1 addition & 1 deletion server/etcdserver/api/v2store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ func (s *store) Update(nodePath string, newValue string, expireOpts TTLOptionSet
eNode := e.Node

if err := n.Write(newValue, nextIndex); err != nil {
return nil, fmt.Errorf("nodePath %v : %v", nodePath, err)
return nil, fmt.Errorf("nodePath %v : %w", nodePath, err)
}

if n.IsDir() {
Expand Down
8 changes: 6 additions & 2 deletions server/etcdserver/api/v2store/store_ttl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ func TestStoreUpdateValueTTL(t *testing.T) {
s.DeleteExpiredKeys(fc.Now())
e, err = s.Get("/foo", false, false)
assert.Nil(t, e)
assert.Equal(t, v2error.EcodeKeyNotFound, err.(*v2error.Error).ErrorCode)
var v2Err *v2error.Error
assert.ErrorAs(t, err, &v2Err)
assert.Equal(t, v2error.EcodeKeyNotFound, v2Err.ErrorCode)
}

// TestStoreUpdateDirTTL ensures that the store can update the TTL on a directory.
Expand All @@ -137,7 +139,9 @@ func TestStoreUpdateDirTTL(t *testing.T) {
s.DeleteExpiredKeys(fc.Now())
e, err = s.Get("/foo/bar", false, false)
assert.Nil(t, e)
assert.Equal(t, v2error.EcodeKeyNotFound, err.(*v2error.Error).ErrorCode)
var v2Err *v2error.Error
assert.ErrorAs(t, err, &v2Err)
assert.Equal(t, v2error.EcodeKeyNotFound, v2Err.ErrorCode)
}

// TestStoreWatchExpire ensures that the store can watch for key expiration.
Expand Down
8 changes: 4 additions & 4 deletions server/etcdserver/api/v3rpc/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,12 +273,12 @@ func (sws *serverWatchStream) recvLoop() error {
err := sws.isWatchPermitted(creq)
if err != nil {
var cancelReason string
switch err {
case auth.ErrInvalidAuthToken:
switch {
case errors.Is(err, auth.ErrInvalidAuthToken):
cancelReason = rpctypes.ErrGRPCInvalidAuthToken.Error()
case auth.ErrAuthOldRevision:
case errors.Is(err, auth.ErrAuthOldRevision):
cancelReason = rpctypes.ErrGRPCAuthOldRevision.Error()
case auth.ErrUserEmpty:
case errors.Is(err, auth.ErrUserEmpty):
cancelReason = rpctypes.ErrGRPCUserEmpty.Error()
default:
if !errors.Is(err, auth.ErrPermissionDenied) {
Expand Down
12 changes: 6 additions & 6 deletions server/etcdserver/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ func bootstrap(cfg config.ServerConfig) (b *bootstrappedServer, err error) {
}

if terr := fileutil.TouchDirAll(cfg.Logger, cfg.DataDir); terr != nil {
return nil, fmt.Errorf("cannot access data directory: %v", terr)
return nil, fmt.Errorf("cannot access data directory: %w", terr)
}

if terr := fileutil.TouchDirAll(cfg.Logger, cfg.MemberDir()); terr != nil {
return nil, fmt.Errorf("cannot access member directory: %v", terr)
return nil, fmt.Errorf("cannot access member directory: %w", terr)
}
ss := bootstrapSnapshot(cfg)
prt, err := rafthttp.NewRoundTripper(cfg.PeerTLSInfo, cfg.PeerDialTimeout())
Expand All @@ -85,7 +85,7 @@ func bootstrap(cfg config.ServerConfig) (b *bootstrappedServer, err error) {

if haveWAL {
if err = fileutil.IsDirWriteable(cfg.WALDir()); err != nil {
return nil, fmt.Errorf("cannot write to WAL directory: %v", err)
return nil, fmt.Errorf("cannot write to WAL directory: %w", err)
}
bwal = bootstrapWALFromSnapshot(cfg, backend.snapshot)
}
Expand Down Expand Up @@ -296,10 +296,10 @@ func bootstrapExistingClusterNoWAL(cfg config.ServerConfig, prt http.RoundTrippe
}
existingCluster, gerr := GetClusterFromRemotePeers(cfg.Logger, getRemotePeerURLs(cl, cfg.Name), prt)
if gerr != nil {
return nil, fmt.Errorf("cannot fetch cluster info from peer urls: %v", gerr)
return nil, fmt.Errorf("cannot fetch cluster info from peer urls: %w", gerr)
}
if err := membership.ValidateClusterAndAssignIDs(cfg.Logger, cl, existingCluster); err != nil {
return nil, fmt.Errorf("error validating peerURLs %s: %v", existingCluster, err)
return nil, fmt.Errorf("error validating peerURLs %s: %w", existingCluster, err)
}
if !isCompatibleWithCluster(cfg.Logger, cl, cl.MemberByName(cfg.Name).ID, prt, cfg.ReqTimeout()) {
return nil, fmt.Errorf("incompatible with current running cluster")
Expand Down Expand Up @@ -363,7 +363,7 @@ func bootstrapNewClusterNoWAL(cfg config.ServerConfig, prt http.RoundTripper) (*

func bootstrapClusterWithWAL(cfg config.ServerConfig, meta *snapshotMetadata) (*bootstrappedCluster, error) {
if err := fileutil.IsDirWriteable(cfg.MemberDir()); err != nil {
return nil, fmt.Errorf("cannot write to member directory: %v", err)
return nil, fmt.Errorf("cannot write to member directory: %w", err)
}

if cfg.ShouldDiscover() {
Expand Down
11 changes: 6 additions & 5 deletions server/etcdserver/corrupt.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -95,7 +96,7 @@ func (cm *corruptionChecker) InitialCheck() error {

h, _, err := cm.hasher.HashByRev(0)
if err != nil {
return fmt.Errorf("%s failed to fetch hash (%v)", cm.hasher.MemberID(), err)
return fmt.Errorf("%s failed to fetch hash (%w)", cm.hasher.MemberID(), err)
}
peers := cm.hasher.PeerHashByRev(h.Revision)
mismatch := 0
Expand Down Expand Up @@ -127,8 +128,8 @@ func (cm *corruptionChecker) InitialCheck() error {
}

if p.err != nil {
switch p.err {
case rpctypes.ErrFutureRev:
switch {
case errors.Is(p.err, rpctypes.ErrFutureRev):
cm.lg.Warn(
"cannot fetch hash from slow remote peer",
zap.String("local-member-id", cm.hasher.MemberID().String()),
Expand All @@ -139,7 +140,7 @@ func (cm *corruptionChecker) InitialCheck() error {
zap.Strings("remote-peer-endpoints", p.eps),
zap.Error(err),
)
case rpctypes.ErrCompacted:
case errors.Is(p.err, rpctypes.ErrCompacted):
cm.lg.Warn(
"cannot fetch hash from remote peer; local member is behind",
zap.String("local-member-id", cm.hasher.MemberID().String()),
Expand All @@ -150,7 +151,7 @@ func (cm *corruptionChecker) InitialCheck() error {
zap.Strings("remote-peer-endpoints", p.eps),
zap.Error(err),
)
case rpctypes.ErrClusterIDMismatch:
case errors.Is(p.err, rpctypes.ErrClusterIDMismatch):
cm.lg.Warn(
"cluster ID mismatch",
zap.String("local-member-id", cm.hasher.MemberID().String()),
Expand Down
12 changes: 6 additions & 6 deletions server/etcdserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2355,12 +2355,12 @@ func (s *EtcdServer) updateClusterVersionV3(ver string) {
_, err := s.raftRequest(ctx, pb.InternalRaftRequest{ClusterVersionSet: &req})
cancel()

switch err {
case nil:
switch {
case errorspkg.Is(err, nil):
lg.Info("cluster version is updated", zap.String("cluster-version", version.Cluster(ver)))
return

case errors.ErrStopped:
case errorspkg.Is(err, errors.ErrStopped):
lg.Warn("aborting cluster version update; server is stopped", zap.Error(err))
return

Expand Down Expand Up @@ -2391,11 +2391,11 @@ func (s *EtcdServer) monitorDowngrade() {
}

func (s *EtcdServer) parseProposeCtxErr(err error, start time.Time) error {
switch err {
case context.Canceled:
switch {
case errorspkg.Is(err, context.Canceled):
return errors.ErrCanceled

case context.DeadlineExceeded:
case errorspkg.Is(err, context.DeadlineExceeded):
s.leadTimeMu.RLock()
curLeadElected := s.leadElectedTime
s.leadTimeMu.RUnlock()
Expand Down
4 changes: 2 additions & 2 deletions server/lease/leasehttp/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func RenewHTTP(ctx context.Context, id lease.LeaseID, url string, rt http.RoundT

lresp := &pb.LeaseKeepAliveResponse{}
if err := lresp.Unmarshal(b); err != nil {
return -1, fmt.Errorf(`lease: %v. data = "%s"`, err, b)
return -1, fmt.Errorf(`lease: %w. data = "%s"`, err, b)
}
if lresp.ID != int64(id) {
return -1, fmt.Errorf("lease: renew id mismatch")
Expand Down Expand Up @@ -254,7 +254,7 @@ func TimeToLiveHTTP(ctx context.Context, id lease.LeaseID, keys bool, url string

lresp := &leasepb.LeaseInternalResponse{}
if err := lresp.Unmarshal(b); err != nil {
return nil, fmt.Errorf(`lease: %v. data = "%s"`, err, string(b))
return nil, fmt.Errorf(`lease: %w. data = "%s"`, err, string(b))
}
if lresp.LeaseTimeToLiveResponse.ID != int64(id) {
return nil, fmt.Errorf("lease: TTL id mismatch")
Expand Down
4 changes: 2 additions & 2 deletions server/storage/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ func newBackend(cfg config.ServerConfig, hooks backend.Hooks) backend.Backend {
func OpenSnapshotBackend(cfg config.ServerConfig, ss *snap.Snapshotter, snapshot raftpb.Snapshot, hooks *BackendHooks) (backend.Backend, error) {
snapPath, err := ss.DBFilePath(snapshot.Metadata.Index)
if err != nil {
return nil, fmt.Errorf("failed to find database snapshot file (%v)", err)
return nil, fmt.Errorf("failed to find database snapshot file (%w)", err)
}
if err := os.Rename(snapPath, cfg.BackendPath()); err != nil {
return nil, fmt.Errorf("failed to rename database snapshot file (%v)", err)
return nil, fmt.Errorf("failed to rename database snapshot file (%w)", err)
}
return OpenBackend(cfg, hooks), nil
}
Expand Down
4 changes: 2 additions & 2 deletions server/storage/mvcc/kvstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -665,12 +665,12 @@ func TestHashKVWithCompactedAndFutureRevisions(t *testing.T) {
}

_, _, errFutureRev := s.HashStorage().HashByRev(int64(rev + 1))
if errFutureRev != ErrFutureRev {
if !errors.Is(errFutureRev, ErrFutureRev) {
t.Error(errFutureRev)
}

_, _, errPastRev := s.HashStorage().HashByRev(int64(compactRev - 1))
if errPastRev != ErrCompacted {
if !errors.Is(errPastRev, ErrCompacted) {
t.Error(errPastRev)
}

Expand Down
4 changes: 2 additions & 2 deletions server/storage/schema/membership.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (s *membershipBackend) readMembersFromBackend() (map[types.ID]*membership.M
return nil
})
if err != nil {
return nil, nil, fmt.Errorf("couldn't read members from backend: %v", err)
return nil, nil, fmt.Errorf("couldn't read members from backend: %w", err)
}

err = tx.UnsafeForEach(MembersRemoved, func(k, v []byte) error {
Expand All @@ -111,7 +111,7 @@ func (s *membershipBackend) readMembersFromBackend() (map[types.ID]*membership.M
return nil
})
if err != nil {
return nil, nil, fmt.Errorf("couldn't read members_removed from backend: %v", err)
return nil, nil, fmt.Errorf("couldn't read members_removed from backend: %w", err)
}
return members, removed, nil
}
Expand Down
4 changes: 2 additions & 2 deletions server/storage/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ func Migrate(lg *zap.Logger, tx backend.BatchTx, w WALVersion, target semver.Ver
func UnsafeMigrate(lg *zap.Logger, tx backend.UnsafeReadWriter, w WALVersion, target semver.Version) error {
current, err := UnsafeDetectSchemaVersion(lg, tx)
if err != nil {
return fmt.Errorf("cannot detect storage schema version: %v", err)
return fmt.Errorf("cannot detect storage schema version: %w", err)
}
plan, err := newPlan(lg, current, target)
if err != nil {
return fmt.Errorf("cannot create migration plan: %v", err)
return fmt.Errorf("cannot create migration plan: %w", err)
}
if target.LessThan(current) {
minVersion := w.MinimalEtcdVersion()
Expand Down

0 comments on commit add6d6f

Please sign in to comment.