-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTaskTracker.java
More file actions
191 lines (162 loc) · 5.99 KB
/
TaskTracker.java
File metadata and controls
191 lines (162 loc) · 5.99 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
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
import java.rmi.RemoteException;
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;
import java.util.*;
import java.io.*;
import java.net.*;
import java.lang.reflect.*;
import IJobTracker.*;
import INameNode.*;
import IDataNode.*;
import Utils.*;
import com.google.protobuf.ByteString;
import Protobuf.HDFS.ReadBlockRequest;
import Protobuf.HDFS.ReadBlockResponse;
import MapReduceProto.MapReduce.*;
public class TaskTracker {
private static IJobTracker jobTracker;
private static INameNode nameNode;
private static Client client;
private static int taskTrackerId;
private static int numMapSlotsFree = 1;
private static int numReduceSlotsFree = 1;
private static final int STATUS_OK = 1;
private static final int STATUS_NOT_OK = 0;
private static final int JOB_FINISH = 2;
private static final int JOB_STARTED = 1;
private static final int JOB_WAIT = 0;
public static void main(String args[]) {
taskTrackerId = 1;
try {
Registry registry = LocateRegistry.getRegistry(Constants.jtIp,Constants.jtPort);
jobTracker = (IJobTracker) registry.lookup("jobtracker");
registry = LocateRegistry.getRegistry(Constants.nnIp,Constants.nnPort);
nameNode = (INameNode) registry.lookup("namenode");
client = new Client();
} catch (Exception e) {
e.printStackTrace();
}
// while (true) {
// Scanner in = new Scanner(System.in);
// String line = in.nextLine();
// if (line.equals("beat"))
// heartBeat();
// }
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try{
heartBeat();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// nope
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}).start();
}
private static void heartBeat() {
HeartBeatRequest.Builder heartBeatRequest = HeartBeatRequest.newBuilder();
heartBeatRequest.setTaskTrackerId(taskTrackerId);
heartBeatRequest.setNumMapSlotsFree(numMapSlotsFree);
heartBeatRequest.setNumReduceSlotsFree(numReduceSlotsFree);
try {
byte[] hBeatResponse = jobTracker.heartBeat(Utils.serialize(heartBeatRequest.build()));
HeartBeatResponse heartBeatResponse = (HeartBeatResponse) Utils.deserialize(hBeatResponse);
if (heartBeatResponse.getStatus() == STATUS_OK) {
List<MapTaskInfo> mapTaskInfos = heartBeatResponse.getMapTasksList();
List<ReducerTaskInfo> reducerTaskInfos = heartBeatResponse.getReduceTasksList();
for (MapTaskInfo mapTaskInfo : mapTaskInfos) {
doMapTask(mapTaskInfo);
}
for (ReducerTaskInfo reducerTaskInfo : reducerTaskInfos) {
doReduceTask(reducerTaskInfo);
}
} else {
// error
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static void doMapTask(MapTaskInfo mapTaskInfo) {
int jobId = mapTaskInfo.getJobId();
int taskId = mapTaskInfo.getTaskId();
String mapName = mapTaskInfo.getMapName();
List<BlockLocations> locations = mapTaskInfo.getInputBlocksList();
String fileContent = "";
try {
for (BlockLocations location : locations) {
DataNodeLocation dataNodeLocation = location.getLocations(0);
String dnIP = dataNodeLocation.getIp();
int dnPort = dataNodeLocation.getPort();
int blockNumber = location.getBlockNumber();
Registry registry = LocateRegistry.getRegistry(dnIP,dnPort);
IDataNode dataNode = (IDataNode) registry.lookup("datanode");
ReadBlockRequest.Builder readBlockRequest = ReadBlockRequest.newBuilder();
readBlockRequest.setBlockNumber(blockNumber);
byte[] rBlockResponse = dataNode.readBlock(Utils.serialize(readBlockRequest.build()));
ReadBlockResponse readBlockResponse = (ReadBlockResponse) Utils.deserialize(rBlockResponse);
int readBlockStatus = readBlockResponse.getStatus();
List<ByteString> data = readBlockResponse.getDataList();
for (ByteString str : data) {
fileContent += str.toStringUtf8();
System.out.println(str.toStringUtf8());
}
}
} catch (Exception e) {
e.printStackTrace();
}
try {
/*File file = new File(mapName + ".jar");
URL url = file.toURI().toURL();
System.out.println(url);
URLClassLoader classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
Method method = URLClassLoader.class.getDeclaredMethod("map", String.class);
method.setAccessible(true);
String mapOutput = (String) method.invoke(classLoader, fileContent);*/
MapNode myMap = (MapNode) Class.forName(mapName).newInstance();
String mapOutput = myMap.map(fileContent) + "\n";
//String mapOutput = new MapNode().map(fileContent) + "\n";
System.out.println(mapOutput);
// write to file
String fileName = "job_" + jobId + "_map_" + taskId;
client.put_file(fileName, mapOutput);
} catch (Exception e) {
e.printStackTrace();
}
}
private static void doReduceTask(ReducerTaskInfo reducerTaskInfo) {
int jobId = reducerTaskInfo.getJobId();
int taskId = reducerTaskInfo.getTaskId();
String reduceName = reducerTaskInfo.getReducerName();
List<String> mapOutputFiles = reducerTaskInfo.getMapOutputFilesList();
String outputFile = reducerTaskInfo.getOutputFile();
String fileContent = "";
String mapOutput = "";
for (String fileName : mapOutputFiles) {
fileContent = client.get_file(fileName);
for (String part : fileContent.split("\n")) {
try {
ReduceNode myReduce = (ReduceNode) Class.forName(reduceName).newInstance();
String output = myReduce.reduce(fileContent);
mapOutput += output;
} catch (Exception e) {
e.printStackTrace();
}
}
}
String outputFileName = outputFile + "_" + jobId + "_" + taskId;
client.put_file(outputFileName, mapOutput);
}
private static void addMapTaskStatus() {
}
private static void addReduceTaskStatus() {
}
}