Class: Polyrun::Partition::Constraints

Inherits:
Object
  • Object
show all
Defined in:
lib/polyrun/partition/constraints.rb

Overview

Hard constraints for plan assignment (spec_queue.md): pins, serial globs. Pins win over serial_glob. First matching pin glob wins.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pin_map: {}, serial_globs: [], serial_shard: 0, root: nil) ⇒ Constraints

Returns a new instance of Constraints.

Parameters:

  • pin_map (Hash{String=>Integer}) (defaults to: {})

    glob or exact path => shard index

  • serial_globs (Array<String>) (defaults to: [])

    fnmatch patterns forced to serial_shard (default 0) unless pinned

  • root (String) (defaults to: nil)

    project root for expanding relative paths



13
14
15
16
17
18
# File 'lib/polyrun/partition/constraints.rb', line 13

def initialize(pin_map: {}, serial_globs: [], serial_shard: 0, root: nil)
  @pin_map = pin_map.transform_keys(&:to_s).transform_values { |v| Integer(v) }
  @serial_globs = Array(serial_globs).map(&:to_s)
  @serial_shard = Integer(serial_shard)
  @root = root ? File.expand_path(root) : Dir.pwd
end

Instance Attribute Details

#pin_mapObject (readonly)

Returns the value of attribute pin_map.



8
9
10
# File 'lib/polyrun/partition/constraints.rb', line 8

def pin_map
  @pin_map
end

#serial_globsObject (readonly)

Returns the value of attribute serial_globs.



8
9
10
# File 'lib/polyrun/partition/constraints.rb', line 8

def serial_globs
  @serial_globs
end

#serial_shardObject (readonly)

Returns the value of attribute serial_shard.



8
9
10
# File 'lib/polyrun/partition/constraints.rb', line 8

def serial_shard
  @serial_shard
end

Class Method Details

.from_hash(h, root: nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/polyrun/partition/constraints.rb', line 20

def self.from_hash(h, root: nil)
  h = h.transform_keys(&:to_s) if h.is_a?(Hash)
  return new(root: root) unless h.is_a?(Hash)

  pins = h["pin"] || h["pins"] || {}
  serial = h["serial_glob"] || h["serial_globs"] || []
  serial_shard = h["serial_shard"] || 0
  new(
    pin_map: pins.is_a?(Hash) ? pins : {},
    serial_globs: serial.is_a?(Array) ? serial : [],
    serial_shard: serial_shard,
    root: root
  )
end

Instance Method Details

#any?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/polyrun/partition/constraints.rb', line 66

def any?
  @pin_map.any? || @serial_globs.any?
end

#forced_shard_for(path) ⇒ Object

Returns Integer shard index if constrained, or nil if free to place by LPT/HRW. For path:line items (example granularity), also matches pins/globs against the file path only.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/polyrun/partition/constraints.rb', line 37

def forced_shard_for(path)
  rel = path.to_s
  abs = File.expand_path(rel, @root)
  variants = [rel, abs]
  if (fp = TimingKeys.file_part_for_constraint(rel))
    variants << fp
    variants << File.expand_path(fp, @root)
  end
  variants.uniq!

  @pin_map.each do |pattern, shard|
    next if pattern.to_s.empty?

    variants.each do |rel_i|
      abs_i = File.expand_path(rel_i, @root)
      return shard if match_pattern?(pattern.to_s, rel_i, abs_i)
    end
  end

  @serial_globs.each do |g|
    variants.each do |rel_i|
      abs_i = File.expand_path(rel_i, @root)
      return @serial_shard if match_pattern?(g, rel_i, abs_i)
    end
  end

  nil
end