-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathlabel_name_value_index.go
353 lines (305 loc) · 10.7 KB
/
label_name_value_index.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
// Copyright (c) 2023 Tigera, Inc. All rights reserved.
//
// 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 labelnamevalueindex
import (
"fmt"
"github.com/sirupsen/logrus"
"github.com/projectcalico/calico/libcalico-go/lib/selector/parser"
"github.com/projectcalico/calico/libcalico-go/lib/set"
)
// LabelNameValueIndex stores a set of Labeled objects by ID, and it indexes
// them according to their own (not inherited) labels/label values for
// efficient scans based on selector LabelRestrictions. The StrategyFor
// method returns the most efficient strategy for a particular restriction.
// Returning strategies allows the caller to compare different available
// strategies against each other without executing them.
//
// Note: LabelNameValueIndex is not inheritance-aware, it indexes items
// solely on their own labels without taking parents into account. This avoids
// needing to reindex every item when a parent changes, allowing that to be
// handled more efficiently at the layer above.
type LabelNameValueIndex[ItemID comparable, Item Labeled] struct {
nameOfTrackedItems string
allItems map[ItemID]Item
labelNameToValueToIDs map[string]values[ItemID]
}
type Labeled interface {
OwnLabels() map[string]string
}
func New[ItemID comparable, Item Labeled](nameOfTrackedItems string) *LabelNameValueIndex[ItemID, Item] {
return &LabelNameValueIndex[ItemID, Item]{
nameOfTrackedItems: nameOfTrackedItems,
allItems: map[ItemID]Item{},
labelNameToValueToIDs: map[string]values[ItemID]{},
}
}
type values[ItemID comparable] struct {
m map[string]*set.Adaptive[ItemID]
count int
}
// Add an item to the index. Note: its labels will be captured at this
// time, so if the objects labels are mutated, it is important to remove the
// item before changing its labels and then re-Add the updated item.
//
// To avoid the above bug, panics if the same ID is added twice.
func (idx *LabelNameValueIndex[ItemID, Item]) Add(id ItemID, item Item) {
if _, ok := idx.allItems[id]; ok {
logrus.WithFields(logrus.Fields{
"id": id,
"item": item,
"index": idx.nameOfTrackedItems,
}).Panic("Add called for ID that is already in the index.")
}
idx.allItems[id] = item
for k, v := range item.OwnLabels() {
vals, ok := idx.labelNameToValueToIDs[k]
if !ok {
vals = values[ItemID]{
m: map[string]*set.Adaptive[ItemID]{},
}
idx.labelNameToValueToIDs[k] = vals
}
setOfIDs := vals.m[v]
if setOfIDs == nil {
setOfIDs = set.NewAdaptive[ItemID]()
vals.m[v] = setOfIDs
}
setOfIDs.Add(id)
vals.count++
// Map stores the value type (not pointer); write back the update.
idx.labelNameToValueToIDs[k] = vals
}
}
// Remove an item from the index. Note that it is important that the labels
// are not mutated between Add and Remove calls.
func (idx *LabelNameValueIndex[ItemID, Item]) Remove(id ItemID) {
v := idx.allItems[id]
for k, v := range v.OwnLabels() {
vals := idx.labelNameToValueToIDs[k]
setOfIDs := vals.m[v]
setOfIDs.Discard(id)
if setOfIDs.Len() == 0 {
delete(vals.m, v)
if len(vals.m) == 0 {
delete(idx.labelNameToValueToIDs, k)
continue
}
}
vals.count--
idx.labelNameToValueToIDs[k] = vals
}
delete(idx.allItems, id)
}
// StrategyFor returns the best available ScanStrategy for the given
// label name and selector LabelRestriction (which should be the restriction
// for that label). If the LabelRestriction is not "useful", returns
// FullScanStrategy().
func (idx *LabelNameValueIndex[ItemID, Item]) StrategyFor(labelName string, r parser.LabelRestriction) ScanStrategy[ItemID] {
if !r.MustBePresent {
// Not much we can do if the selector doesn't match on this label.
return FullScanStrategy[ItemID, Item]{allItems: idx.allItems}
}
if r.MustHaveOneOfValues == nil {
// A selector such as "has(labelName)", which matches the label but
// not any particular value.
if vals, ok := idx.labelNameToValueToIDs[labelName]; !ok {
logrus.Debugf("Found no matches for %s with %s=<any>", idx.nameOfTrackedItems, labelName)
return NoMatchStrategy[ItemID]{}
} else {
logrus.Debugf("Found %d %s with %s=<any>", vals.count, idx.nameOfTrackedItems, labelName)
return LabelNameStrategy[ItemID]{label: labelName, values: vals}
}
}
// If we get here, then the selector does match on this label, and it cares
// about specific values. Whittle down the list of values to the ones that
// match objects that we're tracking.
var filteredMustHaves []string
var idSets []set.Set[ItemID]
for _, v := range r.MustHaveOneOfValues {
if idsSet := idx.labelNameToValueToIDs[labelName].m[v]; idsSet != nil {
filteredMustHaves = append(filteredMustHaves, v)
idSets = append(idSets, idsSet)
}
}
if len(filteredMustHaves) == 0 {
// We filtered all values out! That means that the selector cannot
// match anything. If it could match something, we'd have found it
// in the index.
logrus.Debugf("No %s with %s=%v", idx.nameOfTrackedItems, labelName, r.MustHaveOneOfValues)
return NoMatchStrategy[ItemID]{}
}
if len(filteredMustHaves) == 1 {
// Best case: we got exactly one label value.
return LabelNameSingleValueStrategy[ItemID]{
label: labelName,
value: filteredMustHaves[0],
idSet: idSets[0],
}
}
// We have matches on label and more than one value.
return LabelNameMultiValueStrategy[ItemID]{
label: labelName,
values: filteredMustHaves,
idSets: idSets,
}
}
// FullScanStrategy returns a scan strategy that scans all items.
func (idx *LabelNameValueIndex[ItemID, Item]) FullScanStrategy() ScanStrategy[ItemID] {
return FullScanStrategy[ItemID, Item]{allItems: idx.allItems}
}
// Get looks up an item by its ID. (Allows this object to be the primary
// map datastructure for storing the items.)
func (idx *LabelNameValueIndex[ItemID, Item]) Get(id ItemID) (Item, bool) {
v, ok := idx.allItems[id]
return v, ok
}
func (idx *LabelNameValueIndex[ItemID, Item]) Len() int {
return len(idx.allItems)
}
// ScanStrategy abstracts over particular types of scans of the index, allowing
// them to be compared/scored without actually executing the scan until ready
// to do so.
type ScanStrategy[ItemID any] interface {
// EstimatedItemsToScan returns an estimate for how many items this scan
// strategy would produce if Scan() was called. Some strategies return
// an approximate value because calculating the real value would require
// executing the scan.
EstimatedItemsToScan() int
// Scan executes the scan. It calls the given func once with each ID
// produced by the scan. Each ID is only emitted once (the strategy is
// responsible for any deduplication). Scanning continues while the func
// returns true.
Scan(func(id ItemID) bool)
// Name of the strategy (used in prometheus metrics).
Name() string
fmt.Stringer
}
// NoMatchStrategy is a ScanStrategy that produces no items, it is returned
// when the index can prove that there are no matching items.
type NoMatchStrategy[ItemID any] struct {
}
func (n NoMatchStrategy[ItemID]) String() string {
return "no match"
}
func (n NoMatchStrategy[ItemID]) EstimatedItemsToScan() int {
return 0
}
func (n NoMatchStrategy[ItemID]) Scan(func(id ItemID) bool) {
}
func (n NoMatchStrategy[ItemID]) Name() string {
return "no-match"
}
// LabelNameSingleValueStrategy is a ScanStrategy that scans over items that have
// a specific value for a certain label. It is the narrowest, most optimized
// strategy.
type LabelNameSingleValueStrategy[ItemID comparable] struct {
label string
value string
idSet set.Set[ItemID]
}
func (s LabelNameSingleValueStrategy[ItemID]) String() string {
return fmt.Sprintf("scan single label %s=%v", s.label, s.value)
}
func (s LabelNameSingleValueStrategy[ItemID]) EstimatedItemsToScan() int {
return s.idSet.Len()
}
func (s LabelNameSingleValueStrategy[ItemID]) Scan(f func(id ItemID) bool) {
// Ideal case, we have one set to scan.
s.idSet.Iter(func(id ItemID) error {
if !f(id) {
return set.StopIteration
}
return nil
})
}
func (s LabelNameSingleValueStrategy[ItemID]) Name() string {
return "single-value"
}
// LabelNameMultiValueStrategy is a ScanStrategy that scans over items that have
// specific, values for a certain label.
type LabelNameMultiValueStrategy[ItemID comparable] struct {
label string
values []string
idSets []set.Set[ItemID]
}
func (s LabelNameMultiValueStrategy[ItemID]) String() string {
return fmt.Sprintf("scan multi label %s=%v", s.label, s.values)
}
func (s LabelNameMultiValueStrategy[ItemID]) EstimatedItemsToScan() int {
count := 0
for _, s := range s.idSets {
// Over counts if one object is in multiple sets, but Scan() needs
// to do a bit of work per object to dedupe.
count += s.Len()
}
return count
}
func (s LabelNameMultiValueStrategy[ItemID]) Scan(f func(id ItemID) bool) {
set.IterUnion(s.idSets, f)
}
func (s LabelNameMultiValueStrategy[ItemID]) Name() string {
return "multi-value"
}
// LabelNameStrategy is a ScanStrategy that scans all object that have a
// particular label, no matter the value of that label. It is used for
// selectors such as "has(labelName)".
type LabelNameStrategy[ItemID comparable] struct {
label string
values values[ItemID]
}
func (s LabelNameStrategy[ItemID]) String() string {
return fmt.Sprintf("scan all values of label %s", s.label)
}
func (s LabelNameStrategy[ItemID]) EstimatedItemsToScan() int {
return s.values.count
}
func (s LabelNameStrategy[ItemID]) Scan(f func(id ItemID) bool) {
for _, epIDs := range s.values.m {
stop := false
epIDs.Iter(func(id ItemID) error {
if !f(id) {
stop = true
return set.StopIteration
}
return nil
})
if stop {
return
}
}
}
func (s LabelNameStrategy[ItemID]) Name() string {
return "label-name"
}
// FullScanStrategy is a ScanStrategy that scans all items in a completely
// unoptimized way. It is returned if the selector cannot be optimized.
type FullScanStrategy[ItemID comparable, Item Labeled] struct {
allItems map[ItemID]Item
}
func (s FullScanStrategy[ItemID, Item]) String() string {
return "full-scan"
}
func (s FullScanStrategy[ItemID, Item]) EstimatedItemsToScan() int {
return len(s.allItems)
}
func (s FullScanStrategy[ItemID, Item]) Scan(f func(id ItemID) bool) {
for id := range s.allItems {
if !f(id) {
return
}
}
}
func (s FullScanStrategy[ItemID, Item]) Name() string {
return "full-scan"
}