diff --git a/flow/lcm/LifeCycleManager.rb b/flow/lcm/LifeCycleManager.rb index 334e14a2d6..21c7c0a111 100644 --- a/flow/lcm/LifeCycleManager.rb +++ b/flow/lcm/LifeCycleManager.rb @@ -74,22 +74,28 @@ class ServiceLCM if !service.all_roles_running? service.set_state(Service::STATE['UNKNOWN']) - elsif service.scale_up? + elsif service.scale? - Log.debug "ELAS", "Service #{service.name} to scale up" - Log.debug "ELAS", "Scale up", service.id() - - # TODO: create a new state SCALING, SCALING_UP? + Log.debug "ELAS", "Service #{service.name} to scale" # TODO: changing state stops the elasticity rules # check until current deployments end. - service.set_state(Service::STATE['DEPLOYING']) + service.set_state(Service::STATE['SCALING']) - rc = strategy.boot_step(service) + rc = strategy.scale_step(service) if !rc[0] service.set_state(Service::STATE['FAILED_DEPLOYING']) end end + when Service::STATE['SCALING'] + strategy.monitor_step(service) + + # TODO: Check scalability rules for roles that are + # not scaling now? + + if service.all_roles_running? + service.set_state(Service::STATE['RUNNING']) + end when Service::STATE['UNKNOWN'] strategy.monitor_step(service) diff --git a/flow/lcm/strategy.rb b/flow/lcm/strategy.rb index 58997ad346..1a6ed78d64 100644 --- a/flow/lcm/strategy.rb +++ b/flow/lcm/strategy.rb @@ -74,6 +74,37 @@ class Strategy return [true, nil] end + + def scale_step(service) + Log.debug LOG_COMP, "Scale step", service.id() + + roles_deploy = get_roles_scale(service) + + roles_deploy.each { |name, role| + + if role.cardinality > role.get_nodes.size + Log.debug LOG_COMP, "Scaling up role #{name}", service.id() + + rc = role.deploy + else + Log.debug LOG_COMP, "Scaling down role #{name}", service.id() + + rc = role.shutdown + end + + if !rc[0] + # TODO: Handle error +# role.set_state(Role::STATE['FAILED_DEPLOYING']) + + return rc + else + role.set_state(Role::STATE['SCALING']) + end + } + + return [true, nil] + end + # Performs a monitor step, check if the roles already deployed are running # @param [Service] service service to boot # @return [Array, Array] true if all the nodes @@ -96,7 +127,15 @@ class Strategy elsif !role_nodes_running?(role) role.set_state(Role::STATE['UNKNOWN']) end - when Role::STATE['DEPLOYING'] + when Role::STATE['DEPLOYING'], Role::STATE['SCALING'] + # TODO: For roles scaling up, role_nodes_running detects the + # scalability has finished. + + # For roles scaling down, the node is left in DONE. We need + # to clear or ignore those. Maybe we could keep the node to have + # a reference of all the VMs that were created for this service, + # but with a flag to ignore it for plannification and user output + if OpenNebula.is_error?(rc) role.set_state(Role::STATE['FAILED_DEPLOYING']) elsif role_nodes_running?(role) @@ -189,6 +228,13 @@ protected result end + # Returns all node Roles ready to be scaled up + # @param [Service] service + # @return [Hash] Roles + def get_roles_scale(service) + get_roles_deploy(service) + end + # Determine if the role nodes are running # @param [Role] role # @return [true|false] diff --git a/flow/models/Role.rb b/flow/models/Role.rb index c49fdcbefd..0803d56461 100644 --- a/flow/models/Role.rb +++ b/flow/models/Role.rb @@ -26,7 +26,8 @@ module OpenNebula 'UNKNOWN' => 5, 'DONE' => 6, 'FAILED_UNDEPLOYING' => 7, - 'FAILED_DEPLOYING' => 8 + 'FAILED_DEPLOYING' => 8, + 'SCALING' => 9 } STATE_STR = [ @@ -38,7 +39,8 @@ module OpenNebula 'UNKNOWN', 'DONE', 'FAILED_UNDEPLOYING', - 'FAILED_DEPLOYING' + 'FAILED_DEPLOYING', + 'SCALING' ] LOG_COMP = "ROL" @@ -96,6 +98,22 @@ module OpenNebula return max.to_i end + # Returns the role min cardinality + # @return [Integer] the role cardinality + def min_cardinality + min = nil + + if !@body['elasticity_policy'].nil? + min = @body['elasticity_policy']['min_vms'] + end + + if min.nil? + min = cardinality() + end + + return min.to_i + end + def up_expr expr = nil @@ -106,6 +124,16 @@ module OpenNebula return expr end + def down_expr + expr = nil + + if !@body['elasticity_policy'].nil? + expr = @body['elasticity_policy']['down_expr'] + end + + return expr + end + # Returns the string representation of the service state # @return [String] the state string def state_str @@ -122,7 +150,7 @@ module OpenNebula # @param [Integer] the new state # @return [true, false] true if the value was changed def set_state(state) - if state < 0 || state > 8 + if state < 0 || state > STATE_STR.size return false end @@ -210,7 +238,13 @@ module OpenNebula def shutdown success = true - get_nodes.each { |node| + nodes = get_nodes + n_nodes = nodes.size - cardinality() + +# get_nodes.each { |node| + n_nodes.times { |i| + node = nodes[i] + vm_id = node['deploy_id'] Log.debug LOG_COMP, "Role #{name} : Shutting down VM #{vm_id}", @service.id() @@ -298,5 +332,77 @@ module OpenNebula return [true, nil] end + + ######################################################################## + # Scalability + ######################################################################## + + # Returns true if the scalability rule to scale up is triggered + # @return true if this role has to scale up + def scale_up?() + parser = ElasticityGrammarParser.new + + elas_expr = up_expr + + if elas_expr.nil? + return false + end + + Log.debug "ELAS", "Role #{name} up_expr: #{elas_expr}" + + treetop = parser.parse(elas_expr) + if treetop.nil? + Log.debug "ELAS", "up_expr parse error" + end + + result = treetop.result(self) + + if result + Log.debug "ELAS", "Role #{name} to scale up" + + if cardinality() < max_cardinality() + return true + else + Log.debug "ELAS", "Role #{name} has reached its VM limit" + end + end + + return false + end + + + # Returns true if the scalability rule to scale down is triggered + # @return true if this role has to scale down + def scale_down?() + parser = ElasticityGrammarParser.new + + elas_expr = down_expr + + if elas_expr.nil? + return false + end + + Log.debug "ELAS", "Role #{name} down_expr: #{elas_expr}" + + treetop = parser.parse(elas_expr) + if treetop.nil? + Log.debug "ELAS", "down_expr parse error" + end + + result = treetop.result(self) + + if result + Log.debug "ELAS", "Role #{name} to scale down" + + if cardinality() > min_cardinality() + return true + else + Log.debug "ELAS", "Role #{name} has reached its VM minimum" + end + end + + return false + end + end end diff --git a/flow/models/Service.rb b/flow/models/Service.rb index ca4a932937..1b87b2b620 100644 --- a/flow/models/Service.rb +++ b/flow/models/Service.rb @@ -31,7 +31,8 @@ module OpenNebula 'UNKNOWN' => 5, 'DONE' => 6, 'FAILED_UNDEPLOYING' => 7, - 'FAILED_DEPLOYING' => 8 + 'FAILED_DEPLOYING' => 8, + 'SCALING' => 9 } STATE_STR = [ @@ -43,7 +44,8 @@ module OpenNebula 'UNKNOWN', 'DONE', 'FAILED_UNDEPLOYING', - 'FAILED_DEPLOYING' + 'FAILED_DEPLOYING', + 'SCALING' ] LOG_COMP = "SER" @@ -70,7 +72,7 @@ module OpenNebula # @param [Integer] the new state # @return [true, false] true if the value was changed def set_state(state) - if state < 0 || state > 8 + if state < 0 || state > STATE_STR.size return false end @@ -142,18 +144,23 @@ module OpenNebula return false end - # If any role scalability rule is triggered, the role is set to DEPLOYING + # If any role scalability rule is triggered, the role is set to PENDING # and the cardinality is updated # - # @return true if any role is going to scale up - def scale_up?() + # @return true if any role is going to scale up or down + def scale?() scale = false @roles.each { |name, role| - if role_scale_up?(role) + if role.scale_up? role.set_cardinality( role.cardinality + 1 ) role.set_state(Role::STATE['PENDING']) + scale = true + elsif role.scale_down? + role.set_cardinality( role.cardinality - 1 ) + role.set_state(Role::STATE['PENDING']) + scale = true end } @@ -161,38 +168,6 @@ module OpenNebula return scale end - # Method stub, returns true if any VM has an attribute SCALE_UP=YES - def role_scale_up?(role) - parser = ElasticityGrammarParser.new - - elas_expr = role.up_expr - - if elas_expr.nil? - return false - end - - Log.debug "ELAS", "Role #{role.name} up_expr: #{elas_expr}" - - treetop = parser.parse(elas_expr) - if treetop.nil? - Log.debug "ELAS", "up_expr parse error" - end - - result = treetop.result(role) - - if result - Log.debug "ELAS", "Role #{role.name} to scale up" - - if role.cardinality() < role.max_cardinality() - return true - else - Log.debug "ELAS", "Role #{role.name} has reached its VM limit" - end - end - - return false - end - # Create a new service based on the template provided # @param [String] template_json # @return [nil, OpenNebula::Error] nil in case of success, Error