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



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

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.



6
7
8
# File 'lib/polyrun/partition/constraints.rb', line 6

def pin_map
  @pin_map
end

#serial_globsObject (readonly)

Returns the value of attribute serial_globs.



6
7
8
# File 'lib/polyrun/partition/constraints.rb', line 6

def serial_globs
  @serial_globs
end

#serial_shardObject (readonly)

Returns the value of attribute serial_shard.



6
7
8
# File 'lib/polyrun/partition/constraints.rb', line 6

def serial_shard
  @serial_shard
end

Class Method Details

.from_hash(h, root: nil) ⇒ Object



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

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)


55
56
57
# File 'lib/polyrun/partition/constraints.rb', line 55

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.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/polyrun/partition/constraints.rb', line 34

def forced_shard_for(path)
  rel = path.to_s
  abs = File.expand_path(rel, @root)

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

    if match_pattern?(pattern.to_s, rel, abs)
      return shard
    end
  end

  @serial_globs.each do |g|
    if match_pattern?(g, rel, abs)
      return @serial_shard
    end
  end

  nil
end