github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/x/config/pooling.go (about)

     1  // Copyright (c) 2018 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package config
    22  
    23  import "github.com/m3db/m3/src/x/sync"
    24  
    25  const (
    26  	defaultWorkerPoolStaticSize = 4096
    27  	defaultGrowKillProbability  = 0.001
    28  )
    29  
    30  // WorkerPoolPolicy specifies the policy for the worker pool.
    31  type WorkerPoolPolicy struct {
    32  	// Determines if the worker pool automatically grows to capacity.
    33  	GrowOnDemand bool `yaml:"grow"`
    34  
    35  	// Size for static pools, initial size for dynamically growing pools.
    36  	Size int `yaml:"size"`
    37  
    38  	// The number of shards for the pool.
    39  	NumShards int64 `yaml:"shards"`
    40  
    41  	// The probablility that a worker is killed after completing the task.
    42  	KillWorkerProbability float64 `yaml:"killProbability" validate:"min=0.0,max=1.0"`
    43  }
    44  
    45  // Options converts the worker pool policy to options, providing
    46  // the options, as well as the default size for the worker pool.
    47  func (w WorkerPoolPolicy) Options() (sync.PooledWorkerPoolOptions, int) {
    48  	opts := sync.NewPooledWorkerPoolOptions()
    49  	grow := w.GrowOnDemand
    50  	opts = opts.SetGrowOnDemand(grow)
    51  	if w.KillWorkerProbability != 0 {
    52  		opts = opts.SetKillWorkerProbability(w.KillWorkerProbability)
    53  	} else if grow {
    54  		// NB: if using a growing pool, default kill probability is too low, causing
    55  		// the pool to quickly grow out of control. Use a higher default kill probability
    56  		opts = opts.SetKillWorkerProbability(defaultGrowKillProbability)
    57  	}
    58  
    59  	if w.NumShards != 0 {
    60  		opts = opts.SetNumShards(w.NumShards)
    61  	}
    62  
    63  	if w.Size == 0 {
    64  		if grow {
    65  			w.Size = int(opts.NumShards())
    66  		} else {
    67  			w.Size = defaultWorkerPoolStaticSize
    68  		}
    69  	}
    70  
    71  	return opts, w.Size
    72  }