Merge pull request #14907 from fuweid/chore-reduce-ifelse

chore: use Getter in WarnOfExpensiveReadOnlyTxnRequest
This commit is contained in:
Benjamin Wang 2022-12-07 19:57:39 +08:00 committed by GitHub
commit 6d0bf24e54
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 4 deletions

View File

@ -61,10 +61,10 @@ func WarnOfExpensiveReadOnlyTxnRequest(lg *zap.Logger, warningApplyDuration time
if !isNil(txnResponse) {
var resps []string
for _, r := range txnResponse.Responses {
switch op := r.Response.(type) {
switch r.Response.(type) {
case *pb.ResponseOp_ResponseRange:
if op.ResponseRange != nil {
resps = append(resps, fmt.Sprintf("range_response_count:%d", len(op.ResponseRange.Kvs)))
if op := r.GetResponseRange(); op != nil {
resps = append(resps, fmt.Sprintf("range_response_count:%d", len(op.GetKvs())))
} else {
resps = append(resps, "range_response:nil")
}

View File

@ -19,12 +19,19 @@ import (
"time"
pb "go.etcd.io/etcd/api/v3/etcdserverpb"
"go.etcd.io/etcd/api/v3/mvccpb"
"go.uber.org/zap/zaptest"
)
// TestWarnOfExpensiveReadOnlyTxnRequest verifies WarnOfExpensiveReadOnlyTxnRequest
// never panic no matter what data the txnResponse contains.
func TestWarnOfExpensiveReadOnlyTxnRequest(t *testing.T) {
kvs := []*mvccpb.KeyValue{
&mvccpb.KeyValue{Key: []byte("k1"), Value: []byte("v1")},
&mvccpb.KeyValue{Key: []byte("k2"), Value: []byte("v2")},
}
testCases := []struct {
name string
txnResp *pb.TxnResponse
@ -35,7 +42,9 @@ func TestWarnOfExpensiveReadOnlyTxnRequest(t *testing.T) {
Responses: []*pb.ResponseOp{
{
Response: &pb.ResponseOp_ResponseRange{
ResponseRange: &pb.RangeResponse{},
ResponseRange: &pb.RangeResponse{
Kvs: kvs,
},
},
},
{
@ -60,6 +69,13 @@ func TestWarnOfExpensiveReadOnlyTxnRequest(t *testing.T) {
ResponseRange: nil,
},
},
{
Response: &pb.ResponseOp_ResponseRange{
ResponseRange: &pb.RangeResponse{
Kvs: kvs,
},
},
},
},
},
},