forked from asonawalla/gazette
-
Notifications
You must be signed in to change notification settings - Fork 3
/
broker.go
207 lines (175 loc) · 5.98 KB
/
broker.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
199
200
201
202
203
204
205
206
207
package teststub
import (
"context"
"io"
pb "github.com/LiveRamp/gazette/v2/pkg/protocol"
"github.com/LiveRamp/gazette/v2/pkg/server"
gc "github.com/go-check/check"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc"
)
// LoopbackServer serves a JournalServer over a loopback, for use within tests.
type LoopbackServer struct {
*server.Server
Conn *grpc.ClientConn
}
// NewLoopbackServer returns a LoopbackServer of the provided JournalServer.
func NewLoopbackServer(ctx context.Context, journalServer pb.JournalServer) LoopbackServer {
var srv, err = server.New("127.0.0.1", 0)
if err != nil {
panic(err)
}
pb.RegisterJournalServer(srv.GRPCServer, journalServer)
var conn = srv.MustGRPCLoopback()
// Arrange to stop the server when |ctx| is cancelled.
go func() {
<-ctx.Done()
_ = conn.Close()
srv.GracefulStop()
}()
go srv.MustServe()
return LoopbackServer{Server: srv, Conn: conn}
}
// MustClient returns a JournalClient of the test LoopbackServer.
func (s LoopbackServer) MustClient() pb.JournalClient { return pb.NewJournalClient(s.Conn) }
// Broker stubs the read and write loops of broker RPCs, routing them onto
// channels which can be synchronously read and written within test bodies.
type Broker struct {
LoopbackServer
c *gc.C
ReplReqCh chan *pb.ReplicateRequest
ReplRespCh chan *pb.ReplicateResponse
ReadReqCh chan *pb.ReadRequest
ReadRespCh chan *pb.ReadResponse
AppendReqCh chan *pb.AppendRequest
AppendRespCh chan *pb.AppendResponse
ListFunc func(context.Context, *pb.ListRequest) (*pb.ListResponse, error)
ApplyFunc func(context.Context, *pb.ApplyRequest) (*pb.ApplyResponse, error)
FragmentsFunc func(context.Context, *pb.FragmentsRequest) (*pb.FragmentsResponse, error)
ErrCh chan error
}
// NewBroker returns a Broker instance served by a local GRPC server.
func NewBroker(c *gc.C, ctx context.Context) *Broker {
var p = &Broker{
c: c,
ReplReqCh: make(chan *pb.ReplicateRequest),
ReplRespCh: make(chan *pb.ReplicateResponse),
ReadReqCh: make(chan *pb.ReadRequest),
ReadRespCh: make(chan *pb.ReadResponse),
AppendReqCh: make(chan *pb.AppendRequest),
AppendRespCh: make(chan *pb.AppendResponse),
ErrCh: make(chan error),
}
p.LoopbackServer = NewLoopbackServer(ctx, p)
return p
}
// Replicate implements the JournalServer interface by proxying requests &
// responses through channels ReplReqCh & ReplRespCh.
func (p *Broker) Replicate(srv pb.Journal_ReplicateServer) error {
// Start a read loop of requests from |srv|.
go func() {
log.WithField("id", p.Endpoint()).Info("replicate read loop started")
for done := false; !done; {
var msg, err = srv.Recv()
if err == io.EOF {
msg, err, done = nil, nil, true
} else if err != nil {
done = true
p.c.Check(err, gc.ErrorMatches, `rpc error: code = (Canceled|DeadlineExceeded) .*`)
}
log.WithFields(log.Fields{"ep": p.Endpoint(), "msg": msg, "err": err, "done": done}).Info("read")
select {
case p.ReplReqCh <- msg:
// Pass.
case <-p.Ctx.Done():
done = true
}
}
}()
for {
select {
case resp := <-p.ReplRespCh:
p.c.Check(srv.Send(resp), gc.IsNil)
log.WithFields(log.Fields{"ep": p.Endpoint(), "resp": resp}).Info("sent")
case err := <-p.ErrCh:
log.WithFields(log.Fields{"ep": p.Endpoint(), "err": err}).Info("closing")
return err
case <-p.Ctx.Done():
log.WithFields(log.Fields{"ep": p.Endpoint()}).Info("cancelled")
return p.Ctx.Err()
}
}
}
// Read implements the JournalServer interface by proxying requests & responses
// through channels ReadReqCh & ReadResponseCh.
func (p *Broker) Read(req *pb.ReadRequest, srv pb.Journal_ReadServer) error {
select {
case p.ReadReqCh <- req:
// Pass.
case <-p.Ctx.Done():
return p.Ctx.Err()
}
for {
select {
case resp := <-p.ReadRespCh:
srv.Send(resp) // This may return cancelled context error.
log.WithFields(log.Fields{"ep": p.Endpoint(), "resp": resp}).Info("sent")
case err := <-p.ErrCh:
log.WithFields(log.Fields{"ep": p.Endpoint(), "err": err}).Info("closing")
return err
case <-p.Ctx.Done():
log.WithFields(log.Fields{"ep": p.Endpoint()}).Info("cancelled")
return p.Ctx.Err()
}
}
}
// Append implements the JournalServer interface by proxying requests &
// responses through channels AppendReqCh & AppendRespCh.
func (p *Broker) Append(srv pb.Journal_AppendServer) error {
// Start a read loop of requests from |srv|.
go func() {
log.WithField("ep", p.Endpoint()).Info("append read loop started")
for done := false; !done; {
var msg, err = srv.Recv()
if err == io.EOF {
msg, err, done = nil, nil, true
} else if err != nil {
done = true
p.c.Check(err, gc.ErrorMatches, `rpc error: code = Canceled desc = context canceled`)
}
log.WithFields(log.Fields{"ep": p.Endpoint(), "msg": msg, "err": err, "done": done}).Info("read")
select {
case p.AppendReqCh <- msg:
// Pass.
case <-p.Ctx.Done():
done = true
}
}
}()
for {
select {
case resp := <-p.AppendRespCh:
log.WithFields(log.Fields{"ep": p.Endpoint(), "resp": resp}).Info("sending")
return srv.SendAndClose(resp)
case err := <-p.ErrCh:
log.WithFields(log.Fields{"ep": p.Endpoint(), "err": err}).Info("closing")
return err
case <-p.Ctx.Done():
log.WithFields(log.Fields{"ep": p.Endpoint()}).Info("cancelled")
return p.Ctx.Err()
}
}
}
// List implements the JournalServer interface by proxying through ListFunc.
func (p *Broker) List(ctx context.Context, req *pb.ListRequest) (*pb.ListResponse, error) {
return p.ListFunc(ctx, req)
}
// Apply implements the JournalServer interface by proxying through ApplyFunc.
func (p *Broker) Apply(ctx context.Context, req *pb.ApplyRequest) (*pb.ApplyResponse, error) {
return p.ApplyFunc(ctx, req)
}
// Fragments implements the JournalServer interface by proxying through FragmentsFunc.
func (p *Broker) Fragments(ctx context.Context, req *pb.FragmentsRequest) (*pb.FragmentsResponse, error) {
return p.FragmentsFunc(ctx, req)
}
func init() { pb.RegisterGRPCDispatcher("local") }