()
| 70 | |
| 71 | # Keep session IDs and metadata endpoints stable on single-node runs. |
| 72 | # localhost may resolve to IPv6 on some hosts while peers use IPv4. |
| 73 | force_ipv4 = os.getenv("MOONCAKE_FORCE_IPV4_LOOPBACK", "1") not in ("0", "false", "False") |
| 74 | env_host = os.getenv("MOONCAKE_LOCAL_HOSTNAME", "").strip() |
| 75 | if env_host: |
| 76 | cfg.local_hostname = env_host |
| 77 | elif force_ipv4 and cfg.local_hostname in ("localhost", "::1", "127.0.0.1"): |
| 78 | detected = _detect_non_loopback_ipv4() |
| 79 | if detected is not None: |
| 80 | cfg.local_hostname = detected |
| 81 | else: |
| 82 | cfg.local_hostname = "127.0.0.1" |
| 83 | return cfg |
| 84 | |
| 85 | |
| 86 | class MooncakeTransferEngine: |
| 87 | def __init__(self): |
| 88 | self.engine = None |
| 89 | try: |
| 90 | from mooncake.engine import TransferEngine |
| 91 | |
| 92 | self.engine = TransferEngine() |
| 93 | except ImportError: |
| 94 | logger.warning( |
| 95 | "Please install mooncake by following the instructions at https://github.com/kvcache-ai/Mooncake/blob/main/docs/source/getting_started/build.md to run with MooncakeTransferEngine." |
| 96 | ) |
| 97 | raise |
| 98 | |
| 99 | try: |
| 100 | self.config = MooncakeTransferEngineConfig.load_from_env() |
| 101 | logger.info("Mooncake Configuration loaded successfully.") |
| 102 | except Exception as e: |
| 103 | logger.error(f"Failed to load Mooncake config: {e}") |
| 104 | raise |
| 105 | |
| 106 | # session_suffix = "_" + str(uuid.uuid4()) |
| 107 | self.initialize( |
| 108 | self.config.local_hostname, |
| 109 | self.config.metadata_server, |
| 110 | self.config.protocol, |
| 111 | self.config.device_name, |
| 112 | ) |
| 113 | # session_suffix = ":" + self.engine.get_rpc_port() |
| 114 | # self.session_id = self.config.local_hostname + session_suffix |
| 115 | self.session_id = f"{self.config.local_hostname}:{self.engine.get_rpc_port()}" |
| 116 | |
| 117 | def register(self, ptr, length): |
| 118 | if self.engine: |
| 119 | ret = self.engine.register_memory(ptr, length) |
| 120 | if ret != 0: |
| 121 | raise RuntimeError("Mooncake memory registration failed.") |
| 122 | |
| 123 | def deregister(self, ptr): |
| 124 | if self.engine: |
| 125 | ret = self.engine.unregister_memory(ptr) |
| 126 | if ret != 0: |
| 127 | raise RuntimeError("Mooncake memory deregistration failed.") |
| 128 | |
| 129 | def initialize( |
no test coverage detected