Skip to content

Commit 5e3773d

Browse files
committed
Handled read case when file does not exist
1 parent ad33adf commit 5e3773d

File tree

3 files changed

+27
-18
lines changed

3 files changed

+27
-18
lines changed

run.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ CLIENT_CODE_DIR="Client"
1515
PROTO_CODE_DIR="Proto"
1616
DEPENDENCIES="ExternalLibs/protobuf-java-3.0.0.jar"
1717
COMPILED_DIR="Compiled"
18-
NUM_DNS=2
18+
NUM_DNS=4
1919

2020
echo "Compiling..."
2121

src/Client/Client.java

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,12 @@ public List<Hdfs.BlockLocations> openFileForRead(String fileName) {
104104
try {
105105
byte[] res = nn.openFile(openRequest);
106106
Hdfs.OpenFileResponse openResponse = Hdfs.OpenFileResponse.parseFrom(res);
107-
byte[] blockLocationRequest = ProtoMessage.blockLocationRequest(openResponse.getBlockNumsList());
108-
byte[] blockLocationResponse = nn.getBlockLocations(blockLocationRequest);
109-
Hdfs.BlockLocationResponse response = Hdfs.BlockLocationResponse.parseFrom(blockLocationResponse);
110-
blockLocations = response.getBlockLocationsList();
107+
if(openResponse.getStatus() == 1) {
108+
byte[] blockLocationRequest = ProtoMessage.blockLocationRequest(openResponse.getBlockNumsList());
109+
byte[] blockLocationResponse = nn.getBlockLocations(blockLocationRequest);
110+
Hdfs.BlockLocationResponse response = Hdfs.BlockLocationResponse.parseFrom(blockLocationResponse);
111+
blockLocations = response.getBlockLocationsList();
112+
}
111113
} catch (Exception e) { log(e.toString()); }
112114
return blockLocations;
113115
}
@@ -143,20 +145,25 @@ public void writeFile(String fileName, byte[] data) {
143145
public void readFile(String fileName) {
144146
ByteString data = ByteString.EMPTY;
145147
List<Hdfs.BlockLocations> blockLocations = openFileForRead(fileName);
146-
for (Hdfs.BlockLocations block: blockLocations) {
147-
Random rand = new Random();
148-
Integer dataNodeInd = rand.nextInt(DN_COUNT);
149-
Hdfs.DataNodeLocation dnl = block.getLocations(dataNodeInd);
150-
Integer dataNodeId = dnl.getPort();
151-
log("Pulling data from DN " + dataNodeId.toString());
152-
try {
153-
byte[] request = ProtoMessage.readBlockRequest(block.getBlockNumber());
154-
byte[] response = dns.get(dataNodeId).readBlock(request);
155-
Hdfs.ReadBlockResponse readBlockResponse = Hdfs.ReadBlockResponse.parseFrom(response);
156-
data = data.concat(readBlockResponse.getData(0));
157-
} catch (Exception e) { log(e.toString()); }
148+
if(blockLocations != null) {
149+
for (Hdfs.BlockLocations block: blockLocations) {
150+
Random rand = new Random();
151+
Integer dataNodeInd = rand.nextInt(block.getLocationsCount());
152+
Hdfs.DataNodeLocation dnl = block.getLocations(dataNodeInd);
153+
Integer dataNodeId = dnl.getPort();
154+
log("Pulling data from DN " + dataNodeId.toString());
155+
try {
156+
byte[] request = ProtoMessage.readBlockRequest(block.getBlockNumber());
157+
byte[] response = dns.get(dataNodeId).readBlock(request);
158+
Hdfs.ReadBlockResponse readBlockResponse = Hdfs.ReadBlockResponse.parseFrom(response);
159+
data = data.concat(readBlockResponse.getData(0));
160+
} catch (Exception e) { log(e.toString()); }
161+
}
162+
log("File " + fileName + " has contents: " + data.toStringUtf8());
163+
}
164+
else {
165+
log("File : " + fileName + " does not exist.");
158166
}
159-
System.out.println("File " + fileName + " has contents: " + data.toStringUtf8());
160167
}
161168

162169
public void mainloop() {

src/NameNode/NameNode.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ public byte[] openFile(byte[] inp) throws RemoteException {
108108
}
109109
else {
110110
String filename = openFileRequest.getFileName();
111+
if(fileNameToHandle.containsKey(filename) == false)
112+
return ProtoMessage.openFileResponse(0, -1);
111113
Integer handle = fileNameToHandle.get(filename);
112114
log("Opening file '"
113115
+ openFileRequest.getFileName()

0 commit comments

Comments
 (0)