From 429ea54a2963da5d3f3d5597f18bd437383a49fe Mon Sep 17 00:00:00 2001 From: aleksander-vedvik Date: Thu, 29 Feb 2024 10:31:46 +0100 Subject: [PATCH] refactor(protoc): incorporated replySpec into qSpec --- broadcastTypes.go | 6 ++++++ cmd/protoc-gen-gorums/dev/config.go | 7 +++---- cmd/protoc-gen-gorums/dev/server.go | 3 +-- .../dev/zorums_broadcastcall_gorums.pb.go | 18 ++++++++-------- .../dev/zorums_clientserver_gorums.pb.go | 6 ------ .../dev/zorums_qspec_gorums.pb.go | 21 +++++++++++++++++++ .../gengorums/template_broadcastcall.go | 6 +++--- .../gengorums/template_clientserver.go | 2 +- .../gengorums/template_qspec.go | 8 +++++-- .../gengorums/template_static.go | 12 +++++------ 10 files changed, 55 insertions(+), 34 deletions(-) diff --git a/broadcastTypes.go b/broadcastTypes.go index 081e0511..e6aeb038 100644 --- a/broadcastTypes.go +++ b/broadcastTypes.go @@ -151,6 +151,12 @@ func WithoutUniquenessChecks() BroadcastOption { } } +// not sure if this is necessary because the implementer +// can decide to run the broadcast in a go routine. +func WithoutWaiting() BroadcastOption { + return func(b *BroadcastOptions) {} +} + type BroadcastOptions struct { ServerAddresses []string GossipPercentage float32 diff --git a/cmd/protoc-gen-gorums/dev/config.go b/cmd/protoc-gen-gorums/dev/config.go index 1c672477..142565f6 100644 --- a/cmd/protoc-gen-gorums/dev/config.go +++ b/cmd/protoc-gen-gorums/dev/config.go @@ -8,10 +8,9 @@ import ( // procedure calls may be invoked. type Configuration struct { gorums.RawConfiguration - nodes []*Node - qspec QuorumSpec - srv *clientServerImpl - replySpec ReplySpec + nodes []*Node + qspec QuorumSpec + srv *clientServerImpl } // ConfigurationFromRaw returns a new Configuration from the given raw configuration and QuorumSpec. diff --git a/cmd/protoc-gen-gorums/dev/server.go b/cmd/protoc-gen-gorums/dev/server.go index c044dd76..694d60f5 100644 --- a/cmd/protoc-gen-gorums/dev/server.go +++ b/cmd/protoc-gen-gorums/dev/server.go @@ -59,7 +59,7 @@ type clientServerImpl struct { grpcServer *grpc.Server } -func (c *Configuration) RegisterClientServer(listenAddr string, replySpec ReplySpec, opts ...grpc.ServerOption) error { +func (c *Configuration) RegisterClientServer(listenAddr string, opts ...grpc.ServerOption) error { srvImpl := &clientServerImpl{ grpcServer: grpc.NewServer(opts...), } @@ -71,6 +71,5 @@ func (c *Configuration) RegisterClientServer(listenAddr string, replySpec ReplyS go srvImpl.grpcServer.Serve(lis) srvImpl.ClientServer = srv c.srv = srvImpl - c.replySpec = replySpec return nil } diff --git a/cmd/protoc-gen-gorums/dev/zorums_broadcastcall_gorums.pb.go b/cmd/protoc-gen-gorums/dev/zorums_broadcastcall_gorums.pb.go index f6fa407b..185b771d 100644 --- a/cmd/protoc-gen-gorums/dev/zorums_broadcastcall_gorums.pb.go +++ b/cmd/protoc-gen-gorums/dev/zorums_broadcastcall_gorums.pb.go @@ -37,10 +37,10 @@ func (c *Configuration) BroadcastWithClientHandler1(ctx context.Context, in *Req if c.srv == nil { return nil, fmt.Errorf("a client server is not defined. Use configuration.RegisterClientServer() to define a client server") } - if c.replySpec == nil { - return nil, fmt.Errorf("a reply spec is not defined. Use configuration.RegisterClientServer() to define a reply spec") + if c.qspec == nil { + return nil, fmt.Errorf("a qspec is not defined.") } - doneChan, cd := c.srv.AddRequest(ctx, in, gorums.ConvertToType(c.replySpec.BroadcastWithClientHandler1)) + doneChan, cd := c.srv.AddRequest(ctx, in, gorums.ConvertToType(c.qspec.BroadcastWithClientHandler1QF)) c.RawConfiguration.Multicast(ctx, cd, gorums.WithNoSendWaiting()) response, ok := <-doneChan if !ok { @@ -66,10 +66,10 @@ func (c *Configuration) BroadcastWithClientHandler2(ctx context.Context, in *Req if c.srv == nil { return nil, fmt.Errorf("a client server is not defined. Use configuration.RegisterClientServer() to define a client server") } - if c.replySpec == nil { - return nil, fmt.Errorf("a reply spec is not defined. Use configuration.RegisterClientServer() to define a reply spec") + if c.qspec == nil { + return nil, fmt.Errorf("a qspec is not defined.") } - doneChan, cd := c.srv.AddRequest(ctx, in, gorums.ConvertToType(c.replySpec.BroadcastWithClientHandler2)) + doneChan, cd := c.srv.AddRequest(ctx, in, gorums.ConvertToType(c.qspec.BroadcastWithClientHandler2QF)) c.RawConfiguration.Multicast(ctx, cd, gorums.WithNoSendWaiting()) response, ok := <-doneChan if !ok { @@ -95,10 +95,10 @@ func (c *Configuration) BroadcastWithClientHandlerAndBroadcastOption(ctx context if c.srv == nil { return nil, fmt.Errorf("a client server is not defined. Use configuration.RegisterClientServer() to define a client server") } - if c.replySpec == nil { - return nil, fmt.Errorf("a reply spec is not defined. Use configuration.RegisterClientServer() to define a reply spec") + if c.qspec == nil { + return nil, fmt.Errorf("a qspec is not defined.") } - doneChan, cd := c.srv.AddRequest(ctx, in, gorums.ConvertToType(c.replySpec.BroadcastWithClientHandlerAndBroadcastOption)) + doneChan, cd := c.srv.AddRequest(ctx, in, gorums.ConvertToType(c.qspec.BroadcastWithClientHandlerAndBroadcastOptionQF)) c.RawConfiguration.Multicast(ctx, cd, gorums.WithNoSendWaiting()) response, ok := <-doneChan if !ok { diff --git a/cmd/protoc-gen-gorums/dev/zorums_clientserver_gorums.pb.go b/cmd/protoc-gen-gorums/dev/zorums_clientserver_gorums.pb.go index ad865527..aa60b197 100644 --- a/cmd/protoc-gen-gorums/dev/zorums_clientserver_gorums.pb.go +++ b/cmd/protoc-gen-gorums/dev/zorums_clientserver_gorums.pb.go @@ -47,9 +47,3 @@ var clientServer_ServiceDesc = grpc.ServiceDesc{ Streams: []grpc.StreamDesc{}, Metadata: "", } - -type ReplySpec interface { - BroadcastWithClientHandler1(reqs []*Response) (*Response, bool) - BroadcastWithClientHandler2(reqs []*ClientResponse) (*ClientResponse, bool) - BroadcastWithClientHandlerAndBroadcastOption(reqs []*ClientResponse) (*ClientResponse, bool) -} diff --git a/cmd/protoc-gen-gorums/dev/zorums_qspec_gorums.pb.go b/cmd/protoc-gen-gorums/dev/zorums_qspec_gorums.pb.go index 929fde49..9891cafa 100644 --- a/cmd/protoc-gen-gorums/dev/zorums_qspec_gorums.pb.go +++ b/cmd/protoc-gen-gorums/dev/zorums_qspec_gorums.pb.go @@ -71,6 +71,27 @@ type QuorumSpec interface { // you should implement your quorum function with '_ *Request'. QuorumCallWithBroadcastQF(in *Request, replies map[uint32]*Response) (*Response, bool) + // BroadcastWithClientHandler1QF is the quorum function for the BroadcastWithClientHandler1 + // broadcastcall call method. The in parameter is the request object + // supplied to the BroadcastWithClientHandler1 method at call time, and may or may not + // be used by the quorum function. If the in parameter is not needed + // you should implement your quorum function with '_ *Request'. + BroadcastWithClientHandler1QF(replies []*Response) (*Response, bool) + + // BroadcastWithClientHandler2QF is the quorum function for the BroadcastWithClientHandler2 + // broadcastcall call method. The in parameter is the request object + // supplied to the BroadcastWithClientHandler2 method at call time, and may or may not + // be used by the quorum function. If the in parameter is not needed + // you should implement your quorum function with '_ *Request'. + BroadcastWithClientHandler2QF(replies []*ClientResponse) (*ClientResponse, bool) + + // BroadcastWithClientHandlerAndBroadcastOptionQF is the quorum function for the BroadcastWithClientHandlerAndBroadcastOption + // broadcast call method. The in parameter is the request object + // supplied to the BroadcastWithClientHandlerAndBroadcastOption method at call time, and may or may not + // be used by the quorum function. If the in parameter is not needed + // you should implement your quorum function with '_ *Request'. + BroadcastWithClientHandlerAndBroadcastOptionQF(replies []*ClientResponse) (*ClientResponse, bool) + // QuorumCallAsyncQF is the quorum function for the QuorumCallAsync // asynchronous quorum call method. The in parameter is the request object // supplied to the QuorumCallAsync method at call time, and may or may not diff --git a/cmd/protoc-gen-gorums/gengorums/template_broadcastcall.go b/cmd/protoc-gen-gorums/gengorums/template_broadcastcall.go index 98f420a9..a43f8429 100644 --- a/cmd/protoc-gen-gorums/gengorums/template_broadcastcall.go +++ b/cmd/protoc-gen-gorums/gengorums/template_broadcastcall.go @@ -32,10 +32,10 @@ func (c *Configuration) {{.Method.GoName}}(ctx context.Context, in *{{in .GenFil if c.srv == nil { return nil, fmt.Errorf("a client server is not defined. Use configuration.RegisterClientServer() to define a client server") } - if c.replySpec == nil { - return nil, fmt.Errorf("a reply spec is not defined. Use configuration.RegisterClientServer() to define a reply spec") + if c.qspec == nil { + return nil, fmt.Errorf("a qspec is not defined.") } - doneChan, cd := c.srv.AddRequest(ctx, in, gorums.ConvertToType(c.replySpec.{{.Method.GoName}})) + doneChan, cd := c.srv.AddRequest(ctx, in, gorums.ConvertToType(c.qspec.{{.Method.GoName}}QF)) c.RawConfiguration.Multicast(ctx, cd, gorums.WithNoSendWaiting()) response, ok := <-doneChan if !ok { diff --git a/cmd/protoc-gen-gorums/gengorums/template_clientserver.go b/cmd/protoc-gen-gorums/gengorums/template_clientserver.go index 510d7785..167a50cc 100644 --- a/cmd/protoc-gen-gorums/gengorums/template_clientserver.go +++ b/cmd/protoc-gen-gorums/gengorums/template_clientserver.go @@ -57,4 +57,4 @@ type ReplySpec interface { {{- end}} ` -var clientServer = clientServerVariables + clientServerInterface + clientServerDesc + clientServerImplInterface +var clientServer = clientServerVariables + clientServerInterface + clientServerDesc diff --git a/cmd/protoc-gen-gorums/gengorums/template_qspec.go b/cmd/protoc-gen-gorums/gengorums/template_qspec.go index 45f691cd..7b5065d2 100644 --- a/cmd/protoc-gen-gorums/gengorums/template_qspec.go +++ b/cmd/protoc-gen-gorums/gengorums/template_qspec.go @@ -24,7 +24,11 @@ type QuorumSpec interface { // supplied to the {{$method}} method at call time, and may or may not // be used by the quorum function. If the in parameter is not needed // you should implement your quorum function with '_ *{{$in}}'. + {{- if isBroadcastCall .}} + {{.GoName}}QF(replies []*{{$out}}) (*{{$out}}, bool) + {{- else}} {{$method}}QF(in *{{$in}}, replies map[uint32]*{{$out}}) (*{{$customOut}}{{withCorrectable . ", int"}}, bool) + {{- end}} {{end}} } {{end}} @@ -34,8 +38,8 @@ type QuorumSpec interface { // a quorum function; that is, all except multicast and plain gRPC methods. func qspecMethods(methods []*protogen.Method) (s []*protogen.Method) { for _, method := range methods { - if hasMethodOption(method, gorums.E_Multicast, gorums.E_Unicast, gorums.E_Broadcastcall) || !hasGorumsCallType(method) { - // ignore multicast, broadcast and non-Gorums methods + if hasMethodOption(method, gorums.E_Multicast, gorums.E_Unicast) || !hasGorumsCallType(method) { + // ignore multicast and non-Gorums methods continue } s = append(s, method) diff --git a/cmd/protoc-gen-gorums/gengorums/template_static.go b/cmd/protoc-gen-gorums/gengorums/template_static.go index 4090bc87..4ae03707 100644 --- a/cmd/protoc-gen-gorums/gengorums/template_static.go +++ b/cmd/protoc-gen-gorums/gengorums/template_static.go @@ -9,16 +9,15 @@ var pkgIdentMap = map[string]string{"fmt": "Errorf", "github.com/relab/gorums": // reservedIdents holds the set of Gorums reserved identifiers. // These identifiers cannot be used to define message types in a proto file. -var reservedIdents = []string{"Broadcast", "Configuration", "Manager", "Node", "QuorumSpec", "ReplySpec", "Server"} +var reservedIdents = []string{"Broadcast", "Configuration", "Manager", "Node", "QuorumSpec", "Server"} var staticCode = `// A Configuration represents a static set of nodes on which quorum remote // procedure calls may be invoked. type Configuration struct { gorums.RawConfiguration - nodes []*Node - qspec QuorumSpec - srv *clientServerImpl - replySpec ReplySpec + nodes []*Node + qspec QuorumSpec + srv *clientServerImpl } // ConfigurationFromRaw returns a new Configuration from the given raw configuration and QuorumSpec. @@ -190,7 +189,7 @@ type clientServerImpl struct { grpcServer *grpc.Server } -func (c *Configuration) RegisterClientServer(listenAddr string, replySpec ReplySpec, opts ...grpc.ServerOption) error { +func (c *Configuration) RegisterClientServer(listenAddr string, opts ...grpc.ServerOption) error { srvImpl := &clientServerImpl{ grpcServer: grpc.NewServer(opts...), } @@ -202,7 +201,6 @@ func (c *Configuration) RegisterClientServer(listenAddr string, replySpec ReplyS go srvImpl.grpcServer.Serve(lis) srvImpl.ClientServer = srv c.srv = srvImpl - c.replySpec = replySpec return nil }