-
Notifications
You must be signed in to change notification settings - Fork 1
/
scheduler_filter.go
227 lines (181 loc) · 5.79 KB
/
scheduler_filter.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
package prophet
// Filter is used for filter container
type Filter interface {
// FilterSource returns true means skip this container for schedule
FilterSource(container *ContainerRuntime) bool
// FilterTarget returns true means skip this container for schedule
FilterTarget(container *ContainerRuntime) bool
}
func filterSource(container *ContainerRuntime, filters []Filter) bool {
for _, filter := range filters {
if filter.FilterSource(container) {
return true
}
}
return false
}
func filterTarget(container *ContainerRuntime, filters []Filter) bool {
for _, filter := range filters {
if filter.FilterTarget(container) {
return true
}
}
return false
}
type labelFilter struct {
labels []Pair
}
type stateFilter struct {
cfg *Cfg
}
type storageThresholdFilter struct {
cfg *Cfg
}
type excludedFilter struct {
sources map[uint64]struct{}
targets map[uint64]struct{}
}
type healthFilter struct {
cfg *Cfg
}
type cacheFilter struct {
freezeCache *resourceFreezeCache
}
type distinctScoreFilter struct {
cfg *Cfg
containers []*ContainerRuntime
safeScore float64
}
type snapshotCountFilter struct {
cfg *Cfg
}
// NewStorageThresholdFilter returns a filter for choose resource container by storage rate
func NewStorageThresholdFilter(cfg *Cfg) Filter {
return &storageThresholdFilter{cfg: cfg}
}
// NewStateFilter returns a filter for choose resource container by state
func NewStateFilter(cfg *Cfg) Filter {
return &stateFilter{cfg: cfg}
}
// NewExcludedFilter returns a filter for choose resource container by excluded value
func NewExcludedFilter(sources, targets map[uint64]struct{}) Filter {
return &excludedFilter{
sources: sources,
targets: targets,
}
}
// NewHealthFilter returns a filter for choose resource container by health info
func NewHealthFilter(cfg *Cfg) Filter {
return &healthFilter{cfg: cfg}
}
// NewCacheFilter returns a filter for choose resource container by runtime cache
func NewCacheFilter(freezeCache *resourceFreezeCache) Filter {
return &cacheFilter{freezeCache: freezeCache}
}
// NewBlockFilter returns a filter for choose resource container by block
func NewBlockFilter() Filter {
return &blockFilter{}
}
// NewDistinctScoreFilter a filter for ensures that distinct score will not decrease.
func NewDistinctScoreFilter(cfg *Cfg, containers []*ContainerRuntime, source *ContainerRuntime) Filter {
newContainers := make([]*ContainerRuntime, 0, len(containers)-1)
for _, s := range newContainers {
if s.meta.ID() == source.meta.ID() {
continue
}
newContainers = append(newContainers, s)
}
return &distinctScoreFilter{
cfg: cfg,
containers: newContainers,
safeScore: cfg.getDistinctScore(newContainers, source),
}
}
// NewSnapshotCountFilter returns snapshot filter
func NewSnapshotCountFilter(cfg *Cfg) Filter {
return &snapshotCountFilter{cfg: cfg}
}
// NewLabelFilter returns label filter, if the container missing the labels, skip it.
func NewLabelFilter(labels []Pair) Filter {
return &labelFilter{labels: labels}
}
func (f *stateFilter) filter(container *ContainerRuntime) bool {
return !(container.IsUp() && container.Downtime() < f.cfg.MaxAllowContainerDownDuration)
}
func (f *stateFilter) FilterSource(container *ContainerRuntime) bool {
return f.filter(container)
}
func (f *stateFilter) FilterTarget(container *ContainerRuntime) bool {
return f.filter(container)
}
func (f *storageThresholdFilter) FilterSource(container *ContainerRuntime) bool {
return false
}
func (f *storageThresholdFilter) FilterTarget(container *ContainerRuntime) bool {
return container.StorageUsedRatio() > f.cfg.MinAvailableStorageUsedRate
}
func (f *excludedFilter) FilterSource(container *ContainerRuntime) bool {
_, ok := f.sources[container.meta.ID()]
return ok
}
func (f *excludedFilter) FilterTarget(container *ContainerRuntime) bool {
_, ok := f.targets[container.meta.ID()]
return ok
}
type blockFilter struct{}
func (f *blockFilter) FilterSource(container *ContainerRuntime) bool {
return container.IsBlocked()
}
func (f *blockFilter) FilterTarget(container *ContainerRuntime) bool {
return container.IsBlocked()
}
func (f *healthFilter) filter(container *ContainerRuntime) bool {
if container.HasNoneHeartbeat() || container.busy {
return true
}
return container.Downtime() > f.cfg.MaxAllowContainerDownDuration
}
func (f *healthFilter) FilterSource(container *ContainerRuntime) bool {
return f.filter(container)
}
func (f *healthFilter) FilterTarget(container *ContainerRuntime) bool {
return f.filter(container)
}
func (f *cacheFilter) FilterSource(container *ContainerRuntime) bool {
_, ok := f.freezeCache.get(container.meta.ID())
return ok
}
func (f *cacheFilter) FilterTarget(container *ContainerRuntime) bool {
return false
}
func (f *distinctScoreFilter) FilterSource(container *ContainerRuntime) bool {
return false
}
func (f *distinctScoreFilter) FilterTarget(container *ContainerRuntime) bool {
return f.cfg.getDistinctScore(f.containers, container) < f.safeScore
}
func (f *snapshotCountFilter) filter(container *ContainerRuntime) bool {
return container.sendingSnapCount > f.cfg.MaxLimitSnapshotsCount ||
container.receivingSnapCount > f.cfg.MaxLimitSnapshotsCount ||
container.applyingSnapCount > f.cfg.MaxLimitSnapshotsCount
}
func (f *snapshotCountFilter) FilterSource(container *ContainerRuntime) bool {
return f.filter(container)
}
func (f *snapshotCountFilter) FilterTarget(container *ContainerRuntime) bool {
return f.filter(container)
}
func (f *labelFilter) filter(container *ContainerRuntime) bool {
for _, l := range f.labels {
if l.Value != container.GetLabelValue(l.Key) {
return true
}
}
return false
}
func (f *labelFilter) FilterSource(container *ContainerRuntime) bool {
return f.filter(container)
}
func (f *labelFilter) FilterTarget(container *ContainerRuntime) bool {
return f.filter(container)
}