This repository has been archived by the owner on Apr 7, 2020. It is now read-only.
forked from istio/istio
/
server.go
354 lines (304 loc) · 11 KB
/
server.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
// Copyright 2018 Istio Authors
//
// 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 server
import (
"fmt"
"io"
"strconv"
"sync/atomic"
"time"
"golang.org/x/time/rate"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/peer"
"google.golang.org/grpc/status"
mcp "istio.io/api/mcp/v1alpha1"
"istio.io/istio/pkg/log"
"istio.io/istio/pkg/mcp/env"
"istio.io/istio/pkg/mcp/internal"
"istio.io/istio/pkg/mcp/monitoring"
"istio.io/istio/pkg/mcp/source"
)
var scope = log.RegisterScope("mcp", "mcp debugging", 0)
var (
// For the purposes of rate limiting new connections, this controls how many
// new connections are allowed as a burst every NEW_CONNECTION_FREQ.
newConnectionBurstSize = env.Integer("NEW_CONNECTION_BURST_SIZE", 10)
// For the purposes of rate limiting new connections, this controls how
// frequently new bursts of connections are allowed.
newConnectionFreq = env.Duration("NEW_CONNECTION_FREQ", 10*time.Millisecond)
)
var _ mcp.AggregatedMeshConfigServiceServer = &Server{}
// Server implements the Mesh Configuration Protocol (MCP) gRPC server.
type Server struct {
watcher source.Watcher
collections []source.CollectionOptions
nextStreamID int64
// for auth check
authCheck AuthChecker
connections int64
reporter monitoring.Reporter
newConnectionLimiter *rate.Limiter
}
// AuthChecker is used to check the transport auth info that is associated with each stream. If the function
// returns nil, then the connection will be allowed. If the function returns an error, then it will be
// percolated up to the gRPC stack.
//
// Note that it is possible that this method can be called with nil authInfo. This can happen either if there
// is no peer info, or if the underlying gRPC stream is insecure. The implementations should be resilient in
// this case and apply appropriate policy.
type AuthChecker interface {
Check(authInfo credentials.AuthInfo) error
}
// watch maintains local push state of the most recent watch per-type.
type watch struct {
// only accessed from connection goroutine
cancel func()
nonce string // most recent nonce
}
// connection maintains per-stream connection state for a
// node. Access to the stream and watch state is serialized
// through request and response channels.
type connection struct {
peerAddr string
stream mcp.AggregatedMeshConfigService_StreamAggregatedResourcesServer
id int64
// unique nonce generator for req-resp pairs per xDS stream; the server
// ignores stale nonces. nonce is only modified within send() function.
streamNonce int64
requestC chan *mcp.MeshConfigRequest // a channel for receiving incoming requests
reqError error // holds error if request channel is closed
watches map[string]*watch // per-type watches
watcher source.Watcher
reporter monitoring.Reporter
queue *internal.UniqueQueue
}
// New creates a new gRPC server that implements the Mesh Configuration Protocol (MCP).
func New(options *source.Options, authChecker AuthChecker) *Server {
s := &Server{
watcher: options.Watcher,
collections: options.CollectionOptions,
authCheck: authChecker,
reporter: options.Reporter,
newConnectionLimiter: rate.NewLimiter(rate.Every(newConnectionFreq), newConnectionBurstSize),
}
return s
}
func (s *Server) newConnection(stream mcp.AggregatedMeshConfigService_StreamAggregatedResourcesServer) (*connection, error) {
peerAddr := "0.0.0.0"
var authInfo credentials.AuthInfo
if peerInfo, ok := peer.FromContext(stream.Context()); ok {
peerAddr = peerInfo.Addr.String()
authInfo = peerInfo.AuthInfo
} else {
scope.Warnf("No peer info found on the incoming stream.")
}
if err := s.authCheck.Check(authInfo); err != nil {
return nil, status.Errorf(codes.Unauthenticated, "Authentication failure: %v", err)
}
con := &connection{
stream: stream,
peerAddr: peerAddr,
requestC: make(chan *mcp.MeshConfigRequest),
watches: make(map[string]*watch),
watcher: s.watcher,
id: atomic.AddInt64(&s.nextStreamID, 1),
reporter: s.reporter,
queue: internal.NewUniqueScheduledQueue(len(s.collections)),
}
var collections []string
for i := range s.collections {
collection := s.collections[i]
w := &watch{}
con.watches[collection.Name] = w
collections = append(collections, collection.Name)
}
s.reporter.SetStreamCount(atomic.AddInt64(&s.connections, 1))
scope.Infof("MCP: connection %v: NEW (AggregatedMeshConfigService) supported collections: %#v", con, collections)
return con, nil
}
// IncrementalAggregatedResources implements bidirectional streaming method for incremental MCP.
func (s *Server) IncrementalAggregatedResources(stream mcp.AggregatedMeshConfigService_IncrementalAggregatedResourcesServer) error { // nolint: lll
return status.Errorf(codes.Unimplemented, "not implemented")
}
// StreamAggregatedResources implements bidirectional streaming method for MCP.
func (s *Server) StreamAggregatedResources(stream mcp.AggregatedMeshConfigService_StreamAggregatedResourcesServer) error { // nolint: lll
con, err := s.newConnection(stream)
if err != nil {
return err
}
defer s.closeConnection(con)
go con.receive()
for {
select {
case <-con.queue.Ready():
collection, item, ok := con.queue.Dequeue()
if !ok {
break
}
resp := item.(*source.WatchResponse)
w, ok := con.watches[collection]
if !ok {
scope.Errorf("unknown collection in dequeued watch response: %v", collection)
break // bug?
}
// the response may have been cleared before we got to it
if resp != nil {
if err := con.pushServerResponse(w, resp); err != nil {
return err
}
}
case req, more := <-con.requestC:
if !more {
return con.reqError
}
if err := con.processClientRequest(req); err != nil {
return err
}
case <-con.queue.Done():
return status.Error(codes.Unavailable, "server canceled watch")
case <-stream.Context().Done():
scope.Debugf("MCP: connection %v: stream done, err=%v", con, stream.Context().Err())
return stream.Context().Err()
}
}
}
func (s *Server) closeConnection(con *connection) {
con.close()
s.reporter.SetStreamCount(atomic.AddInt64(&s.connections, -1))
}
// String implements Stringer.String.
func (con *connection) String() string {
return fmt.Sprintf("{addr=%v id=%v}", con.peerAddr, con.id)
}
// Queue the response for sending in the dispatch loop. The caller may provide
// a nil response to indicate that the watch should be closed.
func (con *connection) queueResponse(resp *source.WatchResponse) {
if resp == nil {
con.queue.Close()
} else {
con.queue.Enqueue(resp.Collection, resp)
}
}
func (con *connection) send(resp *source.WatchResponse) (string, error) {
resources := make([]mcp.Resource, 0, len(resp.Resources))
for _, resource := range resp.Resources {
resources = append(resources, *resource)
}
msg := &mcp.MeshConfigResponse{
VersionInfo: resp.Version,
Resources: resources,
TypeUrl: resp.Collection,
}
// increment nonce
con.streamNonce = con.streamNonce + 1
msg.Nonce = strconv.FormatInt(con.streamNonce, 10)
if err := con.stream.Send(msg); err != nil {
con.reporter.RecordSendError(err, status.Code(err))
return "", err
}
scope.Debugf("MCP: connection %v: SEND version=%v nonce=%v", con, resp.Version, msg.Nonce)
return msg.Nonce, nil
}
func (con *connection) receive() {
defer close(con.requestC)
for {
req, err := con.stream.Recv()
if err != nil {
code := status.Code(err)
if code == codes.Canceled || err == io.EOF {
scope.Infof("MCP: connection %v: TERMINATED %q", con, err)
return
}
con.reporter.RecordRecvError(err, code)
scope.Errorf("MCP: connection %v: TERMINATED with errors: %v", con, err)
// Save the stream error prior to closing the stream. The caller
// should access the error after the channel closure.
con.reqError = err
return
}
select {
case con.requestC <- req:
case <-con.queue.Done():
scope.Debugf("MCP: connection %v: stream done", con)
return
case <-con.stream.Context().Done():
scope.Debugf("MCP: connection %v: stream done, err=%v", con, con.stream.Context().Err())
return
}
}
}
func (con *connection) close() {
scope.Infof("MCP: connection %v: CLOSED", con)
for _, w := range con.watches {
if w.cancel != nil {
w.cancel()
}
}
}
func (con *connection) processClientRequest(req *mcp.MeshConfigRequest) error {
collection := req.TypeUrl
con.reporter.RecordRequestSize(collection, con.id, req.Size())
w, ok := con.watches[collection]
if !ok {
return status.Errorf(codes.InvalidArgument, "unsupported collection %q", collection)
}
// nonces can be reused across streams; we verify nonce only if nonce is not initialized
if w.nonce == "" || w.nonce == req.ResponseNonce {
if w.nonce == "" {
scope.Infof("MCP: connection %v: WATCH for %v", con, collection)
} else {
if req.ErrorDetail != nil {
scope.Warnf("MCP: connection %v: NACK collection=%v version=%v with nonce=%q (w.nonce=%q) error=%#v", // nolint: lll
con, collection, req.VersionInfo, req.ResponseNonce, w.nonce, req.ErrorDetail)
con.reporter.RecordRequestNack(collection, con.id, codes.Code(req.ErrorDetail.Code))
} else {
scope.Debugf("MCP: connection %v ACK collection=%q version=%q with nonce=%q",
con, collection, req.VersionInfo, req.ResponseNonce)
con.reporter.RecordRequestAck(collection, con.id)
}
}
if w.cancel != nil {
w.cancel()
}
sr := &source.Request{
SinkNode: req.SinkNode,
Collection: collection,
VersionInfo: req.VersionInfo,
}
w.cancel = con.watcher.Watch(sr, con.queueResponse)
} else {
// This error path should not happen! Skip any requests that don't match the
// latest watch's nonce value. These could be dup requests or out-of-order
// requests from a buggy node.
if req.ErrorDetail != nil {
scope.Errorf("MCP: connection %v: STALE NACK collection=%v version=%v with nonce=%q (w.nonce=%q) error=%#v", // nolint: lll
con, collection, req.VersionInfo, req.ResponseNonce, w.nonce, req.ErrorDetail)
con.reporter.RecordRequestNack(collection, con.id, codes.Code(req.ErrorDetail.Code))
} else {
scope.Errorf("MCP: connection %v: STALE ACK collection=%v version=%v with nonce=%q (w.nonce=%q)", // nolint: lll
con, collection, req.VersionInfo, req.ResponseNonce, w.nonce)
con.reporter.RecordRequestAck(collection, con.id)
}
}
return nil
}
func (con *connection) pushServerResponse(w *watch, resp *source.WatchResponse) error {
nonce, err := con.send(resp)
if err != nil {
return err
}
w.nonce = nonce
return nil
}