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

Adding support for capacity buffer #39

Merged
merged 18 commits into from
Jul 30, 2019
Merged
Show file tree
Hide file tree
Changes from 12 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
4 changes: 2 additions & 2 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ steps:
- cd store
- go test -v
environment:
DATABASE_CONFIG: "root:password@tcp(mysql:3306)/test?parseTime=true"
DATABASE_CONFIG: root:password@tcp(mysql:3306)/test?parseTime=true
DATABASE_DRIVER: mysql

- name: build
Expand All @@ -50,9 +50,9 @@ steps:
image: plugins/docker
settings:
auto_tag: true
repo: drone/autoscaler
password:
from_secret: docker_password
repo: drone/autoscaler
username:
from_secret: docker_username
when:
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type (
Config struct {
License string
Interval time.Duration `default:"5m"`
Standby int `envconfig:"DRONE_STANDBY_CAPACITY" default:"0"`

Slack struct {
Webhook string
Expand Down
5 changes: 5 additions & 0 deletions config/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ func TestDefaults(t *testing.T) {
if got, want := conf.Interval, time.Minute*5; got != want {
t.Errorf("Want default DRONE_INTERVAL of %s, got %s", want, got)
}
if got, want := conf.Standby, 0; got != want {
t.Errorf("Want default DRONE_STANDBY_CAPACITY of %d, got %d", want, got)
}
if got, want := conf.Pool.Max, 4; got != want {
t.Errorf("Want default DRONE_POOL_MIN of %d, got %d", want, got)
}
Expand Down Expand Up @@ -54,6 +57,7 @@ func TestDefaults(t *testing.T) {
func TestLoad(t *testing.T) {
environ := map[string]string{
"DRONE_INTERVAL": "1m",
"DRONE_STANDBY_CAPACITY": "3",
"DRONE_SLACK_WEBHOOK": "https://hooks.slack.com/services/XXX/YYY/ZZZ",
"DRONE_SLACK_CREATE": "false",
"DRONE_SLACK_DESTROY": "false",
Expand Down Expand Up @@ -167,6 +171,7 @@ func TestLoad(t *testing.T) {

var jsonConfig = []byte(`{
"Interval": 60000000000,
"Standby": 3,
"Slack": {
"Webhook": "https://hooks.slack.com/services/XXX/YYY/ZZZ",
"Create": false,
Expand Down
2 changes: 1 addition & 1 deletion engine/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,4 @@ func TestSplitVolumeParts(t *testing.T) {
}
}
}
}
}
6 changes: 3 additions & 3 deletions engine/planner.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ import (
"sort"
"time"

"github.com/dchest/uniuri"
jones2026 marked this conversation as resolved.
Show resolved Hide resolved
"github.com/drone/autoscaler"
"github.com/drone/autoscaler/limiter"
"github.com/drone/drone-go/drone"

"github.com/dchest/uniuri"
"github.com/rs/zerolog/log"
)

Expand All @@ -28,6 +27,7 @@ type planner struct {
min int // min number of servers
max int // max number of servers to allocate
cap int // capacity per-server
standby int // standby capacity to reserve
ttu time.Duration // minimum server age

client drone.Client
Expand Down Expand Up @@ -71,7 +71,7 @@ func (p *planner) Plan(ctx context.Context) error {

ctx = logger.WithContext(ctx)

free := max(capacity-running, 0)
free := max(capacity-running-p.standby, 0)
diff := serverDiff(pending, free, p.cap)

// if the server differential to handle the build volume
Expand Down
44 changes: 43 additions & 1 deletion engine/planner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/drone/autoscaler/config"
"github.com/drone/autoscaler/mocks"
"github.com/drone/drone-go/drone"

"github.com/golang/mock/gomock"
)

Expand Down Expand Up @@ -53,6 +52,49 @@ func TestPlan_Noop(t *testing.T) {
}
}

// This test verifies that if the server capacity is
// less than the pending count, and the server capacity is
// < the pool maximum, additional servers are provisioned.
func TestPlan_StandbyCapacity(t *testing.T) {
controller := gomock.NewController(t)
defer controller.Finish()

// x4 capacity
servers := []*autoscaler.Server{
{Name: "server1", Capacity: 2, State: autoscaler.StateRunning},
{Name: "server2", Capacity: 2, State: autoscaler.StateRunning},
}

// x2 running builds
// x1 pending builds
builds := []*drone.Stage{
{Status: drone.StatusRunning},
{Status: drone.StatusRunning},
{Status: drone.StatusPending},
}

store := mocks.NewMockServerStore(controller)
store.EXPECT().List(gomock.Any()).Return(servers, nil)
store.EXPECT().Create(gomock.Any(), gomock.Any()).Return(nil)

client := mocks.NewMockClient(controller)
client.EXPECT().Queue().Return(builds, nil)

p := planner{
cap: 2,
standby: 2,
min: 2,
max: 4,
client: client,
servers: store,
}

err := p.Plan(context.TODO())
if err != nil {
t.Error(err)
}
}

// This test verifies that if the server capacity is
// < than the pending count, and the server capacity is
// >= the pool maximum, no actions are taken.
Expand Down