分享

hadoop2提交到Yarn: Mapreduce执行过程reduce分析3

howtodown 2014-10-2 17:58:39 发表于 连载型 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 3 34384
本帖最后由 howtodown 于 2014-10-2 18:02 编辑

问题导读:
1.Reduce类主要有哪三个步骤?
2.Reduce的Copy都包含什么过程?
3.Sort主要做了哪些工作?






4.4 Reduce类4.4.1 Reduce介绍
整完了Map,接下来就是Reduce了。YarnChild.main()—>ReduceTask.run()。ReduceTask.run方法开始和MapTask类似,包括initialize()初始化,根据情况看是否调用runJobCleanupTask(),runTaskCleanupTask()等。之后进入正式的工作,主要有这么三个步骤:Copy、Sort、Reduce。
4.4.2 Copy
Copy就是从执行各个Map任务的节点获取map的输出文件。这是由ReduceTask.ReduceCopier 类来负责。ReduceCopier对象负责将Map函数的输出拷贝至Reduce所在机器。如果大小超过一定阈值就写到磁盘,否则放入内存,在远程拷贝数据的同时,Reduce Task启动了两个后台线程对内存和磁盘上的文件进行合并,防止内存使用过多和磁盘文件过多。
Step1:
    首先在ReduceTask的run方法中,通过如下配置来mapreduce.job.reduce.shuffle.consumer.plugin.class装配shuffle的plugin。默认的实现是Shuffle类:
  1.      Class<? extends ShuffleConsumerPlugin> clazz = job.getClass(MRConfig.SHUFFLE_CONSUMER_PLUGIN, Shuffle.class, ShuffleConsumerPlugin.class);
  2.      shuffleConsumerPlugin = ReflectionUtils.newInstance(clazz, job);
  3.      LOG.info("Using ShuffleConsumerPlugin: " + shuffleConsumerPlugin);
复制代码

Step2:
    初始化上述的plugin后,执行其run方法,得到RawKeyValueIterator的实例。
run方法的执行步骤如下:
Step2.1:
    量化Reduce的事件数目:
  1. int eventsPerReducer = Math.max(MIN_EVENTS_TO_FETCH, MAX_RPC_OUTSTANDING_EVENTS / jobConf.getNumReduceTasks());
  2.      int maxEventsToFetch = Math.min(MAX_EVENTS_TO_FETCH, eventsPerReducer);
复制代码
Step2.2:
生成map的完成状态获取线程,并启动此线程:
  1. final EventFetcher<K,V> eventFetcher = new EventFetcher<K,V>(reduceId, umbilical, scheduler, this, maxEventsToFetch);
  2.   eventFetcher.start();
复制代码

获取已经完成的Map信息,如Map的host、mapId等放入ShuffleSchedulerImpl中的Set<MapHost>中便于下面进行数据的拷贝传输。
  1.   URI u = getBaseURI(reduceId, event.getTaskTrackerHttp());
  2.        addKnownMapOutput(u.getHost() + ":" + u.getPort(),
  3.            u.toString(),
  4.            event.getTaskAttemptId());
  5.        maxMapRuntime = Math.max(maxMapRuntime, event.getTaskRunTime());
复制代码

Step2.3:
    在Shuffle类中启动初始化Fetcher线程组,并启动:
  1. boolean isLocal = localMapFiles != null;
  2.     final int numFetchers = isLocal ? 1 :
  3.       jobConf.getInt(MRJobConfig.SHUFFLE_PARALLEL_COPIES, 5);
  4.     Fetcher<K,V>[] fetchers = new Fetcher[numFetchers];
  5.     if (isLocal) {
  6.       fetchers[0] = new LocalFetcher<K, V>(jobConf, reduceId, scheduler,
  7.           merger, reporter, metrics, this, reduceTask.getShuffleSecret(),
  8.           localMapFiles);
  9.       fetchers[0].start();
  10.     } else {
  11.       for (int i=0; i < numFetchers; ++i) {
  12.         fetchers[i] = new Fetcher<K,V>(jobConf, reduceId, scheduler, merger,
  13.                                        reporter, metrics, this,
  14.                                        reduceTask.getShuffleSecret());
  15.         fetchers[i].start();
  16.       }
  17.     }
复制代码

线程的run方法就是进行数据的远程拷贝:
  1. try {
  2.           // If merge is on, block
  3.           merger.waitForResource();
  4.           // Get a host to shuffle from
  5.           host = scheduler.getHost();
  6.           metrics.threadBusy();
  7.           // Shuffle
  8.           copyFromHost(host);
  9.         } finally {
  10.           if (host != null) {
  11.             scheduler.freeHost(host);
  12.             metrics.threadFree();  
  13.           }
  14.         }
复制代码

Step2.4:
来看下这个copyFromHost方法。主要是就是使用HttpURLConnection,实现远程数据的传输。
建立连接之后,从接收到的Stream流中读取数据。每次读取一个map文件。
  1. TaskAttemptID[] failedTasks = null;
  2.       while (!remaining.isEmpty() && failedTasks == null) {
  3.         failedTasks = copyMapOutput(host, input, remaining);
  4.       }
复制代码
上面的copyMapOutput方法中,每次读取一个mapid,根据MergeManagerImpl中的reserve函数,检查map的输出是否超过了mapreduce.reduce.memory.totalbytes配置的大小,此配置的默认值
是当前Runtime的maxMemory*mapreduce.reduce.shuffle.input.buffer.percent配置的值,Buffer.percent的默认值为0.90。
如果mapoutput超过了此配置的大小时,生成一个OnDiskMapOutput实例。在接下来的操作中,map的输出写入到local临时文件中。
如果没有超过此大小,生成一个InMemoryMapOutput实例。在接下来操作中,直接把map输出写入到内存。
最后,执行ShuffleScheduler.copySucceeded完成文件的copy,调用mapout.commit函数,更新状态或者触发merge操作。
Step2.5:
    等待上面所有的拷贝完成之后,关闭相关的线程。
  1. eventFetcher.shutDown();   
  2.     // Stop the map-output fetcher threads
  3.     for (Fetcher<K,V> fetcher : fetchers) {
  4.       fetcher.shutDown();
  5.     }   
  6.     // stop the scheduler
  7.     scheduler.close();
  8.     copyPhase.complete(); // copy is already complete
  9.     taskStatus.setPhase(TaskStatus.Phase.SORT);
  10.     reduceTask.statusUpdate(umbilical);
复制代码

Step2.6:
执行最终的merge操作,由Shuffle中的MergeManager完成:
  1. public RawKeyValueIterator close() throws Throwable {
  2.     // Wait for on-going merges to complete
  3.     if (memToMemMerger != null) {
  4.       memToMemMerger.close();
  5.     }
  6.     inMemoryMerger.close();
  7.     onDiskMerger.close();
  8.    
  9.     List<InMemoryMapOutput<K, V>> memory =
  10.       new ArrayList<InMemoryMapOutput<K, V>>(inMemoryMergedMapOutputs);
  11.     inMemoryMergedMapOutputs.clear();
  12.     memory.addAll(inMemoryMapOutputs);
  13.     inMemoryMapOutputs.clear();
  14.     List<CompressAwarePath> disk = new ArrayList<CompressAwarePath>(onDiskMapOutputs);
  15.     onDiskMapOutputs.clear();
  16.     return finalMerge(jobConf, rfs, memory, disk);
  17.   }
复制代码

Step3:
释放资源。
  1. mapOutputFilesOnDisk.clear();
复制代码

  Copy完毕。
4.4.3 Sort
    Sort(其实相当于合并)就相当于排序工作的一个延续,它会在所有的文件都拷贝完毕后进行。使用工具类Merger归并所有的文件。经过此过程后,会产生一个合并了所有(所有并不准确)Map任务输出文件的新文件,而那些从其他各个服务器搞过来的 Map任务输出文件会删除。根据hadoop是否分布式来决定调用哪种排序方式。
    在上面的4.3.2节中的Step2.4结束之后就会触发此操作。
4.4.4 Reduce
    经过上面的步骤之后,回到ReduceTask中的run方法继续往下执行,调用runNewReducer。创建reducer:
  1. org.apache.hadoop.mapreduce.Reducer<INKEY,INVALUE,OUTKEY,OUTVALUE> reducer =
  2.      (org.apache.hadoop.mapreduce.Reducer<INKEY,INVALUE,OUTKEY,OUTVALUE>)
  3.         ReflectionUtils.newInstance(taskContext.getReducerClass(), job);
复制代码

并执行其run方法,此run方法就是我们的org.apache.hadoop.mapreduce.Reducer中的run方法。
  1. public void run(Context context) throws IOException, InterruptedException {
  2.     setup(context);
  3.     try {
  4.       while (context.nextKey()) {
  5.         reduce(context.getCurrentKey(), context.getValues(), context);
  6.         // If a back up store is used, reset it
  7.         Iterator<VALUEIN> iter = context.getValues().iterator();
  8.         if(iter instanceof ReduceContext.ValueIterator) {
  9.           ((ReduceContext.ValueIterator<VALUEIN>)iter).resetBackupStore();      
  10.         }
  11.       }
  12.     } finally {
  13.       cleanup(context);
  14.     }
  15.   }
  16. }
复制代码

while的循环条件是ReduceContext.nextKey()为真,这个方法就在ReduceContext中实现的,这个方法的目的就是处理下一个唯一的key,因为reduce方法的输入数据是分组的,所以每次都会处理一个key及这个key对应的所有value,又因为已经将所有的Map Task的输出拷贝过来而且做了排序,所以key相同的KV对都是挨着的。
    nextKey方法中,又会调用nextKeyValue方法来尝试去获取下一个key值,并且如果没数据了就会返回false,如果还有数据就返回true。防止获取重复的数据就在这里做的处理。
接下来就是调用用户自定义的reduce方法了。
  1. public void reduce(Text key, Iterable<IntWritable> values,
  2.                        Context context
  3.                        ) throws IOException, InterruptedException {
  4.       int sum = 0;
  5.       for (IntWritable val : values) {
  6.         sum += val.get();
  7.       }
  8.       result.set(sum);
  9.       context.write(key, result);
  10.     }
复制代码



相关文章:
hadoop2提交到Yarn: Mapreduce执行过程分析1

hadoop2提交到Yarn: Mapreduce执行过程reduce分析3



已有(3)人评论

跳转到指定楼层
admln 发表于 2015-3-3 16:37:47
中间少了最重要的yarn的执行流程
回复

使用道具 举报

xuanxufeng 发表于 2015-3-3 16:53:52
admln 发表于 2015-3-3 16:37
中间少了最重要的yarn的执行流程

大神缺的什么,能否给讲讲,学习下
回复

使用道具 举报

zmer 发表于 2015-9-13 15:53:54
nodemanager以及container这部分比较抽象,能讲下不
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条