-
Notifications
You must be signed in to change notification settings - Fork 14
/
config_gorums.pb.go
198 lines (177 loc) · 6.61 KB
/
config_gorums.pb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Code generated by protoc-gen-gorums. DO NOT EDIT.
// versions:
// protoc-gen-gorums v0.7.0-devel
// protoc v3.19.2
// source: config/config.proto
package config
import (
context "context"
fmt "fmt"
gorums "github.com/relab/gorums"
encoding "google.golang.org/grpc/encoding"
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
)
const (
// Verify that this generated code is sufficiently up-to-date.
_ = gorums.EnforceVersion(7 - gorums.MinVersion)
// Verify that the gorums runtime is sufficiently up-to-date.
_ = gorums.EnforceVersion(gorums.MaxVersion - 7)
)
// 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
}
// ConfigurationFromRaw returns a new Configuration from the given raw configuration and QuorumSpec.
//
// This function may for example be used to "clone" a configuration but install a different QuorumSpec:
// cfg1, err := mgr.NewConfiguration(qspec1, opts...)
// cfg2 := ConfigurationFromRaw(cfg1.RawConfig, qspec2)
func ConfigurationFromRaw(rawCfg gorums.RawConfiguration, qspec QuorumSpec) *Configuration {
// return an error if the QuorumSpec interface is not empty and no implementation was provided.
var test interface{} = struct{}{}
if _, empty := test.(QuorumSpec); !empty && qspec == nil {
panic("QuorumSpec may not be nil")
}
return &Configuration{
RawConfiguration: rawCfg,
qspec: qspec,
}
}
// Nodes returns a slice of each available node. IDs are returned in the same
// order as they were provided in the creation of the Manager.
//
// NOTE: mutating the returned slice is not supported.
func (c *Configuration) Nodes() []*Node {
if c.nodes == nil {
c.nodes = make([]*Node, 0, c.Size())
for _, n := range c.RawConfiguration {
c.nodes = append(c.nodes, &Node{n})
}
}
return c.nodes
}
// And returns a NodeListOption that can be used to create a new configuration combining c and d.
func (c Configuration) And(d *Configuration) gorums.NodeListOption {
return c.RawConfiguration.And(d.RawConfiguration)
}
// Except returns a NodeListOption that can be used to create a new configuration
// from c without the nodes in rm.
func (c Configuration) Except(rm *Configuration) gorums.NodeListOption {
return c.RawConfiguration.Except(rm.RawConfiguration)
}
func init() {
if encoding.GetCodec(gorums.ContentSubtype) == nil {
encoding.RegisterCodec(gorums.NewCodec())
}
}
// Manager maintains a connection pool of nodes on
// which quorum calls can be performed.
type Manager struct {
*gorums.RawManager
}
// NewManager returns a new Manager for managing connection to nodes added
// to the manager. This function accepts manager options used to configure
// various aspects of the manager.
func NewManager(opts ...gorums.ManagerOption) (mgr *Manager) {
mgr = &Manager{}
mgr.RawManager = gorums.NewRawManager(opts...)
return mgr
}
// NewConfiguration returns a configuration based on the provided list of nodes (required)
// and an optional quorum specification. The QuorumSpec is necessary for call types that
// must process replies. For configurations only used for unicast or multicast call types,
// a QuorumSpec is not needed. The QuorumSpec interface is also a ConfigOption.
// Nodes can be supplied using WithNodeMap or WithNodeList, or WithNodeIDs.
// A new configuration can also be created from an existing configuration,
// using the And, WithNewNodes, Except, and WithoutNodes methods.
func (m *Manager) NewConfiguration(opts ...gorums.ConfigOption) (c *Configuration, err error) {
if len(opts) < 1 || len(opts) > 2 {
return nil, fmt.Errorf("wrong number of options: %d", len(opts))
}
c = &Configuration{}
for _, opt := range opts {
switch v := opt.(type) {
case gorums.NodeListOption:
c.RawConfiguration, err = gorums.NewRawConfiguration(m.RawManager, v)
if err != nil {
return nil, err
}
case QuorumSpec:
// Must be last since v may match QuorumSpec if it is interface{}
c.qspec = v
default:
return nil, fmt.Errorf("unknown option type: %v", v)
}
}
// return an error if the QuorumSpec interface is not empty and no implementation was provided.
var test interface{} = struct{}{}
if _, empty := test.(QuorumSpec); !empty && c.qspec == nil {
return nil, fmt.Errorf("missing required QuorumSpec")
}
return c, nil
}
// Nodes returns a slice of available nodes on this manager.
// IDs are returned in the order they were added at creation of the manager.
func (m *Manager) Nodes() []*Node {
gorumsNodes := m.RawManager.Nodes()
nodes := make([]*Node, 0, len(gorumsNodes))
for _, n := range gorumsNodes {
nodes = append(nodes, &Node{n})
}
return nodes
}
// Node encapsulates the state of a node on which a remote procedure call
// can be performed.
type Node struct {
*gorums.RawNode
}
// QuorumSpec is the interface of quorum functions for ConfigTest.
type QuorumSpec interface {
gorums.ConfigOption
// ConfigQF is the quorum function for the Config
// quorum call method. The in parameter is the request object
// supplied to the Config 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'.
ConfigQF(in *Request, replies map[uint32]*Response) (*Response, bool)
}
// Config is a quorum call invoked on all nodes in configuration c,
// with the same argument in, and returns a combined result.
func (c *Configuration) Config(ctx context.Context, in *Request) (resp *Response, err error) {
cd := gorums.QuorumCallData{
Message: in,
Method: "config.ConfigTest.Config",
}
cd.QuorumFunction = func(req protoreflect.ProtoMessage, replies map[uint32]protoreflect.ProtoMessage) (protoreflect.ProtoMessage, bool) {
r := make(map[uint32]*Response, len(replies))
for k, v := range replies {
r[k] = v.(*Response)
}
return c.qspec.ConfigQF(req.(*Request), r)
}
res, err := c.RawConfiguration.QuorumCall(ctx, cd)
if err != nil {
return nil, err
}
return res.(*Response), err
}
// ConfigTest is the server-side API for the ConfigTest Service
type ConfigTest interface {
Config(ctx gorums.ServerCtx, request *Request) (response *Response, err error)
}
func RegisterConfigTestServer(srv *gorums.Server, impl ConfigTest) {
srv.RegisterHandler("config.ConfigTest.Config", func(ctx gorums.ServerCtx, in *gorums.Message, finished chan<- *gorums.Message) {
req := in.Message.(*Request)
defer ctx.Release()
resp, err := impl.Config(ctx, req)
gorums.SendMessage(ctx, finished, gorums.WrapMessage(in.Metadata, resp, err))
})
}
type internalResponse struct {
nid uint32
reply *Response
err error
}