Implement a function to simulate a load balancer that routes long‑lived websocket **CONNECT** requests to one of several Jupyter target servers. You must also handle **DISCONNECT** events, per‑server **capacity limits**, and **SHUTDOWN** events that temporarily evict and re‑route existing connections.
---
## Function Signature (Java)
```java
List<String> route_request(
int numTargets,
int maxConnectionsPerTarget,
List<String> requests
)
```
- `numTargets`: number of target Jupyter servers. Targets are **1‑based**.
- `maxConnectionsPerTarget`: per‑server capacity. (Used in Part 4. If very large, assume “no limit”.)
- `requests`: sequence of input lines in order received.
---
## Request Line Schemas
1. `action,connectionId,userId,objectId` — used for `CONNECT` and `DISCONNECT`
2. `action,targetIndex` — used for `SHUTDOWN` (with 1‑based `targetIndex`)
- `action ∈ { CONNECT, DISCONNECT, SHUTDOWN }`
---
## Return Value (Log)
Return a list of strings, **one for every successful connection** (initial routes and any re‑routes after shutdowns), formatted as:
```
connectionId,userId,targetIndex
```
Rejected connections are **not** logged.
---
## Rules by Part
### Part 1 — Basic Load Balancing (tests 1–4)
- On each `CONNECT`, choose the target with the **fewest active connections**.
- Break ties by **smaller target index**.
- Routing is based only on the events seen so far (order matters).
### Part 2 — Disconnections (tests 5–8)
- On `DISCONNECT, connectionId, ...`, remove that active connection from its target.
(You may assume the `connectionId` is valid and active.)
### Part 3 — Object Affinity (tests 9–11)
- If any active connection exists for an `objectId`, **all new connections** with that same `objectId` **must connect to the same target** (the “pinned” server), even if that is not the least‑loaded option.
- If no active connection exists for the object, route normally per Part 1.
- The user ID does **not** affect routing—only the object ID creates affinity.
- When the last connection for an object ends, the affinity disappears.
### Part 4 — Target Capacity (tests 12–14)
- Each target has a max of `maxConnectionsPerTarget` active connections.
- When routing, **exclude** targets that are **at capacity**.
- If an object is pinned to a target that is **full**, the new `CONNECT` is **rejected** (no log).
- If all eligible targets are full, the `CONNECT` is **rejected**.
### Part 5 — Target Shutdown (tests 15–17)
- `SHUTDOWN,targetIndex` makes that target **temporarily unavailable** and **evicts all its active connections**.
- Evicted connections must be **re‑routed in ascending `connectionId` order** using the same rules (least load → tie by index; object affinity; capacity). The shutting‑down target is **not eligible** while down.
- **Log** each successful
if ("CONNECT".equals(action)) {
// CONNECT, connectionId, userId, objectId
Conn c = new Conn(parts[1], parts[2], parts[3], -1);
int t = choose.apply(c);
if (t != -1) attach.accept(c, t); // rejected connects are not logged
}
else if ("DISCONNECT".equals(action)) {
// DISCONNECT, connectionId, userId, objectId
Conn c = byConnId.get(parts[1]);
if (c != null) detach.accept(c); // guaranteed valid per prompt
}
else if ("SHUTDOWN".equals(action)) {
// SHUTDOWN, targetIndex
int down = Integer.parseInt(parts[1]);
// 1) Mark as unavailable so it's excluded from routing decisions.
available[down] = false;
// 2) Evict all connections from this target in connectionId order.
List<Conn> evicted = new ArrayList<>(targetConns.get(down).values());
// 3) Remove them first to clear loads / object affinities.
for (Conn c : evicted) detach.accept(c);
// 4) Re-route evicted connections using the same rules.
for (Conn c : evicted) {
int t = choose.apply(c);
if (t != -1) attach.accept(c, t); // only successful re-routes are logged
}
// 5) Target immediately becomes available again after shutdown completes.
available[down] = true;
}
}