Skip to content

Commit d102d1f

Browse files
committed
fix: AWS random region for PlacementSocre API requests
Recently the region not offering Placement Score was fixed using a fixed region which offers the API, although the initial problem was solved using one single region for all the requests within the same account generates rate limit errors for API call. This commit introduces a way to randomize the region used to run the request distributing the load of the calls. Fixes #617 Signed-off-by: Adrian Riobo <[email protected]>
1 parent 6411dd6 commit d102d1f

File tree

4 files changed

+20
-15
lines changed

4 files changed

+20
-15
lines changed

go.mod

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
module github.com/redhat-developer/mapt
22

3-
go 1.24.1
4-
5-
toolchain go1.25.1
3+
go 1.24.6
64

75
require (
86
github.com/coocood/freecache v1.2.4

pkg/provider/aws/data/regions.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,15 @@ import (
1212

1313
var (
1414
optInStatusFilter string = "opt-in-status"
15-
optInStatusNorRequired string = "opt-in-not-required"
16-
optInStatusOptedIn string = "opted-in"
15+
OptInStatusNotRequired string = "opt-in-not-required"
16+
OptInStatusOptedIn string = "opted-in"
1717
)
1818

1919
func GetRegions() ([]string, error) {
20+
return GetRegionsByOptInStatus([]string{OptInStatusNotRequired, OptInStatusOptedIn})
21+
}
22+
23+
func GetRegionsByOptInStatus(optInStaus []string) ([]string, error) {
2024
cfg, err := config.LoadDefaultConfig(context.TODO())
2125
if err != nil {
2226
return nil, err
@@ -28,7 +32,7 @@ func GetRegions() ([]string, error) {
2832
Filters: []ec2Types.Filter{
2933
{
3034
Name: &optInStatusFilter,
31-
Values: []string{optInStatusNorRequired, optInStatusOptedIn},
35+
Values: optInStaus,
3236
},
3337
}})
3438
if err != nil {

pkg/provider/aws/data/spot.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,14 @@ func SpotInfo(mCtx *mc.Context, args *SpotInfoArgs) (*spot.SpotResults, error) {
172172
if err != nil {
173173
return nil, err
174174
}
175-
175+
// Placement Socres
176+
nOpInRegions, err := GetRegionsByOptInStatus([]string{OptInStatusNotRequired})
177+
if err != nil {
178+
return nil, err
179+
}
176180
placementScores, err := hostingPlaces.RunOnHostingPlaces(regions,
177181
placementScoreArgs{
182+
apiRegion: util.RandomItemFromArray(nOpInRegions),
178183
minPlacementScore: minPlacementScore(*args.SpotTolerance),
179184
instanceTypes: args.InstaceTypes,
180185
capacity: 1,
@@ -368,6 +373,10 @@ func spotPricingAsync(r string, args spotPricingArgs, c chan hostingPlaces.Hosti
368373
}
369374

370375
type placementScoreArgs struct {
376+
// Not all regions offered the API to get
377+
// data about placement score, we need set which one
378+
// will be used
379+
apiRegion string
371380
minPlacementScore int32
372381
instanceTypes []string
373382
capacity int32
@@ -379,15 +388,11 @@ type placementScoreResult struct {
379388
azName string
380389
}
381390

382-
// Every async run will use same region to request data
383-
// as API is not available on all regions.
384-
const PLACEMENT_SCORE_API_REGION = "us-east-2"
385-
386391
// This will get placement scores grouped on map per region
387392
// only scores over tolerance will be added
388393
func placementScoresAsync(r string, args placementScoreArgs, c chan hostingPlaces.HostingPlaceData[[]placementScoreResult]) {
389394
azsByRegion := describeAvailabilityZonesByRegions([]string{r})
390-
cfg, err := getConfig(PLACEMENT_SCORE_API_REGION)
395+
cfg, err := getConfig(args.apiRegion)
391396
if err != nil {
392397
hostingPlaces.SendAsyncErr(c, err)
393398
return

tools/go.mod

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
module github.com/redhat-developer/mapt/tools
22

3-
go 1.24.0
4-
5-
toolchain go1.25.1
3+
go 1.24.6
64

75
require github.com/golangci/golangci-lint/v2 v2.5.0
86

0 commit comments

Comments
 (0)