From d503db616e1710d6eddde57602b2e3efb6ad352d Mon Sep 17 00:00:00 2001 From: Philip O'Toole Date: Mon, 16 Aug 2021 22:08:43 -0400 Subject: [PATCH] Better modeling with oneof --- CONTRIBUTING.md | 4 +- cluster/client.go | 1 + cluster/message.pb.go | 124 +++++++++++++++++++++++++++++------------- cluster/message.proto | 6 +- cluster/service.go | 51 ++++++++--------- 5 files changed, 120 insertions(+), 66 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 831187a6..f2e0ac13 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -52,10 +52,10 @@ export PATH=$PATH:$GOBIN export DEST_DIR=$GOPATH/src export SRC_DIR=$GOPATH/src/github.com/rqlite/rqlite/command -protoc -I=$SRC_DIR --proto_path=$GOPATHsrc/github.com/rqlite/rqlite --go_out=$DEST_DIR $SRC_DIR/command.proto +protoc -I=$SRC_DIR --proto_path=$GOPATH/src/github.com/rqlite/rqlite --go_out=$DEST_DIR $SRC_DIR/command.proto export SRC_DIR=$GOPATH/src/github.com/rqlite/rqlite/cluster -protoc -I=$SRC_DIR --proto_path=$GOPATHsrc/github.com/rqlite/rqlite --go_out=$DEST_DIR $SRC_DIR/message.proto +protoc -I=$SRC_DIR --proto_path=$GOPATH/src/github.com/rqlite/rqlite --go_out=$DEST_DIR $SRC_DIR/message.proto ``` ### Speeding up the build process diff --git a/cluster/client.go b/cluster/client.go index 341897bb..b4e7774e 100644 --- a/cluster/client.go +++ b/cluster/client.go @@ -7,6 +7,7 @@ import ( "time" "github.com/golang/protobuf/proto" + "github.com/rqlite/rqlite/command" ) // Client allows communicating with a remote node. diff --git a/cluster/message.pb.go b/cluster/message.pb.go index 4c1c255b..9fa47ddf 100644 --- a/cluster/message.pb.go +++ b/cluster/message.pb.go @@ -125,8 +125,11 @@ type Command struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Type Command_Type `protobuf:"varint,1,opt,name=type,proto3,enum=cluster.Command_Type" json:"type,omitempty"` - SubCommand []byte `protobuf:"bytes,2,opt,name=sub_command,json=subCommand,proto3" json:"sub_command,omitempty"` + Type Command_Type `protobuf:"varint,1,opt,name=type,proto3,enum=cluster.Command_Type" json:"type,omitempty"` + // Types that are assignable to Request: + // *Command_ExecuteRequest + // *Command_QueryRequest + Request isCommand_Request `protobuf_oneof:"request"` } func (x *Command) Reset() { @@ -168,13 +171,43 @@ func (x *Command) GetType() Command_Type { return Command_COMMAND_TYPE_UNKNOWN } -func (x *Command) GetSubCommand() []byte { - if x != nil { - return x.SubCommand +func (m *Command) GetRequest() isCommand_Request { + if m != nil { + return m.Request + } + return nil +} + +func (x *Command) GetExecuteRequest() *command.ExecuteRequest { + if x, ok := x.GetRequest().(*Command_ExecuteRequest); ok { + return x.ExecuteRequest + } + return nil +} + +func (x *Command) GetQueryRequest() *command.QueryRequest { + if x, ok := x.GetRequest().(*Command_QueryRequest); ok { + return x.QueryRequest } return nil } +type isCommand_Request interface { + isCommand_Request() +} + +type Command_ExecuteRequest struct { + ExecuteRequest *command.ExecuteRequest `protobuf:"bytes,2,opt,name=execute_request,json=executeRequest,proto3,oneof"` +} + +type Command_QueryRequest struct { + QueryRequest *command.QueryRequest `protobuf:"bytes,3,opt,name=query_request,json=queryRequest,proto3,oneof"` +} + +func (*Command_ExecuteRequest) isCommand_Request() {} + +func (*Command_QueryRequest) isCommand_Request() {} + type CommandExecuteResponse struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -292,34 +325,41 @@ var file_message_proto_rawDesc = []byte{ 0x07, 0x63, 0x6c, 0x75, 0x73, 0x74, 0x65, 0x72, 0x1a, 0x15, 0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x2f, 0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x1b, 0x0a, 0x07, 0x41, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x12, 0x10, 0x0a, 0x03, 0x75, 0x72, - 0x6c, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x75, 0x72, 0x6c, 0x22, 0xcc, 0x01, 0x0a, + 0x6c, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x75, 0x72, 0x6c, 0x22, 0xb8, 0x02, 0x0a, 0x07, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x12, 0x29, 0x0a, 0x04, 0x74, 0x79, 0x70, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x15, 0x2e, 0x63, 0x6c, 0x75, 0x73, 0x74, 0x65, 0x72, 0x2e, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x2e, 0x54, 0x79, 0x70, 0x65, 0x52, 0x04, 0x74, - 0x79, 0x70, 0x65, 0x12, 0x1f, 0x0a, 0x0b, 0x73, 0x75, 0x62, 0x5f, 0x63, 0x6f, 0x6d, 0x6d, 0x61, - 0x6e, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x0a, 0x73, 0x75, 0x62, 0x43, 0x6f, 0x6d, - 0x6d, 0x61, 0x6e, 0x64, 0x22, 0x75, 0x0a, 0x04, 0x54, 0x79, 0x70, 0x65, 0x12, 0x18, 0x0a, 0x14, - 0x43, 0x4f, 0x4d, 0x4d, 0x41, 0x4e, 0x44, 0x5f, 0x54, 0x59, 0x50, 0x45, 0x5f, 0x55, 0x4e, 0x4b, - 0x4e, 0x4f, 0x57, 0x4e, 0x10, 0x00, 0x12, 0x21, 0x0a, 0x1d, 0x43, 0x4f, 0x4d, 0x4d, 0x41, 0x4e, - 0x44, 0x5f, 0x54, 0x59, 0x50, 0x45, 0x5f, 0x47, 0x45, 0x54, 0x5f, 0x4e, 0x4f, 0x44, 0x45, 0x5f, - 0x41, 0x50, 0x49, 0x5f, 0x55, 0x52, 0x4c, 0x10, 0x01, 0x12, 0x18, 0x0a, 0x14, 0x43, 0x4f, 0x4d, - 0x4d, 0x41, 0x4e, 0x44, 0x5f, 0x54, 0x59, 0x50, 0x45, 0x5f, 0x45, 0x58, 0x45, 0x43, 0x55, 0x54, - 0x45, 0x10, 0x02, 0x12, 0x16, 0x0a, 0x12, 0x43, 0x4f, 0x4d, 0x4d, 0x41, 0x4e, 0x44, 0x5f, 0x54, - 0x59, 0x50, 0x45, 0x5f, 0x51, 0x55, 0x45, 0x52, 0x59, 0x10, 0x03, 0x22, 0x60, 0x0a, 0x16, 0x43, - 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x45, 0x78, 0x65, 0x63, 0x75, 0x74, 0x65, 0x52, 0x65, 0x73, - 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x18, 0x01, - 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x12, 0x30, 0x0a, 0x07, 0x72, - 0x65, 0x73, 0x75, 0x6c, 0x74, 0x73, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x16, 0x2e, 0x63, + 0x79, 0x70, 0x65, 0x12, 0x42, 0x0a, 0x0f, 0x65, 0x78, 0x65, 0x63, 0x75, 0x74, 0x65, 0x5f, 0x72, + 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x17, 0x2e, 0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x2e, 0x45, 0x78, 0x65, 0x63, 0x75, 0x74, 0x65, 0x52, 0x65, - 0x73, 0x75, 0x6c, 0x74, 0x52, 0x07, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x73, 0x22, 0x54, 0x0a, - 0x14, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x51, 0x75, 0x65, 0x72, 0x79, 0x52, 0x65, 0x73, - 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x18, 0x01, - 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x12, 0x26, 0x0a, 0x04, 0x72, - 0x6f, 0x77, 0x73, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x12, 0x2e, 0x63, 0x6f, 0x6d, 0x6d, - 0x61, 0x6e, 0x64, 0x2e, 0x51, 0x75, 0x65, 0x72, 0x79, 0x52, 0x6f, 0x77, 0x73, 0x52, 0x04, 0x72, - 0x6f, 0x77, 0x73, 0x42, 0x22, 0x5a, 0x20, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, - 0x6d, 0x2f, 0x72, 0x71, 0x6c, 0x69, 0x74, 0x65, 0x2f, 0x72, 0x71, 0x6c, 0x69, 0x74, 0x65, 0x2f, - 0x63, 0x6c, 0x75, 0x73, 0x74, 0x65, 0x72, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x71, 0x75, 0x65, 0x73, 0x74, 0x48, 0x00, 0x52, 0x0e, 0x65, 0x78, 0x65, 0x63, 0x75, 0x74, 0x65, + 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x3c, 0x0a, 0x0d, 0x71, 0x75, 0x65, 0x72, 0x79, + 0x5f, 0x72, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x15, + 0x2e, 0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x2e, 0x51, 0x75, 0x65, 0x72, 0x79, 0x52, 0x65, + 0x71, 0x75, 0x65, 0x73, 0x74, 0x48, 0x00, 0x52, 0x0c, 0x71, 0x75, 0x65, 0x72, 0x79, 0x52, 0x65, + 0x71, 0x75, 0x65, 0x73, 0x74, 0x22, 0x75, 0x0a, 0x04, 0x54, 0x79, 0x70, 0x65, 0x12, 0x18, 0x0a, + 0x14, 0x43, 0x4f, 0x4d, 0x4d, 0x41, 0x4e, 0x44, 0x5f, 0x54, 0x59, 0x50, 0x45, 0x5f, 0x55, 0x4e, + 0x4b, 0x4e, 0x4f, 0x57, 0x4e, 0x10, 0x00, 0x12, 0x21, 0x0a, 0x1d, 0x43, 0x4f, 0x4d, 0x4d, 0x41, + 0x4e, 0x44, 0x5f, 0x54, 0x59, 0x50, 0x45, 0x5f, 0x47, 0x45, 0x54, 0x5f, 0x4e, 0x4f, 0x44, 0x45, + 0x5f, 0x41, 0x50, 0x49, 0x5f, 0x55, 0x52, 0x4c, 0x10, 0x01, 0x12, 0x18, 0x0a, 0x14, 0x43, 0x4f, + 0x4d, 0x4d, 0x41, 0x4e, 0x44, 0x5f, 0x54, 0x59, 0x50, 0x45, 0x5f, 0x45, 0x58, 0x45, 0x43, 0x55, + 0x54, 0x45, 0x10, 0x02, 0x12, 0x16, 0x0a, 0x12, 0x43, 0x4f, 0x4d, 0x4d, 0x41, 0x4e, 0x44, 0x5f, + 0x54, 0x59, 0x50, 0x45, 0x5f, 0x51, 0x55, 0x45, 0x52, 0x59, 0x10, 0x03, 0x42, 0x09, 0x0a, 0x07, + 0x72, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x22, 0x60, 0x0a, 0x16, 0x43, 0x6f, 0x6d, 0x6d, 0x61, + 0x6e, 0x64, 0x45, 0x78, 0x65, 0x63, 0x75, 0x74, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, + 0x65, 0x12, 0x14, 0x0a, 0x05, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, + 0x52, 0x05, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x12, 0x30, 0x0a, 0x07, 0x72, 0x65, 0x73, 0x75, 0x6c, + 0x74, 0x73, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x16, 0x2e, 0x63, 0x6f, 0x6d, 0x6d, 0x61, + 0x6e, 0x64, 0x2e, 0x45, 0x78, 0x65, 0x63, 0x75, 0x74, 0x65, 0x52, 0x65, 0x73, 0x75, 0x6c, 0x74, + 0x52, 0x07, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x73, 0x22, 0x54, 0x0a, 0x14, 0x43, 0x6f, 0x6d, + 0x6d, 0x61, 0x6e, 0x64, 0x51, 0x75, 0x65, 0x72, 0x79, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, + 0x65, 0x12, 0x14, 0x0a, 0x05, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, + 0x52, 0x05, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x12, 0x26, 0x0a, 0x04, 0x72, 0x6f, 0x77, 0x73, 0x18, + 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x12, 0x2e, 0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x2e, + 0x51, 0x75, 0x65, 0x72, 0x79, 0x52, 0x6f, 0x77, 0x73, 0x52, 0x04, 0x72, 0x6f, 0x77, 0x73, 0x42, + 0x22, 0x5a, 0x20, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x72, 0x71, + 0x6c, 0x69, 0x74, 0x65, 0x2f, 0x72, 0x71, 0x6c, 0x69, 0x74, 0x65, 0x2f, 0x63, 0x6c, 0x75, 0x73, + 0x74, 0x65, 0x72, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -342,18 +382,22 @@ var file_message_proto_goTypes = []interface{}{ (*Command)(nil), // 2: cluster.Command (*CommandExecuteResponse)(nil), // 3: cluster.CommandExecuteResponse (*CommandQueryResponse)(nil), // 4: cluster.CommandQueryResponse - (*command.ExecuteResult)(nil), // 5: command.ExecuteResult - (*command.QueryRows)(nil), // 6: command.QueryRows + (*command.ExecuteRequest)(nil), // 5: command.ExecuteRequest + (*command.QueryRequest)(nil), // 6: command.QueryRequest + (*command.ExecuteResult)(nil), // 7: command.ExecuteResult + (*command.QueryRows)(nil), // 8: command.QueryRows } var file_message_proto_depIdxs = []int32{ 0, // 0: cluster.Command.type:type_name -> cluster.Command.Type - 5, // 1: cluster.CommandExecuteResponse.results:type_name -> command.ExecuteResult - 6, // 2: cluster.CommandQueryResponse.rows:type_name -> command.QueryRows - 3, // [3:3] is the sub-list for method output_type - 3, // [3:3] is the sub-list for method input_type - 3, // [3:3] is the sub-list for extension type_name - 3, // [3:3] is the sub-list for extension extendee - 0, // [0:3] is the sub-list for field type_name + 5, // 1: cluster.Command.execute_request:type_name -> command.ExecuteRequest + 6, // 2: cluster.Command.query_request:type_name -> command.QueryRequest + 7, // 3: cluster.CommandExecuteResponse.results:type_name -> command.ExecuteResult + 8, // 4: cluster.CommandQueryResponse.rows:type_name -> command.QueryRows + 5, // [5:5] is the sub-list for method output_type + 5, // [5:5] is the sub-list for method input_type + 5, // [5:5] is the sub-list for extension type_name + 5, // [5:5] is the sub-list for extension extendee + 0, // [0:5] is the sub-list for field type_name } func init() { file_message_proto_init() } @@ -411,6 +455,10 @@ func file_message_proto_init() { } } } + file_message_proto_msgTypes[1].OneofWrappers = []interface{}{ + (*Command_ExecuteRequest)(nil), + (*Command_QueryRequest)(nil), + } type x struct{} out := protoimpl.TypeBuilder{ File: protoimpl.DescBuilder{ diff --git a/cluster/message.proto b/cluster/message.proto index bd59373c..d7a04427 100644 --- a/cluster/message.proto +++ b/cluster/message.proto @@ -17,7 +17,11 @@ message Command { COMMAND_TYPE_QUERY = 3; } Type type = 1; - bytes sub_command = 2; + + oneof request { + command.ExecuteRequest execute_request = 2; + command.QueryRequest query_request = 3; + } } message CommandExecuteResponse { diff --git a/cluster/service.go b/cluster/service.go index e6c7fb56..07fd245a 100644 --- a/cluster/service.go +++ b/cluster/service.go @@ -200,22 +200,23 @@ func (s *Service) handleConn(conn net.Conn) { case Command_COMMAND_TYPE_EXECUTE: stats.Add(numExecuteRequest, 1) - er := &command.ExecuteRequest{} - if err = proto.Unmarshal(c.SubCommand, er); err != nil { - // Write some error, then close? - return - } - resp := &CommandExecuteResponse{} - res, err := s.db.Execute(er) - if err != nil { - resp.Error = err.Error() + + er := c.GetExecuteRequest() + if er == nil { + resp.Error = "ExecuteRequest is nil" } else { - resp.Results = make([]*command.ExecuteResult, len(res)) - for i := range res { - resp.Results[i] = res[i] + res, err := s.db.Execute(er) + if err != nil { + resp.Error = err.Error() + } else { + resp.Results = make([]*command.ExecuteResult, len(res)) + for i := range res { + resp.Results[i] = res[i] + } } } + b, err = proto.Marshal(resp) if err != nil { return @@ -224,24 +225,24 @@ func (s *Service) handleConn(conn net.Conn) { case Command_COMMAND_TYPE_QUERY: stats.Add(numQueryRequest, 1) - return - - qr := &command.QueryRequest{} - if err = proto.Unmarshal(c.SubCommand, qr); err != nil { - // Write some error, then close? - return - } resp := &CommandQueryResponse{} - res, err := s.db.Query(qr) - if err != nil { - resp.Error = err.Error() + + qr := c.GetQueryRequest() + if qr == nil { + resp.Error = "QueryRequest is nil" } else { - resp.Rows = make([]*command.QueryRows, len(res)) - for i := range res { - resp.Rows[i] = res[i] + res, err := s.db.Query(qr) + if err != nil { + resp.Error = err.Error() + } else { + resp.Rows = make([]*command.QueryRows, len(res)) + for i := range res { + resp.Rows[i] = res[i] + } } } + b, err = proto.Marshal(resp) if err != nil { return