@@ -139,6 +139,114 @@ func TestElectNewPrimary(t *testing.T) {
139
139
},
140
140
errContains : nil ,
141
141
},
142
+ {
143
+ name : "more advanced replica has an unknown replication lag" ,
144
+ tmc : & chooseNewPrimaryTestTMClient {
145
+ // zone1-101 is behind zone1-102 bug zone1-102 has an unknown replication lag, hence picking zone1-101
146
+ replicationStatuses : map [string ]* replicationdatapb.Status {
147
+ "zone1-0000000101" : {
148
+ Position : "MySQL56/3E11FA47-71CA-11E1-9E33-C80AA9429562:1" ,
149
+ ReplicationLagSeconds : 10 ,
150
+ },
151
+ "zone1-0000000102" : {
152
+ Position : "MySQL56/3E11FA47-71CA-11E1-9E33-C80AA9429562:1-5" ,
153
+ ReplicationLagUnknown : true ,
154
+ },
155
+ },
156
+ },
157
+ tolerableReplLag : 50 * time .Second ,
158
+ shardInfo : topo .NewShardInfo ("testkeyspace" , "-" , & topodatapb.Shard {
159
+ PrimaryAlias : & topodatapb.TabletAlias {
160
+ Cell : "zone1" ,
161
+ Uid : 100 ,
162
+ },
163
+ }, nil ),
164
+ tabletMap : map [string ]* topo.TabletInfo {
165
+ "primary" : {
166
+ Tablet : & topodatapb.Tablet {
167
+ Alias : & topodatapb.TabletAlias {
168
+ Cell : "zone1" ,
169
+ Uid : 100 ,
170
+ },
171
+ Type : topodatapb .TabletType_PRIMARY ,
172
+ },
173
+ },
174
+ "replica1" : {
175
+ Tablet : & topodatapb.Tablet {
176
+ Alias : & topodatapb.TabletAlias {
177
+ Cell : "zone1" ,
178
+ Uid : 101 ,
179
+ },
180
+ Type : topodatapb .TabletType_REPLICA ,
181
+ },
182
+ },
183
+ "replica2" : {
184
+ Tablet : & topodatapb.Tablet {
185
+ Alias : & topodatapb.TabletAlias {
186
+ Cell : "zone1" ,
187
+ Uid : 102 ,
188
+ },
189
+ Type : topodatapb .TabletType_REPLICA ,
190
+ },
191
+ },
192
+ },
193
+ avoidPrimaryAlias : & topodatapb.TabletAlias {
194
+ Cell : "zone1" ,
195
+ Uid : 0 ,
196
+ },
197
+ expected : & topodatapb.TabletAlias {
198
+ Cell : "zone1" ,
199
+ Uid : 101 ,
200
+ },
201
+ errContains : nil ,
202
+ },
203
+ {
204
+ name : "replica with unknown replication lag" ,
205
+ tmc : & chooseNewPrimaryTestTMClient {
206
+ // zone1-101 is behind zone1-102 bug zone1-102 has an unknown replication lag, hence picking zone1-101
207
+ replicationStatuses : map [string ]* replicationdatapb.Status {
208
+ "zone1-0000000101" : {
209
+ Position : "MySQL56/3E11FA47-71CA-11E1-9E33-C80AA9429562:1" ,
210
+ ReplicationLagUnknown : true ,
211
+ },
212
+ },
213
+ },
214
+ tolerableReplLag : 50 * time .Second ,
215
+ shardInfo : topo .NewShardInfo ("testkeyspace" , "-" , & topodatapb.Shard {
216
+ PrimaryAlias : & topodatapb.TabletAlias {
217
+ Cell : "zone1" ,
218
+ Uid : 100 ,
219
+ },
220
+ }, nil ),
221
+ tabletMap : map [string ]* topo.TabletInfo {
222
+ "primary" : {
223
+ Tablet : & topodatapb.Tablet {
224
+ Alias : & topodatapb.TabletAlias {
225
+ Cell : "zone1" ,
226
+ Uid : 100 ,
227
+ },
228
+ Type : topodatapb .TabletType_PRIMARY ,
229
+ },
230
+ },
231
+ "replica1" : {
232
+ Tablet : & topodatapb.Tablet {
233
+ Alias : & topodatapb.TabletAlias {
234
+ Cell : "zone1" ,
235
+ Uid : 101 ,
236
+ },
237
+ Type : topodatapb .TabletType_REPLICA ,
238
+ },
239
+ },
240
+ },
241
+ avoidPrimaryAlias : & topodatapb.TabletAlias {
242
+ Cell : "zone1" ,
243
+ Uid : 0 ,
244
+ },
245
+ expected : nil ,
246
+ errContains : []string {
247
+ "zone1-0000000101 position known but unknown replication status" ,
248
+ },
249
+ },
142
250
{
143
251
name : "new primary alias provided - no tolerable replication lag" ,
144
252
tolerableReplLag : 0 ,
@@ -881,12 +989,13 @@ func TestFindPositionForTablet(t *testing.T) {
881
989
ctx := context .Background ()
882
990
logger := logutil .NewMemoryLogger ()
883
991
tests := []struct {
884
- name string
885
- tmc * testutil.TabletManagerClient
886
- tablet * topodatapb.Tablet
887
- expectedPosition string
888
- expectedLag time.Duration
889
- expectedErr string
992
+ name string
993
+ tmc * testutil.TabletManagerClient
994
+ tablet * topodatapb.Tablet
995
+ expectedPosition string
996
+ expectedLag time.Duration
997
+ expectedErr string
998
+ expectedUnknownReplLag bool
890
999
}{
891
1000
{
892
1001
name : "executed gtid set" ,
@@ -976,12 +1085,36 @@ func TestFindPositionForTablet(t *testing.T) {
976
1085
},
977
1086
},
978
1087
expectedErr : `parse error: unknown GTIDSet flavor ""` ,
1088
+ }, {
1089
+ name : "unknown replication lag" ,
1090
+ tmc : & testutil.TabletManagerClient {
1091
+ ReplicationStatusResults : map [string ]struct {
1092
+ Position * replicationdatapb.Status
1093
+ Error error
1094
+ }{
1095
+ "zone1-0000000100" : {
1096
+ Position : & replicationdatapb.Status {
1097
+ RelayLogPosition : "MySQL56/3e11fa47-71ca-11e1-9e33-c80aa9429562:1-5" ,
1098
+ ReplicationLagUnknown : true ,
1099
+ },
1100
+ },
1101
+ },
1102
+ },
1103
+ tablet : & topodatapb.Tablet {
1104
+ Alias : & topodatapb.TabletAlias {
1105
+ Cell : "zone1" ,
1106
+ Uid : 100 ,
1107
+ },
1108
+ },
1109
+ expectedLag : 0 ,
1110
+ expectedPosition : "MySQL56/3e11fa47-71ca-11e1-9e33-c80aa9429562:1-5" ,
1111
+ expectedUnknownReplLag : true ,
979
1112
},
980
1113
}
981
1114
982
1115
for _ , test := range tests {
983
1116
t .Run (test .name , func (t * testing.T ) {
984
- pos , lag , err := findPositionAndLagForTablet (ctx , test .tablet , logger , test .tmc , 10 * time .Second )
1117
+ pos , lag , replUnknown , err := findPositionAndLagForTablet (ctx , test .tablet , logger , test .tmc , 10 * time .Second )
985
1118
if test .expectedErr != "" {
986
1119
require .EqualError (t , err , test .expectedErr )
987
1120
return
@@ -990,6 +1123,7 @@ func TestFindPositionForTablet(t *testing.T) {
990
1123
posString := replication .EncodePosition (pos )
991
1124
require .Equal (t , test .expectedPosition , posString )
992
1125
require .Equal (t , test .expectedLag , lag )
1126
+ require .Equal (t , test .expectedUnknownReplLag , replUnknown )
993
1127
})
994
1128
}
995
1129
}
0 commit comments