github.com/m3db/m3@v1.5.0/src/cmd/services/m3dbnode/server/server.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 server is a package for starting servers for M3 components.
    22  package server
    23  
    24  import (
    25  	clusterclient "github.com/m3db/m3/src/cluster/client"
    26  	"github.com/m3db/m3/src/cmd/services/m3dbnode/config"
    27  	"github.com/m3db/m3/src/dbnode/client"
    28  	dbserver "github.com/m3db/m3/src/dbnode/server"
    29  	coordinatorserver "github.com/m3db/m3/src/query/server"
    30  )
    31  
    32  // Options contains options for starting M3 components.
    33  type Options struct {
    34  	// Configuration is the top level configuration that includes both a DB
    35  	// node and a coordinator.
    36  	Configuration config.Configuration
    37  
    38  	// InterruptCh is a programmatic interrupt channel to supply to
    39  	// interrupt and shutdown the server.
    40  	InterruptCh <-chan error
    41  
    42  	// ShutdownCh is an optional channel to supply if interested in receiving
    43  	// a notification that the server has shutdown.
    44  	ShutdownCh chan<- struct{}
    45  }
    46  
    47  // RunComponents runs the appropriate M3 components based on the configuration.
    48  // Kicks off either a single DB node or both a DB node and coordinator.
    49  func RunComponents(opts Options) {
    50  	var (
    51  		cfg         = opts.Configuration
    52  		interruptCh = opts.InterruptCh
    53  		shutdownCh  = opts.ShutdownCh
    54  
    55  		dbClientCh        chan client.Client
    56  		clusterClientCh   chan clusterclient.Client
    57  		coordinatorDoneCh chan struct{}
    58  	)
    59  
    60  	if cfg.DB != nil {
    61  		dbClientCh = make(chan client.Client, 1)
    62  		clusterClientCh = make(chan clusterclient.Client, 1)
    63  	}
    64  
    65  	if cfg.Coordinator != nil {
    66  		coordinatorDoneCh = make(chan struct{}, 1)
    67  		go func() {
    68  			coordinatorserver.Run(coordinatorserver.RunOptions{
    69  				Config:        *cfg.Coordinator,
    70  				DBConfig:      cfg.DB,
    71  				DBClient:      dbClientCh,
    72  				ClusterClient: clusterClientCh,
    73  				InterruptCh:   interruptCh,
    74  				ShutdownCh:    shutdownCh,
    75  			})
    76  			coordinatorDoneCh <- struct{}{}
    77  		}()
    78  	}
    79  
    80  	if cfg.DB != nil {
    81  		dbserver.Run(dbserver.RunOptions{
    82  			Config:          *cfg.DB,
    83  			ClientCh:        dbClientCh,
    84  			ClusterClientCh: clusterClientCh,
    85  			InterruptCh:     interruptCh,
    86  			ShutdownCh:      shutdownCh,
    87  		})
    88  	} else if cfg.Coordinator != nil {
    89  		<-coordinatorDoneCh
    90  	}
    91  }