|
17 | 17 |
|
18 | 18 | from sm_typing import override |
19 | 19 |
|
| 20 | +import contextlib |
20 | 21 | import errno |
| 22 | +import flock |
21 | 23 | import json |
22 | 24 | import linstor |
23 | 25 | import os.path |
|
43 | 45 |
|
44 | 46 | DRBD_BY_RES_PATH = '/dev/drbd/by-res/' |
45 | 47 |
|
| 48 | +CONTROLLER_CACHE_DIRECTORY = os.environ.get('TMPDIR', '/tmp') + '/linstor' |
| 49 | +CONTROLLER_CACHE_FILE = 'controller_uri' |
| 50 | +CONTROLLER_CACHE_PATH = "{}/{}".format(CONTROLLER_CACHE_DIRECTORY, CONTROLLER_CACHE_FILE) |
| 51 | + |
46 | 52 | PLUGIN = 'linstor-manager' |
47 | 53 |
|
48 | 54 |
|
@@ -196,17 +202,123 @@ def _get_controller_uri(): |
196 | 202 | # Not found, maybe we are trying to create the SR... |
197 | 203 | pass |
198 | 204 |
|
199 | | -def get_controller_uri(): |
200 | | - retries = 0 |
201 | | - while True: |
202 | | - uri = _get_controller_uri() |
203 | | - if uri: |
204 | | - return uri |
205 | 205 |
|
206 | | - retries += 1 |
207 | | - if retries >= 10: |
208 | | - break |
209 | | - time.sleep(1) |
| 206 | +@contextlib.contextmanager |
| 207 | +def shared_reader(path): |
| 208 | + with open(path, 'r') as f: |
| 209 | + lock = flock.ReadLock(f.fileno()) |
| 210 | + lock.lock() |
| 211 | + try: |
| 212 | + yield f |
| 213 | + finally: |
| 214 | + lock.unlock() |
| 215 | + |
| 216 | + |
| 217 | +@contextlib.contextmanager |
| 218 | +def excl_writer(path): |
| 219 | + with open(path, 'r+') as f: |
| 220 | + lock = flock.WriteLock(f.fileno()) |
| 221 | + lock.lock() |
| 222 | + try: |
| 223 | + yield f |
| 224 | + finally: |
| 225 | + lock.unlock() |
| 226 | + |
| 227 | + |
| 228 | +def _read_controller_uri_from_file(f): |
| 229 | + try: |
| 230 | + return f.read().strip() |
| 231 | + except Exception as e: |
| 232 | + util.SMlog('Unable to read controller URI cache file at `{}`: {}'.format(CONTROLLER_CACHE_PATH, e)) |
| 233 | + |
| 234 | + |
| 235 | +def _write_controller_uri_to_file(uri, f): |
| 236 | + try: |
| 237 | + f.seek(0) |
| 238 | + f.write(uri) |
| 239 | + f.truncate() |
| 240 | + except Exception as e: |
| 241 | + util.SMlog('Unable to write URI cache file at `{}` : {}'.format(CONTROLLER_CACHE_PATH, e)) |
| 242 | + |
| 243 | + |
| 244 | +def _delete_controller_uri_from_file(f): |
| 245 | + try: |
| 246 | + f.seek(0) |
| 247 | + f.truncate() |
| 248 | + except Exception as e: |
| 249 | + util.SMlog('Unable to delete URI cache file at `{}` : {}'.format(CONTROLLER_CACHE_PATH, e)) |
| 250 | + |
| 251 | + |
| 252 | +def read_controller_uri_cache(): |
| 253 | + try: |
| 254 | + with shared_reader(CONTROLLER_CACHE_PATH) as f: |
| 255 | + return _read_controller_uri_from_file(f) |
| 256 | + except FileNotFoundError: |
| 257 | + pass |
| 258 | + except Exception as e: |
| 259 | + util.SMlog('Unable to read controller URI cache file at `{}`: {}'.format(CONTROLLER_CACHE_PATH, e)) |
| 260 | + |
| 261 | + |
| 262 | +def write_controller_uri_cache(uri): |
| 263 | + try: |
| 264 | + with excl_writer(CONTROLLER_CACHE_PATH) as f: |
| 265 | + _write_controller_uri_to_file(uri, f) |
| 266 | + except FileNotFoundError: |
| 267 | + if os.path.exists(CONTROLLER_CACHE_DIRECTORY): |
| 268 | + raise |
| 269 | + os.makedirs(CONTROLLER_CACHE_DIRECTORY) |
| 270 | + os.chmod(CONTROLLER_CACHE_DIRECTORY, 0o700) |
| 271 | + return write_controller_uri_cache(uri) |
| 272 | + except Exception as e: |
| 273 | + util.SMlog('Unable to write URI cache file at `{}` : {}'.format(CONTROLLER_CACHE_PATH, e)) |
| 274 | + |
| 275 | + |
| 276 | +def delete_controller_uri_cache(uri=None): |
| 277 | + try: |
| 278 | + with excl_writer(CONTROLLER_CACHE_PATH) as f: |
| 279 | + if uri and uri != _read_controller_uri_from_file(f): |
| 280 | + return |
| 281 | + f.seek(0) |
| 282 | + f.truncate() |
| 283 | + except FileNotFoundError: |
| 284 | + pass |
| 285 | + except Exception as e: |
| 286 | + util.SMlog('Unable to delete URI cache file at `{}` : {}'.format(CONTROLLER_CACHE_PATH, e)) |
| 287 | + |
| 288 | + |
| 289 | +def build_controller_uri_cache(): |
| 290 | + uri = '' |
| 291 | + try: |
| 292 | + with excl_writer(CONTROLLER_CACHE_PATH) as f: |
| 293 | + uri = _read_controller_uri_from_file(f) |
| 294 | + if uri: |
| 295 | + return uri |
| 296 | + uri = _get_controller_uri() |
| 297 | + if not uri: |
| 298 | + for retries in range(9): |
| 299 | + time.sleep(1) |
| 300 | + uri = _get_controller_uri() |
| 301 | + if uri: |
| 302 | + break |
| 303 | + if uri: |
| 304 | + _write_controller_uri_to_file(uri, f) |
| 305 | + except FileNotFoundError: |
| 306 | + if os.path.exists(CONTROLLER_CACHE_DIRECTORY): |
| 307 | + raise |
| 308 | + os.makedirs(CONTROLLER_CACHE_DIRECTORY) |
| 309 | + os.chmod(CONTROLLER_CACHE_DIRECTORY, 0o700) |
| 310 | + return build_controller_uri_cache() |
| 311 | + except Exception as e: |
| 312 | + util.SMlog('Unable to write URI cache file at `{}` : {}'.format(CONTROLLER_CACHE_PATH, e)) |
| 313 | + |
| 314 | + return uri |
| 315 | + |
| 316 | + |
| 317 | +def get_controller_uri(): |
| 318 | + uri = read_controller_uri_cache() |
| 319 | + if not uri: |
| 320 | + uri = build_controller_uri_cache() |
| 321 | + return uri |
210 | 322 |
|
211 | 323 |
|
212 | 324 | def get_controller_node_name(): |
@@ -429,6 +541,34 @@ def __init__( |
429 | 541 | self._volume_info_cache_dirty = True |
430 | 542 | self._build_volumes(repair=repair) |
431 | 543 |
|
| 544 | + @staticmethod |
| 545 | + def create_from_cache( |
| 546 | + group_name, repair=False, logger=default_logger.__func__, |
| 547 | + attempt_count=30 |
| 548 | + ): |
| 549 | + """ |
| 550 | + Attempt to create a LinstorVolumeManager from cached data. |
| 551 | + If it fails, refresh the cache and retry once. |
| 552 | +
|
| 553 | + :param str group_name: The SR goup name to use. |
| 554 | + :param bool repair: If true we try to remove bad volumes due to a crash |
| 555 | + or unexpected behavior. |
| 556 | + :param function logger: Function to log messages. |
| 557 | + :param int attempt_count: Number of attempts to join the controller. |
| 558 | + """ |
| 559 | + uri = read_controller_uri_cache() |
| 560 | + if not uri: |
| 561 | + uri = build_controller_uri_cache() |
| 562 | + if not uri: |
| 563 | + raise LinstorVolumeManagerError( |
| 564 | + "Unable to retrieve a valid controller URI from cache or after rebuild." |
| 565 | + ) |
| 566 | + |
| 567 | + return LinstorVolumeManager( |
| 568 | + uri, group_name, repair=repair, |
| 569 | + logger=logger, attempt_count=attempt_count |
| 570 | + ) |
| 571 | + |
432 | 572 | @property |
433 | 573 | def group_name(self): |
434 | 574 | """ |
@@ -1772,6 +1912,7 @@ def create_sr( |
1772 | 1912 | DATABASE_PATH, |
1773 | 1913 | mount=False |
1774 | 1914 | ) |
| 1915 | + delete_controller_uri_cache() |
1775 | 1916 | return sr |
1776 | 1917 |
|
1777 | 1918 | @classmethod |
@@ -2615,8 +2756,10 @@ def connect(uri): |
2615 | 2756 |
|
2616 | 2757 | try: |
2617 | 2758 | return connect(uri) |
2618 | | - except (linstor.errors.LinstorNetworkError, LinstorVolumeManagerError): |
| 2759 | + except LinstorVolumeManagerError: |
2619 | 2760 | pass |
| 2761 | + except linstor.errors.LinstorNetworkError: |
| 2762 | + delete_controller_uri_cache(uri) |
2620 | 2763 |
|
2621 | 2764 | if not keep_uri_unmodified: |
2622 | 2765 | uri = None |
|
0 commit comments