Hadoop之DataNode启动源码解析
添加依赖
为了能够编译和运行 Hadoop 的 DataNode 组件,我们需要在项目的 pom.xml
文件中添加以下依赖:
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>3.1.3</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>3.1.3</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs-client</artifactId>
<version>3.1.3</version>
<scope>provided</scope>
</dependency>
</dependencies>
DataNode 类介绍
DataNode
类是 Hadoop 分布式文件系统 (HDFS) 中的一个核心组件,它负责存储文件系统的数据块。每个部署可以包含一个或多个 DataNode
实例。DataNode
与 NameNode
通信以报告其存储状态,并响应来自 NameNode
的指令,如删除或复制块等操作。此外,DataNode
还需要与客户端代码和其他 DataNode
进行交互。
DataNode 主程序入口
DataNode
的主程序入口点位于 main
方法中,该方法首先检查命令行参数是否存在帮助请求,然后调用 secureMain
方法初始化 DataNode
实例。
public static void main(String args[]) {
if (DFSUtil.parseHelpArgument(args, DataNode.USAGE, System.out, true)) {
System.exit(0);
}
secureMain(args, null);
}
public static void secureMain(String args[], SecureResources resources) {
int errorCode = 0;
try {
StringUtils.startupShutdownMessage(DataNode.class, args, LOG);
DataNode datanode = createDataNode(args, null, resources);
… …
} catch (Throwable e) {
LOG.error("Exception in secureMain", e);
terminate(1, e);
} finally {
LOG.warn("Exiting Datanode");
terminate(errorCode);
}
}
DataNode 实例化
createDataNode
方法用于实例化 DataNode
对象,并启动其守护进程。
public static DataNode createDataNode(String args[], Configuration conf,
SecureResources resources) throws IOException {
// 初始化DN
DataNode dn = instantiateDataNode(args, conf, resources);
if (dn != null) {
// 启动DN进程
dn.runDatanodeDaemon();
}
return dn;
}
public static DataNode instantiateDataNode(String args[], Configuration conf,
SecureResources resources) throws IOException {
... ...
return makeInstance(dataLocations, conf, resources);
}
static DataNode makeInstance(Collection<StorageLocation> dataDirs,
Configuration conf, SecureResources resources) throws IOException {
... ...
return new DataNode(conf, locations, storageLocationChecker, resources);
}
DataNode 构造函数
构造函数初始化了 DataNode
的