Skip to content

Commit 946d6f3

Browse files
Update scaling_manager.py
updated to work on docker
1 parent d4aee93 commit 946d6f3

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

lxc_autoscale/scaling_manager.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
)
1111
from notification import send_notification # Import the notification function
1212
from config import HORIZONTAL_SCALING_GROUPS, IGNORE_LXC, DEFAULTS # Import configuration constants
13+
import paramiko
1314

1415
# Constants for repeated values
1516
TIMEOUT_EXTENDED = 300 # Extended timeout for scaling operations
@@ -129,7 +130,7 @@ def scale_memory(ctid, mem_usage, mem_upper, mem_lower, current_memory, min_memo
129130

130131
elif mem_usage < mem_lower and current_memory > min_memory:
131132
decrease_amount = calculate_decrement(
132-
mem_usage, mem_lower, current_memory,
133+
mem_usage, mem_lower, current_memory,
133134
int(config['min_decrease_chunk'] * behaviour_multiplier), min_memory
134135
)
135136
if decrease_amount > 0:
@@ -146,7 +147,7 @@ def scale_memory(ctid, mem_usage, mem_upper, mem_lower, current_memory, min_memo
146147
def adjust_resources(containers, energy_mode):
147148
"""
148149
Adjust CPU and memory resources for each container based on usage.
149-
150+
150151
Args:
151152
containers (dict): A dictionary of container resource usage data.
152153
energy_mode (bool): Flag to indicate if energy-saving adjustments should be made during off-peak hours.
@@ -172,7 +173,7 @@ def adjust_resources(containers, energy_mode):
172173
rounded_mem_usage = round(usage['mem'], 2)
173174
total_mem_allocated = usage['initial_memory']
174175
free_mem_percent = round(100 - ((rounded_mem_usage / total_mem_allocated) * 100), 2)
175-
176+
176177
logging.info(f"Container {ctid}: CPU usage: {rounded_cpu_usage}%, Memory usage: {rounded_mem_usage}MB "
177178
f"({free_mem_percent}% free of {total_mem_allocated}MB total), "
178179
f"Initial cores: {usage['initial_cores']}, Initial memory: {total_mem_allocated}MB")
@@ -219,7 +220,7 @@ def adjust_resources(containers, energy_mode):
219220
send_notification(f"CPU Increased for Container {ctid}", f"CPU cores increased to {new_cores}.")
220221
else:
221222
logging.warning(f"Container {ctid} - Not enough available cores to increase.")
222-
223+
223224
elif cpu_usage < cpu_lower and current_cores > min_cores:
224225
decrement = calculate_decrement(cpu_usage, cpu_lower, current_cores, config['core_min_increment'], min_cores)
225226
new_cores = max(min_cores, current_cores - decrement)
@@ -274,7 +275,7 @@ def manage_horizontal_scaling(containers):
274275
total_cpu_usage = sum(containers[ctid]['cpu'] for ctid in group_config['lxc_containers'] if ctid in containers)
275276
total_mem_usage = sum(containers[ctid]['mem'] for ctid in group_config['lxc_containers'] if ctid in containers)
276277
num_containers = len(group_config['lxc_containers'])
277-
278+
278279
if num_containers > 0:
279280
avg_cpu_usage = total_cpu_usage / num_containers
280281
avg_mem_usage = total_mem_usage / num_containers
@@ -285,10 +286,10 @@ def manage_horizontal_scaling(containers):
285286
logging.debug(f"Group: {group_name} | Average CPU Usage: {avg_cpu_usage}% | Average Memory Usage: {avg_mem_usage}%")
286287

287288
# Check if scaling out is needed based on usage thresholds
288-
if (avg_cpu_usage > group_config['horiz_cpu_upper_threshold'] or
289+
if (avg_cpu_usage > group_config['horiz_cpu_upper_threshold'] or
289290
avg_mem_usage > group_config['horiz_memory_upper_threshold']):
290291
logging.debug(f"Thresholds exceeded for {group_name}. Evaluating scale-out conditions.")
291-
292+
292293
# Ensure enough time has passed since the last scaling action
293294
if current_time - last_action_time >= timedelta(seconds=group_config.get('scale_out_grace_period', 300)):
294295
scale_out(group_name, group_config)

0 commit comments

Comments
 (0)