-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cherry pick tikv#2707 to release-4.0
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
- Loading branch information
Showing
5 changed files
with
178 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// 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 ( | ||
"bytes" | ||
"errors" | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/pingcap/log" | ||
"go.uber.org/zap" | ||
) | ||
|
||
// testingWriter is a WriteSyncer that writes to the the messages. | ||
type testingWriter struct { | ||
messages []string | ||
} | ||
|
||
func newTestingWriter() *testingWriter { | ||
return &testingWriter{} | ||
} | ||
|
||
func (w *testingWriter) Write(p []byte) (n int, err error) { | ||
n = len(p) | ||
p = bytes.TrimRight(p, "\n") | ||
m := fmt.Sprintf("%s", p) | ||
w.messages = append(w.messages, m) | ||
return n, nil | ||
} | ||
|
||
func (w *testingWriter) Sync() error { | ||
return nil | ||
} | ||
|
||
type verifyLogger struct { | ||
*zap.Logger | ||
w *testingWriter | ||
} | ||
|
||
func (logger *verifyLogger) Contain(t *testing.T, s string) { | ||
if logger.w.messages == nil { | ||
t.Error() | ||
} | ||
msg := logger.w.messages[len(logger.w.messages)-1] | ||
IsContain(t, msg, s) | ||
} | ||
|
||
func newZapTestLogger(cfg *log.Config, opts ...zap.Option) verifyLogger { | ||
// TestingWriter is used to write to memory. | ||
// Used in the verify logger. | ||
writer := newTestingWriter() | ||
lg, _, _ := log.InitLoggerWithWriteSyncer(cfg, writer, opts...) | ||
|
||
return verifyLogger{ | ||
Logger: lg, | ||
w: writer, | ||
} | ||
} | ||
|
||
func IsContain(t *testing.T, s1 string, s2 string) { | ||
if !strings.Contains(s1, s2) { | ||
t.Error() | ||
} | ||
} | ||
|
||
func TestError(t *testing.T) { | ||
conf := &log.Config{Level: "debug", File: log.FileLogConfig{}, DisableTimestamp: true} | ||
lg := newZapTestLogger(conf) | ||
log.ReplaceGlobals(lg.Logger, nil) | ||
|
||
rfc := `[error="[PD:tso:ErrInvalidTimestamp] invalid timestamp"]` | ||
log.Error("test", zap.Error(ErrInvalidTimestamp.FastGenByArgs())) | ||
lg.Contain(t, rfc) | ||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters