vitess.io/vitess@v0.16.2/go/vt/vtctl/reparentutil/durability_test.go (about) 1 /* 2 Copyright 2021 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package reparentutil 18 19 import ( 20 "testing" 21 22 "github.com/stretchr/testify/assert" 23 "github.com/stretchr/testify/require" 24 25 topodatapb "vitess.io/vitess/go/vt/proto/topodata" 26 "vitess.io/vitess/go/vt/topo/topoproto" 27 28 "vitess.io/vitess/go/vt/vtctl/reparentutil/promotionrule" 29 ) 30 31 func TestDurabilityNone(t *testing.T) { 32 durability, err := GetDurabilityPolicy("none") 33 require.NoError(t, err) 34 35 promoteRule := PromotionRule(durability, &topodatapb.Tablet{ 36 Alias: &topodatapb.TabletAlias{ 37 Cell: "cell1", 38 Uid: 100, 39 }, 40 Type: topodatapb.TabletType_PRIMARY, 41 }) 42 assert.Equal(t, promotionrule.Neutral, promoteRule) 43 assert.Equal(t, promotionrule.MustNot, PromotionRule(durability, nil)) 44 45 promoteRule = PromotionRule(durability, &topodatapb.Tablet{ 46 Alias: &topodatapb.TabletAlias{ 47 Cell: "cell1", 48 Uid: 100, 49 }, 50 Type: topodatapb.TabletType_REPLICA, 51 }) 52 assert.Equal(t, promotionrule.Neutral, promoteRule) 53 54 promoteRule = PromotionRule(durability, &topodatapb.Tablet{ 55 Alias: &topodatapb.TabletAlias{ 56 Cell: "cell1", 57 Uid: 100, 58 }, 59 Type: topodatapb.TabletType_RDONLY, 60 }) 61 assert.Equal(t, promotionrule.MustNot, promoteRule) 62 63 promoteRule = PromotionRule(durability, &topodatapb.Tablet{ 64 Alias: &topodatapb.TabletAlias{ 65 Cell: "cell1", 66 Uid: 100, 67 }, 68 Type: topodatapb.TabletType_SPARE, 69 }) 70 assert.Equal(t, promotionrule.MustNot, promoteRule) 71 assert.Equal(t, 0, SemiSyncAckers(durability, nil)) 72 assert.Equal(t, false, IsReplicaSemiSync(durability, nil, nil)) 73 } 74 75 func TestDurabilitySemiSync(t *testing.T) { 76 durability, err := GetDurabilityPolicy("semi_sync") 77 require.NoError(t, err) 78 79 promoteRule := PromotionRule(durability, &topodatapb.Tablet{ 80 Alias: &topodatapb.TabletAlias{ 81 Cell: "cell1", 82 Uid: 100, 83 }, 84 Type: topodatapb.TabletType_PRIMARY, 85 }) 86 assert.Equal(t, promotionrule.Neutral, promoteRule) 87 88 promoteRule = PromotionRule(durability, &topodatapb.Tablet{ 89 Alias: &topodatapb.TabletAlias{ 90 Cell: "cell1", 91 Uid: 100, 92 }, 93 Type: topodatapb.TabletType_REPLICA, 94 }) 95 assert.Equal(t, promotionrule.Neutral, promoteRule) 96 97 promoteRule = PromotionRule(durability, &topodatapb.Tablet{ 98 Alias: &topodatapb.TabletAlias{ 99 Cell: "cell1", 100 Uid: 100, 101 }, 102 Type: topodatapb.TabletType_RDONLY, 103 }) 104 assert.Equal(t, promotionrule.MustNot, promoteRule) 105 106 promoteRule = PromotionRule(durability, &topodatapb.Tablet{ 107 Alias: &topodatapb.TabletAlias{ 108 Cell: "cell1", 109 Uid: 100, 110 }, 111 Type: topodatapb.TabletType_SPARE, 112 }) 113 assert.Equal(t, promotionrule.MustNot, promoteRule) 114 assert.Equal(t, 1, SemiSyncAckers(durability, nil)) 115 assert.Equal(t, true, IsReplicaSemiSync(durability, &topodatapb.Tablet{ 116 Alias: &topodatapb.TabletAlias{ 117 Cell: "cell1", 118 Uid: 101, 119 }, 120 Type: topodatapb.TabletType_PRIMARY, 121 }, &topodatapb.Tablet{ 122 Alias: &topodatapb.TabletAlias{ 123 Cell: "cell1", 124 Uid: 100, 125 }, 126 Type: topodatapb.TabletType_REPLICA, 127 })) 128 assert.Equal(t, false, IsReplicaSemiSync(durability, &topodatapb.Tablet{ 129 Alias: &topodatapb.TabletAlias{ 130 Cell: "cell1", 131 Uid: 101, 132 }, 133 Type: topodatapb.TabletType_PRIMARY, 134 }, &topodatapb.Tablet{ 135 Alias: &topodatapb.TabletAlias{ 136 Cell: "cell1", 137 Uid: 100, 138 }, 139 Type: topodatapb.TabletType_EXPERIMENTAL, 140 })) 141 } 142 143 func TestDurabilityCrossCell(t *testing.T) { 144 durability, err := GetDurabilityPolicy("cross_cell") 145 require.NoError(t, err) 146 147 promoteRule := PromotionRule(durability, &topodatapb.Tablet{ 148 Alias: &topodatapb.TabletAlias{ 149 Cell: "cell1", 150 Uid: 100, 151 }, 152 Type: topodatapb.TabletType_PRIMARY, 153 }) 154 assert.Equal(t, promotionrule.Neutral, promoteRule) 155 156 promoteRule = PromotionRule(durability, &topodatapb.Tablet{ 157 Alias: &topodatapb.TabletAlias{ 158 Cell: "cell1", 159 Uid: 100, 160 }, 161 Type: topodatapb.TabletType_REPLICA, 162 }) 163 assert.Equal(t, promotionrule.Neutral, promoteRule) 164 165 promoteRule = PromotionRule(durability, &topodatapb.Tablet{ 166 Alias: &topodatapb.TabletAlias{ 167 Cell: "cell1", 168 Uid: 100, 169 }, 170 Type: topodatapb.TabletType_RDONLY, 171 }) 172 assert.Equal(t, promotionrule.MustNot, promoteRule) 173 174 promoteRule = PromotionRule(durability, &topodatapb.Tablet{ 175 Alias: &topodatapb.TabletAlias{ 176 Cell: "cell1", 177 Uid: 100, 178 }, 179 Type: topodatapb.TabletType_SPARE, 180 }) 181 assert.Equal(t, promotionrule.MustNot, promoteRule) 182 assert.Equal(t, 1, SemiSyncAckers(durability, nil)) 183 assert.Equal(t, false, IsReplicaSemiSync(durability, &topodatapb.Tablet{ 184 Type: topodatapb.TabletType_PRIMARY, 185 Alias: &topodatapb.TabletAlias{ 186 Cell: "cell1", 187 }, 188 }, &topodatapb.Tablet{ 189 Type: topodatapb.TabletType_REPLICA, 190 Alias: &topodatapb.TabletAlias{ 191 Cell: "cell1", 192 }, 193 })) 194 assert.Equal(t, true, IsReplicaSemiSync(durability, &topodatapb.Tablet{ 195 Type: topodatapb.TabletType_PRIMARY, 196 Alias: &topodatapb.TabletAlias{ 197 Cell: "cell1", 198 }, 199 }, &topodatapb.Tablet{ 200 Type: topodatapb.TabletType_REPLICA, 201 Alias: &topodatapb.TabletAlias{ 202 Cell: "cell2", 203 }, 204 })) 205 assert.Equal(t, false, IsReplicaSemiSync(durability, &topodatapb.Tablet{ 206 Type: topodatapb.TabletType_PRIMARY, 207 Alias: &topodatapb.TabletAlias{ 208 Cell: "cell1", 209 }, 210 }, &topodatapb.Tablet{ 211 Type: topodatapb.TabletType_EXPERIMENTAL, 212 Alias: &topodatapb.TabletAlias{ 213 Cell: "cell2", 214 }, 215 })) 216 } 217 218 func TestError(t *testing.T) { 219 _, err := GetDurabilityPolicy("unknown") 220 assert.EqualError(t, err, "durability policy unknown not found") 221 } 222 223 func TestDurabilityTest(t *testing.T) { 224 cellName := "zone2" 225 durabilityRules := &durabilityTest{} 226 227 testcases := []struct { 228 tablet *topodatapb.Tablet 229 promotionRule promotionrule.CandidatePromotionRule 230 }{ 231 { 232 tablet: &topodatapb.Tablet{ 233 Alias: &topodatapb.TabletAlias{ 234 Cell: cellName, 235 Uid: 0, 236 }, 237 Type: topodatapb.TabletType_SPARE, 238 }, 239 promotionRule: promotionrule.MustNot, 240 }, { 241 tablet: &topodatapb.Tablet{ 242 Alias: &topodatapb.TabletAlias{ 243 Cell: cellName, 244 Uid: 200, 245 }, 246 }, 247 promotionRule: promotionrule.Prefer, 248 }, { 249 tablet: &topodatapb.Tablet{ 250 Alias: &topodatapb.TabletAlias{ 251 Cell: cellName, 252 Uid: 2, 253 }, 254 Type: topodatapb.TabletType_PRIMARY, 255 }, 256 promotionRule: promotionrule.Neutral, 257 }, { 258 tablet: &topodatapb.Tablet{ 259 Alias: &topodatapb.TabletAlias{ 260 Cell: cellName, 261 Uid: 4, 262 }, 263 Type: topodatapb.TabletType_BACKUP, 264 }, 265 promotionRule: promotionrule.MustNot, 266 }, 267 } 268 269 for _, testcase := range testcases { 270 t.Run(topoproto.TabletAliasString(testcase.tablet.Alias), func(t *testing.T) { 271 rule := durabilityRules.promotionRule(testcase.tablet) 272 assert.Equal(t, testcase.promotionRule, rule) 273 }) 274 } 275 }