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

deps: move from github.com/golang/protobuf to google.golang.org/protobuf/proto #7059

Merged
merged 10 commits into from
Apr 5, 2024
8 changes: 4 additions & 4 deletions channelz/internal/protoconv/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ package protoconv
import (
"time"

"github.com/golang/protobuf/ptypes"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/connectivity"
"google.golang.org/grpc/internal/channelz"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/timestamppb"

channelzpb "google.golang.org/grpc/channelz/grpc_channelz_v1"
)
Expand Down Expand Up @@ -56,7 +56,7 @@ func channelTraceToProto(ct *channelz.ChannelTrace) *channelzpb.ChannelTrace {
return pbt
}
pbt.NumEventsLogged = ct.EventNum
if ts, err := ptypes.TimestampProto(ct.CreationTime); err == nil {
if ts := timestamppb.New(ct.CreationTime); ts.IsValid() {
pbt.CreationTimestamp = ts
}
events := make([]*channelzpb.ChannelTraceEvent, 0, len(ct.Events))
Expand All @@ -65,7 +65,7 @@ func channelTraceToProto(ct *channelz.ChannelTrace) *channelzpb.ChannelTrace {
Description: e.Desc,
Severity: channelzpb.ChannelTraceEvent_Severity(e.Severity),
}
if ts, err := ptypes.TimestampProto(e.Timestamp); err == nil {
if ts := timestamppb.New(e.Timestamp); ts.IsValid() {
cte.Timestamp = ts
}
if e.RefID != 0 {
Expand Down Expand Up @@ -93,7 +93,7 @@ func channelToProto(cm *channelz.Channel) *channelzpb.Channel {
CallsSucceeded: cm.ChannelMetrics.CallsSucceeded.Load(),
CallsFailed: cm.ChannelMetrics.CallsFailed.Load(),
}
if ts, err := ptypes.TimestampProto(time.Unix(0, cm.ChannelMetrics.LastCallStartedTimestamp.Load())); err == nil {
if ts := timestamppb.New(time.Unix(0, cm.ChannelMetrics.LastCallStartedTimestamp.Load())); ts.IsValid() {
c.Data.LastCallStartedTimestamp = ts
}
ncs := cm.NestedChans()
Expand Down
4 changes: 2 additions & 2 deletions channelz/internal/protoconv/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ package protoconv
import (
"time"

"github.com/golang/protobuf/ptypes"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/internal/channelz"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/timestamppb"

channelzpb "google.golang.org/grpc/channelz/grpc_channelz_v1"
)
Expand All @@ -39,7 +39,7 @@ func serverToProto(sm *channelz.Server) *channelzpb.Server {
CallsFailed: sm.ServerMetrics.CallsFailed.Load(),
}

if ts, err := ptypes.TimestampProto(time.Unix(0, sm.ServerMetrics.LastCallStartedTimestamp.Load())); err == nil {
if ts := timestamppb.New(time.Unix(0, sm.ServerMetrics.LastCallStartedTimestamp.Load())); ts.IsValid() {
s.Data.LastCallStartedTimestamp = ts
}
lss := sm.ListenSockets()
Expand Down
19 changes: 9 additions & 10 deletions channelz/internal/protoconv/socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@ import (
"net"
"time"

"github.com/golang/protobuf/ptypes"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/internal/channelz"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/protoadapt"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/timestamppb"
"google.golang.org/protobuf/types/known/wrapperspb"

wrpb "github.com/golang/protobuf/ptypes/wrappers"
channelzpb "google.golang.org/grpc/channelz/grpc_channelz_v1"
)

Expand All @@ -46,7 +45,7 @@ func securityToProto(se credentials.ChannelzSecurityValue) *channelzpb.Security
otherSecurity := &channelzpb.Security_OtherSecurity{
Name: v.Name,
}
if anyval, err := anypb.New(protoadapt.MessageV2Of(v.Value)); err == nil {
if anyval, err := anypb.New(v.Value); err == nil {
otherSecurity.Value = anyval
}
return &channelzpb.Security{Model: &channelzpb.Security_Other{Other: otherSecurity}}
Expand Down Expand Up @@ -89,22 +88,22 @@ func socketToProto(skt *channelz.Socket) *channelzpb.Socket {
MessagesReceived: skt.SocketMetrics.MessagesReceived.Load(),
KeepAlivesSent: skt.SocketMetrics.KeepAlivesSent.Load(),
}
if ts, err := ptypes.TimestampProto(time.Unix(0, skt.SocketMetrics.LastLocalStreamCreatedTimestamp.Load())); err == nil {
if ts := timestamppb.New(time.Unix(0, skt.SocketMetrics.LastLocalStreamCreatedTimestamp.Load())); ts.IsValid() {
s.Data.LastLocalStreamCreatedTimestamp = ts
}
if ts, err := ptypes.TimestampProto(time.Unix(0, skt.SocketMetrics.LastRemoteStreamCreatedTimestamp.Load())); err == nil {
if ts := timestamppb.New(time.Unix(0, skt.SocketMetrics.LastRemoteStreamCreatedTimestamp.Load())); ts.IsValid() {
s.Data.LastRemoteStreamCreatedTimestamp = ts
}
if ts, err := ptypes.TimestampProto(time.Unix(0, skt.SocketMetrics.LastMessageSentTimestamp.Load())); err == nil {
if ts := timestamppb.New(time.Unix(0, skt.SocketMetrics.LastMessageSentTimestamp.Load())); ts.IsValid() {
s.Data.LastMessageSentTimestamp = ts
}
if ts, err := ptypes.TimestampProto(time.Unix(0, skt.SocketMetrics.LastMessageReceivedTimestamp.Load())); err == nil {
if ts := timestamppb.New(time.Unix(0, skt.SocketMetrics.LastMessageReceivedTimestamp.Load())); ts.IsValid() {
s.Data.LastMessageReceivedTimestamp = ts
}
if skt.EphemeralMetrics != nil {
e := skt.EphemeralMetrics()
s.Data.LocalFlowControlWindow = &wrpb.Int64Value{Value: e.LocalFlowControlWindow}
s.Data.RemoteFlowControlWindow = &wrpb.Int64Value{Value: e.RemoteFlowControlWindow}
s.Data.LocalFlowControlWindow = wrapperspb.Int64(e.LocalFlowControlWindow)
s.Data.RemoteFlowControlWindow = wrapperspb.Int64(e.RemoteFlowControlWindow)
}

s.Data.Option = sockoptToProto(skt.SocketOptions)
Expand Down
13 changes: 6 additions & 7 deletions channelz/internal/protoconv/sockopt_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@ package protoconv
import (
"time"

"github.com/golang/protobuf/ptypes"
durpb "github.com/golang/protobuf/ptypes/duration"
channelzpb "google.golang.org/grpc/channelz/grpc_channelz_v1"
"google.golang.org/grpc/internal/channelz"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/durationpb"
)

func convertToPtypesDuration(sec int64, usec int64) *durpb.Duration {
return ptypes.DurationProto(time.Duration(sec*1e9 + usec*1e3))
func convertToPbDuration(sec int64, usec int64) *durationpb.Duration {
return durationpb.New(time.Duration(sec*1e9 + usec*1e3))
}

func sockoptToProto(skopts *channelz.SocketOptionData) []*channelzpb.SocketOption {
Expand All @@ -40,7 +39,7 @@ func sockoptToProto(skopts *channelz.SocketOptionData) []*channelzpb.SocketOptio
if skopts.Linger != nil {
additional, err := anypb.New(&channelzpb.SocketOptionLinger{
Active: skopts.Linger.Onoff != 0,
Duration: convertToPtypesDuration(int64(skopts.Linger.Linger), 0),
Duration: convertToPbDuration(int64(skopts.Linger.Linger), 0),
})
if err == nil {
opts = append(opts, &channelzpb.SocketOption{
Expand All @@ -53,7 +52,7 @@ func sockoptToProto(skopts *channelz.SocketOptionData) []*channelzpb.SocketOptio
}
if skopts.RecvTimeout != nil {
additional, err := anypb.New(&channelzpb.SocketOptionTimeout{
Duration: convertToPtypesDuration(int64(skopts.RecvTimeout.Sec), int64(skopts.RecvTimeout.Usec)),
Duration: convertToPbDuration(int64(skopts.RecvTimeout.Sec), int64(skopts.RecvTimeout.Usec)),
})
if err == nil {
opts = append(opts, &channelzpb.SocketOption{
Expand All @@ -66,7 +65,7 @@ func sockoptToProto(skopts *channelz.SocketOptionData) []*channelzpb.SocketOptio
}
if skopts.SendTimeout != nil {
additional, err := anypb.New(&channelzpb.SocketOptionTimeout{
Duration: convertToPtypesDuration(int64(skopts.SendTimeout.Sec), int64(skopts.SendTimeout.Usec)),
Duration: convertToPbDuration(int64(skopts.SendTimeout.Sec), int64(skopts.SendTimeout.Usec)),
})
if err == nil {
opts = append(opts, &channelzpb.SocketOption{
Expand Down
4 changes: 2 additions & 2 deletions channelz/internal/protoconv/subchannel.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ package protoconv
import (
"time"

"github.com/golang/protobuf/ptypes"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/internal/channelz"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/timestamppb"

channelzpb "google.golang.org/grpc/channelz/grpc_channelz_v1"
)
Expand All @@ -40,7 +40,7 @@ func subChannelToProto(cm *channelz.SubChannel) *channelzpb.Subchannel {
CallsSucceeded: cm.ChannelMetrics.CallsSucceeded.Load(),
CallsFailed: cm.ChannelMetrics.CallsFailed.Load(),
}
if ts, err := ptypes.TimestampProto(time.Unix(0, cm.ChannelMetrics.LastCallStartedTimestamp.Load())); err == nil {
if ts := timestamppb.New(time.Unix(0, cm.ChannelMetrics.LastCallStartedTimestamp.Load())); ts.IsValid() {
sc.Data.LastCallStartedTimestamp = ts
}

Expand Down
80 changes: 57 additions & 23 deletions channelz/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,19 @@ import (
"testing"
"time"

"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
"github.com/google/go-cmp/cmp"
"google.golang.org/grpc/connectivity"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/grpclog"
"google.golang.org/grpc/internal/channelz"
"google.golang.org/grpc/internal/grpctest"
"google.golang.org/protobuf/encoding/prototext"
"google.golang.org/protobuf/reflect/protodesc"
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/testing/protocmp"
"google.golang.org/protobuf/types/descriptorpb"
"google.golang.org/protobuf/types/dynamicpb"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/timestamppb"

channelzpb "google.golang.org/grpc/channelz/grpc_channelz_v1"
Expand Down Expand Up @@ -94,20 +98,6 @@ func convertSocketRefSliceToMap(sktRefs []*channelzpb.SocketRef) map[int64]strin
return m
}

type OtherSecurityValue struct {
LocalCertificate []byte `protobuf:"bytes,1,opt,name=local_certificate,json=localCertificate,proto3" json:"local_certificate,omitempty"`
RemoteCertificate []byte `protobuf:"bytes,2,opt,name=remote_certificate,json=remoteCertificate,proto3" json:"remote_certificate,omitempty"`
}

func (m *OtherSecurityValue) Reset() { *m = OtherSecurityValue{} }
func (m *OtherSecurityValue) String() string { return proto.CompactTextString(m) }
func (*OtherSecurityValue) ProtoMessage() {}

func init() {
// Ad-hoc registering the proto type here to facilitate UnmarshalAny of OtherSecurityValue.
proto.RegisterType((*OtherSecurityValue)(nil), "grpc.credentials.OtherChannelzSecurityValue")
}

func (s) TestGetTopChannels(t *testing.T) {
tcs := []*channelz.ChannelMetrics{
channelz.NewChannelMetricForTesting(
Expand Down Expand Up @@ -271,7 +261,7 @@ func (s) TestGetServerSockets(t *testing.T) {
ids[2]: refNames[2],
}
if got := convertSocketRefSliceToMap(resp.GetSocketRef()); !cmp.Equal(got, want) {
t.Fatalf("GetServerSockets want: %#v, got: %#v (resp=%v)", want, got, proto.MarshalTextString(resp))
t.Fatalf("GetServerSockets want: %#v, got: %#v (resp=%v)", want, got, prototext.Format(resp))
}

for i := 0; i < 50; i++ {
Expand Down Expand Up @@ -560,6 +550,47 @@ func newSocket(cs czSocket) *channelz.Socket {
return s
}

type OtherChannelzSecurityValue struct {
LocalCertificate []byte `protobuf:"bytes,1,opt,name=local_certificate,json=localCertificate,proto3" json:"local_certificate,omitempty"`
RemoteCertificate []byte `protobuf:"bytes,2,opt,name=remote_certificate,json=remoteCertificate,proto3" json:"remote_certificate,omitempty"`
}

func (x *OtherChannelzSecurityValue) Reset() {
*x = OtherChannelzSecurityValue{}
}

func (x *OtherChannelzSecurityValue) String() string {
return prototext.Format(x)
}

func (*OtherChannelzSecurityValue) ProtoMessage() {}

func (x OtherChannelzSecurityValue) ProtoReflect() protoreflect.Message {
const s = `
name: "service_test.proto"
syntax: "proto3"
package: "grpc.credentials",
message_type: [{
name: "OtherChannelzSecurityValue"
field: [
{name:"local_certificate" number:1 type:TYPE_BYTES},
{name:"remote_certificate" number:2 type:TYPE_BYTES}
]
}]
`
pb := new(descriptorpb.FileDescriptorProto)
if err := prototext.Unmarshal([]byte(s), pb); err != nil {
panic(err)
}
fd, err := protodesc.NewFile(pb, nil)
if err != nil {
panic(err)
}
md := fd.Messages().Get(0)
mt := dynamicpb.NewMessageType(md)
return mt.New()
}

func (s) TestGetSocket(t *testing.T) {
ss := []*channelz.Socket{newSocket(czSocket{
streamsStarted: 10,
Expand Down Expand Up @@ -622,12 +653,15 @@ func (s) TestGetSocket(t *testing.T) {
},
}), newSocket(czSocket{
security: &credentials.OtherChannelzSecurityValue{
Name: "YYYY",
Value: &OtherSecurityValue{LocalCertificate: []byte{1, 2, 3}, RemoteCertificate: []byte{4, 5, 6}},
Name: "YYYY",
Value: OtherChannelzSecurityValue{
LocalCertificate: []byte{1, 2, 3},
RemoteCertificate: []byte{4, 5, 6},
},
},
}),
}
otherSecVal, err := ptypes.MarshalAny(ss[6].Security.(*credentials.OtherChannelzSecurityValue).Value)
otherSecVal, err := anypb.New(ss[6].Security.(*credentials.OtherChannelzSecurityValue).Value)
if err != nil {
t.Fatal("Error marshalling proto:", err)
}
Expand Down Expand Up @@ -740,7 +774,7 @@ func (s) TestGetSocket(t *testing.T) {
for i := range ss {
resp, _ := svr.GetSocket(ctx, &channelzpb.GetSocketRequest{SocketId: skts[i].ID})
w := &channelzpb.Socket{}
if err := proto.UnmarshalText(want[i], w); err != nil {
if err := prototext.Unmarshal([]byte(want[i]), w); err != nil {
t.Fatalf("Error unmarshalling %q: %v", want[i], err)
}
if diff := cmp.Diff(resp.GetSocket(), w, protocmp.Transform()); diff != "" {
Expand All @@ -760,9 +794,9 @@ func escape(bs []byte) string {
func addr(a net.Addr) string {
switch a := a.(type) {
case *net.TCPAddr:
return string(a.IP)
return escape([]byte(a.IP))
case *net.IPAddr:
return string(a.IP)
return escape([]byte(a.IP))
}
return ""
}
4 changes: 2 additions & 2 deletions credentials/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (

"google.golang.org/grpc/attributes"
icredentials "google.golang.org/grpc/internal/credentials"
"google.golang.org/protobuf/protoadapt"
"google.golang.org/protobuf/proto"
)

// PerRPCCredentials defines the common interface for the credentials which need to
Expand Down Expand Up @@ -287,5 +287,5 @@ type ChannelzSecurityValue interface {
type OtherChannelzSecurityValue struct {
ChannelzSecurityValue
Name string
Value protoadapt.MessageV1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may be a breaking change. It is probably fine since it's experimental, and it's unlikely anyone is using it....so I think I'm OK with it. Just wanted to call it out.

Value proto.Message
}
4 changes: 2 additions & 2 deletions vet.sh
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ git grep -l 'interface{}' -- "*.go" 2>&1 | not grep -v '\.pb\.go\|protoc-gen-go-
# - Do not call grpclog directly. Use grpclog.Component instead.
git grep -l -e 'grpclog.I' --or -e 'grpclog.W' --or -e 'grpclog.E' --or -e 'grpclog.F' --or -e 'grpclog.V' -- "*.go" | not grep -v '^grpclog/component.go\|^internal/grpctest/tlogger_test.go'

# - Ensure all ptypes proto packages are renamed when importing.
not git grep "\(import \|^\s*\)\"github.com/golang/protobuf/ptypes/" -- "*.go"
# - Ensure that the deprecated protobuf dependency is not used.
not git grep "\"github.com/golang/protobuf/*" -- "*.go" ':(exclude)reflection/grpc_testing_not_regenerate/*'

# - Ensure all usages of grpc_testing package are renamed when importing.
not git grep "\(import \|^\s*\)\"google.golang.org/grpc/interop/grpc_testing" -- "*.go"
Expand Down
Loading