github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/cmd/services/m3aggregator/serve/serve.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 serve
    22  
    23  import (
    24  	"fmt"
    25  	"time"
    26  
    27  	"github.com/m3db/m3/src/aggregator/aggregator"
    28  	httpserver "github.com/m3db/m3/src/aggregator/server/http"
    29  	m3msgserver "github.com/m3db/m3/src/aggregator/server/m3msg"
    30  	rawtcpserver "github.com/m3db/m3/src/aggregator/server/rawtcp"
    31  	xdebug "github.com/m3db/m3/src/x/debug"
    32  
    33  	"go.uber.org/zap"
    34  )
    35  
    36  // Serve starts serving RPC traffic.
    37  func Serve(
    38  	aggregator aggregator.Aggregator,
    39  	doneCh chan struct{},
    40  	opts Options,
    41  ) error {
    42  	var (
    43  		iOpts       = opts.InstrumentOpts()
    44  		log         = iOpts.Logger()
    45  		closeLogger = log.With(zap.String("closing", "aggregator_server"))
    46  	)
    47  
    48  	defer func() {
    49  		start := time.Now()
    50  		closeLogger.Info("closing aggregator")
    51  		err := aggregator.Close()
    52  		fields := []zap.Field{zap.String("took", time.Since(start).String())}
    53  		if err != nil {
    54  			closeLogger.Warn("closed aggregator with error", append(fields, zap.Error(err))...)
    55  		} else {
    56  			closeLogger.Info("closed aggregator", fields...)
    57  		}
    58  	}()
    59  
    60  	if m3msgAddr := opts.M3MsgAddr(); m3msgAddr != "" {
    61  		serverOpts := opts.M3MsgServerOpts()
    62  		m3msgServer, err := m3msgserver.NewServer(m3msgAddr, aggregator, serverOpts)
    63  		if err != nil {
    64  			return fmt.Errorf("could not create m3msg server: addr=%s, err=%v", m3msgAddr, err)
    65  		}
    66  		if err := m3msgServer.ListenAndServe(); err != nil {
    67  			return fmt.Errorf("could not start m3msg server at: addr=%s, err=%v", m3msgAddr, err)
    68  		}
    69  
    70  		defer func() {
    71  			start := time.Now()
    72  			closeLogger.Info("closing m3msg server")
    73  			m3msgServer.Close()
    74  			closeLogger.Info("m3msg server closed", zap.String("took", time.Since(start).String()))
    75  		}()
    76  
    77  		log.Info("m3msg server listening", zap.String("addr", m3msgAddr))
    78  	}
    79  
    80  	if rawTCPAddr := opts.RawTCPAddr(); rawTCPAddr != "" {
    81  		serverOpts := opts.RawTCPServerOpts()
    82  		rawTCPServer := rawtcpserver.NewServer(rawTCPAddr, aggregator, serverOpts)
    83  		if err := rawTCPServer.ListenAndServe(); err != nil {
    84  			return fmt.Errorf("could not start raw TCP server at: addr=%s, err=%v", rawTCPAddr, err)
    85  		}
    86  
    87  		defer func() {
    88  			start := time.Now()
    89  			closeLogger.Info("closing raw TCPServer")
    90  			rawTCPServer.Close()
    91  			closeLogger.Info("closed raw TCPServer", zap.String("took", time.Since(start).String()))
    92  		}()
    93  
    94  		log.Info("raw TCP server listening", zap.String("addr", rawTCPAddr))
    95  	}
    96  
    97  	if httpAddr := opts.HTTPAddr(); httpAddr != "" {
    98  		serverOpts := opts.HTTPServerOpts()
    99  		xdebug.RegisterPProfHandlers(serverOpts.Mux())
   100  		httpServer := httpserver.NewServer(httpAddr, aggregator, serverOpts, iOpts)
   101  		if err := httpServer.ListenAndServe(); err != nil {
   102  			return fmt.Errorf("could not start http server at: addr=%s, err=%v", httpAddr, err)
   103  		}
   104  
   105  		defer func() {
   106  			start := time.Now()
   107  			closeLogger.Info("closing http server")
   108  			httpServer.Close()
   109  			closeLogger.Info("closed http server", zap.String("took", time.Since(start).String()))
   110  		}()
   111  
   112  		log.Info("http server listening", zap.String("addr", httpAddr))
   113  	}
   114  
   115  	// Wait for exit signal.
   116  	<-doneCh
   117  	closeLogger.Info("server signaled on doneCh")
   118  
   119  	return nil
   120  }