Skip to content

Commit

Permalink
Merge pull request #14907 from fuweid/chore-reduce-ifelse
Browse files Browse the repository at this point in the history
chore: use Getter in WarnOfExpensiveReadOnlyTxnRequest
  • Loading branch information
ahrtr authored Dec 7, 2022
2 parents d59c9b8 + f59896c commit 6d0bf24
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
6 changes: 3 additions & 3 deletions server/etcdserver/txn/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
18 changes: 17 additions & 1 deletion server/etcdserver/txn/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -35,7 +42,9 @@ func TestWarnOfExpensiveReadOnlyTxnRequest(t *testing.T) {
Responses: []*pb.ResponseOp{
{
Response: &pb.ResponseOp_ResponseRange{
ResponseRange: &pb.RangeResponse{},
ResponseRange: &pb.RangeResponse{
Kvs: kvs,
},
},
},
{
Expand All @@ -60,6 +69,13 @@ func TestWarnOfExpensiveReadOnlyTxnRequest(t *testing.T) {
ResponseRange: nil,
},
},
{
Response: &pb.ResponseOp_ResponseRange{
ResponseRange: &pb.RangeResponse{
Kvs: kvs,
},
},
},
},
},
},
Expand Down

0 comments on commit 6d0bf24

Please sign in to comment.