Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix pricing sync issue #150

Merged
merged 2 commits into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion pkg/ec2pricing/odpricing.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"path/filepath"
"strconv"
"strings"
"sync"
"time"

"github.com/aws/aws-sdk-go/aws"
Expand All @@ -44,6 +45,7 @@ type OnDemandPricing struct {
DirectoryPath string
cache *cache.Cache
pricingClient pricingiface.PricingAPI
sync.RWMutex
}

func LoadODCacheOrNew(pricingClient pricingiface.PricingAPI, region string, fullRefreshTTL time.Duration, directoryPath string) *OnDemandPricing {
Expand Down Expand Up @@ -91,7 +93,9 @@ func loadODCacheFrom(itemTTL time.Duration, region string, expandedDirPath strin
if err := json.Unmarshal(cacheBytes, odCache); err != nil {
return nil, err
}
return cache.NewFrom(itemTTL, itemTTL, *odCache), nil
c := cache.NewFrom(itemTTL, itemTTL, *odCache)
c.DeleteExpired()
return c, nil
}

func getODCacheFilePath(region string, directoryPath string) string {
Expand All @@ -111,6 +115,8 @@ func odCacheRefreshJob(odPricing *OnDemandPricing) {
}

func (c *OnDemandPricing) Refresh() error {
c.Lock()
defer c.Unlock()
odInstanceTypeCosts, err := c.fetchOnDemandPricing("")
if err != nil {
return fmt.Errorf("there was a problem refreshing the on-demand instance type pricing cache: %v", err)
Expand All @@ -128,6 +134,8 @@ func (c *OnDemandPricing) Get(instanceType string) (float64, error) {
if cost, ok := c.cache.Get(instanceType); ok {
return cost.(float64), nil
}
c.RLock()
defer c.RUnlock()
costs, err := c.fetchOnDemandPricing(instanceType)
if err != nil {
return 0, fmt.Errorf("there was a problem fetching on-demand instance type pricing for %s: %v", instanceType, err)
Expand All @@ -154,6 +162,8 @@ func (c *OnDemandPricing) Save() error {
}

func (c *OnDemandPricing) Clear() error {
c.Lock()
defer c.Unlock()
c.cache.Flush()
return os.Remove(getODCacheFilePath(c.Region, c.DirectoryPath))
}
Expand Down
12 changes: 11 additions & 1 deletion pkg/ec2pricing/spotpricing.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"path/filepath"
"sort"
"strconv"
"sync"
"time"

"github.com/aws/aws-sdk-go/aws"
Expand All @@ -43,6 +44,7 @@ type SpotPricing struct {
DirectoryPath string
cache *cache.Cache
ec2Client ec2iface.EC2API
sync.RWMutex
}

type spotPricingEntry struct {
Expand Down Expand Up @@ -98,7 +100,9 @@ func loadSpotCacheFrom(itemTTL time.Duration, region string, expandedDirPath str
if err := decoder.Decode(spotTimeSeries); err != nil {
return nil, err
}
return cache.NewFrom(itemTTL, itemTTL, *spotTimeSeries), nil
c := cache.NewFrom(itemTTL, itemTTL, *spotTimeSeries)
c.DeleteExpired()
return c, nil
}

func getSpotCacheFilePath(region string, directoryPath string) string {
Expand All @@ -118,6 +122,8 @@ func spotCacheRefreshJob(spotPricing *SpotPricing, days int) {
}

func (c *SpotPricing) Refresh(days int) error {
c.Lock()
defer c.Unlock()
spotInstanceTypeCosts, err := c.fetchSpotPricingTimeSeries("", days)
if err != nil {
return fmt.Errorf("there was a problem refreshing the spot instance type pricing cache: %v", err)
Expand All @@ -139,6 +145,8 @@ func (c *SpotPricing) Get(instanceType string, zone string, days int) (float64,
}
}
if !ok {
c.RLock()
defer c.RUnlock()
zonalSpotPricing, err := c.fetchSpotPricingTimeSeries(instanceType, days)
if err != nil {
return -1, fmt.Errorf("there was a problem fetching spot instance type pricing for %s: %v", instanceType, err)
Expand Down Expand Up @@ -224,6 +232,8 @@ func (c *SpotPricing) Save() error {
}

func (c *SpotPricing) Clear() error {
c.Lock()
defer c.Unlock()
c.cache.Flush()
return os.Remove(getSpotCacheFilePath(c.Region, c.DirectoryPath))
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/selector/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func (itf Selector) prepareFilter(filters Filters, instanceTypeInfo instancetype
if itf.EC2Pricing.OnDemandCacheCount() > 0 {
price, err := itf.EC2Pricing.GetOnDemandInstanceTypeCost(instanceTypeName)
if err != nil {
log.Printf("Could not retrieve instantaneous hourly on-demand price for instance type %s\n", instanceTypeName)
log.Printf("Could not retrieve instantaneous hourly on-demand price for instance type %s - %s\n", instanceTypeName, err)
} else {
instanceTypeHourlyPriceOnDemand = &price
instanceTypeInfo.OndemandPricePerHour = instanceTypeHourlyPriceOnDemand
Expand Down