|
| 1 | +# Copyright The Marin Authors |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +"""Actor proxy for forwarding ActorService RPCs to actors within the cluster. |
| 5 | +
|
| 6 | +External clients send actor calls to the controller; the proxy resolves the |
| 7 | +target endpoint from the controller's DB and forwards the raw request to the |
| 8 | +actor server on the worker VM. All ActorService methods are proxied |
| 9 | +transparently (raw byte forwarding, no deserialization). |
| 10 | +
|
| 11 | +Route pattern:: |
| 12 | +
|
| 13 | + POST /iris.actor.ActorService/{method} |
| 14 | + X-Iris-Actor-Endpoint: namespace/actor-name |
| 15 | +""" |
| 16 | + |
| 17 | +import logging |
| 18 | + |
| 19 | +import httpx |
| 20 | +from starlette.requests import Request |
| 21 | +from starlette.responses import JSONResponse, Response |
| 22 | + |
| 23 | +from iris.cluster.controller.db import ControllerDB, EndpointQuery, endpoint_query_predicate |
| 24 | + |
| 25 | +logger = logging.getLogger(__name__) |
| 26 | + |
| 27 | +# Header used by ProxyResolver to tell the proxy which endpoint to forward to. |
| 28 | +# Duplicated from iris.actor.resolver.ACTOR_ENDPOINT_HEADER to avoid a |
| 29 | +# cluster → actor import dependency. |
| 30 | +ACTOR_ENDPOINT_HEADER = "x-iris-actor-endpoint" |
| 31 | + |
| 32 | +PROXY_ROUTE = "/iris.actor.ActorService/{method}" |
| 33 | +PROXY_TIMEOUT_SECONDS = 60.0 |
| 34 | + |
| 35 | +# Headers that should not be forwarded to upstream (hop-by-hop or routing-specific). |
| 36 | +_HOP_BY_HOP_HEADERS = frozenset( |
| 37 | + { |
| 38 | + "host", |
| 39 | + "transfer-encoding", |
| 40 | + "connection", |
| 41 | + "keep-alive", |
| 42 | + "upgrade", |
| 43 | + ACTOR_ENDPOINT_HEADER, |
| 44 | + } |
| 45 | +) |
| 46 | + |
| 47 | + |
| 48 | +class ActorProxy: |
| 49 | + """Forwards ActorService RPCs to actors resolved from the endpoint registry.""" |
| 50 | + |
| 51 | + def __init__(self, db: ControllerDB): |
| 52 | + self._db = db |
| 53 | + self._client = httpx.AsyncClient(timeout=PROXY_TIMEOUT_SECONDS) |
| 54 | + |
| 55 | + async def close(self) -> None: |
| 56 | + await self._client.aclose() |
| 57 | + |
| 58 | + async def handle(self, request: Request) -> Response: |
| 59 | + """Proxy an ActorService RPC to the resolved actor endpoint.""" |
| 60 | + method = request.path_params["method"] |
| 61 | + endpoint_name = request.headers.get(ACTOR_ENDPOINT_HEADER) |
| 62 | + if not endpoint_name: |
| 63 | + return JSONResponse( |
| 64 | + {"error": f"Missing {ACTOR_ENDPOINT_HEADER} header"}, |
| 65 | + status_code=400, |
| 66 | + ) |
| 67 | + |
| 68 | + address = self._resolve_endpoint(endpoint_name) |
| 69 | + if address is None: |
| 70 | + return JSONResponse( |
| 71 | + {"error": f"No endpoint found for '{endpoint_name}'"}, |
| 72 | + status_code=404, |
| 73 | + ) |
| 74 | + |
| 75 | + upstream_url = f"http://{address}/iris.actor.ActorService/{method}" |
| 76 | + body = await request.body() |
| 77 | + forward_headers = {k: v for k, v in request.headers.items() if k.lower() not in _HOP_BY_HOP_HEADERS} |
| 78 | + |
| 79 | + try: |
| 80 | + upstream_resp = await self._client.post( |
| 81 | + upstream_url, |
| 82 | + content=body, |
| 83 | + headers=forward_headers, |
| 84 | + ) |
| 85 | + except httpx.HTTPError as exc: |
| 86 | + logger.warning("Proxy upstream error for %s: %s", endpoint_name, exc) |
| 87 | + return JSONResponse( |
| 88 | + {"error": f"Upstream error: {exc}"}, |
| 89 | + status_code=502, |
| 90 | + ) |
| 91 | + |
| 92 | + return Response( |
| 93 | + content=upstream_resp.content, |
| 94 | + status_code=upstream_resp.status_code, |
| 95 | + media_type=upstream_resp.headers.get("content-type"), |
| 96 | + ) |
| 97 | + |
| 98 | + def _resolve_endpoint(self, name: str) -> str | None: |
| 99 | + """Resolve an endpoint name to an address via the controller DB.""" |
| 100 | + query = EndpointQuery(exact_name=name) |
| 101 | + joins, where = endpoint_query_predicate(query) |
| 102 | + from iris.cluster.controller.service import ENDPOINTS |
| 103 | + |
| 104 | + with self._db.read_snapshot() as q: |
| 105 | + endpoints = q.select(ENDPOINTS, where=where, joins=joins) |
| 106 | + if not endpoints: |
| 107 | + return None |
| 108 | + return endpoints[0].address |
0 commit comments