github.com/matrixorigin/matrixone@v1.2.0/pkg/tnservice/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 tnservice
    16  
    17  import (
    18  	"context"
    19  	"path/filepath"
    20  	"strings"
    21  	"time"
    22  
    23  	logservicepb "github.com/matrixorigin/matrixone/pkg/pb/logservice"
    24  	"github.com/matrixorigin/matrixone/pkg/util"
    25  
    26  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    27  	"github.com/matrixorigin/matrixone/pkg/common/mpool"
    28  	"github.com/matrixorigin/matrixone/pkg/lockservice"
    29  	"github.com/matrixorigin/matrixone/pkg/logservice"
    30  	"github.com/matrixorigin/matrixone/pkg/pb/txn"
    31  	"github.com/matrixorigin/matrixone/pkg/txn/rpc"
    32  	"github.com/matrixorigin/matrixone/pkg/util/toml"
    33  )
    34  
    35  var (
    36  	defaultListenAddress         = "0.0.0.0:22000"
    37  	defaultServiceAddress        = "127.0.0.1:22000"
    38  	defaultLogtailListenAddress  = "0.0.0.0:22001"
    39  	defaultLogtailServiceAddress = "127.0.0.1:22001"
    40  	defaultLockListenAddress     = "0.0.0.0:22002"
    41  	defaultLockServiceAddress    = "127.0.0.1:22002"
    42  	defaultZombieTimeout         = time.Hour
    43  	defaultDiscoveryTimeout      = time.Second * 30
    44  	defaultHeatbeatInterval      = time.Second
    45  	defaultConnectTimeout        = time.Second * 30
    46  	defaultHeatbeatTimeout       = time.Second * 3
    47  
    48  	defaultFlushInterval         = time.Second * 60
    49  	defaultScanInterval          = time.Second * 5
    50  	defaultIncrementalInterval   = time.Minute
    51  	defaultGlobalMinCount        = int64(40)
    52  	defaultMinCount              = int64(100)
    53  	defaultReservedWALEntryCount = uint64(5000)
    54  
    55  	defaultRpcMaxMsgSize              = 1024 * mpool.KB
    56  	defaultRPCStreamPoisonTime        = 5 * time.Second
    57  	defaultLogtailCollectInterval     = 2 * time.Millisecond
    58  	defaultLogtailResponseSendTimeout = 10 * time.Second
    59  
    60  	storageDir     = "storage"
    61  	defaultDataDir = "./mo-data"
    62  
    63  	// Service ports related.
    64  	defaultServiceHost = "127.0.0.1"
    65  	defaultTxnMode     = txn.TxnMode_Pessimistic
    66  )
    67  
    68  // Config tn store configuration
    69  type Config struct {
    70  	// DataDir data dir
    71  	DataDir string `toml:"-" user_setting:"basic"`
    72  	// UUID tn store uuid
    73  	UUID string `toml:"uuid" user_setting:"basic"`
    74  	// ListenAddress listening address for receiving external requests.
    75  	ListenAddress string `toml:"listen-address"`
    76  	// ServiceAddress service address for communication, if this address is not set, use
    77  	// ListenAddress as the communication address.
    78  	ServiceAddress string `toml:"service-address"`
    79  
    80  	// PortBase is the base port for the service. We reserve reservedPorts for
    81  	// the service to start internal server inside it.
    82  	//
    83  	// TODO(volgariver6): The value of this field is also used to determine the version
    84  	// of MO. If it is not set, we use the old listen-address/service-address fields, and
    85  	// if it is set, we use the new policy to distribute the ports to all services.
    86  	PortBase int `toml:"port-base" user_setting:"basic"`
    87  	// ServiceHost is the host name/IP for the service address of RPC request. There is
    88  	// no port value in it.
    89  	ServiceHost string `toml:"service-host" user_setting:"basic"`
    90  
    91  	// HAKeeper configuration
    92  	HAKeeper struct {
    93  		// HeatbeatInterval heartbeat interval to send message to hakeeper. Default is 1s
    94  		HeatbeatInterval toml.Duration `toml:"hakeeper-heartbeat-interval"`
    95  		// HeatbeatTimeout heartbeat request timeout. Default is 3s
    96  		HeatbeatTimeout toml.Duration `toml:"hakeeper-heartbeat-timeout"`
    97  		// DiscoveryTimeout discovery HAKeeper service timeout. Default is 30s
    98  		DiscoveryTimeout toml.Duration `toml:"hakeeper-discovery-timeout"`
    99  		// ClientConfig hakeeper client configuration
   100  		ClientConfig logservice.HAKeeperClientConfig
   101  	}
   102  
   103  	// LogService log service configuration
   104  	LogService struct {
   105  		// ConnectTimeout timeout for connect to logservice. Default is 30s.
   106  		ConnectTimeout toml.Duration `toml:"connect-timeout"`
   107  	}
   108  
   109  	// RPC configuration
   110  	RPC rpc.Config `toml:"rpc"`
   111  
   112  	Ckp struct {
   113  		FlushInterval          toml.Duration `toml:"flush-interval"`
   114  		ScanInterval           toml.Duration `toml:"scan-interval"`
   115  		MinCount               int64         `toml:"min-count"`
   116  		IncrementalInterval    toml.Duration `toml:"incremental-interval"`
   117  		GlobalMinCount         int64         `toml:"global-min-count"`
   118  		ReservedWALEntryCount  uint64        `toml:"reserved-WAL-entry-count"`
   119  		OverallFlushMemControl uint64        `toml:"overall-flush-mem-control"`
   120  	}
   121  
   122  	GCCfg struct {
   123  		GCTTL          toml.Duration `toml:"gc-ttl"`
   124  		ScanGCInterval toml.Duration `toml:"scan-gc-interval"`
   125  		DisableGC      bool          `toml:"disable-gc"`
   126  	}
   127  
   128  	Merge struct {
   129  		CNTakeOverAll    bool          `toml:"offload-all"`
   130  		CNStandaloneTake bool          `toml:"offload-when-standalone"`
   131  		CNTakeOverExceed toml.ByteSize `toml:"offload-exceed"`
   132  		CNMergeMemHint   toml.ByteSize `toml:"offload-mem-hint"`
   133  	}
   134  
   135  	LogtailServer struct {
   136  		ListenAddress              string        `toml:"listen-address"`
   137  		ServiceAddress             string        `toml:"service-address"`
   138  		RpcMaxMessageSize          toml.ByteSize `toml:"rpc-max-message-size"`
   139  		RpcEnableChecksum          bool          `toml:"rpc-enable-checksum" user_setting:"advanced"`
   140  		LogtailRPCStreamPoisonTime toml.Duration `toml:"logtail-rpc-stream-poison-time"`
   141  		LogtailCollectInterval     toml.Duration `toml:"logtail-collect-interval"`
   142  		LogtailResponseSendTimeout toml.Duration `toml:"logtail-response-send-timeout"`
   143  	}
   144  
   145  	// Txn transactions configuration
   146  	Txn struct {
   147  		// ZombieTimeout A transaction timeout, if an active transaction has not operated for more
   148  		// than the specified time, it will be considered a zombie transaction and the backend will
   149  		// roll back the transaction.
   150  		ZombieTimeout toml.Duration `toml:"zombie-timeout"`
   151  
   152  		// Mode. [Optimistic|Pessimistic], default Pessimistic.
   153  		Mode string `toml:"mode"`
   154  
   155  		// If IncrementalDedup is 'true', it will enable the incremental dedup feature.
   156  		// If incremental dedup feature is disable,
   157  		// If empty, it will set 'false' when CN.Txn.Mode is optimistic,  set 'true' when CN.Txn.Mode is pessimistic
   158  		// IncrementalDedup will be treated as FullSkipWorkspaceDedup.
   159  		IncrementalDedup string `toml:"incremental-dedup"`
   160  
   161  		// Storage txn storage config
   162  		Storage struct {
   163  			// dataDir data dir used to store the data
   164  			dataDir string `toml:"-"`
   165  			// Backend txn storage backend implementation. [TAE|Mem], default TAE.
   166  			Backend StorageType `toml:"backend"`
   167  		}
   168  	}
   169  
   170  	// Cluster configuration
   171  	Cluster struct {
   172  		// RefreshInterval refresh cluster info from hakeeper interval
   173  		RefreshInterval toml.Duration `toml:"refresh-interval"`
   174  	}
   175  
   176  	// LockService lockservice config
   177  	LockService lockservice.Config `toml:"lockservice"`
   178  
   179  	// IsStandalone indicates whether the tn is in standalone cluster not an independent process.
   180  	// For the tn does not boost an independent queryservice in standalone mode.
   181  	// cn,tn shares the same queryservice in standalone mode.
   182  	// Under distributed deploy mode, cn,tn are independent os process.
   183  	// they have their own queryservice.
   184  	InStandalone bool
   185  }
   186  
   187  func (c *Config) Validate() error {
   188  	if c.UUID == "" {
   189  		return moerr.NewInternalError(context.Background(), "Config.UUID not set")
   190  	}
   191  	if c.DataDir == "" {
   192  		c.DataDir = defaultDataDir
   193  	}
   194  	c.Txn.Storage.dataDir = filepath.Join(c.DataDir, storageDir)
   195  	if c.ListenAddress == "" {
   196  		c.ListenAddress = defaultListenAddress
   197  	}
   198  	if c.ServiceAddress == "" {
   199  		c.ServiceAddress = defaultServiceAddress
   200  	}
   201  	if c.LockService.ListenAddress == "" {
   202  		c.LockService.ListenAddress = defaultLockListenAddress
   203  	}
   204  	if c.LockService.ServiceAddress == "" {
   205  		c.LockService.ServiceAddress = defaultLockServiceAddress
   206  	}
   207  	if c.Txn.Storage.Backend == "" {
   208  		c.Txn.Storage.Backend = StorageTAE
   209  	}
   210  	if _, ok := supportTxnStorageBackends[c.Txn.Storage.Backend]; !ok {
   211  		return moerr.NewInternalError(context.Background(), "%s txn storage backend not support", c.Txn.Storage)
   212  	}
   213  	if c.Txn.ZombieTimeout.Duration == 0 {
   214  		c.Txn.ZombieTimeout.Duration = defaultZombieTimeout
   215  	}
   216  	if c.HAKeeper.DiscoveryTimeout.Duration == 0 {
   217  		c.HAKeeper.DiscoveryTimeout.Duration = defaultDiscoveryTimeout
   218  	}
   219  	if c.HAKeeper.HeatbeatInterval.Duration == 0 {
   220  		c.HAKeeper.HeatbeatInterval.Duration = defaultHeatbeatInterval
   221  	}
   222  	if c.HAKeeper.HeatbeatTimeout.Duration == 0 {
   223  		c.HAKeeper.HeatbeatTimeout.Duration = defaultHeatbeatTimeout
   224  	}
   225  	if c.LogService.ConnectTimeout.Duration == 0 {
   226  		c.LogService.ConnectTimeout.Duration = defaultConnectTimeout
   227  	}
   228  	if c.Ckp.ScanInterval.Duration == 0 {
   229  		c.Ckp.ScanInterval.Duration = defaultScanInterval
   230  	}
   231  	if c.Ckp.FlushInterval.Duration == 0 {
   232  		c.Ckp.FlushInterval.Duration = defaultFlushInterval
   233  	}
   234  	if c.Ckp.MinCount == 0 {
   235  		c.Ckp.MinCount = defaultMinCount
   236  	}
   237  	if c.Ckp.IncrementalInterval.Duration == 0 {
   238  		c.Ckp.IncrementalInterval.Duration = defaultIncrementalInterval
   239  	}
   240  	if c.Ckp.GlobalMinCount == 0 {
   241  		c.Ckp.GlobalMinCount = defaultGlobalMinCount
   242  	}
   243  	if c.Ckp.ReservedWALEntryCount == 0 {
   244  		c.Ckp.ReservedWALEntryCount = defaultReservedWALEntryCount
   245  	}
   246  
   247  	if c.LogtailServer.ListenAddress == "" {
   248  		c.LogtailServer.ListenAddress = defaultLogtailListenAddress
   249  	}
   250  	if c.LogtailServer.ServiceAddress == "" {
   251  		c.LogtailServer.ServiceAddress = defaultLogtailServiceAddress
   252  	}
   253  	if c.LogtailServer.RpcMaxMessageSize <= 0 {
   254  		c.LogtailServer.RpcMaxMessageSize = toml.ByteSize(defaultRpcMaxMsgSize)
   255  	}
   256  	if c.LogtailServer.LogtailRPCStreamPoisonTime.Duration <= 0 {
   257  		c.LogtailServer.LogtailRPCStreamPoisonTime.Duration = defaultRPCStreamPoisonTime
   258  	}
   259  	if c.LogtailServer.LogtailCollectInterval.Duration <= 0 {
   260  		c.LogtailServer.LogtailCollectInterval.Duration = defaultLogtailCollectInterval
   261  	}
   262  	if c.LogtailServer.LogtailResponseSendTimeout.Duration <= 0 {
   263  		c.LogtailServer.LogtailResponseSendTimeout.Duration = defaultLogtailResponseSendTimeout
   264  	}
   265  	if c.Cluster.RefreshInterval.Duration == 0 {
   266  		c.Cluster.RefreshInterval.Duration = time.Second * 10
   267  	}
   268  
   269  	if c.Txn.Mode == "" {
   270  		c.Txn.Mode = defaultTxnMode.String()
   271  	} else {
   272  		if !txn.ValidTxnMode(c.Txn.Mode) {
   273  			return moerr.NewInternalError(context.Background(), "invalid txn mode %s", c.Txn.Mode)
   274  		}
   275  	}
   276  
   277  	if c.Txn.IncrementalDedup == "" {
   278  		if txn.GetTxnMode(c.Txn.Mode) == txn.TxnMode_Pessimistic {
   279  			c.Txn.IncrementalDedup = "true"
   280  		} else {
   281  			c.Txn.IncrementalDedup = "false"
   282  		}
   283  	} else {
   284  		c.Txn.IncrementalDedup = strings.ToLower(c.Txn.IncrementalDedup)
   285  		if c.Txn.IncrementalDedup != "true" && c.Txn.IncrementalDedup != "false" {
   286  			return moerr.NewBadDBNoCtx("not support txn incremental-dedup: " + c.Txn.IncrementalDedup)
   287  		}
   288  	}
   289  
   290  	c.RPC.Adjust()
   291  	c.LockService.ServiceID = c.UUID
   292  	c.LockService.Validate()
   293  
   294  	if c.PortBase != 0 {
   295  		if c.ServiceHost == "" {
   296  			c.ServiceHost = defaultServiceHost
   297  		}
   298  	}
   299  	return nil
   300  }
   301  
   302  func (c *Config) SetDefaultValue() {
   303  	if c.DataDir == "" {
   304  		c.DataDir = defaultDataDir
   305  	}
   306  	c.Txn.Storage.dataDir = filepath.Join(c.DataDir, storageDir)
   307  	if c.ListenAddress == "" {
   308  		c.ListenAddress = defaultListenAddress
   309  	}
   310  	if c.ServiceAddress == "" {
   311  		c.ServiceAddress = defaultServiceAddress
   312  	}
   313  	if c.LockService.ListenAddress == "" {
   314  		c.LockService.ListenAddress = defaultLockListenAddress
   315  	}
   316  	if c.LockService.ServiceAddress == "" {
   317  		c.LockService.ServiceAddress = defaultLockServiceAddress
   318  	}
   319  	if c.Txn.Storage.Backend == "" {
   320  		c.Txn.Storage.Backend = StorageTAE
   321  	}
   322  
   323  	if c.Txn.ZombieTimeout.Duration == 0 {
   324  		c.Txn.ZombieTimeout.Duration = defaultZombieTimeout
   325  	}
   326  	if c.HAKeeper.DiscoveryTimeout.Duration == 0 {
   327  		c.HAKeeper.DiscoveryTimeout.Duration = defaultDiscoveryTimeout
   328  	}
   329  	if c.HAKeeper.HeatbeatInterval.Duration == 0 {
   330  		c.HAKeeper.HeatbeatInterval.Duration = defaultHeatbeatInterval
   331  	}
   332  	if c.HAKeeper.HeatbeatTimeout.Duration == 0 {
   333  		c.HAKeeper.HeatbeatTimeout.Duration = defaultHeatbeatTimeout
   334  	}
   335  	if c.LogService.ConnectTimeout.Duration == 0 {
   336  		c.LogService.ConnectTimeout.Duration = defaultConnectTimeout
   337  	}
   338  	if c.Ckp.ScanInterval.Duration == 0 {
   339  		c.Ckp.ScanInterval.Duration = defaultScanInterval
   340  	}
   341  	if c.Ckp.FlushInterval.Duration == 0 {
   342  		c.Ckp.FlushInterval.Duration = defaultFlushInterval
   343  	}
   344  	if c.Ckp.MinCount == 0 {
   345  		c.Ckp.MinCount = defaultMinCount
   346  	}
   347  	if c.Ckp.IncrementalInterval.Duration == 0 {
   348  		c.Ckp.IncrementalInterval.Duration = defaultIncrementalInterval
   349  	}
   350  	if c.Ckp.GlobalMinCount == 0 {
   351  		c.Ckp.GlobalMinCount = defaultGlobalMinCount
   352  	}
   353  	if c.LogtailServer.ListenAddress == "" {
   354  		c.LogtailServer.ListenAddress = defaultLogtailListenAddress
   355  	}
   356  	if c.LogtailServer.ServiceAddress == "" {
   357  		c.LogtailServer.ServiceAddress = defaultLogtailServiceAddress
   358  	}
   359  	if c.LogtailServer.RpcMaxMessageSize <= 0 {
   360  		c.LogtailServer.RpcMaxMessageSize = toml.ByteSize(defaultRpcMaxMsgSize)
   361  	}
   362  	if c.LogtailServer.LogtailRPCStreamPoisonTime.Duration <= 0 {
   363  		c.LogtailServer.LogtailRPCStreamPoisonTime.Duration = defaultRPCStreamPoisonTime
   364  	}
   365  	if c.LogtailServer.LogtailCollectInterval.Duration <= 0 {
   366  		c.LogtailServer.LogtailCollectInterval.Duration = defaultLogtailCollectInterval
   367  	}
   368  	if c.LogtailServer.LogtailResponseSendTimeout.Duration <= 0 {
   369  		c.LogtailServer.LogtailResponseSendTimeout.Duration = defaultLogtailResponseSendTimeout
   370  	}
   371  	if c.Cluster.RefreshInterval.Duration == 0 {
   372  		c.Cluster.RefreshInterval.Duration = time.Second * 10
   373  	}
   374  
   375  	if c.Txn.Mode == "" {
   376  		c.Txn.Mode = defaultTxnMode.String()
   377  	}
   378  
   379  	if c.Txn.IncrementalDedup == "" {
   380  		if txn.GetTxnMode(c.Txn.Mode) == txn.TxnMode_Pessimistic {
   381  			c.Txn.IncrementalDedup = "true"
   382  		} else {
   383  			c.Txn.IncrementalDedup = "false"
   384  		}
   385  	} else {
   386  		c.Txn.IncrementalDedup = strings.ToLower(c.Txn.IncrementalDedup)
   387  	}
   388  
   389  	c.RPC.Adjust()
   390  	c.LockService.ServiceID = "tmp"
   391  	c.LockService.Validate()
   392  	c.LockService.ServiceID = c.UUID
   393  
   394  	if c.PortBase != 0 {
   395  		if c.ServiceHost == "" {
   396  			c.ServiceHost = defaultServiceHost
   397  		}
   398  	}
   399  }
   400  
   401  func dumpTnConfig(cfg Config) (map[string]*logservicepb.ConfigItem, error) {
   402  	defCfg := Config{}
   403  	defCfg.SetDefaultValue()
   404  	return util.DumpConfig(cfg, defCfg)
   405  }