Browse Source

Add clarifying comments to vidMap helper functions

Added inline documentation to each helper function (addLocation, deleteLocation,
addEcLocation, deleteEcLocation) explaining the two-level locking strategy:

- RLock on vidMapLock prevents resetVidMap from swapping the pointer
- vidMap has internal locks that protect the actual map mutations
- This design provides optimal concurrency

The comments make it clear why RLock (not Lock) is correct and intentional,
preventing future confusion about the locking strategy.
pr-7412
chrislu 1 month ago
parent
commit
c238153a3b
  1. 10
      weed/wdclient/masterclient.go

10
weed/wdclient/masterclient.go

@ -128,12 +128,12 @@ func (mc *MasterClient) LookupVolumeIdsWithFallback(ctx context.Context, volumeI
// Check cache first and parse volume IDs once
vidStringToUint := make(map[string]uint32, len(volumeIds))
// Get stable pointer to vidMap with minimal lock hold time
mc.vidMapLock.RLock()
vm := mc.vidMap
mc.vidMapLock.RUnlock()
for _, vidString := range volumeIds {
vid, err := strconv.ParseUint(vidString, 10, 32)
if err != nil {
@ -167,7 +167,7 @@ func (mc *MasterClient) LookupVolumeIdsWithFallback(ctx context.Context, volumeI
mc.vidMapLock.RLock()
vm := mc.vidMap
mc.vidMapLock.RUnlock()
for _, vidString := range needsLookup {
vid := vidStringToUint[vidString] // Use pre-parsed value
if locations, found := vm.GetLocations(vid); found && len(locations) > 0 {
@ -560,24 +560,28 @@ func (mc *MasterClient) GetDataCenter() string {
// from swapping it during the operation. The actual map mutations are protected by
// vidMap's internal mutex, so RLock is sufficient here for better concurrency.
// addLocation adds a volume location. RLock prevents pointer swap; vidMap has internal locks for the mutation.
func (mc *MasterClient) addLocation(vid uint32, location Location) {
mc.vidMapLock.RLock()
defer mc.vidMapLock.RUnlock()
mc.vidMap.addLocation(vid, location)
}
// deleteLocation removes a volume location. RLock prevents pointer swap; vidMap has internal locks for the mutation.
func (mc *MasterClient) deleteLocation(vid uint32, location Location) {
mc.vidMapLock.RLock()
defer mc.vidMapLock.RUnlock()
mc.vidMap.deleteLocation(vid, location)
}
// addEcLocation adds an EC volume location. RLock prevents pointer swap; vidMap has internal locks for the mutation.
func (mc *MasterClient) addEcLocation(vid uint32, location Location) {
mc.vidMapLock.RLock()
defer mc.vidMapLock.RUnlock()
mc.vidMap.addEcLocation(vid, location)
}
// deleteEcLocation removes an EC volume location. RLock prevents pointer swap; vidMap has internal locks for the mutation.
func (mc *MasterClient) deleteEcLocation(vid uint32, location Location) {
mc.vidMapLock.RLock()
defer mc.vidMapLock.RUnlock()

Loading…
Cancel
Save