F OpenNebula/one#8020: Expand DRS debug capabilities (#4602)

Signed-off-by: ms4ons <mstojiljkovic@opennebula.io>
This commit is contained in:
Mirko Stojiljković authored and GitHub committed 2026-10-28 15:33:01 +01:00
1 parent e87261a2fc
commit 865f49e37f
3 files changed
+85 -19

No files matched your search

@@ -54,6 +54,7 @@
#
# DIFFERENT_VNETS: When set (YES) the NICs of a VM will be forced to be in
# different Virtual Networks.
# DEBUG: When set (YES), enables more verbose logging.
#*******************************************************************************
DEFAULT_SCHED:
@@ -78,4 +79,6 @@ PREDICTIVE: 0
MEMORY_SYSTEM_DS_SCALE: 0
DIFFERENT_VNETS: YES
DIFFERENT_VNETS: YES
DEBUG: NO
@@ -82,13 +82,16 @@ class OptimizerParser:
"PREDICTIVE": 0,
"MEMORY_SYSTEM_DS_SCALE": 0,
"DIFFERENT_VNETS": True,
"DEBUG": False,
}
__slots__ = (
"parser",
"scheduler_driver_action",
"config",
"debug",
"mode",
"cluster_id",
"_plan_id",
"_system_local_dstore_hosts",
"_system_local_dstore_attrs",
@@ -106,16 +109,20 @@ class OptimizerParser:
io.BytesIO(stdin_data), SchedulerDriverAction
)
self.config = self._load_config(mode)
self.debug = self.config["DEBUG"]
self.mode = mode
self.cluster_id = -1
self._plan_id = -1
action = self.scheduler_driver_action
# Remove `STOPPED` and `UNDEPLOYED` VMs.
if self.mode.upper() == "OPTIMIZE":
self.cluster_id = int(action.cluster_pool.cluster[0].id)
skipped_states = {4, 9}
vms = self.scheduler_driver_action.vm_pool.vm
vms = action.vm_pool.vm
vms[:] = [vm for vm in vms if vm.state not in skipped_states]
vm_ids = [vm.id for vm in vms]
vm_reqs = self.scheduler_driver_action.requirements.vm
vm_reqs = action.requirements.vm
vm_reqs[:] = [vm_req for vm_req in vm_reqs if vm_req.id in vm_ids]
# The required attributes of the datastores.
@@ -125,7 +132,7 @@ class OptimizerParser:
shared_dstore_attrs: dict[int, dict[str, Any]] = {}
# Dict {dstore id: dstore attrs} for image datastores.
image_dstore_attrs: dict[int, dict[str, Any]] = {}
for data in self.scheduler_driver_action.datastore_pool.datastore:
for data in action.datastore_pool.datastore:
type_ = self.DATASTORE_TYPE[data.type_value]
tm_mad = data.tm_mad.upper()
attrs: dict[str, Any] = {"TYPE": type_, "TM_MAD": tm_mad}
@@ -156,7 +163,7 @@ class OptimizerParser:
# The hosts assiciated to the system local datastores.
# Dict {dstore id: host ids} for system local datastores.
local_dstore_hosts: defaultdict[int, set[int]] = defaultdict(set)
for host_data in self.scheduler_driver_action.host_pool.host:
for host_data in action.host_pool.host:
host_id = host_data.id
for dstore_data in host_data.host_share.datastores.ds:
if dstore_data.id in local_dstore_attrs:
@@ -167,7 +174,7 @@ class OptimizerParser:
curr_alloc: dict[int, int] = {}
used_local_dstores: dict[int, int] = {}
used_shared_dstores: dict[int, int] = {}
for data in self.scheduler_driver_action.vm_pool.vm:
for data in action.vm_pool.vm:
if vm_hist := data.history_records.history:
vm_id = int(data.id)
last_rec = max(vm_hist, key=lambda item: int(item.seq))
@@ -224,7 +231,8 @@ class OptimizerParser:
state = f"state {vm.state}"
if vm.state == 3:
state = f"{state} and LCM state {vm.lcm_state}"
msg = f"Optimization skipped: VM {vm.id} in the {state}"
state = f"VM {vm.id} in the {state}"
msg = f"Optimization of cluster {self.cluster_id} skipped: {state}"
self.log_general("ERROR", msg)
return unsupported
@@ -284,6 +292,13 @@ class OptimizerParser:
sched_config["PREDICTIVE"] = config_data.get(
"PREDICTIVE", cls.DEFAULT_CONFIG["PREDICTIVE"]
)
if "DEBUG" in config_data:
debug = config_data["DEBUG"]
sched_config["DEBUG"] = (
debug is True or str(debug).upper() == "YES"
)
else:
sched_config["DEBUG"] = cls.DEFAULT_CONFIG["DEBUG"]
return {
"MODE": mode_config,
@@ -333,7 +348,7 @@ class OptimizerParser:
if policy.upper() == "BALANCE"
else policy.lower()
)
self._plan_id = self.scheduler_driver_action.cluster_pool.cluster[0].id
self._plan_id = self.cluster_id
vmg, affined_hosts, anti_affined_hosts = self._parse_vm_groups()
vm_reqs_dict = self._parse_vm_requirements()
for vm_req in self.scheduler_driver_action.requirements.vm:
+59 -11
View File
@@ -23,6 +23,7 @@ sys.path.insert(
"/usr/lib/one/python/",
)
import base64
from datetime import datetime
import time
from lib import OptimizerParser, OptimizerSerializer
@@ -37,28 +38,75 @@ def main():
parser = OptimizerParser(stdin_data=decoded_data, mode="optimize")
if parser.check_vm_states():
sys.exit(1)
optimizer = parser.build_optimizer()
debug = parser.debug
cluster_id = parser.cluster_id
msg = f"Optimization of cluster {cluster_id}: parsing --"
try:
optimizer = parser.build_optimizer()
if debug:
sys.stderr.write(f"INFO: {msg} SUCCESS\n")
except Exception as exc:
sys.stderr.write(f"ERROR: {msg} {exc}\n")
sys.exit(1)
# Execute Optimizer
msg = f"Optimization of cluster {cluster_id}: mapping --"
start_time = time.time()
optimizer.map()
elapsed = time.time() - start_time
placement = optimizer._opt_placement
elapsed = -1
try:
optimizer.map()
elapsed = time.time() - start_time
placement = optimizer._opt_placement
if debug:
sys.stderr.write(f"INFO: {msg} SUCCESS\n")
except Exception as exc:
sys.stderr.write(f"ERROR: {msg} {exc}\n")
sys.exit(1)
if debug:
try:
# Write the optimizer report.
with open("/tmp/onedrs_report.txt", mode="w") as file:
out = optimizer.report()
file.write(f"{datetime.now()}\n\n{out}\n")
except Exception as exc:
sys.stderr.write(f"ERROR: Writing onedrs_report.txt -- {exc}\n")
try:
# Write the allocations.
with open("/tmp/onedrs_allocs.txt", mode="w") as file:
out = "\n".join(str(val) for val in placement.values())
file.write(f"{datetime.now()}\n\n{out}\n")
except Exception as exc:
sys.stderr.write(f"ERROR: Writing onedrs_allocs.txt -- {exc}\n")
# Serialize Optimizer response
serializer = OptimizerSerializer(parser.plan_id)
scheduler_output, logs = serializer.build_optimizer_output(
curr_placement=optimizer._curr_placement,
opt_placement=placement
)
xml_output = serializer.render(scheduler_output)
sys.stdout.write(xml_output)
msg = f"Optimization of cluster {cluster_id}: serializing --"
try:
serializer = OptimizerSerializer(parser.plan_id)
scheduler_output, logs = serializer.build_optimizer_output(
curr_placement=optimizer._curr_placement,
opt_placement=placement
)
xml_output = serializer.render(scheduler_output)
sys.stdout.write(xml_output)
if debug:
sys.stderr.write(f"INFO: {msg} SUCCESS\n")
except Exception as exc:
sys.stderr.write(f"ERROR: {msg} {exc}\n")
sys.exit(1)
if debug:
try:
# Write the output.
with open("/tmp/onedrs_output.txt", mode="w") as file:
file.write(f"{datetime.now()}\n\nOutput:\n\n{xml_output}\n")
except Exception as exc:
sys.stderr.write(f"ERROR: Writing onedrs_output.txt -- {exc}\n")
# VM log messages
for vm_id, level, message in logs:
parser.log_vm(level, vm_id, message)
# General log message
# TODO: Take the optimizer status from its model.
status = optimizer.report().splitlines()[1].strip().upper()
if status == "ERROR" or status == "INFEASIBLE":
parser.log_general("ERROR", "Unable to find optimal solution")