github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/cmd/services/m3comparator/main/main.go (about)

     1  // Copyright (c) 2019 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 main
    22  
    23  import (
    24  	"net"
    25  	"net/http"
    26  	"time"
    27  
    28  	"github.com/m3db/m3/src/cmd/services/m3comparator/main/parser"
    29  	"github.com/m3db/m3/src/dbnode/encoding"
    30  	"github.com/m3db/m3/src/dbnode/encoding/m3tsz"
    31  	"github.com/m3db/m3/src/query/models"
    32  	"github.com/m3db/m3/src/query/pools"
    33  	"github.com/m3db/m3/src/query/remote"
    34  	"github.com/m3db/m3/src/x/instrument"
    35  	"github.com/m3db/m3/src/x/pool"
    36  
    37  	"go.uber.org/zap"
    38  )
    39  
    40  var (
    41  	iterPools = pools.BuildIteratorPools(encoding.NewOptions(),
    42  		pools.BuildIteratorPoolsOptions{})
    43  	poolWrapper = pools.NewPoolsWrapper(iterPools)
    44  
    45  	iOpts      = instrument.NewOptions()
    46  	logger     = iOpts.Logger()
    47  	tagOptions = models.NewTagOptions()
    48  
    49  	encoderPoolOpts = pool.NewObjectPoolOptions()
    50  	encoderPool     = encoding.NewEncoderPool(encoderPoolOpts)
    51  
    52  	checkedBytesPool pool.CheckedBytesPool
    53  	encodingOpts     encoding.Options
    54  )
    55  
    56  func init() {
    57  	buckets := []pool.Bucket{{Capacity: 10, Count: 10}}
    58  	newBackingBytesPool := func(s []pool.Bucket) pool.BytesPool {
    59  		return pool.NewBytesPool(s, nil)
    60  	}
    61  
    62  	checkedBytesPool = pool.NewCheckedBytesPool(buckets, nil, newBackingBytesPool)
    63  	checkedBytesPool.Init()
    64  
    65  	encodingOpts = encoding.NewOptions().
    66  		SetEncoderPool(encoderPool).
    67  		SetBytesPool(checkedBytesPool).
    68  		SetMetrics(encoding.NewMetrics(iOpts.MetricsScope()))
    69  
    70  	encoderPool.Init(func() encoding.Encoder {
    71  		return m3tsz.NewEncoder(0, nil, true, encodingOpts)
    72  	})
    73  }
    74  
    75  func main() {
    76  	opts := parser.Options{
    77  		EncoderPool:       encoderPool,
    78  		IteratorPools:     iterPools,
    79  		TagOptions:        tagOptions,
    80  		InstrumentOptions: iOpts,
    81  	}
    82  
    83  	seriesLoader := newHTTPSeriesLoadHandler(opts)
    84  
    85  	querier, err := newQuerier(
    86  		opts,
    87  		seriesLoader,
    88  		time.Hour*12,
    89  		time.Second*15,
    90  		5,
    91  	)
    92  	if err != nil {
    93  		logger.Error("could not create querier", zap.Error(err))
    94  		return
    95  	}
    96  
    97  	server := remote.NewGRPCServer(
    98  		querier,
    99  		models.QueryContextOptions{},
   100  		poolWrapper,
   101  		iOpts,
   102  	)
   103  
   104  	addr := "0.0.0.0:9000"
   105  	logger.Info("starting remote server", zap.String("address", addr))
   106  	listener, err := net.Listen("tcp", addr)
   107  	if err != nil {
   108  		logger.Error("listener error", zap.Error(err))
   109  		return
   110  	}
   111  
   112  	loaderAddr := "0.0.0.0:9001"
   113  	go func() {
   114  		if err := http.ListenAndServe(loaderAddr, seriesLoader); err != nil {
   115  			logger.Error("series load handler failed", zap.Error(err))
   116  		}
   117  	}()
   118  
   119  	if err := server.Serve(listener); err != nil {
   120  		logger.Error("serve error", zap.Error(err))
   121  	}
   122  }