Class: Workflow::Orchestrator

Inherits:
Object
  • Object
show all
Defined in:
lib/scout/workflow/deployment/orchestrator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(timer = 5, available_resources = {}) ⇒ Orchestrator

Returns a new instance of Orchestrator.



110
111
112
113
114
115
116
# File 'lib/scout/workflow/deployment/orchestrator.rb', line 110

def initialize(timer = 5, available_resources = {})
  available_resources  = {:cpus => Etc.nprocessors } if available_resources.nil?
  @timer               = timer
  @available_resources = IndiferentHash.setup(available_resources)
  @resources_requested = IndiferentHash.setup({})
  @resources_used      = IndiferentHash.setup({})
end

Instance Attribute Details

#available_resourcesObject

Returns the value of attribute available_resources.



108
109
110
# File 'lib/scout/workflow/deployment/orchestrator.rb', line 108

def available_resources
  @available_resources
end

#resources_requestedObject

Returns the value of attribute resources_requested.



108
109
110
# File 'lib/scout/workflow/deployment/orchestrator.rb', line 108

def resources_requested
  @resources_requested
end

#resources_usedObject

Returns the value of attribute resources_used.



108
109
110
# File 'lib/scout/workflow/deployment/orchestrator.rb', line 108

def resources_used
  @resources_used
end

#timerObject

Returns the value of attribute timer.



108
109
110
# File 'lib/scout/workflow/deployment/orchestrator.rb', line 108

def timer
  @timer
end

Class Method Details

.candidates(workload, rules) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/scout/workflow/deployment/orchestrator.rb', line 83

def self.candidates(workload, rules)
  if rules.empty?
    candidates = workload.
      select{|k,v| v.empty? }.
      collect{|k,v| k }.
      reject{|k| k.done? || k.running? }
  else
    candidates = workload. #select{|k,v| Orchestrator.job_rules(rules, k) }.
      select{|k,v| v.empty? }.
      collect{|k,v| k }.
      reject{|k| k.done? || k.running? }
  end

  #top_level = workload.keys - workload.values.flatten

  candidates = purge_duplicates candidates
  candidates = sort_candidates candidates, rules

  candidates
end

.job_resources(rules, job) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/scout/workflow/deployment/orchestrator.rb', line 61

def self.job_resources(rules, job)
  resources = (job_rules(rules, job) || {})["resources"] || {}

  IndiferentHash.setup(resources)

  default_resources = rules["default_resources"] 
  default_resources ||= rules["defaults"]["resources"] if rules["defaults"]
  default_resources ||= {}

  default_resources.each{|k,v| resources[k] ||= v } if default_resources

  resources = {:cpus => 1} if resources.empty?
  resources
end

.job_rules(rules, job) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/scout/workflow/deployment/orchestrator.rb', line 36

def self.job_rules(rules, job)
  workflow = job.workflow.name
  task_name = job.task_name.to_s
  defaults = rules["defaults"] || {}

  return IndiferentHash.setup(defaults) unless rules[workflow]
  return IndiferentHash.setup(defaults) unless rules[workflow][task_name]

  job_rules = IndiferentHash.setup(rules[workflow][task_name])
  defaults.each{|k,v| job_rules[k] = v if job_rules[k].nil? } if defaults
  job_rules
end

.job_workload(job) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/scout/workflow/deployment/orchestrator.rb', line 4

def self.job_workload(job)
  workload = {job => []}
  return workload if job.done? && job.updated?

  job.dependencies.each do |dep|
    next if dep.done? && dep.updated?
    workload.merge!(job_workload(dep))
    workload[job] += workload[dep]
    workload[job] << dep
    workload[job].uniq!
  end

  job.input_dependencies.each do |dep|
    next if dep.done? && dep.updated?
    workload.merge!(job_workload(dep))
    workload[job] += workload[dep]
    workload[job] << dep
    workload[job].uniq!
  end

  workload
end

.process(*args) ⇒ Object



104
105
106
# File 'lib/scout/workflow/deployment/orchestrator.rb', line 104

def self.process(*args)
  self.new.process(*args)
end

.purge_duplicates(candidates) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/scout/workflow/deployment/orchestrator.rb', line 49

def self.purge_duplicates(candidates)
  seen = Set.new
  candidates.select do |job|
    if seen.include? job.path
      false
    else
      seen << job.path
      true
    end
  end
end

.sort_candidates(candidates, rules) ⇒ Object



76
77
78
79
80
81
# File 'lib/scout/workflow/deployment/orchestrator.rb', line 76

def self.sort_candidates(candidates, rules)
  seen = Set.new
  candidates.sort_by do |job|
    - job_resources(rules, job).values.inject(0){|acc,e| acc += e}
  end
end

.workload(jobs) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/scout/workflow/deployment/orchestrator.rb', line 27

def self.workload(jobs)
  jobs.inject({}) do |acc,job| 
    Orchestrator.job_workload(job).each do |j,d|
      acc[j] = d unless acc.keys.collect{|k| k.path }.include? j.path
    end
    acc
  end
end

Instance Method Details

#check_resources(rules, job) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/scout/workflow/deployment/orchestrator.rb', line 129

def check_resources(rules, job)
  resources = Orchestrator.job_resources(rules, job)

  limit_resources = resources.select{|resource,value| available_resources[resource] && ((resources_requested[resource] || 0) + value) > available_resources[resource]  }.collect{|resource,v| resource }
  if limit_resources.any?
    Log.debug "Orchestrator waiting on #{job.path} due to #{limit_resources * ", "}"
  else

    resources_used[job] = resources
    resources.each do |resource,value| 
      resources_requested[resource] ||= 0
      resources_requested[resource] += value.to_i
    end
    Log.low "Orchestrator producing #{job.path} with resources #{resources}"

    return yield
  end
end

#erase_job_dependencies(job, rules, all_jobs, top_level_jobs) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/scout/workflow/deployment/orchestrator.rb', line 164

def erase_job_dependencies(job, rules, all_jobs, top_level_jobs)
  job.dependencies.each do |dep|
    next if top_level_jobs.include? dep.path
    next unless Orchestrator.job_rules(rules, dep)["erase"].to_s == 'true'

    dep_path = dep.path
    parents = all_jobs.select do |parent| 
      paths = parent.info[:dependencies].nil? ? parent.dependencies.collect{|d| d.path } : parent.info[:dependencies].collect{|d| Array === d ? d.last : d }
      paths.include? dep_path
    end

    next unless parents.reject{|parent| parent.done? }.empty?

    parents.each do |parent|
      Log.high "Erasing #{dep.path} from #{parent.path}"
      parent.archive_deps
      parent.copy_linked_files_dir
      parent.dependencies = parent.dependencies - [dep]
    end
    dep.clean
  end
end

#process(rules, jobs = nil) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/scout/workflow/deployment/orchestrator.rb', line 187

def process(rules, jobs = nil)
  jobs, rules = rules, {} if jobs.nil?
  jobs = [jobs] if Step === jobs
  begin

    workload = Orchestrator.workload(jobs)
    all_jobs = workload.keys

    all_jobs.each{|job| job.clean unless job.done? && job.updated? }

    top_level_jobs = jobs.collect{|job| job.path }
    failed_jobs = []
    while workload.any? 

      candidates = resources_used.keys + Orchestrator.candidates(workload, rules)
      candidates.uniq!
      raise "No candidates and no running jobs" if candidates.empty?

      candidates.each do |job|
        case 
        when (job.error? || job.aborted?)
          begin
            if job.recoverable_error? 
              if failed_jobs.include?(job)
                Log.warn "Failed twice #{job.path} with recoverable error"
                next
              else
                failed_jobs << job
                job.clean
                raise TryAgain
              end
            else
              next
            end
          ensure
            Log.warn "Releases resources from failed job: #{job.path}"
            release_resources(job)
          end
        when job.done?
          Log.debug "Orchestrator done #{job.path}"
          release_resources(job)
          erase_job_dependencies(job, rules, all_jobs, top_level_jobs)

        when job.running?
          next

        else
          check_resources(rules, job) do
            run_with_rules(rules, job)
          end
        end
      end

      new_workload = {}
      workload.each do |k,v|
        next if k.done? || k.error? || k.aborted?
        #new_workload[k] = v.reject{|d| d.done? || ((d.error? || d.aborted?) && ! d.recoverable_error?)}
        new_workload[k] = v.reject{|d| d.done? || d.error? || d.aborted?}
      end
      workload = new_workload
      sleep timer
    end
    all_jobs.each{|s| s.join }
  rescue TryAgain
    retry
  end
end

#release_resources(job) ⇒ Object



118
119
120
121
122
123
124
125
126
127
# File 'lib/scout/workflow/deployment/orchestrator.rb', line 118

def release_resources(job)
  if resources_used[job]
    Log.debug "Orchestrator releasing resouces from #{job.path}"
    resources_used[job].each do |resource,value| 
      next if resource == 'size'
      resources_requested[resource] -= value.to_i
    end
    resources_used.delete job
  end
end

#run_with_rules(rules, job) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/scout/workflow/deployment/orchestrator.rb', line 148

def run_with_rules(rules, job)
  job_rules = Orchestrator.job_rules(rules, job)

  Scout::Config.with_config do 
    job_rules[:config_keys].each do |config|
      Scout::Config.process_config config
    end if job_rules && job_rules[:config_keys]

    log = job_rules[:log] if job_rules 
    log = Log.severity if log.nil?
    Log.with_severity log do
      job.fork
    end
  end
end