DFSOutputStream
概述
DFSOutputStream从字节流中创建文件。
client端写入的数据首先由这个这个流缓存。data数据首先分割成packets,每个packet都是64k的大小。每个packet由chunk组成,每个chunk都是512的字节大小,并且都附带一个checksum。
当client端填充完currentPacket后,会把它放入DataStreamer的dataQueue队列中。DataStreamer是一个线程,它会从dataQueue中轮询取出packet,并将它发送到pipeline管道中的第一个datanode。
成员变量
protected final DFSClient dfsClient;
protected DFSPacket currentPacket = null;
protected DataStreamer streamer;
protected int packetSize = 0; // write packet size, not including the header.
protected int chunksPerPacket = 0;
将currentPacket放入DataStreamer的dataQueue队列
void enqueueCurrentPacket() throws IOException {
getStreamer().waitAndQueuePacket(currentPacket);
currentPacket = null;
}
DataStreamer
概述
DataStreamer负责发送data packet到pipeline上的datanode。它从namenode检索新的block id和block location,然后将packet流式发送到datanode pipeline上。每个packet都有一个与之相连的sequence number。当一个block上的所有packet都已经发送出去,并收到ack时,DataStreamer才会关闭这个block。
DataStreamer线程从dataQueue中取出packet,并将之发送到pipeline上的第一个datanode节点,然后才把这个packet从dataQueue移动到ackQueue。ResponseProcessor接收来自datanode的ack。一个ack表示packet已经成功地被pipeline上的所有datanode接收,当ResponseProcessor接收到ack时,才将对应的packet从ackQueue中清除。
一旦发生错误,将从ackQueue清除所有的packet,并且清除原pipeline中出错的datanode,以此构建一个新的pipeline。
成员变量
private ResponseProcessor response = null;
// both dataQueue and ackQueue are protected by dataQueue lock
protected final LinkedList<DFSPacket> dataQueue = new LinkedList<>();
private final LinkedList<DFSPacket> ackQueue = new LinkedList<>();