File size: 7,118 Bytes
9fb722e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38cba90
9fb722e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
from __future__ import annotations

import time
import uuid
from dataclasses import dataclass
from typing import Any

from hearthnet.bus.capability import (
    CapabilityDescriptor,
    CapabilityEntry,
    Handler,
    ParamsPredicate,
    RouteRequest,
)
from hearthnet.bus.health import HealthTracker
from hearthnet.bus.registry import Diff, RegistryEvent, Registry
from hearthnet.bus.router import BusConfig, Router
from hearthnet.types import CapabilityName, HearthNetError, Version


class BusError(HearthNetError):
    pass


class InMemoryTransport:
    def __init__(self) -> None:
        self._buses: dict[str, CapabilityBus] = {}

    def register(self, bus: CapabilityBus) -> None:
        self._buses[bus.node_id_full] = bus

    async def call(self, node_id: str, req: RouteRequest) -> dict[str, Any]:
        try:
            bus = self._buses[node_id]
        except KeyError as exc:
            raise BusError("partition", f"node {node_id} is not reachable") from exc
        inbound = RouteRequest(
            capability=req.capability,
            version_req=req.version_req,
            body=req.body,
            caller=req.caller,
            trace_id=req.trace_id,
            session_id=req.session_id,
            deadline_ms=req.deadline_ms,
            stream=req.stream,
        )
        return await bus.handle_call(inbound, local_only=True)


@dataclass(frozen=True)
class CallTraceEvent:
    trace_id: str
    capability: CapabilityName
    from_node: str
    to_node: str
    result: str
    ms: float


@dataclass(frozen=True)
class TopologySnapshot:
    our_node_id: str
    peers: list[dict[str, Any]]
    capabilities_local: list[dict[str, Any]]
    capabilities_remote: list[dict[str, Any]]
    in_flight_total: int
    traces: list[CallTraceEvent]


class CapabilityBus:
    def __init__(
        self,
        node_id_full: str,
        community_id: str,
        transport: InMemoryTransport | None = None,
        config: BusConfig | None = None,
    ) -> None:
        self.node_id_full = node_id_full
        self.community_id = community_id
        self.registry = Registry(our_node_id=node_id_full)
        self.health = HealthTracker()
        self.router = Router(self.registry, config)
        self.transport = transport or InMemoryTransport()
        self.transport.register(self)
        self._traces: list[CallTraceEvent] = []
        self._offline_stash: list[tuple[CapabilityDescriptor, Handler, ParamsPredicate | None]] = []

    def register_capability(
        self,
        descriptor: CapabilityDescriptor,
        handler: Handler,
        params_compatible: ParamsPredicate | None = None,
    ) -> None:
        self.registry.register_local(descriptor, handler, params_compatible)

    def register_service(self, service: Any) -> None:
        for item in service.capabilities():
            descriptor, handler, *rest = item
            predicate = rest[0] if rest else None
            self.register_capability(descriptor, handler, predicate)

    async def call(
        self,
        capability: CapabilityName,
        version_req: Version,
        body: dict[str, Any],
        *,
        session_id: str | None = None,
    ) -> dict[str, Any]:
        req = RouteRequest(
            capability=capability,
            version_req=version_req,
            body=body,
            caller=self.node_id_full,
            trace_id=uuid.uuid4().hex,
            session_id=session_id,
            deadline_ms=int((time.monotonic() + 10) * 1000),
        )
        return await self.handle_call(req)

    async def handle_call(self, req: RouteRequest, *, local_only: bool = False) -> dict[str, Any]:
        entry = self.router.route_sticky(req) if req.session_id else self.router.route(req)
        if entry is None:
            raise BusError("not_found", f"no provider for {req.capability}@{req.version_req}")
        started = time.monotonic()
        entry.in_flight += 1
        try:
            if entry.is_local:
                if entry.handler is None:
                    raise BusError("not_implemented", entry.descriptor.name)
                result = await entry.handler(req)
            elif local_only:
                raise BusError("not_found", f"remote entry cannot satisfy inbound {req.capability}")
            else:
                result = await self.transport.call(entry.node_id, req)
            elapsed = (time.monotonic() - started) * 1000
            self.health.record(entry, success=True, latency_ms=elapsed)
            self._traces.append(
                CallTraceEvent(
                    req.trace_id, req.capability, req.caller, entry.node_id, "ok", elapsed
                )
            )
            return result
        except HearthNetError as exc:
            elapsed = (time.monotonic() - started) * 1000
            self.health.record(entry, success=False, latency_ms=elapsed)
            self._traces.append(
                CallTraceEvent(
                    req.trace_id, req.capability, req.caller, entry.node_id, exc.code, elapsed
                )
            )
            raise
        finally:
            entry.in_flight -= 1

    def deregister_internet_capabilities(self) -> int:
        removed = 0
        for entry in list(self.registry.all_local()):
            if entry.descriptor.params.get("requires_internet"):
                removed_entry = self.registry.deregister_local(
                    entry.descriptor.name, entry.descriptor.version
                )
                if removed_entry and removed_entry.handler:
                    self._offline_stash.append(
                        (
                            removed_entry.descriptor,
                            removed_entry.handler,
                            removed_entry.params_compatible,
                        )
                    )
                    removed += 1
        return removed

    def restore_internet_capabilities(self) -> int:
        restored = 0
        while self._offline_stash:
            descriptor, handler, predicate = self._offline_stash.pop(0)
            self.register_capability(descriptor, handler, predicate)
            restored += 1
        return restored

    def topology_snapshot(self, peers: list[dict[str, Any]] | None = None) -> TopologySnapshot:
        return TopologySnapshot(
            our_node_id=self.node_id_full,
            peers=peers or [],
            capabilities_local=[_entry_view(entry) for entry in self.registry.all_local()],
            capabilities_remote=[_entry_view(entry) for entry in self.registry.all_remote()],
            in_flight_total=sum(entry.in_flight for entry in self.registry.all()),
            traces=list(self._traces[-50:]),
        )


def _entry_view(entry: CapabilityEntry) -> dict[str, Any]:
    return {
        "node_id": entry.node_id,
        "name": entry.descriptor.name,
        "version": entry.descriptor.version_str,
        "local": entry.is_local,
        "params": dict(entry.descriptor.params),
        "success_rate": entry.success_rate,
        "quarantined": entry.quarantined_until > time.monotonic(),
    }