-
Notifications
You must be signed in to change notification settings - Fork 162
/
LoadBalancer.java
112 lines (90 loc) · 2.96 KB
/
LoadBalancer.java
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
import java.util.*;
import java.util.stream.Collectors;
// least connection, round robin, etc... algorithms can be mentioned by client
// during initialisation
//
abstract class LoadBalancer {
Map<RequestType, Service> serviceMap;
public void register(RequestType requestType, Service service) {
serviceMap.put(requestType, service);
}
Set<Destination> getDestinations(Request request){
Service service = serviceMap.get(request.requestType);
return service.destinations;
}
abstract Destination balanceLoad(Request request);
}
class LeastConnectionLoadBalancer extends LoadBalancer {
@Override
Destination balanceLoad(Request request) {
return getDestinations(request).stream()
.min(Comparator.comparingInt(d -> d.requestsBeingServed))
.orElseThrow();
}
}
class RoutedLoadBalancer extends LoadBalancer {
@Override
Destination balanceLoad(Request request) {
Set<Destination> destinations = getDestinations(request);
List<Destination> list = destinations.stream().collect(Collectors.toList());
return list.get(request.id.hashCode() % list.size());
}
}
class RoundRobinLoadBalancer extends LoadBalancer {
Map<RequestType, Queue<Destination>> destinationsForRequest;
@Override
Destination balanceLoad(Request request) {
if (!destinationsForRequest.containsKey(request.requestType)){
Set<Destination> destinations = getDestinations(request);
destinationsForRequest.put(request.requestType, cTq(destinations));
}
Destination destination = destinationsForRequest.get(request.requestType).poll();
destinationsForRequest.get(request.requestType).add(destination);
return destination;
}
private Queue<Destination> cTq(Set<Destination> destinations) {
return null;
}
}
class Service {
String name;
Set<Destination> destinations;
public void addDestination(Destination destination){
destinations.add(destination);
}
public void removeDestination(Destination destination){
destinations.remove(destination);
}
}
class Request {
String id;
RequestType requestType;
Map<String, String> parameters;
}
enum RequestType {
}
class Destination {
String ipAddress;
int requestsBeingServed;
int threshold;
public boolean acceptRequest(Request request) {
if (threshold <= requestsBeingServed) {
requestsBeingServed++;
return true;
} else {
return false;
}
}
private void completeRequest(){
requestsBeingServed--;
}
}
class LoadBalancerFactory {
public LoadBalancer createLoadBalancer(String lbType) {
return switch (lbType) {
case "round-robin" -> new RoundRobinLoadBalancer();
case "least-connection" -> new LeastConnectionLoadBalancer();
default -> new LeastConnectionLoadBalancer();
};
}
}