-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
304 lines (255 loc) · 8.54 KB
/
client.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
// 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 client
import (
"context"
"io"
"sort"
"sync"
"time"
"github.com/gogo/protobuf/types"
"github.com/gogo/status"
"google.golang.org/grpc/codes"
mcp "istio.io/api/mcp/v1alpha1"
"istio.io/istio/pkg/log"
"istio.io/istio/pkg/mcp/monitoring"
"istio.io/istio/pkg/mcp/sink"
)
var (
// try to re-establish the bi-directional grpc stream after this delay.
reestablishStreamDelay = time.Second
scope = log.RegisterScope("mcp", "mcp debugging", 0)
)
type perTypeState struct {
sync.Mutex
lastVersion string
}
func (s *perTypeState) setVersion(v string) {
s.Lock()
defer s.Unlock()
s.lastVersion = v
}
func (s *perTypeState) version() string {
s.Lock()
defer s.Unlock()
return s.lastVersion
}
// Client is a client implementation of the Mesh Configuration Protocol (MCP). It is responsible
// for the following:
//
// - Maintaining the bidirectional grpc stream with the server. The stream will be reestablished
// on transient network failures. The provided grpc connection (mcpClient) is assumed to handle
// (re)dialing the server.
//
// - Handling all aspects of the MCP exchange for the supported message types, e.g. request/response,
// ACK/NACK, nonce, versioning,
//
// - Decoding the received configuration updates and providing them to the user via a batched set of changes.
type Client struct {
client mcp.AggregatedMeshConfigServiceClient
stream mcp.AggregatedMeshConfigService_StreamAggregatedResourcesClient
state map[string]*perTypeState
nodeInfo *mcp.SinkNode
updater sink.Updater
journal *sink.RecentRequestsJournal
metadata map[string]string
reporter monitoring.Reporter
}
// New creates a new instance of the MCP client for the specified message types.
func New(mcpClient mcp.AggregatedMeshConfigServiceClient, options *sink.Options) *Client { // nolint: lll
nodeInfo := &mcp.SinkNode{
Id: options.ID,
Annotations: map[string]string{},
}
for k, v := range options.Metadata {
nodeInfo.Annotations[k] = v
}
state := make(map[string]*perTypeState)
for _, collection := range options.CollectionOptions {
state[collection.Name] = &perTypeState{}
}
return &Client{
client: mcpClient,
state: state,
nodeInfo: nodeInfo,
updater: options.Updater,
metadata: options.Metadata,
reporter: options.Reporter,
journal: sink.NewRequestJournal(),
}
}
// Probe point for test code to determine when the client is finished processing responses.
var handleResponseDoneProbe = func() {}
func (c *Client) sendNACKRequest(response *mcp.MeshConfigResponse, version string, err error) *mcp.MeshConfigRequest {
errorDetails, _ := status.FromError(err)
scope.Errorf("MCP: sending NACK for version=%v nonce=%v: error=%q", version, response.Nonce, err)
c.reporter.RecordRequestNack(response.TypeUrl, 0, errorDetails.Code())
req := &mcp.MeshConfigRequest{
SinkNode: c.nodeInfo,
TypeUrl: response.TypeUrl,
VersionInfo: version,
ResponseNonce: response.Nonce,
ErrorDetail: errorDetails.Proto(),
}
return req
}
func (c *Client) handleResponse(response *mcp.MeshConfigResponse) *mcp.MeshConfigRequest {
if handleResponseDoneProbe != nil {
defer handleResponseDoneProbe()
}
collection := response.TypeUrl
state, ok := c.state[collection]
if !ok {
errDetails := status.Errorf(codes.Unimplemented, "unsupported collection: %v", collection)
return c.sendNACKRequest(response, "", errDetails)
}
change := &sink.Change{
Collection: collection,
Objects: make([]*sink.Object, 0, len(response.Resources)),
SystemVersionInfo: response.VersionInfo,
}
for _, resource := range response.Resources {
var dynamicAny types.DynamicAny
if err := types.UnmarshalAny(resource.Body, &dynamicAny); err != nil {
return c.sendNACKRequest(response, state.version(), err)
}
object := &sink.Object{
TypeURL: resource.Body.TypeUrl,
Metadata: resource.Metadata,
Body: dynamicAny.Message,
}
change.Objects = append(change.Objects, object)
}
if err := c.updater.Apply(change); err != nil {
errDetails := status.Error(codes.InvalidArgument, err.Error())
return c.sendNACKRequest(response, state.version(), errDetails)
}
c.reporter.RecordRequestAck(collection, 0)
req := &mcp.MeshConfigRequest{
SinkNode: c.nodeInfo,
TypeUrl: collection,
VersionInfo: response.VersionInfo,
ResponseNonce: response.Nonce,
}
return req
}
// Run starts the run loop for request and receiving configuration updates from
// the server. This function blocks and should typically be run in a goroutine.
// The client will continue to attempt to re-establish the stream with the server
// indefinitely. The function exits when the provided context is canceled.
func (c *Client) Run(ctx context.Context) {
// See https://godoc.org/google.golang.org/grpc#ClientConn.NewStream
// for rules to ensure stream resources are not leaked.
initRequests := make([]*mcp.MeshConfigRequest, 0, len(c.state))
for collection := range c.state {
initRequests = append(initRequests, &mcp.MeshConfigRequest{
SinkNode: c.nodeInfo,
TypeUrl: collection,
})
}
// The first attempt is immediate.
retryDelay := time.Nanosecond
for {
for {
select {
case <-ctx.Done():
return
case <-time.After(retryDelay):
}
// slow subsequent reconnection attempts down
retryDelay = reestablishStreamDelay
scope.Info("(re)trying to establish new MCP stream")
var err error
if c.stream, err = c.client.StreamAggregatedResources(ctx); err == nil {
c.reporter.RecordStreamCreateSuccess()
scope.Info("New MCP stream created")
break
}
scope.Errorf("Failed to create a new MCP stream: %v", err)
}
var nextInitRequest int
// The client begins each new stream by sending an empty
// request for each supported type. The server sends a
// response when resources are available. After processing a
// response, the client sends a new request specifying the
// last version applied and nonce provided by the server.
for {
var req *mcp.MeshConfigRequest
var version string
if nextInitRequest < len(initRequests) {
// Send the entire batch of initial requests before
// trying to receive responses.
req = initRequests[nextInitRequest]
nextInitRequest++
} else {
response, err := c.stream.Recv()
if err != nil {
if err != io.EOF {
c.reporter.RecordRecvError(err, status.Code(err))
scope.Errorf("Error receiving MCP response: %v", err)
}
break
}
version = response.VersionInfo
req = c.handleResponse(response)
}
c.journal.RecordMeshConfigRequest(req)
if err := c.stream.Send(req); err != nil {
c.reporter.RecordSendError(err, status.Code(err))
scope.Errorf("Error sending MCP request: %v", err)
// (from https://godoc.org/google.golang.org/grpc#ClientConn.NewStream)
//
// SendMsg is generally called by generated code. On error, SendMsg aborts
// the stream. If the error was generated by the client, the status is
// returned directly; otherwise, io.EOF is returned and the status of
// the stream may be discovered using RecvMsg.
if err != io.EOF {
break
}
} else {
collection := req.TypeUrl
if req.ErrorDetail == nil && collection != "" {
if state, ok := c.state[collection]; ok {
state.setVersion(version)
}
}
}
}
}
}
// SnapshotRequestInfo returns a snapshot of the last known set of request results.
func (c *Client) SnapshotRequestInfo() []sink.RecentRequestInfo {
return c.journal.Snapshot()
}
// Metadata that is originally supplied when creating this client.
func (c *Client) Metadata() map[string]string {
r := make(map[string]string, len(c.metadata))
for k, v := range c.metadata {
r[k] = v
}
return r
}
// ID is the node id for this client.
func (c *Client) ID() string {
return c.nodeInfo.Id
}
// Collections returns the collections that this client requests.
func (c *Client) Collections() []string {
result := make([]string, 0, len(c.state))
for k := range c.state {
result = append(result, k)
}
sort.Strings(result)
return result
}