diff --git a/implementations/java/load balancer algorithms/IPHash.java b/implementations/java/load balancer algorithms/IPHash.java new file mode 100644 index 0000000..b90314c --- /dev/null +++ b/implementations/java/load balancer algorithms/IPHash.java @@ -0,0 +1,25 @@ +import java.util.List; + +public class IPHash { + private List servers; + + public IPHash(List servers) { + this.servers = servers; + } + + public String getNextServer(String clientIp) { + int hash = clientIp.hashCode(); + int serverIndex = Math.abs(hash) % servers.size(); + return servers.get(serverIndex); + } + + public static void main(String[] args) { + List servers = List.of("Server1", "Server2", "Server3"); + IPHash ipHash = new IPHash(servers); + + List clientIps = List.of("192.168.0.1", "192.168.0.2", "192.168.0.3"); + for (String ip : clientIps) { + System.out.println(ip + " is mapped to " + ipHash.getNextServer(ip)); + } + } +} diff --git a/implementations/java/load balancer algorithms/LeastConnections.java b/implementations/java/load balancer algorithms/LeastConnections.java new file mode 100644 index 0000000..e87f183 --- /dev/null +++ b/implementations/java/load balancer algorithms/LeastConnections.java @@ -0,0 +1,36 @@ +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class LeastConnections { + private Map serverConnections; + + public LeastConnections(List servers) { + serverConnections = new HashMap<>(); + for (String server : servers) { + serverConnections.put(server, 0); + } + } + + public String getNextServer() { + return serverConnections.entrySet().stream() + .min(Map.Entry.comparingByValue()) + .map(Map.Entry::getKey) + .orElse(null); + } + + public void releaseConnection(String server) { + serverConnections.computeIfPresent(server, (k, v) -> v > 0 ? v - 1 : 0); + } + + public static void main(String[] args) { + List servers = List.of("Server1", "Server2", "Server3"); + LeastConnections leastConnectionsLB = new LeastConnections(servers); + + for (int i = 0; i < 6; i++) { + String server = leastConnectionsLB.getNextServer(); + System.out.println(server); + leastConnectionsLB.releaseConnection(server); + } + } +} diff --git a/implementations/java/load balancer algorithms/LeastResponseTime.java b/implementations/java/load balancer algorithms/LeastResponseTime.java new file mode 100644 index 0000000..cea1e49 --- /dev/null +++ b/implementations/java/load balancer algorithms/LeastResponseTime.java @@ -0,0 +1,58 @@ +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +public class LeastResponseTime { + private List servers; + private List responseTimes; + + public LeastResponseTime(List servers) { + this.servers = servers; + this.responseTimes = new ArrayList<>(servers.size()); + for (int i = 0; i < servers.size(); i++) + responseTimes.add(0.0); + } + + public String getNextServer() { + double minResponseTime = responseTimes.get(0); + int minIndex = 0; + for (int i = 1; i < responseTimes.size(); i++) { + if (responseTimes.get(i) < minResponseTime) { + minResponseTime = responseTimes.get(i); + minIndex = i; + } + } + return servers.get(minIndex); + } + + public void updateResponseTime(String server, double responseTime) { + int index = servers.indexOf(server); + responseTimes.set(index, responseTime); + } + + public static double simulateResponseTime(String server) { + // Simulating response time with random delay + Random random = new Random(); + double delay = 0.1 + (1.0 - 0.1) * random.nextDouble(); + try { + Thread.sleep((long) (delay * 1000)); + } catch (InterruptedException e) { + e.printStackTrace(); + } + return delay; + } + + public static void main(String[] args) { + List servers = List.of("Server1", "Server2", "Server3"); + LeastResponseTime leastResponseTimeLB = new LeastResponseTime(servers); + + for (int i = 0; i < 6; i++) { + String server = leastResponseTimeLB.getNextServer(); + System.out.println("Request " + (i + 1) + " -> " + server); + double responseTime = simulateResponseTime(server); + leastResponseTimeLB.updateResponseTime(server, responseTime); + System.out.println("Response Time: " + String.format("%.2f", responseTime) + "s"); + } + + } +} diff --git a/implementations/java/load balancer algorithms/RoundRobin.java b/implementations/java/load balancer algorithms/RoundRobin.java new file mode 100644 index 0000000..ad1964a --- /dev/null +++ b/implementations/java/load balancer algorithms/RoundRobin.java @@ -0,0 +1,26 @@ +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; + +public class RoundRobin { + private List servers; + private AtomicInteger index; + + public RoundRobin(List servers) { + this.servers = servers; + this.index = new AtomicInteger(-1); + } + + public String getNextServer() { + int currentIndex = index.incrementAndGet() % servers.size(); + return servers.get(currentIndex); + } + + public static void main(String[] args) { + List servers = List.of("Server1", "Server2", "Server3"); + RoundRobin roundRobinLB = new RoundRobin(servers); + + for (int i = 0; i < 6; i++) { + System.out.println(roundRobinLB.getNextServer()); + } + } +} diff --git a/implementations/java/load balancer algorithms/WeightedRoundRobin.java b/implementations/java/load balancer algorithms/WeightedRoundRobin.java new file mode 100644 index 0000000..83825ad --- /dev/null +++ b/implementations/java/load balancer algorithms/WeightedRoundRobin.java @@ -0,0 +1,44 @@ +import java.util.List; + +public class WeightedRoundRobin { + private List servers; + private List weights; + private int currentIndex; + private int currentWeight; + + public WeightedRoundRobin(List servers, List weights) { + this.servers = servers; + this.weights = weights; + this.currentIndex = -1; + this.currentWeight = 0; + } + + public String getNextServer() { + while (true) { + currentIndex = (currentIndex + 1) % servers.size(); + if (currentIndex == 0) { + currentWeight--; + if (currentWeight <= 0) { + currentWeight = getMaxWeight(); + } + } + if (weights.get(currentIndex) >= currentWeight) { + return servers.get(currentIndex); + } + } + } + + private int getMaxWeight() { + return weights.stream().max(Integer::compare).orElse(0); + } + + public static void main(String[] args) { + List servers = List.of("Server1", "Server2", "Server3"); + List weights = List.of(5, 1, 1); + WeightedRoundRobin weightedRoundRobinLB = new WeightedRoundRobin(servers, weights); + + for (int i = 0; i < 7; i++) { + System.out.println(weightedRoundRobinLB.getNextServer()); + } + } +}