spark源码阅读之network(1)
spark将在1.6中替换掉akka,而采用netty实现整个集群的rpc的框架,netty的内存管理和NIO支持将有效的提高spark集群的网络传输能力,为了看懂这块代码,在网上找了两本书看《netty in action》和《netty权威指南》,结合了spark的源码既学习了netty也看完了spark netty的部分源码。该部分源码掺杂了太多netty的东西,看起来还是有点累的。
缓存模块
publicabstractclassManagedBuffer{/** Number of bytes of the data. */publicabstractlong size();/*** Exposes this buffer's data as an NIO ByteBuffer. Changing the position and limit of the* returned ByteBuffer should not affect the content of this buffer.*/// TODO: Deprecate this, usage may require expensive memory mapping or allocation.publicabstractByteBuffer nioByteBuffer()throwsIOException;/*** Exposes this buffer's data as an InputStream. The underlying implementation does not* necessarily check for the length of bytes read, so the caller is responsible for making sure* it does not go over the limit.*/publicabstractInputStream createInputStream()throwsIOException;/*** Increment the reference count by one if applicable.*/publicabstractManagedBuffer retain();/*** If applicable, decrement the reference count by one and deallocates the buffer if the* reference count reaches zero.*/publicabstractManagedBuffer release();/*** Convert the buffer into an Netty object, used to write the data out.*/publicabstractObject convertToNetty()throwsIOException;}
publicfinalclassFileSegmentManagedBufferextendsManagedBuffer{privatefinalTransportConf conf;privatefinalFile file;privatefinallong offset;privatefinallong length;publicFileSegmentManagedBuffer(TransportConf conf,File file,long offset,long length){this.conf = conf;this.file = file;this.offset = offset;this.length = length;}@Overridepubliclong size(){return length;}@OverridepublicByteBuffer nioByteBuffer()throwsIOException{FileChannel channel =null;try{channel =newRandomAccessFile(file,"r").getChannel();// Just copy the buffer if it's sufficiently small, as memory mapping has a high overhead.if(length < conf.memoryMapBytes()){ByteBuffer buf =ByteBuffer.allocate((int) length);channel.position(offset);while(buf.remaining()!=0){if(channel.read(buf)==-1){thrownewIOException(String.format("Reached EOF before filling buffer\n"+"offset=%s\nfile=%s\nbuf.remaining=%s",offset, file.getAbsoluteFile(), buf.remaining()));}}buf.flip();return buf;}else{return channel.map(FileChannel.MapMode.READ_ONLY, offset, length);}}catch(IOException e){try{if(channel !=null){long size = channel.size();thrownewIOException("Error in reading "+this+" (actual file length "+ size +")",e);}}catch(IOException ignored){// ignore}thrownewIOException("Error in opening "+this, e);}finally{JavaUtils.closeQuietly(channel);}}@OverridepublicInputStream createInputStream()throwsIOException{FileInputStream is =null;try{is =newFileInputStream(file);ByteStreams.skipFully(is, offset);returnnewLimitedInputStream(is, length);}catch(IOException e){try{if(is !=null){long size = file.length();thrownewIOException("Error in reading "+this+" (actual file length "+ size +")",e);}}catch(IOException ignored){// ignore}finally{JavaUtils.closeQuietly(is);}thrownewIOException("Error in opening "+this, e);}catch(RuntimeException e){JavaUtils.closeQuietly(is);throw e;}}@OverridepublicManagedBuffer retain(){returnthis;}@OverridepublicManagedBuffer release(){returnthis;}@OverridepublicObject convertToNetty()throwsIOException{if(conf.lazyFileDescriptor()){returnnewLazyFileRegion(file, offset, length);}else{FileChannel fileChannel =newFileInputStream(file).getChannel();returnnewDefaultFileRegion(fileChannel, offset, length);}}publicFile getFile(){return file;}publiclong getOffset(){return offset;}publiclong getLength(){return length;}@OverridepublicString toString(){returnObjects.toStringHelper(this).add("file", file).add("offset", offset).add("length", length).toString();}}
publicfinalclassNettyManagedBufferextendsManagedBuffer{privatefinalByteBuf buf;publicNettyManagedBuffer(ByteBuf buf){this.buf = buf;}@Overridepubliclong size(){return buf.readableBytes();}@OverridepublicByteBuffer nioByteBuffer()throwsIOException{return buf.nioBuffer();}@OverridepublicInputStream createInputStream()throwsIOException{returnnewByteBufInputStream(buf);}@OverridepublicManagedBuffer retain(){buf.retain();returnthis;}@OverridepublicManagedBuffer release(){buf.release();returnthis;}@OverridepublicObject convertToNetty()throwsIOException{return buf.duplicate();}@OverridepublicString toString(){returnObjects.toStringHelper(this).add("buf", buf).toString();}}
publicfinalclassNioManagedBufferextendsManagedBuffer{privatefinalByteBuffer buf;publicNioManagedBuffer(ByteBuffer buf){this.buf = buf;}@Overridepubliclong size(){return buf.remaining();}@OverridepublicByteBuffer nioByteBuffer()throwsIOException{return buf.duplicate();}@OverridepublicInputStream createInputStream()throwsIOException{returnnewByteBufInputStream(Unpooled.wrappedBuffer(buf));}@OverridepublicManagedBuffer retain(){returnthis;}@OverridepublicManagedBuffer release(){returnthis;}@OverridepublicObject convertToNetty()throwsIOException{returnUnpooled.wrappedBuffer(buf);}@OverridepublicString toString(){returnObjects.toStringHelper(this).add("buf", buf).toString();}}
spark源码阅读之network(1)的更多相关文章
- spark源码阅读之network(2)
在上节的解读中发现spark的源码中大量使用netty的buffer部分的api,该节将看到netty核心的一些api,比如channel: 在Netty里,Channel是通讯的载体(网络套接字或组 ...
- spark源码阅读之network(3)
TransportContext用来创建TransportServer和TransportclientFactory,同时使用TransportChannelHandler用来配置channel的pi ...
- Spark源码阅读之存储体系--存储体系概述与shuffle服务
一.概述 根据<深入理解Spark:核心思想与源码分析>一书,结合最新的spark源代码master分支进行源码阅读,对新版本的代码加上自己的一些理解,如有错误,希望指出. 1.块管理器B ...
- win7+idea+maven搭建spark源码阅读环境
1.参考. 利用IDEA工具编译Spark源码(1.60~2.20) https://blog.csdn.net/He11o_Liu/article/details/78739699 Maven编译打 ...
- spark源码阅读
根据spark2.2的编译顺序来确定源码阅读顺序,只阅读核心的基本部分. 1.common目录 ①Tags②Sketch③Networking④Shuffle Streaming Service⑤Un ...
- emacs+ensime+sbt打造spark源码阅读环境
欢迎转载,转载请注明出处,徽沪一郎. 概述 Scala越来越流行, Spark也愈来愈红火, 对spark的代码进行走读也成了一个很普遍的行为.不巧的是,当前java社区中很流行的ide如eclips ...
- spark源码阅读---Utils.getCallSite
1 作用 当该方法在spark内部代码中调用时,会返回当前调用spark代码的用户类的名称,以及其所调用的spark方法.所谓用户类,就是我们这些用户使用spark api的类. 2 内部实现 2.1 ...
- spark源码阅读--SparkContext启动过程
##SparkContext启动过程 基于spark 2.1.0 scala 2.11.8 spark源码的体系结构实在是很庞大,从使用spark-submit脚本提交任务,到向yarn申请容器,启 ...
- Spark源码阅读(1): Stage划分
Spark中job由action动作生成,那么stage是如何划分的呢?一般的解答是根据宽窄依赖划分.那么我们深入源码看看吧 一个action 例如count,会在多次runJob中传递,最终会到一个 ...
随机推荐
- 十三、python沉淀之路--文件操作
一.文件的读操作 例1 f = open('学习',encoding='utf-8') #首先要打开文件,不然直接读,是读不出来的 data = f.read() #read后的括号里不添加任何东西 ...
- tornado日志管理
默认数据格式 默认情况下,采用tornado的web框架运行起来之后,任何访问都会直接在控制台输出日志信息,格式如下: [I 160807 09:27:17 web:1971] 200 GET / ( ...
- 用hexo搭建自己的blog
一.工具准备: 1.1 安装node 作用:用来生成静态页面的 到Node.js官网下载相应平台的最新版本,一路安装即可. 1.2 安装Git 作用:把本地的hexo内容提交到github上去. 安装 ...
- hive字段原理--有删除一列想到的
hive删除一张表的字段不会动数据文件,只是修改了一下metadata表里面的表定义:所以会出现一种情况:就是这张表如果之前数据是满的(个格列都有数据),那么被删除的那列后数据都往前窜了一个,最后一个 ...
- MySQL删除数据库
drop命令用于删除数据库. drop命令格式:drop database <数据库名>; 例如,删除名为 xhkdb的数据库: mysql> drop database xhkdb ...
- 基于ThinkPHP的开发笔记3-登录功能(转)
1.前台登录用的form ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 <for ...
- python使用wxPython创建一个简单的文本编辑器。
ubuntu下通过'sudo apt-get install python-wxtools'下载wxPython.load和save函数用于加载和保存文件内容,button通过Bind函数绑定这两个函 ...
- Internet上的网络层
TCP/IP协议栈第三层是网络层,网络层的目的是实现两个系统之间的数据透明传送,具体功能包括寻址和路由选择.连接和建立.保持和终止等. TCP/IP协议给internet上的每台主机和路由分配一个地址 ...
- linux启动自动挂载分区和/etc/fstab简单修复
让后加的分区能够启动时自动挂载,需要把配置写入文件 /etc/fstab vi /etc/fstab UUID=3f5859e0-592f-42cd-b533-570422fb85be / ext ...
- python学习(十八) 程序打包
18.1 Distutils基础 18.2 打包 18.2.1 建立存档文件 18.2.2 创建Windows安装程序或RPM包 18.3 编译扩展 18.4 使用py2exe创建可执行程序