-
Notifications
You must be signed in to change notification settings - Fork 1
/
JobTracker.java
410 lines (355 loc) · 12.9 KB
/
JobTracker.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
import java.rmi.RemoteException;
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;
import java.util.*;
import java.util.concurrent.locks.*;
import java.io.*;
import IJobTracker.*;
import INameNode.*;
import Utils.*;
import com.google.protobuf.ByteString;
import Protobuf.HDFS.OpenFileRequest;
import Protobuf.HDFS.OpenFileResponse;
import Protobuf.HDFS.BlockLocationRequest;
import Protobuf.HDFS.BlockLocationResponse;
import MapReduceProto.MapReduce.*;
public class JobTracker implements IJobTracker {
private static int jobCounter = 1;
private static int taskCounter = 1;
private static Lock lock = new ReentrantLock();
PriorityQueue<Integer> jobQueue = new PriorityQueue<Integer>();
private static HashMap<Integer, JobSubmitRequest> jobList = new HashMap<>();
private static HashMap<Integer, Integer> jobStatusList = new HashMap<>();
private static HashMap<Integer, List<Protobuf.HDFS.BlockLocations>> jobBlockList = new HashMap<>();
// Map task of a Job
private static HashMap<Integer, ArrayList<MapTaskInfo>> jobToTasks = new HashMap<>();
private static HashMap<Integer, Integer> taskStatusList = new HashMap<>(); // map task status
// Reduce task of a Job
private static HashMap<Integer, ArrayList<ReducerTaskInfo>> jobToReduceTask = new HashMap<>();
private static HashMap<Integer, Integer> reduceTaskStatusList = new HashMap<>(); // reduce task status
// reducer's list
private static HashMap<Integer, Set<Integer>> reducerList = new HashMap<>();
private static HashMap<Integer, Integer> reducerAllowed = new HashMap<>();
private static final int JOB_FINISH = 2;
private static final int JOB_MAP_DONE = 1;
private static final int JOB_WAIT = 0;
private static final int STATUS_OK = 1;
private static final int STATUS_NOT_OK = 0;
private static INameNode nameNode;
public static void main(String args[]) {
try {
JobTracker obj = new JobTracker();
IJobTracker stub = (IJobTracker) UnicastRemoteObject.exportObject(obj, 0);
Registry registry = LocateRegistry.getRegistry();
registry.bind("jobtracker", stub);
System.out.println("JobTracker ready");
nameNode = (INameNode) registry.lookup("namenode");
} catch (Exception e) {
e.printStackTrace();
}
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try{
updateJobStatus();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// nope
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}).start();
}
public byte[] jobSubmit(byte[] inp) {
JobSubmitRequest jobSubmitRequest = (JobSubmitRequest) Utils.deserialize(inp);
lock.lock();
int jobId = jobCounter;
jobCounter++;
lock.unlock();
String mapName, reducerName, inputFile, outputFile;
int numReduceTasks;
mapName = jobSubmitRequest.getMapName();
reducerName = jobSubmitRequest.getReducerName();
inputFile = jobSubmitRequest.getInputFile();
outputFile = jobSubmitRequest.getOutputFile();
numReduceTasks = jobSubmitRequest.getNumReduceTasks();
reducerAllowed.put(jobId, numReduceTasks);
OpenFileRequest.Builder openFileRequest = OpenFileRequest.newBuilder();
openFileRequest.setFileName(inputFile);
openFileRequest.setForRead(true);
JobSubmitResponse.Builder jobSubmitResponse = JobSubmitResponse.newBuilder();
jobSubmitResponse.setStatus(STATUS_OK);
jobSubmitResponse.setJobId(jobId);
try {
byte[] oFileRespose = nameNode.openFile(Utils.serialize(openFileRequest.build()));
OpenFileResponse openFileResponse = (OpenFileResponse) Utils.deserialize(oFileRespose);
List<Integer> blockList = openFileResponse.getBlockNumsList();
BlockLocationRequest.Builder blockLocationRequest = BlockLocationRequest.newBuilder();
for (Integer block : blockList) {
blockLocationRequest.addBlockNums(block);
System.out.println(block);
}
byte[] bLocationResponse = nameNode.getBlockLocations(Utils.serialize(blockLocationRequest.build()));
BlockLocationResponse blockLocationResponse = (BlockLocationResponse) Utils.deserialize(bLocationResponse);
List<Protobuf.HDFS.BlockLocations> blockLocationList = blockLocationResponse.getBlockLocationsList();
// breaking job into map tasks
ArrayList<MapTaskInfo> tasks = new ArrayList<MapTaskInfo>();
for(Protobuf.HDFS.BlockLocations blockLocation :blockLocationList) {
MapTaskInfo.Builder mapTaskInfo = MapTaskInfo.newBuilder();
lock.lock();
int taskId = taskCounter;
taskStatusList.put(taskId, JOB_WAIT);
taskCounter++;
lock.unlock();
mapTaskInfo.setMapName(mapName);
mapTaskInfo.setTaskId(taskId);
mapTaskInfo.setJobId(jobId);
mapTaskInfo.addInputBlocks(adapter(blockLocation));
lock.lock();
tasks.add(mapTaskInfo.build());
lock.unlock();
}
lock.lock();
jobToTasks.put(jobId, tasks);
jobQueue.add(jobId);
jobStatusList.put(jobId, JOB_WAIT);
lock.unlock();
// appending reduce tasks into queue
ArrayList<ReducerTaskInfo> reduceTasks = new ArrayList<ReducerTaskInfo>();
for (MapTaskInfo mapTask : tasks) {
ReducerTaskInfo.Builder reducerTaskInfo = ReducerTaskInfo.newBuilder();
int mapTaskId = mapTask.getTaskId();
lock.lock();
int reduceTaskId = taskCounter;
taskCounter++;
lock.unlock();
String mapOutputFile = "job_" + jobId + "_map_" + mapTaskId;
reducerTaskInfo.setJobId(jobId);
reducerTaskInfo.setTaskId(reduceTaskId);
reducerTaskInfo.addMapOutputFiles(mapOutputFile);
reducerTaskInfo.setReducerName(reducerName);
reducerTaskInfo.setOutputFile(outputFile);
lock.lock();
reduceTasks.add(reducerTaskInfo.build());
lock.unlock();
reduceTaskStatusList.put(reduceTaskId, JOB_WAIT);
}
lock.lock();
jobToReduceTask.put(jobId, reduceTasks);
lock.unlock();
} catch(Exception e) {
jobSubmitResponse.setStatus(STATUS_NOT_OK);
e.printStackTrace();
}
System.out.println("Job submitted: " + jobId);
return Utils.serialize(jobSubmitResponse.build());
}
public byte[] getJobStatus(byte[] inp) {
JobStatusRequest jobStatusRequest = (JobStatusRequest) Utils.deserialize(inp);
int jobId = jobStatusRequest.getJobId();
System.out.println("Job Status Query: " + jobId);
printJobStatus();
JobStatusResponse.Builder jobStatusResponse = JobStatusResponse.newBuilder();
if (jobId < jobCounter && jobId > 0) {
jobStatusResponse.setStatus(STATUS_OK);
jobStatusResponse.setJobDone(jobStatusList.get(jobId) == JOB_FINISH);
jobStatusResponse.setTotalMapTasks(0);
jobStatusResponse.setNumMapTasksStarted(0);
jobStatusResponse.setTotalReduceTasks(0);
jobStatusResponse.setNumReduceTasksStarted(0);
} else
jobStatusResponse.setStatus(STATUS_NOT_OK);
return Utils.serialize(jobStatusResponse.build());
}
public byte[] heartBeat(byte[] inp) {
HeartBeatRequest heartBeatRequest = (HeartBeatRequest) Utils.deserialize(inp);
int taskTrackerId = heartBeatRequest.getTaskTrackerId();
int numMapSlotsFree = heartBeatRequest.getNumMapSlotsFree();
int numReduceSlotsFree = heartBeatRequest.getNumReduceSlotsFree();
DataNodeLocation location = heartBeatRequest.getLocations();
updateMapTaskStatus(heartBeatRequest.getMapStatusList());
updateReduceTaskStatus(heartBeatRequest.getReduceStatusList());
HeartBeatResponse.Builder heartBeatResponse = HeartBeatResponse.newBuilder();
if (numMapSlotsFree > 0) {
MapTaskInfo mapTaskInfo = assignJobToMaps();
if (mapTaskInfo != null)
heartBeatResponse.addMapTasks(mapTaskInfo);
}
if (numReduceSlotsFree > 0) {
ReducerTaskInfo reduceTaskInfo = assignJobToReduce(taskTrackerId);
if (reduceTaskInfo != null) {
heartBeatResponse.addReduceTasks(reduceTaskInfo);
}
}
heartBeatResponse.setStatus(STATUS_OK);
return Utils.serialize(heartBeatResponse.build());
}
private MapTaskInfo assignJobToMaps() {
JobSubmitRequest jobSubmitRequest;
String mapName, reducerName, inputFile, outputFile;
int numReduceTasks;
List<Protobuf.HDFS.BlockLocations> blockLocations;
if (jobQueue.size() != 0) {
lock.lock();
int jobId = jobQueue.remove();
ArrayList<MapTaskInfo> tasks = jobToTasks.get(jobId);
lock.unlock();
if(tasks.size()>0){
MapTaskInfo to_give_task = tasks.get(0);
ArrayList<MapTaskInfo> newList = new ArrayList<MapTaskInfo>();
for(int i=1;i<tasks.size();i++){
newList.add(tasks.get(i));
}
tasks = newList;
lock.lock();
if(tasks.size()>0){
jobToTasks.put(jobId, tasks);
jobQueue.add(jobId);
} else {
jobToTasks.remove(jobId);
}
lock.unlock();
return to_give_task;
}
}
return null;
}
private ReducerTaskInfo assignJobToReduce(int taskTrackerId) {
for (Integer jobId : jobStatusList.keySet()) {
lock.lock();
int status = jobStatusList.get(jobId);
Set<Integer> reducers = reducerList.get(jobId);
int allowed = reducerAllowed.get(jobId);
if (reducers == null) {
reducers = new HashSet<Integer>();
}
if (allowed == reducers.size()) {
int done = 0;
for (int i : reducers) {
if (taskTrackerId == i) {
done = 1;
break;
}
}
if(done==0) continue;
} else if (reducers.size() < allowed) {
reducers.add(taskTrackerId);
reducerList.put(jobId, reducers);
} else
continue;
lock.unlock();
if (status == JOB_MAP_DONE) {
lock.lock();
ArrayList<ReducerTaskInfo> reduceTasks = jobToReduceTask.get(jobId);
lock.unlock();
if(reduceTasks.size()>0){
ReducerTaskInfo to_give_task = reduceTasks.get(0);
ArrayList<ReducerTaskInfo> newList = new ArrayList<ReducerTaskInfo>();
for(int i=1;i<reduceTasks.size();i++){
newList.add(reduceTasks.get(i));
}
reduceTasks = newList;
lock.lock();
if(reduceTasks.size()>0){
jobToReduceTask.put(jobId, reduceTasks);
} else {
jobToReduceTask.remove(jobId);
}
lock.unlock();
return to_give_task;
}
}
}
return null;
}
private BlockLocations adapter(Protobuf.HDFS.BlockLocations location) {
BlockLocations.Builder blockLoc = BlockLocations.newBuilder();
blockLoc.setBlockNumber(location.getBlockNumber());
List<Protobuf.HDFS.DataNodeLocation> dataNodeLocations = location.getLocationsList();
for (Protobuf.HDFS.DataNodeLocation dataNodeLocation : dataNodeLocations) {
DataNodeLocation.Builder dnLocation = DataNodeLocation.newBuilder();
dnLocation.setIp(dataNodeLocation.getIp());
dnLocation.setPort(dataNodeLocation.getPort());
blockLoc.addLocations(dnLocation.build());
}
return blockLoc.build();
}
private void updateMapTaskStatus(List<MapTaskStatus> statusList) {
for (MapTaskStatus status : statusList) {
int jobId = status.getJobId();
int taskId = status.getTaskId();
if (status.getTaskCompleted()) {
taskStatusList.put(taskId, JOB_FINISH);
}
}
}
private void updateReduceTaskStatus(List<ReduceTaskStatus> statusList) {
for (ReduceTaskStatus status : statusList) {
int jobId = status.getJobId();
int taskId = status.getTaskId();
if (status.getTaskCompleted()) {
reduceTaskStatusList.put(taskId, JOB_FINISH);
}
}
}
private static void updateJobStatus() {
// update job status
for (Integer jobId : jobStatusList.keySet()) {
lock.lock();
ArrayList<MapTaskInfo> mapTasks = jobToTasks.get(jobId);
lock.unlock();
boolean jobMapDone = true;
if (mapTasks != null) {
for (MapTaskInfo task : mapTasks) {
if (taskStatusList.get(task.getTaskId()) != JOB_FINISH) {
jobMapDone = false;
break;
}
}
}
lock.lock();
ArrayList<ReducerTaskInfo> reduceTasks = jobToReduceTask.get(jobId);
lock.unlock();
boolean jobReduceDone = true;
if (reduceTasks != null) {
for (ReducerTaskInfo task : reduceTasks) {
if (reduceTaskStatusList.get(task.getTaskId()) != JOB_FINISH) {
jobReduceDone = false;
break;
}
}
}
lock.lock();
if (jobMapDone && jobReduceDone)
jobStatusList.put(jobId, JOB_FINISH);
else if (jobMapDone)
jobStatusList.put(jobId, JOB_MAP_DONE);
lock.unlock();
}
}
public void printJobStatus() {
for (Integer jobId : jobStatusList.keySet()) {
System.out.println("Job: " + jobId + " Status :" + jobStatusList.get(jobId));
ArrayList<MapTaskInfo> mapTasks = jobToTasks.get(jobId);
if (mapTasks != null) {
System.out.println("Map");
for (MapTaskInfo task : mapTasks) {
System.out.println(task + ":" + taskStatusList.get(task.getTaskId()));
}
}
ArrayList<ReducerTaskInfo> reduceTasks = jobToReduceTask.get(jobId);
if (reduceTasks != null) {
System.out.println("Reduce");
for (ReducerTaskInfo task : reduceTasks) {
System.out.println(task + ":" + reduceTaskStatusList.get(task.getTaskId()));
}
}
}
}
}