github.com/matrixorigin/matrixone@v0.7.0/pkg/dnservice/cfg.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 dnservice
    16  
    17  import (
    18  	"context"
    19  	"path/filepath"
    20  	"time"
    21  
    22  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    23  	"github.com/matrixorigin/matrixone/pkg/common/mpool"
    24  	"github.com/matrixorigin/matrixone/pkg/logservice"
    25  	"github.com/matrixorigin/matrixone/pkg/txn/rpc"
    26  	"github.com/matrixorigin/matrixone/pkg/util/toml"
    27  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/options"
    28  )
    29  
    30  var (
    31  	defaultListenAddress    = "0.0.0.0:22000"
    32  	defaultServiceAddress   = "127.0.0.1:22000"
    33  	defaultLogtailAddress   = "127.0.0.1:22001"
    34  	defaultZombieTimeout    = time.Hour
    35  	defaultDiscoveryTimeout = time.Second * 30
    36  	defaultHeatbeatInterval = time.Second
    37  	defaultConnectTimeout   = time.Second * 30
    38  	defaultHeatbeatTimeout  = time.Second * 3
    39  
    40  	defaultFlushInterval       = time.Second * 60
    41  	defaultScanInterval        = time.Second * 5
    42  	defaultIncrementalInterval = time.Minute
    43  	defaultGlobalMinCount      = int64(60)
    44  	defaultMinCount            = int64(100)
    45  	defaultLogBackend          = string(options.LogstoreLogservice)
    46  
    47  	defaultRpcMaxMsgSize              = 1024 * mpool.KB
    48  	defaultRpcPayloadCopyBufferSize   = 1024 * mpool.KB
    49  	defaultLogtailCollectInterval     = 50 * time.Millisecond
    50  	defaultLogtailResponseSendTimeout = 10 * time.Second
    51  	defaultMaxLogtailFetchFailure     = 5
    52  
    53  	storageDir     = "storage"
    54  	defaultDataDir = "./mo-data"
    55  )
    56  
    57  // Config dn store configuration
    58  type Config struct {
    59  	// DataDir data dir
    60  	DataDir string `toml:"-"`
    61  	// UUID dn store uuid
    62  	UUID string `toml:"uuid"`
    63  	// ListenAddress listening address for receiving external requests.
    64  	ListenAddress string `toml:"listen-address"`
    65  	// ServiceAddress service address for communication, if this address is not set, use
    66  	// ListenAddress as the communication address.
    67  	ServiceAddress string `toml:"service-address"`
    68  
    69  	// HAKeeper configuration
    70  	HAKeeper struct {
    71  		// HeatbeatInterval heartbeat interval to send message to hakeeper. Default is 1s
    72  		HeatbeatInterval toml.Duration `toml:"hakeeper-heartbeat-interval"`
    73  		// HeatbeatTimeout heartbeat request timeout. Default is 3s
    74  		HeatbeatTimeout toml.Duration `toml:"hakeeper-heartbeat-timeout"`
    75  		// DiscoveryTimeout discovery HAKeeper service timeout. Default is 30s
    76  		DiscoveryTimeout toml.Duration `toml:"hakeeper-discovery-timeout"`
    77  		// ClientConfig hakeeper client configuration
    78  		ClientConfig logservice.HAKeeperClientConfig
    79  	}
    80  
    81  	// LogService log service configuration
    82  	LogService struct {
    83  		// ConnectTimeout timeout for connect to logservice. Default is 30s.
    84  		ConnectTimeout toml.Duration `toml:"connect-timeout"`
    85  	}
    86  
    87  	// RPC configuration
    88  	RPC rpc.Config `toml:"rpc"`
    89  
    90  	Ckp struct {
    91  		FlushInterval       toml.Duration `toml:"flush-interval"`
    92  		ScanInterval        toml.Duration `toml:"scan-interval"`
    93  		MinCount            int64         `toml:"min-count"`
    94  		IncrementalInterval toml.Duration `toml:"incremental-interval"`
    95  		GlobalMinCount      int64         `toml:"global-min-count"`
    96  	}
    97  
    98  	LogtailServer struct {
    99  		ListenAddress              string        `toml:"listen-address"`
   100  		RpcMaxMessageSize          toml.ByteSize `toml:"rpc-max-message-size"`
   101  		RpcPayloadCopyBufferSize   toml.ByteSize `toml:"rpc-payload-copy-buffer-size"`
   102  		RpcEnableChecksum          bool          `toml:"rpc-enable-checksum"`
   103  		LogtailCollectInterval     toml.Duration `toml:"logtail-collect-interval"`
   104  		LogtailResponseSendTimeout toml.Duration `toml:"logtail-response-send-timeout"`
   105  		MaxLogtailFetchFailure     int           `toml:"max-logtail-fetch-failure"`
   106  	}
   107  
   108  	// Txn transactions configuration
   109  	Txn struct {
   110  		// ZombieTimeout A transaction timeout, if an active transaction has not operated for more
   111  		// than the specified time, it will be considered a zombie transaction and the backend will
   112  		// roll back the transaction.
   113  		ZombieTimeout toml.Duration `toml:"zombie-timeout"`
   114  
   115  		// Storage txn storage config
   116  		Storage struct {
   117  			// dataDir data dir used to store the data
   118  			dataDir string `toml:"-"`
   119  			// Backend txn storage backend implementation. [TAE|Mem], default TAE.
   120  			Backend StorageType `toml:"backend"`
   121  			// LogBackend the backend used to store logs
   122  			LogBackend string `toml:"log-backend"`
   123  		}
   124  	}
   125  }
   126  
   127  func (c *Config) Validate() error {
   128  	if c.UUID == "" {
   129  		return moerr.NewInternalError(context.Background(), "Config.UUID not set")
   130  	}
   131  	if c.DataDir == "" {
   132  		c.DataDir = defaultDataDir
   133  	}
   134  	c.Txn.Storage.dataDir = filepath.Join(c.DataDir, storageDir)
   135  	if c.ListenAddress == "" {
   136  		c.ListenAddress = defaultListenAddress
   137  		c.ServiceAddress = defaultServiceAddress
   138  	}
   139  	if c.ServiceAddress == "" {
   140  		c.ServiceAddress = c.ListenAddress
   141  	}
   142  	if c.Txn.Storage.Backend == "" {
   143  		c.Txn.Storage.Backend = StorageTAE
   144  	}
   145  	if c.Txn.Storage.LogBackend == "" {
   146  		c.Txn.Storage.LogBackend = defaultLogBackend
   147  	}
   148  	if _, ok := supportTxnStorageBackends[c.Txn.Storage.Backend]; !ok {
   149  		return moerr.NewInternalError(context.Background(), "%s txn storage backend not support", c.Txn.Storage)
   150  	}
   151  	if c.Txn.ZombieTimeout.Duration == 0 {
   152  		c.Txn.ZombieTimeout.Duration = defaultZombieTimeout
   153  	}
   154  	if c.HAKeeper.DiscoveryTimeout.Duration == 0 {
   155  		c.HAKeeper.DiscoveryTimeout.Duration = defaultDiscoveryTimeout
   156  	}
   157  	if c.HAKeeper.HeatbeatInterval.Duration == 0 {
   158  		c.HAKeeper.HeatbeatInterval.Duration = defaultHeatbeatInterval
   159  	}
   160  	if c.HAKeeper.HeatbeatTimeout.Duration == 0 {
   161  		c.HAKeeper.HeatbeatTimeout.Duration = defaultHeatbeatTimeout
   162  	}
   163  	if c.LogService.ConnectTimeout.Duration == 0 {
   164  		c.LogService.ConnectTimeout.Duration = defaultConnectTimeout
   165  	}
   166  	if c.Ckp.ScanInterval.Duration == 0 {
   167  		c.Ckp.ScanInterval.Duration = defaultScanInterval
   168  	}
   169  	if c.Ckp.FlushInterval.Duration == 0 {
   170  		c.Ckp.FlushInterval.Duration = defaultFlushInterval
   171  	}
   172  	if c.Ckp.MinCount == 0 {
   173  		c.Ckp.MinCount = defaultMinCount
   174  	}
   175  	if c.Ckp.IncrementalInterval.Duration == 0 {
   176  		c.Ckp.IncrementalInterval.Duration = defaultIncrementalInterval
   177  	}
   178  	if c.Ckp.GlobalMinCount == 0 {
   179  		c.Ckp.GlobalMinCount = defaultGlobalMinCount
   180  	}
   181  	if c.LogtailServer.ListenAddress == "" {
   182  		c.LogtailServer.ListenAddress = defaultLogtailAddress
   183  	}
   184  	if c.LogtailServer.RpcMaxMessageSize <= 0 {
   185  		c.LogtailServer.RpcMaxMessageSize = toml.ByteSize(defaultRpcMaxMsgSize)
   186  	}
   187  	if c.LogtailServer.RpcPayloadCopyBufferSize <= 0 {
   188  		c.LogtailServer.RpcPayloadCopyBufferSize = toml.ByteSize(defaultRpcPayloadCopyBufferSize)
   189  	}
   190  	if c.LogtailServer.LogtailCollectInterval.Duration <= 0 {
   191  		c.LogtailServer.LogtailCollectInterval.Duration = defaultLogtailCollectInterval
   192  	}
   193  	if c.LogtailServer.LogtailResponseSendTimeout.Duration <= 0 {
   194  		c.LogtailServer.LogtailResponseSendTimeout.Duration = defaultLogtailResponseSendTimeout
   195  	}
   196  	if c.LogtailServer.MaxLogtailFetchFailure <= 0 {
   197  		c.LogtailServer.MaxLogtailFetchFailure = defaultMaxLogtailFetchFailure
   198  	}
   199  
   200  	return nil
   201  }