From 3590133ef45d547ed57d4645bfd7a2b997411a0d Mon Sep 17 00:00:00 2001 From: redwrasse Date: Tue, 27 Aug 2024 22:19:10 -0700 Subject: [PATCH] Replace err == with errors.Is(err, ) syntax. Signed-off-by: redwrasse --- server/etcdserver/api/v3compactor/periodic.go | 3 ++- .../etcdserver/api/v3discovery/discovery.go | 4 ++-- server/etcdserver/server.go | 11 +++++---- server/etcdserver/v3_server.go | 23 +++++++++---------- tests/framework/e2e/etcd_process.go | 2 +- tests/integration/clientv3/kv_test.go | 5 ++-- .../integration/clientv3/lease/lease_test.go | 3 ++- tests/integration/clientv3/util.go | 7 +++--- tests/integration/v3_failover_test.go | 3 ++- tools/mod/go.mod | 2 +- 10 files changed, 34 insertions(+), 29 deletions(-) diff --git a/server/etcdserver/api/v3compactor/periodic.go b/server/etcdserver/api/v3compactor/periodic.go index 98fbc381bb87..067f69ec36dc 100644 --- a/server/etcdserver/api/v3compactor/periodic.go +++ b/server/etcdserver/api/v3compactor/periodic.go @@ -16,6 +16,7 @@ package v3compactor import ( "context" + "errors" "sync" "time" @@ -139,7 +140,7 @@ func (pc *Periodic) Run() { ) startTime := pc.clock.Now() _, err := pc.c.Compact(pc.ctx, &pb.CompactionRequest{Revision: rev}) - if err == nil || err == mvcc.ErrCompacted { + if err == nil || errors.Is(err, mvcc.ErrCompacted) { pc.lg.Info( "completed auto periodic compaction", zap.Int64("revision", rev), diff --git a/server/etcdserver/api/v3discovery/discovery.go b/server/etcdserver/api/v3discovery/discovery.go index 3e01b7cf6458..028876cd1906 100644 --- a/server/etcdserver/api/v3discovery/discovery.go +++ b/server/etcdserver/api/v3discovery/discovery.go @@ -192,7 +192,7 @@ func newDiscovery(lg *zap.Logger, dcfg *DiscoveryConfig, id types.ID) (*discover func (d *discovery) getCluster() (string, error) { cls, clusterSize, rev, err := d.checkCluster() if err != nil { - if err == ErrFullCluster { + if errors.Is(err, ErrFullCluster) { return cls.getInitClusterStr(clusterSize) } return "", err @@ -303,7 +303,7 @@ func (d *discovery) checkClusterRetry() (*clusterInfo, int, int64, error) { func (d *discovery) checkCluster() (*clusterInfo, int, int64, error) { clusterSize, err := d.getClusterSize() if err != nil { - if err == ErrSizeNotFound || err == ErrBadSizeKey { + if errors.Is(err, ErrSizeNotFound) || errors.Is(err, ErrBadSizeKey) { return nil, 0, 0, err } diff --git a/server/etcdserver/server.go b/server/etcdserver/server.go index 0600a31b8960..7f230a2d2cdc 100644 --- a/server/etcdserver/server.go +++ b/server/etcdserver/server.go @@ -17,6 +17,7 @@ package etcdserver import ( "context" "encoding/json" + errorspkg "errors" "expvar" "fmt" "math" @@ -1445,7 +1446,7 @@ func (s *EtcdServer) PromoteMember(ctx context.Context, id uint64) ([]*membershi learnerPromoteSucceed.Inc() return resp, nil } - if err != errors.ErrNotLeader { + if !errorspkg.Is(err, errors.ErrNotLeader) { learnerPromoteFailed.WithLabelValues(err.Error()).Inc() return resp, err } @@ -1464,13 +1465,13 @@ func (s *EtcdServer) PromoteMember(ctx context.Context, id uint64) ([]*membershi return resp, nil } // If member promotion failed, return early. Otherwise keep retry. - if err == errors.ErrLearnerNotReady || err == membership.ErrIDNotFound || err == membership.ErrMemberNotLearner { + if errorspkg.Is(err, errors.ErrLearnerNotReady) || errorspkg.Is(err, membership.ErrIDNotFound) || errorspkg.Is(err, membership.ErrMemberNotLearner) { return nil, err } } } - if cctx.Err() == context.DeadlineExceeded { + if errorspkg.Is(cctx.Err(), context.DeadlineExceeded) { return nil, errors.ErrTimeout } return nil, errors.ErrCanceled @@ -2149,7 +2150,7 @@ func (s *EtcdServer) snapshot(snapi uint64, confState raftpb.ConfState) { if err != nil { // the snapshot was done asynchronously with the progress of raft. // raft might have already got a newer snapshot. - if err == raft.ErrSnapOutOfDate { + if errorspkg.Is(err, raft.ErrSnapOutOfDate) { return } lg.Panic("failed to create snapshot", zap.Error(err)) @@ -2190,7 +2191,7 @@ func (s *EtcdServer) snapshot(snapi uint64, confState raftpb.ConfState) { if err != nil { // the compaction was done asynchronously with the progress of raft. // raft log might already been compact. - if err == raft.ErrCompacted { + if errorspkg.Is(err, raft.ErrCompacted) { return } lg.Panic("failed to compact", zap.Error(err)) diff --git a/server/etcdserver/v3_server.go b/server/etcdserver/v3_server.go index 6c5eefcb31aa..e8c8040c65da 100644 --- a/server/etcdserver/v3_server.go +++ b/server/etcdserver/v3_server.go @@ -19,13 +19,8 @@ import ( "context" "encoding/base64" "encoding/binary" - "strconv" - "time" - + errorspkg "errors" "github.com/gogo/protobuf/proto" - "go.uber.org/zap" - "golang.org/x/crypto/bcrypt" - pb "go.etcd.io/etcd/api/v3/etcdserverpb" "go.etcd.io/etcd/api/v3/version" "go.etcd.io/etcd/pkg/v3/traceutil" @@ -38,6 +33,10 @@ import ( "go.etcd.io/etcd/server/v3/lease/leasehttp" "go.etcd.io/etcd/server/v3/storage/mvcc" "go.etcd.io/raft/v3" + "go.uber.org/zap" + "golang.org/x/crypto/bcrypt" + "strconv" + "time" ) const ( @@ -313,7 +312,7 @@ func (s *EtcdServer) LeaseRenew(ctx context.Context, id lease.LeaseID) (int64, e for _, url := range leader.PeerURLs { lurl := url + leasehttp.LeasePrefix ttl, err := leasehttp.RenewHTTP(cctx, id, lurl, s.peerRt) - if err == nil || err == lease.ErrLeaseNotFound { + if err == nil || errorspkg.Is(err, lease.ErrLeaseNotFound) { return ttl, err } } @@ -321,7 +320,7 @@ func (s *EtcdServer) LeaseRenew(ctx context.Context, id lease.LeaseID) (int64, e time.Sleep(50 * time.Millisecond) } - if cctx.Err() == context.DeadlineExceeded { + if errorspkg.Is(cctx.Err(), context.DeadlineExceeded) { return -1, errors.ErrTimeout } return -1, errors.ErrCanceled @@ -402,13 +401,13 @@ func (s *EtcdServer) leaseTimeToLive(ctx context.Context, r *pb.LeaseTimeToLiveR if err == nil { return resp.LeaseTimeToLiveResponse, nil } - if err == lease.ErrLeaseNotFound { + if errorspkg.Is(err, lease.ErrLeaseNotFound) { return nil, err } } } - if cctx.Err() == context.DeadlineExceeded { + if errorspkg.Is(cctx.Err(), context.DeadlineExceeded) { return nil, errors.ErrTimeout } return nil, errors.ErrCanceled @@ -854,7 +853,7 @@ func (s *EtcdServer) linearizableReadLoop() { } func isStopped(err error) bool { - return err == raft.ErrStopped || err == errors.ErrStopped + return errorspkg.Is(err, raft.ErrStopped) || errorspkg.Is(err, errors.ErrStopped) } func (s *EtcdServer) requestCurrentIndex(leaderChangedNotifier <-chan struct{}, requestID uint64) (uint64, error) { @@ -942,7 +941,7 @@ func (s *EtcdServer) sendReadIndex(requestIndex uint64) error { cctx, cancel := context.WithTimeout(context.Background(), s.Cfg.ReqTimeout()) err := s.r.ReadIndex(cctx, ctxToSend) cancel() - if err == raft.ErrStopped { + if errorspkg.Is(err, raft.ErrStopped) { return err } if err != nil { diff --git a/tests/framework/e2e/etcd_process.go b/tests/framework/e2e/etcd_process.go index f913efffeec3..b55ef9e90a62 100644 --- a/tests/framework/e2e/etcd_process.go +++ b/tests/framework/e2e/etcd_process.go @@ -307,7 +307,7 @@ func (ep *EtcdServerProcess) IsRunning() bool { } exitCode, err := ep.proc.ExitCode() - if err == expect.ErrProcessRunning { + if errors.Is(err, expect.ErrProcessRunning) { return true } diff --git a/tests/integration/clientv3/kv_test.go b/tests/integration/clientv3/kv_test.go index 223058a03c9a..cfabfbdea9b3 100644 --- a/tests/integration/clientv3/kv_test.go +++ b/tests/integration/clientv3/kv_test.go @@ -17,6 +17,7 @@ package clientv3test import ( "bytes" "context" + "errors" "fmt" "os" "reflect" @@ -443,7 +444,7 @@ func TestKVCompact(t *testing.T) { t.Fatalf("couldn't compact kv space (%v)", err) } _, err = kv.Compact(ctx, 7) - if err == nil || err != rpctypes.ErrCompacted { + if err == nil || !errors.Is(err, rpctypes.ErrCompacted) { t.Fatalf("error got %v, want %v", err, rpctypes.ErrCompacted) } @@ -472,7 +473,7 @@ func TestKVCompact(t *testing.T) { } _, err = kv.Compact(ctx, 1000) - if err == nil || err != rpctypes.ErrFutureRev { + if err == nil || !errors.Is(err, rpctypes.ErrFutureRev) { t.Fatalf("error got %v, want %v", err, rpctypes.ErrFutureRev) } } diff --git a/tests/integration/clientv3/lease/lease_test.go b/tests/integration/clientv3/lease/lease_test.go index f23ed672865a..1264645afb12 100644 --- a/tests/integration/clientv3/lease/lease_test.go +++ b/tests/integration/clientv3/lease/lease_test.go @@ -16,6 +16,7 @@ package lease_test import ( "context" + "errors" "fmt" "reflect" "sort" @@ -761,7 +762,7 @@ func TestV3LeaseFailureOverlap(t *testing.T) { go func() { defer wg.Done() err := updown(n) - if err == nil || err == rpctypes.ErrTimeoutDueToConnectionLost { + if err == nil || errors.Is(err, rpctypes.ErrTimeoutDueToConnectionLost) { return } t.Error(err) diff --git a/tests/integration/clientv3/util.go b/tests/integration/clientv3/util.go index 67d1fd59cc91..623e575be114 100644 --- a/tests/integration/clientv3/util.go +++ b/tests/integration/clientv3/util.go @@ -16,6 +16,7 @@ package clientv3test import ( "context" + "errors" "fmt" "strings" "testing" @@ -64,7 +65,7 @@ func IsClientTimeout(err error) bool { if err == nil { return false } - if err == context.DeadlineExceeded { + if errors.Is(err, context.DeadlineExceeded) { return true } ev, ok := status.FromError(err) @@ -79,7 +80,7 @@ func IsCanceled(err error) bool { if err == nil { return false } - if err == context.Canceled { + if errors.Is(err, context.Canceled) { return true } ev, ok := status.FromError(err) @@ -94,7 +95,7 @@ func IsUnavailable(err error) bool { if err == nil { return false } - if err == context.Canceled { + if errors.Is(err, context.Canceled) { return true } ev, ok := status.FromError(err) diff --git a/tests/integration/v3_failover_test.go b/tests/integration/v3_failover_test.go index cf6c73f24e75..d71dd267b85f 100644 --- a/tests/integration/v3_failover_test.go +++ b/tests/integration/v3_failover_test.go @@ -18,6 +18,7 @@ import ( "bytes" "context" "crypto/tls" + "errors" "testing" "time" @@ -167,7 +168,7 @@ func getWithRetries(t *testing.T, cli *clientv3.Client, key, val string, retryCo func shouldRetry(err error) bool { if clientv3test.IsClientTimeout(err) || clientv3test.IsServerCtxTimeout(err) || - err == rpctypes.ErrTimeout || err == rpctypes.ErrTimeoutDueToLeaderFail { + errors.Is(err, rpctypes.ErrTimeout) || errors.Is(err, rpctypes.ErrTimeoutDueToLeaderFail) { return true } return false diff --git a/tools/mod/go.mod b/tools/mod/go.mod index ae1ae99f70cd..a1458db3062c 100644 --- a/tools/mod/go.mod +++ b/tools/mod/go.mod @@ -1,6 +1,6 @@ module go.etcd.io/etcd/tools/v3 -go 1.22 +go 1.22.1 toolchain go1.22.6