Hadoop源码阅读(二):DataNode启动

说明:

1.Hadoop版本:3.1.3

2.阅读工具:IDEA 2023.1.2

3.源码获取:Index of /dist/hadoop/core/hadoop-3.1.3 (apache.org)

4.工程导入:下载源码之后得到 hadoop-3.1.3-src.tar.gz 压缩包,在当前目录打开PowerShell,使用tar -zxvf指令解压即可,然后使用IDEA打开hadoop-3.1.3-src文件夹,要注意配置好Maven或Gradle仓库,否则jar包导入会比较慢

5.参考课程:www.bilibili.com/video/BV1Qp...

ctrl + n 全局查找datanode,进入DataNode.java

官方对齐介绍如下:

markdown 复制代码
/**********************************************************
 * DataNode is a class (and program) that stores a set of
 * blocks for a DFS deployment.  A single deployment can
 * have one or many DataNodes.  Each DataNode communicates
 * regularly with a single NameNode.  It also communicates
 * with client code and other DataNodes from time to time.
 *
 * DataNodes store a series of named blocks.  The DataNode
 * allows client code to read these blocks, or to write new
 * block data.  The DataNode may also, in response to instructions
 * from its NameNode, delete blocks or copy blocks to/from other
 * DataNodes.
 *
 * The DataNode maintains just one critical table:
 *   block-> stream of bytes (of BLOCK_SIZE or less)
 *
 * This info is stored on a local disk.  The DataNode
 * reports the table's contents to the NameNode upon startup
 * and every so often afterwards.
 *
 * DataNodes spend their lives in an endless loop of asking
 * the NameNode for something to do.  A NameNode cannot connect
 * to a DataNode directly; a NameNode simply returns values from
 * functions invoked by a DataNode.
 *
 * DataNodes maintain an open server socket so that client code 
 * or other DataNodes can read/write data.  The host/port for
 * this server is reported to the NameNode, which then sends that
 * information to clients or other DataNodes that might be interested.
 *
 **********************************************************/

找到main方法

进入secureMain方法:

进入createDataNode方法:

  • 进入instantiateDataNode方法(初始化DN):

进入makeInstance方法(实例化对象):

进入DataNode类:

进入startDataNode方法,之后进行DN启动的一系列操作;

1.初始化DataXceiverServer(initDataXceiver)

startDataNode方法中:

进入initDataXceiver方法:

dataXceiverServer是一个服务(线程),DN利用该服务来接收客户端和其他DN发送过来的数据服务

2.初始化HTTP服务(startInfoServer)

startDataNode方法中:

进入startInfoServer方法,实例化了一个httpserver

进入DatanodeHttpServer类:(DatanodeHttpServer.java)

也是通过builder构建了一个HTTP server

3.初始化RPC服务器(initIpcServer)

startDataNode方法中:

进入initIpcServer,构建RPC服务器

4.向NameNode注册(refreshNamenodes)

startDataNode方法中:

进入refreshNamenodes(BlockPoolManager.java)

进入doRefreshNamenodes方法:

先是创建BPOS,然后启动所有的BPOS

  • 接下来进入createBPOS方法

可以看到根据NameNode个数创建对应的服务:

有多少个NN就创建多少个对应的服务

  • 进入startAll方法

可以看到,遍历了所有的bpos,依次进行启动

这里的offerServices是存储所有通过createBPOS方法创建的bpos的集合:

进入bpos.start()方法:

继续进入:

看到bpThread,表示开启一个线程,因此查找其run方法

可以看到通过connectToNNAndHandshake方法向NN进行注册:

  • 通过connectToNN方法来获取NN的RPC客户端对象,进入该方法:

进入DatanodeProtocolClientSideTranslatorPB

通过createNamenode方法来创建NN的RPC代理:

  • 通过register方法来向NN进行注册:

通过bpNamenode.registerDatanode将注册信息发送给NN(bpNamenode即创建的NN RPC proxy)

注意:这里的registerDatanode方法,由DN调用,但执行在NN

FSNamesystem中查找:

ctrl+alt+h查看该方法的调用:

于是到NameNodeRpcServer中去查找:

可以看到,DN的信息向NN注册这一步骤是在该方法中完成的;

接下来回到FSNamesystem中,进入blockManager.registerDatanode方法:

进入datanodeManager.registerDatanode:(DatanodeManager.java)

进入addDatanode

5.向NameNode发送心跳

回到startAll方法,再向前进入到BPServiceActor.java中的run方法

找到其中的offerService方法

进入offerService方法:

进入sendHeartBeat方法:

这里的bpNamenode是在connectToNNAndHandshake方法中获取到的NN的代理

因此该方法实际上是通过NN的RPC客户端将心跳信息发送给NN

因此该方法实际的实现是在NN中,因此在NameNodeRpcServer.java中搜索sendHeartbeat

通过handleHeartbeat来处理DN的心跳信息,进入该方法:

处理DN发送过来的心跳并作出相应;

继续进入handleHeartbeat方法:(DatanodeManager.java)

通过updateHeartbeat方法来更新心跳信息,进入该方法:(HeartbeatManager.java)

再次进入:(BlockManager.java)

继续进入:(DatanodeDescriptor.java)

进入updateHeartbeatState方法:

相关推荐
逸Y 仙X几秒前
文章六:ElasticSearch 集群通信安全权限
java·大数据·服务器·elasticsearch·搜索引擎·全文检索
小脑斧1234 分钟前
AI Skills 全链路自动化运营实践:抖音热点、小红书种草与文生图一体化方案
大数据·人工智能·小红书·skills·自动化运营
白露与泡影9 分钟前
告别OOM焦虑:Flink 内存模型原理与诊断调优
大数据·flink
清辞85335 分钟前
Ai应用——数据分析
大数据
冯RI375II6948738 分钟前
样品准备指南:LFGB认证检测送样要求详解
大数据
冰上浮云1 小时前
Gravitino iceberg catalog backend 为hive 获取元数据过程
数据仓库·hive·hadoop·gravitino
繁星星繁1 小时前
Git 入门之道:从版本流转到基础操作
大数据·git·elasticsearch
段一凡-华北理工大学1 小时前
工业领域的Hadoop架构学习~系列文章06:Hive数据仓库
数据仓库·hadoop·架构·高炉炼铁·工业智能体·高炉智能化·hive数据仓库
Komorebi_99991 小时前
Day3:监控、日志、限流、成本管控、版本灰度
大数据·运维·人工智能·大模型
ITyunwei09871 小时前
运维团队如何抓住AI?
大数据·运维·人工智能