|
| 1 | +import argparse |
| 2 | +import gzip |
| 3 | +import os |
| 4 | +import traceback |
| 5 | +from http import HTTPStatus |
| 6 | +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer |
| 7 | +from typing import AnyStr |
| 8 | + |
| 9 | +import cloudpickle |
| 10 | +from tensordict import TensorDict |
| 11 | + |
| 12 | +from areal.api.controller_api import DistributedBatch |
| 13 | +from areal.controller.batch import DistributedBatchMemory |
| 14 | +from areal.utils import logging |
| 15 | + |
| 16 | +logger = logging.getLogger("RPCServer") |
| 17 | + |
| 18 | + |
| 19 | +def process_input_to_distributed_batch(*args, **kwargs): |
| 20 | + for i in range(len(args)): |
| 21 | + if isinstance(args[i], DistributedBatch): |
| 22 | + args = list(args) |
| 23 | + args[i] = args[i].get_data() |
| 24 | + args = tuple(args) |
| 25 | + |
| 26 | + for k in list(kwargs.keys()): |
| 27 | + if isinstance(kwargs[k], DistributedBatch): |
| 28 | + kwargs[k] = kwargs[k].get_data() |
| 29 | + |
| 30 | + return args, kwargs |
| 31 | + |
| 32 | + |
| 33 | +def process_output_to_distributed_batch(result): |
| 34 | + if isinstance(result, dict): |
| 35 | + return DistributedBatchMemory.from_dict(result) |
| 36 | + elif isinstance(result, TensorDict): |
| 37 | + return DistributedBatchMemory.from_dict(result.to_dict()) |
| 38 | + elif isinstance(result, (list, tuple)): |
| 39 | + return DistributedBatchMemory.from_list(list(result)) |
| 40 | + else: |
| 41 | + return result |
| 42 | + |
| 43 | + |
| 44 | +class EngineRPCServer(BaseHTTPRequestHandler): |
| 45 | + engine = None |
| 46 | + |
| 47 | + def _read_body(self, timeout=120.0) -> AnyStr: |
| 48 | + old_timeout = None |
| 49 | + try: |
| 50 | + length = int(self.headers["Content-Length"]) |
| 51 | + old_timeout = self.request.gettimeout() |
| 52 | + logger.info(f"Receive rpc call, path: {self.path}, timeout: {old_timeout}") |
| 53 | + # set max read timeout = 120s here, if read hang raise exception |
| 54 | + self.request.settimeout(timeout) |
| 55 | + return self.rfile.read(length) |
| 56 | + except Exception as e: |
| 57 | + raise e |
| 58 | + finally: |
| 59 | + self.request.settimeout(old_timeout) |
| 60 | + |
| 61 | + def do_POST(self): |
| 62 | + data = None |
| 63 | + try: |
| 64 | + data = self._read_body() |
| 65 | + except Exception as e: |
| 66 | + self.send_response( |
| 67 | + HTTPStatus.REQUEST_TIMEOUT |
| 68 | + ) # 408 means read request timeout |
| 69 | + self.end_headers() |
| 70 | + self.wfile.write( |
| 71 | + f"Exception: {e}\n{traceback.format_exc()}".encode("utf-8") |
| 72 | + ) |
| 73 | + logger.error(f"Exception in do_POST: {e}\n{traceback.format_exc()}") |
| 74 | + return |
| 75 | + |
| 76 | + try: |
| 77 | + if self.path == "/create_engine": |
| 78 | + decompressed_data = gzip.decompress(data) |
| 79 | + engine_obj, init_args = cloudpickle.loads(decompressed_data) |
| 80 | + EngineRPCServer.engine = engine_obj |
| 81 | + result = EngineRPCServer.engine.initialize(init_args) |
| 82 | + logger.info(f"Engine created and initialized on RPC server: {result}") |
| 83 | + self.send_response(HTTPStatus.OK) |
| 84 | + self.end_headers() |
| 85 | + self.wfile.write(cloudpickle.dumps(result)) |
| 86 | + elif self.path == "/call": |
| 87 | + if EngineRPCServer.engine is None: |
| 88 | + self.send_response(HTTPStatus.INTERNAL_SERVER_ERROR) |
| 89 | + self.end_headers() |
| 90 | + self.wfile.write(b"Engine is none") |
| 91 | + logger.error("Call received but engine is none.") |
| 92 | + return |
| 93 | + action, args, kwargs = cloudpickle.loads(data) |
| 94 | + method = getattr(EngineRPCServer.engine, action) |
| 95 | + # NOTE: DO NOT print args here, args may be a very huge tensor |
| 96 | + logger.info(f"RPC server calling engine method: {action}") |
| 97 | + args, kwargs = process_input_to_distributed_batch(*args, **kwargs) |
| 98 | + result = method(*args, **kwargs) |
| 99 | + result = process_output_to_distributed_batch(result) |
| 100 | + self.send_response(HTTPStatus.OK) |
| 101 | + self.end_headers() |
| 102 | + self.wfile.write(cloudpickle.dumps(result)) |
| 103 | + else: |
| 104 | + self.send_response(HTTPStatus.NOT_FOUND) |
| 105 | + self.end_headers() |
| 106 | + except Exception as e: |
| 107 | + self.send_response(HTTPStatus.INTERNAL_SERVER_ERROR) |
| 108 | + self.end_headers() |
| 109 | + self.wfile.write( |
| 110 | + f"Exception: {e}\n{traceback.format_exc()}".encode("utf-8") |
| 111 | + ) |
| 112 | + logger.error(f"Exception in do_POST: {e}\n{traceback.format_exc()}") |
| 113 | + |
| 114 | + |
| 115 | +def start_rpc_server(port): |
| 116 | + server = ThreadingHTTPServer(("0.0.0.0", port), EngineRPCServer) |
| 117 | + server.serve_forever() |
| 118 | + |
| 119 | + |
| 120 | +def get_serve_port(args): |
| 121 | + port = args.port |
| 122 | + port_str = os.environ.get("PORT_LIST", "").strip() |
| 123 | + |
| 124 | + # Check if PORT_LIST is set |
| 125 | + if port_str: |
| 126 | + # Split by comma and strip whitespace |
| 127 | + ports = [p.strip() for p in port_str.split(",")] |
| 128 | + # Use the first valid port from the list |
| 129 | + if ports and ports[0]: |
| 130 | + try: |
| 131 | + return int(ports[0]) |
| 132 | + except ValueError: |
| 133 | + logger.warning( |
| 134 | + f"Invalid port '{ports[0]}' in PORT_LIST. Falling back to --port argument." |
| 135 | + ) |
| 136 | + return port |
| 137 | + |
| 138 | + |
| 139 | +if __name__ == "__main__": |
| 140 | + parser = argparse.ArgumentParser() |
| 141 | + |
| 142 | + parser.add_argument("--port", type=int, required=False) |
| 143 | + |
| 144 | + args, unknown = parser.parse_known_args() |
| 145 | + port = get_serve_port(args) |
| 146 | + |
| 147 | + logger.info(f"About to start RPC server on {port}") |
| 148 | + |
| 149 | + start_rpc_server(port) |
0 commit comments