-
Notifications
You must be signed in to change notification settings - Fork 0
/
loadbalancer.go
504 lines (428 loc) · 13.3 KB
/
loadbalancer.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
package goutube
import (
"context"
"errors"
"io"
"sync"
streaming_api "github.com/Brijeshlakkad/goutube/api/streaming/v1"
"github.com/Brijeshlakkad/ring"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
grpc_auth "github.com/grpc-ecosystem/go-grpc-middleware/auth"
"github.com/hashicorp/go-hclog"
"go.opencensus.io/plugin/ocgrpc"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
var (
ErrCannotHandleRequest = errors.New("couldn't handle the request")
ErrMultiStreamMetadataCorrupted = errors.New("metadata cannot be verified")
ErrWorkersNotFound = errors.New("workers not found")
)
// loadBalancer finds the responsible server for the provided request.
// If the request relates to producing, it will redirect it to the leader of the cluster responsible for the respective object.
// For now, the request relating to consuming will get redirected to the replica of that object cluster in the round-robin fashion.
type loadBalancer struct {
streaming_api.UnimplementedStreamingServer
*loadbalancerConfig
connPool map[ServerAddress][]*grpc.ClientConn
connPoolLock sync.Mutex
// streamCtx is used to cancel existing connection handlers.
streamCtx context.Context
streamCancel context.CancelFunc
streamCtxLock sync.RWMutex
shutdown bool
shutdownCh chan struct{}
shutdownLock sync.Mutex
maxPool int
logger hclog.Logger
cache map[ServerAddress]*followerCache // Todo: TTL and invalidate bad addresses
}
type loadbalancerConfig struct {
id string
ring *ring.Ring
Authorizer *authorizer
MaxPool int
Logger hclog.Logger
MultiStreamPercent int
}
func newLoadBalancer(config *loadbalancerConfig) (*loadBalancer, error) {
if config.Logger == nil {
config.Logger = hclog.New(&hclog.LoggerOptions{
Name: "load-balancer-net",
Output: hclog.DefaultOutput,
Level: hclog.DefaultLevel,
})
}
lb := &loadBalancer{
loadbalancerConfig: config,
connPool: make(map[ServerAddress][]*grpc.ClientConn),
logger: config.Logger,
maxPool: config.MaxPool,
shutdownCh: make(chan struct{}),
cache: make(map[ServerAddress]*followerCache),
}
// Create the connection context and then start our listener.
lb.setupStreamContext()
return lb, nil
}
func NewLoadBalancer(config *loadbalancerConfig, opts ...grpc.ServerOption) (*grpc.Server, error) {
lb, err := newLoadBalancer(config)
if err != nil {
return nil, err
}
opts = append(opts,
grpc.StreamInterceptor(
grpc_middleware.ChainStreamServer(
grpc_auth.StreamServerInterceptor(authenticate),
)),
grpc.UnaryInterceptor(
grpc_middleware.ChainUnaryServer(
grpc_auth.UnaryServerInterceptor(authenticate),
)),
grpc.StatsHandler(&ocgrpc.ServerHandler{}),
)
gRPCServer := grpc.NewServer(opts...)
streaming_api.RegisterStreamingServer(gRPCServer, lb)
return gRPCServer, nil
}
func (lb *loadBalancer) ProduceStream(stream streaming_api.Streaming_ProduceStreamServer) error {
if err := lb.Authorizer.Authorize(
subject(stream.Context()),
objectWildCard,
produceAction,
); err != nil {
return err
}
var conn *grpc.ClientConn
returnTheConnection := false
req, err := stream.Recv()
if err == io.EOF {
// can return the connection back.
returnTheConnection = true
} else if err != nil {
// cannot return the connection back to the pool.
return err
} else {
// This will return the rpc address of the leader node.
leaderNodeTags, found := lb.ring.GetNode(req.Point)
if !found {
return ErrCannotHandleRequest
}
conn, err = lb.getConn(ServerAddress(leaderNodeTags[rpcAddressRingTag]))
if err != nil {
return err
}
client := streaming_api.NewStreamingClient(conn)
produceStream, err := client.ProduceStream(lb.streamCtx)
if err != nil {
return err
}
// Forward the request to designated server.
for {
if err = produceStream.Send(req); err != nil {
return err
}
req, err = stream.Recv()
if err == io.EOF {
// Close the forwarding stream.
resp, err := produceStream.CloseAndRecv()
if err != nil {
return err
}
// Send the response and close the client stream.
if err := stream.SendAndClose(resp); err != nil {
return err
}
// can return the connection back.
returnTheConnection = true
break
} else if err != nil {
// cannot return the connection back to the pool.
return err
}
}
}
// return the connection back to the pool.
if returnTheConnection {
lb.returnConn(conn)
}
return nil
}
func (lb *loadBalancer) ConsumeStream(req *streaming_api.ConsumeRequest, stream streaming_api.Streaming_ConsumeStreamServer) error {
if err := lb.Authorizer.Authorize(
subject(stream.Context()),
objectWildCard,
consumeAction,
); err != nil {
return err
}
// This will return the rpc address of the leader node.
shardNodeTags, found := lb.ring.GetNode(req.Point)
if !found {
return ErrCannotHandleRequest
}
shardNodeRPCAddr := ServerAddress(shardNodeTags[rpcAddressRingTag])
leaderConn, err := lb.getConn(shardNodeRPCAddr)
if err != nil {
return err
}
var conn *grpc.ClientConn
_, followerCached := lb.cache[shardNodeRPCAddr]
if !followerCached {
resolverHelperClient := streaming_api.NewFollowerResolverHelperClient(leaderConn)
var resp *streaming_api.GetFollowersResponse
resp, err = resolverHelperClient.GetFollowers(lb.streamCtx, &streaming_api.GetFollowersRequest{})
// cache the list of followers for this leader.
lb.cache[shardNodeRPCAddr] = NewFollowerCache(resp.Servers, lb.MultiStreamPercent)
}
cache, _ := lb.cache[shardNodeRPCAddr]
followerAddress, found := cache.getNextFollower()
if err != nil || !found {
// Try forwarding the request to the leader node of the object replication cluster.
conn = leaderConn
} else {
conn, err = lb.getConn(followerAddress)
if err != nil {
return err
}
}
client := streaming_api.NewStreamingClient(conn)
clientStream, err := client.ConsumeStream(lb.streamCtx, req)
if err != nil {
return err
}
returnTheConnection := false
CONSUME:
for {
select {
case <-stream.Context().Done():
return nil
default:
resp, err := clientStream.Recv()
if err == io.EOF {
returnTheConnection = true
// we've reached the end of the stream
break CONSUME
}
if err := stream.Send(resp); err != nil {
return err
}
}
}
// return the connection back to the pool.
if returnTheConnection {
lb.returnConn(conn)
}
return nil
}
// GetMetadata fetches the metadata from the followers of the object replication cluster and verifies that these workers can serve the request.
func (lb *loadBalancer) GetMetadata(ctx context.Context, req *streaming_api.MetadataRequest) (*streaming_api.MetadataResponse, error) {
// This will return the rpc address of the leader node.
shardNodeTags, found := lb.ring.GetNode(req.Point)
if !found {
return nil, ErrCannotHandleRequest
}
shardNodeRPCAddr := ServerAddress(shardNodeTags[rpcAddressRingTag])
leaderConn, err := lb.getConn(shardNodeRPCAddr)
if err != nil {
return nil, err
}
_, followerCached := lb.cache[shardNodeRPCAddr]
if !followerCached {
resolverHelperClient := streaming_api.NewFollowerResolverHelperClient(leaderConn)
var resp *streaming_api.GetFollowersResponse
resp, err = resolverHelperClient.GetFollowers(lb.streamCtx, &streaming_api.GetFollowersRequest{})
// cache the list of followers for this leader.
lb.cache[shardNodeRPCAddr] = NewFollowerCache(resp.Servers, lb.MultiStreamPercent)
}
cache, _ := lb.cache[shardNodeRPCAddr]
followerAddresses, found := cache.getNextFollowers()
conns := make([]*grpc.ClientConn, len(followerAddresses))
// collects connections of the followers.
for i, followerAddress := range followerAddresses {
conns[i], err = lb.getConn(followerAddress)
if err != nil {
return nil, err
}
}
serverCount := len(conns)
// create clients and fetch metadata of the requested point
clients := make([]streaming_api.StreamingClient, serverCount)
var metadata *streaming_api.MetadataResponse = nil
metadataRequest := &streaming_api.MetadataRequest{
Point: req.Point,
}
for i := 0; i < serverCount; i++ {
clients[i] = streaming_api.NewStreamingClient(conns[i])
metadataResp, err := clients[i].GetMetadata(lb.streamCtx, metadataRequest)
if err != nil {
return nil, err
}
if i != 0 {
// compare metadata with other servers
if metadata.Size != metadataResp.Size {
return nil, ErrMultiStreamMetadataCorrupted
}
} else {
metadata = metadataResp
}
}
var workers []string
for _, conn := range conns {
workers = append(workers, conn.Target())
}
if len(workers) == 0 {
return nil, ErrWorkersNotFound
}
metadata.Workers = workers
return metadata, nil
}
// returnConn returns a connection back to the pool.
func (lb *loadBalancer) returnConn(conn *grpc.ClientConn) {
lb.connPoolLock.Lock()
defer lb.connPoolLock.Unlock()
key := ServerAddress(conn.Target())
conns, _ := lb.connPool[key]
if !lb.IsShutdown() && len(conns) < lb.maxPool {
lb.connPool[key] = append(conns, conn)
} else {
conn.Close()
}
}
// setupStreamContext is used to create a new stream context. This should be
// called with the stream lock held.
func (lb *loadBalancer) setupStreamContext() {
ctx, cancel := context.WithCancel(context.Background())
lb.streamCtx = ctx
lb.streamCancel = cancel
}
// getStreamContext is used retrieve the current stream context.
func (lb *loadBalancer) getStreamContext() context.Context {
lb.streamCtxLock.RLock()
defer lb.streamCtxLock.RUnlock()
return lb.streamCtx
}
// getExistingConn is used to grab a pooled connection.
func (lb *loadBalancer) getPooledConn(target ServerAddress) *grpc.ClientConn {
lb.connPoolLock.Lock()
defer lb.connPoolLock.Unlock()
conns, ok := lb.connPool[target]
if !ok || len(conns) == 0 {
return nil
}
var conn *grpc.ClientConn
num := len(conns)
conn, conns[num-1] = conns[num-1], nil
lb.connPool[target] = conns[:num-1]
return conn
}
// getConn is used to get a connection from the pool.
func (lb *loadBalancer) getConn(target ServerAddress) (*grpc.ClientConn, error) {
// Check for a pooled conn
if conn := lb.getPooledConn(target); conn != nil {
return conn, nil
}
peerTLSConfig, err := SetupTLSConfig(TLSConfig{
CertFile: RootClientCertFile,
KeyFile: RootClientKeyFile,
CAFile: CAFile,
Server: false,
ServerAddress: "127.0.0.1",
})
if err != nil {
return nil, err
}
tlsCreds := credentials.NewTLS(peerTLSConfig)
opts := []grpc.DialOption{grpc.WithTransportCredentials(tlsCreds)}
// Dial a new connection
conn, err := grpc.Dial(string(target), opts...)
return conn, nil
}
// CloseStreams closes the current streams.
func (lb *loadBalancer) CloseStreams() {
lb.connPoolLock.Lock()
defer lb.connPoolLock.Unlock()
// Shutdown all the connections in the connection pool and then remove their entry.
for k, e := range lb.connPool {
for _, conn := range e {
conn.Close()
}
delete(lb.connPool, k)
}
// Cancel the existing connections and create a new context. Both these
// operations must always be done with the lock held otherwise we can create
// connection handlers that are holding a context that will never be
// cancelable.
lb.streamCtxLock.Lock()
lb.streamCancel()
lb.setupStreamContext()
lb.streamCtxLock.Unlock()
}
// Close is used to stop the network transport.
func (lb *loadBalancer) Close() error {
lb.shutdownLock.Lock()
defer lb.shutdownLock.Unlock()
if !lb.shutdown {
close(lb.shutdownCh)
lb.CloseStreams()
lb.shutdown = true
}
return nil
}
// IsShutdown is used to check if the transport is shutdown.
func (lb *loadBalancer) IsShutdown() bool {
select {
case <-lb.shutdownCh:
return true
default:
return false
}
}
// followerCache stores the addresses of the followers and keeps the track of which follower should be requested next.
type followerCache struct {
followers map[uint8]ServerAddress
nextFollowerIndex uint8
lock sync.Mutex
multiStreamPercent int
}
// NewFollowerCache caches the provided followers. multiStreamPercent is used to calculate the percent of followers that will be used to read stream parallel.
func NewFollowerCache(followers []*streaming_api.Server, multiStreamPercent int) *followerCache {
fc := &followerCache{
followers: make(map[uint8]ServerAddress),
nextFollowerIndex: 0,
multiStreamPercent: multiStreamPercent,
}
i := uint8(0)
for _, follower := range followers {
fc.followers[i] = ServerAddress(follower.RpcAddr)
i++
}
return fc
}
// getNextFollower returns the next follower from the cache.
func (f *followerCache) getNextFollower() (ServerAddress, bool) {
if len(f.followers) == 0 {
return "", false
}
curIndex := f.nextFollowerIndex
f.lock.Lock()
f.nextFollowerIndex = (f.nextFollowerIndex + 1) % uint8(len(f.followers))
f.lock.Unlock()
return f.followers[curIndex], true
}
// getNextFollowers returns the number of followers to consume stream from multiple replicas.
func (f *followerCache) getNextFollowers() ([]ServerAddress, bool) {
if len(f.followers) == 0 {
return nil, false
}
count := len(f.followers) * f.multiStreamPercent / 100
followers := make([]ServerAddress, count)
for i := 0; i < count; i++ {
follower, found := f.getNextFollower()
if !found {
return nil, false
}
followers[i] = follower
}
return followers, true
}