forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This change enables future upgrades of google.golang.org/grpc. Recent changes to google.golang.org/grpc have changed the default proto codec such that it is incompatible with gogoproto: grpc/grpc-go#3958 While `proto.Marshal` does do a check for the Marshal/Unmarshal it appears to do this through reflection methods that will panic on fields using gogoproto.nullable. To avoid this, we install a proto codec that does an interface check before dispatching to proto.Marshal/proto.Unmarshal. Note that I haven't replicated the protobuffer caching that previously existed in google.golang.org/grpc. The referenced change claims removing the caching may be a small performance win; however, they also removed the typecheck that we need to do, so it is unclear. Fixes cockroachdb#60531 Release note: None
- Loading branch information
1 parent
3f9f18e
commit 5ba160f
Showing
5 changed files
with
160 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright 2021 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package rpc | ||
|
||
import ( | ||
"github.com/gogo/protobuf/proto" | ||
// Used instead of gogo/protobuf/proto for the fallback case | ||
// to match the behavior of the upstream codec in | ||
// google.golang.org/grpc/encoding/proto that we are | ||
// replacing: | ||
// | ||
// https://github.com/grpc/grpc-go/blob/7b167fd6eca1ab8f05ec14085d63197cacd41438/encoding/proto/proto.go | ||
// | ||
gproto "github.com/golang/protobuf/proto" | ||
"google.golang.org/grpc/encoding" | ||
) | ||
|
||
const name = "proto" | ||
|
||
type codec struct{} | ||
|
||
var _ encoding.Codec = codec{} | ||
|
||
func (codec) Marshal(v interface{}) ([]byte, error) { | ||
if pm, ok := v.(proto.Marshaler); ok { | ||
return pm.Marshal() | ||
} | ||
return gproto.Marshal(v.(gproto.Message)) | ||
} | ||
|
||
func (codec) Unmarshal(data []byte, v interface{}) error { | ||
if pm, ok := v.(proto.Unmarshaler); ok { | ||
return pm.Unmarshal(data) | ||
} | ||
return gproto.Unmarshal(data, v.(gproto.Message)) | ||
} | ||
|
||
func (codec) Name() string { | ||
return name | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// Copyright 2021 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package rpc | ||
|
||
import ( | ||
"reflect" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/roachpb" | ||
"github.com/cockroachdb/cockroach/pkg/util/leaktest" | ||
"github.com/stretchr/testify/require" | ||
"go.etcd.io/etcd/raft/v3/raftpb" | ||
"google.golang.org/grpc/health/grpc_health_v1" | ||
) | ||
|
||
func TestCodecMarshalUnmarshal(t *testing.T) { | ||
defer leaktest.AfterTest(t)() | ||
|
||
testCodec := codec{} | ||
for _, test := range []struct { | ||
name string | ||
filledMsgBuilder func() interface{} | ||
emptyMsgBuilder func() interface{} | ||
}{ | ||
{"rpc.PingRequest", | ||
func() interface{} { return &PingRequest{Ping: "pong"} }, | ||
func() interface{} { return &PingRequest{} }}, | ||
{"raftpb.Message", | ||
func() interface{} { | ||
return &raftpb.Message{ | ||
To: 531, | ||
From: 550, | ||
} | ||
}, | ||
func() interface{} { return &raftpb.Message{} }}, | ||
{"grpc_health_v1.HealthCheckRequest", | ||
func() interface{} { | ||
return &grpc_health_v1.HealthCheckRequest{ | ||
Service: "wombats", | ||
} | ||
}, | ||
func() interface{} { return &grpc_health_v1.HealthCheckRequest{} }}, | ||
{"roachpb.GetRequest", | ||
func() interface{} { | ||
return &roachpb.GetRequest{ | ||
RequestHeader: roachpb.RequestHeader{ | ||
Key: roachpb.Key("turtle"), | ||
}, | ||
} | ||
}, | ||
func() interface{} { return &roachpb.GetRequest{} }}, | ||
} { | ||
t.Run(test.name, func(t *testing.T) { | ||
input := test.filledMsgBuilder() | ||
marshaled, err := testCodec.Marshal(input) | ||
require.NoError(t, err, "marshal failed") | ||
output := test.emptyMsgBuilder() | ||
err = testCodec.Unmarshal(marshaled, output) | ||
require.NoError(t, err, "unmarshal failed") | ||
// reflect.DeepEqual/require.Equal can fail | ||
// because of XXX_sizecache fields | ||
// | ||
// google's proto Equal doesn't understand all | ||
// gogoproto generated types and panics. | ||
// | ||
// gogoproto's proto Equal fails because of | ||
// https://github.com/gogo/protobuf/issues/13 | ||
// | ||
// Here, we zero any top-level fields that | ||
// start with XXX_ and then use require.Equal | ||
// (which uses require.DeepEqual). I doubt | ||
// this would work for the general case, but | ||
// it works for the protobufs tested here. | ||
zeroXXXFields(input) | ||
zeroXXXFields(output) | ||
require.Equal(t, input, output) | ||
}) | ||
} | ||
} | ||
|
||
func zeroXXXFields(v interface{}) { | ||
val := reflect.Indirect(reflect.ValueOf(v)) | ||
typ := val.Type() | ||
for i := 0; i < val.NumField(); i++ { | ||
if strings.HasPrefix(typ.Field(i).Name, "XXX_") { | ||
val.Field(i).Set(reflect.Zero(val.Field(i).Type())) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters