From 18268036e9ee8fcb359f7d98ade7586eeeb49a9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Fri, 31 May 2013 16:40:49 +0200 Subject: [PATCH] Feature #1854 #1852: Scaling works in two steps, to allow manual intevention --- flow/lcm/LifeCycleManager.rb | 19 ++++++------ flow/lcm/strategy.rb | 60 ++++++++++++++++++++---------------- flow/models/Role.rb | 42 ++++++++++++++----------- 3 files changed, 66 insertions(+), 55 deletions(-) diff --git a/flow/lcm/LifeCycleManager.rb b/flow/lcm/LifeCycleManager.rb index bbf1e749cb..ec74226519 100644 --- a/flow/lcm/LifeCycleManager.rb +++ b/flow/lcm/LifeCycleManager.rb @@ -75,20 +75,14 @@ class ServiceLCM if !service.all_roles_running? service.set_state(Service::STATE['UNKNOWN']) else - rc = strategy.scale_step(service) + if strategy.apply_scaling_policies(service) + service.set_state(Service::STATE['SCALING']) - scale = rc[0] - succ = rc[1] - error_msg = rc[2] - - if scale - if succ - service.set_state(Service::STATE['SCALING']) - else + rc = strategy.scale_step(service) + if !rc[0] service.set_state(Service::STATE['FAILED_SCALING']) end end - end when Service::STATE['SCALING'] strategy.monitor_step(service) @@ -100,6 +94,11 @@ class ServiceLCM service.set_state(Service::STATE['FAILED_SCALING']) elsif !service.any_role_scaling? service.set_state(Service::STATE['RUNNING']) + else + rc = strategy.scale_step(service) + if !rc[0] + service.set_state(Service::STATE['FAILED_SCALING']) + end end when Service::STATE['FAILED_SCALING'] strategy.monitor_step(service) diff --git a/flow/lcm/strategy.rb b/flow/lcm/strategy.rb index acee34cd2d..0794e98112 100644 --- a/flow/lcm/strategy.rb +++ b/flow/lcm/strategy.rb @@ -74,45 +74,51 @@ class Strategy return [true, nil] end - # - # @return [Array, Array, Array] - # 1st elem: true if the service needed to scale - # 2nd elem: true if the operation succeeded - # 3rd elem: error reason, if any - def scale_step(service) - Log.debug LOG_COMP, "Scale step", service.id() - - scale = false + # If a role needs to scale, its cardinality is updated, and its state is set + # to SCALING. Only one role is set to scale. + # @param [Service] service + # @return [true|false] true if any role needs to scale + def apply_scaling_policies(service) + Log.debug LOG_COMP, "Apply scaling policies", service.id() service.get_roles.each do |name, role| diff = role.scale? - if diff > 0 - Log.debug LOG_COMP, "Scaling up role #{name}, #{diff} nodes", service.id() - - rc = role.scale_up(role.cardinality() + diff) - elsif diff < 0 - Log.debug LOG_COMP, "Scaling down role #{name}, #{diff} nodes", service.id() - - rc = role.scale_down(role.cardinality() + diff) - end - if diff != 0 + Log.debug LOG_COMP, "Role #{name} needs to scale #{diff} nodes", service.id() + + role.set_cardinality(role.cardinality() + diff) + + role.set_state(Role::STATE['SCALING']) + + return true + end + end + + return false + end + + # If a role is scaling, the nodes are created/destroyed to match the current + # cardinality + # @return [Array, Array] true if the action was + # performed, false and the error reason if there was a problem + def scale_step(service) + Log.debug LOG_COMP, "Scale step", service.id() + + service.get_roles.each do |name, role| + + if role.state == Role::STATE['SCALING'] + rc = role.scale() if !rc[0] role.set_state(Role::STATE['FAILED_SCALING']) - return [true] + rc + return rc end - - role.set_state(Role::STATE['SCALING']) - scale = true - - break end end - return [scale, true, nil] + return [true, nil] end # Performs a monitor step, check if the roles already deployed are running @@ -146,7 +152,7 @@ class Strategy when Role::STATE['SCALING'] if OpenNebula.is_error?(rc) role.set_state(Role::STATE['FAILED_SCALING']) - elsif role_nodes_running?(role) + elsif role_finished_scaling?(role) role.set_state(Role::STATE['RUNNING']) elsif any_node_failed_scaling?(role) role.set_state(Role::STATE['FAILED_SCALING']) diff --git a/flow/models/Role.rb b/flow/models/Role.rb index 9f16bfe637..b726bd6c1c 100644 --- a/flow/models/Role.rb +++ b/flow/models/Role.rb @@ -84,8 +84,12 @@ module OpenNebula # Sets a new cardinality for this role # @param [Integer] the new cardinality - def set_cardinality(cardinality) - @body['cardinality'] = cardinality.to_i + def set_cardinality(target_cardinality) + msg = "Role #{name} scaling up from #{cardinality} to #{target_cardinality} nodes" + Log.info LOG_COMP, msg, @service.id() + @service.log_info(msg) + + @body['cardinality'] = target_cardinality.to_i end # Returns the role max cardinality @@ -512,24 +516,28 @@ module OpenNebula return treetop.result(self) end - def scale_up(target_cardinality) - msg = "Role #{name} scaling up from #{cardinality} to #{target_cardinality} nodes" - Log.info LOG_COMP, msg, @service.id() - @service.log_info(msg) + # Scales up or down the number of nodes needed to match the current + # cardinality + # + # @return [Array, Array] true if all the VMs + # were created/shut down, false and the error reason if there + # was a problem + def scale() + n_nodes = 0 - set_cardinality(target_cardinality) + get_nodes.each do |node| + n_nodes += 1 if node['disposed'] != "1" + end - return deploy() - end + diff = cardinality - n_nodes - def scale_down(target_cardinality) - msg = "Role #{name} scaling down from #{cardinality} to #{target_cardinality} nodes" - Log.info LOG_COMP, msg, @service.id() - @service.log_info(msg) + if diff > 0 + return deploy() + elsif diff < 0 + return shutdown(true) + end - set_cardinality(target_cardinality) - - return shutdown(true) + return [true, nil] end # For a failed scale up, the cardinality is updated to the actual value @@ -581,8 +589,6 @@ module OpenNebula set_cardinality( get_nodes.size() - n_dispose ) end - - def update(template) set_cardinality(template["cardinality"]) end