diff --git a/contracts/native-topology-source.json b/contracts/native-topology-source.json index ccbbbe4..ddb0607 100644 --- a/contracts/native-topology-source.json +++ b/contracts/native-topology-source.json @@ -1,6 +1,6 @@ { "repository": "AI4SAI/sai-portal", "path": "sai_native_topology.py", - "revision": "15a5bcc7c2e38e0d1a7c0c36a068a605b915f104", - "sha256": "af9781be2dda2cfdf62ed77581b22f3ea2b71f9b65efbe62c86bb330593d2a1b" + "revision": "7cee24d9c642b240e367491b10fe8ad7a952a8cd", + "sha256": "6e033ddf70ec67d06e46b90e9c6a7ce2b5cd3f83c8ff3c5c3b3807b874a27f28" } diff --git a/contracts/node-interconnect/v1/README.md b/contracts/node-interconnect/v1/README.md index a2ccbc1..f1429c3 100644 --- a/contracts/node-interconnect/v1/README.md +++ b/contracts/node-interconnect/v1/README.md @@ -11,8 +11,12 @@ publisher to withdraw or replace the affected record. Reuse of a node name or unchanged device counts is insufficient evidence that its hardware is unchanged. An explicit `--expires-at` still emits v1 and its deadline remains binding. -The join envelope accepts both record versions. Upgrade stored-schema consumers -and their embedded validators before the publisher switches records to v2. +The join envelope accepts both record versions. For a remote allocation with +`include_interconnect=true`, clients that can read v2 send +`X-SAI-Interconnect-Schema: node.interconnect/v2`. Without the header the +gateway returns readable v1 records and marks nodes with v2 records unavailable. +In both cases the live allocation is preserved. Upgrade the gateway and any +consumers that read stored records directly before publishing v2 records. `node.interconnect/v1` is a bounded, read-only projection of one compute node's hardware relationships. It is a view over native observations, not a scheduler, diff --git a/src/sai_cli/client.py b/src/sai_cli/client.py index a732007..35c689d 100644 --- a/src/sai_cli/client.py +++ b/src/sai_cli/client.py @@ -255,24 +255,25 @@ def _site_jobs(self, query: JobQuery, *, job_id: str | None = None) -> SiteJobRe ) return result - def _site_get(self, path: str, *, body: dict[str, object]) -> JsonResponse: + def _site_get(self, path: str, *, body: dict[str, object], interconnect: bool = False) -> JsonResponse: if self.site_transport is None or self.capability_provider is None: raise ConfigError("native reads require a configured site_read_base_url and capability mode") lease, capability = self._acquire_capability(body, lease=self.token_provider.get()) - # The gateway receives only the capability. All filters are signed in it. + headers = {CAPABILITY_HEADER: capability.token} + if interconnect: + from sai_native_topology import INTERCONNECT_SCHEMA_HEADER, REVIEWABLE_SCHEMA + + path += "?include_interconnect=true" + headers[INTERCONNECT_SCHEMA_HEADER] = REVIEWABLE_SCHEMA try: - response = self.site_transport.request( - "GET", path, headers={CAPABILITY_HEADER: capability.token}, - ) + return self.site_transport.request("GET", path, headers=headers) except AuthenticationError: if capability.source != "cache": raise self.capability_provider.invalidate(capability) _, capability = self._acquire_capability(body, lease=lease, force_refresh=True) - response = self.site_transport.request( - "GET", path, headers={CAPABILITY_HEADER: capability.token}, - ) - return response + headers[CAPABILITY_HEADER] = capability.token + return self.site_transport.request("GET", path, headers=headers) def job_allocation(self, job_id: str, query: JobQuery, *, interconnect: bool = False) -> SiteJobAllocationResult: identifier = clean_token(job_id, field="job ID", required=True, maximum=64) or "" @@ -280,8 +281,9 @@ def job_allocation(self, job_id: str, query: JobQuery, *, interconnect: bool = F raise ConfigError("allocation requires a numeric job ID or numeric job_task ID") request = query.validated(detail=True) response = self._site_get( - "/v1/site-read/jobs/" + quote(identifier, safe="") + "/allocation" + ("?include_interconnect=true" if interconnect else ""), + "/v1/site-read/jobs/" + quote(identifier, safe="") + "/allocation", body=request.capability_request(operation="jobs.allocation", job_id=identifier), + interconnect=interconnect, ) result = SiteJobAllocationResult.from_payload(response.payload, request_id=response.request_id) if interconnect and result.interconnect is None: diff --git a/src/sai_native_topology.py b/src/sai_native_topology.py index 6e3f5e4..976fca2 100644 --- a/src/sai_native_topology.py +++ b/src/sai_native_topology.py @@ -14,6 +14,7 @@ SCHEMA = "node.interconnect/v1" REVIEWABLE_SCHEMA = "node.interconnect/v2" +INTERCONNECT_SCHEMA_HEADER = "X-SAI-Interconnect-Schema" DEFAULT_ROOT = "/opt/sai/runtime/topology" MAX_BYTES = 512 * 1024 MAX_CPU_IDS = 65536 @@ -194,7 +195,7 @@ def job_cpu_locality(record, cpu_ids): return {row["id"]: sorted(machine.intersection(row["linux_cpu_ids"])) for row in record["numa"]} -def read_interconnect(name, *, cluster, dimensions, gpu_total, root=DEFAULT_ROOT, now=None, trusted_uid=0): +def read_interconnect(name, *, cluster, dimensions, gpu_total, root=DEFAULT_ROOT, now=None, trusted_uid=0, reviewable=True): """Only root-published native mapping assets, without business config/auth.""" if not isinstance(name, str) or not _NAME.fullmatch(name): raise ValueError("invalid topology node") @@ -223,6 +224,8 @@ def read_interconnect(name, *, cluster, dimensions, gpu_total, root=DEFAULT_ROOT current = now or datetime.now(timezone.utc) if value["node"] != name or value["cluster"] != cluster: raise ValueError("topology identity differs") + if not reviewable and value["schema_version"] == REVIEWABLE_SCHEMA: + return {"status": "unavailable", "reason": "record_invalid"} reason = _observation_time_failure(value, current) if reason: return {"status": "unavailable", "reason": reason} @@ -246,7 +249,7 @@ def same_node_generation(expected, observed): return starts[0] == starts[1] -def join_interconnect(allocation, *, cluster, read_node_state, root=DEFAULT_ROOT, clock=None, trusted_uid=0): +def join_interconnect(allocation, *, cluster, read_node_state, root=DEFAULT_ROOT, clock=None, trusted_uid=0, reviewable=True): """Same bounded optional layer for native callers and authorized gateways.""" nodes = allocation["nodes"] or [] clock = clock or (lambda: datetime.now(timezone.utc)) @@ -259,7 +262,7 @@ def join_interconnect(allocation, *, cluster, read_node_state, root=DEFAULT_ROOT else: try: record = read_interconnect(node["name"], cluster=cluster, dimensions=node["cpu_topology"], - gpu_total=node["gpu_total"], root=root, now=clock(), trusted_uid=trusted_uid) + gpu_total=node["gpu_total"], root=root, now=clock(), trusted_uid=trusted_uid, reviewable=reviewable) except (OSError, ValueError, TypeError, KeyError): record = {"status": "unavailable", "reason": "record_invalid"} size += len(json.dumps(record)) diff --git a/tests/test_site_transport.py b/tests/test_site_transport.py index ec214fd..207441f 100644 --- a/tests/test_site_transport.py +++ b/tests/test_site_transport.py @@ -27,6 +27,8 @@ def handle_call(self): "capability": self.headers.get("X-SAI-Capability")} if self.headers.get("Idempotency-Key") is not None: call["idempotency_key"] = self.headers["Idempotency-Key"] + if "X-SAI-Interconnect-Schema" in self.headers: + call["interconnect_schema"] = self.headers["X-SAI-Interconnect-Schema"] calls.append(call) status, payload = respond(call, len(calls)) encoded = json.dumps(payload).encode() @@ -202,6 +204,7 @@ def test_allocation_uses_exact_operation_and_gateway_path(tmp_path, site_allocat "partition": None, "states": [], "job_id": "12345_7", "limit": None, } assert all(c["path"] == "/v1/site-read/jobs/12345_7/allocation" and c["bearer"] is None for c in reads) + assert all("interconnect_schema" not in c for c in reads) def test_optional_interconnect_uses_same_exact_capability_and_fails_if_absent(tmp_path, site_allocation_payload): @@ -216,7 +219,12 @@ def test_optional_interconnect_uses_same_exact_capability_and_fails_if_absent(tm site_allocation_payload["interconnect"] = {"schema_version": "node-interconnect-join/v1", "nodes": [ unknown, {"node": nodes[1]["name"], "status": "recorded", "value": hardware}, ]} - with http_endpoint(issuer) as (portal,issued), http_endpoint(lambda *_:(200,site_allocation_payload)) as (gateway,reads): + def gateway_reply(call, count): + if count == 2: + return 401, {} + return 200, site_allocation_payload + + with http_endpoint(issuer) as (portal,issued), http_endpoint(gateway_reply) as (gateway,reads): config=profile(tmp_path,portal,gateway) code,text,error=invoke(config,tmp_path,['jobs','allocation','12345','--with-interconnect']) assert code==0,error @@ -228,6 +236,8 @@ def test_optional_interconnect_uses_same_exact_capability_and_fails_if_absent(tm assert issued[0]['body']['operation']=='jobs.allocation' and reads[0]['bearer'] is None del site_allocation_payload['interconnect'] assert invoke(config,tmp_path,['jobs','allocation','12345','--with-interconnect'])[0]==7 + assert [call.get("interconnect_schema") for call in reads] == ["node.interconnect/v2"] * 3 + assert all("interconnect_schema" not in call for call in issued) @pytest.mark.parametrize("capability", [False, True])