|
Aggie Student Toolkit — Load Balancer CSCE 412 Project 4
Arnav Fotedar · Fall 2026 · C++ load balancer simulation
|
Distributes queued requests across a dynamically sized pool of WebServer objects. More...
#include <LoadBalancer.h>
Public Member Functions | |
| LoadBalancer (std::string name, std::size_t initialServers, const ScalingPolicy &policy, const Firewall &firewall, std::size_t maxQueue, Logger &logger) | |
| Creates a load balancer with an initial server pool. | |
| bool | admit (const Request &request, unsigned long cycle) |
| Offers a request to the load balancer. | |
| void | start () |
| Records the starting queue size and performs the first dispatch at time 0. | |
| void | processCycle (unsigned long cycle, const std::vector< Request > &arrivals) |
| Runs one clock cycle: complete work, admit arrivals, adjust capacity, dispatch. | |
| void | recordSnapshot (unsigned long cycle) |
| Saves a Snapshot of the current state for the periodic table in the log. | |
| std::string | statusLine (unsigned long cycle) const |
| Builds a one-line status: queue, servers (busy/idle/retiring), completed, rejected. | |
| void | summarize (std::ostream &out) const |
| Writes the end-of-run summary for this load balancer. | |
| const std::string & | name () const |
| std::size_t | queueSize () const |
| std::size_t | liveServers () const |
| std::size_t | activeServers () const |
| std::size_t | busyServers () const |
| std::size_t | idleServers () const |
| std::size_t | retiringServers () const |
| std::size_t | inService () const |
| const LoadBalancerStats & | stats () const |
| const std::vector< ScaleEvent > & | scaleEvents () const |
| const std::vector< Snapshot > & | snapshots () const |
| const std::vector< WebServer > & | servers () const |
| const ScalingPolicy & | policy () const |
| const Firewall & | firewall () const |
Distributes queued requests across a dynamically sized pool of WebServer objects.
The load balancer owns a FIFO queue of pending Request values and a vector of WebServer objects. For each clock cycle, processCycle() performs these steps in order:
Scale-down prefers removing an idle server. If every server is busy, one is marked retiring so that it finishes its request before removal; no request is ever lost when capacity shrinks.
Definition at line 116 of file LoadBalancer.h.
| LoadBalancer::LoadBalancer | ( | std::string | name, |
| std::size_t | initialServers, | ||
| const ScalingPolicy & | policy, | ||
| const Firewall & | firewall, | ||
| std::size_t | maxQueue, | ||
| Logger & | logger ) |
Creates a load balancer with an initial server pool.
| name | Short name used in log lines, for example "LB" or "LB-P". |
| initialServers | Number of servers created at time 0 (at least 1). |
| policy | Scaling thresholds, cooldown and server limits. |
| firewall | Blocked IP ranges applied in admit(). |
| maxQueue | Largest allowed queue size; 0 means unlimited. |
| logger | Destination for event and status messages. |
Definition at line 15 of file LoadBalancer.cpp.
| bool LoadBalancer::admit | ( | const Request & | request, |
| unsigned long | cycle ) |
Offers a request to the load balancer.
The request is rejected if its source IP is blocked, discarded if the queue is full, and otherwise appended to the queue.
| request | Request to admit. |
| cycle | Current clock cycle, for logging. |
Definition at line 36 of file LoadBalancer.cpp.
| void LoadBalancer::start | ( | ) |
Records the starting queue size and performs the first dispatch at time 0.
Call once after the initial queue has been admitted and before the first processCycle().
Definition at line 61 of file LoadBalancer.cpp.
| void LoadBalancer::processCycle | ( | unsigned long | cycle, |
| const std::vector< Request > & | arrivals ) |
Runs one clock cycle: complete work, admit arrivals, adjust capacity, dispatch.
| cycle | Clock cycle being simulated (1, 2, 3, ...). |
| arrivals | Requests generated during this cycle. |
Definition at line 70 of file LoadBalancer.cpp.
| void LoadBalancer::recordSnapshot | ( | unsigned long | cycle | ) |
Saves a Snapshot of the current state for the periodic table in the log.
| cycle | Current clock cycle. |
Definition at line 218 of file LoadBalancer.cpp.
| std::string LoadBalancer::statusLine | ( | unsigned long | cycle | ) | const |
Builds a one-line status: queue, servers (busy/idle/retiring), completed, rejected.
| cycle | Current clock cycle. |
Definition at line 231 of file LoadBalancer.cpp.
| void LoadBalancer::summarize | ( | std::ostream & | out | ) | const |
Writes the end-of-run summary for this load balancer.
| out | Stream to write to. |
Definition at line 240 of file LoadBalancer.cpp.
| const std::string & LoadBalancer::name | ( | ) | const |
Definition at line 297 of file LoadBalancer.cpp.
| std::size_t LoadBalancer::queueSize | ( | ) | const |
Definition at line 299 of file LoadBalancer.cpp.
| std::size_t LoadBalancer::liveServers | ( | ) | const |
Definition at line 301 of file LoadBalancer.cpp.
| std::size_t LoadBalancer::activeServers | ( | ) | const |
Definition at line 303 of file LoadBalancer.cpp.
| std::size_t LoadBalancer::busyServers | ( | ) | const |
Definition at line 308 of file LoadBalancer.cpp.
| std::size_t LoadBalancer::idleServers | ( | ) | const |
Definition at line 313 of file LoadBalancer.cpp.
| std::size_t LoadBalancer::retiringServers | ( | ) | const |
Definition at line 315 of file LoadBalancer.cpp.
| std::size_t LoadBalancer::inService | ( | ) | const |
Definition at line 320 of file LoadBalancer.cpp.
| const LoadBalancerStats & LoadBalancer::stats | ( | ) | const |
Definition at line 322 of file LoadBalancer.cpp.
| const std::vector< ScaleEvent > & LoadBalancer::scaleEvents | ( | ) | const |
Definition at line 324 of file LoadBalancer.cpp.
| const std::vector< Snapshot > & LoadBalancer::snapshots | ( | ) | const |
Definition at line 326 of file LoadBalancer.cpp.
| const std::vector< WebServer > & LoadBalancer::servers | ( | ) | const |
Definition at line 328 of file LoadBalancer.cpp.
| const ScalingPolicy & LoadBalancer::policy | ( | ) | const |
Definition at line 330 of file LoadBalancer.cpp.
| const Firewall & LoadBalancer::firewall | ( | ) | const |
Definition at line 332 of file LoadBalancer.cpp.