|
1 | 1 | import logging |
2 | 2 | import re |
3 | 3 | from time import sleep |
4 | | -from typing import Optional |
5 | | -from cmapi_server.constants import SHMEM_LOCKS_PATH, RESET_LOCKS_PATH |
| 4 | +from typing import Optional, List, Tuple |
| 5 | +from cmapi_server.constants import SHMEM_LOCKS_PATH |
6 | 6 | from cmapi_server.process_dispatchers.base import BaseDispatcher |
7 | 7 |
|
8 | 8 |
|
9 | | -def parse_locks_num(cmd_output: str) -> int: |
10 | | - """Parse output of mcs-shmem-locks command.""" |
11 | | - active_total = 0 |
| 9 | +def parse_locks_state(cmd_output: str, logger: logging.Logger) -> List[Tuple[int, int, int]]: |
| 10 | + """Parse per-lock state from mcs-shmem-locks output. |
| 11 | +
|
| 12 | + Returns a list of tuples: (lock_id, readers, writers) |
| 13 | + """ |
| 14 | + locks: List[Tuple[int, int, int]] = [] |
| 15 | + current_id = 0 |
| 16 | + readers = None |
| 17 | + writers = None |
| 18 | + |
12 | 19 | for line in cmd_output.splitlines(): |
13 | | - m = re.search(r'^\s*(readers|writers)\s*=\s*(\d+)', line) |
| 20 | + if line.strip().endswith('RWLock'): |
| 21 | + # flush previous section counts (if we have both) |
| 22 | + if current_id > 0 and readers is not None and writers is not None: |
| 23 | + locks.append((current_id, readers, writers)) |
| 24 | + current_id += 1 |
| 25 | + readers = None |
| 26 | + writers = None |
| 27 | + continue |
| 28 | + |
| 29 | + m = re.search(r'^\s*readers\s*=\s*(\d+)', line) |
14 | 30 | if m: |
15 | 31 | try: |
16 | | - active_total += int(m.group(2)) |
| 32 | + readers = int(m.group(1)) |
17 | 33 | except ValueError: |
18 | | - pass |
19 | | - return active_total |
| 34 | + logger.warning('Failed to parse readers count from line: %s', line) |
| 35 | + readers = 0 |
| 36 | + continue |
20 | 37 |
|
| 38 | + m = re.search(r'^\s*writers\s*=\s*(\d+)', line) |
| 39 | + if m: |
| 40 | + try: |
| 41 | + writers = int(m.group(1)) |
| 42 | + except ValueError: |
| 43 | + logger.warning('Failed to parse writers count from line: %s', line) |
| 44 | + writers = 0 |
| 45 | + continue |
21 | 46 |
|
22 | | -def get_active_shmem_locks_num(logger: logging.Logger) -> Optional[int]: |
23 | | - """Get number of active shmem locks.""" |
24 | | - cmd = f'{SHMEM_LOCKS_PATH} --lock-id 0' |
25 | | - success, out = BaseDispatcher.exec_command(cmd) |
26 | | - if not success: |
27 | | - logger.error('Failed to inspect shmem locks (command failed)') |
28 | | - return None |
29 | | - if not out: |
30 | | - logger.error('Failed to inspect shmem locks (empty output)') |
31 | | - return None |
| 47 | + # flush the last parsed lock |
| 48 | + if current_id > 0 and readers is not None and writers is not None: |
| 49 | + locks.append((current_id, readers, writers)) |
32 | 50 |
|
33 | | - logger.debug('Current lock state:\n%s', (out or '').strip()) |
| 51 | + return locks |
34 | 52 |
|
35 | | - return parse_locks_num(out) |
36 | 53 |
|
| 54 | +def release_shmem_locks(logger: logging.Logger, max_iterations: int = 5) -> bool: |
| 55 | + """Attempt to release active shmem locks. |
37 | 56 |
|
38 | | -def reset_shmem_locks(logger: logging.Logger) -> None: |
39 | | - """Inspect and reset BRM shmem locks""" |
40 | | - logger.debug('Inspecting and resetting shmem locks.') |
| 57 | + - Inspect all locks. |
| 58 | + - Unlock writer lock (there can be only one) |
| 59 | + - Unlock each reader lock sequentially |
| 60 | + - Re-check and repeat up to max_iterations. |
41 | 61 |
|
42 | | - # Get current lock state |
43 | | - active_locks_num = get_active_shmem_locks_num(logger) |
44 | | - if active_locks_num is None: |
45 | | - return |
| 62 | + Returns True on success (no active readers/writers remain), False otherwise. |
| 63 | + """ |
| 64 | + for attempt in range(1, max_iterations + 1): |
| 65 | + success, out = BaseDispatcher.exec_command(f'{SHMEM_LOCKS_PATH} --lock-id 0') |
| 66 | + if not success or not out: |
| 67 | + logger.error('Failed to inspect shmem locks during unlock (attempt %d)', attempt) |
| 68 | + return False |
46 | 69 |
|
47 | | - # Reset if any read/write locks are active |
48 | | - if active_locks_num > 0: |
49 | | - logger.info('Detected active shmem locks (sum readers+writers=%d). Attempting reset.', active_locks_num) |
| 70 | + locks = parse_locks_state(out, logger=logger) |
50 | 71 |
|
51 | | - # Reset locks |
52 | | - success, out = BaseDispatcher.exec_command(f'{RESET_LOCKS_PATH} -s') |
53 | | - if not success: |
54 | | - logger.error('Failed to reset shmem locks (command failed)') |
55 | | - return |
| 72 | + total_active = sum_active_locks(locks) |
| 73 | + if total_active == 0: |
| 74 | + logger.debug('Unlock attempt %d: no active locks', attempt) |
| 75 | + return True |
| 76 | + logger.debug('Unlock attempt %d: active total=%d; detail=%s', attempt, total_active, locks) |
56 | 77 |
|
57 | | - # Check that locks were reset |
| 78 | + # Issue unlocks per lock |
| 79 | + for lock_id, readers, writers in locks: |
| 80 | + # Unlock writer |
| 81 | + if writers > 0: |
| 82 | + cmd = f'{SHMEM_LOCKS_PATH} -i {lock_id} -w -u' |
| 83 | + ok, _ = BaseDispatcher.exec_command(cmd) |
| 84 | + if not ok: |
| 85 | + logger.warning('Failed to unlock writer for lock-id=%d', lock_id) |
| 86 | + |
| 87 | + # Unlock all readers |
| 88 | + if readers > 0: |
| 89 | + for _ in range(readers): |
| 90 | + cmd = f'{SHMEM_LOCKS_PATH} -i {lock_id} -r -u' |
| 91 | + ok, _ = BaseDispatcher.exec_command(cmd) |
| 92 | + if not ok: |
| 93 | + logger.warning('Failed to unlock a reader for lock-id=%d', lock_id) |
| 94 | + break |
| 95 | + |
| 96 | + # Wait some time for state to settle |
58 | 97 | sleep(1) |
59 | | - active_locks_num = get_active_shmem_locks_num(logger) |
60 | | - if active_locks_num is not None and active_locks_num > 0: |
61 | | - logger.error('Failed to reset shmem locks (locks are still active)') |
62 | | - return |
63 | | - else: |
64 | | - logger.info('No active shmem locks detected.') |
| 98 | + |
| 99 | + logger.error('Failed to fully release shmem locks using mcs-shmem-locks after %d attempts', max_iterations) |
| 100 | + return False |
| 101 | + |
| 102 | + |
| 103 | +def sum_active_locks(locks: List[Tuple[int, int, int]]) -> int: |
| 104 | + return sum(r + w for _, r, w in locks) |
0 commit comments