-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyDispatcher.java
More file actions
77 lines (65 loc) · 2.87 KB
/
MyDispatcher.java
File metadata and controls
77 lines (65 loc) · 2.87 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
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
public class MyDispatcher extends Dispatcher {
private AtomicInteger lastHostId = new AtomicInteger(-1);
public MyDispatcher(SchedulingAlgorithm algorithm, List<Host> hosts) {
super(algorithm, hosts);
}
@Override
public void addTask(Task task) {
switch (algorithm) {
case ROUND_ROBIN:
int nextHostId = lastHostId.incrementAndGet() % hosts.size();
MyHost host = (MyHost) hosts.get(nextHostId);
host.addTask(task);
break;
case SHORTEST_QUEUE:
MyHost shortestQueueHost = (MyHost) hosts.get(0);
int minQueueSize = shortestQueueHost.getQueueSize() + (shortestQueueHost.isTaskRunning() ? 1 : 0);
for (int i = 0; i < hosts.size(); i++) {
MyHost currentHost = (MyHost) hosts.get(i);
int currentQueueSize = currentHost.getQueueSize() + (currentHost.isTaskRunning() ? 1 : 0);
if (currentQueueSize < minQueueSize || (currentQueueSize == minQueueSize)) {
shortestQueueHost = currentHost;
minQueueSize = currentQueueSize;
}
}
shortestQueueHost.addTask(task);
break;
case SIZE_INTERVAL_TASK_ASSIGNMENT:
TaskType taskType = task.getType();
int nodeIndex;
switch (taskType) {
case SHORT:
nodeIndex = 0;
break;
case MEDIUM:
nodeIndex = 1;
break;
case LONG:
nodeIndex = 2;
break;
default:
throw new IllegalArgumentException("Tip de task necunoscut: " + taskType);
}
MyHost assignedHost = (MyHost) hosts.get(nodeIndex);
assignedHost.addTask(task);
break;
case LEAST_WORK_LEFT:
MyHost leastWorkHost = (MyHost) hosts.get(0);
long minWorkLeft = leastWorkHost.getWorkLeft();
for (int i = 1; i < hosts.size(); i++) {
MyHost currentHost = (MyHost) hosts.get(i);
long currentWorkLeft = currentHost.getWorkLeft();
if (currentWorkLeft < minWorkLeft || currentWorkLeft == minWorkLeft) {
leastWorkHost = currentHost;
minWorkLeft = currentWorkLeft;
}
}
leastWorkHost.addTask(task);
break;
default:
System.out.println("Warning: Unknown scheduling algorithm. Defaulting to ROUND_ROBIN.");
}
}
}