github.com/m3db/m3@v1.5.0/src/integration/resources/inprocess/inprocess.go (about)

     1  // Copyright (c) 2021  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 inprocess
    22  
    23  import (
    24  	"github.com/m3db/m3/src/integration/resources"
    25  	"github.com/m3db/m3/src/x/errors"
    26  )
    27  
    28  type inprocessM3Resources struct {
    29  	coordinator resources.Coordinator
    30  	dbNodes     resources.Nodes
    31  	aggregators resources.Aggregators
    32  }
    33  
    34  // ResourceOptions are the options for creating new
    35  // resources.M3Resources.
    36  type ResourceOptions struct {
    37  	Coordinator resources.Coordinator
    38  	DBNodes     resources.Nodes
    39  	Aggregators resources.Aggregators
    40  }
    41  
    42  // NewM3Resources returns an implementation of resources.M3Resources
    43  // backed by in-process implementations of the M3 components.
    44  func NewM3Resources(options ResourceOptions) resources.M3Resources {
    45  	return &inprocessM3Resources{
    46  		coordinator: options.Coordinator,
    47  		dbNodes:     options.DBNodes,
    48  		aggregators: options.Aggregators,
    49  	}
    50  }
    51  
    52  func (i *inprocessM3Resources) Start() {
    53  	for _, node := range i.dbNodes {
    54  		node.Start()
    55  	}
    56  	for _, agg := range i.aggregators {
    57  		agg.Start()
    58  	}
    59  	i.coordinator.Start()
    60  }
    61  
    62  func (i *inprocessM3Resources) Cleanup() error {
    63  	err := errors.NewMultiError()
    64  	if i.coordinator != nil {
    65  		err = err.Add(i.coordinator.Close())
    66  	}
    67  
    68  	for _, d := range i.dbNodes {
    69  		err = err.Add(d.Close())
    70  	}
    71  
    72  	for _, a := range i.aggregators {
    73  		err = err.Add(a.Close())
    74  	}
    75  
    76  	return err.FinalError()
    77  }
    78  
    79  func (i *inprocessM3Resources) Nodes() resources.Nodes {
    80  	return i.dbNodes
    81  }
    82  
    83  func (i *inprocessM3Resources) Coordinator() resources.Coordinator {
    84  	return i.coordinator
    85  }
    86  
    87  func (i *inprocessM3Resources) Aggregators() resources.Aggregators {
    88  	return i.aggregators
    89  }