/
constraints.go
216 lines (185 loc) · 4.94 KB
/
constraints.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
package kafkazk
import (
"errors"
)
var (
// ErrNoBrokers error.
ErrNoBrokers = errors.New("No additional brokers that meet Constraints")
// ErrInvalidSelectionMethod error.
ErrInvalidSelectionMethod = errors.New("Invalid selection method")
)
// Constraints holds a map of
// IDs and locality key-values.
type Constraints struct {
requestSize float64
locality map[string]bool
id map[int]bool
}
// NewConstraints returns an empty *Constraints.
func NewConstraints() *Constraints {
return &Constraints{
locality: make(map[string]bool),
id: make(map[int]bool),
}
}
// ConstraintsParams holds parameters for
// the SelectBroker method.
type ConstraintsParams struct {
SelectorMethod string
MinUniqueRackIDs int
RequestSize float64
SeedVal int64
}
// SelectBroker takes a BrokerList and a ConstraintsParams and
// selects the most suitable broker that passes all specified
// constraints.
func (c *Constraints) SelectBroker(b BrokerList, p ConstraintsParams) (*Broker, error) {
// Sort type based on the
// desired placement criteria.
switch p.SelectorMethod {
case "count":
// XXX Should instantiate
// a dedicated Rand for this.
b.SortPseudoShuffle(p.SeedVal)
case "storage":
b.SortByStorage()
default:
return nil, ErrInvalidSelectionMethod
}
var candidate *Broker
// Iterate over candidates.
for _, candidate = range b.Filter(AllBrokersFn) {
// Candidate passes, return.
if c.passesWithParams(candidate, p) {
c.requestSize = p.RequestSize
c.Add(candidate)
candidate.Used++
return candidate, nil
}
}
// List exhausted, no brokers passed.
return nil, ErrNoBrokers
}
// TODO deprecate.
// BestCandidate takes a *Constraints, selection method and
// pass / iteration number (for use as a seed value for
// pseudo-random number generation) and returns the
// most suitable broker.
func (b BrokerList) BestCandidate(c *Constraints, by string, p int64) (*Broker, error) {
// Sort type based on the
// desired placement criteria.
switch by {
case "count":
// XXX Should instantiate
// a dedicated Rand for this.
b.SortPseudoShuffle(p)
case "storage":
b.SortByStorage()
default:
return nil, ErrInvalidSelectionMethod
}
var candidate *Broker
// Iterate over candidates.
for _, candidate = range b.Filter(AllBrokersFn) {
// Candidate passes, return.
if c.passes(candidate) {
c.Add(candidate)
candidate.Used++
return candidate, nil
}
}
// List exhausted, no brokers passed.
return nil, ErrNoBrokers
}
// Add takes a *Broker and adds its attributes to the *Constraints.
// The requestSize is also subtracted from the *Broker.StorageFree.
func (c *Constraints) Add(b *Broker) {
b.StorageFree -= c.requestSize
if b.Locality != "" {
c.locality[b.Locality] = true
}
c.id[b.ID] = true
}
// MergeConstraints takes a brokerlist and updates the
// *Constraints by merging the attributes of all brokers
// from the supplied list.
func (c *Constraints) MergeConstraints(bl BrokerList) {
// Don't merge in attributes
// from nodes that will be removed.
var f BrokerFilterFn = func(b *Broker) bool {
if b.Replace {
return false
}
return true
}
for _, b := range bl.Filter(f) {
if b.Locality != "" {
c.locality[b.Locality] = true
}
c.id[b.ID] = true
}
}
// TODO deprecate.
// passes takes a *Broker and returns whether
// or not it passes Constraints.
func (c *Constraints) passes(b *Broker) bool {
switch {
// Fail if the candidate is one of the
// IDs already in the replica set.
case c.id[b.ID]:
return false
// Fail if the candidate is in any of
// the existing replica set localities.
case c.locality[b.Locality]:
return false
// Fail if the candidate would run
// out of storage.
case b.StorageFree-c.requestSize < 0:
return false
}
return true
}
func (c *Constraints) passesWithParams(b *Broker, p ConstraintsParams) bool {
var uniqueRackIDsSatisfied bool
if len(c.locality) >= p.MinUniqueRackIDs {
uniqueRackIDsSatisfied = true
}
switch {
// Check the candidate against already used IDs.
case c.id[b.ID]:
return false
// Check the candidate against rack ID constraints
// where all rack IDs must be unique.
case c.locality[b.Locality] && p.MinUniqueRackIDs == 0:
return false
// Check the candidate against rack ID constraints
// where a non-zero MinUniqueRackIDs is set.
case c.locality[b.Locality] && p.MinUniqueRackIDs > 0:
if !uniqueRackIDsSatisfied {
return false
}
// Check the candidate against storage capacity.
case b.StorageFree-p.RequestSize < 0:
return false
}
return true
}
// TODO deprecate.
// MergeConstraints takes a brokerlist and builds a
// *Constraints by merging the attributes of all brokers
// from the supplied list.
func MergeConstraints(bl BrokerList) *Constraints {
c := NewConstraints()
for _, b := range bl {
// Don't merge in attributes
// from nodes that will be removed.
if b.Replace {
continue
}
if b.Locality != "" {
c.locality[b.Locality] = true
}
c.id[b.ID] = true
}
return c
}