HDFS
作为管理员,应该对hdfs的各个组件namenode, secendary namenode, datanode是如何在硬盘上组织文件有个基本的了解。
namenode directory structure
${dfs.name.dir}/
└── current/
├── VERSION
├── edits
├── fsimage
└── fstime
VERSION文件是一个java属性文件,包括了hdfs的版本信息,如下是它的一个典型的内容。
#Tue Mar 10 19:21:36 GMT 2009
namespaceID=134368441
cTime=0
339
storageType=NAME_NODE
layoutVersion=-18
layoutVersion: 是负值用来定义hdfs结构的版本信息,当升级系统的时候会有变化。
namespaceID: 是文件系统的唯一标识符,当系统第一次格式化的时候。${dfs.name.dir}/current/VERSION.namespaceID = ${dfs.data.dir}/current/VERSION.namespaceID
cTime: 标志系统的创建时间,对新格式化的系统是0,每次upgrade的时候会写入新的时间。
storageType: NameNode和DataNode
fimage, edits, ftime这些都是二进制文件
fimage: 是系统的元数据在硬盘上的一个持久化检查点
edits: 保存着文件系统每次的操作,创建文件删除文件等
namenode会在每次启动时候合并fimage和edits文件,当集群运行时间过长的时候,edits变的会越来越大,导致下次再启动的时候会花费很长的时间。
namenode目前还是个单点,当数据节点崩溃的时候,整个集群就启动不了。基于上面两点原因,产生了 secondary namenode,用来周期性的合并数据文件和备份元数据文件。
datanode directory structure
${dfs.data.dir}/
└── current/
├── VERSION
├── blk_<id_1>
├── blk_<id_1>.meta
├── blk_<id_2>
├── blk_<id_2>.meta
├── ...
├── blk_<id_64>
├── blk_<id_64>.meta
├── subdir0/
├── subdir1/
├── ...
└── subdir63/
Safe Mode
当数据节点启动的时候第一件事就是把fsimage文件加载到内存当中,并且把edit文件也加载和合并到内存中。当它重构完了之后会生成新的fsimage文件和空的edit文件。
同时也在等待datanode给它发块报告。在这个阶段处于安全模式,对客户端来说处于只读模式,不允许创建文件等操作。
可以用如下的命令来获取安全模式状态,进入,等待和退出安全模式
% hadoop dfsadmin -safemode get
Safe mode is ON
hadoop dfsadmin -safemode wait
# command to read or write a file
% hadoop dfsadmin -safemode enter
Safe mode is ON
% hadoop dfsadmin -safemode leave
Safe mode is OFF
Tools
dfsadmin
-help Shows help for a given command, or all commands if no command is specified.
-report Shows filesystem statistics (similar to those shown in the web UI) and information on connected datanodes.
-metasave Dumps information to a file in Hadoop’s log directory about blocks that are being replicated or deleted, as well as a list of connected datanodes
-safemode Changes or queries the state of safe mode.
-saveNamespace Saves the current in-memory filesystem image to a new fsimage file and resets the edits file. This operation may be performed only in safe mode.
-refreshNodes Updates the set of datanodes that are permitted to connect to the namenode
-upgradeProgress Gets information on the progress of an HDFS upgrade or forces an upgrade to proceed
Filesystem check (fsck)
% hadoop fsck /
......................Status: HEALTHY
Total size: 511799225 B
Total dirs: 10
Total files: 22
Total blocks (validated): 22 (avg. block size 23263601 B)
Minimally replicated blocks: 22 (100.0 %)
Over-replicated blocks: 0 (0.0 %)
Under-replicated blocks: 0 (0.0 %)
Mis-replicated blocks: 0 (0.0 %)
Default replication factor: 3
Average block replication: 3.0
Corrupt blocks: 0
Missing replicas: 0 (0.0 %)
Number of data-nodes: 4
Number of racks: 1
balancer
% start-balancer.sh
Time Stamp Iteration# Bytes Already Moved Bytes Left To Move Bytes Being Moved
Mar 18, 2009 5:23:42 PM 0 0 KB 219.21 MB 150.29 MB
Mar 18, 2009 5:27:14 PM 1 195.24 MB 22.45 MB 150.29 MB
The cluster is balanced. Exiting...
Balancing took 6.072933333333333 minutes
Maintenance
Metadata backups
如果数据节点的元数据丢失或者损坏的话,整个集群都会崩溃并且无法恢复,因此做好元数据的备份工作很重要。第一种做法是在不同的周期(每小时,每天,每周,每月)同时保存几份
元数据的拷贝。还有一种做法是写个脚本周期性的将secendary namenode中的privious.checkpoint子目录备份到安全的地方。
Data backups
尽管hdfs设计为保存每个块的默认三份拷贝,但是由于系统的bug或者机器的当机也有可能导致数据丢失。由于数据量太大,不可能备份所有的数据,因此很重要的
一点是按优先级重要性备份关键数据。
Filesystem check (fsck)
最好应该周期性的比如每天来检查文件系统当中哪些损坏和丢失的数据块。
Filesystem balancer
运行此工具可以均衡整个集群的数据。
Commissioning and Decommissioning Nodes
作为集群的系统管理员,可能需要经常性的添加或者移除数据节点。通过修改conf/slaves文件和配置来添加和移除节点,hdfs的心跳机制可以检测到有哪些
数据节点加入和离开集群。
Upgrades
当升级hdfs的mapreduce的时候需要仔细的做计划,首先要备份系统的元数据,可能还需要改配置文件和不兼容的代码。
Start the upgrade: % $NEW_HADOOP_INSTALL/bin/start-dfs.sh -upgrade
Wait until the upgrade is complete: % $NEW_HADOOP_INSTALL/bin/hadoop dfsadmin -upgradeProgress status
Roll back the upgrade (optional): % $OLD_HADOOP_INSTALL/bin/start-dfs.sh -rollback
Finalize the upgrade (optional): % $NEW_HADOOP_INSTALL/bin/hadoop dfsadmin -finalizeUpgrad % $NEW_HADOOP_INSTALL/bin/hadoop dfsadmin -upgradeProgress status