local.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. """
  2. Local Coordinator - Single Node Implementation.
  3. This is the default implementation that operates in single-node mode.
  4. All coordination methods are no-ops or return default values
  5. that work for a single instance.
  6. """
  7. import logging
  8. from typing import Any
  9. from gpustack.server.coordinator.base import Coordinator, Event
  10. logger = logging.getLogger(__name__)
  11. class LocalCoordinator(Coordinator):
  12. """
  13. Local (single-node) implementation of Coordinator.
  14. In this mode:
  15. - This instance is always the leader
  16. - Pub/Sub is local only (no distribution)
  17. - Shared state is in-memory
  18. """
  19. def __init__(self, config: Any = None, **kwargs):
  20. super().__init__(config, **kwargs)
  21. self._started = False
  22. async def start(self):
  23. """Start the local coordinator (no-op)."""
  24. self._started = True
  25. self._is_leader = True
  26. logger.debug("Local coordinator started (single-node mode)")
  27. async def stop(self):
  28. """Stop the local coordinator (no-op)."""
  29. self._started = False
  30. logger.debug("Local coordinator stopped")
  31. # Leader Election - Always leader in local mode
  32. async def acquire_leadership(self, ttl: int) -> bool:
  33. """Always returns True in local mode."""
  34. self._is_leader = True
  35. return True
  36. async def renew_leadership(self, ttl: int) -> bool:
  37. """Always returns True in local mode."""
  38. return True
  39. async def release_leadership(self):
  40. """No-op in local mode."""
  41. self._is_leader = False
  42. # Pub/Sub - Local only
  43. async def publish(self, channel: str, event: Event):
  44. """
  45. Publish event to local subscribers only.
  46. In local mode, events don't leave this process.
  47. """
  48. self._notify_local_subscribers(channel, event)