Hadoop(五)—— HDFS NameNode、DataNode工作机制
一、NN与2NN工作机制
NameNode(NN)
1、当HDFS启动时,会加载日志(edits)和镜像文件(fsImage)到内存中。
2-4、当元数据的增删改查请求进来时,NameNode会先将操作记录到日志中,之后再执行内存数据的增删改查
SecondNameNode(2NN)
1、SecondNameNode请求NameNode,请求是否需要checkPoint,checkPoint的触发条件:
- 定时时间到
- 日志文件满了
2-6、请求checkPoint,会将最近写的edits和fsImage拷贝到SecondNameNode本地,加载到内存中合并,生成fsImage.checkpoint。
7-8 将fsImage.checkpoint拷贝到NameNode,并更名为fsImage
二、Fs Image与Edits解析
到/usr/local/hadoop/Hadoop_tmp/dfs/name/current查看日志和fsImage
查看fsImage
hdfs oiv -p XML -i fsimage_0000000000000000131 -o fsimage.xml
得到结果
<?xml version="1.0"?>
<fsimage><NameSection>
<genstampV1>1000</genstampV1><genstampV2>1006</genstampV2><genstampV1Limit>0</genstampV1Limit><lastAllocatedBlockId>1073741829</lastAllocatedBlockId><txid>131</txid></NameSection>
<INodeSection><lastInodeId>16424</lastInodeId><inode><id>16385</id><type>DIRECTORY</type><name></name><mtime>1576421043641</mtime><permission>fangzhijie:supergroup:rwxr-xr-x</permission><nsquota>9223372036854775807</nsquota><dsquota>-1</dsquota></inode>
<inode><id>16386</id><type>DIRECTORY</type><name>fzj</name><mtime>1575714994678</mtime><permission>fangzhijie:supergroup:rwxr-xr-x</permission><nsquota>-1</nsquota><dsquota>-1</dsquota></inode>
<inode><id>16387</id><type>DIRECTORY</type><name>jiagoushi</name><mtime>1575714994678</mtime><permission>fangzhijie:supergroup:rwxr-xr-x</permission><nsquota>-1</nsquota><dsquota>-1</dsquota></inode>
<inode><id>16392</id><type>DIRECTORY</type><name>tmp</name><mtime>1576021083427</mtime><permission>fangzhijie:supergroup:rwx-wx-wx</permission><nsquota>-1</nsquota><dsquota>-1</dsquota></inode>
<inode><id>16393</id><type>DIRECTORY</type><name>hive</name><mtime>1576021083443</mtime><permission>fangzhijie:supergroup:rwx-wx-wx</permission><nsquota>-1</nsquota><dsquota>-1</dsquota></inode>
<inode><id>16394</id><type>DIRECTORY</type><name>fangzhijie</name><mtime>1576022280367</mtime><permission>fangzhijie:supergroup:rwx------</permission><nsquota>-1</nsquota><dsquota>-1</dsquota></inode>
<inode><id>16411</id><type>DIRECTORY</type><name>user</name><mtime>1576021954868</mtime><permission>fangzhijie:supergroup:rwxr-xr-x</permission><nsquota>-1</nsquota><dsquota>-1</dsquota></inode>
<inode><id>16412</id><type>DIRECTORY</type><name>hive</name><mtime>1576021954868</mtime><permission>fangzhijie:supergroup:rwxr-xr-x</permission><nsquota>-1</nsquota><dsquota>-1</dsquota></inode>
<inode><id>16413</id><type>DIRECTORY</type><name>warehouse</name><mtime>1576021954868</mtime><permission>fangzhijie:supergroup:rwxr-xr-x</permission><nsquota>-1</nsquota><dsquota>-1</dsquota></inode>
<inode><id>16414</id><type>DIRECTORY</type><name>bigdata</name><mtime>1576021954868</mtime><permission>fangzhijie:supergroup:rwxr-xr-x</permission><nsquota>-1</nsquota><dsquota>-1</dsquota></inode>
<inode><id>16417</id><type>DIRECTORY</type><name>test</name><mtime>1576326634124</mtime><permission>fangzhijie:supergroup:rwxr-xr-x</permission><nsquota>-1</nsquota><dsquota>-1</dsquota></inode>
<inode><id>16418</id><type>DIRECTORY</type><name>hdfs</name><mtime>1576327378643</mtime><permission>fangzhijie:supergroup:rwxr-xr-x</permission><nsquota>-1</nsquota><dsquota>-1</dsquota></inode>
<inode><id>16419</id><type>DIRECTORY</type><name>client</name><mtime>1576326634124</mtime><permission>fangzhijie:supergroup:rwxr-xr-x</permission><nsquota>-1</nsquota><dsquota>-1</dsquota></inode>
<inode><id>16420</id><type>DIRECTORY</type><name>client1</name><mtime>1576327180252</mtime><permission>fangzhijie:supergroup:rwxr-xr-x</permission><nsquota>-1</nsquota><dsquota>-1</dsquota></inode>
<inode><id>16421</id><type>DIRECTORY</type><name>client2</name><mtime>1576327378643</mtime><permission>fangzhijie:supergroup:rwxr-xr-x</permission><nsquota>-1</nsquota><dsquota>-1</dsquota></inode>
<inode><id>16422</id><type>FILE</type><name>hdfs-test123456.xml</name><replication>3</replication><mtime>1576327781065</mtime><atime>1576327780439</atime><perferredBlockSize>134217728</perferredBlockSize><permission>fangzhijie:supergroup:rw-r--r--</permission><blocks><block><id>1073741827</id><genstamp>1004</genstamp><numBytes>878</numBytes></block>
</blocks>
</inode>
<inode><id>16423</id><type>FILE</type><name>hdfs-test123.xml</name><replication>2</replication><mtime>1576418894630</mtime><atime>1576418893994</atime><perferredBlockSize>134217728</perferredBlockSize><permission>fangzhijie:supergroup:rw-r--r--</permission><blocks><block><id>1073741828</id><genstamp>1005</genstamp><numBytes>878</numBytes></block>
</blocks>
</inode>
</INodeSection>
<INodeReferenceSection></INodeReferenceSection><SnapshotSection><snapshotCounter>0</snapshotCounter></SnapshotSection>
<INodeDirectorySection><directory><parent>16385</parent><inode>16386</inode><inode>16423</inode><inode>16422</inode><inode>16417</inode><inode>16392</inode><inode>16411</inode></directory>
<directory><parent>16386</parent><inode>16387</inode></directory>
<directory><parent>16392</parent><inode>16393</inode></directory>
<directory><parent>16393</parent><inode>16394</inode></directory>
<directory><parent>16411</parent><inode>16412</inode></directory>
<directory><parent>16412</parent><inode>16413</inode></directory>
<directory><parent>16413</parent><inode>16414</inode></directory>
<directory><parent>16417</parent><inode>16418</inode></directory>
<directory><parent>16418</parent><inode>16419</inode><inode>16420</inode><inode>16421</inode></directory>
</INodeDirectorySection>
<FileUnderConstructionSection></FileUnderConstructionSection>
<SnapshotDiffSection><diff><inodeid>16385</inodeid></diff></SnapshotDiffSection>
<SecretManagerSection><currentId>0</currentId><tokenSequenceNumber>0</tokenSequenceNumber></SecretManagerSection><CacheManagerSection><nextDirectiveId>1</nextDirectiveId></CacheManagerSection>
</fsimage>
查看日志文件
hdfs oev -p XML -i edits_0000000000000000130-0000000000000000131 -o edits.xml
得到结果
<?xml version="1.0" encoding="UTF-8"?>
<EDITS>
<EDITS_VERSION>-63</EDITS_VERSION>
<RECORD>
<OPCODE>OP_START_LOG_SEGMENT</OPCODE>
<DATA>
<TXID>130</TXID>
</DATA>
</RECORD>
<RECORD>
<OPCODE>OP_END_LOG_SEGMENT</OPCODE>
<DATA>
<TXID>131</TXID>
</DATA>
</RECORD>
</EDITS>
三、NameNode故障处理
方案一
kill掉NameNode进程
将SecondNameNode的数据拷贝到NameNode中
重启NameNode
方案二
使用-importCheckpoint选项启动NameNode守护进程,从而将SecondNameNode中数据拷贝到NameNode目录中。
kill -9 NameNode
删除NameNode存储的数据
将SecondNameNode的数据拷贝到NameNode中
四、安全模式
NameNode进入安全模式的话,文件系统是只读的。
如何判断是否是进入安全模式
▶ ./hdfs dfsadmin -safemode get
19/12/21 23:44:39 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
Safe mode is OFF
进入安全模式
local/hadoop/bin
▶ ./hdfs dfsadmin -safemode enter
19/12/21 23:53:50 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
Safe mode is ON
退出安全模式
./hdfs dfsadmin -safemode leave
等待安全模式
./hdfs dfsadmin -safemode wait
等待的意思是,在安全模式状态是ON的时候,阻塞,直到状态变为OFF,阻塞才取消,开始执行后续的操作。
五、NameNode 高可用
dir
六、NameNode的ZKFC机制
dir
七、数据完整性
循环冗余校验(CRC)码
使用CRC校验码来保证数据的完整性。
CRC 算法的基本思想:将传输的数据当做一个位数很长的数。将这个数除以另一个数。得到的余数作为校验数据附加到原数据后面。
CRC码中,校验位(R位)在信息位(K位)后面.
例如,CRC生成多项式为G(X) = X4 + X + 1,要求出二进制序列10110011的CRC校验码。
多项式对应的二进制为10011
10011 为除数,10110011为被除数,按照异或的计算方式,同为0,异为1,则计算过程如下:
最后得到余数0100,即是校验位。那么整个CRC码为:10110011 0100
参考文档
关于作者
后端程序员,五年开发经验,从事互联网金融方向。技术公众号「清泉白石」。如果您在阅读文章时有什么疑问或者发现文章的错误,欢迎在公众号里给我留言。