github.com/matrixorigin/matrixone@v1.2.0/pkg/tests/service/network_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 service 16 17 import ( 18 "testing" 19 20 "github.com/stretchr/testify/require" 21 ) 22 23 func TestServiceAddress(t *testing.T) { 24 logServiceNum := 3 25 tnServiceNum := 2 26 cnServiceNum := 2 27 28 address := newServiceAddresses(t, logServiceNum, tnServiceNum, cnServiceNum, "127.0.0.1") 29 address.assertTNService() 30 address.assertLogService() 31 address.assertCnService() 32 33 for i := 0; i < tnServiceNum; i++ { 34 addrList := address.listTnServiceAddresses(i) 35 // 1 address for every tn service now 36 require.Equal(t, 2, len(addrList)) 37 } 38 // valid tn index: 0, 1 39 // invalid tn index: 2 40 addrList := address.listTnServiceAddresses(2) 41 require.Equal(t, 0, len(addrList)) 42 43 for i := 0; i < logServiceNum; i++ { 44 addrList := address.listLogServiceAddresses(i) 45 // 3 addresses for every log service now 46 require.Equal(t, 3, len(addrList)) 47 } 48 // valid tn index: 0, 1, 2 49 // invalid tn index: 3 50 addrList = address.listLogServiceAddresses(3) 51 require.Equal(t, 0, len(addrList)) 52 53 for i := 0; i < cnServiceNum; i++ { 54 addrList := address.listCnServiceAddresses(i) 55 // 1 address for every cn service now 56 require.Equal(t, 1, len(addrList)) 57 } 58 // valid tn index: 0, 1 59 // invalid tn index: 2 60 addrList = address.listCnServiceAddresses(2) 61 require.Equal(t, 0, len(addrList)) 62 63 // ------------------------------ 64 // integrate with NetworkPartition 65 // ------------------------------ 66 tnIndex := uint32(1) 67 logIndex := uint32(2) 68 cnIndex := uint32(1) 69 partition1 := newNetworkPartition( 70 logServiceNum, []uint32{logIndex}, 71 tnServiceNum, []uint32{tnIndex}, 72 cnServiceNum, []uint32{cnIndex}, 73 ) 74 75 partition2 := remainingNetworkPartition(logServiceNum, tnServiceNum, cnServiceNum, partition1) 76 77 addrSets := address.buildPartitionAddressSets(partition1, partition2) 78 // there are 2 address sets corresponding with 2 partitions 79 require.Equal(t, 2, len(addrSets)) 80 // in partition 1, there are 1 tn service, 1 log service and 1 cn service. 81 require.Equal(t, 3+2+1, len(addrSets[0])) 82 // in partition 2, there are 1 tn service, 1 cn service and 2 log service. 83 require.Equal(t, 3*2+2+1, len(addrSets[1])) 84 85 // the first address set should contain the following addresses. 86 tnListenAddr := address.getTnListenAddress(int(tnIndex)) 87 require.True(t, addrSets[0].contains(tnListenAddr)) 88 tnServiceAddr := address.getTnLogtailAddress(int(tnIndex)) 89 require.True(t, addrSets[0].contains(tnServiceAddr)) 90 logListenAddr := address.getLogListenAddress(int(logIndex)) 91 require.True(t, addrSets[0].contains(logListenAddr)) 92 logRaftAddr := address.getLogListenAddress(int(logIndex)) 93 require.True(t, addrSets[0].contains(logRaftAddr)) 94 logGossipAddr := address.getLogListenAddress(int(logIndex)) 95 require.True(t, addrSets[0].contains(logGossipAddr)) 96 } 97 98 func TestGetTnListenAddress(t *testing.T) { 99 tnNum := 3 100 address := newServiceAddresses(t, 1, tnNum, 0, "127.0.0.1") 101 102 addr0 := address.getTnListenAddress(0) 103 addr1 := address.getTnListenAddress(1) 104 addr2 := address.getTnListenAddress(2) 105 addr3 := address.getTnListenAddress(3) 106 107 require.NotEqual(t, addr0, addr1) 108 require.NotEqual(t, addr0, addr2) 109 require.NotEqual(t, addr1, addr2) 110 require.Equal(t, "", addr3) 111 } 112 113 func TestGetTnServiceAddress(t *testing.T) { 114 tnNum := 3 115 address := newServiceAddresses(t, 1, tnNum, 0, "127.0.0.1") 116 117 addr0 := address.getTnLogtailAddress(0) 118 addr1 := address.getTnLogtailAddress(1) 119 addr2 := address.getTnLogtailAddress(2) 120 addr3 := address.getTnLogtailAddress(3) 121 122 require.NotEqual(t, addr0, addr1) 123 require.NotEqual(t, addr0, addr2) 124 require.NotEqual(t, addr1, addr2) 125 require.Equal(t, "", addr3) 126 } 127 128 func TestGetLogListenAddress(t *testing.T) { 129 logNum := 3 130 address := newServiceAddresses(t, logNum, 1, 0, "127.0.0.1") 131 132 addr0 := address.getLogListenAddress(0) 133 addr1 := address.getLogListenAddress(1) 134 addr2 := address.getLogListenAddress(2) 135 addr3 := address.getLogListenAddress(3) 136 137 require.NotEqual(t, addr0, addr1) 138 require.NotEqual(t, addr0, addr2) 139 require.NotEqual(t, addr1, addr2) 140 require.Equal(t, "", addr3) 141 } 142 143 func TestGetCnListenAddress(t *testing.T) { 144 cnNum := 3 145 address := newServiceAddresses(t, 1, 1, cnNum, "127.0.0.1") 146 147 addr0 := address.getCNListenAddress(0) 148 addr1 := address.getCNListenAddress(1) 149 addr2 := address.getCNListenAddress(2) 150 addr3 := address.getCNListenAddress(3) 151 152 require.NotEqual(t, addr0, addr1) 153 require.NotEqual(t, addr0, addr2) 154 require.NotEqual(t, addr1, addr2) 155 require.Equal(t, "", addr3) 156 } 157 158 func TestGetLogRaftAddress(t *testing.T) { 159 logNum := 3 160 address := newServiceAddresses(t, logNum, 1, 0, "127.0.0.1") 161 162 addr0 := address.getLogRaftAddress(0) 163 addr1 := address.getLogRaftAddress(1) 164 addr2 := address.getLogRaftAddress(2) 165 addr3 := address.getLogRaftAddress(3) 166 167 require.NotEqual(t, addr0, addr1) 168 require.NotEqual(t, addr0, addr2) 169 require.NotEqual(t, addr1, addr2) 170 require.Equal(t, "", addr3) 171 } 172 173 func TestGetLogGossipAddress(t *testing.T) { 174 logNum := 3 175 address := newServiceAddresses(t, logNum, 1, 0, "127.0.0.1") 176 177 addr0 := address.getLogGossipAddress(0) 178 addr1 := address.getLogGossipAddress(1) 179 addr2 := address.getLogGossipAddress(2) 180 addr3 := address.getLogGossipAddress(3) 181 182 require.NotEqual(t, addr0, addr1) 183 require.NotEqual(t, addr0, addr2) 184 require.NotEqual(t, addr1, addr2) 185 require.Equal(t, "", addr3) 186 } 187 188 func TestListHAKeeperListenAddresses(t *testing.T) { 189 logNum := 1 190 address := newServiceAddresses(t, logNum, 1, 0, "127.0.0.1") 191 addrs := address.listHAKeeperListenAddresses() 192 require.Equal(t, logNum, len(addrs)) 193 } 194 195 func TestGetLogGossipSeedAddresses(t *testing.T) { 196 logNum := 1 197 address := newServiceAddresses(t, logNum, 1, 0, "127.0.0.1") 198 addrs := address.getLogGossipSeedAddresses() 199 require.Equal(t, defaultGossipSeedNum, len(addrs)) 200 } 201 202 func TestPartition(t *testing.T) { 203 logServiceNum := 3 204 tnServiceNum := 2 205 cnServiceNum := 1 206 207 // normal condition 208 { 209 partition := newNetworkPartition( 210 logServiceNum, []uint32{1}, 211 tnServiceNum, []uint32{0, 1}, 212 cnServiceNum, []uint32{1}, 213 ) 214 require.Equal(t, []uint32{0, 1}, partition.ListTNServiceIndex()) 215 require.Equal(t, []uint32{1}, partition.ListLogServiceIndex()) 216 217 remaining := remainingNetworkPartition(logServiceNum, tnServiceNum, cnServiceNum, partition) 218 require.Nil(t, remaining.ListTNServiceIndex()) 219 require.Equal(t, []uint32{0, 2}, remaining.ListLogServiceIndex()) 220 221 require.Equal(t, uint64(0), remaining.tnIndexSet.GetCardinality()) 222 require.Equal(t, uint64(2), remaining.logIndexSet.GetCardinality()) 223 require.True(t, remaining.logIndexSet.Contains(0)) 224 require.True(t, remaining.logIndexSet.Contains(2)) 225 } 226 227 // valid tn index should be: 0, 1 228 // invoker specifies invalid tn index: 2, 3 229 { 230 partition := newNetworkPartition( 231 logServiceNum, nil, 232 tnServiceNum, []uint32{0, 2, 3}, 233 cnServiceNum, nil, 234 ) 235 require.Equal(t, []uint32{0}, partition.ListTNServiceIndex()) 236 require.Nil(t, partition.ListLogServiceIndex()) 237 238 remaining := remainingNetworkPartition(logServiceNum, tnServiceNum, cnServiceNum, partition) 239 require.Equal(t, []uint32{1}, remaining.ListTNServiceIndex()) 240 require.Equal(t, []uint32{0, 1, 2}, remaining.ListLogServiceIndex()) 241 242 require.Equal(t, uint64(1), remaining.tnIndexSet.GetCardinality()) 243 require.Equal(t, uint64(3), remaining.logIndexSet.GetCardinality()) 244 require.True(t, remaining.tnIndexSet.Contains(1)) 245 require.True(t, remaining.logIndexSet.Contains(0)) 246 require.True(t, remaining.logIndexSet.Contains(1)) 247 require.True(t, remaining.logIndexSet.Contains(2)) 248 } 249 }