|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# Copyright (C) SchedMD LLC. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +import fcntl |
| 18 | +import json |
| 19 | +import logging |
| 20 | +from datetime import datetime, timezone |
| 21 | +from pathlib import Path |
| 22 | +import util |
| 23 | +from util import run, to_hostlist, lookup |
| 24 | +log = logging.getLogger() |
| 25 | +REPAIR_FILE = Path("/slurm/repair_operations.json") |
| 26 | +REPAIR_REASONS = frozenset(["PERFORMANCE", "SDC", "XID", "unspecified"]) |
| 27 | + |
| 28 | +def is_node_being_repaired(node): |
| 29 | + """Check if a node is currently being repaired.""" |
| 30 | + operations = get_operations() |
| 31 | + return node in operations and operations[node]["status"] == "REPAIR_IN_PROGRESS" |
| 32 | + |
| 33 | +def get_operations(): |
| 34 | + """Get all repair operations from the file.""" |
| 35 | + if not REPAIR_FILE.exists(): |
| 36 | + return {} |
| 37 | + with open(REPAIR_FILE, 'r') as f: |
| 38 | + try: |
| 39 | + return json.load(f) |
| 40 | + except json.JSONDecodeError: |
| 41 | + return {} |
| 42 | + |
| 43 | +def store_operations(operations): |
| 44 | + """Store the operations to the file.""" |
| 45 | + with open(REPAIR_FILE, 'w') as f: |
| 46 | + fcntl.lockf(f, fcntl.LOCK_EX) |
| 47 | + try: |
| 48 | + json.dump(operations, f, indent=4) |
| 49 | + finally: |
| 50 | + fcntl.lockf(f, fcntl.LOCK_UN) |
| 51 | + |
| 52 | +def store_operation(node, operation_id, reason): |
| 53 | + """Store a single repair operation.""" |
| 54 | + operations = get_operations() |
| 55 | + operations[node] = { |
| 56 | + "operation_id": operation_id, |
| 57 | + "reason": reason, |
| 58 | + "status": "REPAIR_IN_PROGRESS", |
| 59 | + "timestamp": datetime.now(timezone.utc).isoformat(), |
| 60 | + } |
| 61 | + store_operations(operations) |
| 62 | + |
| 63 | +def call_rr_api(node, reason): |
| 64 | + """Call the R&R API for a given node.""" |
| 65 | + log.info(f"Calling R&R API for node {node} with reason {reason}") |
| 66 | + inst = lookup().instance(node) |
| 67 | + if not inst: |
| 68 | + log.error(f"Instance {node} not found, cannot report fault.") |
| 69 | + return None |
| 70 | + cmd = f"gcloud compute instances report-host-as-faulty {node} --async --disruption-schedule=IMMEDIATE --fault-reasons=behavior={reason},description='VM is managed by Slurm' --zone={inst.zone} --format=json" |
| 71 | + try: |
| 72 | + result = run(cmd) |
| 73 | + op = json.loads(result.stdout) |
| 74 | + if isinstance(op, list): |
| 75 | + op = op[0] |
| 76 | + return op["name"] |
| 77 | + except Exception as e: |
| 78 | + log.error(f"Failed to call R&R API for {node}: {e}") |
| 79 | + return None |
| 80 | + |
| 81 | +def get_operation_status(operation_id): |
| 82 | + """Get the status of a GCP operation.""" |
| 83 | + cmd = f'gcloud compute operations list --filter="name={operation_id}" --format=json' |
| 84 | + try: |
| 85 | + result = run(cmd) |
| 86 | + operations_list = json.loads(result.stdout) |
| 87 | + if operations_list and len(operations_list) > 0: |
| 88 | + return operations_list[0] |
| 89 | + |
| 90 | + return None |
| 91 | + except Exception as e: |
| 92 | + log.error(f"Failed to get operation status for {operation_id}: {e}") |
| 93 | + return None |
| 94 | + |
| 95 | +def poll_operations(): |
| 96 | + """Poll the status of ongoing repair operations.""" |
| 97 | + operations = get_operations() |
| 98 | + if not operations: |
| 99 | + return |
| 100 | + |
| 101 | + log.info("Polling repair operations") |
| 102 | + for node, op_details in operations.items(): |
| 103 | + if op_details["status"] == "REPAIR_IN_PROGRESS": |
| 104 | + op_status = get_operation_status(op_details["operation_id"]) |
| 105 | + if not op_status: |
| 106 | + continue |
| 107 | + |
| 108 | + if op_status.get("status") == "DONE": |
| 109 | + if op_status.get("error"): |
| 110 | + log.error(f"Repair operation for {node} failed: {op_status['error']}") |
| 111 | + op_details["status"] = "FAILURE" |
| 112 | + run(f"{lookup().scontrol} update nodename={node} state=down reason='Repair failed'") |
| 113 | + else: |
| 114 | + log.info(f"Repair operation for {node} succeeded.") |
| 115 | + op_details["status"] = "SUCCESS" |
| 116 | + inst = lookup().instance(node) |
| 117 | + if inst and not inst.scheduling.automatic_restart: |
| 118 | + log.info(f"Manually restarting node {node}.") |
| 119 | + try: |
| 120 | + run(f"gcloud compute instances start {node} --zone={inst.zone}") |
| 121 | + except Exception as e: |
| 122 | + log.error(f"Failed to restart instance {node}: {e}") |
| 123 | + run(f"{lookup().scontrol} update nodename={node} state=down reason='Repair successful, but restart failed'") |
| 124 | + if op_details["status"] == "SUCCESS": |
| 125 | + inst = lookup().instance(node) |
| 126 | + if inst and inst.status == "RUNNING": |
| 127 | + log.info(f"Node {node} is back online, undraining.") |
| 128 | + op_details["status"] = "RECOVERED" |
| 129 | + |
| 130 | + store_operations(operations) |
| 131 | + |
0 commit comments