github.com/matrixorigin/matrixone@v0.7.0/pkg/cnservice/types.go (about)

     1  // Copyright 2021 - 2022 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package cnservice
    16  
    17  import (
    18  	"context"
    19  	"runtime"
    20  	"sync"
    21  	"time"
    22  
    23  	"github.com/matrixorigin/matrixone/pkg/common/morpc"
    24  	"github.com/matrixorigin/matrixone/pkg/common/stopper"
    25  	"github.com/matrixorigin/matrixone/pkg/config"
    26  	"github.com/matrixorigin/matrixone/pkg/fileservice"
    27  	"github.com/matrixorigin/matrixone/pkg/frontend"
    28  	"github.com/matrixorigin/matrixone/pkg/logservice"
    29  	"github.com/matrixorigin/matrixone/pkg/pb/metadata"
    30  	"github.com/matrixorigin/matrixone/pkg/taskservice"
    31  	"github.com/matrixorigin/matrixone/pkg/txn/client"
    32  	"github.com/matrixorigin/matrixone/pkg/txn/rpc"
    33  	"github.com/matrixorigin/matrixone/pkg/util/toml"
    34  	"github.com/matrixorigin/matrixone/pkg/vm/engine"
    35  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/options"
    36  	"go.uber.org/zap"
    37  )
    38  
    39  var (
    40  	defaultListenAddress = "127.0.0.1:6002"
    41  )
    42  
    43  type Service interface {
    44  	Start() error
    45  	Close() error
    46  
    47  	GetTaskRunner() taskservice.TaskRunner
    48  	GetTaskService() (taskservice.TaskService, bool)
    49  	WaitSystemInitCompleted(ctx context.Context) error
    50  }
    51  
    52  type EngineType string
    53  
    54  const (
    55  	EngineTAE                  EngineType = "tae"
    56  	EngineDistributedTAE       EngineType = "distributed-tae"
    57  	EngineMemory               EngineType = "memory"
    58  	EngineNonDistributedMemory EngineType = "non-distributed-memory"
    59  	// ReservedTasks equals how many task must run background.
    60  	// 1 for metric StorageUsage
    61  	// 1 for trace ETLMerge
    62  	ReservedTasks = 2
    63  )
    64  
    65  // Config cn service
    66  type Config struct {
    67  	// UUID cn store uuid
    68  	UUID string `toml:"uuid"`
    69  	// Role cn node role, [AP|TP]
    70  	Role string `toml:"role"`
    71  
    72  	// ListenAddress listening address for receiving external requests
    73  	ListenAddress string `toml:"listen-address"`
    74  	// ServiceAddress service address for communication, if this address is not set, use
    75  	// ListenAddress as the communication address.
    76  	ServiceAddress string `toml:"service-address"`
    77  	// SQLAddress service address for receiving external sql clientß
    78  	SQLAddress string `toml:"sql-address"`
    79  	// FileService file service configuration
    80  
    81  	Engine struct {
    82  		Type                EngineType           `toml:"type"`
    83  		Logstore            options.LogstoreType `toml:"logstore"`
    84  		FlushInterval       toml.Duration        `toml:"flush-interval"`
    85  		MinCount            int64                `toml:"min-count"`
    86  		ScanInterval        toml.Duration        `toml:"scan-interval"`
    87  		IncrementalInterval toml.Duration        `toml:"incremental-interval"`
    88  		GlobalMinCount      int64                `toml:"global-min-count"`
    89  	}
    90  
    91  	// parameters for cn-server related buffer.
    92  	ReadBufferSize  int
    93  	WriteBufferSize int
    94  
    95  	// Pipeline configuration
    96  	Pipeline struct {
    97  		// HostSize is the memory limit
    98  		HostSize int64 `toml:"host-size"`
    99  		// GuestSize is the memory limit for one query
   100  		GuestSize int64 `toml:"guest-size"`
   101  		// OperatorSize is the memory limit for one operator
   102  		OperatorSize int64 `toml:"operator-size"`
   103  		// BatchRows is the batch rows limit for one batch
   104  		BatchRows int64 `toml:"batch-rows"`
   105  		// BatchSize is the memory limit for one batch
   106  		BatchSize int64 `toml:"batch-size"`
   107  	}
   108  
   109  	// Frontend parameters for the frontend
   110  	Frontend config.FrontendParameters `toml:"frontend"`
   111  
   112  	// HAKeeper configuration
   113  	HAKeeper struct {
   114  		// HeatbeatInterval heartbeat interval to send message to hakeeper. Default is 1s
   115  		HeatbeatInterval toml.Duration `toml:"hakeeper-heartbeat-interval"`
   116  		// HeatbeatTimeout heartbeat request timeout. Default is 500ms
   117  		HeatbeatTimeout toml.Duration `toml:"hakeeper-heartbeat-timeout"`
   118  		// DiscoveryTimeout discovery HAKeeper service timeout. Default is 30s
   119  		DiscoveryTimeout toml.Duration `toml:"hakeeper-discovery-timeout"`
   120  		// ClientConfig hakeeper client configuration
   121  		ClientConfig logservice.HAKeeperClientConfig
   122  	}
   123  
   124  	// TaskRunner configuration
   125  	TaskRunner struct {
   126  		QueryLimit        int           `toml:"task-query-limit"`
   127  		Parallelism       int           `toml:"task-parallelism"`
   128  		MaxWaitTasks      int           `toml:"task-max-wait-tasks"`
   129  		FetchInterval     toml.Duration `toml:"task-fetch-interval"`
   130  		FetchTimeout      toml.Duration `toml:"task-fetch-timeout"`
   131  		RetryInterval     toml.Duration `toml:"task-retry-interval"`
   132  		HeartbeatInterval toml.Duration `toml:"task-heartbeat-interval"`
   133  	}
   134  
   135  	// RPC rpc config used to build txn sender
   136  	RPC rpc.Config `toml:"rpc"`
   137  }
   138  
   139  func (c *Config) Validate() error {
   140  	if c.UUID == "" {
   141  		panic("missing cn store UUID")
   142  	}
   143  	if c.ListenAddress == "" {
   144  		c.ListenAddress = defaultListenAddress
   145  	}
   146  	if c.ServiceAddress == "" {
   147  		c.ServiceAddress = c.ListenAddress
   148  	}
   149  	if c.Role == "" {
   150  		c.Role = metadata.CNRole_TP.String()
   151  	}
   152  	if c.HAKeeper.DiscoveryTimeout.Duration == 0 {
   153  		c.HAKeeper.DiscoveryTimeout.Duration = time.Second * 30
   154  	}
   155  	if c.HAKeeper.HeatbeatInterval.Duration == 0 {
   156  		c.HAKeeper.HeatbeatInterval.Duration = time.Second
   157  	}
   158  	if c.HAKeeper.HeatbeatTimeout.Duration == 0 {
   159  		c.HAKeeper.HeatbeatTimeout.Duration = time.Second * 3
   160  	}
   161  	if c.TaskRunner.Parallelism == 0 {
   162  		c.TaskRunner.Parallelism = runtime.NumCPU() / 16
   163  		if c.TaskRunner.Parallelism <= ReservedTasks {
   164  			c.TaskRunner.Parallelism = 1 + ReservedTasks
   165  		}
   166  	}
   167  	if c.TaskRunner.FetchInterval.Duration == 0 {
   168  		c.TaskRunner.FetchInterval.Duration = time.Second * 10
   169  	}
   170  	if c.TaskRunner.FetchTimeout.Duration == 0 {
   171  		c.TaskRunner.FetchTimeout.Duration = time.Second * 5
   172  	}
   173  	if c.TaskRunner.HeartbeatInterval.Duration == 0 {
   174  		c.TaskRunner.HeartbeatInterval.Duration = time.Second * 5
   175  	}
   176  	if c.TaskRunner.MaxWaitTasks == 0 {
   177  		c.TaskRunner.MaxWaitTasks = 256
   178  	}
   179  	if c.TaskRunner.QueryLimit == 0 {
   180  		c.TaskRunner.QueryLimit = c.TaskRunner.Parallelism
   181  	}
   182  	if c.TaskRunner.RetryInterval.Duration == 0 {
   183  		c.TaskRunner.RetryInterval.Duration = time.Second
   184  	}
   185  	if c.Engine.Type == "" {
   186  		c.Engine.Type = EngineDistributedTAE
   187  	}
   188  	if c.Engine.Logstore == "" {
   189  		c.Engine.Logstore = options.LogstoreLogservice
   190  	}
   191  	return nil
   192  }
   193  
   194  type service struct {
   195  	metadata       metadata.CNStore
   196  	cfg            *Config
   197  	responsePool   *sync.Pool
   198  	logger         *zap.Logger
   199  	server         morpc.RPCServer
   200  	requestHandler func(ctx context.Context, message morpc.Message, cs morpc.ClientSession, engine engine.Engine, fService fileservice.FileService, cli client.TxnClient,
   201  		messageAcquirer func() morpc.Message, getClusterDetails engine.GetClusterDetailsFunc) error
   202  	cancelMoServerFunc     context.CancelFunc
   203  	mo                     *frontend.MOServer
   204  	initHakeeperClientOnce sync.Once
   205  	_hakeeperClient        logservice.CNHAKeeperClient
   206  	initTxnSenderOnce      sync.Once
   207  	_txnSender             rpc.TxnSender
   208  	initTxnClientOnce      sync.Once
   209  	_txnClient             client.TxnClient
   210  	storeEngine            engine.Engine
   211  	metadataFS             fileservice.ReplaceableFileService
   212  	fileService            fileservice.FileService
   213  	pu                     *config.ParameterUnit
   214  
   215  	stopper *stopper.Stopper
   216  
   217  	task struct {
   218  		sync.RWMutex
   219  		holder         taskservice.TaskServiceHolder
   220  		runner         taskservice.TaskRunner
   221  		storageFactory taskservice.TaskStorageFactory
   222  	}
   223  }