Hadoop 1.x的Shuffle源码分析之2

2024-06-11 09:58
文章标签 分析 源码 hadoop shuffle

本文主要是介绍Hadoop 1.x的Shuffle源码分析之2,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

ReduceTask类的内嵌类ReduceCopier的内嵌类MapOutputCopier的函数copyOutput是Shuffle里最重要的一环,它以http的方式,从远程主机取数据:创建临时文件名,然后用http读数据,再保存到内存文件系统或者本地文件系统。它读取远程文件的函数是getMapOutput。


getMapOutput函数如下:

private MapOutput getMapOutput(MapOutputLocation mapOutputLoc, Path filename, int reduce)throws IOException, InterruptedException {//建立http链接URL url = mapOutputLoc.getOutputLocation();HttpURLConnection connection = (HttpURLConnection)url.openConnection();//创建输入流InputStream input = setupSecureConnection(mapOutputLoc, connection);//检查连接姿势是否正确int rc = connection.getResponseCode();if (rc != HttpURLConnection.HTTP_OK) {throw new IOException("Got invalid response code " + rc + " from " + url +": " + connection.getResponseMessage());}//从http链接获取mapIdTaskAttemptID mapId = null;try {mapId =TaskAttemptID.forName(connection.getHeaderField(FROM_MAP_TASK));} catch (IllegalArgumentException ia) {LOG.warn("Invalid map id ", ia);return null;}
</pre><pre code_snippet_id="665348" snippet_file_name="blog_20150513_3_7696491" name="code" class="java">        //检查mapId是否一致TaskAttemptID expectedMapId = mapOutputLoc.getTaskAttemptId();if (!mapId.equals(expectedMapId)) {LOG.warn("data from wrong map:" + mapId +" arrived to reduce task " + reduce +", where as expected map output should be from " + expectedMapId);return null;}
        //如果数据有压缩,要获取压缩长度long decompressedLength = Long.parseLong(connection.getHeaderField(RAW_MAP_OUTPUT_LENGTH));  long compressedLength = Long.parseLong(connection.getHeaderField(MAP_OUTPUT_LENGTH));if (compressedLength < 0 || decompressedLength < 0) {LOG.warn(getName() + " invalid lengths in map output header: id: " +mapId + " compressed len: " + compressedLength +", decompressed len: " + decompressedLength);return null;}
        int forReduce =(int)Integer.parseInt(connection.getHeaderField(FOR_REDUCE_TASK));if (forReduce != reduce) {LOG.warn("data for the wrong reduce: " + forReduce +" with compressed len: " + compressedLength +", decompressed len: " + decompressedLength +" arrived to reduce task " + reduce);return null;}if (LOG.isDebugEnabled()) {LOG.debug("header: " + mapId + ", compressed len: " + compressedLength +", decompressed len: " + decompressedLength);}//We will put a file in memory if it meets certain criteria://1. The size of the (decompressed) file should be less than 25% of //    the total inmem fs//2. There is space available in the inmem fs// Check if this map-output can be saved in-memoryboolean shuffleInMemory = ramManager.canFitInMemory(decompressedLength); // ShuffleMapOutput mapOutput = null;if (shuffleInMemory) {if (LOG.isDebugEnabled()) {LOG.debug("Shuffling " + decompressedLength + " bytes (" + compressedLength + " raw bytes) " + "into RAM from " + mapOutputLoc.getTaskAttemptId());}//在内存做shuffle处理mapOutput = shuffleInMemory(mapOutputLoc, connection, input,(int)decompressedLength,(int)compressedLength);} else {if (LOG.isDebugEnabled()) {LOG.debug("Shuffling " + decompressedLength + " bytes (" + compressedLength + " raw bytes) " + "into Local-FS from " + mapOutputLoc.getTaskAttemptId());}//在本地做shuffle处理mapOutput = shuffleToDisk(mapOutputLoc, input, filename, compressedLength);}mapOutput.decompressedSize = decompressedLength;    return mapOutput;}


这篇关于Hadoop 1.x的Shuffle源码分析之2的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1050844

相关文章

Python实现无痛修改第三方库源码的方法详解

《Python实现无痛修改第三方库源码的方法详解》很多时候,我们下载的第三方库是不会有需求不满足的情况,但也有极少的情况,第三方库没有兼顾到需求,本文将介绍几个修改源码的操作,大家可以根据需求进行选择... 目录需求不符合模拟示例 1. 修改源文件2. 继承修改3. 猴子补丁4. 追踪局部变量需求不符合很

Spring事务中@Transactional注解不生效的原因分析与解决

《Spring事务中@Transactional注解不生效的原因分析与解决》在Spring框架中,@Transactional注解是管理数据库事务的核心方式,本文将深入分析事务自调用的底层原理,解释为... 目录1. 引言2. 事务自调用问题重现2.1 示例代码2.2 问题现象3. 为什么事务自调用会失效3

找不到Anaconda prompt终端的原因分析及解决方案

《找不到Anacondaprompt终端的原因分析及解决方案》因为anaconda还没有初始化,在安装anaconda的过程中,有一行是否要添加anaconda到菜单目录中,由于没有勾选,导致没有菜... 目录问题原因问http://www.chinasem.cn题解决安装了 Anaconda 却找不到 An

Spring定时任务只执行一次的原因分析与解决方案

《Spring定时任务只执行一次的原因分析与解决方案》在使用Spring的@Scheduled定时任务时,你是否遇到过任务只执行一次,后续不再触发的情况?这种情况可能由多种原因导致,如未启用调度、线程... 目录1. 问题背景2. Spring定时任务的基本用法3. 为什么定时任务只执行一次?3.1 未启用

C++ 各种map特点对比分析

《C++各种map特点对比分析》文章比较了C++中不同类型的map(如std::map,std::unordered_map,std::multimap,std::unordered_multima... 目录特点比较C++ 示例代码 ​​​​​​代码解释特点比较1. std::map底层实现:基于红黑

Spring、Spring Boot、Spring Cloud 的区别与联系分析

《Spring、SpringBoot、SpringCloud的区别与联系分析》Spring、SpringBoot和SpringCloud是Java开发中常用的框架,分别针对企业级应用开发、快速开... 目录1. Spring 框架2. Spring Boot3. Spring Cloud总结1. Sprin

Spring 中 BeanFactoryPostProcessor 的作用和示例源码分析

《Spring中BeanFactoryPostProcessor的作用和示例源码分析》Spring的BeanFactoryPostProcessor是容器初始化的扩展接口,允许在Bean实例化前... 目录一、概览1. 核心定位2. 核心功能详解3. 关键特性二、Spring 内置的 BeanFactory

MyBatis-Plus中Service接口的lambdaUpdate用法及实例分析

《MyBatis-Plus中Service接口的lambdaUpdate用法及实例分析》本文将详细讲解MyBatis-Plus中的lambdaUpdate用法,并提供丰富的案例来帮助读者更好地理解和应... 目录深入探索MyBATis-Plus中Service接口的lambdaUpdate用法及示例案例背景

MyBatis-Plus中静态工具Db的多种用法及实例分析

《MyBatis-Plus中静态工具Db的多种用法及实例分析》本文将详细讲解MyBatis-Plus中静态工具Db的各种用法,并结合具体案例进行演示和说明,具有很好的参考价值,希望对大家有所帮助,如有... 目录MyBATis-Plus中静态工具Db的多种用法及实例案例背景使用静态工具Db进行数据库操作插入

Go使用pprof进行CPU,内存和阻塞情况分析

《Go使用pprof进行CPU,内存和阻塞情况分析》Go语言提供了强大的pprof工具,用于分析CPU、内存、Goroutine阻塞等性能问题,帮助开发者优化程序,提高运行效率,下面我们就来深入了解下... 目录1. pprof 介绍2. 快速上手:启用 pprof3. CPU Profiling:分析 C