Class: StackBuilder::Stack::NodeTask

Inherits:
Object
  • Object
show all
Defined in:
lib/stackbuilder/stack/node_task.rb

Constant Summary collapse

SYNC_NONE =

All node instances processed asynchronously

0
SYNC_FIRST =

First node instance is processed synchronously and the rest asynchronously

1
SYNC_ALL =

All node instances are processed synchronously

2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(manager, nodes, node_config, id) ⇒ NodeTask

Returns a new instance of NodeTask.

Raises:

  • (ArgumentError)


29
30
31
32
33
34
35
36
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
65
66
67
68
69
70
# File 'lib/stackbuilder/stack/node_task.rb', line 29

def initialize(manager, nodes, node_config, id)
    
    @logger = StackBuilder::Common::Config.logger

    @manager = manager

    @id = id
    @nodes = nodes
    @parent_nodes = [ ]
    @child_nodes = [ ]
    @counter = 0
    
    @name = node_config['node']
    @attributes = node_config['attributes'] || { }

    case node_config['sync']
        when "first"
            @sync = SYNC_FIRST
        when "all"
            @sync = SYNC_ALL
        else
            @sync = SYNC_NONE
    end

    current_scale = manager.get_scale
    if current_scale==0
        @scale = node_config["scale"] || 1
    else
        @scale = current_scale
    end
    @max_scale = node_config["max_scale"] || @scale

    raise ArgumentError, "The scale for node \"#{@name}\" must be greater than 0." if @scale < 1
    @prev_scale = @scale

    @targets = [ ]

    @node_mutex = Mutex.new
    @resource_sync = [ ]

    @deleted = false
end

Instance Attribute Details

#child_nodesObject (readonly)

Returns the value of attribute child_nodes.



20
21
22
# File 'lib/stackbuilder/stack/node_task.rb', line 20

def child_nodes
  @child_nodes
end

#counterObject (readonly)

Returns the value of attribute counter.



18
19
20
# File 'lib/stackbuilder/stack/node_task.rb', line 18

def counter
  @counter
end

#deletedObject

Returns the value of attribute deleted.



16
17
18
# File 'lib/stackbuilder/stack/node_task.rb', line 16

def deleted
  @deleted
end

#managerObject (readonly)

Returns the value of attribute manager.



23
24
25
# File 'lib/stackbuilder/stack/node_task.rb', line 23

def manager
  @manager
end

#max_scaleObject

Returns the value of attribute max_scale.



12
13
14
# File 'lib/stackbuilder/stack/node_task.rb', line 12

def max_scale
  @max_scale
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/stackbuilder/stack/node_task.rb', line 9

def name
  @name
end

#parent_nodesObject (readonly)

Returns the value of attribute parent_nodes.



19
20
21
# File 'lib/stackbuilder/stack/node_task.rb', line 19

def parent_nodes
  @parent_nodes
end

#prev_scaleObject

Returns the value of attribute prev_scale.



13
14
15
# File 'lib/stackbuilder/stack/node_task.rb', line 13

def prev_scale
  @prev_scale
end

#resource_syncObject (readonly)

Returns the value of attribute resource_sync.



22
23
24
# File 'lib/stackbuilder/stack/node_task.rb', line 22

def resource_sync
  @resource_sync
end

#scaleObject

Returns the value of attribute scale.



11
12
13
# File 'lib/stackbuilder/stack/node_task.rb', line 11

def scale
  @scale
end

#syncObject

Returns the value of attribute sync.



14
15
16
# File 'lib/stackbuilder/stack/node_task.rb', line 14

def sync
  @sync
end

Instance Method Details

#add_dependency(node_name, is_target = false) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/stackbuilder/stack/node_task.rb', line 72

def add_dependency(node_name, is_target = false)

    node = @nodes[node_name]

    @targets << node if is_target

    node.parent_nodes << self unless node.parent_nodes.include?(self)
    self.child_nodes << node unless self.child_nodes.include?(node)
end

#dec_dependency_countObject



105
106
107
108
109
110
# File 'lib/stackbuilder/stack/node_task.rb', line 105

def dec_dependency_count
    @node_mutex.synchronize {
        @counter -= 1
        return @counter
    }
end

#init_dependency_count(count = nil) ⇒ Object



94
95
96
97
98
99
100
101
102
103
# File 'lib/stackbuilder/stack/node_task.rb', line 94

def init_dependency_count(count = nil)

    if count.nil?
        @counter = child_nodes.size
    else
        @counter += count
    end

    @counter
end

#orchestrate(events) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/stackbuilder/stack/node_task.rb', line 212

def orchestrate(events)

    threads = [ ]

    if @targets.empty?

        scale = (@deleted ? @manager.get_scale : @scale)
        scale.times do |i|
            spawn_processing(i, events, threads)
        end
    else
        @targets.each do |t|
            
            scale = (@deleted ? t.manager.get_scale : t.scale)
            scale.times do |i|
                spawn_processing(i, events, threads, t)
            end
        end
    end

    threads.each { |t| t.join }

    executable_parents = [ ]
    parent_nodes.each do |p|
        executable_parents << p if p.dec_dependency_count == 0
    end
    executable_parents
end

#prepareObject



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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/stackbuilder/stack/node_task.rb', line 112

def prepare

    threads = [ ]

    if @targets.empty?

        # You need to prepare nodes only if this node task
        # is the target. i.e. no referenced targets

        current_scale = @manager.get_scale

        @resource_sync.size.step(current_scale - 1) do |i|
            @resource_sync[i] ||= StackBuilder::Common::Semaphore.new
            @resource_sync[i].signal
        end

        if current_scale>@scale

            @logger.debug("Scaling #{self} from #{current_scale} down to #{@scale}")

            # Scale Down

            delete_events = Set.new([ "stop", "uninstall" ])
            @scale.step(current_scale - 1) do |i|

                resource_sync = @resource_sync[i]
                resource_sync.wait

                threads << Thread.new {

                    begin
                        @logger.debug("Deleting #{self} #{i}.")
                        $stdout.printf("Deleting node resource '%s[%d]'.\n",
                            @name, i) unless @logger.debug?

                        @manager.process(i, delete_events, parse_attributes(@attributes, i))
                        @manager.delete(i)

                    rescue Exception => msg

                        puts("Fatal Error: #{msg}")
                        @logger.debug(msg.backtrace.join("\n\t"))

                        raise StackBuilder::Common::StackDeleteError,
                            "Deleting node resource '#{name}[{i}]' " +
                            "terminated with an error: #{msg}"
                    ensure
                        resource_sync.signal
                    end
                }
            end

            (current_scale - 1).downto(@scale) do |i|
                @resource_sync.delete_at(i)
            end
        end

        if @scale>current_scale && !@deleted

            @logger.debug("Scaling #{self} from #{current_scale} up to #{@scale}")

            # Scale up

            current_scale.step(@scale - 1) do |i|

                sync = StackBuilder::Common::Semaphore.new
                @resource_sync[i] = sync

                threads << Thread.new {

                    begin
                        @logger.debug("Creating #{self} #{i}.")
                        $stdout.printf( "Creating node resource '%s[%d]'.\n",
                            @name, i) unless @logger.debug?

                        @manager.create(i)

                    rescue Exception => msg

                        puts("Fatal Error: #{msg}")
                        @logger.debug(msg.backtrace.join("\n\t"))

                        raise StackBuilder::Common::StackCreateError,
                            "Creating node resource '#{name}[#{i}]' " +
                            "terminated with an error: #{msg}"
                    ensure
                        @resource_sync[i].signal
                    end
                }
            end
        end

        @prev_scale = current_scale
    end

    @manager.set_scale(@scale)

    threads
end

#process_attribute_dependenciesObject



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/stackbuilder/stack/node_task.rb', line 82

def process_attribute_dependencies

    @attributes.each_value do |v|

        if v =~ /^nodes\[.*\]$/

            lookup_keys = v.split(/[\[\]]/).reject { |l| l == "nodes" || l.empty? }
            add_dependency(lookup_keys.shift)
        end
    end
end

#to_sObject



241
242
243
244
245
246
247
248
# File 'lib/stackbuilder/stack/node_task.rb', line 241

def to_s
    p = "Parent_Nodes[#{@parent_nodes.collect { |n| "#{n.name}:#{n.counter}" }.join(", ")}]"
    c = "Child_Nodes[#{@child_nodes.collect { |n| n.name }.join(", ")}]"
    
    "(#{@name}, #{p}, #{c}, " +
        "Sync[#{@sync==SYNC_NONE ? "async" : @sync==SYNC_FIRST ? "first" : "alls"}], " +
        "Scale[#{manager.get_scale}])"
end