HDFS-14106. Refactor NamenodeFsck#copyBlock. Contributed by Beluga Behr.

This commit is contained in:
Giovanni Matteo Fumarola 2018-11-30 10:47:59 -08:00
parent 6d7b44c489
commit b09cfad432

View File

@ -1091,30 +1091,35 @@ public Peer newConnectedPeer(InetSocketAddress addr,
deadNodes.add(chosenNode); deadNodes.add(chosenNode);
} }
} }
byte[] buf = new byte[1024];
int cnt = 0; long bytesRead = 0L;
boolean success = true;
long bytesRead = 0;
try { try {
while ((cnt = blockReader.read(buf, 0, buf.length)) > 0) { bytesRead = copyBock(blockReader, fos);
fos.write(buf, 0, cnt);
bytesRead += cnt;
}
if ( bytesRead != block.getNumBytes() ) {
throw new IOException("Recorded block size is " + block.getNumBytes() +
", but datanode returned " +bytesRead+" bytes");
}
} catch (Exception e) { } catch (Exception e) {
LOG.error("Error reading block", e); throw new Exception("Could not copy block data for " + lblock.getBlock(),
success = false; e);
} finally { } finally {
blockReader.close(); blockReader.close();
} }
if (!success) {
throw new Exception("Could not copy block data for " + lblock.getBlock()); if (bytesRead != block.getNumBytes()) {
throw new IOException("Recorded block size is " + block.getNumBytes()
+ ", but datanode returned " + bytesRead + " bytes");
} }
} }
private long copyBock(BlockReader blockReader, OutputStream os)
throws IOException {
final byte[] buf = new byte[8192];
int cnt = 0;
long bytesRead = 0L;
while ((cnt = blockReader.read(buf, 0, buf.length)) > 0) {
os.write(buf, 0, cnt);
bytesRead += cnt;
}
return bytesRead;
}
@Override @Override
public DataEncryptionKey newDataEncryptionKey() throws IOException { public DataEncryptionKey newDataEncryptionKey() throws IOException {
return namenode.getRpcServer().getDataEncryptionKey(); return namenode.getRpcServer().getDataEncryptionKey();