Adds support for cooldown-time variable in scaling-policy 65/6665/3
authorBenjamin Diaz <bdiaz@whitestack.com>
Tue, 9 Oct 2018 18:04:24 +0000 (15:04 -0300)
committerBenjamin Diaz <bdiaz@whitestack.com>
Tue, 9 Oct 2018 20:07:09 +0000 (17:07 -0300)
Cooldown time defines the minimum time that needs to happen between
each scaling action. If an alarm triggers before this has happened,
no action should be executed. When POL receives an alarm notification
it will check that the difference between the current time and the
last scaling action of the alarm's scaling policy is bigger than the
cooldown time.
During configuration of the scaling groups, POL will now store the
cooldown time of the scaling policy. Also, it adds a new last_scale
var to ScalingPolicy, which has as default the oldest time supported
by Python datetime.

Signed-off-by: Benjamin Diaz <bdiaz@whitestack.com>
Change-Id: I6d110ccc7f89af29500c602d05054e7ba78808a0

osm_policy_module/core/agent.py
osm_policy_module/core/database.py

index dfddb3f..77a290f 100644 (file)
@@ -21,6 +21,7 @@
 # For those usages not covered by the Apache License, Version 2.0 please
 # contact: bdiaz@whitestack.com or glavado@whitestack.com
 ##
+import datetime
 import json
 import logging
 import threading
@@ -99,11 +100,17 @@ class PolicyModuleAgent:
             alarm_id, metric_name, operation, threshold, vdu_name, vnf_member_index, ns_id)
         try:
             alarm = ScalingAlarm.select().where(ScalingAlarm.alarm_id == alarm_id).get()
+            delta = datetime.datetime.now() - alarm.scaling_criteria.scaling_policy.last_scale
+            if delta.total_seconds() < alarm.scaling_criteria.scaling_policy.cooldown_time:
+                log.info("Time between last scale and now is less than cooldown time. Skipping.")
+                return
             log.info("Sending scaling action message for ns: %s", alarm_id)
             self.lcm_client.scale(alarm.scaling_criteria.scaling_policy.scaling_group.nsr_id,
                                   alarm.scaling_criteria.scaling_policy.scaling_group.name,
                                   alarm.vnf_member_index,
                                   alarm.action)
+            alarm.scaling_criteria.scaling_policy.last_scale = datetime.datetime.now()
+            alarm.scaling_criteria.scaling_policy.save()
         except ScalingAlarm.DoesNotExist:
             log.info("There is no action configured for alarm %s.", alarm_id)
 
@@ -161,6 +168,7 @@ class PolicyModuleAgent:
                             scaling_policy_record = ScalingPolicy.create(
                                 nsr_id=nsr_id,
                                 name=scaling_policy['name'],
+                                cooldown_time=scaling_policy['cooldown-time'],
                                 scaling_group=scaling_group_record
                             )
                             log.info("Created scaling policy record in DB : name=%s, scaling_group.name=%s",
index 212c13b..5d927d0 100644 (file)
 # For those usages not covered by the Apache License, Version 2.0 please
 # contact: bdiaz@whitestack.com or glavado@whitestack.com
 ##
+import datetime
 import logging
 
-from peewee import CharField, IntegerField, ForeignKeyField, Model, TextField, AutoField
+from peewee import CharField, IntegerField, ForeignKeyField, Model, TextField, AutoField, DateTimeField
 from playhouse.sqlite_ext import SqliteExtDatabase
 
 from osm_policy_module.core.config import Config
@@ -49,6 +50,8 @@ class ScalingGroup(BaseModel):
 
 class ScalingPolicy(BaseModel):
     name = CharField()
+    cooldown_time = IntegerField()
+    last_scale = DateTimeField(default=datetime.datetime.min)
     scaling_group = ForeignKeyField(ScalingGroup, related_name='scaling_policies')