|
| 1 | +"""Shared helpers for live AerospikeL2Plugin integration tests (LMCache L2 contract).""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import importlib |
| 6 | +import os |
| 7 | +import select |
| 8 | +from collections.abc import Iterator |
| 9 | + |
| 10 | +import pytest |
| 11 | +import torch |
| 12 | + |
| 13 | +from tests.integration.helpers import ( |
| 14 | + aerospike_hosts, |
| 15 | + ensure_native_storage_ops_for_l2_tests, |
| 16 | +) |
| 17 | + |
| 18 | +ensure_native_storage_ops_for_l2_tests() |
| 19 | + |
| 20 | +l2_mod = importlib.import_module("lmcache_aerospike.l2_plugin") |
| 21 | +if not l2_mod.L2_MP_AVAILABLE: |
| 22 | + pytest.skip( |
| 23 | + "LMCache multiprocess L2 APIs (L2StoreResult) not in this lmcache build", |
| 24 | + allow_module_level=True, |
| 25 | + ) |
| 26 | + |
| 27 | +from lmcache.v1.distributed.api import ObjectKey # noqa: E402 |
| 28 | +from lmcache.v1.distributed.l2_adapters.factory import ( # noqa: E402 |
| 29 | + create_l2_adapter_from_registry, |
| 30 | +) |
| 31 | +from lmcache.v1.distributed.l2_adapters.plugin_l2_adapter import ( # noqa: E402 |
| 32 | + PluginL2AdapterConfig, |
| 33 | +) |
| 34 | +from lmcache.v1.memory_management import ( # noqa: E402 |
| 35 | + MemoryFormat, |
| 36 | + MemoryObjMetadata, |
| 37 | + TensorMemoryObj, |
| 38 | +) |
| 39 | +from lmcache.v1.platform import consume_fd # noqa: E402 |
| 40 | +from lmcache.v1.protocol import init_remote_metadata_info # noqa: E402 |
| 41 | + |
| 42 | +init_remote_metadata_info(1) |
| 43 | + |
| 44 | +RUN_INTEGRATION = os.environ.get("RUN_INTEGRATION") == "1" |
| 45 | +L2_IT_SET = os.environ.get("AEROSPIKE_L2_TEST_SET", "kv_chunks_l2_it") |
| 46 | + |
| 47 | + |
| 48 | +def wait_for_event_fd(event_fd: int, timeout: float = 30.0) -> bool: |
| 49 | + poll = select.poll() |
| 50 | + poll.register(event_fd, select.POLLIN) |
| 51 | + if not poll.poll(timeout * 1000): |
| 52 | + return False |
| 53 | + try: |
| 54 | + consume_fd(event_fd) |
| 55 | + except BlockingIOError: |
| 56 | + pass |
| 57 | + return True |
| 58 | + |
| 59 | + |
| 60 | +def object_key(chunk_id: int, model_name: str = "lmcache_aerospike_it") -> ObjectKey: |
| 61 | + return ObjectKey( |
| 62 | + chunk_hash=ObjectKey.IntHash2Bytes(chunk_id), |
| 63 | + model_name=model_name, |
| 64 | + kv_rank=0, |
| 65 | + ) |
| 66 | + |
| 67 | + |
| 68 | +def memory_obj(size: int = 256, fill_value: float = 1.0) -> TensorMemoryObj: |
| 69 | + """TensorMemoryObj with shapes/dtypes (LMCache dev metadata for serde).""" |
| 70 | + raw = torch.empty(size, dtype=torch.float32) |
| 71 | + raw.fill_(fill_value) |
| 72 | + meta = MemoryObjMetadata( |
| 73 | + shape=torch.Size([size]), |
| 74 | + dtype=torch.float32, |
| 75 | + address=0, |
| 76 | + phy_size=size * 4, |
| 77 | + fmt=MemoryFormat.KV_2LTD, |
| 78 | + ref_count=1, |
| 79 | + shapes=[torch.Size([size])], |
| 80 | + dtypes=[torch.float32], |
| 81 | + ) |
| 82 | + return TensorMemoryObj(raw, meta, parent_allocator=None) |
| 83 | + |
| 84 | + |
| 85 | +def plugin_config() -> PluginL2AdapterConfig: |
| 86 | + host, port = aerospike_hosts()[0] |
| 87 | + return PluginL2AdapterConfig( |
| 88 | + module_path="lmcache_aerospike.l2_plugin", |
| 89 | + class_name="AerospikeL2Plugin", |
| 90 | + adapter_params={ |
| 91 | + "hosts": f"{host}:{port}", |
| 92 | + "namespace": os.environ.get("AEROSPIKE_TEST_NAMESPACE", "lmcache"), |
| 93 | + "set_name": L2_IT_SET, |
| 94 | + }, |
| 95 | + ) |
| 96 | + |
| 97 | + |
| 98 | +@pytest.fixture |
| 99 | +def aerospike_l2_adapter() -> Iterator: |
| 100 | + adapter = create_l2_adapter_from_registry(plugin_config()) |
| 101 | + try: |
| 102 | + yield adapter |
| 103 | + finally: |
| 104 | + adapter.close() |
0 commit comments