Class: Puppet::Graph::RelationshipGraph Private

Inherits:
SimpleGraph show all
Defined in:
lib/puppet/graph/relationship_graph.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

The relationship graph is the final form of a puppet catalog in which all dependency edges are explicitly in the graph. This form of the catalog is used to traverse the graph in the order in which resources are managed.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from SimpleGraph

#add_edge, #adjacent, #clear, #dependencies, #dependents, #direct_dependencies_of, #direct_dependents_of, #directed?, #downstream_from_vertex, #each_edge, #edge?, #edges, #edges_between, #find_cycles_in_graph, #initialize_from_hash, #leaves, #matching_edges, #path_between, #paths_in_cycle, #remove_edge!, #report_cycles_in_graph, #reversal, #size, #stringify, #tarjan, #to_a, #to_data_hash, #to_dot, #to_dot_graph, #tree_from_vertex, #upstream_from_vertex, #vertex?, #vertices, #walk, #write_cycles_to_graph, #write_graph

Methods included from Util::PsychSupport

#encode_with, #init_with

Constructor Details

#initialize(prioritizer) ⇒ RelationshipGraph

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of RelationshipGraph.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/puppet/graph/relationship_graph.rb', line 12

def initialize(prioritizer)
  super()

  @prioritizer = prioritizer

  @ready = Puppet::Graph::RbTreeMap.new
  @generated = {}
  @done = {}
  @blockers = {}
  @providerless_types = []
end

Instance Attribute Details

#blockersObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



10
11
12
# File 'lib/puppet/graph/relationship_graph.rb', line 10

def blockers
  @blockers
end

Instance Method Details

#add_relationship(f, t, label = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



46
47
48
49
# File 'lib/puppet/graph/relationship_graph.rb', line 46

def add_relationship(f, t, label = nil)
  super(f, t, label)
  @ready.delete(@prioritizer.priority_of(t))
end

#add_vertex(vertex, priority = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



36
37
38
39
40
41
42
43
44
# File 'lib/puppet/graph/relationship_graph.rb', line 36

def add_vertex(vertex, priority = nil)
  super(vertex)

  if priority
    @prioritizer.record_priority_for(vertex, priority)
  else
    @prioritizer.generate_priority_for(vertex)
  end
end

#clear_blockersObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



80
81
82
# File 'lib/puppet/graph/relationship_graph.rb', line 80

def clear_blockers
  @blockers.clear
end

#enqueue(*resources) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



84
85
86
87
88
# File 'lib/puppet/graph/relationship_graph.rb', line 84

def enqueue(*resources)
  resources.each do |resource|
    @ready[@prioritizer.priority_of(resource)] = resource
  end
end

#enqueue_rootsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Enqueue the initial set of resources, those with no dependencies.



61
62
63
64
65
66
# File 'lib/puppet/graph/relationship_graph.rb', line 61

def enqueue_roots
  vertices.each do |v|
    @blockers[v] = direct_dependencies_of(v).length
    enqueue(v) if @blockers[v] == 0
  end
end

#finish(resource) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



90
91
92
93
94
95
# File 'lib/puppet/graph/relationship_graph.rb', line 90

def finish(resource)
  direct_dependents_of(resource).each do |v|
    enqueue(v) if unblock(v)
  end
  @done[resource] = true
end

#next_resourceObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



97
98
99
# File 'lib/puppet/graph/relationship_graph.rb', line 97

def next_resource
  @ready.delete_min
end

#populate_from(catalog) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/puppet/graph/relationship_graph.rb', line 24

def populate_from(catalog)
  add_all_resources_as_vertices(catalog)
  build_manual_dependencies
  build_autorelation_dependencies(catalog)

  write_graph(:relationships) if catalog.host_config?

  replace_containers_with_anchors(catalog)

  write_graph(:expanded_relationships) if catalog.host_config?
end

#remove_vertex!(vertex) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



51
52
53
54
# File 'lib/puppet/graph/relationship_graph.rb', line 51

def remove_vertex!(vertex)
  super
  @prioritizer.forget(vertex)
end

#resource_priority(resource) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



56
57
58
# File 'lib/puppet/graph/relationship_graph.rb', line 56

def resource_priority(resource)
  @prioritizer.priority_of(resource)
end

#traverse(options = {}, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/puppet/graph/relationship_graph.rb', line 101

def traverse(options = {}, &block)
  continue_while = options[:while] || -> { true }
  pre_process = options[:pre_process] || ->(resource) {}
  overly_deferred_resource_handler = options[:overly_deferred_resource_handler] || ->(resource) {}
  canceled_resource_handler = options[:canceled_resource_handler] || ->(resource) {}
  teardown = options[:teardown] || -> {}
  graph_cycle_handler = options[:graph_cycle_handler] || -> { [] }

  cycles = report_cycles_in_graph
  if cycles
    graph_cycle_handler.call(cycles)
  end

  enqueue_roots

  deferred_resources = []

  while continue_while.call() && (resource = next_resource)
    if resource.suitable?
      made_progress = true

      pre_process.call(resource)

      yield resource

      finish(resource)
    else
      deferred_resources << resource
    end

    next unless @ready.empty? and deferred_resources.any?

    if made_progress
      enqueue(*deferred_resources)
    else
      deferred_resources.each do |res|
        overly_deferred_resource_handler.call(res)
        finish(res)
      end
    end

    made_progress = false
    deferred_resources = []
  end

  unless continue_while.call()
    while (resource = next_resource)
      canceled_resource_handler.call(resource)
      finish(resource)
    end
  end

  teardown.call()
end

#unblock(resource) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Decrement the blocker count for the resource by 1. If the number of blockers is unknown, count them and THEN decrement by 1.



70
71
72
73
74
75
76
77
78
# File 'lib/puppet/graph/relationship_graph.rb', line 70

def unblock(resource)
  @blockers[resource] ||= direct_dependencies_of(resource).select { |r2| !@done[r2] }.length
  if @blockers[resource] > 0
    @blockers[resource] -= 1
  else
    resource.warning _("appears to have a negative number of dependencies")
  end
  @blockers[resource] <= 0
end