-
Notifications
You must be signed in to change notification settings - Fork 18
/
sort.go
355 lines (322 loc) · 10.3 KB
/
sort.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/***************************************************************
*
* Copyright (C) 2024, Pelican Project, Morgridge Institute for Research
*
* 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.
*
***************************************************************/
package director
import (
"archive/tar"
"cmp"
"compress/gzip"
"context"
"fmt"
"io"
"math/rand"
"net"
"net/http"
"net/netip"
"os"
"path"
"path/filepath"
"slices"
"sort"
"strings"
"sync/atomic"
"time"
"github.com/oschwald/geoip2-golang"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/pelicanplatform/pelican/param"
"github.com/pelicanplatform/pelican/server_structs"
)
const (
maxMindURL string = "https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City&license_key=%s&suffix=tar.gz"
)
var (
maxMindReader atomic.Pointer[geoip2.Reader]
)
type (
SwapMap struct {
Weight float64
Index int
}
SwapMaps []SwapMap
)
type Coordinate struct {
Lat float64 `mapstructure:"lat"`
Long float64 `mapstructure:"long"`
}
type GeoIPOverride struct {
IP string `mapstructure:"IP"`
Coordinate Coordinate `mapstructure:"Coordinate"`
}
var invalidOverrideLogOnce = map[string]bool{}
var geoIPOverrides []GeoIPOverride
func (me SwapMaps) Len() int {
return len(me)
}
func (me SwapMaps) Less(left, right int) bool {
return me[left].Weight < me[right].Weight
}
func (me SwapMaps) Swap(left, right int) {
me[left], me[right] = me[right], me[left]
}
// Check for any pre-configured IP-to-lat/long overrides. If the passed address
// matches an override IP (either directly or via CIDR masking), then we use the
// configured lat/long from the override instead of relying on MaxMind.
// NOTE: We don't return an error because if checkOverrides encounters an issue,
// we still have GeoIP to fall back on.
func checkOverrides(addr net.IP) (coordinate *Coordinate) {
// Unmarshal the values, but only the first time we run through this block
if geoIPOverrides == nil {
err := param.GeoIPOverrides.Unmarshal(&geoIPOverrides)
if err != nil {
log.Warningf("Error while unmarshaling GeoIP Overrides: %v", err)
}
}
for _, geoIPOverride := range geoIPOverrides {
// Check for regular IP addresses before CIDR
overrideIP := net.ParseIP(geoIPOverride.IP)
if overrideIP == nil {
// The IP is malformed
if !invalidOverrideLogOnce[geoIPOverride.IP] && !strings.Contains(geoIPOverride.IP, "/") {
// Don't return here, because we have more to check.
// Do provide a notice to the user, however.
log.Warningf("Failed to parse configured GeoIPOverride address (%s). Unable to use for GeoIP resolution!", geoIPOverride.IP)
invalidOverrideLogOnce[geoIPOverride.IP] = true
}
}
if overrideIP.Equal(addr) {
return &geoIPOverride.Coordinate
}
// Alternatively, we can match by CIDR blocks
if strings.Contains(geoIPOverride.IP, "/") {
_, ipNet, err := net.ParseCIDR(geoIPOverride.IP)
if err != nil {
if !invalidOverrideLogOnce[geoIPOverride.IP] {
// Same reason as above for not returning.
log.Warningf("Failed to parse configured GeoIPOverride CIDR address (%s): %v. Unable to use for GeoIP resolution!", geoIPOverride.IP, err)
invalidOverrideLogOnce[geoIPOverride.IP] = true
}
continue
}
if ipNet.Contains(addr) {
return &geoIPOverride.Coordinate
}
}
}
return nil
}
func getLatLong(addr netip.Addr) (lat float64, long float64, err error) {
ip := net.IP(addr.AsSlice())
override := checkOverrides(ip)
if override != nil {
log.Infof("Overriding Geolocation of detected IP (%s) to lat:long %f:%f based on configured overrides", ip.String(), (override.Lat), override.Long)
return override.Lat, override.Long, nil
}
reader := maxMindReader.Load()
if reader == nil {
err = errors.New("No GeoIP database is available")
return
}
record, err := reader.City(ip)
if err != nil {
return
}
lat = record.Location.Latitude
long = record.Location.Longitude
if lat == 0 && long == 0 {
log.Infof("GeoIP Resolution of the address %s resulted in the nul lat/long.", ip.String())
}
return
}
func getClientLatLong(addr netip.Addr) (coord Coordinate, ok bool) {
var err error
coord.Lat, coord.Long, err = getLatLong(addr)
ok = (err == nil && !(coord.Lat == 0 && coord.Long == 0))
if err != nil {
log.Warningf("failed to resolve lat/long for address %s: %v", addr, err)
}
return
}
// Sort serverAds based on the IP address of the client with shorter distance between
// server IP and client having higher priority
func sortServerAdsByIP(addr netip.Addr, ads []server_structs.ServerAd) ([]server_structs.ServerAd, error) {
// Each entry in weights will map a priority to an index in the original ads slice.
// A larger weight is a higher priority.
weights := make(SwapMaps, len(ads))
sortMethod := param.Director_CacheSortMethod.GetString()
// For each ad, we apply the configured sort method to determine a priority weight.
for idx, ad := range ads {
switch sortMethod {
case "distance":
clientCoord, ok := getClientLatLong(addr)
if !ok {
// Unable to compute distances for this server; just do random distances.
// Below we sort weights in descending order, so we assign negative value here,
// causing them to always be at the end of the sorted list.
weights[idx] = SwapMap{0 - rand.Float64(), idx}
} else {
weights[idx] = SwapMap{distanceWeight(clientCoord, ad),
idx}
}
case "distanceAndLoad":
clientCoord, ok := getClientLatLong(addr)
if !ok {
weights[idx] = SwapMap{0 - rand.Float64(), idx}
} else {
// Each server ad will have a load value that we can use for sorting
weights[idx] = SwapMap{distanceAndLoadWeight(clientCoord, ad),
idx}
}
case "random":
weights[idx] = SwapMap{rand.Float64(), idx}
default:
return nil, errors.Errorf("Invalid sort method '%s' set in Director.CacheSortMethod. Valid methods are 'distance',"+
"'distanceAndLoad', and 'random.'", param.Director_CacheSortMethod.GetString())
}
}
// Larger weight = higher priority, so we reverse the sort (which would otherwise default to ascending)
sort.Sort(sort.Reverse(weights))
resultAds := make([]server_structs.ServerAd, len(ads))
for idx, weight := range weights {
resultAds[idx] = ads[weight.Index]
}
return resultAds, nil
}
// Sort a list of ServerAds with the following rule:
// * if a ServerAds has FromTopology = true, then it will be moved to the end of the list
// * if two ServerAds has the SAME FromTopology value (both true or false), then
func sortServerAdsByTopo(ads []*server_structs.Advertisement) []*server_structs.Advertisement {
slices.SortStableFunc(ads, func(a, b *server_structs.Advertisement) int {
if a.FromTopology && !b.FromTopology {
return 1
} else if !a.FromTopology && b.FromTopology {
return -1
} else {
return cmp.Compare(a.Name, b.Name)
}
})
return ads
}
func downloadDB(localFile string) error {
err := os.MkdirAll(filepath.Dir(localFile), 0755)
if err != nil {
return err
}
var licenseKey string
keyFile := param.Director_MaxMindKeyFile.GetString()
keyFromEnv := viper.GetString("MAXMINDKEY")
if keyFile != "" {
contents, err := os.ReadFile(keyFile)
if err != nil {
return err
}
licenseKey = strings.TrimSpace(string(contents))
} else if keyFromEnv != "" {
licenseKey = keyFromEnv
} else {
return errors.New("A MaxMind key file must be specified in the config (Director.MaxMindKeyFile), in the environment (PELICAN_DIRECTOR_MAXMINDKEYFILE), or the key must be provided via the environment variable PELICAN_MAXMINDKEY)")
}
url := fmt.Sprintf(maxMindURL, licenseKey)
localDir := filepath.Dir(localFile)
fileHandle, err := os.CreateTemp(localDir, filepath.Base(localFile)+".tmp")
if err != nil {
return err
}
defer fileHandle.Close()
resp, err := http.Get(url)
if err != nil {
os.Remove(fileHandle.Name())
return err
}
defer resp.Body.Close()
gz, err := gzip.NewReader(resp.Body)
if err != nil {
return err
}
tr := tar.NewReader(gz)
foundDB := false
for {
hdr, err := tr.Next()
if err == io.EOF {
break
} else if err != nil {
return err
}
baseName := path.Base(hdr.Name)
if baseName != "GeoLite2-City.mmdb" {
continue
}
if _, err = io.Copy(fileHandle, tr); err != nil {
os.Remove(fileHandle.Name())
return err
}
foundDB = true
break
}
if !foundDB {
return errors.New("GeoIP database not found in downloaded resource")
}
if err = os.Rename(fileHandle.Name(), localFile); err != nil {
return err
}
return nil
}
func periodicMaxMindReload(ctx context.Context) {
// The MaxMindDB updates Tuesday/Thursday. While a free API key
// does get 1000 downloads a month, we might still want to change
// this eventually to guarantee we only update on those days...
// Update once every other day
ticker := time.NewTicker(48 * time.Hour)
for {
select {
case <-ticker.C:
localFile := param.Director_GeoIPLocation.GetString()
if err := downloadDB(localFile); err != nil {
log.Warningln("Failed to download GeoIP database:", err)
} else {
localReader, err := geoip2.Open(localFile)
if err != nil {
log.Warningln("Failed to re-open GeoIP database:", err)
} else {
maxMindReader.Store(localReader)
}
}
case <-ctx.Done():
return
}
}
}
func InitializeDB(ctx context.Context) {
go periodicMaxMindReload(ctx)
localFile := param.Director_GeoIPLocation.GetString()
localReader, err := geoip2.Open(localFile)
if err != nil {
log.Warningln("Local GeoIP database file not present; will attempt a download.", err)
err = downloadDB(localFile)
if err != nil {
log.Errorln("Failed to download GeoIP database! Will not be available:", err)
return
}
localReader, err = geoip2.Open(localFile)
if err != nil {
log.Errorln("Failed to reopen GeoIP database! Will not be available:", err)
return
}
}
maxMindReader.Store(localReader)
}