diff options
Diffstat (limited to 'weed/worker/tasks/base/volume_utils.go')
| -rw-r--r-- | weed/worker/tasks/base/volume_utils.go | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/weed/worker/tasks/base/volume_utils.go b/weed/worker/tasks/base/volume_utils.go new file mode 100644 index 000000000..2aaf795b2 --- /dev/null +++ b/weed/worker/tasks/base/volume_utils.go @@ -0,0 +1,36 @@ +package base + +import ( + "github.com/seaweedfs/seaweedfs/weed/admin/topology" +) + +// FindVolumeDisk finds the disk ID where a specific volume is located on a given server. +// Returns the disk ID and a boolean indicating whether the volume was found. +// Uses O(1) indexed lookup for optimal performance on large clusters. +// +// This is a shared utility function used by multiple task detection algorithms +// (balance, vacuum, etc.) to locate volumes efficiently. +// +// Example usage: +// +// // In balance task: find source disk for a volume that needs to be moved +// sourceDisk, found := base.FindVolumeDisk(topology, volumeID, collection, sourceServer) +// +// // In vacuum task: find disk containing volume that needs cleanup +// diskID, exists := base.FindVolumeDisk(topology, volumeID, collection, serverID) +func FindVolumeDisk(activeTopology *topology.ActiveTopology, volumeID uint32, collection string, serverID string) (uint32, bool) { + if activeTopology == nil { + return 0, false + } + + // Use the new O(1) indexed lookup for better performance + locations := activeTopology.GetVolumeLocations(volumeID, collection) + for _, loc := range locations { + if loc.ServerID == serverID { + return loc.DiskID, true + } + } + + // Volume not found on this server + return 0, false +} |
