-
Notifications
You must be signed in to change notification settings - Fork 14
/
template_correctable.go
139 lines (127 loc) · 3.73 KB
/
template_correctable.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
package gengorums
var correctableCallVariables = `
{{$context := use "context.Context" .GenFile}}
{{$opts := use "grpc.CallOption" .GenFile}}
{{$correctableOut := outType .Method $customOut}}
{{$appendFn := printf "append"}}
{{if correctableStream .Method}}
{{$appendFn = printf "appendIfNotPresent"}}
{{end}}
`
var correctableCallComment = `
{{$comments := .Method.Comments.Leading}}
{{if ne $comments ""}}
{{$comments -}}
{{else}}
{{if hasPerNodeArg .Method}}
// {{$method}} asynchronously invokes a correctable quorum call on each node
// in configuration c, with the argument returned by the provided function f
// and returns a {{$correctableOut}}, which can be used to inspect
// the reply and error when available.
// The provide per node function f takes the provided {{$in}} argument
// and returns an {{$out}} object to be passed to the given nodeID.
// The per node function f should be thread-safe.
{{else}}
// {{$method}} asynchronously invokes a correctable quorum call on each node
// in configuration c and returns a {{$correctableOut}}, which can be used
// to inspect any replies or errors when available.
{{if correctableStream .Method -}}
// This method supports server-side preliminary replies (correctable stream).
{{end -}}
{{end -}}
{{end -}}
`
var correctableCallSignature = `func (c *Configuration) {{$method}}(` +
`ctx {{$context}}, in *{{$in}}` +
`{{perNodeFnType .GenFile .Method ", f"}}` +
`, opts ...{{$opts}}) ` +
`*{{$correctableOut}} {`
var correctableCallBody = `
corr := &{{$correctableOut}}{
level: LevelNotSet,
NodeIDs: make([]uint32, 0, c.n),
donech: make(chan struct{}),
}
go c.{{unexport .Method.GoName}}(ctx, in{{perNodeArg .Method ", f"}}, corr, opts...)
return corr
}
`
var correctableCallUnexportedSignature = `
func (c *Configuration) {{unexport .Method.GoName}}(` +
`ctx {{$context}}, in *{{$in}}` +
`{{perNodeFnType .GenFile .Method ", f"}}` +
`, resp *{{$correctableOut}}, opts ...{{$opts}}) {
`
var correctableCallReply = `
var (
replies = make(map[uint32]*{{$out}}, c.n*2)
clevel = LevelNotSet
reply *{{$customOut}}
rlevel int
errs []GRPCError
quorum bool
)
for {
select {
case r := <-replyChan:
resp.NodeIDs = {{$appendFn}}(resp.NodeIDs, r.nid)
if r.err != nil {
errs = append(errs, GRPCError{r.nid, r.err})
break
}
{{template "traceLazyLog"}}
replies[r.nid] = r.reply
reply, rlevel, quorum = c.qspec.{{$method}}QF(in, replies)
if quorum {
resp.set(reply, rlevel, nil, true)
return
}
if rlevel > clevel {
clevel = rlevel
resp.set(reply, rlevel, nil, false)
}
case <-ctx.Done():
resp.set(reply, clevel, QuorumCallError{ctx.Err().Error(), len(replies), errs}, true)
return
}
{{- if correctableStream .Method}}
if len(errs) == expected { // Can't rely on reply count.
{{else}}
if len(errs)+len(replies) == expected {
{{end -}}
resp.set(reply, clevel, QuorumCallError{"incomplete call", len(replies), errs}, true)
return
}
}
}
`
var correctableStreamCallGrpc = `
func (n *Node) {{$method}}(ctx {{$context}}, in *{{$in}}, replyChan chan<- {{$intOut}}) {
x := New{{serviceName .Method}}Client(n.conn)
y, err := x.{{$method}}(ctx, in)
if err != nil {
replyChan <- {{$intOut}}{n.id, nil, err}
return
}
for {
reply, err := y.Recv()
if err == {{use "io.EOF" .GenFile}} {
return
}
replyChan <- {{$intOut}}{n.id, reply, err}
if err != nil {
return
}
}
}
`
var correctableCommon = commonVariables +
correctableCallVariables +
correctableCallComment +
correctableCallSignature +
correctableCallBody +
correctableCallUnexportedSignature +
quorumCallLoop +
correctableCallReply
var correctableCall = correctableCommon + nodeCallGrpc
var correctableStreamCall = correctableCommon + correctableStreamCallGrpc