Skip to content

Commit

Permalink
*: optimize log and make their specification consistent (#2853)
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Leung <rleungx@gmail.com>
  • Loading branch information
rleungx authored Sep 1, 2020
1 parent 6322b8e commit 6876790
Show file tree
Hide file tree
Showing 32 changed files with 208 additions and 152 deletions.
14 changes: 7 additions & 7 deletions client/base_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (c *baseClient) leaderLoop() {
}

if err := c.updateLeader(); err != nil {
log.Error("[pd] failed updateLeader", errs.ZapError(errs.ErrUpdateLeader, err))
log.Error("[pd] failed updateLeader", errs.ZapError(err))
}
}
}
Expand Down Expand Up @@ -177,7 +177,7 @@ func (c *baseClient) initClusterID() error {
members, err := c.getMembers(timeoutCtx, u)
timeoutCancel()
if err != nil || members.GetHeader() == nil {
log.Warn("[pd] failed to get cluster id", zap.String("url", u), errs.ZapError(errs.ErrGetClusterID, err))
log.Warn("[pd] failed to get cluster id", zap.String("url", u), errs.ZapError(err))
continue
}
c.clusterID = members.GetHeader().GetClusterId()
Expand All @@ -191,7 +191,7 @@ func (c *baseClient) updateLeader() error {
ctx, cancel := context.WithTimeout(c.ctx, updateLeaderTimeout)
members, err := c.getMembers(ctx, u)
if err != nil {
log.Warn("[pd] cannot update leader", zap.String("address", u), errs.ZapError(errs.ErrUpdateLeader, err))
log.Warn("[pd] cannot update leader", zap.String("address", u), errs.ZapError(err))
}
cancel()
if err != nil || members.GetLeader() == nil || len(members.GetLeader().GetClientUrls()) == 0 {
Expand All @@ -205,7 +205,7 @@ func (c *baseClient) updateLeader() error {
c.updateURLs(members.GetMembers())
return c.switchLeader(members.GetLeader().GetClientUrls())
}
return errors.Errorf("failed to get leader from %v", c.urls)
return errs.ErrClientGetLeader.FastGenByArgs(c.urls)
}

func (c *baseClient) getMembers(ctx context.Context, url string) (*pdpb.GetMembersResponse, error) {
Expand All @@ -216,7 +216,7 @@ func (c *baseClient) getMembers(ctx context.Context, url string) (*pdpb.GetMembe
members, err := pdpb.NewPDClient(cc).GetMembers(ctx, &pdpb.GetMembersRequest{})
if err != nil {
attachErr := errors.Errorf("error:%s target:%s status:%s", err, cc.Target(), cc.GetState().String())
return nil, errors.WithStack(attachErr)
return nil, errs.ErrClientGetMember.Wrap(attachErr).GenWithStackByCause()
}
return members, nil
}
Expand Down Expand Up @@ -273,13 +273,13 @@ func (c *baseClient) getOrCreateGRPCConn(addr string) (*grpc.ClientConn, error)
KeyPath: c.security.KeyPath,
}.ToTLSConfig()
if err != nil {
return nil, errors.WithStack(err)
return nil, err
}
dctx, cancel := context.WithTimeout(c.ctx, dialTimeout)
defer cancel()
cc, err := grpcutil.GetClientConn(dctx, addr, tlsCfg, c.gRPCDialOptions...)
if err != nil {
return nil, errors.WithStack(err)
return nil, err
}
c.connMu.Lock()
defer c.connMu.Unlock()
Expand Down
6 changes: 3 additions & 3 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func (c *client) tsCancelLoop() {
case d := <-c.tsDeadlineCh:
select {
case <-d.timer:
log.Error("tso request is canceled due to timeout", zap.Error(errs.ErrGetTSO.FastGenByArgs()))
log.Error("tso request is canceled due to timeout", errs.ZapError(errs.ErrClientGetTSOTimeout))
d.cancel()
case <-d.done:
case <-ctx.Done():
Expand Down Expand Up @@ -255,7 +255,7 @@ func (c *client) tsLoop() {
return
default:
}
log.Error("[pd] create tso stream error", errs.ZapError(errs.ErrCreateTSOStream, err))
log.Error("[pd] create tso stream error", errs.ZapError(errs.ErrClientCreateTSOStream, err))
c.ScheduleCheckLeader()
cancel()
c.revokeTSORequest(errors.WithStack(err))
Expand Down Expand Up @@ -302,7 +302,7 @@ func (c *client) tsLoop() {
return
default:
}
log.Error("[pd] getTS error", errs.ZapError(errs.ErrGetTSO, err))
log.Error("[pd] getTS error", errs.ZapError(errs.ErrClientGetTSO, err))
c.ScheduleCheckLeader()
cancel()
stream, cancel = nil, nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/apiutil/serverapi/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,14 @@ func (p *customReverseProxies) ServeHTTP(w http.ResponseWriter, r *http.Request)

resp, err := p.client.Do(r)
if err != nil {
log.Error("request failed", errs.ZapError(errs.ErrRequestHTTP, err))
log.Error("request failed", errs.ZapError(errs.ErrSendRequest, err))
continue
}

b, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
log.Error("read failed", errs.ZapError(errs.ErrReadHTTPBody, err))
log.Error("read failed", errs.ZapError(errs.ErrIORead, err))
continue
}

Expand Down
7 changes: 3 additions & 4 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 Expand Up @@ -196,7 +195,7 @@ func (m *Manager) startService() {
return
}
if err := m.service.Start(m.ctx); err != nil {
log.Error("Can not start dashboard server", errs.ZapError(errs.ErrStartDashboard, err))
log.Error("Can not start dashboard server", errs.ZapError(errs.ErrDashboardStart, err))
} else {
log.Info("Dashboard server is started")
}
Expand All @@ -207,7 +206,7 @@ func (m *Manager) stopService() {
return
}
if err := m.service.Stop(context.Background()); err != nil {
log.Error("Stop dashboard server error", errs.ZapError(errs.ErrStopDashboard, err))
log.Error("Stop dashboard server error", errs.ZapError(errs.ErrDashboardStop, err))
} else {
log.Info("Dashboard server is stopped")
}
Expand Down
123 changes: 74 additions & 49 deletions pkg/errs/errno.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,38 @@ package errs

import "github.com/pingcap/errors"

// The internal error which is generated in PD project.
// tso errors
var (
ErrInvalidTimestamp = errors.Normalize("invalid timestamp", errors.RFCCodeText("PD:tso:ErrInvalidTimestamp"))
ErrLogicOverflow = errors.Normalize("logic part overflow", errors.RFCCodeText("PD:tso:ErrLogicOverflow"))
ErrIncorrectSystemTime = errors.Normalize("incorrect system time", errors.RFCCodeText("PD:tso:ErrIncorrectSystemTime"))
)

// adapter errors
// member errors
var (
ErrStartDashboard = errors.Normalize("start dashboard failed", errors.RFCCodeText("PD:adapter:ErrStartDashboard"))
ErrStopDashboard = errors.Normalize("stop dashboard failed", errors.RFCCodeText("PD:adapter:ErrStopDashboard"))
ErrEtcdLeaderNotFound = errors.Normalize("etcd leader not found", errors.RFCCodeText("PD:member:ErrEtcdLeaderNotFound"))
ErrMarshalLeader = errors.Normalize("marshal leader failed", errors.RFCCodeText("PD:member:ErrMarshalLeader"))
)

// member errors
// client errors
var (
ErretcdLeaderNotFound = errors.Normalize("etcd leader not found", errors.RFCCodeText("PD:member:ErretcdLeaderNotFound"))
ErrGetLeader = errors.Normalize("get leader failed", errors.RFCCodeText("PD:member:ErrGetLeader"))
ErrDeleteLeaderKey = errors.Normalize("delete leader key failed", errors.RFCCodeText("PD:member:ErrDeleteLeaderKey"))
ErrLoadLeaderPriority = errors.Normalize("load leader priority failed", errors.RFCCodeText("PD:member:ErrLoadLeaderPriority"))
ErrLoadetcdLeaderPriority = errors.Normalize("load etcd leader priority failed", errors.RFCCodeText("PD:member:ErrLoadetcdLeaderPriority"))
ErrTransferetcdLeader = errors.Normalize("transfer etcd leader failed", errors.RFCCodeText("PD:member:ErrTransferetcdLeader"))
ErrWatcherCancel = errors.Normalize("watcher canceled", errors.RFCCodeText("PD:member:ErrWatcherCancel"))
ErrMarshalLeader = errors.Normalize("marshal leader failed", errors.RFCCodeText("PD:member:ErrMarshalLeader"))
ErrClientCreateTSOStream = errors.Normalize("create TSO stream failed", errors.RFCCodeText("PD:client:ErrClientCreateTSOStream"))
ErrClientGetTSOTimeout = errors.Normalize("get TSO timeout", errors.RFCCodeText("PD:client:ErrClientGetTSOTimeout"))
ErrClientGetTSO = errors.Normalize("get TSO failed", errors.RFCCodeText("PD:client:ErrClientGetTSO"))
ErrClientGetLeader = errors.Normalize("get leader from %v error", errors.RFCCodeText("PD:client:ErrClientGetLeader"))
ErrClientGetMember = errors.Normalize("get member failed", errors.RFCCodeText("PD:client:ErrClientGetMember"))
)

// client errors
// scheduler errors
var (
ErrCloseGRPCConn = errors.Normalize("close gRPC connection failed", errors.RFCCodeText("PD:client:ErrCloseGRPCConn"))
ErrUpdateLeader = errors.Normalize("update leader failed", errors.RFCCodeText("PD:client:ErrUpdateLeader"))
ErrCreateTSOStream = errors.Normalize("create TSO stream failed", errors.RFCCodeText("PD:client:ErrCreateTSOStream"))
ErrGetTSO = errors.Normalize("get TSO failed", errors.RFCCodeText("PD:client:ErrGetTSO"))
ErrGetClusterID = errors.Normalize("get cluster ID failed", errors.RFCCodeText("PD:client:ErrGetClusterID"))
ErrGetSourceStore = errors.Normalize("failed to get the source store", errors.RFCCodeText("PD:scheduler:ErrGetSourceStore"))
ErrSchedulerExisted = errors.Normalize("scheduler existed", errors.RFCCodeText("PD:scheduler:ErrSchedulerExisted"))
ErrSchedulerNotFound = errors.Normalize("scheduler not found", errors.RFCCodeText("PD:scheduler:ErrSchedulerNotFound"))
ErrScheduleConfigNotExist = errors.Normalize("the config does not exist", errors.RFCCodeText("PD:scheduler:ErrScheduleConfigNotExist"))
ErrSchedulerConfig = errors.Normalize("wrong scheduler config %s", errors.RFCCodeText("PD:scheduler:ErrSchedulerConfig"))
ErrCacheOverflow = errors.Normalize("cache overflow", errors.RFCCodeText("PD:scheduler:ErrCacheOverflow"))
ErrInternalGrowth = errors.Normalize("unknown interval growth type error", errors.RFCCodeText("PD:scheduler:ErrInternalGrowth"))
)

// placement errors
Expand All @@ -57,19 +57,6 @@ var (
ErrBuildRuleList = errors.Normalize("build rule list failed, %s", errors.RFCCodeText("PD:placement:ErrBuildRuleList"))
)

// kv errors
var (
ErrEtcdKVSave = errors.Normalize("etcd KV save failed", errors.RFCCodeText("PD:kv:ErrEtcdKVSave"))
ErrEtcdKVRemove = errors.Normalize("etcd KV remove failed", errors.RFCCodeText("PD:kv:ErrEtcdKVRemove"))
)

// apiutil errors
var (
ErrRequestHTTP = errors.Normalize("send HTTP request failed", errors.RFCCodeText("PD:apiutil:ErrRequestHTTP"))
ErrReadHTTPBody = errors.Normalize("read HTTP body failed", errors.RFCCodeText("PD:apiutil:ErrReadHTTPBody"))
ErrWriteHTTPBody = errors.Normalize("write HTTP body failed", errors.RFCCodeText("PD:apiutil:ErrWriteHTTPBody"))
)

// cluster errors
var (
ErrPersistStore = errors.Normalize("failed to persist store", errors.RFCCodeText("PD:cluster:ErrPersistStore"))
Expand All @@ -79,43 +66,81 @@ var (
ErrDeleteStore = errors.Normalize("failed to delete store", errors.RFCCodeText("PD:cluster:ErrDeleteStore"))
ErrPersistClusterVersion = errors.Normalize("persist cluster version meet error", errors.RFCCodeText("PD:cluster:ErrPersistClusterVersion"))
ErrGetMembers = errors.Normalize("get members failed", errors.RFCCodeText("PD:cluster:ErrGetMembers"))
// TODO: ErrNewHTTPRequest may not be suitable to put in cluster category
ErrNewHTTPRequest = errors.Normalize("new HTTP request failed", errors.RFCCodeText("PD:cluster:ErrNewHTTPRequest"))
)

// metricutil errors
// grpcutil errors
var (
ErrPushGateway = errors.Normalize("push metrics to gateway failed", errors.RFCCodeText("PD:metricutil:ErrPushGateway"))
ErrSecurityConfig = errors.Normalize("security config error: %s", errors.RFCCodeText("PD:grpcutil:ErrSecurityConfig"))
)

// etcdutil errors
// The third-party project error.
// url errors
var (
ErrLoadValue = errors.Normalize("load value from etcd failed", errors.RFCCodeText("PD:etcdutil:ErrLoadValue"))
ErrGetCluster = errors.Normalize("get cluster from remote peer failed", errors.RFCCodeText("PD:etcdutil:ErrGetCluster"))
ErrURLParse = errors.Normalize("parse url error", errors.RFCCodeText("PD:url:ErrURLParse"))
ErrQueryUnescape = errors.Normalize("inverse transformation of QueryEscape error", errors.RFCCodeText("PD:url:ErrQueryUnescape"))
)

// scheduler errors
// grpc errors
var (
ErrGetSourceStore = errors.Normalize("failed to get the source store", errors.RFCCodeText("PD:scheduler:ErrGetSourceStore"))
ErrSchedulerExisted = errors.Normalize("scheduler existed", errors.RFCCodeText("PD:scheduler:ErrSchedulerExisted"))
ErrSchedulerNotFound = errors.Normalize("scheduler not found", errors.RFCCodeText("PD:scheduler:ErrSchedulerNotFound"))
ErrScheduleConfigNotExist = errors.Normalize("the config does not exist", errors.RFCCodeText("PD:scheduler:ErrScheduleConfigNotExist"))
ErrSchedulerConfig = errors.Normalize("wrong scheduler config %s", errors.RFCCodeText("PD:scheduler:ErrSchedulerConfig"))
ErrCacheOverflow = errors.Normalize("cache overflow", errors.RFCCodeText("PD:scheduler:ErrCacheOverflow"))
ErrInternalGrowth = errors.Normalize("unknown interval growth type error", errors.RFCCodeText("PD:scheduler:ErrInternalGrowth"))
ErrGRPCDial = errors.Normalize("dial error", errors.RFCCodeText("PD:grpc:ErrGRPCDial"))
ErrCloseGRPCConn = errors.Normalize("close gRPC connection failed", errors.RFCCodeText("PD:grpc:ErrCloseGRPCConn"))
)

// proto errors
var (
ErrProtoUnmarshal = errors.Normalize("failed to unmarshal proto", errors.RFCCodeText("PD:proto:ErrProtoUnmarshal"))
)

// etcd errors
var (
ErrCloseEtcdClient = errors.Normalize("close etcd client failed", errors.RFCCodeText("PD:etcd:ErrCloseEtcdClient"))
ErrEtcdTxn = errors.Normalize("etcd Txn failed", errors.RFCCodeText("PD:etcd:ErrEtcdTxn"))
ErrEtcdKVPut = errors.Normalize("etcd KV put failed", errors.RFCCodeText("PD:etcd:ErrEtcdKVPut"))
ErrEtcdKVDelete = errors.Normalize("etcd KV delete failed", errors.RFCCodeText("PD:etcd:ErrEtcdKVDelete"))
ErrEtcdKVGet = errors.Normalize("etcd KV get failed", errors.RFCCodeText("PD:etcd:ErrEtcdKVGet"))
ErrEtcdKVGetResponse = errors.Normalize("etcd invalid get value response %v, must only one", errors.RFCCodeText("PD:etcd:ErrEtcdKVGetResponse"))
ErrEtcdGetCluster = errors.Normalize("etcd get cluster from remote peer failed", errors.RFCCodeText("PD:etcd:ErrEtcdGetCluster"))
ErrEtcdMoveLeader = errors.Normalize("etcd move leader error", errors.RFCCodeText("PD:etcd:ErrEtcdMoveLeader"))
ErrEtcdTLSConfig = errors.Normalize("etcd TLS config error", errors.RFCCodeText("PD:etcd:ErrEtcdTLSConfig"))
ErrEtcdGetProtoMsgWithModRev = errors.Normalize("etcd get proto message with mod rev error", errors.RFCCodeText("PD:etcd:ErrEtcdGetProtoMsgWithModRev"))
ErrEtcdWatcherCancel = errors.Normalize("watcher canceled", errors.RFCCodeText("PD:etcd:ErrEtcdWatcherCancel"))
ErrCloseEtcdClient = errors.Normalize("close etcd client failed", errors.RFCCodeText("PD:etcd:ErrCloseEtcdClient"))
)

// dashboard errors
var (
ErrDashboardStart = errors.Normalize("start dashboard failed", errors.RFCCodeText("PD:dashboard:ErrDashboardStart"))
ErrDashboardStop = errors.Normalize("stop dashboard failed", errors.RFCCodeText("PD:dashboard:ErrDashboardStop"))
)

// strconv errors
var (
ErrStrconvParseInt = errors.Normalize("parse int error", errors.RFCCodeText("PD:strconv:ErrStrconvParseInt"))
ErrStrconvParseUint = errors.Normalize("parse uint error", errors.RFCCodeText("PD:strconv:ErrStrconvParseUint"))
)

// url errors
// prometheus errors
var (
ErrQueryUnescape = errors.Normalize("inverse transformation of QueryEscape error", errors.RFCCodeText("PD:url:ErrQueryUnescape"))
ErrPrometheusPushMetrics = errors.Normalize("push metrics to gateway failed", errors.RFCCodeText("PD:prometheus:ErrPrometheusPushMetrics"))
)

// http errors
var (
ErrSendRequest = errors.Normalize("send HTTP request failed", errors.RFCCodeText("PD:http:ErrSendRequest"))
ErrWriteHTTPBody = errors.Normalize("write HTTP body failed", errors.RFCCodeText("PD:http:ErrWriteHTTPBody"))
ErrNewHTTPRequest = errors.Normalize("new HTTP request failed", errors.RFCCodeText("PD:http:ErrNewHTTPRequest"))
)

// ioutil error
var (
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
17 changes: 16 additions & 1 deletion pkg/errs/errs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package errs
import (
"bytes"
"fmt"
"strconv"
"strings"
"testing"

Expand Down Expand Up @@ -89,7 +90,6 @@ func (s *testErrorSuite) TestError(c *C) {
err := errors.New("test error")
log.Error("test", ZapError(ErrInvalidTimestamp, err))
rfc = `[error="[PD:tso:ErrInvalidTimestamp] test error"]`
fmt.Println(lg.Message())
c.Assert(strings.Contains(lg.Message(), rfc), IsTrue)
}

Expand Down Expand Up @@ -127,3 +127,18 @@ func (s *testErrorSuite) TestZapError(c *C) {
log.Info("test", ZapError(err1))
log.Info("test", ZapError(err1, err))
}

func (s *testErrorSuite) TestErrorWithStack(c *C) {
conf := &log.Config{Level: "debug", File: log.FileLogConfig{}, DisableTimestamp: true}
lg := newZapTestLogger(conf)
log.ReplaceGlobals(lg.Logger, nil)

_, err := strconv.ParseUint("-42", 10, 64)
log.Error("test", ZapError(ErrStrconvParseInt.Wrap(err).GenWithStackByCause()))
m1 := lg.Message()
log.Error("test", zap.Error(errors.WithStack(err)))
m2 := lg.Message()
// This test is based on line number and the first log is in line 141, the second is in line 142.
// So they have the same length stack. Move this test to another place need to change the corresponding length.
c.Assert(len(m1[strings.Index(m1, "[stack="):]), Equals, len(m2[strings.Index(m2, "[stack="):]))
}
Loading

0 comments on commit 6876790

Please sign in to comment.