volume_utils.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. package base
  2. import (
  3. "github.com/seaweedfs/seaweedfs/weed/admin/topology"
  4. )
  5. // FindVolumeDisk finds the disk ID where a specific volume is located on a given server.
  6. // Returns the disk ID and a boolean indicating whether the volume was found.
  7. // Uses O(1) indexed lookup for optimal performance on large clusters.
  8. //
  9. // This is a shared utility function used by multiple task detection algorithms
  10. // (balance, vacuum, etc.) to locate volumes efficiently.
  11. //
  12. // Example usage:
  13. //
  14. // // In balance task: find source disk for a volume that needs to be moved
  15. // sourceDisk, found := base.FindVolumeDisk(topology, volumeID, collection, sourceServer)
  16. //
  17. // // In vacuum task: find disk containing volume that needs cleanup
  18. // diskID, exists := base.FindVolumeDisk(topology, volumeID, collection, serverID)
  19. func FindVolumeDisk(activeTopology *topology.ActiveTopology, volumeID uint32, collection string, serverID string) (uint32, bool) {
  20. if activeTopology == nil {
  21. return 0, false
  22. }
  23. // Use the new O(1) indexed lookup for better performance
  24. locations := activeTopology.GetVolumeLocations(volumeID, collection)
  25. for _, loc := range locations {
  26. if loc.ServerID == serverID {
  27. return loc.DiskID, true
  28. }
  29. }
  30. // Volume not found on this server
  31. return 0, false
  32. }