/
subdomains.go
319 lines (302 loc) · 7.75 KB
/
subdomains.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
package services
import (
"fmt"
"math"
"net"
"sort"
"strconv"
"sync"
"github.com/pkg/errors"
"github.com/urfave/cli"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
webOriginHostRedirectAddressTypeFlag = "origin-host-redirect-address-type"
webOriginHostRedirectPrefixFlag = "origin-host-redirect-prefix"
maxSubdomains = 3
infohashMaxSpread = 1
)
type Subdomains struct {
subs []string
sc []NodeStatWithScore
inited bool
err error
mux sync.Mutex
k8s *K8SClient
nsp *NodesStatPool
infoHash string
redirectPrefix string
redirectAddressType string
jobNamespace string
useCPU bool
useBandwidth bool
pools []string
skipActiveJobSearch bool
}
func RegisterSubdomainsFlags(f []cli.Flag) []cli.Flag {
return append(f,
cli.StringFlag{
Name: webOriginHostRedirectPrefixFlag,
Usage: "subdomain prefix of host to be redirected",
EnvVar: "WEB_ORIGIN_HOST_REDIRECT_PREFIX",
Value: "abra--",
},
cli.StringFlag{
Name: webOriginHostRedirectAddressTypeFlag,
Usage: "preferred node address type",
EnvVar: "WEB_ORIGIN_HOST_REDIRECT_ADDRESS_TYPE",
Value: "ExternalIP",
},
)
}
func NewSubdomains(c *cli.Context, k8s *K8SClient, nsp *NodesStatPool, infoHash string, skipActiveJobSearch bool, useCPU bool, useBandwidth bool, pools []string) *Subdomains {
return &Subdomains{
k8s: k8s,
nsp: nsp,
jobNamespace: c.String(jobNamespaceFlag),
redirectPrefix: c.String(webOriginHostRedirectPrefixFlag),
redirectAddressType: c.String(webOriginHostRedirectAddressTypeFlag),
infoHash: infoHash,
useCPU: useCPU,
useBandwidth: useBandwidth,
pools: pools,
skipActiveJobSearch: skipActiveJobSearch,
}
}
type NodeStatWithScore struct {
NodeStat
Score float64
Distance int
}
func (s *Subdomains) filterByPool(stats []NodeStatWithScore, pool string) []NodeStatWithScore {
var res []NodeStatWithScore
for _, st := range stats {
for _, p := range st.Pool {
if pool == p {
res = append(res, st)
}
}
}
return res
}
func (s *Subdomains) filterWithZeroScore(stats []NodeStatWithScore) []NodeStatWithScore {
var res []NodeStatWithScore
for _, st := range stats {
if st.Score != 0 {
res = append(res, st)
}
}
return res
}
func (s *Subdomains) filterByActivePod(stats []NodeStatWithScore) ([]NodeStatWithScore, error) {
cl, err := s.k8s.Get()
if err != nil {
return nil, errors.Wrap(err, "failed to get k8s client")
}
var nodeNames []string
infoHash := s.infoHash
timeout := int64(30)
if infoHash != "" {
opts := metav1.ListOptions{
LabelSelector: fmt.Sprintf("%vinfo-hash=%v", k8SLabelPrefix, infoHash),
TimeoutSeconds: &timeout,
}
pods, err := cl.CoreV1().Pods(s.jobNamespace).List(opts)
if err != nil {
return nil, errors.Wrap(err, "failed to find active job")
}
for _, p := range pods.Items {
if p.Status.Phase != corev1.PodFailed {
nodeNames = append(nodeNames, p.Spec.NodeName)
}
}
}
if len(nodeNames) == 0 {
return stats, nil
}
var res []NodeStatWithScore
for _, nn := range nodeNames {
for _, st := range stats {
if nn == st.Name {
res = append(res, st)
}
}
}
return res, nil
}
func (s *Subdomains) updateScoreByCPU(stats []NodeStatWithScore) []NodeStatWithScore {
for i, v := range stats {
if v.NodeCPU.Current < v.NodeCPU.Low {
continue
} else if v.NodeCPU.Current >= v.NodeCPU.High {
stats[i].Score = 0
} else {
ratio := (v.NodeCPU.High - v.NodeCPU.Current) / (v.NodeCPU.High - v.NodeCPU.Low)
stats[i].Score = stats[i].Score * ratio * ratio
}
}
return stats
}
func (s *Subdomains) updateScoreByInfoHash(stats []NodeStatWithScore, useCPU bool) ([]NodeStatWithScore, error) {
if s.infoHash == "" {
return stats, nil
}
if len(stats) == 0 {
return stats, nil
}
sort.Slice(stats, func(i, j int) bool {
return stats[i].Name > stats[j].Name
})
hex := s.infoHash[0:5]
num, err := strconv.ParseInt(hex, 16, 64)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse hex from infohash=%v", s.infoHash)
}
num = num * 1000
total := 1048575 * 1000
t := 0
if useCPU {
totalCPU := 0
for _, s := range stats {
totalCPU += int(s.NodeCPU.High * 1000)
}
interval := int64(total / totalCPU)
cur := int64(0)
for i := 0; i < len(stats); i++ {
cur += int64(stats[i].NodeCPU.High*1000) * interval
if num < cur {
t = i
break
}
}
} else {
interval := int64(total / len(stats))
for i := 0; i < len(stats); i++ {
if num < (int64(i)+1)*interval {
t = i
break
}
}
}
spread := int(math.Floor(float64(len(stats)) / 2))
if spread > infohashMaxSpread {
spread = infohashMaxSpread
}
for i := range stats {
stats[i].Distance = spread + 1
}
for n := -spread; n <= spread; n++ {
m := t + n
if m < 0 {
m = len(stats) + m
}
if m >= len(stats) {
m = m - len(stats)
}
d := math.Abs(float64(n))
stats[m].Distance = int(d)
}
for i := range stats {
if stats[i].Distance == 0 {
continue
}
ratio := 1 / float64(stats[i].Distance) / 2
stats[i].Score = stats[i].Score * ratio
}
return stats, nil
}
func (s *Subdomains) updateScoreByBandwidth(stats []NodeStatWithScore) []NodeStatWithScore {
for i, v := range stats {
if v.NodeBandwidth.Current < v.NodeBandwidth.Low {
continue
} else if v.NodeBandwidth.Current >= v.NodeBandwidth.High {
stats[i].Score = 0
} else {
ratio := float64(v.NodeBandwidth.High-v.NodeBandwidth.Current) / float64(v.NodeBandwidth.High-v.NodeBandwidth.Low)
stats[i].Score = stats[i].Score * ratio * ratio
}
}
return stats
}
func (s *Subdomains) getScoredStats() ([]NodeStatWithScore, error) {
if len(s.pools) == 0 {
return s.getScoredStatsByPool("")
}
for _, p := range s.pools {
sc, err := s.getScoredStatsByPool(p)
if err != nil {
return nil, errors.Wrapf(err, "failed to get nodes stat for pool %v", p)
}
if len(sc) > 0 {
return sc, nil
}
}
return []NodeStatWithScore{}, nil
}
func (s *Subdomains) getScoredStatsByPool(pool string) ([]NodeStatWithScore, error) {
stats, err := s.nsp.Get()
if err != nil {
return nil, errors.Wrap(err, "failed to get nodes stat")
}
var sc []NodeStatWithScore
for _, s := range stats {
sc = append(sc, NodeStatWithScore{
NodeStat: s,
Score: 1,
Distance: -1,
})
}
if pool != "" {
sc = s.filterByPool(sc, pool)
}
if !s.skipActiveJobSearch {
sc, err = s.filterByActivePod(sc)
if err != nil {
return nil, errors.Wrap(err, "failed to filter by active job")
}
}
if s.useCPU {
sc = s.updateScoreByCPU(sc)
}
if s.useBandwidth {
sc = s.updateScoreByBandwidth(sc)
}
sc, err = s.updateScoreByInfoHash(sc, s.useCPU)
if err != nil {
return nil, errors.Wrap(err, "failed to update score by hash")
}
sort.Slice(sc, func(i, j int) bool {
return sc[i].Score > sc[j].Score
})
sc = s.filterWithZeroScore(sc)
// fmt.Printf("%+v", sc)
return sc, nil
}
func (s *Subdomains) get() ([]NodeStatWithScore, []string, error) {
stats, err := s.getScoredStats()
if err != nil {
return nil, nil, errors.Wrap(err, "failed to get sorted nodes stat")
}
var res []string
for _, st := range stats {
byteIP := net.ParseIP(st.IP)
hexIP := fmt.Sprintf("%02x%02x%02x%02x", byteIP[12], byteIP[13], byteIP[14], byteIP[15])
res = append(res, s.redirectPrefix+hexIP)
}
l := len(res)
if l > maxSubdomains {
l = maxSubdomains
}
return stats, res[0:l], nil
}
func (s *Subdomains) Get() ([]NodeStatWithScore, []string, error) {
s.mux.Lock()
defer s.mux.Unlock()
if s.inited {
return s.sc, s.subs, s.err
}
s.sc, s.subs, s.err = s.get()
s.inited = true
return s.sc, s.subs, s.err
}