github.com/matrixorigin/matrixone@v0.7.0/pkg/tests/service/options.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 service
    16  
    17  import (
    18  	"time"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/cnservice"
    21  	"github.com/matrixorigin/matrixone/pkg/common/mpool"
    22  	"github.com/matrixorigin/matrixone/pkg/dnservice"
    23  	"github.com/matrixorigin/matrixone/pkg/hakeeper"
    24  	"github.com/matrixorigin/matrixone/pkg/logutil"
    25  	"go.uber.org/zap"
    26  	"go.uber.org/zap/zapcore"
    27  )
    28  
    29  const (
    30  	// default cluster initial information
    31  	defaultDNServiceNum  = 1
    32  	defaultDNShardNum    = 1
    33  	defaultLogServiceNum = 1
    34  	defaultLogShardNum   = 1
    35  	defaultLogReplicaNum = 1
    36  	defaultCNServiceNum  = 1
    37  	defaultCNShardNum    = 1
    38  
    39  	// default configuration for services
    40  	defaultHostAddr    = "127.0.0.1"
    41  	defaultRootDataDir = "/tmp/mo/tests"
    42  
    43  	// default configuration for dn service
    44  	defaultDNStorage = dnservice.StorageTAE
    45  	defaultCNEngine  = cnservice.EngineDistributedTAE
    46  
    47  	// default configuration for log service
    48  	defaultGossipSeedNum = 1
    49  	defaultHAKeeperNum   = 1
    50  	// we only use 3 seeds at most.
    51  	maxGossipSeedNum = 3
    52  	maxHAKeeperNum   = 3
    53  
    54  	// default hakeeper configuration
    55  	defaultTickPerSecond   = 10
    56  	defaultLogStoreTimeout = 4 * time.Second
    57  	defaultDNStoreTimeout  = 3 * time.Second
    58  	defaultCNStoreTimeout  = 3 * time.Second
    59  	defaultCheckInterval   = 1 * time.Second
    60  
    61  	// default heartbeat configuration
    62  	defaultLogHeartbeatInterval = 1 * time.Second
    63  	defaultDNHeartbeatInterval  = 1 * time.Second
    64  	defaultCNHeartbeatInterval  = 1 * time.Second
    65  
    66  	// default logtail push server configuration
    67  	defaultRpcMaxMessageSize          = 16 * mpool.KB
    68  	defaultRpcPayloadCopyBufferSize   = 16 * mpool.KB
    69  	defaultLogtailCollectInterval     = 50 * time.Millisecond
    70  	defaultLogtailResponseSendTimeout = 10 * time.Second
    71  	defaultMaxLogtailFetchFailure     = 5
    72  )
    73  
    74  // Options are params for creating test cluster.
    75  type Options struct {
    76  	hostAddr    string
    77  	rootDataDir string
    78  	keepData    bool
    79  	logger      *zap.Logger
    80  
    81  	initial struct {
    82  		dnServiceNum  int
    83  		logServiceNum int
    84  		cnServiceNum  int
    85  		dnShardNum    uint64
    86  		logShardNum   uint64
    87  		cnShardNum    uint64
    88  		logReplicaNum uint64
    89  	}
    90  
    91  	heartbeat struct {
    92  		dn  time.Duration
    93  		cn  time.Duration
    94  		log time.Duration
    95  	}
    96  
    97  	storage struct {
    98  		dnStorage dnservice.StorageType
    99  		cnEngine  cnservice.EngineType
   100  	}
   101  
   102  	hakeeper struct {
   103  		tickPerSecond   int
   104  		checkInterval   time.Duration
   105  		logStoreTimeout time.Duration
   106  		dnStoreTimeout  time.Duration
   107  		cnStoreTimeout  time.Duration
   108  	}
   109  
   110  	logtailPushServer struct {
   111  		rpcMaxMessageSize          int64
   112  		rpcPayloadCopyBufferSize   int64
   113  		logtailCollectInterval     time.Duration
   114  		logtailResponseSendTimeout time.Duration
   115  		maxLogtailFetchFailure     int
   116  	}
   117  }
   118  
   119  // DefaultOptions sets a list of recommended options.
   120  func DefaultOptions() Options {
   121  	opt := Options{}
   122  	opt.validate()
   123  	return opt
   124  }
   125  
   126  // validate check and fill empty configurations.
   127  func (opt *Options) validate() {
   128  	if opt.hostAddr == "" {
   129  		opt.hostAddr = defaultHostAddr
   130  	}
   131  	if opt.rootDataDir == "" {
   132  		opt.rootDataDir = defaultRootDataDir
   133  	}
   134  	if opt.initial.dnServiceNum <= 0 {
   135  		opt.initial.dnServiceNum = defaultDNServiceNum
   136  	}
   137  	if opt.initial.logServiceNum <= 0 {
   138  		opt.initial.logServiceNum = defaultLogServiceNum
   139  	}
   140  	if opt.initial.cnServiceNum <= 0 {
   141  		opt.initial.cnServiceNum = defaultCNServiceNum
   142  	}
   143  	if opt.initial.dnShardNum <= 0 {
   144  		opt.initial.dnShardNum = defaultDNShardNum
   145  	}
   146  	if opt.initial.logShardNum <= 0 {
   147  		opt.initial.logShardNum = defaultLogShardNum
   148  	}
   149  	if opt.initial.cnShardNum <= 0 {
   150  		opt.initial.cnShardNum = defaultCNShardNum
   151  	}
   152  	if opt.initial.logReplicaNum <= 0 {
   153  		opt.initial.logReplicaNum = defaultLogReplicaNum
   154  	}
   155  	if opt.storage.dnStorage == "" {
   156  		opt.storage.dnStorage = defaultDNStorage
   157  	}
   158  	if opt.storage.cnEngine == "" {
   159  		opt.storage.cnEngine = defaultCNEngine
   160  	}
   161  
   162  	// hakeeper configuration
   163  	if opt.hakeeper.tickPerSecond == 0 {
   164  		opt.hakeeper.tickPerSecond = defaultTickPerSecond
   165  	}
   166  	if opt.hakeeper.logStoreTimeout == 0 {
   167  		opt.hakeeper.logStoreTimeout = defaultLogStoreTimeout
   168  	}
   169  	if opt.hakeeper.dnStoreTimeout == 0 {
   170  		opt.hakeeper.dnStoreTimeout = defaultDNStoreTimeout
   171  	}
   172  	if opt.hakeeper.cnStoreTimeout == 0 {
   173  		opt.hakeeper.cnStoreTimeout = defaultCNStoreTimeout
   174  	}
   175  	if opt.hakeeper.checkInterval == 0 {
   176  		opt.hakeeper.checkInterval = defaultCheckInterval
   177  	}
   178  
   179  	// heartbeat configuration
   180  	if opt.heartbeat.log == 0 {
   181  		opt.heartbeat.log = defaultLogHeartbeatInterval
   182  	}
   183  	if opt.heartbeat.dn == 0 {
   184  		opt.heartbeat.dn = defaultDNHeartbeatInterval
   185  	}
   186  	if opt.heartbeat.cn == 0 {
   187  		opt.heartbeat.cn = defaultCNHeartbeatInterval
   188  	}
   189  	if opt.logger == nil {
   190  		opt.logger = logutil.GetGlobalLogger()
   191  	}
   192  
   193  	// logtail push server configuration
   194  	if opt.logtailPushServer.rpcMaxMessageSize <= 0 {
   195  		opt.logtailPushServer.rpcMaxMessageSize = defaultRpcMaxMessageSize
   196  	}
   197  	if opt.logtailPushServer.rpcPayloadCopyBufferSize <= 0 {
   198  		opt.logtailPushServer.rpcPayloadCopyBufferSize = defaultRpcPayloadCopyBufferSize
   199  	}
   200  	if opt.logtailPushServer.logtailCollectInterval <= 0 {
   201  		opt.logtailPushServer.logtailCollectInterval = defaultLogtailCollectInterval
   202  	}
   203  	if opt.logtailPushServer.logtailResponseSendTimeout <= 0 {
   204  		opt.logtailPushServer.logtailResponseSendTimeout = defaultLogtailResponseSendTimeout
   205  	}
   206  	if opt.logtailPushServer.maxLogtailFetchFailure <= 0 {
   207  		opt.logtailPushServer.maxLogtailFetchFailure = defaultMaxLogtailFetchFailure
   208  	}
   209  }
   210  
   211  // BuildHAKeeperConfig returns hakeeper.Config
   212  //
   213  // We could check timeout for dn/log store via hakeeper.Config.
   214  func (opt Options) BuildHAKeeperConfig() hakeeper.Config {
   215  	return hakeeper.Config{
   216  		TickPerSecond:   opt.hakeeper.tickPerSecond,
   217  		LogStoreTimeout: opt.hakeeper.logStoreTimeout,
   218  		DNStoreTimeout:  opt.hakeeper.dnStoreTimeout,
   219  		CNStoreTimeout:  opt.hakeeper.cnStoreTimeout,
   220  	}
   221  }
   222  
   223  // WithDNServiceNum sets dn service number in the cluster.
   224  func (opt Options) WithDNServiceNum(num int) Options {
   225  	opt.initial.dnServiceNum = num
   226  	return opt
   227  }
   228  
   229  // WithLogServiceNum sets log service number in the cluster.
   230  func (opt Options) WithLogServiceNum(num int) Options {
   231  	opt.initial.logServiceNum = num
   232  	return opt
   233  }
   234  
   235  func (opt Options) WithCNServiceNum(num int) Options {
   236  	opt.initial.cnServiceNum = num
   237  	return opt
   238  }
   239  
   240  // WithLogShardNum sets log shard number in the cluster.
   241  func (opt Options) WithLogShardNum(num uint64) Options {
   242  	opt.initial.logShardNum = num
   243  	return opt
   244  }
   245  
   246  // WithDNShardNum sets dn shard number in the cluster.
   247  func (opt Options) WithDNShardNum(num uint64) Options {
   248  	opt.initial.dnShardNum = num
   249  	return opt
   250  }
   251  
   252  func (opt Options) WithCNShardNum(num uint64) Options {
   253  	opt.initial.cnShardNum = num
   254  	return opt
   255  }
   256  
   257  // WithLogReplicaNum sets log replica number for the cluster.
   258  func (opt Options) WithLogReplicaNum(num uint64) Options {
   259  	opt.initial.logReplicaNum = num
   260  	return opt
   261  }
   262  
   263  // WithRootDataDir sets root for service data directory.
   264  func (opt Options) WithRootDataDir(root string) Options {
   265  	opt.rootDataDir = root
   266  	return opt
   267  }
   268  
   269  // WithDNUseTAEStorage sets dn transaction use tae storage.
   270  func (opt Options) WithDNUseTAEStorage() Options {
   271  	opt.storage.dnStorage = dnservice.StorageTAE
   272  	return opt
   273  }
   274  
   275  // WithDNUseMEMStorage sets dn transaction use mem storage.
   276  func (opt Options) WithDNUseMEMStorage() Options {
   277  	opt.storage.dnStorage = dnservice.StorageMEM
   278  	return opt
   279  }
   280  
   281  // WithCNUseTAEEngine use tae engine as cn engine
   282  func (opt Options) WithCNUseTAEEngine() Options {
   283  	opt.storage.cnEngine = cnservice.EngineTAE
   284  	return opt
   285  }
   286  
   287  // WithCNUseDistributedTAEEngine use distributed tae engine as cn engine
   288  func (opt Options) WithCNUseDistributedTAEEngine() Options {
   289  	opt.storage.cnEngine = cnservice.EngineDistributedTAE
   290  	return opt
   291  }
   292  
   293  // WithCNUseMemoryEngine use memory engine as cn engine
   294  func (opt Options) WithCNUseMemoryEngine() Options {
   295  	opt.storage.cnEngine = cnservice.EngineDistributedTAE
   296  	return opt
   297  }
   298  
   299  // WithHostAddress sets host address for all services.
   300  func (opt Options) WithHostAddress(host string) Options {
   301  	opt.hostAddr = host
   302  	return opt
   303  }
   304  
   305  // WithLogLevel sets log level.
   306  func (opt Options) WithLogLevel(lvl zapcore.Level) Options {
   307  	opt.logger = logutil.GetPanicLoggerWithLevel(lvl)
   308  	return opt
   309  }
   310  
   311  // WithLogger sets logger.
   312  func (opt Options) WithLogger(logger *zap.Logger) Options {
   313  	opt.logger = logger
   314  	return opt
   315  }
   316  
   317  // WithHKTickPerSecond sets tick per second for hakeeper.
   318  func (opt Options) WithHKTickPerSecond(tick int) Options {
   319  	opt.hakeeper.tickPerSecond = tick
   320  	return opt
   321  }
   322  
   323  // WithHKLogStoreTimeout sets log store timeout for hakeeper.
   324  func (opt Options) WithHKLogStoreTimeout(timeout time.Duration) Options {
   325  	opt.hakeeper.logStoreTimeout = timeout
   326  	return opt
   327  }
   328  
   329  // WithHKDNStoreTimeout sets dn store timeout for hakeeper.
   330  func (opt Options) WithHKDNStoreTimeout(timeout time.Duration) Options {
   331  	opt.hakeeper.dnStoreTimeout = timeout
   332  	return opt
   333  }
   334  
   335  func (opt Options) WithHKCNStoreTimeout(timeout time.Duration) Options {
   336  	opt.hakeeper.cnStoreTimeout = timeout
   337  	return opt
   338  }
   339  
   340  // WithHKCheckInterval sets check interval for hakeeper.
   341  func (opt Options) WithHKCheckInterval(interval time.Duration) Options {
   342  	opt.hakeeper.checkInterval = interval
   343  	return opt
   344  }
   345  
   346  // WithDNHeartbeatInterval sets heartbeat interval fo dn service.
   347  func (opt Options) WithDNHeartbeatInterval(interval time.Duration) Options {
   348  	opt.heartbeat.dn = interval
   349  	return opt
   350  }
   351  
   352  // WithLogHeartbeatInterval sets heartbeat interval fo log service.
   353  func (opt Options) WithLogHeartbeatInterval(interval time.Duration) Options {
   354  	opt.heartbeat.log = interval
   355  	return opt
   356  }
   357  
   358  // WithCNHeartbeatInterval sets heartbeat interval fo cn service.
   359  func (opt Options) WithCNHeartbeatInterval(interval time.Duration) Options {
   360  	opt.heartbeat.cn = interval
   361  	return opt
   362  }
   363  
   364  // GetDNStorageType returns the storage type that the dnservice used
   365  func (opt Options) GetDNStorageType() dnservice.StorageType {
   366  	return opt.storage.dnStorage
   367  }
   368  
   369  // GetCNEngineType returns the engine type that the cnservice used
   370  func (opt Options) GetCNEngineType() dnservice.StorageType {
   371  	return opt.storage.dnStorage
   372  }
   373  
   374  // WithKeepData sets keep data after cluster closed.
   375  func (opt Options) WithKeepData() Options {
   376  	opt.keepData = true
   377  	return opt
   378  }
   379  
   380  // WithLogtailRpcMaxMessageSize sets max rpc message size for logtail push server.
   381  func (opt Options) WithLogtailRpcMaxMessageSize(size int64) Options {
   382  	opt.logtailPushServer.rpcMaxMessageSize = size
   383  	return opt
   384  }
   385  
   386  // WithLogtailRpcPayloadCopyBufferSize sets max rpc payload copy buffer size.
   387  func (opt Options) WithLogtailRpcPayloadCopyBufferSize(size int64) Options {
   388  	opt.logtailPushServer.rpcPayloadCopyBufferSize = size
   389  	return opt
   390  }
   391  
   392  // WithLogtailCollectInterval sets collection interval for logtail push server.
   393  func (opt Options) WithLogtailCollectInterval(interval time.Duration) Options {
   394  	opt.logtailPushServer.logtailCollectInterval = interval
   395  	return opt
   396  }
   397  
   398  // WithLogtailResponseSendTimeout sets response send timeout for logtail push server.
   399  func (opt Options) WithLogtailResponseSendTimeout(timeout time.Duration) Options {
   400  	opt.logtailPushServer.logtailResponseSendTimeout = timeout
   401  	return opt
   402  }
   403  
   404  // WithLogtailMaxFetchFailure sets max failure times when collecting logtail.
   405  func (opt Options) WithLogtailMaxFetchFailure(max int) Options {
   406  	opt.logtailPushServer.maxLogtailFetchFailure = max
   407  	return opt
   408  }
   409  
   410  // gossipSeedNum calculates the count of gossip seed.
   411  //
   412  // Select gossip addresses of the first 3 log services.
   413  // If the number of log services was less than 3,
   414  // then just return the number.
   415  func gossipSeedNum(logServiceNum int) int {
   416  	if logServiceNum < defaultGossipSeedNum {
   417  		return defaultGossipSeedNum
   418  	}
   419  	if logServiceNum > maxGossipSeedNum {
   420  		return maxGossipSeedNum
   421  	}
   422  	return logServiceNum
   423  }
   424  
   425  // haKeeperNum calculates the count of hakeeper replica.
   426  //
   427  // Select the first 3 log services to start hakeeper replica.
   428  // If the number of log services was less than 3,
   429  // then just return the number.
   430  func haKeeperNum(haKeeperNum int) int {
   431  	if haKeeperNum < defaultHAKeeperNum {
   432  		return defaultHAKeeperNum
   433  	}
   434  	if haKeeperNum > maxHAKeeperNum {
   435  		return maxHAKeeperNum
   436  	}
   437  	return haKeeperNum
   438  }