Aggie Student Toolkit — Load Balancer CSCE 412 Project 4
Arnav Fotedar · Fall 2026 · C++ load balancer simulation
Loading...
Searching...
No Matches
LoadBalancer Class Reference

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

Detailed Description

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:

  1. Complete work - every busy server advances one cycle; finished requests are counted and retiring servers that became idle are removed.
  2. Admit arrivals - each new request passes through the Firewall; allowed requests join the back of the queue.
  3. Adjust capacity - every ScalingPolicy::cooldownCycles cycles the queue size is compared with the number of active servers and one server may be added or retired.
  4. Dispatch - the oldest queued requests are assigned to idle, non-retiring servers in server-id 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.

Constructor & Destructor Documentation

◆ LoadBalancer()

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.

Parameters
nameShort name used in log lines, for example "LB" or "LB-P".
initialServersNumber of servers created at time 0 (at least 1).
policyScaling thresholds, cooldown and server limits.
firewallBlocked IP ranges applied in admit().
maxQueueLargest allowed queue size; 0 means unlimited.
loggerDestination for event and status messages.

Definition at line 15 of file LoadBalancer.cpp.

Member Function Documentation

◆ admit()

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.

Parameters
requestRequest to admit.
cycleCurrent clock cycle, for logging.
Returns
True if the request was queued.

Definition at line 36 of file LoadBalancer.cpp.

◆ start()

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.

◆ processCycle()

void LoadBalancer::processCycle ( unsigned long cycle,
const std::vector< Request > & arrivals )

Runs one clock cycle: complete work, admit arrivals, adjust capacity, dispatch.

Parameters
cycleClock cycle being simulated (1, 2, 3, ...).
arrivalsRequests generated during this cycle.

Definition at line 70 of file LoadBalancer.cpp.

◆ recordSnapshot()

void LoadBalancer::recordSnapshot ( unsigned long cycle)

Saves a Snapshot of the current state for the periodic table in the log.

Parameters
cycleCurrent clock cycle.

Definition at line 218 of file LoadBalancer.cpp.

◆ statusLine()

std::string LoadBalancer::statusLine ( unsigned long cycle) const

Builds a one-line status: queue, servers (busy/idle/retiring), completed, rejected.

Parameters
cycleCurrent clock cycle.
Returns
Status text without color codes.

Definition at line 231 of file LoadBalancer.cpp.

◆ summarize()

void LoadBalancer::summarize ( std::ostream & out) const

Writes the end-of-run summary for this load balancer.

Parameters
outStream to write to.

Definition at line 240 of file LoadBalancer.cpp.

◆ name()

const std::string & LoadBalancer::name ( ) const
Returns
Name used in logs.

Definition at line 297 of file LoadBalancer.cpp.

◆ queueSize()

std::size_t LoadBalancer::queueSize ( ) const
Returns
Requests waiting in the queue.

Definition at line 299 of file LoadBalancer.cpp.

◆ liveServers()

std::size_t LoadBalancer::liveServers ( ) const
Returns
All servers, including retiring ones.

Definition at line 301 of file LoadBalancer.cpp.

◆ activeServers()

std::size_t LoadBalancer::activeServers ( ) const
Returns
Servers that accept new work (not retiring).

Definition at line 303 of file LoadBalancer.cpp.

◆ busyServers()

std::size_t LoadBalancer::busyServers ( ) const
Returns
Servers currently processing a request.

Definition at line 308 of file LoadBalancer.cpp.

◆ idleServers()

std::size_t LoadBalancer::idleServers ( ) const
Returns
Servers with no request in progress.

Definition at line 313 of file LoadBalancer.cpp.

◆ retiringServers()

std::size_t LoadBalancer::retiringServers ( ) const
Returns
Servers marked for removal after their current request.

Definition at line 315 of file LoadBalancer.cpp.

◆ inService()

std::size_t LoadBalancer::inService ( ) const
Returns
Requests currently being processed.

Definition at line 320 of file LoadBalancer.cpp.

◆ stats()

const LoadBalancerStats & LoadBalancer::stats ( ) const
Returns
Run counters.

Definition at line 322 of file LoadBalancer.cpp.

◆ scaleEvents()

const std::vector< ScaleEvent > & LoadBalancer::scaleEvents ( ) const
Returns
Every change made to the server pool.

Definition at line 324 of file LoadBalancer.cpp.

◆ snapshots()

const std::vector< Snapshot > & LoadBalancer::snapshots ( ) const
Returns
Snapshots recorded with recordSnapshot().

Definition at line 326 of file LoadBalancer.cpp.

◆ servers()

const std::vector< WebServer > & LoadBalancer::servers ( ) const
Returns
The live servers in id order.

Definition at line 328 of file LoadBalancer.cpp.

◆ policy()

const ScalingPolicy & LoadBalancer::policy ( ) const
Returns
The scaling policy in use.

Definition at line 330 of file LoadBalancer.cpp.

◆ firewall()

const Firewall & LoadBalancer::firewall ( ) const
Returns
The firewall in use.

Definition at line 332 of file LoadBalancer.cpp.


The documentation for this class was generated from the following files: