@@ -191,34 +191,54 @@ func (r *Router) bucketSearchBatched(ctx context.Context, bucketIDToFind uint64)
191
191
// DiscoveryHandleBuckets arrange downloaded buckets to the route map so as they reference a given replicaset.
192
192
func (r * Router ) DiscoveryHandleBuckets (ctx context.Context , rs * Replicaset , buckets []uint64 ) {
193
193
routeMap := r .getRouteMap ()
194
- removedFrom := make (map [* Replicaset ]int )
194
+ removedFrom := make (map [string ]int )
195
+
196
+ var newRsName string
197
+ if rs != nil {
198
+ newRsName = rs .info .Name
199
+ }
195
200
196
201
for _ , bucketID := range buckets {
202
+ // We don't validate bucketID intentionally, because we can't return an error here using the current API.
203
+ // We can't silence this error too, the caller should know about invalid arguments.
204
+ // So the only way to inform the caller is to panic here,
205
+ // that should happen if bucketID is out of the array bounds.
206
+ // if bucketID < 1 || r.cfg.TotalBucketCount < bucketID {
207
+ // continue
208
+ // }
209
+
197
210
oldRs := routeMap [bucketID ].Swap (rs )
198
211
199
- if oldRs == rs {
200
- continue
212
+ var oldRsName string
213
+ if oldRs != nil {
214
+ oldRsName = oldRs .info .Name
201
215
}
202
216
203
- // NOTE: oldRs and rs might have the same name, we intentionally don't check this case to keep the logic simple
204
-
205
- // We don't check oldRs for nil here, because it's a valid key too (if rs == nil, it means removed from unknown buckets set)
206
- removedFrom [oldRs ]++
217
+ if oldRsName != newRsName {
218
+ // Something has changed.
219
+ // NOTE: We don't check oldRsName for empty string ("") here,
220
+ // because it's a valid key too (it means bucketID became known)
221
+ removedFrom [oldRsName ]++
222
+ }
207
223
}
208
224
209
- var addedToRs int
210
- for rs , removedFromRs := range removedFrom {
211
- addedToRs += removedFromRs
225
+ var addedToNewRsTotal int
226
+ for removedFromRsName , removedFromCount := range removedFrom {
227
+ addedToNewRsTotal += removedFromCount
212
228
213
- switch rs {
214
- case nil :
215
- r .log ().Debugf (ctx , "Added new %d buckets to the cluster map" , removedFromRs )
216
- default :
217
- r .log ().Debugf (ctx , "Removed %d buckets from replicaset %s " , removedFromRs , rs . info . Name )
229
+ if removedFromRsName == "" {
230
+ // newRsName cannot be an empty string here due to the previous for-loop above.
231
+ r .log ().Debugf (ctx , "Added new %d buckets to the cluster map" , removedFromCount )
232
+ } else {
233
+ r .log ().Debugf (ctx , "Removed %d buckets from replicaset '%s' " , removedFromCount , removedFromRsName )
218
234
}
219
235
}
220
236
221
- r .log ().Infof (ctx , "Added %d buckets to replicaset %s" , addedToRs , rs .info .Name )
237
+ if newRsName == "" {
238
+ r .log ().Infof (ctx , "Removed %d buckets from the cluster map" , addedToNewRsTotal )
239
+ } else {
240
+ r .log ().Infof (ctx , "Added %d buckets to replicaset '%s'" , addedToNewRsTotal , newRsName )
241
+ }
222
242
}
223
243
224
244
func (r * Router ) DiscoveryAllBuckets (ctx context.Context ) error {
0 commit comments