Skip to content

Commit

Permalink
distinguish biz error from rpc wire error
Browse files Browse the repository at this point in the history
  • Loading branch information
chickenlj committed Jul 21, 2024
1 parent cb400b2 commit 8db8346
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
20 changes: 12 additions & 8 deletions cluster/cluster/failover/cluster_invoker.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package failover

import (
"context"
"dubbo.apache.org/dubbo-go/v3/protocol/triple/triple_protocol"
"fmt"
)

Expand Down Expand Up @@ -83,7 +84,7 @@ func (invoker *failoverClusterInvoker) Invoke(ctx context.Context, invocation pr
invoked = append(invoked, ivk)
// DO INVOKE
result = ivk.Invoke(ctx, invocation)
if result.Error() != nil {
if result.Error() != nil && !isBizError(result.Error()) {
providers = append(providers, ivk.GetURL().Key())
continue
}
Expand All @@ -100,13 +101,16 @@ func (invoker *failoverClusterInvoker) Invoke(ctx context.Context, invocation pr
}
}

return &protocol.RPCResult{
Err: perrors.Wrap(result.Error(), fmt.Sprintf("Failed to invoke the method %v in the service %v. "+
"Tried %v times of the providers %v (%v/%v)from the registry %v on the consumer %v using the dubbo version %v. "+
"Last error is %+v.", methodName, invokerSvc, retries, providers, len(providers), len(invokers),
invokerUrl, ip, constant.Version, result.Error().Error()),
),
}
logger.Errorf(fmt.Sprintf("Failed to invoke the method %v in the service %v. "+
"Tried %v times of the providers %v (%v/%v)from the registry %v on the consumer %v using the dubbo version %v. "+
"Last error is %+v.", methodName, invokerSvc, retries, providers, len(providers), len(invokers),
invokerUrl, ip, constant.Version, result.Error().Error()))

return result
}

func isBizError(err error) bool {
return triple_protocol.IsWireError(err) && triple_protocol.CodeOf(err) == triple_protocol.CodeBizError
}

func getRetries(invokers []protocol.Invoker, methodName string) int {
Expand Down
17 changes: 14 additions & 3 deletions protocol/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ var _ Result = (*RPCResult)(nil)

// RPCResult is default RPC result.
type RPCResult struct {
Attrs map[string]interface{}
Err error
Rest interface{}
Attrs map[string]interface{}
BizErr error
Err error
Rest interface{}
}

// SetError sets error.
Expand All @@ -68,6 +69,16 @@ func (r *RPCResult) Error() error {
return r.Err
}

// SetBizError sets error.
func (r *RPCResult) SetBizError(err error) {
r.BizErr = err
}

// BizError gets error. Replaced with error code in triple protocol since 3.2.0
func (r *RPCResult) BizError() error {
return r.BizErr
}

// SetResult sets invoker result.
func (r *RPCResult) SetResult(rest interface{}) {
r.Rest = rest
Expand Down
2 changes: 2 additions & 0 deletions protocol/triple/triple_protocol/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ const (
// authentication credentials for the operation.
CodeUnauthenticated Code = 16

CodeBizError Code = 17

minCode = CodeCanceled
maxCode = CodeUnauthenticated
)
Expand Down
5 changes: 4 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package server

import (
"context"
"dubbo.apache.org/dubbo-go/v3/protocol/triple/triple_protocol"
"fmt"
"sort"
"sync"
Expand Down Expand Up @@ -106,7 +107,9 @@ func (ii *infoInvoker) Invoke(ctx context.Context, invocation protocol.Invocatio
if method, ok := ii.methodMap[name]; ok {
res, err := method.MethodFunc(ctx, args, ii.svc)
result.SetResult(res)
result.SetError(err)
if err != nil {
result.SetError(triple_protocol.NewError(triple_protocol.CodeBizError, err))
}
return result
}
result.SetError(fmt.Errorf("no match method for %s", name))
Expand Down

0 comments on commit 8db8346

Please sign in to comment.