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