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

server: Refine log error format (#2873) #2911

Merged
merged 12 commits into from
Sep 7, 2020
10 changes: 7 additions & 3 deletions server/core/region_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import (
"sync"
"time"

"github.com/pingcap/errors"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/log"
"github.com/tikv/pd/pkg/errs"
"github.com/tikv/pd/server/kv"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -140,7 +140,7 @@ func loadRegions(kv kv.Base, f func(region *RegionInfo) []*RegionInfo) error {
for _, s := range res {
region := &metapb.Region{}
if err := region.Unmarshal([]byte(s)); err != nil {
return errors.WithStack(err)
return errs.ErrProtoUnmarshal.Wrap(err).GenWithStackByArgs()
}

nextID = region.GetId() + 1
Expand Down Expand Up @@ -181,5 +181,9 @@ func (s *RegionStorage) Close() error {
log.Error("meet error before close the region storage", zap.Error(err))
}
s.regionStorageCancel()
return errors.WithStack(s.LeveldbKV.Close())
err = s.LeveldbKV.Close()
if err != nil {
return errs.ErrLevelDBClose.Wrap(err).GenWithStackByArgs()
}
return nil
}
12 changes: 6 additions & 6 deletions server/core/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func (s *Storage) LoadConfig(cfg interface{}) (bool, error) {
}
err = json.Unmarshal([]byte(value), cfg)
if err != nil {
return false, errors.WithStack(err)
return false, errs.ErrJSONUnmarshal.Wrap(err).GenWithStackByCause()
}
return true, nil
}
Expand Down Expand Up @@ -264,7 +264,7 @@ func (s *Storage) LoadRules(f func(k, v string)) (bool, error) {
func (s *Storage) SaveReplicationStatus(mode string, status interface{}) error {
value, err := json.Marshal(status)
if err != nil {
return errors.WithStack(err)
return errs.ErrJSONMarshal.Wrap(err).GenWithStackByArgs()
}
return s.Save(path.Join(replicationPath, mode), string(value))
}
Expand All @@ -280,7 +280,7 @@ func (s *Storage) LoadReplicationStatus(mode string, status interface{}) (bool,
}
err = json.Unmarshal([]byte(v), status)
if err != nil {
return false, errors.WithStack(err)
return false, errs.ErrJSONUnmarshal.Wrap(err).GenWithStackByArgs()
}
return true, nil
}
Expand All @@ -305,7 +305,7 @@ func (s *Storage) LoadComponent(component interface{}) (bool, error) {
}
err = json.Unmarshal([]byte(v), component)
if err != nil {
return false, errors.WithStack(err)
return false, errs.ErrJSONUnmarshal.Wrap(err).GenWithStackByArgs()
}
return true, nil
}
Expand All @@ -323,7 +323,7 @@ func (s *Storage) LoadStores(f func(store *StoreInfo)) error {
for _, str := range res {
store := &metapb.Store{}
if err := store.Unmarshal([]byte(str)); err != nil {
return errors.WithStack(err)
return errs.ErrProtoUnmarshal.Wrap(err).GenWithStackByArgs()
}
leaderWeight, err := s.loadFloatWithDefaultValue(s.storeLeaderWeightPath(store.GetId()), 1.0)
if err != nil {
Expand Down Expand Up @@ -364,7 +364,7 @@ func (s *Storage) loadFloatWithDefaultValue(path string, def float64) (float64,
}
val, err := strconv.ParseFloat(res, 64)
if err != nil {
return 0, errors.WithStack(err)
return 0, errs.ErrStrconvParseFloat.Wrap(err).GenWithStackByArgs()
}
return val, nil
}
Expand Down
3 changes: 2 additions & 1 deletion server/grpc_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/kvproto/pkg/pdpb"
"github.com/pingcap/log"
"github.com/tikv/pd/pkg/errs"
"github.com/tikv/pd/server/cluster"
"github.com/tikv/pd/server/core"
"go.uber.org/zap"
Expand Down Expand Up @@ -404,7 +405,7 @@ func (s *Server) RegionHeartbeat(stream pdpb.PD_RegionHeartbeatServer) error {

region := core.RegionFromHeartbeat(request)
if region.GetLeader() == nil {
log.Error("invalid request, the leader is nil", zap.Reflect("reqeust", request))
log.Error("invalid request, the leader is nil", zap.Reflect("request", request), errs.ZapError(errs.ErrLeaderNil))
continue
}
if region.GetID() == 0 {
Expand Down
6 changes: 3 additions & 3 deletions server/heartbeat_streams.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,15 @@ func (s *heartbeatStreams) run() {
if store == nil {
log.Error("failed to get store",
zap.Uint64("region-id", msg.RegionId),
zap.Uint64("store-id", storeID))
zap.Uint64("store-id", storeID), errs.ZapError(errs.ErrGetSourceStore))
delete(s.streams, storeID)
continue
}
storeAddress := store.GetAddress()
if stream, ok := s.streams[storeID]; ok {
if err := stream.Send(msg); err != nil {
log.Error("send heartbeat message fail",
zap.Uint64("region-id", msg.RegionId), zap.Error(err))
zap.Uint64("region-id", msg.RegionId), errs.ZapError(errs.ErrGRPCSend.Wrap(err).GenWithStackByArgs()))
delete(s.streams, storeID)
regionHeartbeatCounter.WithLabelValues(storeAddress, storeLabel, "push", "err").Inc()
} else {
Expand All @@ -112,7 +112,7 @@ func (s *heartbeatStreams) run() {
for storeID, stream := range s.streams {
store := s.cluster.GetStore(storeID)
if store == nil {
log.Error("failed to get store", zap.Uint64("store-id", storeID))
log.Error("failed to get store", zap.Uint64("store-id", storeID), errs.ZapError(errs.ErrGetSourceStore))
delete(s.streams, storeID)
continue
}
Expand Down
6 changes: 3 additions & 3 deletions server/id/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
package id

import (
"github.com/tikv/pd/pkg/errs"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix it

"path"
"sync"

"github.com/pingcap/errors"
"github.com/pingcap/log"
"github.com/tikv/pd/pkg/etcdutil"
"github.com/tikv/pd/pkg/typeutil"
Expand Down Expand Up @@ -101,10 +101,10 @@ func (alloc *AllocatorImpl) generate() (uint64, error) {
t := txn.If(append([]clientv3.Cmp{cmp}, clientv3.Compare(clientv3.Value(leaderPath), "=", alloc.member))...)
resp, err := t.Then(clientv3.OpPut(key, string(value))).Commit()
if err != nil {
return 0, err
return 0, errs.ErrEtcdTxn.Wrap(err).GenWithStackByArgs()
}
if !resp.Succeeded {
return 0, errors.New("generate id failed, we may not leader")
return 0, errs.ErrEtcdTxn.FastGenByArgs()
}

log.Info("idAllocator allocates a new id", zap.Uint64("alloc-id", end))
Expand Down
2 changes: 1 addition & 1 deletion server/kv/etcd_kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (kv *etcdKVBase) Load(key string) (string, error) {
if n := len(resp.Kvs); n == 0 {
return "", nil
} else if n > 1 {
return "", errors.Errorf("load more than one kvs: key %v kvs %v", key, n)
return "", errs.ErrEtcdKVGetResponse.GenWithStackByArgs(resp.Kvs)
}
return string(resp.Kvs[0].Value), nil
}
Expand Down
3 changes: 1 addition & 2 deletions server/member/lease.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"sync/atomic"
"time"

"github.com/pingcap/errors"
"github.com/pingcap/log"
"github.com/tikv/pd/pkg/errs"
"go.etcd.io/etcd/clientv3"
Expand Down Expand Up @@ -50,7 +49,7 @@ func (l *LeaderLease) Grant(leaseTimeout int64) error {
leaseResp, err := l.lease.Grant(ctx, leaseTimeout)
cancel()
if err != nil {
return errors.WithStack(err)
return errs.ErrEtcdGrantLease.Wrap(err).GenWithStackByCause()
}
if cost := time.Since(start); cost > slowRequestTime {
log.Warn("lease grants too slow", zap.Duration("cost", cost))
Expand Down