• 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.参考课程:尚硅谷大数据Hadoop教程,hadoop3.x搭建到集群调优,百万播放_哔哩哔哩_bilibili

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

    官方对其介绍如下:

    1. /**********************************************************
    2. * DataNode is a class (and program) that stores a set of
    3. * blocks for a DFS deployment. A single deployment can
    4. * have one or many DataNodes. Each DataNode communicates
    5. * regularly with a single NameNode. It also communicates
    6. * with client code and other DataNodes from time to time.
    7. *
    8. * DataNodes store a series of named blocks. The DataNode
    9. * allows client code to read these blocks, or to write new
    10. * block data. The DataNode may also, in response to instructions
    11. * from its NameNode, delete blocks or copy blocks to/from other
    12. * DataNodes.
    13. *
    14. * The DataNode maintains just one critical table:
    15. * block-> stream of bytes (of BLOCK_SIZE or less)
    16. *
    17. * This info is stored on a local disk. The DataNode
    18. * reports the table's contents to the NameNode upon startup
    19. * and every so often afterwards.
    20. *
    21. * DataNodes spend their lives in an endless loop of asking
    22. * the NameNode for something to do. A NameNode cannot connect
    23. * to a DataNode directly; a NameNode simply returns values from
    24. * functions invoked by a DataNode.
    25. *
    26. * DataNodes maintain an open server socket so that client code
    27. * or other DataNodes can read/write data. The host/port for
    28. * this server is reported to the NameNode, which then sends that
    29. * information to clients or other DataNodes that might be interested.
    30. *
    31. **********************************************************/

    找到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方法:

     

     

  • 相关阅读:
    24 | Linux 使用 systemd的使用方法
    Qt Creato配置PCL库
    Element UI表格将两列数据放在一起显示
    【计算机网络】Servlet API重点知识汇总
    云原生周刊:Kubernetes v1.31 发布
    微服务测试怎么做
    化工行业调研:中国二氧化碳(CO2)2028年市场规模将达到64.66亿元
    什么是黑盒测试?白盒测试?
    LA@二次型分类@正定二次型@主子式
    Spring Boot整合日志
  • 原文地址:https://blog.csdn.net/qq_51235856/article/details/132921987