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 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
5 changes: 3 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import (
type (
// Config stores the configuration settings.
Config struct {
License string
Interval time.Duration `default:"5m"`
License string
Interval time.Duration `default:"5m"`
CapacityBuffer int `default:"0" split_words:"true"`

Slack struct {
Webhook string
Expand Down
7 changes: 6 additions & 1 deletion 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.CapacityBuffer, 0; got != want {
t.Errorf("Want default DRONE_CAPACITY_BUFFER 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 @@ -60,6 +63,7 @@ func TestLoad(t *testing.T) {
"DRONE_LOGS_DEBUG": "true",
"DRONE_LOGS_COLOR": "true",
"DRONE_LOGS_PRETTY": "true",
"DRONE_CAPACITY_BUFFER": "3",
"DRONE_POOL_MIN_AGE": "1h",
"DRONE_POOL_MIN": "1",
"DRONE_POOL_MAX": "5",
Expand Down Expand Up @@ -167,6 +171,7 @@ func TestLoad(t *testing.T) {

var jsonConfig = []byte(`{
"Interval": 60000000000,
"CapacityBuffer": 3,
"Slack": {
"Webhook": "https://hooks.slack.com/services/XXX/YYY/ZZZ",
"Create": false,
Expand All @@ -181,7 +186,7 @@ var jsonConfig = []byte(`{
"Pool": {
"Min": 1,
"Max": 5,
"MinAge": 3600000000000
"MinAge": 3600000000000
},
"Server": {
"Host": "drone.company.com",
Expand Down
1 change: 1 addition & 0 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func New(
arch: config.Agent.Arch,
version: config.Agent.Version,
kernel: config.Agent.Kernel,
buffer: config.CapacityBuffer,
ttu: config.Pool.MinAge,
min: config.Pool.Min,
max: config.Pool.Max,
Expand Down
4 changes: 3 additions & 1 deletion engine/planner.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type planner struct {
min int // min number of servers
max int // max number of servers to allocate
cap int // capacity per-server
buffer int // buffer capacity to have warm and ready
ttu time.Duration // minimum server age
labels map[string]string

Expand Down Expand Up @@ -59,6 +60,7 @@ func (p *planner) Plan(ctx context.Context) error {
logger.Debug().
Int("min-pool", p.min).
Int("max-pool", p.max).
Int("server-buffer", p.buffer).
Int("server-capacity", capacity).
Int("server-count", servers).
Int("pending-builds", pending).
Expand All @@ -72,7 +74,7 @@ func (p *planner) Plan(ctx context.Context) error {

ctx = logger.WithContext(ctx)

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

// if the server differential to handle the build volume
Expand Down
126 changes: 126 additions & 0 deletions engine/planner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,132 @@ func TestPlan_Noop(t *testing.T) {
}
}

// This test verifies that if that no servers are
// destroyed if there is excess capacity and the
// the server count <= the min pool size.
func TestPlan_MinBufferCapacity(t *testing.T) {
controller := gomock.NewController(t)
defer controller.Finish()

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

// x0 running builds
// x0 pending builds
builds := []*drone.Stage{}

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

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

p := planner{
cap: 2,
buffer: 1,
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 minus buffer is
// less than the pending count, and the server capacity is
// >= the pool maximum, no actions are taken.
func TestPlan_MaxBufferCapacity(t *testing.T) {
controller := gomock.NewController(t)
defer controller.Finish()

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

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

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

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

p := planner{
cap: 1,
buffer: 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 minus buffer is
// less than the pending count, and the server capacity is
// < the pool maximum, additional servers are provisioned.
func TestPlan_MoreBufferCapacity(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,
buffer: 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