@@ -137,6 +137,114 @@ func TestElectNewPrimary(t *testing.T) {
137
137
},
138
138
errContains : nil ,
139
139
},
140
+ {
141
+ name : "more advanced replica has an unknown replication lag" ,
142
+ tmc : & chooseNewPrimaryTestTMClient {
143
+ // zone1-101 is behind zone1-102 bug zone1-102 has an unknown replication lag, hence picking zone1-101
144
+ replicationStatuses : map [string ]* replicationdatapb.Status {
145
+ "zone1-0000000101" : {
146
+ Position : "MySQL56/3E11FA47-71CA-11E1-9E33-C80AA9429562:1" ,
147
+ ReplicationLagSeconds : 10 ,
148
+ },
149
+ "zone1-0000000102" : {
150
+ Position : "MySQL56/3E11FA47-71CA-11E1-9E33-C80AA9429562:1-5" ,
151
+ ReplicationLagUnknown : true ,
152
+ },
153
+ },
154
+ },
155
+ tolerableReplLag : 50 * time .Second ,
156
+ shardInfo : topo .NewShardInfo ("testkeyspace" , "-" , & topodatapb.Shard {
157
+ PrimaryAlias : & topodatapb.TabletAlias {
158
+ Cell : "zone1" ,
159
+ Uid : 100 ,
160
+ },
161
+ }, nil ),
162
+ tabletMap : map [string ]* topo.TabletInfo {
163
+ "primary" : {
164
+ Tablet : & topodatapb.Tablet {
165
+ Alias : & topodatapb.TabletAlias {
166
+ Cell : "zone1" ,
167
+ Uid : 100 ,
168
+ },
169
+ Type : topodatapb .TabletType_PRIMARY ,
170
+ },
171
+ },
172
+ "replica1" : {
173
+ Tablet : & topodatapb.Tablet {
174
+ Alias : & topodatapb.TabletAlias {
175
+ Cell : "zone1" ,
176
+ Uid : 101 ,
177
+ },
178
+ Type : topodatapb .TabletType_REPLICA ,
179
+ },
180
+ },
181
+ "replica2" : {
182
+ Tablet : & topodatapb.Tablet {
183
+ Alias : & topodatapb.TabletAlias {
184
+ Cell : "zone1" ,
185
+ Uid : 102 ,
186
+ },
187
+ Type : topodatapb .TabletType_REPLICA ,
188
+ },
189
+ },
190
+ },
191
+ avoidPrimaryAlias : & topodatapb.TabletAlias {
192
+ Cell : "zone1" ,
193
+ Uid : 0 ,
194
+ },
195
+ expected : & topodatapb.TabletAlias {
196
+ Cell : "zone1" ,
197
+ Uid : 101 ,
198
+ },
199
+ errContains : nil ,
200
+ },
201
+ {
202
+ name : "replica with unknown replication lag" ,
203
+ tmc : & chooseNewPrimaryTestTMClient {
204
+ // zone1-101 is behind zone1-102 bug zone1-102 has an unknown replication lag, hence picking zone1-101
205
+ replicationStatuses : map [string ]* replicationdatapb.Status {
206
+ "zone1-0000000101" : {
207
+ Position : "MySQL56/3E11FA47-71CA-11E1-9E33-C80AA9429562:1" ,
208
+ ReplicationLagUnknown : true ,
209
+ },
210
+ },
211
+ },
212
+ tolerableReplLag : 50 * time .Second ,
213
+ shardInfo : topo .NewShardInfo ("testkeyspace" , "-" , & topodatapb.Shard {
214
+ PrimaryAlias : & topodatapb.TabletAlias {
215
+ Cell : "zone1" ,
216
+ Uid : 100 ,
217
+ },
218
+ }, nil ),
219
+ tabletMap : map [string ]* topo.TabletInfo {
220
+ "primary" : {
221
+ Tablet : & topodatapb.Tablet {
222
+ Alias : & topodatapb.TabletAlias {
223
+ Cell : "zone1" ,
224
+ Uid : 100 ,
225
+ },
226
+ Type : topodatapb .TabletType_PRIMARY ,
227
+ },
228
+ },
229
+ "replica1" : {
230
+ Tablet : & topodatapb.Tablet {
231
+ Alias : & topodatapb.TabletAlias {
232
+ Cell : "zone1" ,
233
+ Uid : 101 ,
234
+ },
235
+ Type : topodatapb .TabletType_REPLICA ,
236
+ },
237
+ },
238
+ },
239
+ avoidPrimaryAlias : & topodatapb.TabletAlias {
240
+ Cell : "zone1" ,
241
+ Uid : 0 ,
242
+ },
243
+ expected : nil ,
244
+ errContains : []string {
245
+ "zone1-0000000101 position known but unknown replication status" ,
246
+ },
247
+ },
140
248
{
141
249
name : "new primary alias provided - no tolerable replication lag" ,
142
250
tolerableReplLag : 0 ,
@@ -751,12 +859,13 @@ func TestFindPositionForTablet(t *testing.T) {
751
859
ctx := context .Background ()
752
860
logger := logutil .NewMemoryLogger ()
753
861
tests := []struct {
754
- name string
755
- tmc * testutil.TabletManagerClient
756
- tablet * topodatapb.Tablet
757
- expectedPosition string
758
- expectedLag time.Duration
759
- expectedErr string
862
+ name string
863
+ tmc * testutil.TabletManagerClient
864
+ tablet * topodatapb.Tablet
865
+ expectedPosition string
866
+ expectedLag time.Duration
867
+ expectedErr string
868
+ expectedUnknownReplLag bool
760
869
}{
761
870
{
762
871
name : "executed gtid set" ,
@@ -846,12 +955,36 @@ func TestFindPositionForTablet(t *testing.T) {
846
955
},
847
956
},
848
957
expectedErr : `parse error: unknown GTIDSet flavor ""` ,
958
+ }, {
959
+ name : "unknown replication lag" ,
960
+ tmc : & testutil.TabletManagerClient {
961
+ ReplicationStatusResults : map [string ]struct {
962
+ Position * replicationdatapb.Status
963
+ Error error
964
+ }{
965
+ "zone1-0000000100" : {
966
+ Position : & replicationdatapb.Status {
967
+ RelayLogPosition : "MySQL56/3e11fa47-71ca-11e1-9e33-c80aa9429562:1-5" ,
968
+ ReplicationLagUnknown : true ,
969
+ },
970
+ },
971
+ },
972
+ },
973
+ tablet : & topodatapb.Tablet {
974
+ Alias : & topodatapb.TabletAlias {
975
+ Cell : "zone1" ,
976
+ Uid : 100 ,
977
+ },
978
+ },
979
+ expectedLag : 0 ,
980
+ expectedPosition : "MySQL56/3e11fa47-71ca-11e1-9e33-c80aa9429562:1-5" ,
981
+ expectedUnknownReplLag : true ,
849
982
},
850
983
}
851
984
852
985
for _ , test := range tests {
853
986
t .Run (test .name , func (t * testing.T ) {
854
- pos , lag , err := findPositionAndLagForTablet (ctx , test .tablet , logger , test .tmc , 10 * time .Second )
987
+ pos , lag , replUnknown , err := findPositionAndLagForTablet (ctx , test .tablet , logger , test .tmc , 10 * time .Second )
855
988
if test .expectedErr != "" {
856
989
require .EqualError (t , err , test .expectedErr )
857
990
return
@@ -860,6 +993,7 @@ func TestFindPositionForTablet(t *testing.T) {
860
993
posString := replication .EncodePosition (pos )
861
994
require .Equal (t , test .expectedPosition , posString )
862
995
require .Equal (t , test .expectedLag , lag )
996
+ require .Equal (t , test .expectedUnknownReplLag , replUnknown )
863
997
})
864
998
}
865
999
}
0 commit comments