This repository has been archived by the owner on Mar 14, 2022. It is now read-only.
forked from istio/istio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutableBag.go
441 lines (371 loc) · 11.2 KB
/
mutableBag.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
// Copyright 2016 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 attribute
import (
"bytes"
"fmt"
"sort"
"sync"
"time"
me "github.com/hashicorp/go-multierror"
mixerpb "istio.io/api/mixer/v1"
"istio.io/istio/mixer/pkg/pool"
"istio.io/istio/pkg/log"
)
// MutableBag is a generic mechanism to read and write a set of attributes.
//
// Bags can be chained together in a parent/child relationship. A child bag
// represents a delta over a parent. By default a child looks identical to
// the parent. But as mutations occur to the child, the two start to diverge.
// Resetting a child makes it look identical to its parent again.
type MutableBag struct {
parent Bag
values map[string]interface{}
}
var mutableBags = sync.Pool{
New: func() interface{} {
return &MutableBag{
values: make(map[string]interface{}),
}
},
}
var scope = log.RegisterScope("attributes", "Attribute-related messsages.", 0)
// GetMutableBag returns an initialized bag.
//
// Bags can be chained in a parent/child relationship. You can pass nil if the
// bag has no parent.
//
// When you are done using the mutable bag, call the Done method to recycle it.
func GetMutableBag(parent Bag) *MutableBag {
mb := mutableBags.Get().(*MutableBag)
if parent == nil {
mb.parent = empty
} else {
mb.parent = parent
}
return mb
}
// GetFakeMutableBagForTesting returns a Mutable bag based on the specified map
// Use this function only for testing purposes.
func GetFakeMutableBagForTesting(v map[string]interface{}) *MutableBag {
m := GetMutableBag(nil)
m.values = v
return m
}
// CopyBag makes a deep copy of a bag.
func CopyBag(b Bag) *MutableBag {
mb := GetMutableBag(nil)
for _, k := range b.Names() {
v, _ := b.Get(k)
mb.Set(k, copyValue(v))
}
return mb
}
// Given an attribute value, create a deep copy of it
func copyValue(v interface{}) interface{} {
switch t := v.(type) {
case []byte:
c := make([]byte, len(t))
copy(c, t)
return c
case map[string]string:
c := make(map[string]string, len(t))
for k2, v2 := range t {
c[k2] = v2
}
return c
}
return v
}
// Done indicates the bag can be reclaimed.
func (mb *MutableBag) Done() {
// prevent use of a bag that's in the pool
if mb.parent == nil {
panic(fmt.Errorf("attempt to use a bag after its Done method has been called"))
}
mb.parent = nil
mb.Reset()
mutableBags.Put(mb)
}
// Get returns an attribute value.
func (mb *MutableBag) Get(name string) (interface{}, bool) {
// prevent use of a bag that's in the pool
if mb.parent == nil {
panic(fmt.Errorf("attempt to use a bag after its Done method has been called"))
}
var r interface{}
var b bool
if r, b = mb.values[name]; !b {
r, b = mb.parent.Get(name)
}
return r, b
}
// Names returns the names of all the attributes known to this bag.
func (mb *MutableBag) Names() []string {
if mb == nil {
return []string{}
}
if mb.parent == nil {
panic(fmt.Errorf("attempt to use a bag after its Done method has been called"))
}
parentNames := mb.parent.Names()
m := make(map[string]bool, len(parentNames)+len(mb.values))
for _, name := range parentNames {
m[name] = true
}
for name := range mb.values {
m[name] = true
}
i := 0
names := make([]string, len(m))
for name := range m {
names[i] = name
i++
}
return names
}
// Set creates an override for a named attribute.
func (mb *MutableBag) Set(name string, value interface{}) {
mb.values[name] = value
}
// Delete removes a named item from the local state.
// The item may still be present higher in the hierarchy
func (mb *MutableBag) Delete(name string) {
delete(mb.values, name)
}
// Reset removes all local state.
func (mb *MutableBag) Reset() {
mb.values = make(map[string]interface{})
}
// Merge combines an array of bags into the current bag. If the current bag already defines
// a particular attribute, it keeps its value and is not overwritten.
//
// Note that this does a 'shallow' merge. Only the value defined explicitly in the
// mutable bags themselves, and not in any of their parents, are considered.
func (mb *MutableBag) Merge(bag *MutableBag) {
// get the known symbols for the target bag
names := make(map[string]bool)
for _, name := range mb.Names() {
names[name] = true
}
for k, v := range bag.values {
// the input bags cannot override values already in the destination bag
_, found := names[k]
if !found {
mb.values[k] = copyValue(v)
}
}
}
// ToProto fills-in an Attributes proto based on the content of the bag.
func (mb *MutableBag) ToProto(output *mixerpb.CompressedAttributes, globalDict map[string]int32, globalWordCount int) {
ds := newDictState(globalDict, globalWordCount)
keys := mb.Names()
for _, k := range keys {
index := ds.assignDictIndex(k)
v, _ := mb.Get(k) // if not found, nil return will be ignored by the switch below
switch t := v.(type) {
case string:
if output.Strings == nil {
output.Strings = make(map[int32]int32)
}
output.Strings[index] = ds.assignDictIndex(t)
case int64:
if output.Int64S == nil {
output.Int64S = make(map[int32]int64)
}
output.Int64S[index] = t
case float64:
if output.Doubles == nil {
output.Doubles = make(map[int32]float64)
}
output.Doubles[index] = t
case bool:
if output.Bools == nil {
output.Bools = make(map[int32]bool)
}
output.Bools[index] = t
case time.Time:
if output.Timestamps == nil {
output.Timestamps = make(map[int32]time.Time)
}
output.Timestamps[index] = t
case time.Duration:
if output.Durations == nil {
output.Durations = make(map[int32]time.Duration)
}
output.Durations[index] = t
case []byte:
if output.Bytes == nil {
output.Bytes = make(map[int32][]byte)
}
output.Bytes[index] = t
case map[string]string:
sm := make(map[int32]int32, len(t))
for smk, smv := range t {
sm[ds.assignDictIndex(smk)] = ds.assignDictIndex(smv)
}
if output.StringMaps == nil {
output.StringMaps = make(map[int32]mixerpb.StringMap)
}
output.StringMaps[index] = mixerpb.StringMap{Entries: sm}
}
}
output.Words = ds.getMessageWordList()
}
// GetBagFromProto returns an initialized bag from an Attribute proto.
func GetBagFromProto(attrs *mixerpb.CompressedAttributes, globalWordList []string) (*MutableBag, error) {
mb := GetMutableBag(nil)
err := mb.UpdateBagFromProto(attrs, globalWordList)
if err != nil {
mb.Done()
return nil, err
}
return mb, nil
}
// add an attribute to the bag, guarding against duplicate names
func (mb *MutableBag) insertProtoAttr(name string, value interface{},
seen map[string]struct{}, lg func(string, ...interface{})) error {
lg(" %s -> '%v'", name, value)
if _, duplicate := seen[name]; duplicate {
previous := mb.values[name]
return fmt.Errorf("duplicate attribute %s (type %T, was %T)", name, value, previous)
}
seen[name] = struct{}{}
mb.values[name] = value
return nil
}
// UpdateBagFromProto refreshes the bag based on the content of the attribute proto.
//
// Note that in the case of semantic errors in the supplied proto which leads to
// an error return, it's likely that the bag will have been partially updated.
func (mb *MutableBag) UpdateBagFromProto(attrs *mixerpb.CompressedAttributes, globalWordList []string) error {
messageWordList := attrs.Words
var e error
var name string
var value string
// fail if the proto carries multiple attributes by the same name (but different types)
seen := make(map[string]struct{})
var buf *bytes.Buffer
lg := func(format string, args ...interface{}) {}
if scope.DebugEnabled() {
buf = pool.GetBuffer()
lg = func(format string, args ...interface{}) {
fmt.Fprintf(buf, format, args...)
buf.WriteString("\n")
}
defer func() {
if buf != nil {
scope.Debug(buf.String())
pool.PutBuffer(buf)
}
}()
}
lg("Updating bag from wire attributes:")
lg(" setting string attributes:")
for k, v := range attrs.Strings {
name, e = lookup(k, e, globalWordList, messageWordList)
value, e = lookup(v, e, globalWordList, messageWordList)
if err := mb.insertProtoAttr(name, value, seen, lg); err != nil {
return err
}
}
lg(" setting int64 attributes:")
for k, v := range attrs.Int64S {
name, e = lookup(k, e, globalWordList, messageWordList)
if err := mb.insertProtoAttr(name, v, seen, lg); err != nil {
return err
}
}
lg(" setting double attributes:")
for k, v := range attrs.Doubles {
name, e = lookup(k, e, globalWordList, messageWordList)
if err := mb.insertProtoAttr(name, v, seen, lg); err != nil {
return err
}
}
lg(" setting bool attributes:")
for k, v := range attrs.Bools {
name, e = lookup(k, e, globalWordList, messageWordList)
if err := mb.insertProtoAttr(name, v, seen, lg); err != nil {
return err
}
}
lg(" setting timestamp attributes:")
for k, v := range attrs.Timestamps {
name, e = lookup(k, e, globalWordList, messageWordList)
if err := mb.insertProtoAttr(name, v, seen, lg); err != nil {
return err
}
}
lg(" setting duration attributes:")
for k, v := range attrs.Durations {
name, e = lookup(k, e, globalWordList, messageWordList)
if err := mb.insertProtoAttr(name, v, seen, lg); err != nil {
return err
}
}
lg(" setting bytes attributes:")
for k, v := range attrs.Bytes {
name, e = lookup(k, e, globalWordList, messageWordList)
if err := mb.insertProtoAttr(name, v, seen, lg); err != nil {
return err
}
}
lg(" setting string map attributes:")
for k, v := range attrs.StringMaps {
name, e = lookup(k, e, globalWordList, messageWordList)
sm := make(map[string]string, len(v.Entries))
for k2, v2 := range v.Entries {
var name2 string
var value2 string
name2, e = lookup(k2, e, globalWordList, messageWordList)
value2, e = lookup(v2, e, globalWordList, messageWordList)
sm[name2] = value2
}
if err := mb.insertProtoAttr(name, sm, seen, lg); err != nil {
return err
}
}
return e
}
func lookup(index int32, err error, globalWordList []string, messageWordList []string) (string, error) {
if index < 0 {
slot := indexToSlot(index)
if slot < len(messageWordList) {
return messageWordList[slot], err
}
} else if index < int32(len(globalWordList)) {
return globalWordList[index], err
}
return "", me.Append(err, fmt.Errorf("attribute index %d is not defined in the available dictionaries", index))
}
// String prints out the attributes from the parent bag, then
// walks through the local changes and prints them as well.
func (mb *MutableBag) String() string {
if len(mb.values) == 0 {
return mb.parent.String()
}
buf := &bytes.Buffer{}
buf.WriteString(mb.parent.String())
buf.WriteString("---\n")
keys := make([]string, 0, len(mb.values))
for key := range mb.values {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
fmt.Fprintf(buf, "%-30s: %v\n", key, mb.values[key])
}
return buf.String()
}