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

[3.5] etcdserver: fix nil pointer panic for readonly txn #14899

Merged
Merged
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
6 changes: 5 additions & 1 deletion server/etcdserver/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,11 @@ func warnOfExpensiveReadOnlyTxnRequest(lg *zap.Logger, warningApplyDuration time
for _, r := range txnResponse.Responses {
switch op := r.Response.(type) {
case *pb.ResponseOp_ResponseRange:
resps = append(resps, fmt.Sprintf("range_response_count:%d", len(op.ResponseRange.Kvs)))
if op.ResponseRange != nil {
resps = append(resps, fmt.Sprintf("range_response_count:%d", len(op.ResponseRange.Kvs)))
} else {
resps = append(resps, "range_response:nil")
}
default:
// only range responses should be in a read only txn request
}
Expand Down
104 changes: 104 additions & 0 deletions server/etcdserver/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ import (
"time"

"go.uber.org/zap"
"go.uber.org/zap/zaptest"

pb "go.etcd.io/etcd/api/v3/etcdserverpb"
"go.etcd.io/etcd/client/pkg/v3/types"
"go.etcd.io/etcd/raft/v3/raftpb"
"go.etcd.io/etcd/server/v3/etcdserver/api/membership"
Expand Down Expand Up @@ -110,3 +112,105 @@ type testStringerFunc func() string
func (s testStringerFunc) String() string {
return s()
}

// TestWarnOfExpensiveReadOnlyTxnRequest verifies WarnOfExpensiveReadOnlyTxnRequest
// never panic no matter what data the txnResponse contains.
func TestWarnOfExpensiveReadOnlyTxnRequest(t *testing.T) {
testCases := []struct {
name string
txnResp *pb.TxnResponse
}{
{
name: "all readonly responses",
txnResp: &pb.TxnResponse{
Responses: []*pb.ResponseOp{
{
Response: &pb.ResponseOp_ResponseRange{
ResponseRange: &pb.RangeResponse{},
},
},
{
Response: &pb.ResponseOp_ResponseRange{
ResponseRange: &pb.RangeResponse{},
},
},
},
},
},
{
name: "all readonly responses with partial nil responses",
txnResp: &pb.TxnResponse{
Responses: []*pb.ResponseOp{
{
Response: &pb.ResponseOp_ResponseRange{
ResponseRange: &pb.RangeResponse{},
},
},
{
Response: &pb.ResponseOp_ResponseRange{
ResponseRange: nil,
},
},
},
},
},
{
name: "all readonly responses with all nil responses",
txnResp: &pb.TxnResponse{
Responses: []*pb.ResponseOp{
{
Response: &pb.ResponseOp_ResponseRange{
ResponseRange: nil,
},
},
{
Response: &pb.ResponseOp_ResponseRange{
ResponseRange: nil,
},
},
},
},
},
{
name: "partial non readonly responses",
txnResp: &pb.TxnResponse{
Responses: []*pb.ResponseOp{
{
Response: &pb.ResponseOp_ResponseRange{
ResponseRange: nil,
},
},
{
Response: &pb.ResponseOp_ResponsePut{},
},
{
Response: &pb.ResponseOp_ResponseDeleteRange{},
},
},
},
},
{
name: "all non readonly responses",
txnResp: &pb.TxnResponse{
Responses: []*pb.ResponseOp{
{
Response: &pb.ResponseOp_ResponsePut{},
},
{
Response: &pb.ResponseOp_ResponseDeleteRange{},
},
},
},
},
}

for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
lg := zaptest.NewLogger(t)
start := time.Now().Add(-1 * time.Second)
// WarnOfExpensiveReadOnlyTxnRequest shouldn't panic.
warnOfExpensiveReadOnlyTxnRequest(lg, 0, start, &pb.TxnRequest{}, tc.txnResp, nil)
})
}
}