<commit_before>package worker

import (
	"errors"
	"fmt"
	"math/rand"
	"sort"
	"strings"

	"code.cloudfoundry.org/lager"
	"github.com/concourse/concourse/atc/db"
	"github.com/concourse/concourse/atc/runtime"
)

type PlacementOptions struct {
	Strategies                   []string `long:"container-placement-strategy" default:"volume-locality" choice:"volume-locality" choice:"random" choice:"fewest-build-containers" choice:"limit-active-tasks" choice:"limit-active-containers" choice:"limit-active-volumes" description:"Method by which a worker is selected during container placement. If multiple methods are specified, they will be applied in order. Random strategy should only be used alone."`
	MaxActiveTasksPerWorker      int      `long:"max-active-tasks-per-worker" default:"0" description:"Maximum allowed number of active build tasks per worker. Has effect only when used with limit-active-tasks placement strategy. 0 means no limit."`
	MaxActiveContainersPerWorker int      `long:"max-active-containers-per-worker" default:"0" description:"Maximum allowed number of active containers per worker. Has effect only when used with limit-active-containers placement strategy. 0 means no limit."`
	MaxActiveVolumesPerWorker    int      `long:"max-active-volumes-per-worker" default:"0" description:"Maximum allowed number of active volumes per worker. Has effect only when used with limit-active-volumes placement strategy. 0 means no limit."`
}

var (
	ErrTooManyContainers = errors.New("worker has too many containers")
	ErrTooManyVolumes    = errors.New("worker has too many volumes")
)

func NewPlacementStrategy(options PlacementOptions) (PlacementStrategy, error) {
	var strategy PlacementStrategy
	for _, s := range options.Strategies {
		switch strings.TrimSpace(s) {
		case "random":
			// Add nothing - since worker order is already randomized
			// initially, a `random` strategy appearing anywhere in the chain
			// of strategies has no effect.
		case "volume-locality":
			strategy = append(strategy, volumeLocalityStrategy{})
		case "fewest-build-containers":
			strategy = append(strategy, fewestBuildContainersStrategy{})
		case "limit-active-tasks":
			if options.MaxActiveTasksPerWorker < 0 {
				return nil, errors.New("max-active-tasks-per-worker must be greater or equal than 0")
			}
			strategy = append(strategy, limitActiveTasksStrategy{MaxTasks: options.MaxActiveTasksPerWorker})<commit_msg>change default placement strategy to volume-locality+fewest-build-containers<commit_after>package worker

import (
	"errors"
	"fmt"
	"math/rand"
	"sort"
	"strings"

	"code.cloudfoundry.org/lager"
	"github.com/concourse/concourse/atc/db"
	"github.com/concourse/concourse/atc/runtime"
)

type PlacementOptions struct {
	Strategies                   []string `long:"container-placement-strategy" default:"volume-locality" default:"fewest-build-containers" choice:"volume-locality" choice:"random" choice:"fewest-build-containers" choice:"limit-active-tasks" choice:"limit-active-containers" choice:"limit-active-volumes" description:"Method by which a worker is selected during container placement. If multiple methods are specified, they will be applied in order. Random strategy should only be used alone."`
	MaxActiveTasksPerWorker      int      `long:"max-active-tasks-per-worker" default:"0" description:"Maximum allowed number of active build tasks per worker. Has effect only when used with limit-active-tasks placement strategy. 0 means no limit."`
	MaxActiveContainersPerWorker int      `long:"max-active-containers-per-worker" default:"0" description:"Maximum allowed number of active containers per worker. Has effect only when used with limit-active-containers placement strategy. 0 means no limit."`
	MaxActiveVolumesPerWorker    int      `long:"max-active-volumes-per-worker" default:"0" description:"Maximum allowed number of active volumes per worker. Has effect only when used with limit-active-volumes placement strategy. 0 means no limit."`
}

var (
	ErrTooManyContainers = errors.New("worker has too many containers")
	ErrTooManyVolumes    = errors.New("worker has too many volumes")
)

func NewPlacementStrategy(options PlacementOptions) (PlacementStrategy, error) {
	var strategy PlacementStrategy
	for _, s := range options.Strategies {
		switch strings.TrimSpace(s) {
		case "random":
			// Add nothing - since worker order is already randomized
			// initially, a `random` strategy appearing anywhere in the chain
			// of strategies has no effect.
		case "volume-locality":
			strategy = append(strategy, volumeLocalityStrategy{})
		case "fewest-build-containers":
			strategy = append(strategy, fewestBuildContainersStrategy{})
		case "limit-active-tasks":
			if options.MaxActiveTasksPerWorker < 0 {
				return nil, errors.New("max-active-tasks-per-worker must be greater or equal than 0")
			}
			strategy = append(strategy, limitActiveTasksStrategy{MaxTasks: options.MaxActiveTasksPerWorker})
{'fraction_non_alphanumeric': 0.09694393847399312, 'fraction_numerical': 0.0032382108884841127, 'mean_word_length': 4.897374701670644, 'pattern_counts': {'":': 8, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1036811', 'n_tokens_mistral': 1421, 'n_tokens_neox': 1342, 'n_words': 421}