Skip to content

Commit 94e50db

Browse files
nurzhan-saktaganovKaymeKaydex
authored andcommitted
DiscoveryHandleBuckets: identify replicasets by name for logging purpose.
* handle rs=nil arguments properly too
1 parent c6dd605 commit 94e50db

File tree

2 files changed

+37
-16
lines changed

2 files changed

+37
-16
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ This is due to an inherently bad approach to controversial interfaces.
1616
BUG FIXES:
1717
* Router.Call bugfix: set destinationName := rs.info.Name if destination exists.
1818
* Router.Route bugfix: handle outdated *Replicaset object (resolve issue #11).
19+
* DiscoveryHandleBuckets: identify replicasets by name for logging purpose.
1920

2021
FEATURES:
2122
* Now when calling RemoveInstance, if an empty replicaset name is passed, the replicaset will be calculated automatically.

discovery.go

+36-16
Original file line numberDiff line numberDiff line change
@@ -191,34 +191,54 @@ func (r *Router) bucketSearchBatched(ctx context.Context, bucketIDToFind uint64)
191191
// DiscoveryHandleBuckets arrange downloaded buckets to the route map so as they reference a given replicaset.
192192
func (r *Router) DiscoveryHandleBuckets(ctx context.Context, rs *Replicaset, buckets []uint64) {
193193
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+
}
195200

196201
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+
197210
oldRs := routeMap[bucketID].Swap(rs)
198211

199-
if oldRs == rs {
200-
continue
212+
var oldRsName string
213+
if oldRs != nil {
214+
oldRsName = oldRs.info.Name
201215
}
202216

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+
}
207223
}
208224

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
212228

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)
218234
}
219235
}
220236

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+
}
222242
}
223243

224244
func (r *Router) DiscoveryAllBuckets(ctx context.Context) error {

0 commit comments

Comments
 (0)