forked from sanketmarkan/HDFS-MapReduce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataNode.java
185 lines (171 loc) · 6.29 KB
/
DataNode.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
import java.rmi.RemoteException;
import java.rmi.ConnectException;
import java.rmi.server.UnicastRemoteObject;
import IDataNode.*;
import Utils.*;
import java.io.*;
import java.util.*;
import java.net.*;
import INameNode.*;
import Protobuf.HDFS.*;
import com.google.protobuf.ByteString;
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
public class DataNode implements IDataNode {
private static int myId;
private static ArrayList<Integer> blockList;
public static void main(String args[]){
myId = Integer.parseInt(args[0]);
blockList = new ArrayList<Integer>();
init();
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try{
sendHeartBeat();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// nope
}
} catch (ConnectException e) {
System.out.println("NameNode not found");
} catch (Exception e) {
//e.printStackTrace();
}
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try{
sendBlockReport();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// nope
}
} catch (ConnectException e) {
System.out.println("NameNode not found");
} catch (Exception e) {
//e.printStackTrace();
}
}
}
}).start();
}
public static void init() {
try {
DataNode obj = new DataNode();
IDataNode stub = (IDataNode) UnicastRemoteObject.exportObject(obj, 0);
Registry registry = LocateRegistry.getRegistry();
registry.bind("datanode", stub);
System.out.println("DataNode " + myId +" ready");
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public byte[] readBlock(byte[] inp) throws RemoteException {
ReadBlockRequest request = (ReadBlockRequest) Utils.deserialize(inp);
int block = request.getBlockNumber();
String fileName = "block-"+block;
File file = new File(fileName);
ReadBlockResponse.Builder response = ReadBlockResponse.newBuilder();
if (file.exists()) {
response.setStatus(1);
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String currLine;
while ((currLine = br.readLine()) != null) {
response.addData(ByteString.copyFromUtf8(currLine));
}
} catch (Exception e) {
e.printStackTrace();
}
}
else {
response.setStatus(0);
}
return Utils.serialize(response.build());
}
@Override
public byte[] writeBlock(byte[] inp) throws RemoteException {
WriteBlockRequest request = (WriteBlockRequest) Utils.deserialize(inp);
int block = request.getBlockNumber();
String data = request.getData(0).toStringUtf8();
//System.out.println(data);
String fileName = "block-"+block;
File file = new File(fileName);
// data = request.getData();
WriteBlockResponse.Builder response = WriteBlockResponse.newBuilder();
if (file.exists()) {
response.setStatus(0);
}
else {
blockList.add(block);
response.setStatus(1);
try {
FileWriter writer = new FileWriter(file, true);
writer.write(data);
writer.flush();
writer.close();
System.out.println("Wrote to file");
} catch (Exception e) {
e.printStackTrace();
response.setStatus(0);
}
}
return Utils.serialize(response.build());
}
public static byte[] sendBlockReport() throws RemoteException {
BlockReportRequest.Builder request = BlockReportRequest.newBuilder();
request.setId(myId);
for(int block:blockList)
request.addBlockNumbers(block);
try{
Registry registry = LocateRegistry.getRegistry(/*Constants.nnIp,Constants.nnPort*/);
INameNode stub = (INameNode) registry.lookup("namenode");
stub.blockReport(Utils.serialize(request.build()));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void sendHeartBeat() throws RemoteException{
try{
// Registry registry = LocateRegistry.getRegistry(nnIP, nnPort);
Registry registry = LocateRegistry.getRegistry(/*Constants.nnIp,Constants.nnPort*/);
INameNode stub = (INameNode) registry.lookup("namenode");
HeartBeatRequest.Builder request = HeartBeatRequest.newBuilder();
request.setId(myId);
String networkInterfaceName = "lo";
Inet4Address inetAddress = null;
try {
Enumeration<InetAddress> enumeration = NetworkInterface.getByName(networkInterfaceName).getInetAddresses();
while (enumeration.hasMoreElements()) {
InetAddress tempInetAddress = enumeration.nextElement();
if (tempInetAddress instanceof Inet4Address) {
inetAddress = (Inet4Address) tempInetAddress;
}
}
} catch (SocketException e) {
e.printStackTrace();
}
if (inetAddress == null) {
System.err.println("Error Obtaining Network Information");
System.exit(-1);
}
DataNodeLocation.Builder dNbuilder = DataNodeLocation.newBuilder();
dNbuilder.setIp(inetAddress.getHostAddress());
dNbuilder.setPort(Registry.REGISTRY_PORT);
request.setLocation(dNbuilder.build());
stub.heartBeat(Utils.serialize(request.build()));
} catch (Exception e) {
e.printStackTrace();
}
}
}