学习 HDFS(六):Trouble Shooting

问题一:HDFS 读超时

堆栈信息:

java.net.SocketTimeoutException: 60000 millis timeout while waiting for channel to be ready for read. ch : java.nio.channels.SocketChannel[connected local=/xxx.xxx.xxxx.xxx:36622 remote=/xxx.xxx.xxx.xxx:50010]  
 at org.apache.hadoop.net.SocketIOWithTimeout.doIO(SocketIOWithTimeout.java:164)
 at org.apache.hadoop.net.SocketInputStream.read(SocketInputStream.java:161)
 at org.apache.hadoop.net.SocketInputStream.read(SocketInputStream.java:131)
 at org.apache.hadoop.net.SocketInputStream.read(SocketInputStream.java:118)
 at java.io.FilterInputStream.read(FilterInputStream.java:83)
 at org.apache.hadoop.hdfs.protocolPB.PBHelper.vintPrefixed(PBHelper.java:2239)
 at org.apache.hadoop.hdfs.RemoteBlockReader2.newBlockReader(RemoteBlockReader2.java:430)
 at org.apache.hadoop.hdfs.BlockReaderFactory.getRemoteBlockReader(BlockReaderFactory.java:881)
 at org.apache.hadoop.hdfs.BlockReaderFactory.getRemoteBlockReaderFromTcp(BlockReaderFactory.java:759)
 at org.apache.hadoop.hdfs.BlockReaderFactory.build(BlockReaderFactory.java:376)
 at org.apache.hadoop.hdfs.DFSInputStream.blockSeekTo(DFSInputStream.java:662)
 at org.apache.hadoop.hdfs.DFSInputStream.readWithStrategy(DFSInputStream.java:889)
 at org.apache.hadoop.hdfs.DFSInputStream.read(DFSInputStream.java:942)
 at org.apache.hadoop.hdfs.DFSInputStream.read(DFSInputStream.java:742)
 at java.io.DataInputStream.readByte(DataInputStream.java:265)
 at org.apache.hadoop.io.WritableUtils.readVLong(WritableUtils.java:308)
 at org.apache.hadoop.io.WritableUtils.readVIntInRange(WritableUtils.java:348)
 at org.apache.hadoop.io.Text.readString(Text.java:471)
 at org.apache.hadoop.io.Text.readString(Text.java:464)

解决方法:

产生问题的原因是读取 DataNode 的并发过大,导致 Socket 连接超时。解决的办法有两个:

  1. 增加 HDFS 客户端连接超时时间:编辑 hdfs-core.xml 文件,修改 dfs.client.socket-timeout 属性,默认值为 60000 (1 分钟)
  2. 增加 DataNode 可处理连接的并发数:编辑 hdfs-core.xml 文件,修改 dfs.datanode.handler.count 属性,默认值为 10

问题二:最后一个 block 没有足够的副本数,文件关闭失败

堆栈信息:

java.io.IOException: Unable to close file because the last block does not have enough number of replicas.  
    at org.apache.hadoop.hdfs.DFSOutputStream.completeFile(DFSOutputStream.java:2528)
    at org.apache.hadoop.hdfs.DFSOutputStream.closeImpl(DFSOutputStream.java:2495)
    at org.apache.hadoop.hdfs.DFSOutputStream.close(DFSOutputStream.java:2458)
    at org.apache.hadoop.fs.FSDataOutputStream$PositionCache.close(FSDataOutputStream.java:72)
    at org.apache.hadoop.fs.FSDataOutputStream.close(FSDataOutputStream.java:106)

解决办法:

NameNode 收到 HDFS 客户端关闭文件请求之后,会检查最后一个 block 的完成状态,只有当足够的 DataNode 上报 block 完成才可关闭文件。

网络 IO 延迟和 GC 等原因都将导致 DataNode 延迟上报。

HDFS 客户端会多次尝试关闭文件,通过增加重试次数可减少该问题。编辑文件 hdfs-core.xml 文件,修改 dfs.client.block.write.locateFollowingBlock.retries 属性,默认为 5