Skip to content

Commit

Permalink
Merge pull request #7519 from vmg/vmg/gogoproto
Browse files Browse the repository at this point in the history
perf: RPC Serialization
  • Loading branch information
shlomi-noach authored Feb 24, 2021
2 parents 81e6480 + a348a58 commit 5da35cd
Show file tree
Hide file tree
Showing 36 changed files with 76,488 additions and 2,733 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ java_test:
VTROOT=${PWD} mvn -f java/pom.xml -B clean verify

install_protoc-gen-go:
go install github.com/golang/protobuf/protoc-gen-go
go install github.com/gogo/protobuf/protoc-gen-gofast

PROTO_SRCS = $(wildcard proto/*.proto)
PROTO_SRC_NAMES = $(basename $(notdir $(PROTO_SRCS)))
Expand All @@ -187,7 +187,7 @@ endif

$(PROTO_GO_OUTS): minimaltools install_protoc-gen-go proto/*.proto
for name in $(PROTO_SRC_NAMES); do \
$(VTROOT)/bin/protoc --go_out=plugins=grpc:. -I${PWD}/dist/vt-protoc-3.6.1/include:proto proto/$${name}.proto && \
$(VTROOT)/bin/protoc --gofast_out=plugins=grpc:. -I${PWD}/dist/vt-protoc-3.6.1/include:proto proto/$${name}.proto && \
goimports -w vitess.io/vitess/go/vt/proto/$${name}/$${name}.pb.go; \
done
cp -Rf vitess.io/vitess/go/vt/proto/* go/vt/proto
Expand Down
4 changes: 2 additions & 2 deletions go/cmd/vtctldclient/internal/command/reparents.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ package command
import (
"time"

"github.com/golang/protobuf/ptypes"
"github.com/spf13/cobra"

"vitess.io/vitess/go/cmd/vtctldclient/cli"
"vitess.io/vitess/go/protoutil"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/topo/topoproto"

Expand Down Expand Up @@ -60,7 +60,7 @@ func commandInitShardPrimary(cmd *cobra.Command, args []string) error {
Keyspace: keyspace,
Shard: shard,
PrimaryElectTabletAlias: tabletAlias,
WaitReplicasTimeout: ptypes.DurationProto(initShardPrimaryOptions.WaitReplicasTimeout),
WaitReplicasTimeout: protoutil.DurationToProto(initShardPrimaryOptions.WaitReplicasTimeout),
Force: initShardPrimaryOptions.Force,
})

Expand Down
31 changes: 23 additions & 8 deletions go/protoutil/duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,41 @@ limitations under the License.
package protoutil

import (
"fmt"
"time"

"github.com/golang/protobuf/ptypes"

durationpb "github.com/golang/protobuf/ptypes/duration"
"vitess.io/vitess/go/vt/proto/vttime"
)

// DurationFromProto converts a durationpb type to a time.Duration. It returns a
// three-tuple of (dgo, ok, err) where dgo is the go time.Duration, ok indicates
// whether the proto value was set, and err is set on failure to convert the
// proto value.
func DurationFromProto(dpb *durationpb.Duration) (time.Duration, bool, error) {
func DurationFromProto(dpb *vttime.Duration) (time.Duration, bool, error) {
if dpb == nil {
return 0, false, nil
}

dgo, err := ptypes.Duration(dpb)
if err != nil {
return 0, true, err
d := time.Duration(dpb.Seconds) * time.Second
if int64(d/time.Second) != dpb.Seconds {
return 0, true, fmt.Errorf("duration: %v is out of range for time.Duration", dpb)
}
if dpb.Nanos != 0 {
d += time.Duration(dpb.Nanos) * time.Nanosecond
if (d < 0) != (dpb.Nanos < 0) {
return 0, true, fmt.Errorf("duration: %v is out of range for time.Duration", dpb)
}
}
return d, true, nil
}

return dgo, true, nil
// DurationToProto converts a time.Duration to a durpb.Duration.
func DurationToProto(d time.Duration) *vttime.Duration {
nanos := d.Nanoseconds()
secs := nanos / 1e9
nanos -= secs * 1e9
return &vttime.Duration{
Seconds: secs,
Nanos: int32(nanos),
}
}
9 changes: 4 additions & 5 deletions go/protoutil/duration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,22 @@ import (

"github.com/stretchr/testify/assert"

durationpb "github.com/golang/protobuf/ptypes/duration"
"vitess.io/vitess/go/vt/proto/vttime"
)

func TestDurationFromProto(t *testing.T) {
t.Parallel()

tests := []struct {
name string
in *durationpb.Duration
in *vttime.Duration
expected time.Duration
isOk bool
shouldErr bool
}{
{
name: "success",
in: &durationpb.Duration{Seconds: 1000},
in: &vttime.Duration{Seconds: 1000},
expected: time.Second * 1000,
isOk: true,
shouldErr: false,
Expand All @@ -51,7 +51,7 @@ func TestDurationFromProto(t *testing.T) {
},
{
name: "error",
in: &durationpb.Duration{
in: &vttime.Duration{
// This is the max allowed seconds for a durationpb, plus 1.
Seconds: int64(10000*365.25*24*60*60) + 1,
},
Expand All @@ -71,7 +71,6 @@ func TestDurationFromProto(t *testing.T) {
if tt.shouldErr {
assert.Error(t, err)
assert.Equal(t, tt.isOk, ok, "expected (_, ok, _) = DurationFromProto; to be ok = %v", tt.isOk)

return
}

Expand Down
Loading

0 comments on commit 5da35cd

Please sign in to comment.