`
zy19982004
  • 浏览: 662066 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
博客专栏
F6f66edc-1c1a-3859-b76b-a22e740b7aa7
Hadoop学习
浏览量:252004
社区版块
存档分类
最新评论

Hadoop学习十三:Hadoop-Hdfs DataStorage源码

 
阅读更多

一.DataStorage

  • DataStorage继承Storage。
  • DataStorage对应Hdfs的dfs.data.dir目录。
  • DataNode启动时,检查恢复状态转换dfs.data.dir下所有的StorageDirectory。

二.DataStorage类图

 

 

三.详细描述

  1. org.apache.hadoop.hdfs.server.protocol.NamespaceInfo:NamespaceInfo is returned by the name-node in reply  to a data-node handshake.当我们配置完整个Hadoop系统时,做的第一件事就是
    $bin/hadoop namenode -format
     format后的NameNode产生一个namespaceId。当DataNode启动时,会向NameNode handshake,NameNode返回这个namespaceId。
    NamespaceInfo nsInfo = handshake();
  2.  DataStorage:
    1. 参考代码中的步骤阅读。状态分析-----恢复操作-----状态转换
    2. 关于“一致性”的说明:参考http://zy19982004.iteye.com/blog/1876706第三张图,/update /rollback /finalize指令后,StorageDirectory下会存在不同的文件夹,当整个文件夹是某个中间状态时,就是“不一致”。举例:/update开始后,“一致”状态指的是current和previous都存在,也就是/update的123步都完成了;如果123中的中间某一步发生了问题或者还没有进行,造成了previous.tmp存在current不存在的情况,就是“不一致”,造成了previous.tmp存在current存在也是不一致,都需要doRecover。

四.DataStorage源码时序图

 

 

五.DataStorage源码

 

package org.apache.hadoop.hdfs.server.datanode;

public class DataStorage extends Storage {

	//DataStorag独特属性
	private String storageID;

	DataStorage() {
		super(NodeType.DATA_NODE);
		storageID = "";
	}

	// DataNode.start时触发此方法
	void recoverTransitionRead(NamespaceInfo nsInfo, Collection<File> dataDirs,
			StartupOption startOpt) throws IOException {
		// 1.在doTransition之前首先对每个SorageDirectory进行分析,检查其是否处于“一致状态”
		// 如果处于“不一致状态“,format or doRecover
		this.storageID = "";
		this.storageDirs = new ArrayList<StorageDirectory>(dataDirs.size());
		ArrayList<StorageState> dataDirStates = new ArrayList<StorageState>(
				dataDirs.size());
		for (Iterator<File> it = dataDirs.iterator(); it.hasNext();) {
			File dataDir = it.next();
			StorageDirectory sd = new StorageDirectory(dataDir);
			StorageState curState;
			try {
				// 1.1 调用Storage.analyzeStorage进行状态分析
				curState = sd.analyzeStorage(startOpt);
				// 1.2 调用format或者Storage.doRecover进行恢复操作
				switch (curState) {
				case NORMAL:
					break;
				case NON_EXISTENT:
					// ignore this storage
					it.remove();
					continue;
				case NOT_FORMATTED: // format
					format(sd, nsInfo);
					break;
				default: // recovery part is common
					sd.doRecover(curState);
				}
			} catch (IOException ioe) {
				sd.unlock();
				throw ioe;
			}
			// 1.3 "一致状态"的StorageDirectory才会加入到Storage
			addStorageDir(sd);
			dataDirStates.add(curState);
		}

		// 2. doTransition 状态转换
		// 根据DataNode启动参数做upgrade or rollback,其它正常启动
		for (int idx = 0; idx < getNumStorageDirs(); idx++) {
			doTransition(getStorageDir(idx), nsInfo, startOpt);
		}

		// 3. Update 每个 StorageDirectory的VERSION
		this.writeAll();
	}

	void format(StorageDirectory sd, NamespaceInfo nsInfo) throws IOException {
		sd.clearDirectory(); // create directory
		this.layoutVersion = FSConstants.LAYOUT_VERSION;
		this.namespaceID = nsInfo.getNamespaceID();
		this.cTime = 0;
		// store storageID as it currently is
		sd.write();
	}

	//重写,DataNode增加storageID到VERSION
	protected void setFields(Properties props, StorageDirectory sd)
			throws IOException {
		super.setFields(props, sd);
		props.setProperty("storageID", storageID);
	}

	//重写,DataNode增加storageID到VERSION
	protected void getFields(Properties props, StorageDirectory sd)
			throws IOException {
		super.getFields(props, sd);
		String ssid = props.getProperty("storageID");
		storageID = ssid;
	}

	/**
	 * Analize which and whether a transition of the fs state is required and
	 * perform it if necessary. Rollback if previousLV >= LAYOUT_VERSION &&
	 * prevCTime <= namenode.cTime Upgrade if this.LV > LAYOUT_VERSION ||
	 * this.cTime < namenode.cTime Regular startup if this.LV = LAYOUT_VERSION
	 * && this.cTime = namenode.cTime
	 */
	// 2 doTransition 状态转换
	// 根据DataNode启动参数做upgrade or rollback,其它正常启动
	private void doTransition( StorageDirectory sd,  NamespaceInfo nsInfo,  StartupOption startOpt ) throws IOException {
    if (startOpt == StartupOption.ROLLBACK)
      //2.1 doRollback
      doRollback(sd, nsInfo); // rollback if applicable
    sd.read();
    ...
    if (this.layoutVersion > FSConstants.LAYOUT_VERSION
        || this.cTime < nsInfo.getCTime()) {
      //2.2 doUpgrade
      doUpgrade(sd, nsInfo);  // upgrade
      return;
    }
  }

	// 2.1 doRollback
	// 1.current-----> removed.tmp
	// 2.previous---->current
	// 3.删除removed.tmp
	void doRollback(StorageDirectory sd, NamespaceInfo nsInfo)
			throws IOException {
		rename(curDir, tmpDir);
		rename(prevDir, curDir);
		deleteDir(tmpDir);
	}

	// 2.2 doUpgrade
	// 1.current-----> previous.tmp
	// 2.重建current:创建previous.tmp到current的硬链接,写入VERSION
	// 3.previous.tmp----->previous
	void doUpgrade(StorageDirectory sd, NamespaceInfo nsInfo)
			throws IOException {
		HardLink hardLink = new HardLink();
		rename(curDir, tmpDir);
		linkBlocks(tmpDir, curDir, this.getLayoutVersion(), hardLink);
		sd.write();
		rename(tmpDir, prevDir);
	}

	void finalizeUpgrade() throws IOException {
		for (Iterator<StorageDirectory> it = storageDirs.iterator(); it
				.hasNext();) {
			doFinalize(it.next());
		}
	}

	// 1.previous---->finalized.tmp
	// 2.删除finalized.tmp
	void doFinalize(StorageDirectory sd) throws IOException {
		rename(prevDir, tmpDir);
		new Daemon(new Runnable() {
			public void run() {
				deleteDir(tmpDir);
			}
		}).start();
	}

}

 

分享到:
评论

相关推荐

    hadoop-hdfs-client-2.9.1-API文档-中文版.zip

    赠送jar包:hadoop-hdfs-client-2.9.1.jar 赠送原API文档:hadoop-hdfs-client-2.9.1-javadoc.jar 赠送源代码:hadoop-hdfs-client-2.9.1-sources.jar 包含翻译后的API文档:hadoop-hdfs-client-2.9.1-javadoc-...

    hadoop-hdfs-client-2.9.1-API文档-中英对照版.zip

    赠送jar包:hadoop-hdfs-client-2.9.1.jar; 赠送原API文档:hadoop-hdfs-client-2.9.1-javadoc.jar; 赠送源代码:hadoop-hdfs-client-2.9.1-sources.jar; 赠送Maven依赖信息文件:hadoop-hdfs-client-2.9.1.pom;...

    hadoop-hdfs-2.7.3-API文档-中英对照版.zip

    赠送jar包:hadoop-hdfs-2.7.3.jar; 赠送原API文档:hadoop-hdfs-2.7.3-javadoc.jar; 赠送源代码:hadoop-hdfs-2.7.3-sources.jar; 赠送Maven依赖信息文件:hadoop-hdfs-2.7.3.pom; 包含翻译后的API文档:hadoop...

    hadoop-hdfs-2.6.5-API文档-中文版.zip

    赠送jar包:hadoop-hdfs-2.6.5.jar; 赠送原API文档:hadoop-hdfs-2.6.5-javadoc.jar; 赠送源代码:hadoop-hdfs-2.6.5-sources.jar; 赠送Maven依赖信息文件:hadoop-hdfs-2.6.5.pom; 包含翻译后的API文档:hadoop...

    hadoop-hdfs-2.5.1-API文档-中文版.zip

    赠送jar包:hadoop-hdfs-2.5.1.jar; 赠送原API文档:hadoop-hdfs-2.5.1-javadoc.jar; 赠送源代码:hadoop-hdfs-2.5.1-sources.jar; 赠送Maven依赖信息文件:hadoop-hdfs-2.5.1.pom; 包含翻译后的API文档:hadoop...

    hadoop-hdfs-2.7.3-API文档-中文版.zip

    赠送jar包:hadoop-hdfs-2.7.3.jar; 赠送原API文档:hadoop-hdfs-2.7.3-javadoc.jar; 赠送源代码:hadoop-hdfs-2.7.3-sources.jar; 赠送Maven依赖信息文件:hadoop-hdfs-2.7.3.pom; 包含翻译后的API文档:hadoop...

    hadoop-hdfs-2.9.1-API文档-中文版.zip

    赠送jar包:hadoop-hdfs-2.9.1.jar 赠送原API文档:hadoop-hdfs-2.9.1-javadoc.jar 赠送源代码:hadoop-hdfs-2.9.1-sources.jar 包含翻译后的API文档:hadoop-hdfs-2.9.1-javadoc-API文档-中文(简体)版.zip 对应...

    hadoop-hdfs-2.6.5-API文档-中英对照版.zip

    赠送jar包:hadoop-hdfs-2.6.5.jar; 赠送原API文档:hadoop-hdfs-2.6.5-javadoc.jar; 赠送源代码:hadoop-hdfs-2.6.5-sources.jar; 赠送Maven依赖信息文件:hadoop-hdfs-2.6.5.pom; 包含翻译后的API文档:hadoop...

    hadoop-hdfs-2.5.1-API文档-中英对照版.zip

    赠送jar包:hadoop-hdfs-2.5.1.jar; 赠送原API文档:hadoop-hdfs-2.5.1-javadoc.jar; 赠送源代码:hadoop-hdfs-2.5.1-sources.jar; 赠送Maven依赖信息文件:hadoop-hdfs-2.5.1.pom; 包含翻译后的API文档:hadoop...

    hadoop-hdfs-2.9.1-API文档-中英对照版.zip

    赠送jar包:hadoop-hdfs-2.9.1.jar; 赠送原API文档:hadoop-hdfs-2.9.1-javadoc.jar; 赠送源代码:hadoop-hdfs-2.9.1-sources.jar; 赠送Maven依赖信息文件:hadoop-hdfs-2.9.1.pom; 包含翻译后的API文档:hadoop...

    hadoop最新版本3.1.1全量jar包

    hadoop-auth-3.1.1.jar hadoop-hdfs-3.1.1.jar hadoop-mapreduce-client-hs-3.1.1.jar hadoop-yarn-client-3.1.1.jar hadoop-client-api-3.1.1.jar hadoop-hdfs-client-3.1.1.jar hadoop-mapreduce-client-jobclient...

    Hadoop 3.x(HDFS)----【HDFS 的 API 操作】---- 代码

    Hadoop 3.x(HDFS)----【HDFS 的 API 操作】---- 代码 Hadoop 3.x(HDFS)----【HDFS 的 API 操作】---- 代码 Hadoop 3.x(HDFS)----【HDFS 的 API 操作】---- 代码 Hadoop 3.x(HDFS)----【HDFS 的 API 操作】--...

    hadoop-hdfs-2.7.7.jar

    flume 想要将数据输出到hdfs,必须要有hadoop相关jar包。本资源是hadoop 2.7.7版本

    hadoop测试(1)---HDFS文件操作 完整测试代码

    hadoop测试(1)---HDFS文件操作 完整测试代码, 相关文章:http://www.cnblogs.com/yinpengxiang/archive/2011/07/03/2096605.html

    hadoop-common-2.6.0-bin-master.zip

    `hadoop-common-2.6.0-bin-master.zip` 是一个针对Hadoop 2.6.0版本的压缩包,特别适用于在Windows环境下进行本地开发和测试。这个版本的Hadoop包含了对Windows系统的优化,比如提供了`winutils.exe`,这是在Windows...

    hadoop插件apache-hadoop-3.1.0-winutils-master.zip

    标题中的"apache-hadoop-3.1.0-winutils-master.zip"是一个针对Windows用户的Hadoop工具包,它包含了运行Hadoop所需的特定于Windows的工具和配置。`winutils.exe`是这个工具包的关键组件,它是Hadoop在Windows上的一...

    hadoop-hdfs-2.4.1.jar

    hadoop-hdfs-2.4.1.jar

    hadoop-hdfs-test-0.21.0.jar

    hadoop-hdfs-test-0.21.0.jar

    hadoop-common-2.2.0-bin-master.zip

    hadoop-common-2.2.0-bin-master(包含windows端开发Hadoop和Spark需要的winutils.exe),Windows下IDEA开发Hadoop和Spark程序会报错,原因是因为如果本机操作系统是windows,在程序中使用了hadoop相关的东西,比如写入...

Global site tag (gtag.js) - Google Analytics