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

Errors add stack #1246

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@

[[constraint]]
name = "github.com/pkg/errors"
version = ">= 0.8.0"
version = ">= 0.9.0"
source = "https://github.com/pingcap/errors"

[[override]]
name = "github.com/BurntSushi/toml"
Expand Down
36 changes: 18 additions & 18 deletions pd-client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func (c *client) initClusterID() error {
time.Sleep(time.Second)
}

return errors.WithStack(errFailInitClusterID)
return errors.AddStack(errFailInitClusterID)
}

func (c *client) updateLeader() error {
Expand All @@ -186,7 +186,7 @@ func (c *client) updateLeader() error {
if err != nil || members.GetLeader() == nil || len(members.GetLeader().GetClientUrls()) == 0 {
select {
case <-c.ctx.Done():
return errors.WithStack(err)
return errors.AddStack(err)
default:
continue
}
Expand All @@ -204,7 +204,7 @@ func (c *client) getMembers(ctx context.Context, url string) (*pdpb.GetMembersRe
}
members, err := pdpb.NewPDClient(cc).GetMembers(ctx, &pdpb.GetMembersRequest{})
if err != nil {
return nil, errors.WithStack(err)
return nil, errors.AddStack(err)
}
return members, nil
}
Expand Down Expand Up @@ -274,11 +274,11 @@ func (c *client) getOrCreateGRPCConn(addr string) (*grpc.ClientConn, error) {
}
u, err := url.Parse(addr)
if err != nil {
return nil, errors.WithStack(err)
return nil, errors.AddStack(err)
}
cc, err := grpc.Dial(u.Host, opt)
if err != nil {
return nil, errors.WithStack(err)
return nil, errors.AddStack(err)
}
c.connMu.Lock()
defer c.connMu.Unlock()
Expand Down Expand Up @@ -367,7 +367,7 @@ func (c *client) tsLoop() {
log.Errorf("[pd] create tso stream error: %v", err)
c.ScheduleCheckLeader()
cancel()
c.revokeTSORequest(errors.WithStack(err))
c.revokeTSORequest(errors.AddStack(err))
select {
case <-time.After(time.Second):
case <-loopCtx.Done():
Expand Down Expand Up @@ -440,19 +440,19 @@ func (c *client) processTSORequests(stream pdpb.PD_TsoClient, requests []*tsoReq
}

if err := stream.Send(req); err != nil {
err = errors.WithStack(err)
err = errors.AddStack(err)
c.finishTSORequest(requests, 0, 0, err)
return err
}
resp, err := stream.Recv()
if err != nil {
err = errors.WithStack(err)
err = errors.AddStack(err)
c.finishTSORequest(requests, 0, 0, err)
return err
}
requestDuration.WithLabelValues("tso").Observe(time.Since(start).Seconds())
if resp.GetCount() != uint32(len(requests)) {
err = errors.WithStack(errTSOLength)
err = errors.AddStack(errTSOLength)
c.finishTSORequest(requests, 0, 0, err)
return err
}
Expand Down Expand Up @@ -486,7 +486,7 @@ func (c *client) Close() {
c.cancel()
c.wg.Wait()

c.revokeTSORequest(errors.WithStack(errClosing))
c.revokeTSORequest(errors.AddStack(errClosing))

c.connMu.Lock()
defer c.connMu.Unlock()
Expand Down Expand Up @@ -562,7 +562,7 @@ func (req *tsoRequest) Wait() (physical int64, logical int64, err error) {
cmdDuration.WithLabelValues("tso_async_wait").Observe(time.Since(req.start).Seconds())
select {
case err = <-req.done:
err = errors.WithStack(err)
err = errors.AddStack(err)
defer tsoReqPool.Put(req)
if err != nil {
cmdFailedDuration.WithLabelValues("tso").Observe(time.Since(req.start).Seconds())
Expand All @@ -572,7 +572,7 @@ func (req *tsoRequest) Wait() (physical int64, logical int64, err error) {
cmdDuration.WithLabelValues("tso").Observe(time.Since(req.start).Seconds())
return
case <-req.ctx.Done():
return 0, 0, errors.WithStack(req.ctx.Err())
return 0, 0, errors.AddStack(req.ctx.Err())
}
}

Expand All @@ -599,7 +599,7 @@ func (c *client) GetRegion(ctx context.Context, key []byte) (*metapb.Region, *me
if err != nil {
cmdFailedDuration.WithLabelValues("get_region").Observe(time.Since(start).Seconds())
c.ScheduleCheckLeader()
return nil, nil, errors.WithStack(err)
return nil, nil, errors.AddStack(err)
}
return resp.GetRegion(), resp.GetLeader(), nil
}
Expand All @@ -622,7 +622,7 @@ func (c *client) GetPrevRegion(ctx context.Context, key []byte) (*metapb.Region,
if err != nil {
cmdFailedDuration.WithLabelValues("get_prev_region").Observe(time.Since(start).Seconds())
c.ScheduleCheckLeader()
return nil, nil, errors.WithStack(err)
return nil, nil, errors.AddStack(err)
}
return resp.GetRegion(), resp.GetLeader(), nil
}
Expand All @@ -645,7 +645,7 @@ func (c *client) GetRegionByID(ctx context.Context, regionID uint64) (*metapb.Re
if err != nil {
cmdFailedDuration.WithLabelValues("get_region_byid").Observe(time.Since(start).Seconds())
c.ScheduleCheckLeader()
return nil, nil, errors.WithStack(err)
return nil, nil, errors.AddStack(err)
}
return resp.GetRegion(), resp.GetLeader(), nil
}
Expand All @@ -668,7 +668,7 @@ func (c *client) GetStore(ctx context.Context, storeID uint64) (*metapb.Store, e
if err != nil {
cmdFailedDuration.WithLabelValues("get_store").Observe(time.Since(start).Seconds())
c.ScheduleCheckLeader()
return nil, errors.WithStack(err)
return nil, errors.AddStack(err)
}
store := resp.GetStore()
if store == nil {
Expand Down Expand Up @@ -697,7 +697,7 @@ func (c *client) GetAllStores(ctx context.Context) ([]*metapb.Store, error) {
if err != nil {
cmdFailedDuration.WithLabelValues("get_all_stores").Observe(time.Since(start).Seconds())
c.ScheduleCheckLeader()
return nil, errors.WithStack(err)
return nil, errors.AddStack(err)
}
stores := resp.GetStores()
return stores, nil
Expand All @@ -721,7 +721,7 @@ func (c *client) UpdateGCSafePoint(ctx context.Context, safePoint uint64) (uint6
if err != nil {
cmdFailedDuration.WithLabelValues("update_gc_safe_point").Observe(time.Since(start).Seconds())
c.ScheduleCheckLeader()
return 0, errors.WithStack(err)
return 0, errors.AddStack(err)
}
return resp.GetNewSafePoint(), nil
}
Expand Down
2 changes: 1 addition & 1 deletion pdctl/command/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func InitHTTPSClient(CAPath, CertPath, KeyPath string) error {
}
tlsConfig, err := tlsInfo.ClientConfig()
if err != nil {
return errors.WithStack(err)
return errors.AddStack(err)
}

dialClient = &http.Client{Transport: &http.Transport{
Expand Down
2 changes: 1 addition & 1 deletion pdctl/command/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ func parseUint64s(args []string) ([]uint64, error) {
for _, arg := range args {
v, err := strconv.ParseUint(arg, 10, 64)
if err != nil {
return nil, errors.WithStack(err)
return nil, errors.AddStack(err)
}
results = append(results, v)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/apiutil/apiutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
// This is designed to be used in a defer statement.
func DeferClose(c io.Closer, err *error) {
if cerr := c.Close(); cerr != nil && *err == nil {
*err = errors.WithStack(cerr)
*err = errors.AddStack(cerr)
}
}

Expand Down Expand Up @@ -55,7 +55,7 @@ func ReadJSON(r io.ReadCloser, data interface{}) error {
defer DeferClose(r, &err)
b, err := ioutil.ReadAll(r)
if err != nil {
return errors.WithStack(err)
return errors.AddStack(err)
}

err = json.Unmarshal(b, data)
Expand Down
6 changes: 3 additions & 3 deletions pkg/etcdutil/etcdutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,21 @@ func AddEtcdMember(client *clientv3.Client, urls []string) (*clientv3.MemberAddR
ctx, cancel := context.WithTimeout(client.Ctx(), DefaultRequestTimeout)
addResp, err := client.MemberAdd(ctx, urls)
cancel()
return addResp, errors.WithStack(err)
return addResp, errors.AddStack(err)
}

// ListEtcdMembers returns a list of internal etcd members.
func ListEtcdMembers(client *clientv3.Client) (*clientv3.MemberListResponse, error) {
ctx, cancel := context.WithTimeout(client.Ctx(), DefaultRequestTimeout)
listResp, err := client.MemberList(ctx)
cancel()
return listResp, errors.WithStack(err)
return listResp, errors.AddStack(err)
}

// RemoveEtcdMember removes a member by the given id.
func RemoveEtcdMember(client *clientv3.Client, id uint64) (*clientv3.MemberRemoveResponse, error) {
ctx, cancel := context.WithTimeout(client.Ctx(), DefaultRequestTimeout)
rmResp, err := client.MemberRemove(ctx, id)
cancel()
return rmResp, errors.WithStack(err)
return rmResp, errors.AddStack(err)
}
6 changes: 3 additions & 3 deletions pkg/faketikv/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,21 +107,21 @@ func (c *client) initClusterID() error {
return nil
}

return errors.WithStack(errFailInitClusterID)
return errors.AddStack(errFailInitClusterID)
}

func (c *client) getMembers(ctx context.Context) (*pdpb.GetMembersResponse, error) {
members, err := c.pdClient().GetMembers(ctx, &pdpb.GetMembersRequest{})
if err != nil {
return nil, errors.WithStack(err)
return nil, errors.AddStack(err)
}
return members, nil
}

func (c *client) createConn() (*grpc.ClientConn, error) {
cc, err := grpc.Dial(strings.TrimPrefix(c.url, "http://"), grpc.WithInsecure())
if err != nil {
return nil, errors.WithStack(err)
return nil, errors.AddStack(err)
}
return cc, nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/faketikv/drive.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (d *Driver) Prepare() error {
var id uint64
id, err = d.client.AllocID(context.Background())
if err != nil {
return errors.WithStack(err)
return errors.AddStack(err)
}
if id > d.conf.MaxID {
break
Expand Down
2 changes: 1 addition & 1 deletion pkg/faketikv/raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ func (r *RaftEngine) allocID(storeID uint64) (uint64, error) {
return 0, errors.Errorf("node %d not found", storeID)
}
id, err := node.client.AllocID(context.Background())
return id, errors.WithStack(err)
return id, errors.AddStack(err)
}

const (
Expand Down
4 changes: 2 additions & 2 deletions pkg/integration_test/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (s *testServer) GetEtcdLeader() (string, error) {
req := &pdpb.GetMembersRequest{Header: &pdpb.RequestHeader{ClusterId: s.server.ClusterID()}}
members, err := s.server.GetMembers(context.TODO(), req)
if err != nil {
return "", errors.WithStack(err)
return "", errors.AddStack(err)
}
return members.GetEtcdLeader().GetName(), nil
}
Expand Down Expand Up @@ -190,7 +190,7 @@ func (c *testCluster) RunServers(ctx context.Context, servers []*testServer) err
}
for _, c := range res {
if err := <-c; err != nil {
return errors.WithStack(err)
return errors.AddStack(err)
}
}
return nil
Expand Down
6 changes: 3 additions & 3 deletions pkg/typeutil/duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ func (d *Duration) MarshalJSON() ([]byte, error) {
func (d *Duration) UnmarshalJSON(text []byte) error {
s, err := strconv.Unquote(string(text))
if err != nil {
return errors.WithStack(err)
return errors.AddStack(err)
}
duration, err := time.ParseDuration(s)
if err != nil {
return errors.WithStack(err)
return errors.AddStack(err)
}
d.Duration = duration
return nil
Expand All @@ -54,5 +54,5 @@ func (d *Duration) UnmarshalJSON(text []byte) error {
func (d *Duration) UnmarshalText(text []byte) error {
var err error
d.Duration, err = time.ParseDuration(string(text))
return errors.WithStack(err)
return errors.AddStack(err)
}
6 changes: 3 additions & 3 deletions pkg/typeutil/size.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ func (b ByteSize) MarshalJSON() ([]byte, error) {
func (b *ByteSize) UnmarshalJSON(text []byte) error {
s, err := strconv.Unquote(string(text))
if err != nil {
return errors.WithStack(err)
return errors.AddStack(err)
}
v, err := gh.ParseBytes(s)
if err != nil {
return errors.WithStack(err)
return errors.AddStack(err)
}
*b = ByteSize(v)
return nil
Expand All @@ -46,7 +46,7 @@ func (b *ByteSize) UnmarshalJSON(text []byte) error {
func (b *ByteSize) UnmarshalText(text []byte) error {
v, err := gh.ParseBytes(string(text))
if err != nil {
return errors.WithStack(err)
return errors.AddStack(err)
}
*b = ByteSize(v)
return nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/typeutil/string_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (s StringSlice) MarshalJSON() ([]byte, error) {
func (s *StringSlice) UnmarshalJSON(text []byte) error {
data, err := strconv.Unquote(string(text))
if err != nil {
return errors.WithStack(err)
return errors.AddStack(err)
}
if len(data) == 0 {
*s = nil
Expand Down
2 changes: 1 addition & 1 deletion server/api/diagnose.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (d *diagnoseHandler) membersDiagnose(rdd *[]*Recommendation) error {
req := &pdpb.GetMembersRequest{Header: &pdpb.RequestHeader{ClusterId: d.svr.ClusterID()}}
members, err := d.svr.GetMembers(context.Background(), req)
if err != nil {
return errors.WithStack(err)
return errors.AddStack(err)
}
lenMembers := len(members.Members)
if lenMembers > 0 {
Expand Down
4 changes: 2 additions & 2 deletions server/api/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ func newStoresLabelFilter(name, value string) (*storesLabelFilter, error) {
// add (?i) to set a case-insensitive flag
keyPattern, err := regexp.Compile("(?i)" + name)
if err != nil {
return nil, errors.WithStack(err)
return nil, errors.AddStack(err)
}
valuePattern, err := regexp.Compile("(?i)" + value)
if err != nil {
return nil, errors.WithStack(err)
return nil, errors.AddStack(err)
}
return &storesLabelFilter{
keyPattern: keyPattern,
Expand Down
2 changes: 1 addition & 1 deletion server/api/member.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (h *memberHandler) getMembers() (*pdpb.GetMembersResponse, error) {
req := &pdpb.GetMembersRequest{Header: &pdpb.RequestHeader{ClusterId: h.svr.ClusterID()}}
members, err := h.svr.GetMembers(context.Background(), req)
if err != nil {
return nil, errors.WithStack(err)
return nil, errors.AddStack(err)
}
// Fill leader priorities.
for _, m := range members.GetMembers() {
Expand Down
2 changes: 1 addition & 1 deletion server/api/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ func newStoreStateFilter(u *url.URL) (*storeStateFilter, error) {
for _, s := range v {
state, err := strconv.Atoi(s)
if err != nil {
return nil, errors.WithStack(err)
return nil, errors.AddStack(err)
}

storeState := metapb.StoreState(state)
Expand Down
Loading