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