之前我们分析了四个构造BlockReader类对象函数
1、getLegacyBlockReaderLocal(),该函数返回一个BlockReaderLocalLegacy类对象
2、getBlockReaderLocal(),该函数返回一个BlockReaderLocal类对象
3、getRemoteBlockReaderFromDomain(),该函数返回一个RemoteBlockReader2类对象
4、getRemoteBlockReaderFromTcp(),该函数返回一个RemoteBlockReader2类对象
这三个类的关系如下:
接下来我们开始分析这三个类的read函数。
我们回顾一下之前分析过的read函数,主要分析的是从FSDataInputStream类中的read函数到DFSInputStream类read函数,再到BlockReaderLocal类read函数的过程,但我们没有深入到相应的BlockReaderLocal类的read函数中,这里我们主要开始分析这个类中的read函数,来看看客户端是怎么读取到相应的文件数据的。
总结下来,最终调用BlockReaderLocal类中的函数有两个:
第一个:int read(ByteBuffer buf)
第二个:int read(byte[] buf, int off, int len)
接下来我们将分别分析上面三个类中的这两个函数。
BlockReaderLocalLegacy类中的read函数
BlockReaderLocalLegacy类是老版本的短路读类,基本上已经废弃了,不过我们这里还是进行分析一下,了解它的思路。我们开始分析函数read(ByteBuffer buf),该函数代码如下:
@Override
public synchronized int read(ByteBuffer buf) throws IOException {
int nRead = 0;
//如果是校验和读取
if (verifyChecksum) {
// A 'direct' read actually has three phases. The first drains any
// remaining bytes from the slow read buffer. After this the read is
// guaranteed to be on a checksum chunk boundary. If there are still bytes
// to read, the fast direct path is used for as many remaining bytes as
// possible, up to a multiple of the checksum chunk size. Finally, any
// 'odd' bytes remaining at the end of the read cause another slow read to
// be issued, which involves an extra copy.
// Every 'slow' read tries to