/
searcher.go
282 lines (228 loc) · 7.41 KB
/
searcher.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
/*
* Copyright 2020 The Dragonfly 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.
*/
//go:generate mockgen -destination mocks/searcher_mock.go -source searcher.go -package mocks
package searcher
import (
"context"
"errors"
"fmt"
"net"
"regexp"
"sort"
"strings"
"github.com/mitchellh/mapstructure"
"github.com/yl2chen/cidranger"
"go.uber.org/zap"
logger "d7y.io/dragonfly/v2/internal/dflog"
"d7y.io/dragonfly/v2/manager/models"
"d7y.io/dragonfly/v2/pkg/math"
"d7y.io/dragonfly/v2/pkg/types"
)
const (
// Condition IDC key.
ConditionIDC = "idc"
// Condition location key.
ConditionLocation = "location"
)
const (
// cidrAffinityWeight is CIDR affinity weight.
cidrAffinityWeight float64 = 0.3
// hostnameAffinityWeight is hostname affinity weight.
hostnameAffinityWeight = 0.3
// idcAffinityWeight is IDC affinity weight.
idcAffinityWeight float64 = 0.25
// locationAffinityWeight is location affinity weight.
locationAffinityWeight = 0.14
// clusterTypeWeight is cluster type weight.
clusterTypeWeight float64 = 0.01
)
const (
// Maximum score.
maxScore float64 = 1.0
// Minimum score.
minScore = 0
)
const (
// Maximum number of elements.
maxElementLen = 5
)
// Scheduler cluster scopes.
type Scopes struct {
IDC string `mapstructure:"idc"`
Location string `mapstructure:"location"`
CIDRs []string `mapstructure:"cidrs"`
Hostnames []string `mapstructure:"hostnames"`
}
type Searcher interface {
// FindSchedulerClusters finds scheduler clusters that best matches the evaluation.
FindSchedulerClusters(ctx context.Context, schedulerClusters []models.SchedulerCluster, ip, hostname string,
conditions map[string]string, log *zap.SugaredLogger) ([]models.SchedulerCluster, error)
}
type searcher struct{}
func New(pluginDir string) Searcher {
s, err := LoadPlugin(pluginDir)
if err != nil {
logger.Info("use default searcher")
return &searcher{}
}
logger.Info("use searcher plugin")
return s
}
// FindSchedulerClusters finds scheduler clusters that best matches the evaluation.
func (s *searcher) FindSchedulerClusters(ctx context.Context, schedulerClusters []models.SchedulerCluster, ip, hostname string,
conditions map[string]string, log *zap.SugaredLogger) ([]models.SchedulerCluster, error) {
log = log.With("ip", ip, "hostname", hostname, "conditions", conditions)
if len(schedulerClusters) <= 0 {
return nil, errors.New("empty scheduler clusters")
}
clusters := FilterSchedulerClusters(conditions, schedulerClusters)
if len(clusters) == 0 {
return nil, fmt.Errorf("conditions %#v does not match any scheduler cluster", conditions)
}
sort.Slice(
clusters,
func(i, j int) bool {
var si, sj Scopes
if err := mapstructure.Decode(clusters[i].Scopes, &si); err != nil {
log.Errorf("cluster %s decode scopes failed: %v", clusters[i].Name, err)
return false
}
if err := mapstructure.Decode(clusters[j].Scopes, &sj); err != nil {
log.Errorf("cluster %s decode scopes failed: %v", clusters[i].Name, err)
return false
}
return Evaluate(ip, hostname, conditions, si, clusters[i], log) > Evaluate(ip, hostname, conditions, sj, clusters[j], log)
},
)
return clusters, nil
}
// Filter the scheduler clusters that dfdaemon can be used.
func FilterSchedulerClusters(conditions map[string]string, schedulerClusters []models.SchedulerCluster) []models.SchedulerCluster {
var clusters []models.SchedulerCluster
for _, schedulerCluster := range schedulerClusters {
// There are no active schedulers in the scheduler cluster
if len(schedulerCluster.Schedulers) == 0 {
continue
}
clusters = append(clusters, schedulerCluster)
}
return clusters
}
// Evaluate the degree of matching between scheduler cluster and dfdaemon.
func Evaluate(ip, hostname string, conditions map[string]string, scopes Scopes, cluster models.SchedulerCluster, log *zap.SugaredLogger) float64 {
return cidrAffinityWeight*calculateCIDRAffinityScore(ip, scopes.CIDRs, log) +
hostnameAffinityWeight*calculateHostnameAffinityScore(hostname, scopes.Hostnames, log) +
idcAffinityWeight*calculateIDCAffinityScore(conditions[ConditionIDC], scopes.IDC) +
locationAffinityWeight*calculateMultiElementAffinityScore(conditions[ConditionLocation], scopes.Location) +
clusterTypeWeight*calculateClusterTypeScore(cluster)
}
// calculateCIDRAffinityScore 0.0~1.0 larger and better.
func calculateCIDRAffinityScore(ip string, cidrs []string, log *zap.SugaredLogger) float64 {
// Construct CIDR ranger.
ranger := cidranger.NewPCTrieRanger()
for _, cidr := range cidrs {
_, network, err := net.ParseCIDR(cidr)
if err != nil {
log.Error(err)
continue
}
if err := ranger.Insert(cidranger.NewBasicRangerEntry(*network)); err != nil {
log.Error(err)
continue
}
}
// Determine whether an IP is contained in the constructed networks ranger.
contains, err := ranger.Contains(net.ParseIP(ip))
if err != nil {
log.Error(err)
return minScore
}
if !contains {
return minScore
}
return maxScore
}
// calculateHostnameAffinityScore 0.0~1.0 larger and better.
func calculateHostnameAffinityScore(hostname string, hostnames []string, log *zap.SugaredLogger) float64 {
if hostname == "" {
return minScore
}
if len(hostnames) == 0 {
return minScore
}
for _, v := range hostnames {
regex, err := regexp.Compile(v)
if err != nil {
log.Error(err)
continue
}
if regex.MatchString(hostname) {
return maxScore
}
}
return minScore
}
// calculateIDCAffinityScore 0.0~1.0 larger and better.
func calculateIDCAffinityScore(dst, src string) float64 {
if dst == "" || src == "" {
return minScore
}
if strings.EqualFold(dst, src) {
return maxScore
}
// Dst has only one element, src has multiple elements separated by "|".
// When dst element matches one of the multiple elements of src,
// it gets the max score of idc.
srcElements := strings.Split(src, types.AffinitySeparator)
for _, srcElement := range srcElements {
if strings.EqualFold(dst, srcElement) {
return maxScore
}
}
return minScore
}
// calculateMultiElementAffinityScore 0.0~1.0 larger and better.
func calculateMultiElementAffinityScore(dst, src string) float64 {
if dst == "" || src == "" {
return minScore
}
if strings.EqualFold(dst, src) {
return maxScore
}
// Calculate the number of multi-element matches divided by "|".
var score, elementLen int
dstElements := strings.Split(dst, types.AffinitySeparator)
srcElements := strings.Split(src, types.AffinitySeparator)
elementLen = math.Min(len(dstElements), len(srcElements))
// Maximum element length is 5.
if elementLen > maxElementLen {
elementLen = maxElementLen
}
for i := 0; i < elementLen; i++ {
if !strings.EqualFold(dstElements[i], srcElements[i]) {
break
}
score++
}
return float64(score) / float64(maxElementLen)
}
// calculateClusterTypeScore 0.0~1.0 larger and better.
func calculateClusterTypeScore(cluster models.SchedulerCluster) float64 {
if cluster.IsDefault {
return maxScore
}
return minScore
}