forked from ubccr/coldfront
-
Notifications
You must be signed in to change notification settings - Fork 1
Parse sinfo output to manage resources #370
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
antonio-rodriguez-tam
wants to merge
8
commits into
fasrc:master
Choose a base branch
from
theam:sinfo_resource_management
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6f20298
Parse sinfo output to manage resources
antonio-rodriguez-tam 0a8649a
Merge branch 'master' into sinfo_resource_management
antonio-rodriguez-tam aef859d
Add resourceattrtypes to defauls command. Fix parsing of gpu count. P…
antonio-rodriguez-tam 37c36b4
Add resourceattrtypes to defauls command. Fix parsing of gpu count. P…
antonio-rodriguez-tam 357cc4e
Merge branch 'master' into sinfo_resource_management
antonio-rodriguez-tam 5d2df29
Use bulk_update_with_history and bulk_create_with_history to generate…
antonio-rodriguez-tam 2b6be8d
Merge branch 'sinfo_resource_management' of https://github.com/theam/…
antonio-rodriguez-tam 7abd545
Add current cluster as parent resoruce for compute nodes
antonio-rodriguez-tam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
179 changes: 179 additions & 0 deletions
179
coldfront/plugins/slurm/management/commands/slurm_manage_resources.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
import logging | ||
import os | ||
import re | ||
from functools import reduce | ||
from cProfile import Profile | ||
|
||
from django.core.management.base import BaseCommand, CommandError | ||
from simple_history.utils import bulk_update_with_history, bulk_create_with_history | ||
|
||
from coldfront.core.resource.models import ResourceType, ResourceAttribute, ResourceAttributeType, AttributeType, Resource | ||
from coldfront.core.project.models import Project | ||
from coldfront.plugins.slurm.utils import slurm_get_nodes_info | ||
from django.utils.datetime_safe import datetime | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
class Command(BaseCommand): | ||
help = 'Manage slurm resources from sinfo output' | ||
|
||
def get_output_from_file(self, file_path): | ||
try: | ||
keys = None | ||
with open(file_path, 'r') as output_file: | ||
for line in output_file: | ||
if keys is None: | ||
keys = re.sub(r'\s+', ' ', line).strip().lower().split(' ') | ||
else: | ||
values = re.sub(r'\s+', ' ', line).strip().split(' ') | ||
yield dict(zip(keys, values)) | ||
except FileNotFoundError: | ||
logger.error(f"File at {file_path} does not exist. Cant simulate output.") | ||
except IOError as e: | ||
logger.error(f"An error occurred: {e}") | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument("-e", "--environment", help="Environment, use dev to simulate output") | ||
parser.add_argument('--profile', action='store_true', default=False) | ||
|
||
def handle(self, *args, **options): | ||
if options.get('profile', False): | ||
profiler = Profile() | ||
profiler.runcall(self._handle, *args, **options) | ||
profiler.print_stats() | ||
else: | ||
self._handle(*args, **options) | ||
|
||
def _handle(self, *args, **options): | ||
def calculate_gpu_count(gres_value): | ||
if 'null' in gres_value: | ||
return 0 | ||
gpu_list = gres_value.split('),') | ||
return reduce(lambda x, y: x + y,[int(gpu_info.split(':')[2].replace('(S','')) for gpu_info in gpu_list]) | ||
|
||
def calculate_cpu_count(row): | ||
if row.get('s:c:t', None) is None: | ||
return 0 | ||
cpu_count = row.get('s:c:t').split(':')[1] | ||
return int(cpu_count) | ||
|
||
def calculate_owner_value(project_list, row): | ||
owner_name = '' | ||
project_name_list = [project.title for project in project_list] | ||
owner_lists = row.get('groups', '').split(',') | ||
owner_project = [name_owner for name_owner in owner_lists if name_owner in project_name_list] | ||
if len(owner_project) > 0: | ||
return owner_project[0] | ||
if {'cluster_users', 'slurm-admin'}.issubset(set(owner_lists)): | ||
return'FASRC' | ||
return owner_name | ||
|
||
env = options['environment'] or 'production' | ||
if 'dev' in env: | ||
output = self.get_output_from_file(os.path.join(os.getcwd(), 'coldfront/plugins/slurm/management/commands/sinfo_output.txt')) | ||
else: | ||
output = slurm_get_nodes_info() | ||
logger.debug(f'Running on {env} mode') | ||
project_list = Project.objects.all() | ||
compute_node = ResourceType.objects.get(name='Compute Node') | ||
attribute_type_name_list = ['GPU Count', 'Core Count', 'Features', 'Owner', 'ServiceEnd'] | ||
partition_resource_type = ResourceType.objects.get(name='Cluster Partition') | ||
gpu_count_attribute_type = ResourceAttributeType.objects.get(name='GPU Count') | ||
core_count_attribute_type = ResourceAttributeType.objects.get(name='Core Count') | ||
features_attribute_type = ResourceAttributeType.objects.get(name='Features') | ||
owner_attribute_type = ResourceAttributeType.objects.get(name='Owner') | ||
service_end_attribute_type = ResourceAttributeType.objects.get(name='ServiceEnd') | ||
existing_resource_attributes = list(ResourceAttribute.objects.filter( | ||
resource_attribute_type__name__in=attribute_type_name_list, | ||
resource__resource_type__name='Compute Node' | ||
).values_list('pk', 'resource__name', 'resource_attribute_type__name') | ||
) | ||
existing_resource_attributes_check = [f'{resource_att[1]} {resource_att[2]}' for resource_att in existing_resource_attributes] | ||
existing_resource_attributes_pk_map = {f'{resource_att[1]} {resource_att[2]}': resource_att[0] for resource_att in existing_resource_attributes} | ||
processed_resources = set() | ||
bulk_update_resource_attribute = [] | ||
bulk_create_resource_attribute = [] | ||
bulk_update_resource = [] | ||
processed_resource_attribute = [] | ||
for row in output: | ||
new_resource, compute_node_created_created = Resource.objects.get_or_create(name=row['nodelist'], defaults={'is_allocatable':False, 'resource_type':compute_node}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we also add parent_resource=(Cluster Resource where the node is installed) to the defaults here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
Resource.objects.get_or_create(name=row['partition'], defaults={'resource_type':partition_resource_type}) | ||
|
||
gpu_count = ResourceAttribute(resource_attribute_type=gpu_count_attribute_type, resource=new_resource, value=calculate_gpu_count(row['gres'])) | ||
gpu_count_key = f"{row['nodelist']} {gpu_count_attribute_type.name}" | ||
if gpu_count_key in existing_resource_attributes_check: | ||
gpu_count.pk = existing_resource_attributes_pk_map[gpu_count_key] | ||
bulk_update_resource_attribute.append(gpu_count) | ||
else: | ||
if gpu_count_key not in processed_resource_attribute: | ||
bulk_create_resource_attribute.append(gpu_count) | ||
processed_resource_attribute.append(gpu_count_key) | ||
|
||
core_count = ResourceAttribute(resource_attribute_type=core_count_attribute_type, resource=new_resource, value=calculate_cpu_count(row)) | ||
core_count_key = f"{row['nodelist']} {core_count_attribute_type.name}" | ||
if core_count_key in existing_resource_attributes_check: | ||
core_count.pk = existing_resource_attributes_pk_map[core_count_key] | ||
bulk_update_resource_attribute.append(core_count) | ||
else: | ||
if core_count_key not in processed_resource_attribute: | ||
bulk_create_resource_attribute.append(core_count) | ||
processed_resource_attribute.append(core_count_key) | ||
|
||
features = ResourceAttribute(resource_attribute_type=features_attribute_type, resource=new_resource, value=row.get('avail_features', '(null)')) | ||
features_key = f"{row['nodelist']} {features_attribute_type.name}" | ||
if features_key in existing_resource_attributes_check: | ||
features.pk = existing_resource_attributes_pk_map[features_key] | ||
bulk_update_resource_attribute.append(features) | ||
else: | ||
if features_key not in processed_resource_attribute: | ||
bulk_create_resource_attribute.append(features) | ||
processed_resource_attribute.append(features_key) | ||
|
||
owner = ResourceAttribute(resource_attribute_type=owner_attribute_type, resource=new_resource, value=calculate_owner_value(project_list, row)) | ||
owner_key = f"{row['nodelist']} {owner_attribute_type.name}" | ||
if owner_key in existing_resource_attributes_check: | ||
owner.pk = existing_resource_attributes_pk_map[owner_key] | ||
bulk_update_resource_attribute.append(owner) | ||
else: | ||
if owner_key not in processed_resource_attribute: | ||
bulk_create_resource_attribute.append(owner) | ||
processed_resource_attribute.append(owner_key) | ||
|
||
if new_resource.is_available is False: | ||
new_resource.is_available = True | ||
bulk_update_resource.append(new_resource) | ||
service_end_pk = existing_resource_attributes_pk_map[f"{row['nodelist']} {service_end_attribute_type.name}"] | ||
bulk_update_resource_attribute.append(ResourceAttribute(resource=new_resource, value=None, resource_attribute_type=service_end_attribute_type, pk=service_end_pk)) | ||
processed_resources.add(new_resource.name) | ||
try: | ||
logger.debug(f'Updating {len(bulk_update_resource_attribute)} ResourceAttribute records') | ||
bulk_update_with_history(bulk_update_resource_attribute, ResourceAttribute, ['value'], batch_size=500, default_change_reason='slurm_manage_resource command') | ||
logger.debug(f'Updating {len(bulk_update_resource)} Resource records') | ||
bulk_update_with_history(bulk_update_resource, Resource, ['is_available'], batch_size=500, default_change_reason='slurm_manage_resource command') | ||
logger.debug(f'Creating {len(bulk_create_resource_attribute)} ResourceAttribute records') | ||
bulk_create_with_history(bulk_create_resource_attribute, ResourceAttribute, batch_size=500, default_change_reason='slurm_manage_resource command') | ||
except Exception as e: | ||
logger.debug(f'Error processing resources info: {str(e)}') | ||
raise | ||
bulk_update_resource_attribute = [] | ||
bulk_create_resource_attribute = [] | ||
bulk_update_resource = [] | ||
for resource_to_delete in Resource.objects.exclude(name__in=list(processed_resources)).filter(is_available=True, resource_type=compute_node): | ||
resource_to_delete.is_available = False | ||
bulk_update_resource.append(resource_to_delete) | ||
service_end = ResourceAttribute(resource=resource_to_delete, value=str(datetime.now()), resource_attribute_type=service_end_attribute_type) | ||
if f"{resource_to_delete.name} {service_end_attribute_type.name}" in existing_resource_attributes_check: | ||
service_end.pk = existing_resource_attributes_pk_map[f"{resource_to_delete.name} {service_end_attribute_type.name}"] | ||
bulk_update_resource_attribute.append(service_end) | ||
else: | ||
bulk_create_resource_attribute.append(service_end) | ||
try: | ||
logger.debug(f'Decommissioning {len(bulk_update_resource)} Resource records') | ||
bulk_update_with_history(bulk_update_resource, Resource, ['is_available'], batch_size=500, default_change_reason='slurm_manage_resource command') | ||
logger.debug(f'Creating {len(bulk_create_resource_attribute)} ServiceEnd ResourceAttribute records') | ||
bulk_create_with_history(bulk_create_resource_attribute, ResourceAttribute, batch_size=500, default_change_reason='slurm_manage_resource command') | ||
logger.debug(f'Updating {len(bulk_update_resource_attribute)} ServiceEnd ResourceAttribute records') | ||
bulk_update_with_history(bulk_update_resource_attribute, ResourceAttribute, ['value'], batch_size=500, default_change_reason='slurm_manage_resource command') | ||
except Exception as e: | ||
logger.error(f'Error cleaning up resources: {str(e)}') | ||
raise |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.