github.com/matrixorigin/matrixone@v1.2.0/pkg/config/configuration.go (about)

     1  // Copyright 2021 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 config
    16  
    17  import (
    18  	"context"
    19  	"os"
    20  	"path/filepath"
    21  	"time"
    22  
    23  	"github.com/matrixorigin/matrixone/pkg/fileservice"
    24  	"github.com/matrixorigin/matrixone/pkg/lockservice"
    25  	"github.com/matrixorigin/matrixone/pkg/logservice"
    26  	"github.com/matrixorigin/matrixone/pkg/logutil"
    27  	qclient "github.com/matrixorigin/matrixone/pkg/queryservice/client"
    28  	"github.com/matrixorigin/matrixone/pkg/taskservice"
    29  	"github.com/matrixorigin/matrixone/pkg/txn/client"
    30  	"github.com/matrixorigin/matrixone/pkg/udf"
    31  	"github.com/matrixorigin/matrixone/pkg/util/toml"
    32  	"github.com/matrixorigin/matrixone/pkg/vm/engine"
    33  )
    34  
    35  type ConfigurationKeyType int
    36  
    37  const (
    38  	ParameterUnitKey ConfigurationKeyType = 1
    39  )
    40  
    41  var (
    42  
    43  	//port defines which port the mo-server listens on and clients connect to
    44  	defaultPort = 6001
    45  
    46  	//listening ip
    47  	defaultHost = "0.0.0.0"
    48  
    49  	//listening unix domain socket
    50  	defaultUnixAddr = "/tmp/mysql.sock"
    51  
    52  	//guest mmu limitation.  1 << 40 = 1099511627776
    53  	defaultGuestMmuLimitation = 1099511627776
    54  
    55  	//mempool maxsize.  1 << 40 = 1099511627776
    56  	defaultMempoolMaxSize = 1099511627776
    57  
    58  	//mempool factor.
    59  	defaultMempoolFactor = 8
    60  
    61  	//process.Limitation.Size.  10 << 32 = 42949672960
    62  	defaultProcessLimitationSize = 42949672960
    63  
    64  	//process.Limitation.BatchRows.  10 << 32 = 42949672960
    65  	defaultProcessLimitationBatchRows = 42949672960
    66  
    67  	//process.Limitation.PartitionRows.  10 << 32 = 42949672960
    68  	defaultProcessLimitationPartitionRows = 42949672960
    69  
    70  	defaultServerVersionPrefix = "8.0.30-MatrixOne-v"
    71  
    72  	//the length of query printed into console. -1, complete string. 0, empty string. >0 , length of characters at the header of the string.
    73  	defaultLengthOfQueryPrinted = 1024
    74  
    75  	//the count of rows in vector of batch in load data
    76  	defaultBatchSizeInLoadData = 40000
    77  
    78  	//initial value is 4. The count of go routine writing batch into the storage.
    79  	defaultLoadDataConcurrencyCount = 4
    80  
    81  	//KB. When the number of bytes in the outbuffer exceeds the it,the outbuffer will be flushed.
    82  	defaultMaxBytesInOutbufToFlush = 1024
    83  
    84  	//printLog Interval is 10s.
    85  	defaultPrintLogInterVal = 10
    86  
    87  	//export data to csv file default flush size
    88  	defaultExportDataDefaultFlushSize = 1
    89  
    90  	//port defines which port the rpc server listens on
    91  	defaultPortOfRpcServerInComputationEngine = 20000
    92  
    93  	//statusPort defines which port the mo status server (for metric etc.) listens on and clients connect to
    94  	defaultStatusPort = 7001
    95  
    96  	// defaultTraceExportInterval default: 15 sec.
    97  	defaultTraceExportInterval = 15
    98  
    99  	// defaultMetricExportInterval default: 15 sec.
   100  	defaultMetricExportInterval = 15
   101  
   102  	// defaultLogShardID default: 1
   103  	defaultLogShardID = 1
   104  
   105  	// defaultTNReplicaID default: 1
   106  	defaultTNReplicaID = 1
   107  	// defaultMetricGatherInterval default: 15 sec.
   108  	defaultMetricGatherInterval = 15
   109  
   110  	// defaultMetricInternalGatherInterval default: 1 min.
   111  	defaultMetricInternalGatherInterval = time.Minute
   112  
   113  	// defaultMetricUpdateStorageUsageInterval default: 15 min.
   114  	defaultMetricUpdateStorageUsageInterval = 15 * time.Minute
   115  
   116  	// defaultMetricStorageUsageCheckNewInterval default: 1 min
   117  	defaultMetricStorageUsageCheckNewInterval = time.Minute
   118  
   119  	// defaultMergeCycle default: 5 minute
   120  	defaultMergeCycle = 5 * time.Minute
   121  
   122  	// defaultSessionTimeout default: 24 hour
   123  	defaultSessionTimeout = 24 * time.Hour
   124  
   125  	// defaultOBShowStatsInterval default: 1min
   126  	defaultOBShowStatsInterval = time.Minute
   127  
   128  	// defaultOBMaxBufferCnt
   129  	defaultOBBufferCnt int32 = -1
   130  
   131  	//defaultOBBufferSize, 10 << 20 = 10485760
   132  	defaultOBBufferSize int64 = 10485760
   133  
   134  	// defaultOBCollectorCntPercent
   135  	defaultOBCollectorCntPercent = 1000
   136  	// defaultOBGeneratorCntPercent
   137  	defaultOBGeneratorCntPercent = 1000
   138  	// defaultOBExporterCntPercent
   139  	defaultOBExporterCntPercent = 1000
   140  
   141  	// defaultPrintDebugInterval default: 30 minutes
   142  	defaultPrintDebugInterval = 30
   143  
   144  	// defaultKillRountinesInterval default: 10 seconds
   145  	defaultKillRountinesInterval = 10
   146  
   147  	//defaultCleanKillQueueInterval default: 60 minutes
   148  	defaultCleanKillQueueInterval = 60
   149  
   150  	// defaultLongSpanTime default: 10 s
   151  	defaultLongSpanTime = 10 * time.Second
   152  
   153  	defaultAggregationWindow = 5 * time.Second
   154  
   155  	defaultSelectThreshold = 200 * time.Millisecond
   156  
   157  	// defaultLongQueryTime
   158  	defaultLongQueryTime = 1.0
   159  	// defaultSkipRunningStmt
   160  	defaultSkipRunningStmt = true
   161  	// defaultLoggerLabelKey and defaultLoggerLabelVal
   162  	defaultLoggerLabelKey = "role"
   163  	defaultLoggerLabelVal = "logging_cn"
   164  
   165  	// default lower_case_table_names
   166  	defaultLowerCaseTableNames = "1"
   167  
   168  	CNPrimaryCheck = false
   169  )
   170  
   171  // FrontendParameters of the frontend
   172  type FrontendParameters struct {
   173  	MoVersion string
   174  
   175  	//port defines which port the mo-server listens on and clients connect to
   176  	Port int64 `toml:"port" user_setting:"basic"`
   177  
   178  	//listening ip
   179  	Host string `toml:"host" user_setting:"basic"`
   180  
   181  	// UnixSocketAddress listening unix domain socket
   182  	UnixSocketAddress string `toml:"unix-socket" user_setting:"advanced"`
   183  
   184  	//guest mmu limitation. default: 1 << 40 = 1099511627776
   185  	GuestMmuLimitation int64 `toml:"guestMmuLimitation"`
   186  
   187  	//mempool maxsize. default: 1 << 40 = 1099511627776
   188  	MempoolMaxSize int64 `toml:"mempoolMaxSize"`
   189  
   190  	//mempool factor. default: 8
   191  	MempoolFactor int64 `toml:"mempoolFactor"`
   192  
   193  	//process.Limitation.Size. default: 10 << 32 = 42949672960
   194  	ProcessLimitationSize int64 `toml:"processLimitationSize"`
   195  
   196  	//process.Limitation.BatchRows. default: 10 << 32 = 42949672960
   197  	ProcessLimitationBatchRows int64 `toml:"processLimitationBatchRows"`
   198  
   199  	//process.Limitation.BatchSize. default: 0
   200  	ProcessLimitationBatchSize int64 `toml:"processLimitationBatchSize"`
   201  
   202  	//process.Limitation.PartitionRows. default: 10 << 32 = 42949672960
   203  	ProcessLimitationPartitionRows int64 `toml:"processLimitationPartitionRows"`
   204  
   205  	//the root directory of the storage and matrixcube's data. The actual dir is cubeDirPrefix + nodeID
   206  	ServerVersionPrefix string `toml:"serverVersionPrefix"`
   207  
   208  	//the length of query printed into console. -1, complete string. 0, empty string. >0 , length of characters at the header of the string.
   209  	LengthOfQueryPrinted int64 `toml:"lengthOfQueryPrinted" user_setting:"advanced"`
   210  
   211  	//the count of rows in vector of batch in load data
   212  	BatchSizeInLoadData int64 `toml:"batchSizeInLoadData"`
   213  
   214  	//default is 4. The count of go routine writing batch into the storage.
   215  	LoadDataConcurrencyCount int64 `toml:"loadDataConcurrencyCount"`
   216  
   217  	//default is false. Skip writing batch into the storage
   218  	LoadDataSkipWritingBatch bool `toml:"loadDataSkipWritingBatch"`
   219  
   220  	//KB. When the number of bytes in the outbuffer exceeds it,the outbuffer will be flushed.
   221  	MaxBytesInOutbufToFlush int64 `toml:"maxBytesInOutbufToFlush"`
   222  
   223  	//default printLog Interval is 10s.
   224  	PrintLogInterVal int64 `toml:"printLogInterVal"`
   225  
   226  	//export data to csv file default flush size
   227  	ExportDataDefaultFlushSize int64 `toml:"exportDataDefaultFlushSize"`
   228  
   229  	//port defines which port the rpc server listens on
   230  	PortOfRpcServerInComputationEngine int64 `toml:"portOfRpcServerInComputationEngine"`
   231  
   232  	//default is false. With true. Server will support tls
   233  	EnableTls bool `toml:"enableTls" user_setting:"advanced"`
   234  
   235  	//default is ''. Path of file that contains list of trusted SSL CAs for client
   236  	TlsCaFile string `toml:"tlsCaFile" user_setting:"advanced"`
   237  
   238  	//default is ''. Path of file that contains X509 certificate in PEM format for client
   239  	TlsCertFile string `toml:"tlsCertFile" user_setting:"advanced"`
   240  
   241  	//default is ''. Path of file that contains X509 key in PEM format for client
   242  	TlsKeyFile string `toml:"tlsKeyFile" user_setting:"advanced"`
   243  
   244  	//default is 1
   245  	LogShardID uint64 `toml:"logshardid"`
   246  
   247  	//default is 1
   248  	TNReplicaID uint64 `toml:"tnreplicalid"`
   249  
   250  	EnableDoComQueryInProgress bool `toml:"comQueryInProgress"`
   251  
   252  	//timeout of the session. the default is 10minutes
   253  	SessionTimeout toml.Duration `toml:"sessionTimeout"`
   254  
   255  	// MaxMessageSize max size for read messages from dn. Default is 10M
   256  	MaxMessageSize uint64 `toml:"max-message-size"`
   257  
   258  	// default off
   259  	SaveQueryResult string `toml:"saveQueryResult" user_setting:"advanced"`
   260  
   261  	// default 24 (h)
   262  	QueryResultTimeout uint64 `toml:"queryResultTimeout" user_setting:"advanced"`
   263  
   264  	// default 100 (MB)
   265  	QueryResultMaxsize uint64 `toml:"queryResultMaxsize" user_setting:"advanced"`
   266  
   267  	AutoIncrCacheSize uint64 `toml:"autoIncrCacheSize"`
   268  
   269  	LowerCaseTableNames string `toml:"lowerCaseTableNames" user_setting:"advanced"`
   270  
   271  	PrintDebug bool `toml:"printDebug"`
   272  
   273  	PrintDebugInterval int `toml:"printDebugInterval"`
   274  
   275  	// Interval in seconds
   276  	KillRountinesInterval int `toml:"killRountinesInterval"`
   277  
   278  	CleanKillQueueInterval int `toml:"cleanKillQueueInterval"`
   279  
   280  	// ProxyEnabled indicates that proxy module is enabled and something extra
   281  	// is needed, such as update the salt.
   282  	ProxyEnabled bool `toml:"proxy-enabled"`
   283  
   284  	// SkipCheckPrivilege denotes the privilege check should be passed.
   285  	SkipCheckPrivilege bool `toml:"skipCheckPrivilege"`
   286  
   287  	// skip checking the password of the user
   288  	SkipCheckUser bool `toml:"skipCheckUser"`
   289  
   290  	// disable select into
   291  	DisableSelectInto bool `toml:"disable-select-into"`
   292  }
   293  
   294  func (fp *FrontendParameters) SetDefaultValues() {
   295  
   296  	if fp.Port == 0 {
   297  		fp.Port = int64(defaultPort)
   298  	}
   299  
   300  	if fp.Host == "" {
   301  		fp.Host = defaultHost
   302  	}
   303  
   304  	if fp.UnixSocketAddress == "" {
   305  		fp.UnixSocketAddress = defaultUnixAddr
   306  	}
   307  
   308  	if fp.GuestMmuLimitation == 0 {
   309  		fp.GuestMmuLimitation = int64(toml.ByteSize(defaultGuestMmuLimitation))
   310  	}
   311  
   312  	if fp.MempoolMaxSize == 0 {
   313  		fp.MempoolMaxSize = int64(toml.ByteSize(defaultMempoolMaxSize))
   314  	}
   315  
   316  	if fp.MempoolFactor == 0 {
   317  		fp.MempoolFactor = int64(defaultMempoolFactor)
   318  	}
   319  
   320  	if fp.ProcessLimitationSize == 0 {
   321  		fp.ProcessLimitationSize = int64(toml.ByteSize(defaultProcessLimitationSize))
   322  	}
   323  
   324  	if fp.ProcessLimitationBatchRows == 0 {
   325  		fp.ProcessLimitationBatchRows = int64(toml.ByteSize(defaultProcessLimitationBatchRows))
   326  	}
   327  
   328  	if fp.ProcessLimitationPartitionRows == 0 {
   329  		fp.ProcessLimitationPartitionRows = int64(toml.ByteSize(defaultProcessLimitationPartitionRows))
   330  	}
   331  
   332  	if fp.ServerVersionPrefix == "" {
   333  		fp.ServerVersionPrefix = defaultServerVersionPrefix
   334  	}
   335  
   336  	if fp.LengthOfQueryPrinted == 0 {
   337  		fp.LengthOfQueryPrinted = int64(defaultLengthOfQueryPrinted)
   338  	}
   339  
   340  	if fp.BatchSizeInLoadData == 0 {
   341  		fp.BatchSizeInLoadData = int64(defaultBatchSizeInLoadData)
   342  	}
   343  
   344  	if fp.LoadDataConcurrencyCount == 0 {
   345  		fp.LoadDataConcurrencyCount = int64(defaultLoadDataConcurrencyCount)
   346  	}
   347  
   348  	if fp.MaxBytesInOutbufToFlush == 0 {
   349  		fp.MaxBytesInOutbufToFlush = int64(defaultMaxBytesInOutbufToFlush)
   350  	}
   351  
   352  	if fp.PrintLogInterVal == 0 {
   353  		fp.PrintLogInterVal = int64(defaultPrintLogInterVal)
   354  	}
   355  
   356  	if fp.ExportDataDefaultFlushSize == 0 {
   357  		fp.ExportDataDefaultFlushSize = int64(defaultExportDataDefaultFlushSize)
   358  	}
   359  
   360  	if fp.PortOfRpcServerInComputationEngine == 0 {
   361  		fp.PortOfRpcServerInComputationEngine = int64(defaultPortOfRpcServerInComputationEngine)
   362  	}
   363  
   364  	if fp.TNReplicaID == 0 {
   365  		fp.TNReplicaID = uint64(defaultTNReplicaID)
   366  	}
   367  
   368  	if fp.LogShardID == 0 {
   369  		fp.LogShardID = uint64(defaultLogShardID)
   370  	}
   371  
   372  	if fp.SessionTimeout.Duration == 0 {
   373  		fp.SessionTimeout.Duration = defaultSessionTimeout
   374  	}
   375  
   376  	if fp.SaveQueryResult == "" {
   377  		fp.SaveQueryResult = "off"
   378  	}
   379  
   380  	if fp.QueryResultTimeout == 0 {
   381  		fp.QueryResultTimeout = 24
   382  	}
   383  
   384  	if fp.QueryResultMaxsize == 0 {
   385  		fp.QueryResultMaxsize = 100
   386  	}
   387  
   388  	if fp.AutoIncrCacheSize == 0 {
   389  		fp.AutoIncrCacheSize = 3000000
   390  	}
   391  
   392  	if fp.LowerCaseTableNames == "" {
   393  		fp.LowerCaseTableNames = defaultLowerCaseTableNames
   394  	}
   395  
   396  	if fp.PrintDebugInterval == 0 {
   397  		fp.PrintDebugInterval = defaultPrintDebugInterval
   398  	}
   399  
   400  	if fp.KillRountinesInterval == 0 {
   401  		fp.KillRountinesInterval = defaultKillRountinesInterval
   402  	}
   403  
   404  	if fp.CleanKillQueueInterval == 0 {
   405  		fp.CleanKillQueueInterval = defaultCleanKillQueueInterval
   406  	}
   407  }
   408  
   409  func (fp *FrontendParameters) SetMaxMessageSize(size uint64) {
   410  	fp.MaxMessageSize = size
   411  }
   412  
   413  func (fp *FrontendParameters) SetLogAndVersion(log *logutil.LogConfig, version string) {
   414  	fp.MoVersion = version
   415  }
   416  
   417  func (fp *FrontendParameters) GetUnixSocketAddress() string {
   418  	if fp.UnixSocketAddress == "" {
   419  		return ""
   420  	}
   421  	ok, err := isFileExist(fp.UnixSocketAddress)
   422  	if err != nil || ok {
   423  		return ""
   424  	}
   425  
   426  	canCreate := func() string {
   427  		f, err := os.Create(fp.UnixSocketAddress)
   428  		if err != nil {
   429  			return ""
   430  		}
   431  		if err := f.Close(); err != nil {
   432  			panic(err)
   433  		}
   434  		if err := os.Remove(fp.UnixSocketAddress); err != nil {
   435  			panic(err)
   436  		}
   437  		return fp.UnixSocketAddress
   438  	}
   439  
   440  	rootPath := filepath.Dir(fp.UnixSocketAddress)
   441  	f, err := os.Open(rootPath)
   442  	if os.IsNotExist(err) {
   443  		err := os.MkdirAll(rootPath, 0755)
   444  		if err != nil {
   445  			return ""
   446  		}
   447  		return canCreate()
   448  	} else if err != nil {
   449  		return ""
   450  	}
   451  	if err := f.Close(); err != nil {
   452  		panic(err)
   453  	}
   454  	return canCreate()
   455  }
   456  
   457  func isFileExist(file string) (bool, error) {
   458  	f, err := os.Open(file)
   459  	if err == nil {
   460  		return true, f.Close()
   461  	}
   462  	if os.IsNotExist(err) {
   463  		return false, nil
   464  	}
   465  	return false, err
   466  }
   467  
   468  // ObservabilityParameters hold metric/trace switch
   469  type ObservabilityParameters struct {
   470  	// MoVersion, see SetDefaultValues
   471  	MoVersion string
   472  
   473  	// Host listening ip. normally same as FrontendParameters.Host
   474  	Host string `toml:"host" user_setting:"advanced"`
   475  
   476  	// StatusPort defines which port the mo status server (for metric etc.) listens on and clients connect to
   477  	// Start listen with EnableMetricToProm is true.
   478  	StatusPort int `toml:"statusPort" user_setting:"advanced"`
   479  
   480  	// EnableMetricToProm default is false. if true, metrics can be scraped through host:status/metrics endpoint
   481  	EnableMetricToProm bool `toml:"enableMetricToProm" user_setting:"advanced"`
   482  
   483  	// DisableMetric default is false. if false, enable metric at booting
   484  	DisableMetric bool `toml:"disableMetric" user_setting:"advanced"`
   485  
   486  	// DisableTrace default is false. if false, enable trace at booting
   487  	DisableTrace bool `toml:"disableTrace" user_setting:"advanced"`
   488  
   489  	// EnableTraceDebug default is false. With true, system will check all the children span is ended, which belong to the closing span.
   490  	EnableTraceDebug bool `toml:"enableTraceDebug"`
   491  
   492  	// TraceExportInterval default is 15s.
   493  	TraceExportInterval int `toml:"traceExportInterval"`
   494  
   495  	// LongQueryTime default is 0.0 sec. if 0.0f, record every query. Record with exec time longer than LongQueryTime.
   496  	LongQueryTime float64 `toml:"longQueryTime" user_setting:"advanced"`
   497  
   498  	// MetricExportInterval default is 15 sec.
   499  	MetricExportInterval int `toml:"metric-export-interval"`
   500  
   501  	// MetricGatherInterval default is 15 sec.
   502  	MetricGatherInterval int `toml:"metric-gather-interval"`
   503  
   504  	// MetricInternalGatherInterval default is 1 min, handle metric.SubSystemMO metric
   505  	MetricInternalGatherInterval toml.Duration `toml:"metric-internal-gather-interval"`
   506  
   507  	// MetricStorageUsageUpdateInterval, default: 15 min
   508  	MetricStorageUsageUpdateInterval toml.Duration `toml:"metricStorageUsageUpdateInterval"`
   509  
   510  	// MetricStorageUsageCheckNewInterval, default: 1 min
   511  	MetricStorageUsageCheckNewInterval toml.Duration `toml:"metricStorageUsageCheckNewInterval"`
   512  
   513  	// MergeCycle default: 300 sec (5 minutes).
   514  	// PS: only used while MO init.
   515  	MergeCycle toml.Duration `toml:"mergeCycle"`
   516  
   517  	// DisableSpan default: false. Disable span collection
   518  	DisableSpan bool `toml:"disableSpan"`
   519  
   520  	// DisableError default: false. Disable error collection
   521  	DisableError bool `toml:"disableError"`
   522  
   523  	// LongSpanTime default: 500 ms. Only record span, which duration >= LongSpanTime
   524  	LongSpanTime toml.Duration `toml:"longSpanTime"`
   525  
   526  	// SkipRunningStmt default: false. Skip status:Running entry while collect statement_info
   527  	SkipRunningStmt bool `toml:"skipRunningStmt"`
   528  
   529  	// If disabled, the logs will be written to files stored in s3
   530  	DisableSqlWriter bool `toml:"disableSqlWriter"`
   531  
   532  	// DisableStmtAggregation ctrl statement aggregation. If disabled, the statements will not be aggregated.
   533  	// If false, LongQueryTime is NO less than SelectAggrThreshold
   534  	DisableStmtAggregation bool `toml:"disableStmtAggregation"`
   535  
   536  	// Seconds to aggregate the statements
   537  	AggregationWindow toml.Duration `toml:"aggregationWindow"`
   538  
   539  	// SelectAggrThreshold Duration to filter statements for aggregation
   540  	SelectAggrThreshold toml.Duration `toml:"selectAggrThreshold"`
   541  
   542  	// Disable merge statements
   543  	EnableStmtMerge bool `toml:"enableStmtMerge"`
   544  
   545  	// LabelSelector
   546  	LabelSelector map[string]string `toml:"labelSelector"`
   547  
   548  	// estimate tcp network packet cost
   549  	TCPPacket bool `toml:"tcpPacket"`
   550  
   551  	// for cu calculation
   552  	CU   OBCUConfig `toml:"cu"`
   553  	CUv1 OBCUConfig `toml:"cu_v1"`
   554  
   555  	OBCollectorConfig
   556  }
   557  
   558  func NewObservabilityParameters() *ObservabilityParameters {
   559  	op := &ObservabilityParameters{
   560  		MoVersion:                          "",
   561  		Host:                               defaultHost,
   562  		StatusPort:                         defaultStatusPort,
   563  		EnableMetricToProm:                 false,
   564  		DisableMetric:                      false,
   565  		DisableTrace:                       false,
   566  		EnableTraceDebug:                   false,
   567  		TraceExportInterval:                defaultTraceExportInterval,
   568  		LongQueryTime:                      defaultLongQueryTime,
   569  		MetricExportInterval:               defaultMetricExportInterval,
   570  		MetricGatherInterval:               defaultMetricGatherInterval,
   571  		MetricInternalGatherInterval:       toml.Duration{},
   572  		MetricStorageUsageUpdateInterval:   toml.Duration{},
   573  		MetricStorageUsageCheckNewInterval: toml.Duration{},
   574  		MergeCycle:                         toml.Duration{},
   575  		DisableSpan:                        false,
   576  		DisableError:                       false,
   577  		LongSpanTime:                       toml.Duration{},
   578  		SkipRunningStmt:                    defaultSkipRunningStmt,
   579  		DisableSqlWriter:                   false,
   580  		DisableStmtAggregation:             false,
   581  		AggregationWindow:                  toml.Duration{},
   582  		SelectAggrThreshold:                toml.Duration{},
   583  		EnableStmtMerge:                    false,
   584  		LabelSelector:                      map[string]string{defaultLoggerLabelKey: defaultLoggerLabelVal}, /*role=logging_cn*/
   585  		TCPPacket:                          false,
   586  		CU:                                 *NewOBCUConfig(),
   587  		CUv1:                               *NewOBCUConfig(),
   588  		OBCollectorConfig:                  *NewOBCollectorConfig(),
   589  	}
   590  	op.MetricInternalGatherInterval.Duration = defaultMetricInternalGatherInterval
   591  	op.MetricStorageUsageUpdateInterval.Duration = defaultMetricUpdateStorageUsageInterval
   592  	op.MetricStorageUsageCheckNewInterval.Duration = defaultMetricStorageUsageCheckNewInterval
   593  	op.MergeCycle.Duration = defaultMergeCycle
   594  	op.LongSpanTime.Duration = defaultLongSpanTime
   595  	op.AggregationWindow.Duration = defaultAggregationWindow
   596  	op.SelectAggrThreshold.Duration = defaultSelectThreshold
   597  	return op
   598  }
   599  
   600  func (op *ObservabilityParameters) SetDefaultValues(version string) {
   601  	op.OBCollectorConfig.SetDefaultValues()
   602  	op.CU.SetDefaultValues()
   603  	op.CUv1.SetDefaultValues()
   604  
   605  	op.MoVersion = version
   606  
   607  	if op.Host == "" {
   608  		op.Host = defaultHost
   609  	}
   610  
   611  	if op.StatusPort == 0 {
   612  		op.StatusPort = defaultStatusPort
   613  	}
   614  
   615  	if op.TraceExportInterval <= 0 {
   616  		op.TraceExportInterval = defaultTraceExportInterval
   617  	}
   618  
   619  	if op.MetricExportInterval <= 0 {
   620  		op.MetricExportInterval = defaultMetricExportInterval
   621  	}
   622  
   623  	if op.MetricGatherInterval <= 0 {
   624  		op.MetricGatherInterval = defaultMetricGatherInterval
   625  	}
   626  
   627  	if op.MetricStorageUsageUpdateInterval.Duration <= 0 {
   628  		op.MetricStorageUsageUpdateInterval.Duration = defaultMetricUpdateStorageUsageInterval
   629  	}
   630  
   631  	if op.MetricStorageUsageCheckNewInterval.Duration <= 0 {
   632  		op.MetricStorageUsageCheckNewInterval.Duration = defaultMetricStorageUsageCheckNewInterval
   633  	}
   634  
   635  	if op.MergeCycle.Duration <= 0 {
   636  		op.MergeCycle.Duration = defaultMergeCycle
   637  	}
   638  
   639  	if op.LongSpanTime.Duration <= 0 {
   640  		op.LongSpanTime.Duration = defaultLongSpanTime
   641  	}
   642  
   643  	if op.AggregationWindow.Duration <= 0 {
   644  		op.AggregationWindow.Duration = defaultAggregationWindow
   645  	}
   646  
   647  	if op.SelectAggrThreshold.Duration <= 0 {
   648  		op.SelectAggrThreshold.Duration = defaultSelectThreshold
   649  	}
   650  
   651  	// this loop must after SelectAggrThreshold and DisableStmtAggregation
   652  	if !op.DisableStmtAggregation {
   653  		val := float64(op.SelectAggrThreshold.Duration) / float64(time.Second)
   654  		if op.LongQueryTime <= val {
   655  			op.LongQueryTime = val
   656  		}
   657  	}
   658  }
   659  
   660  type OBCollectorConfig struct {
   661  	ShowStatsInterval toml.Duration `toml:"showStatsInterval"`
   662  	// BufferCnt
   663  	BufferCnt  int32 `toml:"bufferCnt"`
   664  	BufferSize int64 `toml:"bufferSize"`
   665  	// Collector Worker
   666  
   667  	CollectorCntPercent int `toml:"collector_cnt_percent"`
   668  	GeneratorCntPercent int `toml:"generator_cnt_percent"`
   669  	ExporterCntPercent  int `toml:"exporter_cnt_percent"`
   670  }
   671  
   672  func NewOBCollectorConfig() *OBCollectorConfig {
   673  	cfg := &OBCollectorConfig{
   674  		ShowStatsInterval:   toml.Duration{},
   675  		BufferCnt:           defaultOBBufferCnt,
   676  		BufferSize:          defaultOBBufferSize,
   677  		CollectorCntPercent: defaultOBCollectorCntPercent,
   678  		GeneratorCntPercent: defaultOBGeneratorCntPercent,
   679  		ExporterCntPercent:  defaultOBExporterCntPercent,
   680  	}
   681  	cfg.ShowStatsInterval.Duration = defaultOBShowStatsInterval
   682  	return cfg
   683  }
   684  
   685  func (c *OBCollectorConfig) SetDefaultValues() {
   686  	if c.ShowStatsInterval.Duration == 0 {
   687  		c.ShowStatsInterval.Duration = defaultOBShowStatsInterval
   688  	}
   689  	if c.BufferCnt == 0 {
   690  		c.BufferCnt = defaultOBBufferCnt
   691  	}
   692  	if c.BufferSize == 0 {
   693  		c.BufferSize = defaultOBBufferSize
   694  	}
   695  	if c.CollectorCntPercent <= 0 {
   696  		c.CollectorCntPercent = defaultOBCollectorCntPercent
   697  	}
   698  	if c.GeneratorCntPercent <= 0 {
   699  		c.GeneratorCntPercent = defaultOBGeneratorCntPercent
   700  	}
   701  	if c.ExporterCntPercent <= 0 {
   702  		c.ExporterCntPercent = defaultOBExporterCntPercent
   703  	}
   704  }
   705  
   706  type OBCUConfig struct {
   707  	// cu unit
   708  	CUUnit float64 `toml:"cu_unit"`
   709  	// price
   710  	CpuPrice      float64 `toml:"cpu_price"`
   711  	MemPrice      float64 `toml:"mem_price"`
   712  	IoInPrice     float64 `toml:"io_in_price"`
   713  	IoOutPrice    float64 `toml:"io_out_price"`
   714  	TrafficPrice0 float64 `toml:"traffic_price_0"`
   715  	TrafficPrice1 float64 `toml:"traffic_price_1"`
   716  	TrafficPrice2 float64 `toml:"traffic_price_2"`
   717  }
   718  
   719  const CUUnitDefault = 1.002678e-06
   720  const CUCpuPriceDefault = 3.45e-14
   721  const CUMemPriceDefault = 4.56e-24
   722  const CUIOInPriceDefault = 5.67e-06
   723  const CUIOOutPriceDefault = 6.78e-06
   724  const CUTrafficPrice0Default = 7.89e-10
   725  const CUTrafficPrice1Default = 7.89e-10
   726  const CUTrafficPrice2Default = 7.89e-10
   727  
   728  func NewOBCUConfig() *OBCUConfig {
   729  	cfg := &OBCUConfig{
   730  		CUUnit:        CUUnitDefault,
   731  		CpuPrice:      CUCpuPriceDefault,
   732  		MemPrice:      CUMemPriceDefault,
   733  		IoInPrice:     CUIOInPriceDefault,
   734  		IoOutPrice:    CUIOOutPriceDefault,
   735  		TrafficPrice0: CUTrafficPrice0Default,
   736  		TrafficPrice1: CUTrafficPrice1Default,
   737  		TrafficPrice2: CUTrafficPrice2Default,
   738  	}
   739  	return cfg
   740  }
   741  
   742  func (c *OBCUConfig) SetDefaultValues() {
   743  	if c.CUUnit <= 0 {
   744  		c.CUUnit = CUUnitDefault
   745  	}
   746  	if c.CpuPrice <= 0 {
   747  		c.CpuPrice = CUCpuPriceDefault
   748  	}
   749  	if c.MemPrice <= 0 {
   750  		c.MemPrice = CUMemPriceDefault
   751  	}
   752  	if c.IoInPrice <= 0 {
   753  		c.IoInPrice = CUIOInPriceDefault
   754  	}
   755  	if c.IoOutPrice <= 0 {
   756  		c.IoOutPrice = CUIOOutPriceDefault
   757  	}
   758  	if c.TrafficPrice0 <= 0 {
   759  		c.TrafficPrice0 = CUTrafficPrice0Default
   760  	}
   761  	if c.TrafficPrice1 <= 0 {
   762  		c.TrafficPrice1 = CUTrafficPrice1Default
   763  	}
   764  	if c.TrafficPrice2 <= 0 {
   765  		c.TrafficPrice2 = CUTrafficPrice2Default
   766  	}
   767  }
   768  
   769  type ParameterUnit struct {
   770  	SV *FrontendParameters
   771  
   772  	//Storage Engine
   773  	StorageEngine engine.Engine
   774  
   775  	//TxnClient
   776  	TxnClient client.TxnClient
   777  
   778  	//Cluster Nodes
   779  	ClusterNodes engine.Nodes
   780  
   781  	// FileService
   782  	FileService fileservice.FileService
   783  
   784  	// LockService instance
   785  	LockService lockservice.LockService
   786  
   787  	// QueryClient instance
   788  	QueryClient qclient.QueryClient
   789  
   790  	UdfService udf.Service
   791  
   792  	// HAKeeper client, which is used to get connection ID
   793  	// from HAKeeper currently.
   794  	HAKeeperClient logservice.CNHAKeeperClient
   795  
   796  	TaskService taskservice.TaskService
   797  }
   798  
   799  func NewParameterUnit(
   800  	sv *FrontendParameters,
   801  	storageEngine engine.Engine,
   802  	txnClient client.TxnClient,
   803  	clusterNodes engine.Nodes,
   804  ) *ParameterUnit {
   805  	return &ParameterUnit{
   806  		SV:            sv,
   807  		StorageEngine: storageEngine,
   808  		TxnClient:     txnClient,
   809  		ClusterNodes:  clusterNodes,
   810  	}
   811  }
   812  
   813  // GetParameterUnit gets the configuration from the context.
   814  func GetParameterUnit(ctx context.Context) *ParameterUnit {
   815  	pu := ctx.Value(ParameterUnitKey).(*ParameterUnit)
   816  	if pu == nil {
   817  		panic("parameter unit is invalid")
   818  	}
   819  	return pu
   820  }