client: enhance the function shouldRetryWatch and added unit test

Signed-off-by: Benjamin Wang <wachao@vmware.com>
This commit is contained in:
Benjamin Wang 2022-12-13 05:08:46 +08:00
parent ee9db729da
commit 19dc0cb413
2 changed files with 43 additions and 6 deletions

View File

@ -18,7 +18,6 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"strings"
"sync" "sync"
"time" "time"
@ -588,8 +587,7 @@ func (w *watchGrpcStream) run() {
switch { switch {
case pbresp.Created: case pbresp.Created:
cancelReasonError := v3rpc.Error(errors.New(pbresp.CancelReason)) if shouldRetryWatch(pbresp.CancelReason) {
if shouldRetryWatch(cancelReasonError) {
var newErr error var newErr error
if wc, newErr = w.newWatchClient(); newErr != nil { if wc, newErr = w.newWatchClient(); newErr != nil {
w.lg.Error("failed to create a new watch client", zap.Error(newErr)) w.lg.Error("failed to create a new watch client", zap.Error(newErr))
@ -717,9 +715,9 @@ func (w *watchGrpcStream) run() {
} }
} }
func shouldRetryWatch(cancelReasonError error) bool { func shouldRetryWatch(cancelReason string) bool {
return (strings.Compare(cancelReasonError.Error(), v3rpc.ErrGRPCInvalidAuthToken.Error()) == 0) || return (cancelReason == v3rpc.ErrGRPCInvalidAuthToken.Error()) ||
(strings.Compare(cancelReasonError.Error(), v3rpc.ErrGRPCAuthOldRevision.Error()) == 0) (cancelReason == v3rpc.ErrGRPCAuthOldRevision.Error())
} }
// nextResume chooses the next resuming to register with the grpc stream. Abandoned // nextResume chooses the next resuming to register with the grpc stream. Abandoned

View File

@ -17,7 +17,10 @@ package clientv3
import ( import (
"testing" "testing"
"github.com/stretchr/testify/assert"
"go.etcd.io/etcd/api/v3/mvccpb" "go.etcd.io/etcd/api/v3/mvccpb"
"go.etcd.io/etcd/api/v3/v3rpc/rpctypes"
) )
func TestEvent(t *testing.T) { func TestEvent(t *testing.T) {
@ -53,3 +56,39 @@ func TestEvent(t *testing.T) {
} }
} }
} }
func TestShouldRetryWatch(t *testing.T) {
testCases := []struct {
name string
msg string
expectedRetry bool
}{
{
name: "equal to ErrGRPCInvalidAuthToken",
msg: rpctypes.ErrGRPCInvalidAuthToken.Error(),
expectedRetry: true,
},
{
name: "equal to ErrGRPCAuthOldRevision",
msg: rpctypes.ErrGRPCAuthOldRevision.Error(),
expectedRetry: true,
},
{
name: "valid grpc error but not equal to ErrGRPCInvalidAuthToken or ErrGRPCAuthOldRevision",
msg: rpctypes.ErrGRPCUserEmpty.Error(),
expectedRetry: false,
},
{
name: "invalid grpc error and not equal to ErrGRPCInvalidAuthToken or ErrGRPCAuthOldRevision",
msg: "whatever error message",
expectedRetry: false,
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.expectedRetry, shouldRetryWatch(tc.msg))
})
}
}