forked from istio/istio
/
protoBag.go
444 lines (366 loc) · 11.1 KB
/
protoBag.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
442
443
444
// Copyright 2017 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"
mixerpb "istio.io/api/mixer/v1"
)
type attributeRef struct {
Name string
MapKey string
}
// ReferencedAttributeSnapshot keeps track of the attribute reference state for a mutable bag.
// You can snapshot the referenced attributes with SnapshotReferencedAttributes and later
// reinstall them with RestoreReferencedAttributes. Note that a snapshot can only be used
// once, the RestoreReferencedAttributes call is destructive.
type ReferencedAttributeSnapshot struct {
referencedAttrs map[attributeRef]mixerpb.ReferencedAttributes_Condition
}
// ProtoBag implements the Bag interface on top of an Attributes proto.
type ProtoBag struct {
proto *mixerpb.CompressedAttributes
globalDict map[string]int32
globalWordList []string
messageDict map[string]int32
convertedStringMaps map[int32]StringMap
stringMapMutex sync.RWMutex
// to keep track of attributes that are referenced
referencedAttrs map[attributeRef]mixerpb.ReferencedAttributes_Condition
referencedAttrsMutex sync.Mutex
}
// referencedAttrsSize is the size of referenced attributes.
const referencedAttrsSize = 16
var protoBags = sync.Pool{
New: func() interface{} {
return &ProtoBag{
referencedAttrs: make(map[attributeRef]mixerpb.ReferencedAttributes_Condition, referencedAttrsSize),
}
},
}
// GetProtoBag returns a proto-based attribute bag.
// When you are done using the proto bag, call the Done method to recycle it.
func GetProtoBag(proto *mixerpb.CompressedAttributes, globalDict map[string]int32, globalWordList []string) *ProtoBag {
pb := protoBags.Get().(*ProtoBag)
// build the message-level dictionary
d := make(map[string]int32, len(proto.Words))
for i, name := range proto.Words {
d[name] = slotToIndex(i)
}
pb.proto = proto
pb.globalDict = globalDict
pb.globalWordList = globalWordList
pb.messageDict = d
scope.Debugf("Returning bag with attributes:\n%v", pb)
return pb
}
// Get returns an attribute value.
func (pb *ProtoBag) Get(name string) (interface{}, bool) {
// find the dictionary index for the given string
index, ok := pb.getIndex(name)
if !ok {
scope.Debugf("Attribute '%s' not in either global or message dictionaries", name)
// the string is not in the dictionary, and hence the attribute is not in the proto either
pb.trackReference(name, mixerpb.ABSENCE)
return nil, false
}
result, ok := pb.internalGet(name, index)
if !ok {
// the named attribute was not present
pb.trackReference(name, mixerpb.ABSENCE)
return nil, false
}
// Do not record StringMap access. Keys in it will be recorded separately.
if _, smFound := result.(StringMap); !smFound {
pb.trackReference(name, mixerpb.EXACT)
}
return result, ok
}
// GetReferencedAttributes returns the set of attributes that have been referenced through this bag.
func (pb *ProtoBag) GetReferencedAttributes(globalDict map[string]int32, globalWordCount int) *mixerpb.ReferencedAttributes {
output := &mixerpb.ReferencedAttributes{}
ds := newDictState(globalDict, globalWordCount)
output.AttributeMatches = make([]mixerpb.ReferencedAttributes_AttributeMatch, len(pb.referencedAttrs))
i := 0
for k, v := range pb.referencedAttrs {
mk := int32(0)
if len(k.MapKey) > 0 {
mk = ds.assignDictIndex(k.MapKey)
}
output.AttributeMatches[i] = mixerpb.ReferencedAttributes_AttributeMatch{
Name: ds.assignDictIndex(k.Name),
MapKey: mk,
Condition: v,
}
i++
}
output.Words = ds.getMessageWordList()
return output
}
// ClearReferencedAttributes clears the list of referenced attributes being tracked by this bag
func (pb *ProtoBag) ClearReferencedAttributes() {
for k := range pb.referencedAttrs {
delete(pb.referencedAttrs, k)
}
}
// RestoreReferencedAttributes sets the list of referenced attributes being tracked by this bag
func (pb *ProtoBag) RestoreReferencedAttributes(snap ReferencedAttributeSnapshot) {
ra := make(map[attributeRef]mixerpb.ReferencedAttributes_Condition, len(snap.referencedAttrs))
for k, v := range snap.referencedAttrs {
ra[k] = v
}
pb.referencedAttrs = ra
}
// SnapshotReferencedAttributes grabs a snapshot of the currently referenced attributes
func (pb *ProtoBag) SnapshotReferencedAttributes() ReferencedAttributeSnapshot {
var snap ReferencedAttributeSnapshot
pb.referencedAttrsMutex.Lock()
snap.referencedAttrs = make(map[attributeRef]mixerpb.ReferencedAttributes_Condition, len(pb.referencedAttrs))
for k, v := range pb.referencedAttrs {
snap.referencedAttrs[k] = v
}
pb.referencedAttrsMutex.Unlock()
return snap
}
func (pb *ProtoBag) trackMapReference(name string, key string, condition mixerpb.ReferencedAttributes_Condition) {
pb.referencedAttrsMutex.Lock()
pb.referencedAttrs[attributeRef{Name: name, MapKey: key}] = condition
pb.referencedAttrsMutex.Unlock()
}
func (pb *ProtoBag) trackReference(name string, condition mixerpb.ReferencedAttributes_Condition) {
pb.referencedAttrsMutex.Lock()
pb.referencedAttrs[attributeRef{Name: name}] = condition
pb.referencedAttrsMutex.Unlock()
}
func (pb *ProtoBag) internalGet(name string, index int32) (interface{}, bool) {
strIndex, ok := pb.proto.Strings[index]
if ok {
// found the attribute, now convert its value from a dictionary index to a string
str, err := pb.lookup(strIndex)
if err != nil {
scope.Errorf("string attribute %s: %v", name, err)
return nil, false
}
return str, true
}
var value interface{}
// see if the requested attribute is a string map that's already been converted
pb.stringMapMutex.RLock()
value, ok = pb.convertedStringMaps[index]
pb.stringMapMutex.RUnlock()
if ok {
return value, true
}
// now see if its an unconverted string map
sm, ok := pb.proto.StringMaps[index]
if ok {
// convert from map[int32]int32 to map[string]string
m, err := pb.convertStringMap(sm.Entries)
if err != nil {
scope.Errorf("string map %s: %v", name, err)
return nil, false
}
// cache the converted string map for later calls
ssm := StringMap{name: name, entries: m, pb: pb}
pb.stringMapMutex.Lock()
if pb.convertedStringMaps == nil {
pb.convertedStringMaps = make(map[int32]StringMap)
}
pb.convertedStringMaps[index] = ssm
pb.stringMapMutex.Unlock()
return ssm, true
}
value, ok = pb.proto.Int64S[index]
if ok {
return value, true
}
value, ok = pb.proto.Doubles[index]
if ok {
return value, true
}
value, ok = pb.proto.Bools[index]
if ok {
return value, true
}
value, ok = pb.proto.Timestamps[index]
if ok {
return value, true
}
value, ok = pb.proto.Durations[index]
if ok {
return value, true
}
value, ok = pb.proto.Bytes[index]
if ok {
return value, true
}
// not found
return nil, false
}
// given a string, find the corresponding dictionary index if it exists
func (pb *ProtoBag) getIndex(str string) (int32, bool) {
if index, ok := pb.messageDict[str]; ok {
return index, true
}
if index, ok := pb.globalDict[str]; ok {
return index, true
}
return 0, false
}
// given a dictionary index, find the corresponding string if it exists
func (pb *ProtoBag) lookup(index int32) (string, error) {
if index < 0 {
slot := indexToSlot(index)
if slot < len(pb.proto.Words) {
return pb.proto.Words[slot], nil
}
} else if index < int32(len(pb.globalWordList)) {
return pb.globalWordList[index], nil
}
return "", fmt.Errorf("string index %d is not defined in the available dictionaries", index)
}
// convert a map[int32]int32 into a map[string]string, where the int32 are dictionary indices
func (pb *ProtoBag) convertStringMap(s map[int32]int32) (map[string]string, error) {
d := make(map[string]string, len(s))
for k, v := range s {
key, err := pb.lookup(k)
if err != nil {
return nil, err
}
value, err := pb.lookup(v)
if err != nil {
return nil, err
}
d[key] = value
}
return d, nil
}
// Contains returns true if protobag contains this key.
func (pb *ProtoBag) Contains(key string) bool {
idx, found := pb.getIndex(key)
if !found {
return false
}
if _, ok := pb.proto.Strings[idx]; ok {
return true
}
if _, ok := pb.proto.StringMaps[idx]; ok {
return true
}
if _, ok := pb.proto.Int64S[idx]; ok {
return true
}
if _, ok := pb.proto.Doubles[idx]; ok {
return true
}
if _, ok := pb.proto.Bools[idx]; ok {
return true
}
if _, ok := pb.proto.Timestamps[idx]; ok {
return true
}
if _, ok := pb.proto.Durations[idx]; ok {
return true
}
if _, ok := pb.proto.Bytes[idx]; ok {
return true
}
return false
}
// Names returns the names of all the attributes known to this bag.
func (pb *ProtoBag) Names() []string {
names := make(map[string]bool)
for k := range pb.proto.Strings {
if name, err := pb.lookup(k); err == nil {
names[name] = true
}
}
for k := range pb.proto.Int64S {
if name, err := pb.lookup(k); err == nil {
names[name] = true
}
}
for k := range pb.proto.Doubles {
if name, err := pb.lookup(k); err == nil {
names[name] = true
}
}
for k := range pb.proto.Bools {
if name, err := pb.lookup(k); err == nil {
names[name] = true
}
}
for k := range pb.proto.Timestamps {
if name, err := pb.lookup(k); err == nil {
names[name] = true
}
}
for k := range pb.proto.Durations {
if name, err := pb.lookup(k); err == nil {
names[name] = true
}
}
for k := range pb.proto.Bytes {
if name, err := pb.lookup(k); err == nil {
names[name] = true
}
}
for k := range pb.proto.StringMaps {
if name, err := pb.lookup(k); err == nil {
names[name] = true
}
}
n := make([]string, len(names))
i := 0
for name := range names {
n[i] = name
i++
}
return n
}
// Done indicates the bag can be reclaimed.
func (pb *ProtoBag) Done() {
pb.Reset()
protoBags.Put(pb)
}
// Reset removes all local state.
func (pb *ProtoBag) Reset() {
pb.proto = nil
pb.globalDict = make(map[string]int32)
pb.globalWordList = nil
pb.messageDict = make(map[string]int32)
pb.stringMapMutex.Lock()
pb.convertedStringMaps = make(map[int32]StringMap)
pb.stringMapMutex.Unlock()
pb.referencedAttrsMutex.Lock()
pb.referencedAttrs = make(map[attributeRef]mixerpb.ReferencedAttributes_Condition, referencedAttrsSize)
pb.referencedAttrsMutex.Unlock()
}
// String runs through the named attributes, looks up their values,
// and prints them to a string.
func (pb *ProtoBag) String() string {
buf := &bytes.Buffer{}
names := pb.Names()
sort.Strings(names)
for _, name := range names {
// find the dictionary index for the given string
index, _ := pb.getIndex(name)
if result, ok := pb.internalGet(name, index); ok {
fmt.Fprintf(buf, "%-30s: %v\n", name, result)
}
}
return buf.String()
}