Aggie Student Toolkit — Load Balancer CSCE 412 Project 4
Arnav Fotedar · Fall 2026 · C++ load balancer simulation
Loading...
Searching...
No Matches
LoadBalancer.h
Go to the documentation of this file.
1
6
7#ifndef LOADBALANCER_H
8#define LOADBALANCER_H
9
10#include <cstddef>
11#include <iosfwd>
12#include <queue>
13#include <string>
14#include <vector>
15
16#include "Firewall.h"
17#include "Logger.h"
18#include "Request.h"
19#include "WebServer.h"
20
30 double upperFactor = 80.0;
31 double lowerFactor = 50.0;
32 unsigned int cooldownCycles = 10;
33 unsigned int minServers = 1;
34 unsigned int maxServers = 100;
35};
36
40struct ScaleEvent {
42 enum class Kind {
43 Added,
44 Reactivated,
45 Removed,
46 Retiring,
47 Retired
48 };
49 unsigned long cycle = 0;
51 unsigned int serverId = 0;
52 std::size_t queueSize = 0;
53 std::size_t activeServers = 0;
54};
55
59struct Snapshot {
60 unsigned long cycle = 0;
61 std::size_t queueSize = 0;
62 std::size_t liveServers = 0;
63 std::size_t busyServers = 0;
64 std::size_t idleServers = 0;
65 std::size_t retiringServers = 0;
66 unsigned long completed = 0;
67 unsigned long rejected = 0;
68};
69
74 unsigned long received = 0;
75 unsigned long admitted = 0;
76 unsigned long rejected = 0;
77 unsigned long discarded = 0;
78 unsigned long dispatched = 0;
79 unsigned long completed = 0;
80 unsigned long long totalWait = 0;
81 unsigned long maxWait = 0;
82 unsigned long scaleUps = 0;
83 unsigned long scaleDowns = 0;
84 std::size_t startingQueue = 0;
85 std::size_t queueAfterFirstDispatch = 0;
86 std::size_t peakQueue = 0;
87 unsigned long peakQueueCycle = 0;
88 std::size_t initialServers = 0;
89 std::size_t minActiveServers = 0;
90 std::size_t peakLiveServers = 0;
91 unsigned long long serverCycles = 0;
92 unsigned long long busyCycles = 0;
93};
94
117public:
127 LoadBalancer(std::string name, std::size_t initialServers, const ScalingPolicy& policy,
128 const Firewall& firewall, std::size_t maxQueue, Logger& logger);
129
140 bool admit(const Request& request, unsigned long cycle);
141
147 void start();
148
154 void processCycle(unsigned long cycle, const std::vector<Request>& arrivals);
155
160 void recordSnapshot(unsigned long cycle);
161
167 std::string statusLine(unsigned long cycle) const;
168
173 void summarize(std::ostream& out) const;
174
176 const std::string& name() const;
178 std::size_t queueSize() const;
180 std::size_t liveServers() const;
182 std::size_t activeServers() const;
184 std::size_t busyServers() const;
186 std::size_t idleServers() const;
188 std::size_t retiringServers() const;
190 std::size_t inService() const;
192 const LoadBalancerStats& stats() const;
194 const std::vector<ScaleEvent>& scaleEvents() const;
196 const std::vector<Snapshot>& snapshots() const;
198 const std::vector<WebServer>& servers() const;
200 const ScalingPolicy& policy() const;
202 const Firewall& firewall() const;
203
204private:
205 void completeWork(unsigned long cycle);
206 void adjustCapacity(unsigned long cycle);
207 void dispatch(unsigned long cycle);
208 void addServer(unsigned long cycle);
209 void recordScale(ScaleEvent::Kind kind, unsigned long cycle, unsigned int serverId);
210 void updateExtremes(unsigned long cycle);
211 std::string describe(const Request& request) const;
212
213 std::string name_;
214 ScalingPolicy policy_;
215 Firewall firewall_;
216 std::size_t maxQueue_;
217 Logger& logger_;
218 std::queue<Request> pending_;
219 std::vector<WebServer> servers_;
220 unsigned int nextServerId_ = 1;
221 LoadBalancerStats stats_;
222 std::vector<ScaleEvent> scaleEvents_;
223 std::vector<Snapshot> snapshots_;
224};
225
226#endif
Declares the Firewall class, an IPv4 range blocker used before requests are queued.
Declares the Logger class that prints colored, mode-aware simulation events.
Defines the Request structure and its JobType that flow through the load balancer.
Declares the WebServer class, a worker that processes one Request at a time.
Rejects requests whose source address falls in a blocked IPv4 range.
Definition Firewall.h:22
void start()
Records the starting queue size and performs the first dispatch at time 0.
const ScalingPolicy & policy() const
std::size_t queueSize() const
const std::string & name() const
const LoadBalancerStats & stats() const
std::size_t busyServers() const
void summarize(std::ostream &out) const
Writes the end-of-run summary for this load balancer.
std::size_t activeServers() const
void processCycle(unsigned long cycle, const std::vector< Request > &arrivals)
Runs one clock cycle: complete work, admit arrivals, adjust capacity, dispatch.
const std::vector< Snapshot > & snapshots() const
std::size_t inService() const
std::size_t idleServers() const
std::string statusLine(unsigned long cycle) const
Builds a one-line status: queue, servers (busy/idle/retiring), completed, rejected.
void recordSnapshot(unsigned long cycle)
Saves a Snapshot of the current state for the periodic table in the log.
std::size_t retiringServers() const
std::size_t liveServers() const
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.
const std::vector< ScaleEvent > & scaleEvents() const
const Firewall & firewall() const
const std::vector< WebServer > & servers() const
bool admit(const Request &request, unsigned long cycle)
Offers a request to the load balancer.
Writes simulation events to the console and, optionally, to a file.
Definition Logger.h:40
Counters accumulated over a whole run.
unsigned long dispatched
Requests assigned to a server.
std::size_t peakLiveServers
Most live servers at any cycle.
std::size_t queueAfterFirstDispatch
Queue size at time 0, after the first dispatch.
unsigned long discarded
Requests dropped because the queue was full.
unsigned long completed
Requests finished by a server.
unsigned long admitted
Requests that entered the queue.
unsigned long peakQueueCycle
Cycle at which the largest queue was seen.
unsigned long long serverCycles
Sum over cycles of live servers (capacity offered).
std::size_t initialServers
Servers at the start of the run.
unsigned long scaleDowns
Servers removed or marked for retirement.
std::size_t peakQueue
Largest queue size seen.
unsigned long long busyCycles
Server cycles spent processing (capacity used).
unsigned long long totalWait
Sum of queue waits of dispatched requests.
unsigned long rejected
Requests blocked by the firewall.
unsigned long maxWait
Longest queue wait of any dispatched request.
std::size_t startingQueue
Queue size at time 0, before the first dispatch.
std::size_t minActiveServers
Fewest active servers at any cycle.
unsigned long scaleUps
Servers added or reactivated.
unsigned long received
Requests offered to admit().
A single web request waiting for, or receiving, service.
Definition Request.h:41
One change to the server pool, kept for the scaling timeline in the log.
std::size_t queueSize
Queue size that triggered the change.
unsigned long cycle
Clock cycle of the change.
Kind
What happened to the server.
@ Added
A new server was created.
unsigned int serverId
Server affected.
std::size_t activeServers
Active (non-retiring) servers after the change.
Kind kind
Type of change.
Rules for growing and shrinking the server pool.
unsigned int maxServers
Never more live servers (active + retiring) than this.
double lowerFactor
Scale down when queue < lowerFactor x active servers.
unsigned int cooldownCycles
Cycles between scaling evaluations.
unsigned int minServers
Never fewer active servers than this.
double upperFactor
Scale up when queue > upperFactor x active servers.
Point-in-time picture of the load balancer, recorded at regular intervals.
unsigned long rejected
Requests blocked by the firewall so far.
std::size_t liveServers
All servers, including retiring ones.
std::size_t queueSize
Requests waiting in the queue.
unsigned long completed
Requests completed so far.
std::size_t idleServers
Servers with no request (inactive).
std::size_t busyServers
Servers processing a request.
unsigned long cycle
Clock cycle of the snapshot.
std::size_t retiringServers
Servers finishing work before removal.