github.com/matrixorigin/matrixone@v0.7.0/pkg/logservice/config_test.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 logservice
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/stretchr/testify/assert"
    21  	"github.com/stretchr/testify/require"
    22  
    23  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    24  	"github.com/matrixorigin/matrixone/pkg/hakeeper"
    25  )
    26  
    27  func getTestConfig() Config {
    28  	c := Config{
    29  		UUID:                 "uuid",
    30  		DeploymentID:         100,
    31  		DataDir:              "/mydata/dir",
    32  		ServiceAddress:       "localhost:9000",
    33  		ServiceListenAddress: "localhost:9000",
    34  		RaftAddress:          "localhost:9001",
    35  		RaftListenAddress:    "localhost:9001",
    36  		GossipAddress:        "localhost:9002",
    37  		GossipListenAddress:  "localhost:9002",
    38  		GossipSeedAddresses:  []string{"localhost:9002"},
    39  	}
    40  	c.Fill()
    41  	return c
    42  }
    43  
    44  func TestSplitAddress(t *testing.T) {
    45  	tests := []struct {
    46  		input  string
    47  		output []string
    48  	}{
    49  		{"", []string{}},
    50  		{" ; ", []string{}},
    51  		{" ;; ", []string{}},
    52  		{";", []string{}},
    53  		{"localhost:1000;localhost:1001", []string{"localhost:1000", "localhost:1001"}},
    54  		{" localhost:1000 ; localhost:1001\t\n", []string{"localhost:1000", "localhost:1001"}},
    55  		{"localhost:1000 \n", []string{"localhost:1000"}},
    56  		{"localhost:1000;", []string{"localhost:1000"}},
    57  		{";localhost:1000", []string{"localhost:1000"}},
    58  	}
    59  
    60  	for _, tt := range tests {
    61  		v := splitAddresses(tt.input)
    62  		assert.Equal(t, tt.output, v)
    63  	}
    64  }
    65  
    66  func TestGetInitHAKeeperMembers(t *testing.T) {
    67  	cfg1 := Config{}
    68  	cfg1.BootstrapConfig.InitHAKeeperMembers = []string{" 131072 :9c4dccb4-4d3c-41f8-b482-5251dc7a41bf "}
    69  	result, err := cfg1.GetInitHAKeeperMembers()
    70  	require.NoError(t, err)
    71  	assert.Equal(t, 1, len(result))
    72  	v, ok := result[131072]
    73  	assert.True(t, ok)
    74  	assert.Equal(t, "9c4dccb4-4d3c-41f8-b482-5251dc7a41bf", v)
    75  
    76  	cfg2 := Config{}
    77  	cfg2.BootstrapConfig.InitHAKeeperMembers = []string{"131072:9c4dccb4-4d3c-41f8-b482-5251dc7a41bf", "131073:9c4dccb4-4d3c-41f8-b482-5251dc7a41be"}
    78  	result, err = cfg2.GetInitHAKeeperMembers()
    79  	require.NoError(t, err)
    80  	assert.Equal(t, 2, len(result))
    81  	v1, ok1 := result[131072]
    82  	v2, ok2 := result[131073]
    83  	assert.True(t, ok1)
    84  	assert.True(t, ok2)
    85  	assert.Equal(t, "9c4dccb4-4d3c-41f8-b482-5251dc7a41bf", v1)
    86  	assert.Equal(t, "9c4dccb4-4d3c-41f8-b482-5251dc7a41be", v2)
    87  
    88  	tests := [][]string{
    89  		{"131071:9c4dccb4-4d3c-41f8-b482-5251dc7a41bf"},
    90  		{"262144:9c4dccb4-4d3c-41f8-b482-5251dc7a41bf"},
    91  		{"262145:9c4dccb4-4d3c-41f8-b482-5251dc7a41bf"},
    92  		{"131072:9c4dccb4-4d3c-41f8-b482-5251dc7a41b"},
    93  		{"131072:9c4dccb4-4d3c-41f8-b482-5251dc7a41bf", ""},
    94  		{"131072:9c4dccb4-4d3c-41f8-b482-5251dc7a41bf", "1:1"},
    95  	}
    96  
    97  	for _, v := range tests {
    98  		cfg := Config{}
    99  		cfg.BootstrapConfig.InitHAKeeperMembers = v
   100  		_, err := cfg.GetInitHAKeeperMembers()
   101  		assert.True(t, moerr.IsMoErrCode(err, moerr.ErrBadConfig))
   102  	}
   103  }
   104  
   105  func TestConfigCanBeValidated(t *testing.T) {
   106  	c := getTestConfig()
   107  	assert.NoError(t, c.Validate())
   108  
   109  	c1 := c
   110  	c1.DeploymentID = 0
   111  	err := c1.Validate()
   112  	assert.True(t, moerr.IsMoErrCode(err, moerr.ErrBadConfig))
   113  
   114  	c2 := c
   115  	c2.ServiceAddress = ""
   116  	err = c2.Validate()
   117  	assert.True(t, moerr.IsMoErrCode(err, moerr.ErrBadConfig))
   118  
   119  	c3 := c
   120  	c3.RaftAddress = ""
   121  	err = c2.Validate()
   122  	assert.True(t, moerr.IsMoErrCode(err, moerr.ErrBadConfig))
   123  
   124  	c4 := c
   125  	c4.GossipAddress = ""
   126  	err = c4.Validate()
   127  	assert.True(t, moerr.IsMoErrCode(err, moerr.ErrBadConfig))
   128  
   129  	c5 := c
   130  	c5.GossipSeedAddresses = []string{}
   131  	err = c5.Validate()
   132  	assert.True(t, moerr.IsMoErrCode(err, moerr.ErrBadConfig))
   133  
   134  	c6 := c
   135  	c6.GossipProbeInterval.Duration = 0
   136  	err = c6.Validate()
   137  	assert.True(t, moerr.IsMoErrCode(err, moerr.ErrBadConfig))
   138  }
   139  
   140  func TestBootstrapConfigCanBeValidated(t *testing.T) {
   141  	c := getTestConfig()
   142  	assert.NoError(t, c.Validate())
   143  
   144  	c.BootstrapConfig.BootstrapCluster = true
   145  	c.BootstrapConfig.NumOfLogShards = 3
   146  	c.BootstrapConfig.NumOfDNShards = 3
   147  	c.BootstrapConfig.NumOfLogShardReplicas = 1
   148  	c.BootstrapConfig.InitHAKeeperMembers = []string{"131072:9c4dccb4-4d3c-41f8-b482-5251dc7a41bf"}
   149  	assert.NoError(t, c.Validate())
   150  
   151  	c1 := c
   152  	c1.BootstrapConfig.NumOfLogShards = 0
   153  	err := c1.Validate()
   154  	assert.True(t, moerr.IsMoErrCode(err, moerr.ErrBadConfig))
   155  
   156  	c2 := c
   157  	c2.BootstrapConfig.NumOfDNShards = 0
   158  	err = c2.Validate()
   159  	assert.True(t, moerr.IsMoErrCode(err, moerr.ErrBadConfig))
   160  
   161  	c3 := c
   162  	c3.BootstrapConfig.NumOfDNShards = 2
   163  	err = c3.Validate()
   164  	assert.True(t, moerr.IsMoErrCode(err, moerr.ErrBadConfig))
   165  
   166  	c4 := c
   167  	c4.BootstrapConfig.NumOfLogShardReplicas = 2
   168  	err = c4.Validate()
   169  	assert.True(t, moerr.IsMoErrCode(err, moerr.ErrBadConfig))
   170  }
   171  
   172  func TestFillConfig(t *testing.T) {
   173  	c := Config{}
   174  	c.Fill()
   175  
   176  	assert.Equal(t, uint64(0), c.DeploymentID)
   177  	assert.Equal(t, defaultDataDir, c.DataDir)
   178  	assert.Equal(t, defaultSnapshotExportDir, c.SnapshotExportDir)
   179  	assert.Equal(t, defaultMaxExportedSnapshot, c.MaxExportedSnapshot)
   180  	assert.Equal(t, defaultServiceAddress, c.ServiceAddress)
   181  	assert.Equal(t, defaultServiceAddress, c.ServiceListenAddress)
   182  	assert.Equal(t, defaultRaftAddress, c.RaftAddress)
   183  	assert.Equal(t, defaultRaftAddress, c.RaftListenAddress)
   184  	assert.Equal(t, defaultGossipAddress, c.GossipAddress)
   185  	assert.Equal(t, defaultGossipAddress, c.GossipListenAddress)
   186  	assert.Equal(t, 0, len(c.GossipSeedAddresses))
   187  	assert.Equal(t, hakeeper.DefaultTickPerSecond, c.HAKeeperConfig.TickPerSecond)
   188  	assert.Equal(t, hakeeper.DefaultLogStoreTimeout, c.HAKeeperConfig.LogStoreTimeout.Duration)
   189  	assert.Equal(t, hakeeper.DefaultDNStoreTimeout, c.HAKeeperConfig.DNStoreTimeout.Duration)
   190  }
   191  
   192  func TestListenAddressCanBeFilled(t *testing.T) {
   193  	cfg := Config{
   194  		DeploymentID:        1,
   195  		RTTMillisecond:      5,
   196  		DataDir:             "data-1",
   197  		ServiceAddress:      "127.0.0.1:9002",
   198  		RaftAddress:         "127.0.0.1:9000",
   199  		GossipAddress:       "127.0.0.1:9001",
   200  		GossipSeedAddresses: []string{"127.0.0.1:9011"},
   201  	}
   202  	cfg.Fill()
   203  	assert.Equal(t, cfg.ServiceAddress, cfg.ServiceListenAddress)
   204  	assert.Equal(t, cfg.RaftAddress, cfg.RaftListenAddress)
   205  	assert.Equal(t, cfg.GossipAddress, cfg.GossipListenAddress)
   206  }
   207  
   208  func TestClientConfigValidate(t *testing.T) {
   209  	tests := []struct {
   210  		cfg ClientConfig
   211  		ok  bool
   212  	}{
   213  		{
   214  			ClientConfig{}, false,
   215  		},
   216  		{
   217  			ClientConfig{LogShardID: 1, DiscoveryAddress: "localhost:9090"}, false,
   218  		},
   219  		{
   220  			ClientConfig{LogShardID: 1, DiscoveryAddress: "localhost:9090"}, false,
   221  		},
   222  		{
   223  			ClientConfig{LogShardID: 1, DNReplicaID: 100, DiscoveryAddress: "localhost:9090"}, true,
   224  		},
   225  		{
   226  			ClientConfig{
   227  				LogShardID:       1,
   228  				DNReplicaID:      100,
   229  				DiscoveryAddress: "localhost:9090",
   230  				ServiceAddresses: []string{"localhost:9091"},
   231  			},
   232  			true,
   233  		},
   234  	}
   235  
   236  	for _, tt := range tests {
   237  		err := tt.cfg.Validate()
   238  		if tt.ok {
   239  			assert.NoError(t, err)
   240  		} else {
   241  			assert.True(t, moerr.IsMoErrCode(err, moerr.ErrBadConfig))
   242  		}
   243  	}
   244  }
   245  
   246  func TestHAKeeperClientConfigValidate(t *testing.T) {
   247  	tests := []struct {
   248  		cfg HAKeeperClientConfig
   249  		ok  bool
   250  	}{
   251  		{
   252  			HAKeeperClientConfig{}, false,
   253  		},
   254  		{
   255  			HAKeeperClientConfig{DiscoveryAddress: "localhost:9090"}, true,
   256  		},
   257  		{
   258  			HAKeeperClientConfig{ServiceAddresses: []string{"localhost:9090"}}, true,
   259  		},
   260  		{
   261  			HAKeeperClientConfig{
   262  				DiscoveryAddress: "localhost:9091",
   263  				ServiceAddresses: []string{"localhost:9090"},
   264  			}, true,
   265  		},
   266  	}
   267  
   268  	for _, tt := range tests {
   269  		err := tt.cfg.Validate()
   270  		if tt.ok {
   271  			assert.NoError(t, err)
   272  		} else {
   273  			assert.True(t, moerr.IsMoErrCode(err, moerr.ErrBadConfig))
   274  		}
   275  	}
   276  }