Hadoop源代码分析:HDFS读取和写入数据流控制(DataTransferThrottler类别)
DataTransferThrottler类别Datanode读取和写入数据时控制传输数据速率。这个类是线程安全的,它可以由多个线程共享。
用途是构建DataTransferThrottler对象,并设置期限period和带宽bandwidthPerSec,际读写前调用DataTransferThrottler.throttle()方法。假设I/O的速率相对给定的带宽太快,则该方法会将当前线程wait。
两个构造函数
- 双參构造函数,能够设置周期period和带宽bandwidthPerSec。
public DataTransferThrottler( long period,
long bandwidthPerSec)
- 单參构造函数,能够设置带宽bandwidthPerSec, 周期period默认被设置为500ms。
public DataTransferThrottler( long bandwidthPerSec)
重要属性
| period | 周期,单位毫秒 |
| periodExtension | 周期扩展时间。单位毫秒 |
| bytesPerPeriod | 一个周期内能够发送/接收的byte总数 |
| curPeriodStart | 当前周期開始时间,单位毫秒 |
| curReserve | 当前周期内还能够发送/接收的byte数 |
| bytesAlreadyUsed | 当前周期内已经使用的byte数 |
带宽bandWidthPerPerSec = bytesPerPeriod * period/1000
DataTransferThrottler.throttle()方法
该方法有两个入參:
- numOfBytes是自从上次throttle被调用到如今,须要发送/接收的byte数;
- canceler是一个外部取消器,能够用来检測是否取消流量控制。
DataTransferThrottler.throttle()方法会先将curReserve减去numOfBytes。接着运行例如以下逻辑。
- 假设curReserve大于0,则说明当前周期还有余量,throttle方法直接返回。
- 假设curReserve小于等于0,则说明当前周期已经没有余量,会运行以下逻辑。
>假设当前时间now小于当前周期结束时间curPeriodEnd,则wait到下一个周期
>假设当前时间now大于当前周期结束时间curPeriodEnd而且小于curPeriodStart+periodExtension。说明已经进入下一个周期而且throttle应该不是非常长时间没有使用,则将curReserve加上下一个周期能够传输的byte总数bytesPerPeriod,并将curPeriodStart设置到下一个周期的開始。
>假设当前时间now大于curPeriodStart+periodExtension。则可能Throttler非常长时间没有使用。则抛弃上一个周期。
DataTransferThrottler.throttle()方法源代码例如以下:
public synchronized void throttle(long numOfBytes , Canceler canceler) {
if ( numOfBytes <= 0 ) {
return;
}
//当前周期余量减去须要发送/接收的byte数numOfBytes
curReserve -= numOfBytes ;
bytesAlreadyUsed += numOfBytes;
//假设curReserve小于等于0,则说明当前周期已经没有余量
while ( curReserve <= 0) {
//假设传入了有效取消器canceler,而且取消器的取消状态isCancelled是true,则直接退出while循环
if (canceler != null && canceler.isCancelled()) {
return;
}
long now = monotonicNow();
//计算当前周期结束时间。并存放在curPeriodEnd变量中
long curPeriodEnd = curPeriodStart + period ;
if ( now < curPeriodEnd ) {
//等待下一个周期,这样curReserve就能够添加
try {
wait( curPeriodEnd - now );
} catch (InterruptedException e) {
//终止throttle, 而且重置interrupted状态来确保在调用栈中其他interrupt处理器能够正确运行
Thread. currentThread().interrupt();
break;
}
}
//假设当前时间now比当前结束时间curPeriodEnd晚,而且小于curPeriodStart+periodExtension(周期3倍时间),则进入下一个周期
//并添加bytesPerPeriod到curReserve
else if ( now < (curPeriodStart + periodExtension)) {
curPeriodStart = curPeriodEnd;
curReserve += bytesPerPeriod ;
}
//假设当前时间now大于curPeriodStart+periodExtension,则可能Throttler非常长时间没有使用。抛弃上一个周期
else {
curPeriodStart = now;
curReserve = bytesPerPeriod - bytesAlreadyUsed;
}
}
bytesAlreadyUsed -= numOfBytes;
}
总结
DataTransferThrottler类能够控制传输数据在一段时期内的平均速率。但可能在一个周期结束时瞬时速率会失控。
參考资料
hadoop-release-2.5.0源代码
转载请附上原博客地址:http://blog.csdn.net/jeff_fangji/article/details/44258827
版权声明:本文博客原创文章,博客,未经同意,不得转载。
Hadoop源代码分析:HDFS读取和写入数据流控制(DataTransferThrottler类别)的更多相关文章
- Hadoop源代码分析
http://wenku.baidu.com/link?url=R-QoZXhc918qoO0BX6eXI9_uPU75whF62vFFUBIR-7c5XAYUVxDRX5Rs6QZR9hrBnUdM ...
- Hadoop源代码分析(完整版)
Hadoop源代码分析(一) 关键字: 分布式云计算 Google的核心竞争技术是它的计算平台.Google的大牛们用了下面5篇文章,介绍了它们的计算设施. GoogleCluster:http:// ...
- Hadoop集群-HDFS集群中大数据运维常用的命令总结
Hadoop集群-HDFS集群中大数据运维常用的命令总结 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客会简单涉及到滚动编辑,融合镜像文件,目录的空间配额等运维操作简介.话 ...
- nodeks —— fs模块 —— 从流中 读取和写入数据
Fs流读取和写入数据 使用文件流来读取大文件不会卡顿 1, 从流中读取数据 var fs = require("fs"); var data = ''; var count = 0 ...
- 转载-python学习笔记之输入输出功能读取和写入数据
读取.写入和 Python 在 “探索 Python” 系列以前的文章中,学习了基本的 Python 数据类型和一些容器数据类型,例如tuple.string 和 list.其他文章讨论了 Pytho ...
- openVswitch(OVS)源代码分析之工作流程(数据包处理)
上篇分析到数据包的收发,这篇开始着手分析数据包的处理问题.在openVswitch中数据包的处理是其核心技术,该技术分为三部分来实现:第一.根据skb数据包提取相关信息封装成key值:第二.根据提取到 ...
- 用OpenPyXL处理Excel表格 - 向sheet读取、写入数据
假设一个名叫"模板"的excel表格里有四个sheet,名字分别是['平台', '制冷', '洗衣机', '空调'] 1.读取 from openpyxl import load_ ...
- Hadoop源代码分析【IO专题】
由于Hadoop的MapReduce和HDFS都有通信的需求,需要对通信的对象进行序列化.Hadoop并没有采用Java的序列化(因为Java序列化比较复杂,且不能深度控制),而是引入了它自己的系统. ...
- HDFS 读取、写入、遍历文件夹获取文件全路径、append
版权声明:本文为博主原创文章,未经博主同意不得转载.安金龙 的博客. https://blog.csdn.net/smile0198/article/details/37573081 1.从HDFS中 ...
随机推荐
- Genymotion加入模拟器时报“Unable to create virtual device,Server returned HTTP status code 0”
今天也遇到这个问题,算是对这个文章的一点补充 打开图中这个文件 C:\Users\xxx\AppData\Local\Genymobile 搜索 [downloadFile] 找到这个一串URL ht ...
- Angular 2 HostListener & HostBinding
原文 https://www.jianshu.com/p/20c2d60802f7 大纲 1.宿主元素(Host Element) 2.HostListener 3.HostListenerDecor ...
- [ES7] Await multi promises sequentially or concurrently
Somtime 'async await' can have a bad effect on code proferemence. Let's take a look the below exampl ...
- Java fork join ForkJoinPool 用法例子
本例是把一个大的数组求和的计算的大任务分解到在小范围内求和的小任务,然后把这些小任务之和加起来就是所求之结果. 技术:JDK8.0, Javafork-join模式下的RecursiveTask技术, ...
- Use Word 2010's Navigation Pane to quickly reorganize documents
Use Word 2010's Navigation Pane to quickly reorganize documents http://www.techrepublic.com/blog/mic ...
- 关于pptpd log日志文件的配置
如何开启pptpd默认日志记录功能. 修改/etc/ppp/options.pptpd中的nologfd,默认没有开,把nologfd注释掉,然后添加 logfile /var/log/pptpd.l ...
- 基于 Android NDK 的学习之旅-----环境搭建
工欲善其事 必先利其器 , 下面介绍下 Eclipse SDK NDK Cygwin CDT 集成开发环境的搭建. 1.Android 开发环境搭建 Android开发环境搭建不是重点,相信看此文章的 ...
- C#-numericUpDown-数字选择---ShinePans
program.cs using System; using System.Collections.Generic; using System.Linq; using System.Windows.F ...
- ios9 xcode7以后编译需要进行的几项设置
http://blog.csdn.net/hero82748274/article/details/48629461 1.库后缀变了:.dylib->tbd libsqlite3.0.dylib ...
- 高CPU、数据库无法读写
高CPU.数据库无法读写的真凶 有兴趣的同学可以参考如下系列文章,都是针对dump分析的实战和总结: Windbg DUMP分析(原创汇总) http://www.cnblogs.com/Love ...