Skip to content

Commit

Permalink
update balancer-internals-class (#417)
Browse files Browse the repository at this point in the history
> [!CAUTION]
> DO NOT merge yourself, this will be auto merged upon approval
  • Loading branch information
dyc3 authored Apr 17, 2024
1 parent 993dc02 commit b91c6a6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
4 changes: 2 additions & 2 deletions balancer-design.typ
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
) <Figure::balancer-internals-class>

#pagebreak()
Shown in @Figure::balancer-internals-class is the internal structure of the Balancer. The Balancer will discover Monoiths using the `MonolithDiscoverer` (a process further described in @Chapter::ServiceDiscovery), and the `MonolithConnectionManager` will establish connections to each Monolith. `Balancer` will update `BalancerContext` according to the messages it receives from `BalancerLink`. `Balancer` will then use `BalancerContext` to route messages to the appropriate Monoliths. Clients work similarly, except that they establish connections to the Balancer via `BalancerService`. `BalancerService` handles proxying HTTP requests to the appropriate Monoliths, and also accepting and upgrading WebSocket connections.
Shown in @Figure::balancer-internals-class is the internal structure of the Balancer. The Balancer will discover Monoiths using the `MonolithDiscoverer` (a process further described in @Chapter::ServiceDiscovery), and the `MonolithConnectionManager` will establish connections to each Monolith. `Balancer` will update `BalancerContext` according to the messages it receives from `BalancerLink`. `Balancer` will then use `BalancerContext` to route messages to the appropriate Monoliths. Message routing is further described in @Section::MessageRouting. Clients work similarly, except that they establish connections to the Balancer via `BalancerService`. `BalancerService` handles proxying HTTP requests to the appropriate Monoliths, and also accepting and upgrading WebSocket connections.


#figure(
Expand Down Expand Up @@ -149,7 +149,7 @@ The Balancer, like the Monolith, exports Prometheus metrics at the `/api/status/

The Balancer also renders some text based on it's current internal state at the `/api/balancing` endpoint. This can be useful for debugging and understanding the current state of the Balancer. Currently, this endpoint renders text in a human readable format, but it could be modified to render JSON or another format instead. System visualization is specified in @Chapter::Visualization-Design.

== Message Routing
== Message Routing <Section::MessageRouting>

The Balancer routes messages such that messages sent from a client end up at the correct Monolith, and vice versa. The Balancer uses the `BalancerContext` to keep track of the state of the Monoliths and the clients, protected with an `Arc<RwLock<T>>`. To minimize the amount of locking and hashmap lookups necessary to route messages, the Balancer sets up direct channels between tasks such that incoming client messages are sent directly to the appropriate Monolith, as shown in @Figure::balancer-channels-client-monolith.

Expand Down
18 changes: 12 additions & 6 deletions figures/balancer-internals-class.mmd
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ classDiagram
dispatch_loop()
}

class MonolithDiscoverer{
class ServiceDiscoverer{
<<trait>>
}

class MonolithConnectionManager {
link: BalancerLink
}

MonolithDiscoverer "1" --> "1" MonolithConnectionManager : sends connection configs
ServiceDiscoverer "1" --> "1" MonolithConnectionManager : sends connection configs
MonolithConnectionManager "1" --> "1" BalancerLink : sends new monolith websockets via

class BalancerService {
Expand All @@ -28,7 +28,6 @@ classDiagram

class BalancerLink {
new_client_tx
client_msg_tx
new_monolith_tx
monolith_msg_tx
}
Expand All @@ -41,14 +40,21 @@ classDiagram
<<trait>>
}

MonolithSelection "1" --> "1" BalancerContext : selects monoliths
class MonolithSelectionStrategy {
<<enum>>
MinRooms
HashRing
}

MonolithSelectionStrategy "1" --o "1" BalancerContext : selects monoliths
MonolithSelection "1" <|-- "1" MonolithSelectionStrategy : implements

class BalancerContext {
clients: HashMap~ClientId-BalancerClient~,
monoliths: HashMap~MonolithId-BalancerMonolith~,
rooms_to_monoliths: HashMap~RoomName-MonolithId~,
monoliths_by_region: HashMap~String-Vec~MonolithId~~,
monolith_selection: Box~MonolithSelection~,
monolith_selection: MonolithSelectionStrategy,
}

Balancer "1" *-- "1" BalancerContext
Expand Down Expand Up @@ -90,4 +96,4 @@ classDiagram
NewClient --> BalancerClient : consumed to create

Balancer --> UnauthorizedClient : create upon websocket
Balancer --> NewMonolith : create upon websocket
Balancer --> NewMonolith : create upon websocket
Loading

0 comments on commit b91c6a6

Please sign in to comment.