Skip to content

Commit

Permalink
Merge pull request LeanerCloud#79 from cristim/improve_replacement_logic
Browse files Browse the repository at this point in the history
Improve replacement logic
  • Loading branch information
cristim authored Feb 16, 2017
2 parents fe0e326 + b231c2f commit 75561c2
Show file tree
Hide file tree
Showing 11 changed files with 1,203 additions and 252 deletions.
17 changes: 8 additions & 9 deletions core/autoscaling.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ func (a *autoScalingGroup) launchCheapestSpotInstance(azToLaunchIn *string) {
}

logger.Println("Trying to launch spot instance in", *azToLaunchIn,
"\nfirst finding an on-demand instance to use as a template")
"first finding an on-demand instance to use as a template")

baseInstance := a.findOndemandInstanceInAZ(azToLaunchIn)

Expand All @@ -528,7 +528,7 @@ func (a *autoScalingGroup) launchCheapestSpotInstance(azToLaunchIn *string) {

newInstanceType, err := baseInstance.getCheapestCompatibleSpotInstanceType()

if newInstanceType == nil {
if err != nil {
logger.Println("No cheaper compatible instance type was found, "+
"nothing to do here...", err)
return
Expand All @@ -537,20 +537,19 @@ func (a *autoScalingGroup) launchCheapestSpotInstance(azToLaunchIn *string) {
baseOnDemandPrice := baseInstance.price

currentSpotPrice := a.region.
instanceTypeInformation[*newInstanceType].pricing.spot[*azToLaunchIn]
instanceTypeInformation[newInstanceType].pricing.spot[*azToLaunchIn]

logger.Println("Finished searching for best spot instance in ",
*azToLaunchIn,
"\nreplacing an on-demand", *baseInstance.InstanceType,
"instance having the ondemand price", baseOnDemandPrice,
"\nLaunching best compatible instance:", *newInstanceType,
logger.Println("Finished searching for best spot instance in ", *azToLaunchIn)
logger.Println("Replacing an on-demand", *baseInstance.InstanceType,
"instance having the ondemand price", baseOnDemandPrice)
logger.Println("Launching best compatible instance:", newInstanceType,
"with current spot price:", currentSpotPrice)

lc := a.getLaunchConfiguration()

spotLS := lc.convertLaunchConfigurationToSpotSpecification(
baseInstance,
*newInstanceType,
newInstanceType,
*azToLaunchIn)

logger.Println("Bidding for spot instance for ", a.name)
Expand Down
11 changes: 1 addition & 10 deletions core/autoscaling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
)

func TestGetTagValue(t *testing.T) {
Expand Down Expand Up @@ -965,14 +964,6 @@ func TestAlreadyRunningInstanceCount(t *testing.T) {
}
}

type mockEC2Client struct {
ec2iface.EC2API
}

func (m *mockEC2Client) TerminateInstances(*ec2.TerminateInstancesInput) (*ec2.TerminateInstancesOutput, error) {
return nil, nil
}

func TestNeedReplaceOnDemandInstances(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -1052,7 +1043,7 @@ func TestNeedReplaceOnDemandInstances(t *testing.T) {
region: &region{
name: "test-region",
services: connections{
ec2: &mockEC2Client{},
ec2: &mock{},
},
},
},
Expand Down
Loading

0 comments on commit 75561c2

Please sign in to comment.