-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
observer.go
380 lines (349 loc) · 9.38 KB
/
observer.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
// Copyright 2020 Authors of Cilium
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package relay
import (
"context"
"io"
"time"
observerpb "github.com/cilium/cilium/api/v1/observer"
relaypb "github.com/cilium/cilium/api/v1/relay"
"github.com/cilium/cilium/pkg/hubble/relay/queue"
nodeTypes "github.com/cilium/cilium/pkg/node/types"
"github.com/golang/protobuf/ptypes"
"github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// ensure that Server implements the observer.ObserverServer interface.
var _ observerpb.ObserverServer = (*Server)(nil)
func retrieveFlowsFromPeer(
ctx context.Context,
conn *grpc.ClientConn,
req *observerpb.GetFlowsRequest,
flows chan<- *observerpb.GetFlowsResponse,
) error {
client := observerpb.NewObserverClient(conn)
c, err := client.GetFlows(ctx, req)
if err != nil {
return err
}
for {
flow, err := c.Recv()
switch err {
case io.EOF, context.Canceled:
return nil
case nil:
select {
case flows <- flow:
case <-ctx.Done():
return nil
}
default:
if status.Code(err) != codes.Canceled {
return err
}
return nil
}
}
}
func sortFlows(
ctx context.Context,
flows <-chan *observerpb.GetFlowsResponse,
qlen int,
bufferDrainTimeout time.Duration,
) <-chan *observerpb.GetFlowsResponse {
pq := queue.NewPriorityQueue(qlen)
sortedFlows := make(chan *observerpb.GetFlowsResponse, qlen)
go func() {
defer close(sortedFlows)
flowsLoop:
for {
select {
case flow, ok := <-flows:
if !ok {
break flowsLoop
}
if pq.Len() == qlen {
f := pq.Pop()
select {
case sortedFlows <- f:
case <-ctx.Done():
return
}
}
pq.Push(flow)
case <-time.After(bufferDrainTimeout): // make sure to drain the queue when no new flow responses are received
if f := pq.Pop(); f != nil {
select {
case sortedFlows <- f:
case <-ctx.Done():
return
}
}
case <-ctx.Done():
return
}
}
// drain the queue
for f := pq.Pop(); f != nil; f = pq.Pop() {
select {
case sortedFlows <- f:
case <-ctx.Done():
return
}
}
}()
return sortedFlows
}
func nodeStatusError(err error, nodeNames ...string) *observerpb.GetFlowsResponse {
msg := err.Error()
if s, ok := status.FromError(err); ok && s.Code() == codes.Unknown {
msg = s.Message()
}
return &observerpb.GetFlowsResponse{
NodeName: nodeTypes.GetName(),
Time: ptypes.TimestampNow(),
ResponseTypes: &observerpb.GetFlowsResponse_NodeStatus{
NodeStatus: &relaypb.NodeStatusEvent{
StateChange: relaypb.NodeState_NODE_ERROR,
NodeNames: nodeNames,
Message: msg,
},
},
}
}
func nodeStatusEvent(state relaypb.NodeState, nodeNames ...string) *observerpb.GetFlowsResponse {
return &observerpb.GetFlowsResponse{
NodeName: nodeTypes.GetName(),
Time: ptypes.TimestampNow(),
ResponseTypes: &observerpb.GetFlowsResponse_NodeStatus{
NodeStatus: &relaypb.NodeStatusEvent{
StateChange: state,
NodeNames: nodeNames,
},
},
}
}
func aggregateErrors(
ctx context.Context,
responses <-chan *observerpb.GetFlowsResponse,
errorAggregationWindow time.Duration,
) <-chan *observerpb.GetFlowsResponse {
aggregated := make(chan *observerpb.GetFlowsResponse, cap(responses))
var flushPending <-chan time.Time
var pendingResponse *observerpb.GetFlowsResponse
go func() {
defer close(aggregated)
aggregateErrorsLoop:
for {
select {
case response, ok := <-responses:
if !ok {
// flush any pending response before exiting
if pendingResponse != nil {
select {
case aggregated <- pendingResponse:
case <-ctx.Done():
}
}
return
}
// any non-error responses are directly forwarded
current := response.GetNodeStatus()
if current.GetStateChange() != relaypb.NodeState_NODE_ERROR {
select {
case aggregated <- response:
continue aggregateErrorsLoop
case <-ctx.Done():
return
}
}
// either merge with pending or flush it
if pending := pendingResponse.GetNodeStatus(); pending != nil {
if current.GetMessage() == pending.GetMessage() {
pending.NodeNames = append(pending.NodeNames, current.NodeNames...)
continue aggregateErrorsLoop
}
select {
case aggregated <- pendingResponse:
case <-ctx.Done():
return
}
}
pendingResponse = response
flushPending = time.After(errorAggregationWindow)
case <-flushPending:
select {
case aggregated <- pendingResponse:
pendingResponse = nil
flushPending = nil
case <-ctx.Done():
return
}
case <-ctx.Done():
return
}
}
}()
return aggregated
}
// GetFlows implements observer.ObserverServer.GetFlows by proxying requests to
// the hubble instance the proxy is connected to.
func (s *Server) GetFlows(req *observerpb.GetFlowsRequest, stream observerpb.Observer_GetFlowsServer) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func() {
select {
case <-s.stop:
cancel()
case <-ctx.Done():
}
}()
peers := s.peerList()
qlen := s.opts.BufferMaxLen // we don't want to buffer too many flows
if nqlen := req.GetNumber() * uint64(len(peers)); nqlen > 0 && nqlen < uint64(qlen) {
// don't make the queue bigger than necessary as it would be a problem
// with the priority queue (we pop out when the queue is full)
qlen = int(nqlen)
}
g, gctx := errgroup.WithContext(ctx)
flows := make(chan *observerpb.GetFlowsResponse, qlen)
var connectedNodes, unavailableNodes []string
for _, p := range peers {
p := p
if p.conn == nil || p.connErr != nil {
s.log.WithField("address", p.Address.String()).Infof(
"No connection to peer %s, skipping", p.Name,
)
unavailableNodes = append(unavailableNodes, p.Name)
go s.connectPeer(p.Name, p.Address.String())
continue
}
connectedNodes = append(connectedNodes, p.Name)
g.Go(func() error {
// retrieveFlowsFromPeer returns blocks until the peer finishes
// the request by closing the connection, an error occurs,
// or gctx expires.
err := retrieveFlowsFromPeer(gctx, p.conn, req, flows)
if err != nil {
s.log.WithFields(logrus.Fields{
"error": err,
"peer": p,
}).Warning("Failed to retrieve flows from peer")
select {
case flows <- nodeStatusError(err, p.Name):
case <-gctx.Done():
}
}
return nil
})
}
go func() {
g.Wait()
close(flows)
}()
aggregated := aggregateErrors(ctx, flows, s.opts.ErrorAggregationWindow)
sortedFlows := sortFlows(ctx, aggregated, qlen, s.opts.BufferDrainTimeout)
// inform the client about the nodes from which we expect to receive flows first
if len(connectedNodes) > 0 {
status := nodeStatusEvent(relaypb.NodeState_NODE_CONNECTED, connectedNodes...)
if err := stream.Send(status); err != nil {
return err
}
}
if len(unavailableNodes) > 0 {
status := nodeStatusEvent(relaypb.NodeState_NODE_UNAVAILABLE, unavailableNodes...)
if err := stream.Send(status); err != nil {
return err
}
}
sortedFlowsLoop:
for {
select {
case flow, ok := <-sortedFlows:
if !ok {
break sortedFlowsLoop
}
if err := stream.Send(flow); err != nil {
return err
}
case <-ctx.Done():
break sortedFlowsLoop
}
}
return g.Wait()
}
// ServerStatus implements observer.ObserverServer.ServerStatus by aggregating
// the ServerStatus answer of all hubble peers.
func (s *Server) ServerStatus(ctx context.Context, req *observerpb.ServerStatusRequest) (*observerpb.ServerStatusResponse, error) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
g, ctx := errgroup.WithContext(ctx)
go func() {
select {
case <-s.stop:
cancel()
case <-ctx.Done():
}
}()
peers := s.peerList()
statuses := make(chan *observerpb.ServerStatusResponse, len(peers))
for _, p := range peers {
p := p
if p.conn == nil || p.connErr != nil {
s.log.WithField("address", p.Address.String()).Infof(
"No connection to peer %s, skipping", p.Name,
)
go s.connectPeer(p.Name, p.Address.String())
continue
}
g.Go(func() error {
client := observerpb.NewObserverClient(p.conn)
status, err := client.ServerStatus(ctx, req)
if err != nil {
s.log.WithFields(logrus.Fields{
"error": err,
"peer": p,
}).Warning("Failed to retrieve server status")
return nil
}
select {
case statuses <- status:
case <-ctx.Done():
}
return nil
})
}
go func() {
g.Wait()
close(statuses)
}()
resp := &observerpb.ServerStatusResponse{}
for status := range statuses {
if status == nil {
continue
}
resp.MaxFlows += status.MaxFlows
resp.NumFlows += status.NumFlows
resp.SeenFlows += status.SeenFlows
// use the oldest uptime as a reference for the uptime as cumulating
// values would make little sense
if resp.UptimeNs == 0 || resp.UptimeNs > status.UptimeNs {
resp.UptimeNs = status.UptimeNs
}
}
return resp, g.Wait()
}