forked from istio/istio
-
Notifications
You must be signed in to change notification settings - Fork 1
/
session.go
392 lines (333 loc) · 11.2 KB
/
session.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
// 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 dispatcher
import (
"bytes"
"context"
"strings"
"github.com/gogo/googleapis/google/rpc"
multierror "github.com/hashicorp/go-multierror"
"go.opencensus.io/stats"
tpb "istio.io/api/mixer/adapter/model/v1beta1"
mixerpb "istio.io/api/mixer/v1"
descriptor "istio.io/api/policy/v1beta1"
"istio.io/istio/mixer/pkg/adapter"
"istio.io/istio/mixer/pkg/attribute"
"istio.io/istio/mixer/pkg/pool"
"istio.io/istio/mixer/pkg/runtime/monitoring"
"istio.io/istio/mixer/pkg/runtime/routing"
"istio.io/istio/mixer/pkg/status"
"istio.io/istio/pkg/log"
)
const queueAllocSize = 64
// session represents a call session to the Impl. It contains all the mutable state needed for handling the
// call. It is used as temporary memory location to keep ephemeral state, thus avoiding garbage creation.
type session struct {
// owner
impl *Impl
// routing context for the life of this session
rc *RoutingContext
// input parameters that was collected as part of the call.
ctx context.Context
bag attribute.Bag
quotaArgs QuotaMethodArgs
responseBag *attribute.MutableBag
reportStates map[*routing.Destination]*dispatchState
// output parameters that get collected / accumulated as results.
checkResult adapter.CheckResult
quotaResult adapter.QuotaResult
err error
// The current number of activeDispatches handler dispatches.
activeDispatches int
// channel for collecting states of completed dispatches.
completed chan *dispatchState
// The variety of the operation that is being performed.
variety tpb.TemplateVariety
}
func (s *session) clear() {
s.impl = nil
s.rc = nil
s.variety = 0
s.ctx = nil
s.bag = nil
s.quotaArgs = QuotaMethodArgs{}
s.responseBag = nil
s.reportStates = nil
s.activeDispatches = 0
s.err = nil
s.quotaResult = adapter.QuotaResult{}
s.checkResult = adapter.CheckResult{}
// Drain the channel
exit := false
for !exit {
select {
case <-s.completed:
log.Warn("Leaked dispatch state discovered!")
continue
default:
exit = true
}
}
}
func (s *session) ensureParallelism(minParallelism int) {
// Resize the channel to accommodate the parallelism, if necessary.
if cap(s.completed) < minParallelism {
allocSize := ((minParallelism / queueAllocSize) + 1) * queueAllocSize
s.completed = make(chan *dispatchState, allocSize)
}
}
func (s *session) dispatch() error { //nolint: unparam
// Determine namespace to scope config resolution
namespace := getIdentityNamespace(s.bag)
destinations := s.rc.Routes.GetDestinations(s.variety, namespace)
// Ensure that we can run dispatches to all destinations in parallel.
s.ensureParallelism(destinations.Count())
foundQuota := false
ninputs := 0
ndestinations := 0
for _, destination := range destinations.Entries() {
var state *dispatchState
if s.variety == tpb.TEMPLATE_VARIETY_REPORT {
// We buffer states for report calls and dispatch them later
state = s.reportStates[destination]
if state == nil {
state = s.impl.getDispatchState(s.ctx, destination)
s.reportStates[destination] = state
}
}
for _, group := range destination.InstanceGroups {
groupMatched := group.Matches(s.bag)
if groupMatched {
ndestinations++
}
for j, input := range group.Builders {
if s.variety == tpb.TEMPLATE_VARIETY_QUOTA {
// only dispatch instances with a matching name
if !strings.EqualFold(input.InstanceShortName, s.quotaArgs.Quota) {
continue
}
if !groupMatched {
// This is a conditional quota and it does not apply to the requester
// return what was requested
s.quotaResult.Amount = s.quotaArgs.Amount
s.quotaResult.ValidDuration = defaultValidDuration
}
foundQuota = true
}
if !groupMatched {
continue
}
var instance interface{}
var err error
if instance, err = input.Builder(s.bag); err != nil {
log.Errorf("error creating instance: destination='%v', error='%v'", destination.FriendlyName, err)
s.err = multierror.Append(s.err, err)
continue
}
ninputs++
// For report templates, accumulate instances as much as possible before commencing dispatch.
if s.variety == tpb.TEMPLATE_VARIETY_REPORT {
state.instances = append(state.instances, instance)
continue
}
// for other templates, dispatch for each instance individually.
state = s.impl.getDispatchState(s.ctx, destination)
state.instances = append(state.instances, instance)
if s.variety == tpb.TEMPLATE_VARIETY_ATTRIBUTE_GENERATOR {
state.mapper = group.Mappers[j]
state.inputBag = s.bag
}
// Dispatch for singleton dispatches
state.quotaArgs.BestEffort = s.quotaArgs.BestEffort
state.quotaArgs.DeduplicationID = s.quotaArgs.DeduplicationID
state.quotaArgs.QuotaAmount = s.quotaArgs.Amount
state.outputPrefix = input.ActionName + ".output."
// TODO(kuat) make output bag concurrency safe
s.dispatchToHandler(state)
}
}
}
stats.Record(s.ctx,
monitoring.DestinationsPerRequest.M(int64(ndestinations)),
monitoring.InstancesPerRequest.M(int64(ninputs)))
s.waitForDispatched()
if s.variety == tpb.TEMPLATE_VARIETY_QUOTA && !foundQuota {
// If quota is not found it is very likely that quotaSpec / quotaSpecBinding was applied first
// We still err on the side of allowing access, but warn about the fact that quota was not found.
s.quotaResult.Amount = s.quotaArgs.Amount
s.quotaResult.ValidDuration = defaultValidDuration
log.Warnf("Requested quota '%s' is not configured", s.quotaArgs.Quota)
}
// aggregate header operations after filtering by attribute conditions
if s.variety == tpb.TEMPLATE_VARIETY_CHECK && status.IsOK(s.checkResult.Status) {
for _, directiveGroup := range destinations.Directives() {
if directiveGroup.Condition != nil {
if matches, err := directiveGroup.Condition.EvaluateBoolean(s.bag); err != nil || !matches {
continue
}
}
for _, op := range directiveGroup.Operations {
hop := mixerpb.HeaderOperation{
Name: op.HeaderName,
}
switch op.Operation {
case descriptor.APPEND:
hop.Operation = mixerpb.APPEND
case descriptor.REMOVE:
hop.Operation = mixerpb.REMOVE
case descriptor.REPLACE:
hop.Operation = mixerpb.REPLACE
}
if op.Operation != descriptor.REMOVE {
var verr error
hop.Value, verr = op.HeaderValue.EvaluateString(s.responseBag)
if verr != nil {
log.Warnf("Failed to evaluate header value: %v", verr)
continue
}
if hop.Value == "" {
continue
}
}
// default response if RouteDirective is only action
if s.checkResult.IsDefault() {
s.checkResult = adapter.CheckResult{
ValidUseCount: defaultValidUseCount,
ValidDuration: defaultValidDuration,
}
}
if s.checkResult.RouteDirective == nil {
s.checkResult.RouteDirective = &mixerpb.RouteDirective{}
}
switch op.Type {
case routing.RequestHeaderOperation:
s.checkResult.RouteDirective.RequestHeaderOperations = append(s.checkResult.RouteDirective.RequestHeaderOperations, hop)
case routing.ResponseHeaderOperation:
s.checkResult.RouteDirective.ResponseHeaderOperations = append(s.checkResult.RouteDirective.ResponseHeaderOperations, hop)
}
}
}
}
return nil
}
func (s *session) dispatchBufferedReports() {
// Ensure that we can run dispatches to all destinations in parallel.
s.ensureParallelism(len(s.reportStates))
// dispatch the buffered dispatchStates we've got
for k, v := range s.reportStates {
if len(v.instances) == 0 {
// do not dispatch to handler if nothing is buffered
continue
}
s.dispatchToHandler(v)
delete(s.reportStates, k)
}
s.waitForDispatched()
}
func (s *session) dispatchToHandler(ds *dispatchState) {
s.activeDispatches++
ds.session = s
s.impl.gp.ScheduleWork(ds.invokeHandler, nil)
}
func (s *session) waitForDispatched() {
// wait on the dispatch states and accumulate results
var buf *bytes.Buffer
code := rpc.OK
for s.activeDispatches > 0 {
state := <-s.completed
s.activeDispatches--
// Aggregate errors
if state.err != nil {
s.err = multierror.Append(s.err, state.err)
}
st := rpc.Status{Code: int32(rpc.OK)}
switch s.variety {
case tpb.TEMPLATE_VARIETY_REPORT:
// Do nothing
case tpb.TEMPLATE_VARIETY_CHECK:
if s.checkResult.IsDefault() {
// no results so far
s.checkResult = state.checkResult
} else {
// combine with a previously obtained result
if s.checkResult.ValidDuration > state.checkResult.ValidDuration {
s.checkResult.ValidDuration = state.checkResult.ValidDuration
}
if s.checkResult.ValidUseCount > state.checkResult.ValidUseCount {
s.checkResult.ValidUseCount = state.checkResult.ValidUseCount
}
}
st = state.checkResult.Status
if state.outputBag != nil {
s.responseBag.Merge(state.outputBag)
state.outputBag.Done()
}
case tpb.TEMPLATE_VARIETY_QUOTA:
if s.quotaResult.IsDefault() {
s.quotaResult = state.quotaResult
} else {
log.Warnf("Skipping quota op result due to previous value: '%v', op: '%s'",
state.quotaResult, state.destination.FriendlyName)
}
st = state.quotaResult.Status
case tpb.TEMPLATE_VARIETY_ATTRIBUTE_GENERATOR:
if state.outputBag != nil {
s.responseBag.Merge(state.outputBag)
}
}
if !status.IsOK(st) {
if buf == nil {
buf = pool.GetBuffer()
// the first failure result's code becomes the result code for the output
// `buf` variable guards the first failure since it is set the first time
code = rpc.Code(st.Code)
// update the direct response matching the error status
if s.variety == tpb.TEMPLATE_VARIETY_CHECK {
if response := status.GetDirectHTTPResponse(st); response != nil {
if s.checkResult.RouteDirective == nil {
s.checkResult.RouteDirective = &mixerpb.RouteDirective{}
}
directive := s.checkResult.RouteDirective
if response.Code != 0 {
directive.DirectResponseCode = uint32(response.Code)
} else {
directive.DirectResponseCode = uint32(status.HTTPStatusFromCode(rpc.Code(st.Code)))
}
directive.DirectResponseBody = response.Body
for header, value := range response.Headers {
directive.ResponseHeaderOperations = append(directive.ResponseHeaderOperations,
mixerpb.HeaderOperation{
Name: header,
Value: value,
})
}
}
}
} else {
buf.WriteString(", ")
}
buf.WriteString(state.destination.HandlerName + ":" + st.Message)
}
s.impl.putDispatchState(state)
}
if buf != nil {
switch s.variety {
case tpb.TEMPLATE_VARIETY_CHECK:
s.checkResult.Status = status.WithMessage(code, buf.String())
case tpb.TEMPLATE_VARIETY_QUOTA:
s.quotaResult.Status = status.WithMessage(code, buf.String())
}
pool.PutBuffer(buf)
}
}