self.sorted_devices

in cookbooks/fb_storage/libraries/storage.rb [512:621]


    def self.sorted_devices(node, maintenance_disks)
      if node['fb_storage']['_ordered_disks']
        return node['fb_storage']['_ordered_disks']
      end

      self._handle_custom_device_order_method(node)

      prev = load_previous_disk_order

      disk_to_slot_mapping = {}
      if node['fb'] && node['fb']['fbjbod'] &&
          !node['fb']['fbjbod']['shelves'].keys.length.zero?
        shelves = node['fb']['fbjbod']['shelves'].keys.sort
        shelves.each do |shelf|
          node['fb']['fbjbod']['shelves'][shelf].
            each_with_index do |drive, drive_index|
              disk_to_slot_mapping[drive] = {
                'disk' => drive_index,
                'shelf' => shelf,
              }
            end
        end
      end
      disk_to_scsi_mapping = {}
      node['scsi']&.each do |id, info|
        disk_to_scsi_mapping[info['device']] = id
      end

      unsorted_devs = Set.new(
        FB::Storage.eligible_devices(node) +
        maintenance_disks.map { |x| ::File.basename(x) },
      )

      
      
      devices_to_skip = self.devices_to_skip(node)
      prev&.keep_if do |disk|
        if devices_to_skip.include?(disk)
          Chef::Log.warn('fb_storage: previous ordering includes now skipped ' +
            " disk: #{disk}")
          false
        else
          true
        end
      end

      
      
      if prev && unsorted_devs == Set.new(prev)
        Chef::Log.debug(
          'fb_storage: Using previous disk ordering from cache',
        )
        node.default['fb_storage']['_ordered_disks'] = prev
        return prev
      end

      devs = unsorted_devs.to_a.sort do |a, b|
        fa = "/dev/#{a}"
        fb = "/dev/#{b}"
        
        
        if !disk_to_slot_mapping[fa] && disk_to_slot_mapping[fb]
          Chef::Log.debug(
            "fb_storage: #{a} is not jbod, #{b} is, #{a} sorts first",
          )
          -1
        elsif disk_to_slot_mapping[fa] && !disk_to_slot_mapping[fb]
          
          Chef::Log.debug(
            "fb_storage: #{a} is jbod, #{b} is not, #{b} sorts first",
          )
          1
        elsif disk_to_slot_mapping[fa] && disk_to_slot_mapping[fb]
          
          Chef::Log.debug(
            "fb_storage: #{a} and #{b} are both jbod " +
              'sorting by slot number',
          )
          sort_disk_shelves(disk_to_slot_mapping[fa],
                            disk_to_slot_mapping[fb])

        else
          
          
          
          Chef::Log.debug(
            "fb_storage: #{a} and #{b} are neither jbod " +
              'sorting by length, alphanumeric',
          )
          block_device_sort(fa, fb, disk_to_scsi_mapping)
        end
      end

      if prev
        devs = calculate_updated_order(prev, devs)
      end

      if prev != devs && !devs.empty?
        if can_use_dev_id?(node)
          version = 2
        else
          version = 1
        end
        write_out_disk_order(devs, version)
      end

      node.default['fb_storage']['_ordered_disks'] = devs
      devs
    end