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

Set the max number of driver instances to CPU cores #25

Merged
merged 1 commit into from
Jun 14, 2017
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
21 changes: 20 additions & 1 deletion pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package server
import (
"fmt"
"math"
"os"
"runtime"
"strconv"
"sync/atomic"
"time"

Expand All @@ -14,6 +17,11 @@ var (
// DefaultPoolTimeout is the time a request to the DriverPool can wait
// before getting a driver assigned.
DefaultPoolTimeout = time.Second * 5

// DefaultMaxInstancesPerDriver is the maximum number of instances of
// the same driver which can be launched following the default
// scaling policy (see DefaultScalingPolicy()).
DefaultMaxInstancesPerDriver = runtime.NumCPU()
)

// DriverPool controls a pool of drivers and balances requests among them,
Expand Down Expand Up @@ -235,7 +243,7 @@ type ScalingPolicy interface {
// DefaultScalingPolicy returns a new instance of the default scaling policy.
// Instances returned by this function should not be reused.
func DefaultScalingPolicy() ScalingPolicy {
return MovingAverage(10, MinMax(1, 10, AIMD(1, 0.5)))
return MovingAverage(10, MinMax(1, DefaultMaxInstancesPerDriver, AIMD(1, 0.5)))
}

type movingAverage struct {
Expand Down Expand Up @@ -334,3 +342,14 @@ func (p *aimd) Scale(total, load int) int {

return res
}

func init() {
// Try to read DefaultMaxInstancesPerDriver from the environment variable
defaultMaxInstancesPerDriverEnv := os.Getenv("BBLFSH_MAX_INSTANCES_PER_DRIVER")
if len(defaultMaxInstancesPerDriverEnv) > 0 {
maxInstances, err := strconv.Atoi(defaultMaxInstancesPerDriverEnv)
if err == nil && maxInstances > 0 {
DefaultMaxInstancesPerDriver = maxInstances
}
}
}
3 changes: 2 additions & 1 deletion pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package server

import (
"fmt"
"runtime"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -121,7 +122,7 @@ func TestDriverPoolParallel(t *testing.T) {
}

wg.Wait()
require.Equal(10, dp.cur)
require.Equal(runtime.NumCPU(), dp.cur)

time.Sleep(time.Second * 2)
require.Equal(1, dp.cur)
Expand Down