forked from ganjingcatherine/Lintcode_HighFreq
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path526.load-balancer.java
More file actions
49 lines (41 loc) · 1.31 KB
/
526.load-balancer.java
File metadata and controls
49 lines (41 loc) · 1.31 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
public class LoadBalancer {
private Map<Integer, Integer> idxMap; // Key: server_id, Value: idx in array
private List<Integer> servers; // array of server_id
private Random rand; // random generator
private int n; // current idx of the servers
public LoadBalancer() {
idxMap = new HashMap<>();
servers = new ArrayList<>();
rand = new Random();
n = 0;
}
/*
* @param server_id: add a new server to the cluster
* @return: nothing
*/
public void add(int server_id) {
servers.add(server_id);
idxMap.put(server_id, n++);
}
/*
* @param server_id: server_id remove a bad server from the cluster
* @return: nothing
*/
public void remove(int server_id) {
int lastItem = servers.get(n - 1);
int removeIdx = idxMap.get(server_id);
idxMap.put(lastItem, removeIdx);
servers.set(removeIdx, lastItem);
// remove the last
servers.remove(n - 1);
idxMap.remove(server_id);
n--;
}
/*
* @return: pick a server in the cluster randomly with equal probability
*/
public int pick() {
int idx = rand.nextInt(n);
return servers.get(idx);
}
}