Skip to content

Commit

Permalink
update more errors
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Leung <rleungx@gmail.com>
  • Loading branch information
rleungx committed Aug 31, 2020
1 parent a779355 commit 1712dda
Show file tree
Hide file tree
Showing 24 changed files with 79 additions and 57 deletions.
3 changes: 1 addition & 2 deletions pkg/dashboard/adapter/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (

"github.com/pingcap-incubator/tidb-dashboard/pkg/apiserver"
"github.com/pingcap/kvproto/pkg/pdpb"
"go.uber.org/zap"

"github.com/pingcap/log"
"github.com/tikv/pd/pkg/errs"
Expand Down Expand Up @@ -106,7 +105,7 @@ func (m *Manager) updateInfo() {

var err error
if m.members, err = cluster.GetMembers(m.srv.GetClient()); err != nil {
log.Warn("failed to get members", zap.Error(err))
log.Warn("failed to get members", errs.ZapError(err))
m.members = nil
return
}
Expand Down
12 changes: 11 additions & 1 deletion pkg/errs/errno.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,15 @@ var (

// ioutil error
var (
ErrIORead = errors.Normalize("IO read err", errors.RFCCodeText("PD:ioutil:ErrIORead"))
ErrIORead = errors.Normalize("IO read error", errors.RFCCodeText("PD:ioutil:ErrIORead"))
)

// netstat error
var (
ErrNetstatTCPSocks = errors.Normalize("TCP socks error", errors.RFCCodeText("PD:netstat:ErrNetstatTCPSocks"))
)

// hex error
var (
ErrHexDecodingString = errors.Normalize("decode string %s error", errors.RFCCodeText("PD:hex:ErrHexDecodingString"))
)
3 changes: 3 additions & 0 deletions pkg/errs/errs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import (

// ZapError is used to make the log output eaiser.
func ZapError(err error, causeError ...error) zap.Field {
if err == nil {
return zap.Skip()
}
if e, ok := err.(*errors.Error); ok {
if len(causeError) >= 1 {
err = e.Wrap(causeError[0]).FastGenWithCause()
Expand Down
6 changes: 3 additions & 3 deletions pkg/tempurl/check_env_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ package tempurl
import (
"github.com/cakturk/go-netstat/netstat"
"github.com/pingcap/log"
"go.uber.org/zap"
"github.com/tikv/pd/pkg/errs"
)

func environmentCheck(addr string) bool {
valid, err := checkAddr(addr[len("http://"):])
if err != nil {
log.Error("check port status failed", zap.Error(err))
log.Error("check port status failed", errs.ZapError(err))
return false
}
return valid
Expand All @@ -34,7 +34,7 @@ func checkAddr(addr string) (bool, error) {
return s.RemoteAddr.String() == addr || s.LocalAddr.String() == addr
})
if err != nil {
return false, err
return false, errs.ErrNetstatTCPSocks.Wrap(err).FastGenWithCause()
}
return len(tabs) < 1, nil
}
4 changes: 2 additions & 2 deletions plugin/scheduler_example/evict_leader.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ import (
"github.com/pingcap/errors"
"github.com/pingcap/log"
"github.com/tikv/pd/pkg/apiutil"
"github.com/tikv/pd/pkg/errs"
"github.com/tikv/pd/server/core"
"github.com/tikv/pd/server/schedule"
"github.com/tikv/pd/server/schedule/filter"
"github.com/tikv/pd/server/schedule/operator"
"github.com/tikv/pd/server/schedule/opt"
"github.com/tikv/pd/server/schedulers"
"github.com/unrolled/render"
"go.uber.org/zap"
)

const (
Expand Down Expand Up @@ -226,7 +226,7 @@ func (s *evictLeaderScheduler) Schedule(cluster opt.Cluster) []*operator.Operato
}
op, err := operator.CreateTransferLeaderOperator(EvictLeaderType, cluster, region, region.GetLeader().GetStoreId(), target.GetID(), operator.OpLeader)
if err != nil {
log.Debug("fail to create evict leader operator", zap.Error(err))
log.Debug("fail to create evict leader operator", errs.ZapError(err))
continue

}
Expand Down
4 changes: 2 additions & 2 deletions server/cluster/cluster_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func (c *RaftCluster) HandleReportSplit(request *pdpb.ReportSplitRequest) (*pdpb
log.Warn("report split region is invalid",
zap.Stringer("left-region", core.RegionToHexMeta(left)),
zap.Stringer("right-region", core.RegionToHexMeta(right)),
zap.Error(err))
errs.ZapError(err))
return nil, err
}

Expand All @@ -215,7 +215,7 @@ func (c *RaftCluster) HandleBatchReportSplit(request *pdpb.ReportBatchSplitReque
if err != nil {
log.Warn("report batch split region is invalid",
zap.Stringer("region-meta", hrm),
zap.Error(err))
errs.ZapError(err))
return nil, err
}
last := len(regions) - 1
Expand Down
2 changes: 1 addition & 1 deletion server/cluster/coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ func (c *coordinator) runScheduler(s *scheduleController) {
case <-s.Ctx().Done():
log.Info("scheduler has been stopped",
zap.String("scheduler-name", s.GetName()),
zap.Error(s.Ctx().Err()))
errs.ZapError(s.Ctx().Err()))
return
}
}
Expand Down
3 changes: 2 additions & 1 deletion server/core/basic_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/log"
"github.com/tikv/pd/pkg/errs"
"github.com/tikv/pd/pkg/slice"
"github.com/tikv/pd/server/schedule/storelimit"
"go.uber.org/zap"
Expand Down Expand Up @@ -329,7 +330,7 @@ func (bc *BasicCluster) PutRegion(region *RegionInfo) []*RegionInfo {
func (bc *BasicCluster) CheckAndPutRegion(region *RegionInfo) []*RegionInfo {
origin, err := bc.PreCheckPutRegion(region)
if err != nil {
log.Debug("region is stale", zap.Error(err), zap.Stringer("origin", origin.GetMeta()))
log.Debug("region is stale", zap.Stringer("origin", origin.GetMeta()), errs.ZapError(err))
// return the state region to delete.
return []*RegionInfo{region}
}
Expand Down
2 changes: 1 addition & 1 deletion server/election/leadership.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (ls *Leadership) Campaign(leaseTimeout int64, leaderData string) error {
If(clientv3.Compare(clientv3.CreateRevision(ls.leaderKey), "=", 0)).
Then(clientv3.OpPut(ls.leaderKey, leaderData, clientv3.WithLease(ls.getLease().ID))).
Commit()
log.Info("check campaign resp", zap.Any("resp", resp), zap.Error(err))
log.Info("check campaign resp", zap.Any("resp", resp))
if err != nil {
return errors.WithStack(err)
}
Expand Down
3 changes: 2 additions & 1 deletion server/election/lease.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"github.com/pingcap/errors"
"github.com/pingcap/log"
"github.com/tikv/pd/pkg/errs"
"github.com/tikv/pd/pkg/etcdutil"
"go.etcd.io/etcd/clientv3"
"go.uber.org/zap"
Expand Down Expand Up @@ -125,7 +126,7 @@ func (l *lease) keepAliveWorker(ctx context.Context, interval time.Duration) <-c
defer cancel()
res, err := l.lease.KeepAliveOnce(ctx1, l.ID)
if err != nil {
log.Warn("lease keep alive failed", zap.Error(err), zap.String("purpose", l.Purpose))
log.Warn("lease keep alive failed", zap.String("purpose", l.Purpose), errs.ZapError(err))
return
}
if res.TTL > 0 {
Expand Down
15 changes: 8 additions & 7 deletions server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,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/config"
"github.com/tikv/pd/server/core"
Expand Down Expand Up @@ -457,7 +458,7 @@ func (h *Handler) AddTransferLeaderOperator(regionID uint64, storeID uint64) err

op, err := operator.CreateTransferLeaderOperator("admin-transfer-leader", c, region, region.GetLeader().GetStoreId(), newLeader.GetStoreId(), operator.OpAdmin)
if err != nil {
log.Debug("fail to create transfer leader operator", zap.Error(err))
log.Debug("fail to create transfer leader operator", errs.ZapError(err))
return err
}
if ok := c.GetOperatorController().AddOperator(op); !ok {
Expand Down Expand Up @@ -505,7 +506,7 @@ func (h *Handler) AddTransferRegionOperator(regionID uint64, storeIDs map[uint64

op, err := operator.CreateMoveRegionOperator("admin-move-region", c, region, operator.OpAdmin, peers)
if err != nil {
log.Debug("fail to create move region operator", zap.Error(err))
log.Debug("fail to create move region operator", errs.ZapError(err))
return err
}
if ok := c.GetOperatorController().AddOperator(op); !ok {
Expand Down Expand Up @@ -542,7 +543,7 @@ func (h *Handler) AddTransferPeerOperator(regionID uint64, fromStoreID, toStoreI
newPeer := &metapb.Peer{StoreId: toStoreID, Role: oldPeer.GetRole()}
op, err := operator.CreateMovePeerOperator("admin-move-peer", c, region, operator.OpAdmin, fromStoreID, newPeer)
if err != nil {
log.Debug("fail to create move peer operator", zap.Error(err))
log.Debug("fail to create move peer operator", errs.ZapError(err))
return err
}
if ok := c.GetOperatorController().AddOperator(op); !ok {
Expand Down Expand Up @@ -588,7 +589,7 @@ func (h *Handler) AddAddPeerOperator(regionID uint64, toStoreID uint64) error {
newPeer := &metapb.Peer{StoreId: toStoreID}
op, err := operator.CreateAddPeerOperator("admin-add-peer", c, region, newPeer, operator.OpAdmin)
if err != nil {
log.Debug("fail to create add peer operator", zap.Error(err))
log.Debug("fail to create add peer operator", errs.ZapError(err))
return err
}
if ok := c.GetOperatorController().AddOperator(op); !ok {
Expand All @@ -611,7 +612,7 @@ func (h *Handler) AddAddLearnerOperator(regionID uint64, toStoreID uint64) error

op, err := operator.CreateAddPeerOperator("admin-add-learner", c, region, newPeer, operator.OpAdmin)
if err != nil {
log.Debug("fail to create add learner operator", zap.Error(err))
log.Debug("fail to create add learner operator", errs.ZapError(err))
return err
}
if ok := c.GetOperatorController().AddOperator(op); !ok {
Expand All @@ -638,7 +639,7 @@ func (h *Handler) AddRemovePeerOperator(regionID uint64, fromStoreID uint64) err

op, err := operator.CreateRemovePeerOperator("admin-remove-peer", c, operator.OpAdmin, region, fromStoreID)
if err != nil {
log.Debug("fail to create move peer operator", zap.Error(err))
log.Debug("fail to create move peer operator", errs.ZapError(err))
return err
}
if ok := c.GetOperatorController().AddOperator(op); !ok {
Expand Down Expand Up @@ -680,7 +681,7 @@ func (h *Handler) AddMergeRegionOperator(regionID uint64, targetID uint64) error

ops, err := operator.CreateMergeRegionOperator("admin-merge-region", c, region, target, operator.OpAdmin)
if err != nil {
log.Debug("fail to create merge region operator", zap.Error(err))
log.Debug("fail to create merge region operator", errs.ZapError(err))
return err
}
if ok := c.GetOperatorController().AddOperator(ops...); !ok {
Expand Down
3 changes: 2 additions & 1 deletion server/heartbeat_streams.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,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/pkg/logutil"
"github.com/tikv/pd/server/cluster"
"github.com/tikv/pd/server/core"
Expand Down Expand Up @@ -120,7 +121,7 @@ func (s *heartbeatStreams) run() {
if err := stream.Send(keepAlive); err != nil {
log.Warn("send keepalive message fail, store maybe disconnected",
zap.Uint64("target-store-id", storeID),
zap.Error(err))
errs.ZapError(err))
delete(s.streams, storeID)
regionHeartbeatCounter.WithLabelValues(storeAddress, storeLabel, "keepalive", "err").Inc()
} else {
Expand Down
10 changes: 5 additions & 5 deletions server/kv/etcd_kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ func (kv *etcdKVBase) Save(key, value string) error {
txn := NewSlowLogTxn(kv.client)
resp, err := txn.Then(clientv3.OpPut(key, value)).Commit()
if err != nil {
err = errs.ErrEtcdKVPut.Wrap(err).GenWithStackByCause()
log.Error("save to etcd meet error", zap.String("key", key), zap.String("value", value), errs.ZapError(err))
return err
e := errs.ErrEtcdKVPut.Wrap(err).GenWithStackByCause()
log.Error("save to etcd meet error", zap.String("key", key), zap.String("value", value), errs.ZapError(e))
return e
}
if !resp.Succeeded {
return errs.ErrEtcdTxn.FastGenByArgs()
Expand Down Expand Up @@ -158,9 +158,9 @@ func (t *SlowLogTxn) Commit() (*clientv3.TxnResponse, error) {
cost := time.Since(start)
if cost > slowRequestTime {
log.Warn("txn runs too slow",
zap.Error(err),
zap.Reflect("response", resp),
zap.Duration("cost", cost))
zap.Duration("cost", cost),
errs.ZapError(err))
}
label := "success"
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion server/region_syncer/history_buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"sync"

"github.com/pingcap/log"
"github.com/tikv/pd/pkg/errs"
"github.com/tikv/pd/server/core"
"github.com/tikv/pd/server/kv"
"go.uber.org/zap"
Expand Down Expand Up @@ -151,6 +152,6 @@ func (h *historyBuffer) persist() {
regionSyncerStatus.WithLabelValues("last_index").Set(float64(h.nextIndex()))
err := h.kv.Save(historyKey, strconv.FormatUint(h.nextIndex(), 10))
if err != nil {
log.Warn("persist history index failed", zap.Uint64("persist-index", h.nextIndex()), zap.Error(err))
log.Warn("persist history index failed", zap.Uint64("persist-index", h.nextIndex()), errs.ZapError(err))
}
}
15 changes: 8 additions & 7 deletions server/replication/replication_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

pb "github.com/pingcap/kvproto/pkg/replication_modepb"
"github.com/pingcap/log"
"github.com/tikv/pd/pkg/errs"
"github.com/tikv/pd/server/config"
"github.com/tikv/pd/server/core"
"github.com/tikv/pd/server/schedule/opt"
Expand Down Expand Up @@ -217,15 +218,15 @@ func (m *ModeManager) drSwitchToAsync() error {
func (m *ModeManager) drSwitchToAsyncWithLock() error {
id, err := m.cluster.AllocID()
if err != nil {
log.Warn("failed to switch to async state", zap.String("replicate-mode", modeDRAutoSync), zap.Error(err))
log.Warn("failed to switch to async state", zap.String("replicate-mode", modeDRAutoSync), errs.ZapError(err))
return err
}
dr := drAutoSyncStatus{State: drStateAsync, StateID: id}
if err := m.drPersistStatus(dr); err != nil {
return err
}
if err := m.storage.SaveReplicationStatus(modeDRAutoSync, dr); err != nil {
log.Warn("failed to switch to async state", zap.String("replicate-mode", modeDRAutoSync), zap.Error(err))
log.Warn("failed to switch to async state", zap.String("replicate-mode", modeDRAutoSync), errs.ZapError(err))
return err
}
m.drAutoSync = dr
Expand All @@ -242,15 +243,15 @@ func (m *ModeManager) drSwitchToSyncRecover() error {
func (m *ModeManager) drSwitchToSyncRecoverWithLock() error {
id, err := m.cluster.AllocID()
if err != nil {
log.Warn("failed to switch to sync_recover state", zap.String("replicate-mode", modeDRAutoSync), zap.Error(err))
log.Warn("failed to switch to sync_recover state", zap.String("replicate-mode", modeDRAutoSync), errs.ZapError(err))
return err
}
dr := drAutoSyncStatus{State: drStateSyncRecover, StateID: id, RecoverStartTime: time.Now()}
if err := m.drPersistStatus(dr); err != nil {
return err
}
if err = m.storage.SaveReplicationStatus(modeDRAutoSync, dr); err != nil {
log.Warn("failed to switch to sync_recover state", zap.String("replicate-mode", modeDRAutoSync), zap.Error(err))
log.Warn("failed to switch to sync_recover state", zap.String("replicate-mode", modeDRAutoSync), errs.ZapError(err))
return err
}
m.drAutoSync = dr
Expand All @@ -264,15 +265,15 @@ func (m *ModeManager) drSwitchToSync() error {
defer m.Unlock()
id, err := m.cluster.AllocID()
if err != nil {
log.Warn("failed to switch to sync state", zap.String("replicate-mode", modeDRAutoSync), zap.Error(err))
log.Warn("failed to switch to sync state", zap.String("replicate-mode", modeDRAutoSync), errs.ZapError(err))
return err
}
dr := drAutoSyncStatus{State: drStateSync, StateID: id}
if err := m.drPersistStatus(dr); err != nil {
return err
}
if err := m.storage.SaveReplicationStatus(modeDRAutoSync, dr); err != nil {
log.Warn("failed to switch to sync state", zap.String("replicate-mode", modeDRAutoSync), zap.Error(err))
log.Warn("failed to switch to sync state", zap.String("replicate-mode", modeDRAutoSync), errs.ZapError(err))
return err
}
m.drAutoSync = dr
Expand All @@ -286,7 +287,7 @@ func (m *ModeManager) drPersistStatus(status drAutoSyncStatus) error {
defer cancel()
data, _ := json.Marshal(status)
if err := m.fileReplicater.ReplicateFileToAllMembers(ctx, drStatusFile, data); err != nil {
log.Warn("failed to switch state", zap.String("replicate-mode", modeDRAutoSync), zap.String("new-state", status.State), zap.Error(err))
log.Warn("failed to switch state", zap.String("replicate-mode", modeDRAutoSync), zap.String("new-state", status.State), errs.ZapError(err))
// Throw away the error to make it possible to switch to async when
// primary and dr DC are disconnected. This will result in the
// inability to accurately determine whether data is fully
Expand Down
4 changes: 2 additions & 2 deletions server/schedule/checker/learner_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ package checker

import (
"github.com/pingcap/log"
"github.com/tikv/pd/pkg/errs"
"github.com/tikv/pd/server/core"
"github.com/tikv/pd/server/schedule/operator"
"github.com/tikv/pd/server/schedule/opt"
"go.uber.org/zap"
)

// LearnerChecker ensures region has a learner will be promoted.
Expand All @@ -41,7 +41,7 @@ func (l *LearnerChecker) Check(region *core.RegionInfo) *operator.Operator {
}
op, err := operator.CreatePromoteLearnerOperator("promote-learner", l.cluster, region, p)
if err != nil {
log.Debug("fail to create promote learner operator", zap.Error(err))
log.Debug("fail to create promote learner operator", errs.ZapError(err))
return nil
}
return op
Expand Down
Loading

0 comments on commit 1712dda

Please sign in to comment.