github.com/m3db/m3@v1.5.0/src/aggregator/tools/deploy/helper_options.go (about)

     1  // Copyright (c) 2017 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 deploy
    22  
    23  import (
    24  	"errors"
    25  	"net/http"
    26  	"time"
    27  
    28  	"github.com/m3db/m3/src/x/instrument"
    29  	"github.com/m3db/m3/src/x/retry"
    30  	"github.com/m3db/m3/src/x/sync"
    31  )
    32  
    33  const (
    34  	defaultSettleDurationBetweenSteps = time.Minute
    35  	defaultHelperWorkerPoolSize       = 16
    36  )
    37  
    38  var (
    39  	errNoPlannerOptions          = errors.New("no planner options")
    40  	errNoManager                 = errors.New("no manager")
    41  	errNoHTTPClient              = errors.New("no http client")
    42  	errNoToPlacementInstanceIDFn = errors.New("no to placement instance id function")
    43  	errNoToAPIEndpointFn         = errors.New("no to api endpoint function")
    44  )
    45  
    46  // ToPlacementInstanceIDFn converts a deployment instance id to the corresponding
    47  // placement instance id.
    48  type ToPlacementInstanceIDFn func(deploymentInstanceID string) (string, error)
    49  
    50  // ToAPIEndpointFn converts a placement instance endpoint to the corresponding
    51  // aggregator instance api endpoint.
    52  type ToAPIEndpointFn func(placementEndpoint string) (string, error)
    53  
    54  // HelperOptions provide a set of options for the deployment helper.
    55  type HelperOptions interface {
    56  	// SetInstrumentOptions sets the instrument options.
    57  	SetInstrumentOptions(value instrument.Options) HelperOptions
    58  
    59  	// InstrumentOptions returns the instrument options.
    60  	InstrumentOptions() instrument.Options
    61  
    62  	// SetPlannerOptions sets the deployment planner options.
    63  	SetPlannerOptions(value PlannerOptions) HelperOptions
    64  
    65  	// PlannerOptions returns the deployment planner options.
    66  	PlannerOptions() PlannerOptions
    67  
    68  	// SetManager sets the deployment manager.
    69  	SetManager(value Manager) HelperOptions
    70  
    71  	// Manager returns the deployment manager.
    72  	Manager() Manager
    73  
    74  	// SetHTTPClient sets the http client.
    75  	SetHTTPClient(value *http.Client) HelperOptions
    76  
    77  	// HTTPClient returns the http client.
    78  	HTTPClient() *http.Client
    79  
    80  	// SetRetryOptions sets the retry options.
    81  	SetRetryOptions(value retry.Options) HelperOptions
    82  
    83  	// RetryOptions returns the retry options.
    84  	RetryOptions() retry.Options
    85  
    86  	// SetWorkerPool sets the worker pool.
    87  	SetWorkerPool(value sync.WorkerPool) HelperOptions
    88  
    89  	// WorkerPool returns the worker pool.
    90  	WorkerPool() sync.WorkerPool
    91  
    92  	// SetToPlacementInstanceIDFn sets the function that converts a deployment
    93  	// instance id to the corresponding placement instance id.
    94  	SetToPlacementInstanceIDFn(value ToPlacementInstanceIDFn) HelperOptions
    95  
    96  	// ToPlacementInstanceIDFn returns the function that converts a deployment
    97  	// instance id to the corresponding placement instance id.
    98  	ToPlacementInstanceIDFn() ToPlacementInstanceIDFn
    99  
   100  	// SetToAPIEndpointFn sets the function that converts a placement
   101  	// instance endpoint to the corresponding aggregator instance api endpoint.
   102  	SetToAPIEndpointFn(value ToAPIEndpointFn) HelperOptions
   103  
   104  	// ToAPIEndpointFn returns the function that converts a placement
   105  	// instance endpoint to the corresponding aggregator instance api endpoint.
   106  	ToAPIEndpointFn() ToAPIEndpointFn
   107  
   108  	// SetSettleDurationBetweenSteps sets the settlement duration between consecutive steps.
   109  	SetSettleDurationBetweenSteps(value time.Duration) HelperOptions
   110  
   111  	// SettleDurationBetweenSteps returns the settlement duration between consecutive steps.
   112  	SettleDurationBetweenSteps() time.Duration
   113  
   114  	// Validate validates the options.
   115  	Validate() error
   116  }
   117  
   118  type helperOptions struct {
   119  	instrumentOpts          instrument.Options
   120  	plannerOpts             PlannerOptions
   121  	manager                 Manager
   122  	httpClient              *http.Client
   123  	retryOpts               retry.Options
   124  	workerPool              sync.WorkerPool
   125  	toPlacementInstanceIDFn ToPlacementInstanceIDFn
   126  	toAPIEndpointFn         ToAPIEndpointFn
   127  	settleDuration          time.Duration
   128  }
   129  
   130  // NewHelperOptions create a set of deployment helper options.
   131  func NewHelperOptions() HelperOptions {
   132  	workers := sync.NewWorkerPool(defaultHelperWorkerPoolSize)
   133  	workers.Init()
   134  	return &helperOptions{
   135  		instrumentOpts: instrument.NewOptions(),
   136  		retryOpts:      retry.NewOptions(),
   137  		workerPool:     workers,
   138  		settleDuration: defaultSettleDurationBetweenSteps,
   139  	}
   140  }
   141  
   142  func (o *helperOptions) SetInstrumentOptions(value instrument.Options) HelperOptions {
   143  	opts := *o
   144  	opts.instrumentOpts = value
   145  	return &opts
   146  }
   147  
   148  func (o *helperOptions) InstrumentOptions() instrument.Options {
   149  	return o.instrumentOpts
   150  }
   151  
   152  func (o *helperOptions) SetPlannerOptions(value PlannerOptions) HelperOptions {
   153  	opts := *o
   154  	opts.plannerOpts = value
   155  	return &opts
   156  }
   157  
   158  func (o *helperOptions) PlannerOptions() PlannerOptions {
   159  	return o.plannerOpts
   160  }
   161  
   162  func (o *helperOptions) SetManager(value Manager) HelperOptions {
   163  	opts := *o
   164  	opts.manager = value
   165  	return &opts
   166  }
   167  
   168  func (o *helperOptions) Manager() Manager {
   169  	return o.manager
   170  }
   171  
   172  func (o *helperOptions) SetHTTPClient(value *http.Client) HelperOptions {
   173  	opts := *o
   174  	opts.httpClient = value
   175  	return &opts
   176  }
   177  
   178  func (o *helperOptions) HTTPClient() *http.Client {
   179  	return o.httpClient
   180  }
   181  
   182  func (o *helperOptions) SetRetryOptions(value retry.Options) HelperOptions {
   183  	opts := *o
   184  	opts.retryOpts = value
   185  	return &opts
   186  }
   187  
   188  func (o *helperOptions) RetryOptions() retry.Options {
   189  	return o.retryOpts
   190  }
   191  
   192  func (o *helperOptions) SetWorkerPool(value sync.WorkerPool) HelperOptions {
   193  	opts := *o
   194  	opts.workerPool = value
   195  	return &opts
   196  }
   197  
   198  func (o *helperOptions) WorkerPool() sync.WorkerPool {
   199  	return o.workerPool
   200  }
   201  
   202  func (o *helperOptions) SetToPlacementInstanceIDFn(value ToPlacementInstanceIDFn) HelperOptions {
   203  	opts := *o
   204  	opts.toPlacementInstanceIDFn = value
   205  	return &opts
   206  }
   207  
   208  func (o *helperOptions) ToPlacementInstanceIDFn() ToPlacementInstanceIDFn {
   209  	return o.toPlacementInstanceIDFn
   210  }
   211  
   212  func (o *helperOptions) SetToAPIEndpointFn(value ToAPIEndpointFn) HelperOptions {
   213  	opts := *o
   214  	opts.toAPIEndpointFn = value
   215  	return &opts
   216  }
   217  
   218  func (o *helperOptions) ToAPIEndpointFn() ToAPIEndpointFn {
   219  	return o.toAPIEndpointFn
   220  }
   221  
   222  func (o *helperOptions) SetSettleDurationBetweenSteps(value time.Duration) HelperOptions {
   223  	opts := *o
   224  	opts.settleDuration = value
   225  	return &opts
   226  }
   227  
   228  func (o *helperOptions) SettleDurationBetweenSteps() time.Duration {
   229  	return o.settleDuration
   230  }
   231  
   232  func (o *helperOptions) Validate() error {
   233  	if o.plannerOpts == nil {
   234  		return errNoPlannerOptions
   235  	}
   236  	if o.manager == nil {
   237  		return errNoManager
   238  	}
   239  	if o.httpClient == nil {
   240  		return errNoHTTPClient
   241  	}
   242  	if o.toPlacementInstanceIDFn == nil {
   243  		return errNoToPlacementInstanceIDFn
   244  	}
   245  	if o.toAPIEndpointFn == nil {
   246  		return errNoToAPIEndpointFn
   247  	}
   248  	return nil
   249  }