-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathspan_config_bounds.go
89 lines (79 loc) · 2.53 KB
/
span_config_bounds.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
// Copyright 2023 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package spanconfigbounds
import (
"sort"
"github.com/cockroachdb/cockroach/pkg/multitenant/tenantcapabilities/tenantcapabilitiespb"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/util/protoutil"
)
// TODO(ajwerner): Add benchmarking.
// Bounds wraps the tenantcapabilities.SpanConfigBounds and utilizes its
// policy to interact with SpanConfigs.
type Bounds struct {
b *tenantcapabilitiespb.SpanConfigBounds
}
// MakeBounds constructs a Bounds from its serialization.
func MakeBounds(b *tenantcapabilitiespb.SpanConfigBounds) Bounds {
if cb := b.ConstraintBounds; cb != nil {
sort.Sort(sortedConstraints(cb.Allowed))
}
return Bounds{b: b}
}
// Clamp will update the SpanConfig in place, clamping any properties
// which do not conform to the valueBound. It will return true if
// any properties were changed.
//
// An invariant on Clamp is that if it returns true, Conforms would have
// returned false, and now will return true.
func (b Bounds) Clamp(c *roachpb.SpanConfig) (changed bool) {
return b.clamp(c, nil)
}
// Conforms returns true if the SpanConfig conforms to the specified bounds.
func (b Bounds) Conforms(c *roachpb.SpanConfig) bool {
for _, f := range fields {
if !f.FieldBound(b).conforms(c, f) {
return false
}
}
return true
}
// Check will check the SpanConfig for bounds violations and report them.
// Note that it is less efficient than Clamp or Conforms, and it will allocate
// memory. Use this when a detailed error is desired. Violations can be
// transformed into an error via its AsError() method.
func (b Bounds) Check(c *roachpb.SpanConfig) Violations {
if b.Conforms(c) {
return nil
}
clone := protoutil.Clone(c).(*roachpb.SpanConfig)
var ret []Violation
b.clamp(clone, func(f Field) {
ret = append(ret, Violation{
Field: f,
Bounds: f.FieldBound(b),
Value: f.FieldValue(c),
ClampedTo: f.FieldValue(clone),
})
})
return ret
}
func (b Bounds) clamp(c *roachpb.SpanConfig, reporter func(Field)) (changed bool) {
for _, f := range fields {
if bb := f.FieldBound(b); !bb.clamp(c, f) {
continue
}
changed = true
if reporter != nil {
reporter(f)
}
}
return changed
}