Class: DeploymentRecommendation

Inherits:
Base
  • Object
show all
Defined in:
app/models/deployment_recommendation.rb

Constant Summary collapse

DEFAULT_MIN_RAM =

Constraint defaults

1
DEFAULT_MIN_CPUS =
1
DEFAULT_DEPENDENCY_WEIGHT =
100
DEFAULT_REGION_AREAS =
%w(EU)
DEFAULT_MAX_NUM_INSTANCES =
0
INTRA_REGION_TRANSFER =

Transfer costs (assumed)

0
INTER_REGION_SAME_PROVIDER_TRANSFER =
10
INTER_REGION_DIFFERENT_PROVIDER_TRANSFER =
30
INTER_REGION_AREA_TRANSFER =
100
SOLN_SEP =

MiniZinc

'----------'
SEARCH_COMPLETE_MSG =

'--soln-sep'

'=========='
UNSATISFIABLE_MSG =

'--search-complete-msg'

'=====UNSATISFIABLE====='
UNCONSTRUCTED =

Intermediate status

'unconstructed'
UNEVALUATED =
'unevaluated'
EVALUATING =
'evaluating'
UNSATISFIABLE =

Final status

'unsatisfiable'
SATISFIABLE =
'satisfiable'
EVALUATION_ERROR =
'evaluation_error'
MINIZINC_ERROR_REGEX =
/lib\/.+\.mzn:(\d+):/

Instance Method Summary collapse

Methods inherited from Base

ma_accessor, ma_reader, ma_writer, #serialize_more_attributes

Instance Method Details

#as_json(options = {}) ⇒ Object


280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'app/models/deployment_recommendation.rb', line 280

def as_json(options={})
  hash = extract_params

  ingredients = []
  hash[:recommendation].each do |ingredient_id, resource_code|
    entry = {}
    entry[:ingredient] = Ingredient.find(ingredient_id.to_i).as_json({skip_children: true, skip_constraints: true})
    entry[:resource] = Resource.find_by_resource_code(resource_code).as_json({skip_children: true, skip_constraints: true})
    entry[:resource_count] = self.more_attributes['num_resources'][ingredients.count] rescue 1
    ingredients << entry
  end

  hash[:recommendation] = ingredients
  hash[:num_simultaneous_users] = self.num_simultaneous_users
  hash[:application] = self.ingredient.as_json({skip_children: true, skip_constraints: true})
  hash[:status] = self.status
  hash[:unsatisfiable_message] = self.more_attributes['unsatisfiable_message'] if self.status == UNSATISFIABLE
  hash
end

#construct(provider) ⇒ Object

Constructs the variable resource and ingredient data for the MiniZinc model for a given provider


41
42
43
44
45
46
# File 'app/models/deployment_recommendation.rb', line 41

def construct(provider)
  self.generate_resources_data(provider.id)
  self.generate_ingredients_data(provider.id)
  self.status = DeploymentRecommendation::UNEVALUATED
  self.save!
end

#constructed?Boolean

Returns:

  • (Boolean)

272
273
274
# File 'app/models/deployment_recommendation.rb', line 272

def constructed?
  self.status != UNCONSTRUCTED
end

#embed_ingredientsObject


300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'app/models/deployment_recommendation.rb', line 300

def embed_ingredients
  hash = extract_params

  ingredients = []
  hash[:recommendation].each do |ingredient_id, resource_code|
    entry = {}
    entry[:ingredient] = Ingredient.find(ingredient_id.to_i)
    entry[:resource] = Resource.find_by_resource_code(resource_code)
    ingredients << entry
  end

  hash[:recommendation] = ingredients
  hash[:num_simultaneous_users] = self.num_simultaneous_users
  hash[:status] = self.status
  hash[:unsatisfiable_message] = self.more_attributes['unsatisfiable_message'] if self.status == UNSATISFIABLE
  hash
end

#evaluateObject

Evaluates the MiniZinc model using the constructed resources and ingredient data


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/models/deployment_recommendation.rb', line 59

def evaluate
  self.status = EVALUATING
  self.save!
  resources = Tempfile.new(%w(resources .dzn))
  resources.write self.resources_data
  resources.close
  ingredients = Tempfile.new(%w(ingredients .dzn))
  ingredients.write self.ingredients_data
  ingredients.close

  command = "minizinc -G or-tools -f fzn-or-tools #{minizinc_model} #{resources.path} #{ingredients.path}"
  stdout, stderr, status = Open3.capture3(command)
  if status.success?
    parse_result(stdout, stderr)
  else
    fail [ 'Error executing MiniZinc!',
           '----------stdout----------',
           stdout,
           '----------stderr----------',
           stderr,
           '--------------------------' ].join("\n")
  end
rescue
  self.status = EVALUATION_ERROR
  self.save!
ensure
  resources.unlink
  ingredients.unlink
end

#evaluated?Boolean

Returns:

  • (Boolean)

276
277
278
# File 'app/models/deployment_recommendation.rb', line 276

def evaluated?
  self.status == SATISFIABLE || self.status == UNSATISFIABLE || self.status == EVALUATION_ERROR
end

#extract_paramsObject (private)


320
321
322
323
324
325
326
327
328
329
# File 'app/models/deployment_recommendation.rb', line 320

def extract_params
  hash = {}
  hash[:id] = self.id
  hash[:vm_cost] = self.more_attributes['vm_cost']
  hash[:total_cost] = self.more_attributes['total_cost']
  hash[:recommendation] = (self.more_attributes['ingredients']) ? self.more_attributes['ingredients'] : []
  hash[:created_at] = self.created_at
  hash[:updated_at] = self.updated_at
  hash
end

#extract_result(output) ⇒ Object


127
128
129
130
131
# File 'app/models/deployment_recommendation.rb', line 127

def extract_result(output)
  output.gsub!(', ]', ']')
  results = output.split(SOLN_SEP)
  results[results.size - 2] # last entry contains the search complete msg
end

#filtered_resources(provider_id) ⇒ Object


176
177
178
179
180
181
# File 'app/models/deployment_recommendation.rb', line 176

def filtered_resources(provider_id)
  resources = Resource.compute
  resources = resources.where(provider_id: provider_id) if provider_id
  resources = resources.region_area(self.ingredient.preferred_region_areas)
  resources.order(:id)
end

#generate_ingredients_data(provider_id) ⇒ Object


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
# File 'app/models/deployment_recommendation.rb', line 201

def generate_ingredients_data(provider_id)
  ingredients_data = ''

  all_leafs = self.ingredient.all_leafs
  num_ingredients = all_leafs.count
  ingredients_data << "num_ingredients = #{num_ingredients};"
  ingredients_data << "\n"

  ingredients_data << "inter_ingredient_traffic = array2d(Ingredients, Ingredients,
                        #{inter_ingredient_traffic(all_leafs).to_a.flatten.to_json});"
  ingredients_data << "\n"

  min_ram = all_leafs.collect do |i|
    i.ram_constraint.min_ram rescue DEFAULT_MIN_RAM
  end
  ingredients_data << "min_ram = #{min_ram.to_json};"
  ingredients_data << "\n"

  min_cpus = all_leafs.collect do |i|
    100 * (i.cpu_constraint.min_cpus rescue DEFAULT_MIN_CPUS)
  end
  ingredients_data << "min_cpus = #{min_cpus.to_json};"
  ingredients_data << "\n"

  ingredients_data << "preferred_regions = array2d(Ingredients, Resources,
                        #{preferred_regions(self.ingredient, provider_id).to_json});"
  ingredients_data << "\n"

  max_num_resources = all_leafs.collect do |i|
    i.scaling_constraint.max_num_instances rescue DEFAULT_MAX_NUM_INSTANCES
  end
  ingredients_data << "max_num_resources = #{max_num_resources.to_json}"
  ingredients_data << "\n"

  self.ingredients_data = ingredients_data
end

#generate_resources_data(provider_id) ⇒ Object


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
# File 'app/models/deployment_recommendation.rb', line 147

def generate_resources_data(provider_id)
  resources = filtered_resources(provider_id)

  resources_data = ''
  resources_data << "num_resources = #{resources.count};"
  resources_data << "\n"

  resources_data << "resources = #{resources.map(&:resource_code).to_json};"
  resources_data << "\n"

  resources_data << "regions = #{resources.map(&:region_code).to_json};"
  resources_data << "\n"

  prices = resources.map { |r| (r.price_per_month.to_f * 1000).to_i }
  resources_data << "costs = #{prices.to_json};"
  resources_data << "\n"

  ram_mb = resources.map { |r| ((r.ma['mem_gb'].to_f) * 1024).to_i }
  resources_data << "ram = #{ram_mb.to_json};"
  resources_data << "\n"

  cores = resources.map { |r| (r.ma['cores'].to_f * 100).to_i rescue 0 }
  resources_data << "cpu = #{cores};"
  resources_data << "\n"

  resources_data << "transfer_costs = array2d(Resources, Resources, #{transfer_costs(resources).to_a.flatten.to_json});"
  self.resources_data = resources_data
end

#ingredient_resource_mapping(ingredient_resources) ⇒ Object

Transforms the ordered resource code strings for ingredients into a mappings hash: [ingredient id] => [resource code] Example: ingredient_resource_mapping(["79526981", "79526981", "4044876983"]) 4=>79526981, 5=>4044876983


141
142
143
144
145
# File 'app/models/deployment_recommendation.rb', line 141

def ingredient_resource_mapping(ingredient_resources)
  ingredient_ids = self.ingredient.all_leafs.map(&:id)
  resource_codes = ingredient_resources.map(&:to_i)
  Hash[ingredient_ids.zip(resource_codes)]
end

#inter_ingredient_traffic(all_leafs) ⇒ Object


238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'app/models/deployment_recommendation.rb', line 238

def inter_ingredient_traffic(all_leafs)
  dependencies = Hash.new
  all_leafs.each do |i|
    i.dependency_constraints.each do |dc|
      source_index = all_leafs.index(dc.source)
      target_index = all_leafs.index(dc.target)
      # Use bi-directional dependency
      dependencies[[source_index,target_index]] = DEFAULT_DEPENDENCY_WEIGHT
      dependencies[[target_index,source_index]] = DEFAULT_DEPENDENCY_WEIGHT
    end
  end
  Matrix.build(all_leafs.count, all_leafs.count) do |row, col|
    if dependencies[[row,col]]
      dependencies[[row,col]]
    else
      0
    end
  end
end

#line_from_minizinc_model(line) ⇒ Object


118
119
120
# File 'app/models/deployment_recommendation.rb', line 118

def line_from_minizinc_model(line)
  File.readlines(minizinc_model)[line - 1].strip
end

#line_with_error(stderr) ⇒ Object


123
124
125
# File 'app/models/deployment_recommendation.rb', line 123

def line_with_error(stderr)
  MINIZINC_ERROR_REGEX.match(stderr)[1].to_i
end

#minizinc_modelObject


89
90
91
# File 'app/models/deployment_recommendation.rb', line 89

def minizinc_model
  "#{Rails.root}/lib/horizontal-scaling.mzn"
end

#parse_result(stdout, stderr) ⇒ Object


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'app/models/deployment_recommendation.rb', line 93

def parse_result(stdout, stderr)
  result = extract_result(stdout)
  if satisfiable?(result)
    self.status = SATISFIABLE
    self.more_attributes = result
    unless self.save # serializes `more_attributes` result string into a hash
      err_msg = self.errors.full_messages
      self.destroy!
      fail [ 'Error parsing MiniZinc output!',
             '----------result----------',
             result,
             '----------error----------',
             err_msg,
             '--------------------------' ].join("\n")
    end

    mapping = ingredient_resource_mapping(self.more_attributes['ingredients'])
    self.more_attributes['ingredients'] = mapping
    self.save!
  else
    self.status = UNSATISFIABLE
    self.save!
  end
end

#preferred_regions(ingredient, provider_id) ⇒ Object

Returns a flattened Boolean matrix (i.e., as array of [true|false]) determining which resources are allowed for which ingredients following the preferred region constraint


261
262
263
264
265
266
267
268
269
270
# File 'app/models/deployment_recommendation.rb', line 261

def preferred_regions(ingredient, provider_id)
  resource_region_codes = filtered_resources(provider_id).map(&:region_code)
  region_areas = ingredient.region_constraints
  regions = Array.new
  region_areas.each do |region_area|
    preferred_region_codes = Set.new(Resource.region_codes(region_area))
    regions.push(resource_region_codes.map { |rrc| preferred_region_codes.member?(rrc) })
  end
  regions.flatten
end

#satisfiable?(result) ⇒ Boolean

Returns:

  • (Boolean)

133
134
135
# File 'app/models/deployment_recommendation.rb', line 133

def satisfiable?(result)
  !result.include?(UNSATISFIABLE_MSG)
end

#schedule_evaluationObject

Schedules the MiniZinc evaluation and keeps track of the scheduled job ID


49
50
51
52
53
54
55
# File 'app/models/deployment_recommendation.rb', line 49

def schedule_evaluation
  ActiveRecord::Base.transaction do
    job = EvaluateRecommendationJob.perform_later(self)
    self.ingredient.add_job(job.job_id)
    job
  end
end

#transfer_costs(resources) ⇒ Object

NOTICE: Currently, these transfer costs are only a heuristic and do not reflect actual transfer cost from real cloud providers


185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'app/models/deployment_recommendation.rb', line 185

def transfer_costs(resources)
  Matrix.build(resources.count, resources.count) do |row, col|
    if resources[row].region_code == resources[col].region_code
      INTRA_REGION_TRANSFER
    elsif resources[row].region_area == resources[col].region_area
      if resources[row].provider_id == resources[col].provider_id
        INTER_REGION_SAME_PROVIDER_TRANSFER
      else
        INTER_REGION_DIFFERENT_PROVIDER_TRANSFER
      end
    else
      INTER_REGION_AREA_TRANSFER
    end
  end
end