学习笔记
剖析文件写入
- 客户端通过Distributed FileSystem模块向NameNode请求上传文件,NameNode检查目标文件是否已存在,父目录是否存在
- NameNode 返回是否可以上传
- 客户端请求第一个Block上传到哪几个DataNode服务器上
- NameNode 返回3个DataNode节点,分别为 dn1、dn2、dn3
- 客户端通过FSDataOutputStream 模块请求dn1上传数据,dn1收到请求会继续调用dn2,然后dn2调用dn3,将这个通信管道建立完成
- dn1,dn2,dn3逐级应答客户端
- 客户端开始往dn1上传第一个Block(先从磁盘读取数据放到一个本地内存缓存),以Packet为单位,dn1传到一个Packet就会传给dn2,dn2传给dn3; dn1每传一个packet会放入一个应答队列等待应答
- 当一个Block传输完成之后,客户端再次请求NameNode上传第二个Block的服务器。(重复执行3-7步)
网络拓扑-节点距离计算
在HDFS写数据的过程中,NameNode会选择距离待上传数据最近距离的DataNode接收数据.
节点距离:两个节点到达最近的共同祖先的距离总和
那么这个最近距离怎么计算呢?
例如,假设有数据中心d1机架r1中的节点n1.该节点可以表示为/d1/r1/n1
.
9到5的距离就是3
机架感知(副本存储节点选择)
机架感知说明
官方说明: hadoop.apache.org/docs/r3.1.3…
For the common case, when the replication factor is three, HDFS’s placement policy is to put one replica on the local machine if the writeris on a datanode, otherwise on a random datanode, another replica on a node in a different (remote) rack, and the last on a different node in the same remote rack. This policy cuts the inter-rack write traffic which generally improves write performance. The chance of rack failure is far less than that of node failure; this policy does not impact data reliability and availability guarantees. However, it does reduce the aggregate network bandwidth used when reading data since a block is placed in only two unique racks rather than three. With this policy, the replicas of a file do not evenly distribute across the racks. One third of replicas are on one node, two thirds of replicas are on one rack, and the other third are evenly distributed across the remaining racks. This policy improves write performance without compromising data reliability or read performance.
Crtl + n 查找 BlockPlacementPolicyDefault,在该类中可以查找 chooseTargetInOrder 方法。