github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/dbnode/storage/bootstrap/bootstrapper/peers/options.go (about)

     1  // Copyright (c) 2016 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 peers
    22  
    23  import (
    24  	"errors"
    25  	"fmt"
    26  	"math"
    27  	"runtime"
    28  
    29  	"github.com/m3db/m3/src/dbnode/client"
    30  	"github.com/m3db/m3/src/dbnode/persist"
    31  	"github.com/m3db/m3/src/dbnode/persist/fs"
    32  	m3dbruntime "github.com/m3db/m3/src/dbnode/runtime"
    33  	fsbootstrapper "github.com/m3db/m3/src/dbnode/storage/bootstrap/bootstrapper/fs"
    34  	"github.com/m3db/m3/src/dbnode/storage/bootstrap/result"
    35  	"github.com/m3db/m3/src/dbnode/storage/index"
    36  	"github.com/m3db/m3/src/dbnode/storage/index/compaction"
    37  	"github.com/m3db/m3/src/x/context"
    38  	"github.com/m3db/m3/src/x/pool"
    39  )
    40  
    41  var (
    42  	// DefaultShardConcurrency controls how many shards in parallel to stream
    43  	// for in memory data being streamed between peers (most recent block).
    44  	// Update BootstrapPeersConfiguration comment in
    45  	// src/cmd/services/m3dbnode/config package if this is changed.
    46  	DefaultShardConcurrency = runtime.GOMAXPROCS(0)
    47  	// DefaultShardPersistenceConcurrency controls how many shards in parallel to stream
    48  	// for historical data being streamed between peers (historical blocks).
    49  	// Update BootstrapPeersConfiguration comment in
    50  	// src/cmd/services/m3dbnode/config package if this is changed.
    51  	DefaultShardPersistenceConcurrency = int(math.Max(1, float64(runtime.GOMAXPROCS(0))/2))
    52  	defaultPersistenceMaxQueueSize     = 0
    53  	// DefaultShardPersistenceFlushConcurrency controls how many shards in parallel to flush
    54  	// for historical data being streamed between peers (historical blocks).
    55  	// Update BootstrapPeersConfiguration comment in
    56  	// src/cmd/services/m3dbnode/config package if this is changed.
    57  	DefaultShardPersistenceFlushConcurrency = 1
    58  )
    59  
    60  var (
    61  	errAdminClientNotSet           = errors.New("admin client not set")
    62  	errPersistManagerNotSet        = errors.New("persist manager not set")
    63  	errIndexClaimsManagerNotSet    = errors.New("index claims manager not set")
    64  	errCompactorNotSet             = errors.New("compactor not set")
    65  	errIndexOptionsNotSet          = errors.New("index options not set")
    66  	errFilesystemOptionsNotSet     = errors.New("filesystem options not set")
    67  	errRuntimeOptionsManagerNotSet = errors.New("runtime options manager not set")
    68  )
    69  
    70  type options struct {
    71  	resultOpts                       result.Options
    72  	client                           client.AdminClient
    73  	defaultShardConcurrency          int
    74  	shardPersistenceConcurrency      int
    75  	shardPersistenceFlushConcurrency int
    76  	indexSegmentConcurrency          int
    77  	persistenceMaxQueueSize          int
    78  	persistManager                   persist.Manager
    79  	indexClaimsManager               fs.IndexClaimsManager
    80  	runtimeOptionsManager            m3dbruntime.OptionsManager
    81  	contextPool                      context.Pool
    82  	fsOpts                           fs.Options
    83  	indexOpts                        index.Options
    84  	compactor                        *compaction.Compactor
    85  }
    86  
    87  // NewOptions creates new bootstrap options.
    88  func NewOptions() Options {
    89  	return &options{
    90  		resultOpts:                       result.NewOptions(),
    91  		defaultShardConcurrency:          DefaultShardConcurrency,
    92  		shardPersistenceConcurrency:      DefaultShardPersistenceConcurrency,
    93  		shardPersistenceFlushConcurrency: DefaultShardPersistenceFlushConcurrency,
    94  		indexSegmentConcurrency:          fsbootstrapper.DefaultIndexSegmentConcurrency,
    95  		persistenceMaxQueueSize:          defaultPersistenceMaxQueueSize,
    96  		// Use a zero pool, this should be overridden at config time.
    97  		contextPool: context.NewPool(context.NewOptions().
    98  			SetContextPoolOptions(pool.NewObjectPoolOptions().SetSize(0)).
    99  			SetFinalizerPoolOptions(pool.NewObjectPoolOptions().SetSize(0))),
   100  	}
   101  }
   102  
   103  func (o *options) Validate() error {
   104  	if client := o.client; client == nil {
   105  		return errAdminClientNotSet
   106  	}
   107  	if o.persistManager == nil {
   108  		return errPersistManagerNotSet
   109  	}
   110  	if o.indexClaimsManager == nil {
   111  		return errIndexClaimsManagerNotSet
   112  	}
   113  	if o.compactor == nil {
   114  		return errCompactorNotSet
   115  	}
   116  	if o.runtimeOptionsManager == nil {
   117  		return errRuntimeOptionsManagerNotSet
   118  	}
   119  	if o.indexOpts == nil {
   120  		return errIndexOptionsNotSet
   121  	}
   122  	if o.fsOpts == nil {
   123  		return errFilesystemOptionsNotSet
   124  	}
   125  	if n := o.indexSegmentConcurrency; n <= 0 {
   126  		return fmt.Errorf("index segment concurrency not >= 1: actual=%d", n)
   127  	}
   128  	if n := o.shardPersistenceConcurrency; n <= 0 {
   129  		return fmt.Errorf("shard persistence concurrency not >= 1: actual=%d", n)
   130  	}
   131  	if n := o.shardPersistenceFlushConcurrency; n <= 0 {
   132  		return fmt.Errorf("shard persistence flush concurrency not >= 1: actual=%d", n)
   133  	}
   134  	if n := o.defaultShardConcurrency; n <= 0 {
   135  		return fmt.Errorf("default shard concurrency not >= 1: actual=%d", n)
   136  	}
   137  	return nil
   138  }
   139  
   140  func (o *options) SetResultOptions(value result.Options) Options {
   141  	opts := *o
   142  	opts.resultOpts = value
   143  	return &opts
   144  }
   145  
   146  func (o *options) ResultOptions() result.Options {
   147  	return o.resultOpts
   148  }
   149  
   150  func (o *options) SetAdminClient(value client.AdminClient) Options {
   151  	opts := *o
   152  	opts.client = value
   153  	return &opts
   154  }
   155  
   156  func (o *options) AdminClient() client.AdminClient {
   157  	return o.client
   158  }
   159  
   160  func (o *options) SetDefaultShardConcurrency(value int) Options {
   161  	opts := *o
   162  	opts.defaultShardConcurrency = value
   163  	return &opts
   164  }
   165  
   166  func (o *options) DefaultShardConcurrency() int {
   167  	return o.defaultShardConcurrency
   168  }
   169  
   170  func (o *options) SetShardPersistenceConcurrency(value int) Options {
   171  	opts := *o
   172  	opts.shardPersistenceConcurrency = value
   173  	return &opts
   174  }
   175  
   176  func (o *options) ShardPersistenceConcurrency() int {
   177  	return o.shardPersistenceConcurrency
   178  }
   179  
   180  func (o *options) SetShardPersistenceFlushConcurrency(value int) Options {
   181  	opts := *o
   182  	opts.shardPersistenceFlushConcurrency = value
   183  	return &opts
   184  }
   185  
   186  func (o *options) ShardPersistenceFlushConcurrency() int {
   187  	return o.shardPersistenceFlushConcurrency
   188  }
   189  
   190  func (o *options) SetIndexSegmentConcurrency(value int) Options {
   191  	opts := *o
   192  	opts.indexSegmentConcurrency = value
   193  	return &opts
   194  }
   195  
   196  func (o *options) IndexSegmentConcurrency() int {
   197  	return o.indexSegmentConcurrency
   198  }
   199  
   200  func (o *options) SetPersistenceMaxQueueSize(value int) Options {
   201  	opts := *o
   202  	opts.persistenceMaxQueueSize = value
   203  	return &opts
   204  }
   205  
   206  func (o *options) PersistenceMaxQueueSize() int {
   207  	return o.persistenceMaxQueueSize
   208  }
   209  
   210  func (o *options) SetPersistManager(value persist.Manager) Options {
   211  	opts := *o
   212  	opts.persistManager = value
   213  	return &opts
   214  }
   215  
   216  func (o *options) PersistManager() persist.Manager {
   217  	return o.persistManager
   218  }
   219  
   220  func (o *options) SetIndexClaimsManager(value fs.IndexClaimsManager) Options {
   221  	opts := *o
   222  	opts.indexClaimsManager = value
   223  	return &opts
   224  }
   225  
   226  func (o *options) IndexClaimsManager() fs.IndexClaimsManager {
   227  	return o.indexClaimsManager
   228  }
   229  
   230  func (o *options) SetCompactor(value *compaction.Compactor) Options {
   231  	opts := *o
   232  	opts.compactor = value
   233  	return &opts
   234  }
   235  
   236  func (o *options) Compactor() *compaction.Compactor {
   237  	return o.compactor
   238  }
   239  
   240  func (o *options) SetRuntimeOptionsManager(value m3dbruntime.OptionsManager) Options {
   241  	opts := *o
   242  	opts.runtimeOptionsManager = value
   243  	return &opts
   244  }
   245  
   246  func (o *options) RuntimeOptionsManager() m3dbruntime.OptionsManager {
   247  	return o.runtimeOptionsManager
   248  }
   249  
   250  func (o *options) SetContextPool(value context.Pool) Options {
   251  	opts := *o
   252  	opts.contextPool = value
   253  	return &opts
   254  }
   255  
   256  func (o *options) ContextPool() context.Pool {
   257  	return o.contextPool
   258  }
   259  
   260  func (o *options) SetFilesystemOptions(value fs.Options) Options {
   261  	opts := *o
   262  	opts.fsOpts = value
   263  	return &opts
   264  }
   265  
   266  func (o *options) FilesystemOptions() fs.Options {
   267  	return o.fsOpts
   268  }
   269  
   270  func (o *options) SetIndexOptions(value index.Options) Options {
   271  	opts := *o
   272  	opts.indexOpts = value
   273  	return &opts
   274  }
   275  
   276  func (o *options) IndexOptions() index.Options {
   277  	return o.indexOpts
   278  }