-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02.DesignALoadBalancerSystem.java
More file actions
197 lines (168 loc) · 6.52 KB
/
02.DesignALoadBalancerSystem.java
File metadata and controls
197 lines (168 loc) · 6.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.io.*;
/**
* Demonstration of Load Balancer supporting:
* - Round Robin
* - Weighted Round Robin
* - Least Connections
* along with simulated health checks and concurrency-safe operations.
*/
public class LoadBalancerDemo {
/* ---------------------------- Server Class ---------------------------- */
static class Server {
String id, addr;
int weight;
AtomicInteger activeConn = new AtomicInteger(0);
AtomicInteger totalReq = new AtomicInteger(0);
volatile boolean healthy = true;
Server(String id, String addr, int weight) {
this.id = id;
this.addr = addr;
this.weight = Math.max(1, weight);
}
void acquire() {
activeConn.incrementAndGet();
totalReq.incrementAndGet();
}
void release() {
if (activeConn.get() > 0)
activeConn.decrementAndGet();
}
void markHealthy(boolean val) {
healthy = val;
}
Map<String, Object> snapshot() {
Map<String, Object> m = new LinkedHashMap<>();
m.put("id", id);
m.put("addr", addr);
m.put("weight", weight);
m.put("activeConn", activeConn.get());
m.put("totalReq", totalReq.get());
m.put("healthy", healthy);
return m;
}
}
/* ------------------------ Load Balancer Class ------------------------ */
static class LoadBalancer {
ConcurrentHashMap<String, Server> servers = new ConcurrentHashMap<>();
String algo;
AtomicInteger rrIndex = new AtomicInteger(0);
List<String> weightedQueue = Collections.synchronizedList(new ArrayList<>());
Random rand = new Random();
LoadBalancer(String algo) {
this.algo = algo;
}
void addServer(Server s) {
servers.put(s.id, s);
rebuildWeightedQueue();
}
void removeServer(String id) {
servers.remove(id);
rebuildWeightedQueue();
}
void setAlgorithm(String algo) {
this.algo = algo;
rrIndex.set(0);
}
List<Server> healthyServers() {
List<Server> list = new ArrayList<>();
for (Server s : servers.values())
if (s.healthy) list.add(s);
return list;
}
void rebuildWeightedQueue() {
weightedQueue.clear();
for (Server s : servers.values()) {
if (s.healthy) {
int count = Math.min(s.weight, 10); // capped for demo
for (int i = 0; i < count; i++)
weightedQueue.add(s.id);
}
}
}
Server selectServer() {
List<Server> healthy = healthyServers();
if (healthy.isEmpty()) return null;
switch (algo) {
case "round_robin":
int idx = rrIndex.getAndIncrement() % healthy.size();
return healthy.get(idx);
case "weighted_round_robin":
if (weightedQueue.isEmpty()) rebuildWeightedQueue();
if (weightedQueue.isEmpty()) return null;
String sid = weightedQueue.remove(0);
weightedQueue.add(sid);
return servers.get(sid);
case "least_connections":
return Collections.min(healthy, Comparator.comparingInt(s -> s.activeConn.get()));
default:
return healthy.get(rand.nextInt(healthy.size()));
}
}
void routeRequest(int reqId, ExecutorService pool) {
Server s = selectServer();
if (s == null) {
System.out.println("[LB] No healthy servers for req " + reqId);
return;
}
s.acquire();
System.out.println("[LB] Routed req " + reqId + " -> " + s.id + " (active=" + s.activeConn.get() + ")");
pool.submit(() -> {
try {
Thread.sleep(100 + rand.nextInt(400));
} catch (InterruptedException ignored) {}
s.release();
System.out.println("[Server " + s.id + "] Completed req " + reqId + " (active=" + s.activeConn.get() + ")");
});
}
void healthCheckCycle() {
new Thread(() -> {
while (true) {
for (Server s : servers.values()) {
double p = Math.random();
if (p < 0.02) {
s.markHealthy(false);
System.out.println("[Health] " + s.id + " marked UNHEALTHY");
} else if (!s.healthy && p < 0.3) {
s.markHealthy(true);
System.out.println("[Health] " + s.id + " marked HEALTHY");
}
}
rebuildWeightedQueue();
try { Thread.sleep(1000); } catch (InterruptedException ignored) {}
}
}).start();
}
void snapshot() {
for (Server s : servers.values())
System.out.println(s.snapshot());
}
}
/* ----------------------------- Main Demo ----------------------------- */
public static void main(String[] args) {
LoadBalancer lb = new LoadBalancer("round_robin");
lb.addServer(new Server("s1", "10.0.0.1", 1));
lb.addServer(new Server("s2", "10.0.0.2", 3));
lb.addServer(new Server("s3", "10.0.0.3", 2));
lb.healthCheckCycle();
ExecutorService pool = Executors.newCachedThreadPool();
for (int i = 0; i < 25; i++) {
if (i == 10) {
lb.setAlgorithm("weighted_round_robin");
System.out.println("\n--- Switched to Weighted Round Robin ---\n");
}
if (i == 18) {
lb.setAlgorithm("least_connections");
System.out.println("\n--- Switched to Least Connections ---\n");
}
lb.routeRequest(i, pool);
try { Thread.sleep(80); } catch (InterruptedException ignored) {}
}
pool.shutdown();
try { pool.awaitTermination(3, TimeUnit.SECONDS); } catch (InterruptedException ignored) {}
System.out.println("\nFinal Snapshot:");
lb.snapshot();
}
}