Skip to content

Commit

Permalink
*: refine member/tso/adapter logs (#2707)
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Leung <rleungx@gmail.com>
  • Loading branch information
rleungx authored Aug 3, 2020
1 parent 552de69 commit 52772d8
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 135 deletions.
5 changes: 3 additions & 2 deletions pkg/dashboard/adapter/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"go.uber.org/zap"

"github.com/pingcap/log"
"github.com/pingcap/pd/v4/pkg/errs"
"github.com/pingcap/pd/v4/pkg/logutil"
"github.com/pingcap/pd/v4/server"
"github.com/pingcap/pd/v4/server/cluster"
Expand Down Expand Up @@ -195,7 +196,7 @@ func (m *Manager) startService() {
return
}
if err := m.service.Start(m.ctx); err != nil {
log.Error("Can not start dashboard server", zap.Error(err))
log.Error("Can not start dashboard server", zap.Error(errs.ErrStartDashboard.FastGenByArgs()), zap.NamedError("cause", err))
} else {
log.Info("Dashboard server is started")
}
Expand All @@ -206,7 +207,7 @@ func (m *Manager) stopService() {
return
}
if err := m.service.Stop(context.Background()); err != nil {
log.Error("Stop dashboard server error", zap.Error(err))
log.Error("Stop dashboard server error", zap.Error(errs.ErrStopDashboard.FastGenByArgs()), zap.NamedError("cause", err))
} else {
log.Info("Dashboard server is stopped")
}
Expand Down
115 changes: 0 additions & 115 deletions pkg/errors/errors.go

This file was deleted.

51 changes: 51 additions & 0 deletions pkg/errs/errs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2020 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package errs

import "github.com/pingcap/errors"

var (
reg = errors.NewRegistry("PD")
// ClassTSO defines tso error class
ClassTSO = reg.RegisterErrorClass(1, "tso")
// ClassAdaptor defines adapter error class
ClassAdaptor = reg.RegisterErrorClass(2, "adapter")
// ClassMember defines member error class
ClassMember = reg.RegisterErrorClass(3, "member")
)

// tso errors
var (
ErrInvalidTimestamp = ClassTSO.DefineError().TextualCode("ErrInvalidTimestamp").MessageTemplate("invalid timestamp").Build()
ErrLogicOverflow = ClassTSO.DefineError().TextualCode("ErrLogicOverflow").MessageTemplate("logic part overflow").Build()
ErrIncorrectSystemTime = ClassTSO.DefineError().TextualCode("ErrIncorrectSystemTime").MessageTemplate("incorrect system time").Build()
)

// adapter errors
var (
ErrStartDashboard = ClassAdaptor.DefineError().TextualCode("ErrStartDashboard").MessageTemplate("fail to start dashboard").Build()
ErrStopDashboard = ClassAdaptor.DefineError().TextualCode("ErrStopDashboard").MessageTemplate("fail to stop dashboard").Build()
)

// member errors
var (
ErretcdLeaderNotFound = ClassMember.DefineError().TextualCode("ErretcdLeaderNotFound").MessageTemplate("etcd leader not found").Build()
ErrGetLeader = ClassMember.DefineError().TextualCode("ErrGetLeader").MessageTemplate("fail to get leader").Build()
ErrDeleteLeaderKey = ClassMember.DefineError().TextualCode("ErrDeleteLeaderKey").MessageTemplate("fail to delete leader key").Build()
ErrLoadLeaderPriority = ClassMember.DefineError().TextualCode("ErrLoadLeaderPriority").MessageTemplate("fail to load leader priority").Build()
ErrLoadetcdLeaderPriority = ClassMember.DefineError().TextualCode("ErrLoadetcdLeaderPriority").MessageTemplate("fail to load etcd leader priority").Build()
ErrTransferetcdLeader = ClassMember.DefineError().TextualCode("ErrTransferetcdLeader").MessageTemplate("fail to transfer etcd leader").Build()
ErrWatcherCancel = ClassMember.DefineError().TextualCode("ErrWatcherCancel").MessageTemplate("watcher canceled").Build()
ErrMarshalLeader = ClassMember.DefineError().TextualCode("ErrMarshalLeader").MessageTemplate("fail to marshal leader").Build()
)
17 changes: 10 additions & 7 deletions pkg/errors/errors_test.go → pkg/errs/errs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package errors
package errs

import (
"bytes"
"errors"
"fmt"
"github.com/pingcap/log"
"go.uber.org/zap"
"strings"
"testing"

"github.com/pingcap/log"
"go.uber.org/zap"
)

// testingWriter is a WriteSyncer that writes to the the messages.
Expand Down Expand Up @@ -79,10 +81,11 @@ func TestError(t *testing.T) {
lg := newZapTestLogger(conf)
log.ReplaceGlobals(lg.Logger, nil)

rfc := `[error="[PD:format:ErrFormatParseHistoryIndex] parse history index error"]`
log.Error(ErrFormatParseHistoryIndex.MessageTemplate(), zap.Error(ErrFormatParseHistoryIndex.FastGenByArgs()))
rfc := `[error="[PD:tso:ErrInvalidTimestamp] invalid timestamp"]`
log.Error("test", zap.Error(ErrInvalidTimestamp.FastGenByArgs()))
lg.Contain(t, rfc)
rfc = `[error="[PD:internal:ErrInternalStoreNotFound] store id 1 not found"]`
log.Error(ErrInternalStoreNotFound.MessageTemplate(), zap.Error(ErrInternalStoreNotFound.FastGenByArgs(1)))
cause := `[cause="test err"]`
log.Error("test", zap.Error(ErrInvalidTimestamp.FastGenByArgs()), zap.NamedError("cause", errors.New("test err")))
lg.Contain(t, rfc)
lg.Contain(t, cause)
}
17 changes: 9 additions & 8 deletions server/member/leader.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/pingcap/failpoint"
"github.com/pingcap/kvproto/pkg/pdpb"
"github.com/pingcap/log"
"github.com/pingcap/pd/v4/pkg/errs"
"github.com/pingcap/pd/v4/pkg/etcdutil"
"github.com/pingcap/pd/v4/server/config"
"github.com/pingcap/pd/v4/server/kv"
Expand Down Expand Up @@ -131,14 +132,14 @@ func (m *Member) GetLeaderPath() string {
// CheckLeader checks returns true if it is needed to check later.
func (m *Member) CheckLeader(name string) (*pdpb.Member, int64, bool) {
if m.GetEtcdLeader() == 0 {
log.Error("no etcd leader, check leader later")
log.Error("no etcd leader, check leader later", zap.Error(errs.ErretcdLeaderNotFound.FastGenByArgs()))
time.Sleep(200 * time.Millisecond)
return nil, 0, true
}

leader, rev, err := getLeader(m.client, m.GetLeaderPath())
if err != nil {
log.Error("get leader meet error", zap.Error(err))
log.Error("get leader meet error", zap.Error(errs.ErrGetLeader.FastGenByArgs()), zap.NamedError("cause", err))
time.Sleep(200 * time.Millisecond)
return nil, 0, true
}
Expand All @@ -148,7 +149,7 @@ func (m *Member) CheckLeader(name string) (*pdpb.Member, int64, bool) {
// in previous CampaignLeader. we can delete and campaign again.
log.Warn("the leader has not changed, delete and campaign again", zap.Stringer("old-leader", leader))
if err = m.deleteLeaderKey(); err != nil {
log.Error("delete leader key meet error", zap.Error(err))
log.Error("delete leader key meet error", zap.Error(errs.ErrDeleteLeaderKey.FastGenByArgs()), zap.NamedError("cause", err))
time.Sleep(200 * time.Millisecond)
return nil, 0, true
}
Expand All @@ -165,18 +166,18 @@ func (m *Member) CheckPriority(ctx context.Context) {
}
myPriority, err := m.GetMemberLeaderPriority(m.ID())
if err != nil {
log.Error("failed to load leader priority", zap.Error(err))
log.Error("failed to load leader priority", zap.Error(errs.ErrLoadLeaderPriority.FastGenByArgs()), zap.NamedError("cause", err))
return
}
leaderPriority, err := m.GetMemberLeaderPriority(etcdLeader)
if err != nil {
log.Error("failed to load etcd leader priority", zap.Error(err))
log.Error("failed to load etcd leader priority", zap.Error(errs.ErrLoadetcdLeaderPriority.FastGenByArgs()), zap.NamedError("cause", err))
return
}
if myPriority > leaderPriority {
err := m.MoveEtcdLeader(ctx, etcdLeader, m.ID())
if err != nil {
log.Error("failed to transfer etcd leader", zap.Error(err))
log.Error("failed to transfer etcd leader", zap.Error(errs.ErrTransferetcdLeader.FastGenByArgs()), zap.NamedError("cause", err))
} else {
log.Info("transfer etcd leader",
zap.Uint64("from", etcdLeader),
Expand Down Expand Up @@ -227,7 +228,7 @@ func (m *Member) MemberInfo(cfg *config.Config, name string, rootPath string) {
data, err := leader.Marshal()
if err != nil {
// can't fail, so panic here.
log.Fatal("marshal leader meet error", zap.Stringer("leader", leader), zap.Error(err))
log.Fatal("marshal leader meet error", zap.Stringer("leader", leader), zap.Error(errs.ErrMarshalLeader.FastGenByArgs()), zap.NamedError("cause", err))
}
m.member = leader
m.memberValue = string(data)
Expand Down Expand Up @@ -475,7 +476,7 @@ func (m *Member) WatchLeader(serverCtx context.Context, leader *pdpb.Member, rev
break
}
if wresp.Canceled {
log.Error("leader watcher is canceled with", zap.Int64("revision", revision), zap.Error(wresp.Err()))
log.Error("leader watcher is canceled with", zap.Int64("revision", revision), zap.Error(errs.ErrWatcherCancel.FastGenByArgs()), zap.NamedError("cause", wresp.Err()))
return
}

Expand Down
7 changes: 4 additions & 3 deletions server/tso/tso.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/pingcap/failpoint"
"github.com/pingcap/kvproto/pkg/pdpb"
"github.com/pingcap/log"
"github.com/pingcap/pd/v4/pkg/errs"
"github.com/pingcap/pd/v4/pkg/etcdutil"
"github.com/pingcap/pd/v4/pkg/tsoutil"
"github.com/pingcap/pd/v4/pkg/typeutil"
Expand Down Expand Up @@ -144,7 +145,7 @@ func (t *TimestampOracle) SyncTimestamp(lease *member.LeaderLease) error {
// If the current system time minus the saved etcd timestamp is less than `updateTimestampGuard`,
// the timestamp allocation will start from the saved etcd timestamp temporarily.
if typeutil.SubTimeByWallClock(next, last) < updateTimestampGuard {
log.Error("system time may be incorrect", zap.Time("last", last), zap.Time("next", next))
log.Error("system time may be incorrect", zap.Time("last", last), zap.Time("next", next), zap.Error(errs.ErrIncorrectSystemTime.FastGenByArgs()))
next = last.Add(updateTimestampGuard)
}

Expand Down Expand Up @@ -299,7 +300,7 @@ func (t *TimestampOracle) GetRespTS(count uint32) (pdpb.Timestamp, error) {
time.Sleep(200 * time.Millisecond)
continue
}
log.Error("invalid timestamp", zap.Any("timestamp", current))
log.Error("invalid timestamp", zap.Any("timestamp", current), zap.Error(errs.ErrInvalidTimestamp.FastGenByArgs()))
return pdpb.Timestamp{}, errors.New("can not get timestamp, may be not leader")
}

Expand All @@ -308,7 +309,7 @@ func (t *TimestampOracle) GetRespTS(count uint32) (pdpb.Timestamp, error) {
if resp.Logical >= maxLogical {
log.Error("logical part outside of max logical interval, please check ntp time",
zap.Reflect("response", resp),
zap.Int("retry-count", i))
zap.Int("retry-count", i), zap.Error(errs.ErrLogicOverflow.FastGenByArgs()))
tsoCounter.WithLabelValues("logical_overflow").Inc()
time.Sleep(UpdateTimestampStep)
continue
Expand Down

0 comments on commit 52772d8

Please sign in to comment.