| 52 | @staticmethod |
| 53 | def load_from_env() -> "MooncakeTransferEngineConfig": |
| 54 | config_file_path = os.getenv("MOONCAKE_CONFIG_PATH") |
| 55 | if not config_file_path: |
| 56 | raise ValueError("The environment variable 'MOONCAKE_CONFIG_PATH' is not set.") |
| 57 | cfg = MooncakeTransferEngineConfig.from_file(config_file_path) |
| 58 | |
| 59 | env_metadata_server = os.getenv("MOONCAKE_METADATA_SERVER", "").strip() |
| 60 | if env_metadata_server: |
| 61 | cfg.metadata_server = env_metadata_server |
| 62 | |
| 63 | env_protocol = os.getenv("MOONCAKE_PROTOCOL", "").strip() |
| 64 | if env_protocol: |
| 65 | cfg.protocol = env_protocol |
| 66 | |
| 67 | env_device_name = os.getenv("MOONCAKE_DEVICE_NAME", "").strip() |
| 68 | if env_device_name: |
| 69 | cfg.device_name = env_device_name |
| 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, |