锘??xml version="1.0" encoding="utf-8" standalone="yes"?>BlogJava-涓婂杽鑻按http://www.8634070.com/DLevin/In general the OO style is to use a lot of little objects with a lot of little methods that give us a lot of plug points for overriding and variation. To do is to be -Nietzsche, To bei is to do -Kant, Do be do be do -Sinatrazh-cnTue, 07 May 2019 08:52:05 GMTTue, 07 May 2019 08:52:05 GMT60浣跨敤NamedParameterJdbcTemplate閬囧埌鏃犳硶浣跨敤鐨勫潙http://www.8634070.com/DLevin/archive/2015/11/11/428149.htmlDLevinDLevinWed, 11 Nov 2015 10:46:00 GMThttp://www.8634070.com/DLevin/archive/2015/11/11/428149.htmlhttp://www.8634070.com/DLevin/comments/428149.htmlhttp://www.8634070.com/DLevin/archive/2015/11/11/428149.html#Feedback0http://www.8634070.com/DLevin/comments/commentRss/428149.htmlhttp://www.8634070.com/DLevin/services/trackbacks/428149.html鏈杩?#28051;鐩村湪鎹紦HBase鐨勯鐩紝涔嬪墠鍐欎簡涓浜涗唬鐮佷粠鏁?#37817;?#25652;撳姞杞?#37825;?#37817;埌HBase?#32029;?#22669;鏈?#37928;勪唬鐮?#38318;借窇寰楀濂藉湴?#32029;?#21159;鑰?#28000;婂?#30398;璇曠潃鎹簡涓涓?#37825;?#37817;?#25652;擄紝?#27688;?#31367;涓?#38315;氫簡銆?#38315;?#26473;?#37825;?#37817;?#23480;叿?#32029;?#24434;浠彂鐜?#26473;?#37818;病鏈夐棶棰橈紝鑰?#28051;?#37832;?#38318;?#37714;?#38315;昏緫寰堥鍒?#38315;?#26473;囦簡?#32029;?#21159;鑰?#37832;?#28051;浜涘氨鏄崱涓讳簡?#32029;閫?#26473;jstack鎵撳嵃鍑烘潵鐨勪俊鎭彲浠?#37813;惧埌杩?#37837;?#37928;勫爢鏍?#38171;?/span>
"runner{object-loader#292}-objecthandler" #323 prio=5 os_prio=0 tid=0x00002aaadc5ec800 nid=0x7f62 in Object.wait() [0x0000000056ce4000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        at java.lang.Object.wait(Object.java:502)
        at org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:1104)
        - locked <0x00000007736013e8> (a org.apache.commons.pool.impl.GenericObjectPool$Latch)
        at org.apache.commons.dbcp.PoolingDataSource.getConnection(PoolingDataSource.java:106)
        at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
        at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111)
        at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77)
?#22669;浠紑濮嬫垜鎬鐤?#37828;?#26473;?#37818;?#37928;勯棶棰橈紝?#31904;?#32137;涓?#28052;?#37813;惧埌?#31777;涓涓被鍨?#37928;?#37916;拌薄?#32029;鏈?#27996;烘鐤?#37828;DBPC鐨?#28051;涓猙ug瀵艰嚧姝婚攣锛歨ttp://stackoverflow.com/questions/5714511/deadlock-issue-in-dbcp-deployed-on-tomcat?#32029;?#22669;浠垜鍗囩骇?#31777;DBCP鐗?#37832;?.4?#32029;?#21159;鑰屽拰杩欎汉涓鏍?#37928;?#32513;撴灉?#32029;鍗囩骇DBCP鐗?#37832;?#39582;舵病鏈夎鍐抽棶棰?#37510;傜畝鍗?#37928;?#37930;婦BCP鐨勪唬鐮侊紝閮藉紑濮嬫鐤?#37828;?#28051;?#37828;?#37733;?#28051;烘病鏈塖pring JdbcTemplate?#30149;鏈?#23005;?#32429;?#37928;勬妸Connection杩?#37733;?#37733;炲幓寮曡捣娉勬紡?#31777;?#32029;?#21159;鑰?#28052;?#37832;夌偣鎰熻涓?#28598;彲?#20824;?#32029;鍥?#28051;?#26473;?#23048;典唬鐮佸湪鍏?#28000;?#37825;?#37817;?#25652;?#38318;借窇寰楀濂藉湴?#32029;浣?#37828;?#37812;戜滑鐨?#37825;?#37817;?#25652;?#37911;?#37832;?#38318;芥槸涓?#22183;鐨勶紝?#21159;鑰屽叾浠栭厤缃?#28051;?#28052;熻鍋囪涓?#22183;?#31777;锛堣蹇界暐鐨?#28051;涓噸瑕?#37928;勭偣锛?#37510;?br />
鍚庢潵?#32017;濮嬭皟?#21412;缃紝鍑?#28751;?#26473;?#37818;?#37825;帮紝鍑?#28751;?#32510;跨鏁帮紝缁?#26473;囧悇绉?#32513;勫?#22572;紝?#24386;鐜?#35120;撴妸DB璇?#37928;刡atch闄嶅埌1鐨?#37827;?#37706;欏氨?#24434;浠work?#31777;?#32029;闈炲父璇?#23534;?#37928;?#28051;涓棶棰?#37510;備粠鏁?#37817;?#23480;叿涓煡?#22476;?#32029;濡傛灉?#25956;batch?#32029;寰楀埌鐨凷QL鏄?
SELECT <column>, <column> FROM <table> where iid in (@p0, @p1)
濡傛灉鏄痓atch鏄?鐨?#29831;?#38171;?br />
SELECT <column>, <column> FROM <table> where iid in (@p0)
杩?#23048;SQL璇?#37721;?#37828;?#26473;?#28052;?#27996;?#37922;?#37928;?#38171;?br />
DataSource dataSource = ....
this.jdbc = new NamedParameterJdbcTemplate(dataSource);
...
MapSqlParameterSource parameters = new MapSqlParameterSource();
parameters.addValue("params", paramsMap.keySet());
jdbc.query("SELECT <columns> FROM <table> where <column> in (:params)";, parameters, new ResultSetExtractor<Void>() {
....
})

濡傛灉鏄?#28051;涓猙atch鐨?#29831;濓紝?#28266;jstack鍫?#37837;?#28051;彲浠?#37930;嬪埌?#30048;涓鐩村湪绛?#37825;?#37817;?#25652;?#37928;?#26473;?#37733;?#32513;撴灉锛?br />
"runner{object-loader#16}-objecthandler" #47 prio=5 os_prio=0 tid=0x0000000006ddd800 nid=0x2694 runnable [0x0000000045434000]
   java.lang.Thread.State: RUNNABLE
        at java.net.SocketInputStream.socketRead0(Native Method)
        at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
        at java.net.SocketInputStream.read(SocketInputStream.java:170)
        at java.net.SocketInputStream.read(SocketInputStream.java:141)
        at com.sybase.jdbc3.timedio.RawDbio.reallyRead(Unknown Source)
        at com.sybase.jdbc3.timedio.Dbio.doRead(Unknown Source)
        at com.sybase.jdbc3.timedio.InStreamMgr.a(Unknown Source)
        at com.sybase.jdbc3.timedio.InStreamMgr.doRead(Unknown Source)
        at com.sybase.jdbc3.tds.TdsProtocolContext.getChunk(Unknown Source)
杩?#28052;熻閲婁簡绗?#28051;涓?#37739;?#37837;?#28051;鐩村仠?#28266;borrowObject(getConnection)鐨勯?#33333;?#30857;紝鍥?#28051;?#28052;嬪墠?#22669;鏈?#37928;凜onnection閮藉湪鏁?#37817;?#25652;撳牭浣忔病鏈?#26473;?#37733;烇紝?#22669;浠?#26473;?#28051;?#32510;跨?#23338;?#23942;?#32254;onnection鐨?#37827;?#37706;欒秴杩囦簡?#22428;璁?#32515;?#37928;?#37832;澶Connection鏁帮紝?#22669;浠氨绛夌潃鎷?#28051;嶅埌Connection銆?br />
?#28266;鍚庢潵?#29025;?#31777;涓涓?#28051;嶅悓鏁?#37817;?#25652;?#37928;凧DBC Driver?#20426;鎭?sp_version):
jConnect (TM) for JDBC(TM)/7.07 ESD #4 (Build 26793)/P/EBF20302/JDK 1.6.0/jdbcmain/OPT/Thu Jul  5 22:08:44 PDT 2012
jConnect (TM) for JDBC(TM)/1000/Wed Mar 11 05:01:24 2015 PDT

涔熷氨鏄?#29831;?#26473;?#32457;嶇敤?#30838;鏄?#37733;?#28051;?#37827;?#37928;凧DBC Driver瀵筃amedParameterJdbcTemplate涓嶅?#23677;杽寮曡捣鐨勶紝杩?#28051;潙鑺变簡?#22428;涓鏁村鐨?#37827;?#38338;?#37510;?#37510;?#37510;?#37510;?img src ="http://www.8634070.com/DLevin/aggbug/428149.html" width = "1" height = "1" />

]]>
SSTable璇?#29785;?/title><link>http://www.8634070.com/DLevin/archive/2015/09/25/427481.html</link><dc:creator>DLevin</dc:creator><author>DLevin</author><pubDate>Thu, 24 Sep 2015 17:35:00 GMT</pubDate><guid>http://www.8634070.com/DLevin/archive/2015/09/25/427481.html</guid><wfw:comment>http://www.8634070.com/DLevin/comments/427481.html</wfw:comment><comments>http://www.8634070.com/DLevin/archive/2015/09/25/427481.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.8634070.com/DLevin/comments/commentRss/427481.html</wfw:commentRss><trackback:ping>http://www.8634070.com/DLevin/services/trackbacks/427481.html</trackback:ping><description><![CDATA[<h2> ?#22688;璁?/h2>鍑?#39582;村墠?#28266;璇籊oogle鐨凚igTable璁烘枃鐨?#37827;?#37706;欙紝褰?#37827;?#39582;舵病鏈夌悊瑙?#29825;烘枃閲岄潰琛?#26440;?#37928;?#37804;濇兂?#32029;鍥?#38000;屽洬鍥?#37722;炴?#32029;骞舵病鏈?#23049;?#37808;忓埌SSTable鐨勬蹇?#37510;?#37712;嶅?#24226;潵?#32017;濮嬪叧娉HBase鐨勮璁?#37724;屾簮鐮佸悗?#32029;?#32017;濮嬪BigTable浼?#38315;?#37928;?#37804;濇兂鎱?#37809;?#37928;勬竻鏅拌捣?#28533;?#32029;浣?#37828;?#37733;?#28051;?#27996;嬫儏澶?#28598;氾紝?#30149;鏈夊畨鎺?#37713;?#37827;?#38338;撮噸璇籅igTable鐨勮?#28888;枃銆傚湪椤?#37929;噷?#32029;?#22428;鍥?#28051;鸿嚜宸卞湪瀛HBase?#32029;?#32017;濮?#28051;?#37818;HBase?#32029;鑰屽彟涓涓?#37722;?#27996;嬪垯鍥?#28051;?#28725;笴assandra姣?#26440;?#37808;?#37712;掞紝鍥?#38000;?#28000;?#28051;昏?#20344;叧娉Cassandra鐨勮璁紝涓?#26473;?#37812;戜滑涓?#28051;?#27996;?#37707;?#28751;?#38318;?#28028;氳璁?#28051;涓嬫妧鏈?#37510;?#29825;?#29825;?#37928;勫悇绉?#29785;傜偣鍜屽績寰楋紝?#21159;鍚?#28000;?#37707;剁劧鐨?#29831;翠簡涓鍙?#38171;欳assandra?#25328;HBase閮?#38322;囩敤SSTable鏍?#23534;忓?#27194;偍?#32029;?#21159;鍚庢垜鏈兘鐨勯棶?#31777;涓鍙?#38171;?#28000;涔?#37828;SSTable锛?#28000;?#39582;舵病鏈?#37733;?#32475;旓紝?#24434;?#20824;涔?#28051;?#37828;偅涔?#37713;?#37721;?#38003;借?#23384;竻妤?#37928;勶紝鎴?#38000;?#28000;?#38007;?#23480;?#28052;熸病鏈夊皾璇?#37928;勫?#23130;棶杩?#38007;?#23480;?#26473;?#28051;棶棰?#37510;傜劧鑰?#26473;?#28051;棶棰?#37832;?#38892;?#37719;?#28051;鐩村?#29256;?#25198;潃?#22428;?#32029;鍥?#38000;岃?#20346;潃鐜板湪鏈?#28051;浜?#37827;?#38338;存繁鍏?#28699;?#28052;燞Base?#25328;Cassandra鐩?#37711;?#29825;?#29825;?#37928;?#37827;?#37706;?#37711;堟妸杩?#28051;棶棰?#23534;勬竻妤氫簡銆?br /><h2>SSTable鐨勫畾涔?/h2>瑕?#29785;噴杩?#28051;?#37832;?#29831;?#37928;?#37930;?#23005;?#37722;?#28052;夛紝鏈濂?#37928;勬?#35268;硶?#27688;鏄粠?#30048;鐨勫嚭澶?#37813;?#32475;旀?#22572;紝?#22669;浠?#38322;嶆?#25198;炕?#32017;BigTable鐨勮?#28888;枃銆傚湪杩欑?#22250;?#28888;枃涓紝鏈鍒?#28725;筍STable鏄?#26473;?#28052;堟弿杩?#37928;?#38171;堢涓夐鏈?#37724;岀鍥?#26916;?#37714;?#38171;?#38171;?br /><fieldset><legend>SSTable</legend> <div class="page" title="Page 3"> <div class="layoutArea"> <div class="column"> <p><span style="font-size: 10.000000pt; font-family: 'Times'">The Google </span><span style="font-size: 10.000000pt; font-family: 'Times'; font-style: italic"><strong>SSTable</strong> </span><span style="font-size: 10.000000pt; font-family: 'Times'">file format is used internally to store Bigtable data. An <strong>SSTable</strong> provides a <strong>persistent, ordered immutable map from keys to values, where both keys and values are arbitrary byte strings.</strong> Operations are provided to look up the value associated with a specified</span><span style="font-size: 10.000000pt; font-family: 'Times'"> key, and to iterate over all key/value pairs in a specified key range. Internally, each SSTable contains a sequence of blocks (typically each block is 64KB in size, but this is configurable). A block index (stored at the end of the SSTable) is used to locate blocks; the index is loaded into memory when the SSTable is opened. A lookup can be performed with a single disk seek: we first find the appropriate block by performing a binary search in the in-memory index, and then reading the appropriate block from disk. Optionally, an SSTable can be completely mapped into memory, which allows us to perform lookups and scans without touching disk. </span> </p> </div></div></div></fieldset><span style="font-family: Comic Sans MS; font-size: 10pt;">?#30045;鍗?#37928;勯潪鐩?#29831;?#38171;?/span><span style="font-family: Comic Sans MS; font-size: 10pt;"><br /></span><span style="font-family: Comic Sans MS; font-size: 10pt;">SSTable鏄疊igtable鍐?#38318;敤浜?#37825;?#37817;?#37928;勬枃浠?#37837;?#23534;忥紝?#30048;鐨?#37837;?#23534;?#28051;烘枃浠?#37832;?#38892;氨鏄?#28051;涓?#37818;?#25652;?#37928;?#37510;?#28051;嶅彲鍙?#37928;?#37510;佹寔涔?#37928;凨ey/Value瀵筂ap?#32029;鍏?#28051;璌ey?#25328;value閮藉彲浠?#37828;?#28000;?#37808;?#37928;刡yte瀛?#32471;?#28051;?#37510;?#28003;跨敤Key?#28533;?#29025;鎵綱alue?#32029;鎴?#38315;?#26473;囩?#27407;畾Key鑼?#37733;?#38316;嶅?#21976;墍鏈?#37928;凨ey/Value瀵?#37510;傛瘡涓猄STable鍖?#37722;?#28051;绯?#37714;?#37928;凚lock锛?#28051;鑸珺lock澶?#28751;?#28051;?4KB?#32029;浣?#37828;畠鏄彲?#21412;缃?#37928;?#38171;夛紝?#28266;SSTable鐨?#37832;?#28751;?#37828;疊lock绱?#23534;曪紝?#25956;浜庡畾浣岯lock?#32029;杩欎簺绱?#23534;曞湪SSTable鎵撳紑鏃惰?#23006;杞藉埌鍐?#28699;?#28051;紝?#28266;?#29025;鎵?#37827;堕鍏堜粠鍐?#28699;?#28051;?#37928;勭储寮?#27996;屽?#21976;煡鎵?#37813;惧埌Block?#32029;?#21159;鍚?#28051;娆?#32446;?#37929;?#28725;?#38316;撳嵆?#24434;璇?#37721;栧埌鐩?#25652;?#37928;凚lock銆?#26473;?#37832;?#28051;绉嶆?#35268;鏄?#28751;?#26473;?#28051;猄STable?#23006;杞藉埌鍐?#28699;?#28051;紝?#31904;鑰屽湪?#29025;鎵惧?#23678;壂?#24383;涓?#28051;嶉渶瑕?#29831;?#37721;栫鐩?#37510;?/span><br /><br />杩?#28051;?#29864;屼?#29006;氨鏄疕File绗?#28051;涓?#37911;?#37832;?#37928;?#37837;?#23534;?#28052;堬紝璐村紶鍥?#37808;?#37721;?#28051;涓?#38171;?br /><img src="http://www.8634070.com/images/blogjava_net/dlevin/image0080.jpg" alt="" height="160" border="0" width="554" /><br />?#28266;HBase浣跨敤杩囩涓紝瀵?#26473;?#28051;?#37911;?#37832;?#37928;凥File閬囧埌浠?#28051;?#28051;浜涢棶棰?#38171;?#37721;?#38000;?a href="http://hbase.apache.org/book.html#_hfile_format_2">杩欓噷</a>锛?#38171;?br />1. 瑙?#37835;?#37827;?#37712;?#28699;?#28003;跨敤?#22138;姣?#26440;?#26946;?#37510;?br />2. Bloom Filter?#25328;Block绱?#23534;曚細鍙?#37928;勫緢澶紝鑰屽?#21342;?#23941;惎鍔?#37804;兘銆傚叿浣?#37928;勶紝Bloom Filter?#24434;浠?#28583;為暱?#22476;100MB姣?#28051;狧File?#32029;鑰孊lock绱?#23534;曞彲浠?#28583;為暱?#22476;300MB?#32029;濡傛灉涓涓狧RegionServer涓?#37832;?0涓狧Region?#32029;鍒欎粬浠?#37714;?#37714;?#38003;藉?#28858;暱?#22476;2GB?#25328;6GB鐨勫灏?#37510;侶Region?#28214;瑕佸湪鎵撳紑鏃讹紝?#28214;瑕佸姞杞芥墍鏈?#37928;凚lock绱?#23534;曞埌鍐?#28699;?#28051;紝鍥?#38000;屽?#21342;?#23941;惎鍔?#37804;兘锛?#38000;屽湪绗?#28051;娆Request鏃讹紝?#28214;瑕佸皢鏁?#28051;狟loom Filter?#23006;杞藉埌鍐?#28699;?#28051;紝鍐嶅紑濮嬫煡鎵撅紝鍥?#38000;孊loom Filter澶?#28598;細褰卞?#23943;涓娆?#29831;?#23033;?#37928;勫欢杩?#37510;?br />鑰孒File?#28266;鐗?#37832;?涓?#28725;?#26473;欎?#28066;棶棰?#37707;氫簡涓浜涗?#27194;?#26677;紝?#21503;浣撲細?#28266;HFile瑙?#37835;?#37827;?#29831;?#32513;?#29831;?#37828;?#37510;?br /><h2>SSTable浣?#28051;?#28699;樺偍浣跨敤</h2>缁?#32513;BigTable鐨勮?#28888;?#22247;線涓嬭?#24110;紝?#28266;5.3 Tablet Serving灏?#38010;?#28051;?#26473;?#37837;?#37712;?#38316;?#38171;堢6椤?#38171;?#38171;?br /><fieldset><legend>Tablet Serving</legend> <div class="page" title="Page 6"> <div class="layoutArea"> <div class="column"> <p><span style="font-size: 10.000000pt; font-family: 'Times'">Updates are committed to a commit log that stores redo records. Of these updates, the recently committed ones are stored in memory in a sorted buffer called a </span><span style="font-size: 10.000000pt; font-family: 'Times'; font-style: italic">memtable</span><span style="font-size: 10.000000pt; font-family: 'Times'">; the older updates are stored in a sequence of SSTables. To recover a tablet, a tablet server</span><span style="font-size: 10.000000pt; font-family: 'Times'"> reads its metadata from the </span><span style="font-size: 10.000000pt; font-family: 'Courier~45'">METADATA </span><span style="font-size: 10.000000pt; font-family: 'Times'">table. This metadata contains the list of SSTables that comprise a tablet and a set of a redo points, which are pointers into any commit logs that may contain data for the tablet. The server reads the indices of the SSTables into memory and reconstructs the memtable by applying all of the updates that have committed since the redo points. </span><br /></p><div class="page" title="Page 6"><div class="layoutArea"><div class="column"><p><span style="font-size: 10.000000pt; font-family: 'Times'">When a write operation arrives at a tablet server, the server checks that it is well-formed, and that the sender is authorized to perform the mutation. Authorization is performed by reading the list of permitted writers from a Chubby file (which is almost always a hit in the Chubby client cache). A valid mutation is written to the commit log. Group commit is used to improve the throughput of lots of small mutations [13, 16]. After the write has been committed, its contents are inserted into the memtable. <br /></span></p> <p><span style="font-size: 10.000000pt; font-family: 'Times'">When a read operation arrives at a tablet server, it is similarly checked for well-formedness and proper authorization. A valid read operation is executed on a merged view of the sequence of SSTables and the memtable. Since the SSTables and the memtable are lexicographically sorted data structures, the merged view can be formed efficiently. </span></p> <p><span style="font-size: 10.000000pt; font-family: 'Times'">Incoming read and write operations can continue while tablets are split and merged. </span></p> </div> </div> </div> </div> </div> </div> </fieldset><span style="font-family: Comic Sans MS; font-size: 10pt;">绗?#28051;娈靛?#23680;涓?#23048;电畝鍗曟弿杩帮紝闈炵炕璇?#38171;?br />?#28266;鏂?#37825;?#37817;?#37712;?#37711;?#37827;讹紝杩?#28051;?#37823;?#28003;?#26851;?#37711;堟彁浜埌鏃?#36423;?#28051;?#28003;?#28051;redo绾?#35120;曪紝鏈杩?#37928;?#37825;?#37817;?#28699;樺偍?#28266;鍐?#28699;?#37928;?#37818;?#25652;忕紦瀛memtable涓?#38171;?#37827;?#37928;?#37825;?#37817;?#28699;樺偍?#28266;涓绯?#37714;?#37928;凷STable 涓?#37510;傚湪recover涓紝tablet server?#31904;METADATA琛?#28051;?#29831;?#37721;metadata?#32029;metadata鍖?#37722;簡缁勬?#24626;ablet鐨勬墍鏈塖STable锛?#32510;?#35120;曚簡杩欎簺SSTable鐨勫厓 鏁?#37817;俊鎭紝濡SSTable鐨?#28003;?#32515;?#37510;丼tartKey銆丒ndKey绛?#38171;?#28000;?#37721;?#28051;绯?#37714;?#37827;?#36423;?#28051;?#37928;剅edo?#20579;銆俆ablet Server璇?#37721;朣STable鐨勭储寮曞埌鍐?#28699;橈紝骞replay杩欎簺redo?#20579;涔嬪悗鐨勬洿鏂版潵閲嶆?#21044;emtable銆?br />?#28266;璇?#37827;讹紝瀹屾垚鏍?#23534;?#37510;?#37818;堟潈绛夋?#29025;鍚庯紝璇讳細鍚?#37827;?#29831;?#37721;朣STable銆乵emtable锛圚Base涓?#26473;樺寘鍚簡BlockCache涓?#37928;?#37825;?#37817;?#38171;?#39582;?#37722;?#39582;?#28000;?#28000;?#37928;?#32513;撴灉?#32029;鐢?#27996;嶴STable?#25328;memtable閮芥槸瀛楀吀搴?#37818;?#37714;楋紝鍥?#38000;屽悎骞舵搷浣滃彲浠?#23536;堥珮鏁堝?#23678;垚銆?br /></span><h2>SSTable?#28266;Compaction杩囩涓?#37928;?#28003;跨敤</h2>?#28266;BigTable璁烘枃5.4 Compaction灏?#38010;?#28051;?#37828;?#26473;?#37837;?#29831;?#37928;?#38171;?br /><fieldset><legend>Compaction</legend> <div class="page" title="Page 6"> <div class="layoutArea"> <div class="column"> <p><span style="font-size: 10.000000pt; font-family: 'Times'">As write operations execute, the size of the memtable increases. When the memtable size reaches a threshold, the memtable is frozen, a new memtable is created, and the frozen memtable is converted to an SSTable and written to GFS. This </span><span style="font-size: 10.000000pt; font-family: 'Times'; font-style: italic">minor compaction </span><span style="font-size: 10.000000pt; font-family: 'Times'">process has two goals: it shrinks the memory usage of the tablet server, and it reduces the amount of data that has to be read from the commit log during recovery if this server dies. Incoming read and write operations can continue while compactions occur. </span></p> <p><span style="font-size: 10.000000pt; font-family: 'Times'">Every minor compaction creates a new SSTable. If this behavior continued unchecked, read operations might need to merge updates from an arbitrary number of SSTables. Instead, we bound the number of such files by periodically executing a </span><span style="font-size: 10.000000pt; font-family: 'Times'; font-style: italic">merging compaction </span><span style="font-size: 10.000000pt; font-family: 'Times'">in the background. A merging compaction reads the contents of a few SSTables and the memtable, and writes out a new SSTable. The input SSTables and memtable can be discarded as soon as the compaction has finished. </span></p> <p><span style="font-size: 10.000000pt; font-family: 'Times'">A merging compaction that rewrites all SSTables into exactly one SSTable is called a </span><span style="font-size: 10.000000pt; font-family: 'Times'; font-style: italic">major compaction</span><span style="font-size: 10.000000pt; font-family: 'Times'">. SSTables produced by non-major compactions can contain special deletion entries that suppress deleted data in older SSTables that are still live. A major compaction, on the other hand, produces an SSTable that contains no deletion information or deleted data. Bigtable cycles through all of its tablets and regularly applies major compactions to them. These major compactions allow Bigtable to reclaim resources used by deleted data, and also allow it to ensure that deleted data disappears from the system in a timely fashion, which is important for services that store sensitive data. </span></p> </div> </div> </div> </fieldset><span style="font-family: Comic Sans MS; font-size: 10pt;">闅忕潃memtable澶?#28751;忓?#28850;姞?#22476;涓涓榾鍊硷紝杩?#28051;猰emtable浼氳鍐?#28003;?#38000;屽?#28056;缓涓涓?#37826;?#37928;刴emtable浠緵浣跨敤?#32029;鑰?#37827;?#37928;刴emtable?#32048;杞?#37817;?#37812;?#28051;涓猄STable鑰?#37712;?#38316;揋FS涓紝杩?#28051;?#26473;囩?#23338;彨鍋歮inor compaction銆?#26473;?#28051;猰inor compaction?#24434;浠?#37713;?#28751;?#37712;?#28699;?#28003;跨敤?#22138;?#32029;骞跺彲浠?#37713;?#28751;?#37827;?#36423;楀灏忥紝鍥?#28051;烘寔涔?#37718;?#37722;?#37928;?#37825;?#37817;彲浠粠鏃?#36423;?#28051;?#37714;?#38340;?#37510;?/span>?#28266;minor compaction杩囩涓紝?#24434;浠?#32513;?#32513;?#28598;勭悊璇?#37712;?#29831;?#23033;?#37510;?br /><span style="font-size: 10pt; font-family: Comic Sans MS;">姣忔minor compaction?#32048;鐢?#37812;?#37826;?#37928;凷STable?#26499;浠讹紝濡傛灉SSTable?#26499;浠?#37825;伴噺澧炲姞?#32029;鍒欎細褰卞搷璇?#37928;?#37804;兘?#32029;鍥?#38000;屾?#24532;璇?#38318;介渶瑕?#29831;?#37721;栨墍鏈塖STable?#26499;浠讹紝?#21159;鍚?#37722;?#39582;?#32513;撴灉?#32029;鍥?#38000;屽?#31565;STable?#26499;浠?#28051;?#37825;伴渶瑕?#37832;?#28051;?#38340;愶紝骞?#28051;?#37827;?#28051;?#37827;?#37928;勯渶瑕佸湪鍚?#37721;?#37707;歮erging compaction?#32029;杩?#28051;猰erging compaction璇?#37721;?#28051;浜汼STable?#26499;浠跺拰memtable鐨勫唴瀹癸紝骞?#28751;?#28000;?#28000;?#37722;?#39582;?#37712;?#37711;?#28051;涓?#37826;?#37928;凷STable涓?#37510;?#35120;?#26473;?#28051;?#26473;囩?#23338;?#23678;垚鍚庯紝杩欎簺婧怱STable?#25328;memtable?#27688;?#24434;浠?#29722;?#37714;?#38340;簡銆?br />濡傛灉涓涓猰erging compaction鏄?#37722;?#39582;舵墍鏈塖STable?#22476;涓涓猄STable?#32029;鍒?#26473;?#28051;?#26473;囩?#23340;鍋歮ajor compaction銆?#28051;娆major compaction?#32048;灏唌ark鎴?#37714;?#38340;?#37928;勪俊鎭?#37510;?#37825;?#37817;?#37714;?#38340;紝鑰屽叾浠?#28051;?#23046;compaction鍒欎?#27691;?#28616;暀杩欎?#28055;俊鎭?#37510;?#37825;?#37817;?#38171;mark鐨勫舰寮?#38171;?#37510;侭igtable?#32048;鏃?#28051;?#37827;?#37928;?#37813;?#37819;忔墍鏈?#37928;Tablet?#32029;骞?#28725;瑰畠浠?#37707;歮ajor compaction銆?#26473;?#28051;猰ajor compaction?#24434;浠?#28751;嗛渶瑕?#37714;?#38340;?#37928;?#37825;?#37817;?#37930;?#23005;?#37928;勫垹闄粠鑰岃?#20636;?#20346;闂达紝骞朵?#28615;?#20346;郴缁?#28051;?#22183;鎬?#37510;?/span><br /><h2>SSTable鐨刲ocality?#25328;In Memory</h2><span style="font-family: Comic Sans MS; font-size: 10pt;">?#28266;Bigtable涓紝?#30048;鐨?#37832;?#37734;版?#37828;?#37922;盠ocality group?#28533;?#30078;涔?#37928;勶紝鍗?#28598;?#28051;猚olumn family?#24434;浠?#32513;勫?#22557;埌涓涓猯ocality group涓紝?#28266;鍚?#28051;涓猅ablet涓紝浣跨敤鍗曠嫭鐨凷STable瀛樺偍杩欎?#28056;湪鍚?#28051;涓猯ocality group鐨刢olumn family銆侶Base?#22968;杩?#28051;?#22959;?#37736;嬬畝鍖栦簡?#32029;鍗?#23011;?#28051;猚olumn family?#28266;姣?#28051;狧Region閮?#28003;跨敤鍗曠嫭鐨凥File瀛樺偍?#32029;HFile?#30149;鏈locality group鐨勬蹇碉紝鎴?#38000;?#28051;涓猚olumn family?#27688;鏄?#28051;涓猯ocality group銆?/span><br /><br /><span style="font-family: Comic Sans MS; font-size: 10pt;">?#28266;Bigtable涓紝杩樺彲浠敮鎸佸湪locality group?#39559;鍒?#29825;?#32515;?#37828;?#37722;?#28751;嗘墍鏈?#26473;?#28051;猯ocality group鐨?#37825;?#37817;姞杞藉埌鍐?#28699;?#28051;紝?#28266;HBase涓?#38315;?#26473;column family?#30078;涔?#37827;惰缃?#37510;?#26473;?#28051;?#37712;?#28699;樺姞杞?#38322;囩敤寤?#37827;跺姞杞斤紝涓昏搴旂敤浜?#28051;浜涘皬鐨刢olumn family?#32029;骞?#28051;?#32513;忓父琚敤?#22476;鐨勶紝?#31904;鑰屾彁鍗?#29831;?#37928;?#37804;兘?#32029;鍥?#38000;?#26473;?#37837;峰氨涓嶉渶瑕?#37712;嶄粠纾?#37929;?#28051;?#29831;?#37721;栦簡銆?/span><br /><h2>SSTable鍘嬬缉</h2>Bigtable鐨勫?#23340;缉鏄?#37737;?#27996;locality group?#39559;鍒?#38171;?br /><fieldset><legend>Compression</legend> <div class="page" title="Page 7"> <div class="layoutArea"> <div class="column"> <p><span style="font-size: 10.000000pt; font-family: 'Times'">Clients can control whether or not the SSTables for a locality group are compressed, and if so, which compression format is used. The user-specified compression format is applied to each SSTable block (whose size is controllable via a locality group specific tuning parameter). Although we lose some space by compressing each block separately, we benefit in that small portions of an SSTable can be read without decompressing the entire file. Many clients use a two-pass custom compression scheme. The first pass uses Bentley and McIlroy’s scheme [6], which compresses long common strings across a large window. The second pass uses a fast compression algorithm that looks for repetitions in a small 16 KB window of the data. Both compression passes are very fast—they encode at 100–200 MB/s, and decode at 400–1000 MB/s on modern machines.</span></p></div></div></div></fieldset><span style="font-family: Comic Sans MS; font-size: 10pt;">Bigtable鐨勫?#23340;缉浠SSTable涓?#37928;?#28051;涓狟lock涓?#37719;?#28003;嶏紝铏界劧姣?#28051;狟lock涓哄?#23340;缉鍗?#28003;?#37817;?#28598;?#28051;浜涚闂达紝浣?#37828;?#38322;囩敤杩?#32457;嶆柟寮忥紝鎴戜滑?#24434;浠?#28000;Block涓?#37719;?#28003;?#29831;?#37721;?#37510;?#29785;?#37720;?#37510;?#37714;嗘瀽?#32029;鑰?#28051;?#37828;?#23011;忔浠?#28051;涓?#8220;澶?#8221;鐨凷STable涓?#37719;?#28003;?#29831;?#37721;?#37510;?#29785;?#37720;?#37510;?#37714;嗘瀽銆?/span><br /><h2>SSTable鐨?#29831;荤紦瀛?/h2>涓轰簡鎻?#37719;?#29831;?#37928;?#37804;兘?#32029;Bigtable閲囩敤涓?#28766;傜紦瀛?#37832;?#37714;?#38171;?br /><fieldset><legend>Caching for read performance</legend> <div class="page" title="Page 7"> <div class="layoutArea"> <div class="column"> <p><span style="font-size: 10.000000pt; font-family: 'Times'">To improve read performance, tablet servers use two levels of caching. The Scan Cache is a higher-level cache that caches the key-value pairs returned by the SSTable interface to the tablet server code. The Block Cache is a lower-level cache that caches SSTables blocks that were read from GFS. The Scan Cache is most useful for applications that tend to read the same data repeatedly. The Block Cache is useful for applications that tend to read data that is close to the data they recently read (e.g., sequential reads, or random reads of different columns in the same locality group within a hot row).</span></p></div></div></div></fieldset>涓?#28766;傜紦瀛?#37714;?#37714;?#37828;?#38171;?br />1. High Level?#32029;?#32038;瀛樹粠SSTable璇?#37721;?#37928;凨ey/Value瀵?#37510;傛彁鍗囬偅浜涘惧悜?#22136;澶?#37928;?#29831;?#37721;?#37929;?#37722;?#37928;?#37825;?#37817;?#37928;勬搷浣?#38171;?#23534;曠敤灞閮?#37804;?#37720;熺悊锛?#37510;?br />2. Low Level?#32029;BlockCache?#32029;?#32038;瀛楽STable涓?#37928;凚lock銆傛彁鍗囬偅浜涘惧?#25116;簬璇?#37721;?#37929;?#26473;?#37825;?#37817;?#37928;勬搷浣?#37510;?br /><h2>Bloom Filter</h2>?#22688;?#26499;鏈夋?#24876;埌Bigtable閲囩敤鍚?#39582;?#29831;伙紝鍗抽渶瑕?#29831;?#37721;?#23011;?#28051;猄STable涓?#37928;?#37929;?#37711;?#37825;?#37817;紝骞?#37722;?#39582;舵垚涓涓?#32513;撴灉杩?#37733;烇紝?#21159;鑰屾?#24532;璇?#38318;介渶瑕?#29831;?#37721;栨墍鏈塖STable?#32029;鑷劧?#32048;鑰?#29840;?#37804;兘?#32029;鍥?#38000;屽紩鍏簡Bloom Filter?#32029;?#30048;?#24434;浠?#23536;?#36423;?#38315;?#37928;?#37813;惧埌涓涓猂owKey涓嶅湪鏌?#28051;猄STable涓?#37928;?#27996;嬪疄锛堟敞锛?#37721;?#26473;囨潵鍒?#28051;嶆?#24878;珛锛?#37510;?br /><fieldset><legend>Bloom Filter</legend> <div class="page" title="Page 7"> <div class="layoutArea"> <div class="column"> <p><span style="font-size: 10.000000pt; font-family: 'Times'">As described in Section 5.3, a read operation has to read from all SSTables that make up the state of a tablet. If these SSTables are not in memory, we may end up doing many disk accesses. We reduce the number of accesses by allowing clients to specify that Bloom fil- ters [7] should be created for SSTables in a particu- lar locality group. A Bloom filter allows us to ask whether an SSTable might contain any data for a spec- ified row/column pair. For certain applications, a small amount of tablet server memory used for storing Bloom filters drastically reduces the number of disk seeks re- quired for read operations. Our use of Bloom filters also implies that most lookups for non-existent rows or columns do not need to touch disk. </span></p> </div> </div> </div> </fieldset><h2>SSTable璁?#29825;?#37812;怚mmutable鐨勫?#34249;</h2>?#28266;SSTable?#30078;涔?#28051;氨鏈夋?#24876;埌SSTable鏄?#28051;涓狪mmutable鐨刼rder map?#32029;杩?#28051;狪mmutable鐨勮璁彲浠?#29825;?#32495;?#32513;熺畝鍗?#23536;?#28598;?#38171;?br /><fieldset><legend>Exploiting Immutability</legend> <div class="page" title="Page 8"> <div class="layoutArea"> <div class="column"> <p><span style="font-size: 10.000000pt; font-family: 'Times'">Besides the SSTable caches, various other parts of the Bigtable system have been simplified by the fact that all </span><span style="font-size: 10.000000pt; font-family: 'Times'">of the SSTables that we generate are immutable. For example, we do not need any synchronization of accesses to the file system when reading from SSTables. As a result, concurrency control over rows can be implemented very efficiently. The only mutable data structure that is accessed by both reads and writes is the memtable. To reduce contention during reads of the memtable, we make each memtable row copy-on-write and allow reads and writes to proceed in parallel. </span> </p><div class="page" title="Page 8"><div class="layoutArea"><div class="column"><p><span style="font-size: 10.000000pt; font-family: 'Times'">Since SSTables are immutable, the problem of permanently removing deleted data is transformed to garbage collecting obsolete SSTables. Each tablet’s SSTables are registered in the </span><span style="font-size: 10.000000pt; font-family: 'Courier~45'">METADATA </span><span style="font-size: 10.000000pt; font-family: 'Times'">table. The master removes obsolete SSTables as a mark-and-sweep garbage collection [25] over the set of SSTables, where the </span><span style="font-size: 10.000000pt; font-family: 'Courier~45'">METADATA </span><span style="font-size: 10.000000pt; font-family: 'Times'">table contains the set of roots. </span></p> <p><span style="font-size: 10.000000pt; font-family: 'Times'">Finally, the immutability of SSTables enables us to split tablets quickly. Instead of generating a new set of SSTables for each child tablet, we let the child tablets share the SSTables of the parent tablet. </span></p> </div> </div> </div> </div></div></div></fieldset><span style="font-family: Comic Sans MS; font-size: 10pt;">鍏?#27996;嶪mmutable鐨勪紭?#20579;鏈?#28000;?#28051;嬪嚑?#20579;锛?/span><br /><span style="font-family: Comic Sans MS; font-size: 10pt;">1. ?#28266;璇SSTable鏄?#28051;嶉渶瑕佸?#23678;銆?#29831;?#37712;?#37722;屾鍙渶瑕佸湪memtable涓?#28598;勭?#21997;紝涓轰簡鍑?#28751;memtable鐨?#29831;?#37712;欑珵浜夛紝Bigtable灏唌emtable鐨剅ow璁?#29825;?#37812;恈opy-on-write?#32029;?#31904;鑰?#29831;?#37712;欏彲浠?#37722;?#37827;?#26473;涜銆?/span><br /><span style="font-family: Comic Sans MS; font-size: 10pt;">2. 姘?#28052;?#37928;?#32457;婚櫎鏁?#37817;?#26462;?#37721;?#28051;篠STable鐨凣arbage Collect銆傛瘡涓猅ablet涓?#37928;凷STable?#28266;METADATA琛?#28051;?#37832;?#23049;?#37712;岋紝master浣跨敤mark-and-sweep绠楁硶灏哠STable?#28266;GC杩囩涓?#32457;婚櫎銆?/span><br /><span style="font-family: Comic Sans MS; font-size: 10pt;">3. ?#24434;浠?#29825;Tablet Split杩囩?#23338;彉鐨勯珮鏁堬紝鎴戜滑涓嶉渶瑕?#28051;烘瘡涓?#28699;怲ablet鍒涘?#28888;柊鐨凷STable?#32029;鑰屾槸?#24434;浠?#37711;?#27996;埗</span><span style="font-family: Comic Sans MS; font-size: 10pt;">Tablet鐨凷STable銆?/span><img src ="http://www.8634070.com/DLevin/aggbug/427481.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.8634070.com/DLevin/" target="_blank">DLevin</a> 2015-09-25 01:35 <a href="http://www.8634070.com/DLevin/archive/2015/09/25/427481.html#Feedback" target="_blank" style="text-decoration:none;">?#24386;琛?#29831;勮</a></div>]]></description></item><item><title>[杞琞楂?#37804;兘IO妯?#37736;嬫鏋?/title><link>http://www.8634070.com/DLevin/archive/2015/09/04/427118.html</link><dc:creator>DLevin</dc:creator><author>DLevin</author><pubDate>Fri, 04 Sep 2015 07:16:00 GMT</pubDate><guid>http://www.8634070.com/DLevin/archive/2015/09/04/427118.html</guid><wfw:comment>http://www.8634070.com/DLevin/comments/427118.html</wfw:comment><comments>http://www.8634070.com/DLevin/archive/2015/09/04/427118.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.8634070.com/DLevin/comments/commentRss/427118.html</wfw:commentRss><trackback:ping>http://www.8634070.com/DLevin/services/trackbacks/427118.html</trackback:ping><description><![CDATA[<h1 id="reader-title">楂?#37804;兘IO妯?#37736;嬫鏋?/h1><p>杞?#38007;?#38171;歨ttp://www.cnblogs.com/fanzhidongyzby/p/4098546.html</p><p><span>鏈嶅姟鍣?#32468;?#32514;?#32459;嬬粡?#29238;?#28214;瑕?#37835;勯犻珮鎬兘鐨処O妯?#37736;嬶紝?#29238;瑙?#37928;処O妯?#37736;?#37832;?#37733;?#32457;?#38171;?/span></p><p><span>锛?锛?/span><span>鍚屾闃诲?#28657;O锛圔locking IO锛?#38171;?#37719;?#28028;?#32513;?#37928;処O妯?#37736;?#37510;?/span></p><p><span>锛?锛?/span><span>鍚屾闈為?#35826;</span><span>IO锛圢on-blocking IO锛?#38171;?#27035;?#29825;?#37714;涘缓鐨剆ocket閮芥槸闃诲鐨勶紝闈為?#35826;?#28657;O瑕佹眰socket琚?#29825;?#32515;?#28051;篘ONBLOCK銆?#23049;?#37808;?#26473;?#38322;屾墍璇?#37928;凬IO骞?#38344;Java鐨凬IO锛圢ew IO锛?#25652;?#37510;?/span></p><p><span>锛?锛?/span><span>IO澶?#29882;?#28598;嶇敤锛圛O Multiplexing锛?#38171;?#37719;?#32513;?#37711;?#37928;凴eactor璁?#29825;?#22959;?#23534;忥紝鏈?#37827;?#28052;?#32457;?#28051;?#23534;?#23005;?#38339;诲?#28657;O?#32029;Java涓?#37928;凷elector?#25328;Linux涓?#37928;epoll閮芥槸杩?#32457;嶆鍨?#37510;?/span></p><p><span>锛?锛?/span><span>寮?#23005;IO锛圓synchronous IO锛?#38171;?#37719;?#32513;?#37711;?#37928;凱roactor璁?#29825;?#22959;?#23534;忥紝涔?#32457;?#28051;?#23534;?#23005;?#38344;為?#35826;?#28657;O銆?/span></p><p><span>鍚屾鍜屽紓姝?/span><span>鐨勬蹇垫弿杩?#37928;?#37828;敤鎴风?#36328;涓?#37712;?#37837;?#37928;?#27996;?#27996;?#37826;?#23534;?#38171;?#37722;屾鏄?#37816;囩敤鎴风?#36328;?#23338;彂?#25443;IO璇?#23033;?#37722;庨渶瑕佺瓑寰?#37812;?#38000;?#26462;?#29831;?#37712;?#37837;窱O鎿?#28003;?#28729;屾垚鍚庢墠?#20824;缁?#32513;?#37813;?#29723;?#38171;?#38000;屽紓姝?#37828;?#37816;囩敤鎴风?#36328;?#23338;彂?#25443;IO璇?#23033;?#37722;?#28000;?#32513;?#32513;?#37813;?#29723;岋紝褰?#37712;?#37837;窱O鎿?#28003;?#28729;屾垚鍚庝細閫氱煡?#25956;鎴风?#36328;?#23350;紝鎴?#38000;呰皟?#25956;?#25956;鎴风?#36328;?#23339;敞鍐?#37928;勫洖?#30367;鍑?#37825;?#37510;?/span></p><p><span>闃诲?#28850;?#23684;?#28858;?#35826;</span><span>鐨勬蹇垫弿杩?#37928;?#37828;敤鎴风?#36328;?#23341;皟?#25956;鍐?#37837;窱O鎿?#28003;?#37928;勬柟寮?#38171;?#38339;诲鏄?#37816;IO鎿?#28003;滈渶瑕佸交搴?#28729;屾垚鍚庢墠杩?#37733;炲埌?#25956;鎴风闂?#38171;?#38000;岄?#28858;?#35826;鏄?#37816;IO鎿?#28003;滆?#30367;?#25956;鍚庣?#23338;嵆杩?#37733;?#32513;欑敤鎴?#28051;涓姸鎬佸硷紝鏃犻渶绛夊埌IO鎿?#28003;?#35120;?#25652;?#28729;屾垚銆?/span></p><p><span>鍙?#28598;栵紝</span><span>Richard Stevens</span><span> ?#28266;銆奤nix ?#32137;缁?#32514;?#32459;?#37510;嬪嵎1涓?#37819;愬埌鐨勫熀浜庝俊鍙?#26937;卞姩鐨処O锛圫ignal Driven IO锛夋鍨嬶紝鐢?#27996;?#29831;?#22959;?#37736;嬪苟涓嶅父?#25956;?#32029;鏈枃涓?#28003;?#23057;?#37721;?#37510;?#37818;?#28051;嬫潵?#32029;鎴戜滑璇?#32513;?#37714;嗘瀽鍥?#32457;嶅父瑙?#37928;処O妯?#37736;?#37928;勫疄鐜板?#29114;悊銆?#28051;轰簡鏂逛?#25358;弿杩帮紝鎴戜滑缁?#28051;浣跨敤IO鐨?#29831;绘搷浣?#28003;?#28051;?#32448;轰緥銆?/span></p><p><span>涓銆?/span><span>鍚屾闃诲?#28657;O</span></p><p><span>鍚屾闃诲?#28657;O妯?#37736;?#37828;?#37832;?#30045;鍗?#37928;処O妯?#37736;嬶紝?#25956;鎴风?#36328;?#23338;湪鍐?#37837;?#26473;涜?#23382;O鎿?#28003;?#37827;惰闃诲銆?/span></p><p><img src="http://images.cnitblog.com/blog/405877/201411/142330286789443.png" alt="" height="285" width="535" /></p><p><span>鍥?/span><span>1 鍚屾闃诲?#28657;O</span></p><p><span>濡?#37733;??#22669;绀猴紝?#25956;鎴风?#36328;閫?#26473;囩郴缁熻皟?#25956;read?#24386;?#25443;IO璇绘搷浣滐紝鐢辩敤鎴风闂?#26462;埌鍐?#37837;哥闂?#37510;?#37712;?#37837;?#32475;夊埌鏁?#37817;?#37718;呭埌杈惧悗?#32029;?#21159;鍚?#28751;?#37818;?#37824;?#37928;?#37825;?#37817;?#37815;?#29840;濆埌?#25956;鎴风闂达紝瀹屾垚read鎿?#28003;?#37510;?/span></p><p><span>?#25956;鎴风?#36328;浣跨敤鍚屾闃诲?#28657;O妯?#37736;?#37928;勪吉?#21804;鐮佹弿杩?#28051;?#38171;?/span></p><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000; ">{<br />    read(socket, buffer);<br />    process(buffer);<br />}</span></div><p><span>鍗崇敤鎴烽渶瑕佺瓑寰卹ead灏唖ocket涓?#37928;?#37825;?#37817;?#29831;?#37721;栧埌buffer鍚庯紝?#22688;缁?#32513;?#28598;勭悊鎺?#37824;?#37928;?#37825;?#37817;?#37510;?#37825;?#28051;狪O璇?#23033;?#37928;?#26473;囩涓紝?#25956;鎴风?#36328;鏄?#29722;?#38339;诲鐨勶紝杩?#28725;艰嚧?#25956;鎴峰湪?#24386;?#25443;IO璇?#23033;?#37827;讹紝涓嶈?#34249;仛浠?#28003;?#27996;嬫儏?#32029;瀵笴PU鐨勮?#21228;簮鍒敤鐜?#28051;?#28598;?#37510;?/span></p><p><span>浜?#37510;?/span><span>鍚屾闈為?#35826;?#28657;O</span></p><p><span>鍚屾闈為?#35826;?#28657;O鏄湪鍚屾闃诲?#28657;O鐨勫熀纭涓婏紝灏唖ocket璁?#32515;?#28051;篘ONBLOCK銆?#26473;?#37837;?#37707;氱敤鎴风?#36328;?#23338;彲浠湪?#24386;?#25443;IO璇?#23033;?#37722;庡彲浠?#32468;嬪嵆杩?#37733;?#37510;?/span></p><p> <img src="http://images.cnitblog.com/blog/405877/201411/142332004602984.png" alt="" height="315" width="562" /></p><p><span>鍥? 鍚屾闈為?#35826;?#28657;O</span></p><p><span>濡?#37733;??#22669;绀猴紝鐢?#27996;巗ocket鏄?#38344;為?#35826;鐨勬柟寮忥紝鍥犳?#25956;鎴风?#36328;?#23338;彂?#25443;IO璇?#23033;?#37827;剁?#23338;嵆杩?#37733;?#37510;?#28003;?#39582;?#37832;?#29831;?#37721;栧埌浠?#28003;?#37825;?#37817;紝?#25956;鎴风?#36328;?#23342;渶瑕?#28051;嶆柇?#28276;?#24386;?#25443;IO璇?#23033;傦紝鐩村埌鏁?#37817;埌杈惧悗?#32029;?#22688;鐪?#23005;?#29831;?#37721;栧埌鏁?#37817;紝缁?#32513;?#37813;?#29723;?#37510;?/span></p><p><span>?#25956;鎴风?#36328;浣跨敤鍚屾闈為?#35826;?#28657;O妯?#37736;?#37928;勪吉?#21804;鐮佹弿杩?#28051;?#38171;?/span></p><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000; ">{<br />    </span><span style="color: #0000FF; ">while</span><span style="color: #000000; ">(read(socket, buffer) </span><span style="color: #000000; ">!=</span><span style="color: #000000; "> SUCCESS) { }<br />    process(buffer);<br />}</span></div><p><span>鍗? ?#25956;鎴烽渶瑕?#28051;嶆柇?#28276;?#30367;?#25956;read?#32029;?#30398;璇?#29831;?#37721;杝ocket涓?#37928;?#37825;?#37817;紝鐩村埌璇?#37721;?#37812;?#37716;?#37722;庯紝?#22688;缁?#32513;?#28598;勭悊鎺?#37824;?#37928;?#37825;?#37817;?#37510;?#37825;?#28051;狪O璇?#23033;?#37928;?#26473;囩涓紝铏界劧?#25956;鎴风?#36328;?#23339;?#24532;?#24386;?#25443;IO璇? 姹?#37722;庡彲浠?#32468;嬪嵆杩?#37733;烇紝浣?#37828;?#28051;轰簡绛夊埌鏁?#37817;紝浠嶉渶瑕?#28051;嶆柇?#28276;杞?#29831;?#37510;侀噸澶?#29831;?#23033;傦紝娑?#38000;椾簡澶噺鐨凜PU鐨勮?#21228;簮銆?#28051;鑸?#23536;?#28751;?#37929;?#37818;?#28003;跨敤杩?#32457;嶆鍨嬶紝鑰屾槸?#28266;鍏?#28000;朓O妯?#37736;?#28051;?#28003;跨敤闈為樆 濉濱O杩?#28051;鐗?#37804;?#37510;?/span></p><p><span>涓?#37510;?/span><span>IO澶?#29882;?#28598;嶇敤</span></p><p><span>IO澶?#29882;?#28598;嶇敤妯?#37736;?#37828;?#23524;?#32468;嬪湪鍐?#37837;?#37819;愪緵鐨勫璺?#37714;嗙鍑?#37825;皊elect鍩?#32429;涔?#28051;?#37928;勶紝浣跨敤select鍑?#37825;板彲浠?#38316;?#37711;嶅?#23678;闈為?#35826;?#28657;O妯?#37736;?#28051;?#26462;?#29831;?#32475;?#23536;?#37928;勯棶棰?#37510;?/span></p><p><img src="http://images.cnitblog.com/blog/405877/201411/142332187256396.png" alt="" height="320" width="539" /></p><p><span>鍥? 澶?#29882;?#37714;嗙鍑?#37825;皊elect</span></p><p><span>濡?#37733;??#22669;绀猴紝?#25956;鎴?#26851;?#37711;?#28751;嗛渶瑕?#26473;涜?#23382;O鎿?#28003;?#37928;剆ocket娣诲姞?#22476;select涓紝?#21159;鍚?#38339;诲绛?#23536;select绯?#32513;熻皟?#25956;杩?#37733;?#37510;?#35120;?#37825;?#37817;埌杈?#37827;讹紝socket琚縺娲伙紝select鍑?#37825;?#26473;?#37733;?#37510;傜敤鎴风?#36328;?#23339;寮忓彂?#25443;read璇?#23033;傦紝璇?#37721;?#37825;?#37817;?#39582;?#32513;?#32513;?#37813;?#29723;?#37510;?/span></p><p><span>?#31904; 娴佺涓婃潵鐪嬶紝浣跨敤select鍑?#37825;?#26473;涜?#23382;O璇?#23033;傚?#23677;?#23678;闃诲?#28852;鍨嬫病鏈?#28598;?#28598;?#37928;勫尯鍒紝鐢氳嚦杩?#28598;氫簡娣诲姞鐩戣?#21782;ocket?#32029;浠?#37721;婅皟?#25956;select鍑?#37825;?#37928;勯澶?#37823;?#28003;滐紝鏁? 鐜?#37831;村樊銆?#28003;?#37828;紝浣跨敤select浠?#37722;?#37832;澶?#37928;勪?#27194;娍鏄敤鎴峰彲浠湪涓涓?#32510;跨?#23338;唴鍚?#37827;?#28598;勭悊澶?#28051;猻ocket鐨処O璇?#23033;?#37510;傜敤鎴峰彲浠?#23049;?#37712;?#28598;?#28051;猻ocket?#32029;?#21159;鍚?#28051;嶆柇?#28276;?#30367; ?#25956;select璇?#37721;栬?#32314;娲?#37928;剆ocket?#32029;鍗冲彲杈惧埌?#28266;</span><span>鍚?#28051;涓?#32510;跨?#23338;唴鍚?#37827;?#28598;勭悊澶?#28051;狪O璇?#23033;?#37928;?#37929;?#37928;?/span><span>銆?#38000;屽湪鍚屾闃诲?#28852;鍨?#28051;紝蹇?#26916;?#38315;?#26473;囧绾跨鐨勬柟寮忔墠鑳借?#24807;埌杩?#28051;?#37929;?#37928;?#37510;?/span></p><p><span>?#25956;鎴风?#36328;浣跨敤select鍑?#37825;?#37928;勪吉?#21804;鐮佹弿杩?#28051;?#38171;?/span></p><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000; ">{<br />    select(socket);<br />    </span><span style="color: #0000FF; ">while</span><span style="color: #000000; ">(</span><span style="color: #000000; ">1</span><span style="color: #000000; ">) {<br />        sockets </span><span style="color: #000000; ">=</span><span style="color: #000000; "> select();<br />        </span><span style="color: #0000FF; ">for</span><span style="color: #000000; ">(socket </span><span style="color: #0000FF; ">in</span><span style="color: #000000; "> sockets) {<br />            </span><span style="color: #0000FF; ">if</span><span style="color: #000000; ">(can_read(socket)) {<br />                read(socket, buffer);<br />                process(buffer);<br />            }<br />        }<br />    }<br />}</span></div><p><span>鍏?#28051;while寰?#37916;墠灏唖ocket娣诲姞?#22476;select鐩戣涓紝?#21159;鍚庡湪while鍐?#28051;鐩磋皟?#25956;select鑾?#37721;栬?#32314;娲?#37928;剆ocket?#32029;涓鏃socket?#24434;璇伙紝渚皟?#25956;read鍑?#37825;?#28751;唖ocket涓?#37928;?#37825;?#37817;?#29831;?#37721;?#37713;烘潵銆?/span></p><p><span>?#21159; 鑰岋紝浣跨敤select鍑?#37825;?#37928;勪紭?#20579;骞?#28051;?#28000;?#38340;?#27996;?#23005;?#37510;傝?#30028;劧涓?#26473;版柟寮?#37711;?#29825;?#37719;曠?#36328;?#23338;唴澶勭悊澶?#28051;狪O璇?#23033;傦紝浣?#37828;?#23011;?#28051;狪O璇?#23033;?#37928;?#26473;囩杩?#37828;?#38339;诲鐨?#38171;堝湪select鍑?#37825;?#28051;婇樆 濉?#38171;夛紝骞?#37735;?#37827;?#38338;寸?#27699;嚦姣斿?#23678;闃诲?#28657;O妯?#37736;?#26473;?#29781;侀暱銆?#28641;傛灉?#25956;鎴风?#36328;?#23338;彧娉?#37712;?#38007;?#23480;?#37808;?#37711;磋叮鐨剆ocket鎴?#38000;匢O璇?#23033;傦紝?#21159;鍚庡幓鍋氳嚜宸?#37928;?#27996;嬫儏?#32029;绛夊埌鏁?#37817;埌?#28533;鏃?#37712;?#26473;涜澶? 鐞嗭紝鍒欏彲浠?#37819;?#26946;楥PU鐨勫埄?#25956;鐜?#37510;?/span></p><p><span>IO澶?#29882;?#28598;嶇敤妯?#37736;?#28003;跨敤?#31777;Reactor璁?#29825;?#22959;?#23534;忓疄鐜颁簡杩?#28051;鏈?#37714;?#37510;?/span></p><p><img src="http://images.cnitblog.com/blog/405877/201411/142332350853195.png" alt="" height="185" width="567" /></p><p><span>鍥? Reactor璁?#29825;?#22959;?#23534;?/span></p><p><span>濡? 鍥??#22669;绀猴紝EventHandler鎶借薄绫昏绀篒O浜?#28000;?#28598;勭悊鍣紝?#30048;鎷?#37832;塈O?#26499;浠?#37721;?#37836;凥andle锛?#38315;?#26473;get_handle鑾?#37721;?#38171;夛紝浠?#37721;婂?#31547;andle鐨? 鎿?#28003;handle_event锛?#29831;?鍐?#32475;?#38171;?#37510;?#32513;?#37813;?#27996;嶦ventHandler鐨勫?#24878;被?#24434;浠?#28725;?#27996;?#28000;?#28598;勭悊鍣?#37928;勮涓?#26473;涜?#23677;畾鍒?#37510;俁eactor?#34987;?#25956;浜庣?#24714; EventHandler锛堟敞鍐?#37510;?#37714;?#38340;?#32475;?#38171;夛紝骞?#28003;跨敤handle_events瀹?#37916;?#27996;?#28000;?#23536;?#37916;紝涓嶆柇?#30367;?#25956;鍚屾浜?#28000;?#28598;?#29882;?#37714;嗙?#35826;櫒锛?#28051;鑸?#37828;?#37712;?#37837;?#38171;?#37928;勫璺?#37714;嗙鍑?#37825;? select?#32029;鍙?#29781;佹煇涓枃浠?#37721;?#37836;勮?#32314;娲?#38171;堝彲璇?鍐?#32475;?#38171;夛紝select?#27688;杩?#37733;?#38171;堥?#35826;锛夛紝handle_events?#27688;浼氳皟?#25956;涓庢枃浠?#37721;?#37836;勫叧鑱?#37928;?#27996;?#28000;?#28598;勭悊鍣?#37928;? handle_event杩涜鐩?#37711;虫搷浣?#37510;?/span></p><p><img src="http://images.cnitblog.com/blog/405877/201411/142333254136604.png" alt="" height="300" width="647" /></p><p><span>鍥?/span><span>5 IO澶?#29882;?#28598;嶇敤</span></p><p><span>濡? 鍥??#22669;绀猴紝閫?#26473;Reactor鐨勬柟寮忥紝?#24434;浠?#28751;嗙敤鎴风?#36328;杞?#29831;IO鎿?#28003;滅姸鎬?#37928;勫伐浣?#32513;?#28051;浜?#32513;handle_events浜?#28000;?#23536;?#37916;?#26473;涜澶勭悊銆傜敤鎴风?#36328;?#23339;敞鍐?#27996;?#28000;?#28598;勭悊 鍣?#28052;嬪悗?#24434;浠?#32513;?#32513;?#37813;?#29723;屽仛鍏?#28000;?#37928;勫伐浣?#38171;?#23534;?#23005;?#38171;夛紝鑰孯eactor绾跨?#23341;?#29115;矗?#30367;?#25956;鍐?#37837;?#37928;剆elect鍑?#37825;版?#29025;socket?#23032;鎬?#37510;?#35120;?#37832;塻ocket琚縺娲?#37827;讹紝鍒?#38315;氱煡 鐩?#25652;?#37928;勭敤鎴风?#36328;锛堟垨鎵?#29723;岀敤鎴风?#36328;鐨勫洖?#30367;鍑?#37825;?#38171;夛紝鎵?#29723;宧andle_event杩涜鏁?#37817;?#29831;?#37721;?#37510;?#28598;勭悊鐨勫伐浣?#37510;傜敱浜巗elect鍑?#37825;版槸闃诲鐨勶紝鍥犳澶?#29882;疘O澶嶇敤 妯?#37736;?#28052;熻绉?#28051;?#23534;?#23005;?#38339;诲?#28657;O妯?#37736;?#37510;?#23049;?#37808;忥紝杩欓噷鐨勬墍璇?#37928;勯?#35826;鏄?#37816;select鍑?#37825;版墽琛?#37827;?#32510;跨?#23341;闃诲?#28871;紝鑰?#28051;?#37828;?#37816;socket銆?#28051;鑸湪浣跨敤IO澶?#29882;?#28598;嶇敤妯?#37736;? 鏃讹紝socket閮芥槸璁?#32515;?#28051;篘ONBLOCK鐨勶紝涓?#26473;?#26473;?#39582;?#28051;嶄細浜?#37922;?#35120;卞搷?#32029;鍥?#28051;虹敤鎴峰彂?#25443;IO璇?#23033;?#37827;讹紝鏁?#37817;?#23480;?#32513;忓埌杈句簡?#32029;?#25956;鎴风?#36328;涓?#30078;涓?#28028;氳闃诲銆?/span></p><p><span>?#25956;鎴风?#36328;浣跨敤IO澶?#29882;?#28598;嶇敤妯?#37736;?#37928;勪吉?#21804;鐮佹弿杩?#28051;?#38171;?/span></p><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000FF; ">void</span><span style="color: #000000; "> UserEventHandler::handle_event() {<br />    </span><span style="color: #0000FF; ">if</span><span style="color: #000000; ">(can_read(socket)) {<br />        read(socket, buffer);<br />        process(buffer);<br />    }<br />}<br /><br />{<br />    Reactor.register(</span><span style="color: #0000FF; ">new</span><span style="color: #000000; "> UserEventHandler(socket));<br />}</span></div><p><span>?#25956;鎴烽渶瑕侀噸鍐橢ventHandler鐨刪andle_event鍑?#37825;?#26473;涜璇?#37721;?#37825;?#37817;?#37510;?#28598;勭悊鏁?#37817;?#37928;勫伐浣滐紝?#25956;鎴风?#36328;?#23338;彧?#28214;瑕佸皢鑷?#23480;?#37928;凟ventHandler娉?#37712;屽埌Reactor鍗冲彲銆俁eactor涓環andle_events浜?#28000;?#23536;?#37916;?#37928;勪吉?#21804;鐮?#28598;?#38007;村涓?#37510;?/span></p><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000; ">Reactor::handle_events() {<br />    </span><span style="color: #0000FF; ">while</span><span style="color: #000000; ">(</span><span style="color: #000000; ">1</span><span style="color: #000000; ">) {<br />       sockets </span><span style="color: #000000; ">=</span><span style="color: #000000; "> select();<br />       </span><span style="color: #0000FF; ">for</span><span style="color: #000000; ">(socket </span><span style="color: #0000FF; ">in</span><span style="color: #000000; "> sockets) {<br />            get_event_handler(socket).handle_event();<br />       }<br />    }<br />}</span></div><p><span>浜?#28000;?#23536;?#37916;?#28051;嶆柇?#28276;?#30367;?#25956;select鑾?#37721;栬?#32314;娲?#37928;剆ocket?#32029;?#21159;鍚?#37837;?#37817;?#38014;?#37721;杝ocket瀵?#25652;?#37928;凟ventHandler?#32029;鎵?#29723;屽櫒handle_event鍑?#37825;?#37719;冲彲銆?/span></p><p><span>IO澶?#29882;?#28598;嶇敤鏄?#37832;?#29238;浣跨敤鐨処O妯?#37736;嬶紝浣?#37828;?#37711;?#23534;?#23005;?#32459;嬪害杩?#28051;?#28598;?#8220;褰?#25652;?#8221;?#32029;鍥?#28051;哄畠浣跨敤?#31777;?#32048;闃诲绾跨鐨剆elect绯?#32513;熻皟?#25956;銆?#37733;犳IO澶?#29882;?#28598;嶇敤鍙兘绉?#28051;?#23534;?#23005;?#38339;诲?#28657;O?#32029;鑰岄潪鐪?#23005;?#37928;勫紓姝IO銆?/span></p><p><span>鍥?#37510;?/span><span>寮?#23005;IO</span></p><p><span>“鐪? 姝?#8221;鐨勫紓姝IO?#28214;瑕佹搷浣?#32495;?#32513;熸?#26449;己鐨勬敮?#23508;銆傚湪IO澶?#29882;?#28598;嶇敤妯?#37736;?#28051;紝浜?#28000;?#23536;?#37916;?#28751;嗘枃浠?#37721;?#37836;?#37928;勭姸鎬?#27996;?#28000;?#38315;氱煡缁欑敤鎴风?#36328;?#23350;紝鐢辩敤鎴风?#36328;鑷?#29723;?#29831;?#37721;?#37825;?#37817;?#37510;?#28598;勭悊鏁?#37817;?#37510;?#38000;屽湪寮? 姝IO妯?#37736;?#28051;紝褰撶敤鎴风?#36328;?#23339;?#36346;埌閫氱煡鏃讹紝鏁?#37817;?#23480;?#32513;忚鍐?#37837;?#29831;?#37721;栧?#23678;?#26346;紝骞舵?#24807;湪?#31777;?#25956;鎴风?#36328;?#23339;?#22247;畾鐨勭紦鍐?#37718;?#37712;咃紝鍐?#37837;稿湪IO瀹屾垚鍚?#38315;氱煡?#25956;鎴风?#36328;鐩?#37818;?#28003;跨敤鍗冲彲銆?/span></p><p><span>寮?#23005;IO妯?#37736;?#28003;跨敤?#31777;Proactor璁?#29825;?#22959;?#23534;忓疄鐜颁簡杩?#28051;鏈?#37714;?#37510;?/span></p><p><img src="http://images.cnitblog.com/blog/405877/201411/151608309061672.jpg" alt="" height="248" width="744" /></p><p><span>鍥? Proactor璁?#29825;?#22959;?#23534;?/span></p><p><span>濡? 鍥??#32029;Proactor妯?#23534;忓拰Reactor妯?#23534;忓湪缁撴瀯涓?#23011;?#26440;?#37929;?#28028;硷紝涓?#26473;囧湪?#25956;鎴?#38171;圕lient锛?#28003;跨敤鏂?#23534;?#28051;婂樊鍒?#26440;?#28598;?#37510;俁eactor妯?#23534;?#28051;紝?#25956;鎴风?#36328;閫?#26473;? 鍚慠eactor瀵硅薄娉?#37712;屾劅鍏磋叮鐨?#27996;?#28000;?#37929;戝惉?#32029;?#21159;鍚?#27996;?#28000;惰?#24386;鏃惰皟?#25956;浜?#28000;?#28598;勭悊鍑?#37825;?#37510;?#38000;孭roactor妯?#23534;?#28051;紝?#25956;鎴风?#36328;?#23338;皢 AsynchronousOperation锛?#29831;?鍐?#32475;?#38171;?#37510;丳roactor浠?#37721;?#37823;?#28003;?#28729;屾垚鏃?#37928;凜ompletionHandler娉?#37712;屽埌 AsynchronousOperationProcessor銆侫synchronousOperationProcessor浣跨敤Facade妯?#23534;忔彁 渚涗簡涓缁勫紓姝?#37823;?#28003;淎PI锛?#29831;?鍐?#32475;?#38171;変?#28058;敤鎴?#28003;跨敤?#32029;褰撶敤鎴风?#36328;?#23341;皟?#25956;寮?#23005;API鍚庯紝渚跨户缁?#37813;?#29723;?#38007;?#23480;?#37928;?#28000;诲姟銆? AsynchronousOperationProcessor ?#32048;?#32017;鍚嫭?#29659;鐨勫唴鏍?#32510;跨鎵?#29723;屽紓姝?#37823;?#28003;滐紝瀹?#37916;?#37930;?#23005;?#37928;勫紓姝?#37510;?#35120;撳紓姝IO鎿?#28003;?#28729;屾垚 鏃讹紝AsynchronousOperationProcessor灏嗙敤鎴风?#36328;涓AsynchronousOperation涓?#25443;娉?#37712;?#37928;凱roactor ?#25328;CompletionHandler鍙?#37713;猴紝?#21159;鍚?#28751;咰ompletionHandler涓嶪O鎿?#28003;?#37928;?#32513;撴灉鏁?#37817;?#28051;?#25443;杞彂缁? Proactor?#32029;Proactor璐熻矗鍥炶皟姣?#28051;涓?#23534;?#23005;?#37823;?#28003;?#37928;?#27996;?#28000;跺?#23678;垚澶勭悊鍑?#37825;handle_event銆傝?#30028;劧Proactor妯?#23534;?#28051;?#23011;?#28051;?#23534;?#23005;?#37823;?#28003;?#38318;藉彲浠? 缁戝畾涓涓狿roactor瀵硅薄?#32029;浣?#37828;?#28051;鑸湪鎿?#28003;?#32495;?#32513;?#28051;紝Proactor琚?#28729;?#37916;?#28051;篠ingleton妯?#23534;忥紝浠?#28186;?#27996;庨泦涓?#37718;?#37714;嗗彂鎿?#28003;?#28729;屾垚浜?#28000;?#37510;?/span></p><p><img src="http://images.cnitblog.com/blog/405877/201411/142333511475767.png" alt="" height="297" width="686" /></p><p><span>鍥?/span><span>7 寮?#23005;IO</span></p><p><span>濡? 鍥??#22669;绀猴紝寮?#23005;IO妯?#37736;?#28051;紝?#25956;鎴风?#36328;鐩?#37818;?#28003;跨敤鍐?#37837;?#37819;愪緵鐨勫紓姝IO API?#24386;?#25443;read璇?#23033;傦紝涓斿彂?#25443;鍚庣?#23338;嵆杩?#37733;烇紝缁?#32513;?#37813;?#29723;岀敤鎴风?#36328;?#23337;唬鐮?#37510;?#28051;?#26473;?#23005;?#37827;剁敤鎴风?#36328;?#23338;凡 缁?#28751;嗚皟?#25956;鐨凙synchronousOperation?#25328;CompletionHandler娉?#37712;屽埌鍐?#37837;革紝?#21159;鍚庢搷浣?#32495;?#32513;熷紑鍚嫭?#29659;鐨勫唴鏍?#32510;跨?#23338;幓澶勭悊IO鎿? 浣?#37510;?#35120;read璇?#23033;?#37928;?#37825;?#37817;埌杈?#37827;讹紝鐢?#37712;?#37837;?#29840;熻矗璇?#37721;杝ocket涓?#37928;?#37825;?#37817;紝骞?#37712;?#37711;敤鎴?#37816;囧畾鐨勭紦鍐?#37718;?#28051;?#37510;?#37832;鍚?#37712;?#37837;?#28751;read鐨?#37825;?#37817;?#37724;岀敤鎴风?#36328;?#23339;敞鍐?#37928;? CompletionHandler鍒嗗彂缁?#37712;?#38318;Proactor?#32029;Proactor灏咺O瀹屾垚鐨勪俊鎭?#38315;氱煡缁欑敤鎴风?#36328;锛?#28051;鑸?#38315;?#26473;囪皟?#25956;?#25956;鎴风?#36328;?#23339;敞鍐?#37928;勫?#23678;垚浜?#28000;? 澶勭悊鍑?#37825;?#38171;夛紝瀹屾垚寮?#23005;IO銆?/span></p><p><span>?#25956;鎴风?#36328;浣跨敤寮?#23005;IO妯?#37736;?#37928;勪吉?#21804;鐮佹弿杩?#28051;?#38171;?/span></p><br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000FF; ">void</span><span style="color: #000000; "> UserCompletionHandler::handle_event(buffer) {<br />    process(buffer);<br />}<br /><br />{<br />    aio_read(socket, </span><span style="color: #0000FF; ">new</span><span style="color: #000000; "> UserCompletionHandler);<br />}</span></div><p><span>?#25956;鎴烽渶瑕侀噸鍐機ompletionHandler鐨刪andle_event鍑?#37825;?#26473;涜澶勭悊鏁?#37817;?#37928;勫伐浣滐紝鍙?#37825;癰uffer琛?#32448;Proactor宸?#32513;?#37713;?#28598;囧鐨?#37825;?#37817;紝?#25956;鎴风?#36328;鐩?#37818;皟?#25956;鍐?#37837;?#37819;愪緵鐨勫紓姝IO API?#32029;骞?#28751;嗛噸鍐?#37928;凜ompletionHandler娉?#37712;屽嵆?#24434;銆?/span></p><p><span>鐩? 姣?#27996;嶪O澶?#29882;?#28598;嶇敤妯?#37736;嬶紝寮?#23005;IO骞?#28051;?#37719;?#37714;嗗父?#25956;?#32029;涓嶅皯楂?#37804;?#38003;藉?#36346;彂鏈嶅姟绋嬪簭浣跨敤IO澶?#29882;?#28598;嶇敤妯?#37736;?澶?#32510;跨浠诲姟澶勭悊鐨勬?#33333;?#21227;熀鏈彲浠?#23114;?#29906;抽渶姹?#37510;?#37712;?#28051;?#37929;墠鎿?#28003;?#32495;?#32513;?#28725;? 寮?#23005;IO鐨勬敮?#23508;骞?#38344;?#37911;?#37714;?#28729;屽杽?#32029;鏇村鐨?#37828;?#38322;囩敤IO澶?#29882;?#28598;嶇敤妯?#37736;嬫鎷?#23534;?#23005;IO鐨勬柟寮?#38171;圛O浜?#28000;惰?#24386;鏃?#28051;?#37929;?#37818;?#38315;氱煡?#25956;鎴风?#36328;?#23350;紝鑰屾槸灏?#37825;?#37817;?#29831;?#37712;欏?#23678;瘯鍚庢?#24807;埌?#25956;鎴?#37816;囧畾鐨? ?#32038;鍐?#37718;?#28051;?#38171;?#37510;侸ava7涔嬪悗宸?#32513;忔敮鎸佷簡寮?#23005;IO?#32029;鎰?#37711;磋叮鐨?#29831;?#38000;呭彲浠皾璇?#28003;跨敤銆?/span></p><p><span>鏈?#37826;囦粠鍩?#37832;?#22994;?#36423;?#37510;?#23480;?#28003;?#23092;佺?#23338;?#23676;唬鐮佺 渚?#28051;?#28051;?#28766;傛?#30045;瑕佹弿杩颁簡?#29238;瑙?#37928;勫洓绉嶉珮鎬兘IO妯?#37736;?#37928;?#32513;撴?#21227;?#23677;?#29114;?#21997;紝鐞嗘竻?#31777;鍚屾銆?#23534;?#23005;?#37510;?#38339;诲銆?#38344;為?#35826;杩欎?#28056;鏄撴?#38155;穯鐨勬蹇?#37510;?#38315;?#26473;囧楂?#37804;兘IO妯?#37736;?#37928;勭悊瑙紝?#24434;浠湪鏈? 鍔?#32468;?#32459;嬪簭鐨勫紑?#24386;涓?#38315;夋嫨鏇寸鍚堝疄闄?#28051;?#37716;?#37911;圭偣鐨処O妯?#37736;嬶紝鎻?#26946;?#37832;嶅姟璐噺銆傚笇鏈?#37832;?#37826;囧浣犳湁?#22669;甯?#37716;?#37510;?/span></p><p><br /><span></span></p><p><span>鐩?#28028;?#37928;?#38171;?br />http://www.cnblogs.com/nufangrensheng/p/3588690.html<br />http://www.ibm.com/developerworks/cn/linux/l-async/<br /></span></p><img src ="http://www.8634070.com/DLevin/aggbug/427118.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.8634070.com/DLevin/" target="_blank">DLevin</a> 2015-09-04 15:16 <a href="http://www.8634070.com/DLevin/archive/2015/09/04/427118.html#Feedback" target="_blank" style="text-decoration:none;">?#24386;琛?#29831;勮</a></div>]]></description></item><item><title>Netty3鏋舵?#21230;鏋?/title><link>http://www.8634070.com/DLevin/archive/2015/09/04/427031.html</link><dc:creator>DLevin</dc:creator><author>DLevin</author><pubDate>Fri, 04 Sep 2015 01:40:00 GMT</pubDate><guid>http://www.8634070.com/DLevin/archive/2015/09/04/427031.html</guid><wfw:comment>http://www.8634070.com/DLevin/comments/427031.html</wfw:comment><comments>http://www.8634070.com/DLevin/archive/2015/09/04/427031.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.8634070.com/DLevin/comments/commentRss/427031.html</wfw:commentRss><trackback:ping>http://www.8634070.com/DLevin/services/trackbacks/427031.html</trackback:ping><description><![CDATA[<h2>?#22688;璁?/h2>寰?#37827;?#28000;墠?#27688;鏈?#29831;籒etty婧?#37934;?#37928;?#37813;?#32480;椾簡?#32029;?#21159;鑰岀涓娆皾璇?#37928;?#37827;?#37706;欎粠Netty4?#32017;濮嬶紝涓鐩存姄涓嶅埌鏍?#36423;?#37928;勬?#21976;?#33333;?#20346;?#23350;紝鍚庢潵鍥?#28051;?#37711;?#28000;?#27996;嬫儏?#32342;?#28483;?#27688;鏀?#28051;嬩簡銆?#26473;欐瓒佺潃浼戝?#22252;?#23942;柊鎹捣杩?#28051;?#32429;?#26976;?#28598;达紝鍥?#28051;篘etty3鐜板湪杩樺湪琚?#23536;?#28598;?#26916;?#37929;?#28003;跨敤?#32029;鍥?#38000;?#26473;欐鍐冲畾鍏堜粠Netty3鍏?#37813;嬶紝?#28763;闂村彂鐜癗etty3鐨勪唬鐮?#23011;擭etty4涓?#29785;?#28051;煩鐨勫?#27710;紝寰?#28598;氭蹇靛湪?#21804;鐮?#37832;?#38892;?#28051;?#38318;?#37832;夋竻鏅?#37928;勮杈撅紝?#22669;浠?#37719;婂?#27688;?#22968;鏁?#28051;?#22935;嗘灦鐨勯鏋舵?#28852;竻妤氫簡銆?#37712;?#29831;?a href="http://netty.io/wiki/new-and-noteworthy-in-4.0.html">Netty4瀵筃etty3鐨勬敼杩?#37804;?#32513;?/a>?#32029;鍥炲幓璇籒etty4鐨勬簮鐮侊紝鍙?#38000;?#29785;?#23536;?#26462;?#37833;句簡?#32029;涓绉嶈?#20346;劧?#32017;鏈?#37928;勬?#29115;銆?br /><br />璁?#23536;楀幓骞?#29831;籎etty婧?#37934;?#37928;?#37827;?#37706;欙紝鍥?#28051;轰唬鐮?#28598;?#25652;?#28598;紝骞?#28051;?#38007;?#23480;?#37928;凥TTP Server鐨勪簡瑙?#28598;?#28751;戯紝鍥?#38000;屽彧鑳借嚜搴?#37722;?#28051;?#37928;?#28051;涓?#28051;涓?#22959;?#37735;?#37928;勫?#29362;姞?#32029;鐩村埌鏈鍚庢妸?#22669;浠?#37928;勬鍧?#26473;?#37818;湪涓?#25443;鑰岀?#23339;竻?#30048;鐨?#37930;?#23005;?#37837;?#36423;?#26976;?#37835;?#37510;?#37916;板湪璇?#23143;?#37934;侊紝?#32017;濮?#28052;犳鍏堟妸楠?#37835;剁?#21976;竻?#32029;?#21159;鍚庡?#26421;?#31295;埌涓嶅悓鐨勫櫒瀹?#37510;佽鑲?#38000;岀?#23339;竻鏁?#28051;?#27996;?#28003;?#37510;?br /><br />鏈?#37826;囦粠Reactor妯?#23534;忓湪Netty3涓?#37928;勫簲?#25956;?#32029;寮?#37713;篘etty3鐨?#37825;?#28003;撴?#33333;瀯浠?#37721;?#37818;?#37714;舵?#20346;锛涚劧鑰岄櫎?#31777;Reactor妯?#23534;忥紝Netty3杩樺湪ChannelPipeline涓?#28003;跨敤?#31777;<a href="http://www.8634070.com/DLevin/archive/2015/09/03/427086.html">Intercepting Filter</a>妯?#23534;忥紝杩?#28051;?#22959;?#23534;?#28052;熷湪Servlet鐨凢ilter涓?#37812;?#37716;?#28003;跨敤?#32029;鍥?#38000;?#37832;枃杩?#28028;氫粠Intercepting Filter妯?#23534;?#37713;哄彂璇?#32513;?#28000;嬬粛ChannelPipeline鐨勮璁悊蹇?#37510;?#37832;?#37826;囧?#22250;璇?#38000;?#23480;?#32513;?#28725;筃etty鏈?#28051;?#30078;鐨勪簡瑙紝鍥?#38000;?#28051;嶄細鍖?#37722;?#26473;囧鍏?#38338;?#28000;嬬?#23951;紝浠?#37721;婂?#29958;etty鍋?#28729;?#28028;?#37928;勬?#22247;瓧銆?br /><h2>Netty3涓?#37928;凴eactor妯?#23534;?/h2>Reactor妯?#23534;忓湪Netty涓?#25652;旂敤闈炲父鎴?#37716;燂紝鍥?#38000;屽畠涔?#37828;湪Netty涓?#37721;楀鑲?#28729;?#28028;?#37928;勬寮忥紝鍏?#27996;嶳eactor妯?#23534;忓彲浠?#29831;?#32513;?#37721;?#38000;?#37832;?#27996;?#37928;勫彟涓?#30226;鏂囩珷<a href="http://www.8634070.com/DLevin/archive/2015/09/02/427045.html">銆奟eactor妯?#23534;?#29831;?#29785;?#37510;?/a>?#32029;瀵筊eactor妯?#23534;?#37928;勫疄鐜版槸Netty3鐨勫熀鏈?#26976;?#37835;讹紝鍥?#38000;?#37832;?#28751;?#38010;備細璇?#32513;?#28000;嬬粛Reactor妯?#23534;?#28641;?#28003;?#25652;旂敤Netty3涓?#37510;?br /><br />濡傛灉璇?#37510;奟eactor妯?#23534;?#29831;?#29785;?#37510;嬶紝鎴戜滑?#29025;閬Reactor妯?#23534;?#37922;盚andle銆丼ynchronous Event Demultiplexer銆両nitiation Dispatcher銆丒vent Handler銆丆oncrete Event Handler鏋勬垚?#32029;?#28266;Java鐨勫疄鐜?#37911;?#37832;?#28051;紝Channel瀵?#25652;擧andle?#32029;Selector瀵?#25652;Synchronous Event Demultiplexer?#32029;骞?#28051;擭etty3杩?#28003;跨敤?#31777;涓眰Reactor锛歁ain Reactor?#25956;浜?#28598;勭悊Client鐨?#26473;?#37818;?#29831;?#23033;傦紝Sub Reactor?#25956;浜?#28598;勭悊?#25328;Client杩?#37818;?#37722;?#37928;?#29831;?#37712;?#29831;?#23033;?#38171;?#37711;?#27996;?#26473;?#28051;?#22994;?#36423;?#26473;樺彲浠?#37721;?#38000;僁oug Lea鐨?#26473;欑瘒PPT锛?a href="http://gee.cs.oswego.edu/dl/cpjslides/nio.pdf">Scalable IO In Java</a>锛?#37510;傛墍浠?#37812;戜滑鍏堣瑙?#37712;砃etty3涓?#28003;跨敤浠涔堢被瀹?#37916;版墍鏈?#37928;?#28051;?#26473;版鍧楀?#33333;妸浠?#28000;?#38001;?#32495;诲湪涓?#25443;鐨勶紝浠NIO瀹?#37916;版柟寮?#28051;轰緥锛?br /><img alt="" src="http://www.8634070.com/images/blogjava_net/dlevin/Netty3_Architecture.png" /><br />妯?#23534;?#37828;?#28051;绉嶆?#20511;薄?#32029;浣?#37828;湪瀹?#37916;?#28051;紝缁忓父?#32048;鍥?#28051;?#29831;?#29815;鐗?#37804;?#37510;佹?#21976;灦鍜屾兘?#28214;瑕?#38000;屽仛涓浜涙敼鍙橈紝鍥?#38000;孨etty3瀵筊eactor妯?#23534;?#37928;勫疄鐜?#37832;?#28051;濂?#38007;?#23480;?#37928;勮璁?#38171;?br /><strong>1. ChannelEvent锛?/strong>Reactor鏄?#37737;?#27996;?#27996;?#28000;剁紪绋?#37928;勶紝鍥?#38000;屽湪Netty3涓?#28003;跨敤ChannelEvent鎶借薄鐨勮杈綨etty3鍐?#38318;彲浠?#27996;?#37922;?#37928;勫悇绉?#27996;?#28000;讹紝?#22669;鏈?#26473;欎簺浜?#28000;?#28725;硅薄?#28266;Channels甯?#37716;被涓?#27996;?#37922;燂紝骞?#28051;?#37922;卞畠灏?#27996;?#28000;?#37818;?#37711;埌ChannelPipeline涓紝ChannelPipeline鏋勫缓ChannelHandler绠?#38316;擄紝ChannelEvent娴?#32513;?#26473;?#28051;?#32480;?#38316;撳疄鐜版墍鏈?#37928;?#28051;?#37716;?#38315;昏緫澶勭悊銆侰hannelEvent瀵?#25652;?#37928;?#27996;?#28000;?#37832;?#38171;欳hannelStateEvent琛?#32448;篊hannel?#23032;鎬?#37928;勫?#27194;寲浜?#28000;讹紝鑰屽?#20635;灉褰撳墠Channel瀛樺湪Parent Channel?#32029;鍒?#29831;?#27996;?#28000;?#26473;?#28028;氫紶閫掑埌Parent Channel鐨凜hannelPipeline涓紝濡侽PEN銆丅OUND銆丆ONNECTED銆両NTEREST_OPS绛夛紝璇?#27996;?#28000;跺彲浠湪鍚?#32457;?#28051;嶅?#23677;疄鐜?#37928;凜hannel銆丆hannelSink涓?#27996;?#37922;?#38171;汳essageEvent琛?#32448;轰粠Socket涓?#29831;?#37721;?#37825;?#37817;?#28729;屾垚銆侀渶瑕佸悜Socket鍐?#37825;?#37817;?#37812;朇hannelHandler瀵?#35120;撳墠Message瑙?#37835;?濡侱ecoder銆丒ncoder)鍚?#29785;彂鐨?#27996;?#28000;讹紝?#30048;鐢盢ioWorker銆侀渶瑕?#28725;筂essage鍋?#26473;?#28051;姝?#28598;勭悊鐨凜hannelHandler浜?#37922;?#38171;沇riteCompletionEvent琛?#32448;?#37712;欏?#23678;垚鑰?#29785;彂鐨?#27996;?#28000;讹紝?#30048;鐢盢ioWorker浜?#37922;?#38171;汦xceptionEvent琛?#32448;哄湪澶勭悊杩囩涓?#37713;?#37916;?#37928;凟xception?#32029;?#30048;?#24434;浠彂鐢熷湪鍚?#28051;?#37835;?#28000;?#28051;紝濡侰hannel銆丆hannelSink銆丯ioWorker銆丆hannelHandler涓?#38171;汭dleStateEvent鐢盜dleStateHandler瑙彂?#32029;杩?#28052;?#37828;?#28051;涓狢hannelEvent?#24434;浠?#37827;?#32514;?#37813;?#28766;?#37928;勪?#23338;瓙銆?#23049;?#38171;氬湪Netty4鍚庯紝宸?#32513;忔病鏈塁hannelEvent?#34987;?#32029;?#22669;鏈?#28051;嶅悓浜?#28000;?#38318;界敤瀵?#25652;?#37826;规硶琛?#26440;撅紝杩?#28052;?#37808;忓懗杩機hannelEvent涓嶅彲鎵?#28766;曪紝Netty4閲囩敤?#28266;ChannelInboundHandler涓姞鍏userEventTriggered()鏂规硶?#28533;瀹?#37916;?#26473;?#32457;?#37813;?#28766;曪紝?#21503;浣撳彲浠?#37721;?#38000;?a href="http://netty.io/wiki/new-and-noteworthy-in-4.0.html">杩欓噷</a>銆?br /><strong>2. ChannelHandler锛?/strong>?#28266;Netty3涓紝ChannelHandler?#25956;浜?#29723;?#32448;篟eactor妯?#23534;?#28051;?#37928;凟ventHandler銆侰hannelHandler鍙?#37828;?#28051;涓?#37837;囪?#29256;帴鍙紝?#30048;鏈?#28051;?#28051;?#28699;?#37818;?#37721;?#38171;欳hannelDownstreamHandler?#25328;ChannelUpstreamHandler?#32029;鍏?#28051;瑿hannelDownstreamHandler琛?#32448;轰粠?#25956;鎴?#25652;旂敤绋嬪?#24532;?#20344;悜Netty3鍐?#38318;?#37929;村埌鍚慡ocket鍐?#37825;?#37817;?#37928;勭閬擄紝?#28266;Netty4涓?#37824;?#37722;?#28051;篊hannelOutboundHandler锛汣hannelUpstreamHandler琛?#32448;?#37825;?#37817;粠Socket杩涘叆Netty3鍐?#38318;?#37722;戠敤鎴?#25652;旂敤绋嬪簭鍋?#37825;?#37817;?#28598;勭悊鐨勭閬擄紝?#28266;Netty4涓?#37824;?#37722;?#28051;篊hannelInboundHandler銆?br /><strong>3. ChannelPipeline锛?/strong>?#25956;浜庣?#24714;ChannelHandler鐨勭閬擄紝姣?#28051;狢hannel涓涓狢hannelPipeline瀹?#28186;嬶紝?#24434;浠?#26473;?#29723;?#26473;囩涓?#37716;?#37804;?#37928;勫悜杩?#28051;?#32480;?#38316;?#28051;?#23075;诲姞銆?#37714;?#38340;ChannelHandler锛?#37922;?#27996;庡疄鐜?#37928;勯檺鍒讹紝?#28266;鏈鏈?#32468;?#37928;凜hannelHandler鍚戝?#24226;?#35826;?#29363;垨鍒?#38340;ChannelHandler涓?#28051;?#30078;?#28266;褰撳墠鎵?#29723;屾?#20346;涓捣鏁堬紝鍙?#38000;?a href="http://netty.io/3.10/api/org/jboss/netty/channel/ChannelPipeline.html">杩欓噷</a>锛?#37510;侰hannelPipeline鍐?#38318;?#32513;存姢涓涓狢hannelHandler鐨勫?#23677;悜閾捐?#32029;?#30048;浠Upstream(Inbound)鏂?#37722;?#28051;烘鍚戯紝Downstream(Outbound)鏂?#37722;?#28051;烘柟鍚?#37510;侰hannelPipeline閲囩敤Intercepting Filter妯?#23534;忓疄鐜帮紝?#21503;浣撳彲浠?#37721;?#38000;?a href="http://www.8634070.com/DLevin/archive/2015/09/03/427086.html">杩欓噷</a>?#32029;杩?#28051;?#22959;?#23534;?#37928;勫疄鐜板湪鍚?#28051;鑺?#28051;?#26473;?#37828;?#29831;?#32513;?#28000;嬬粛銆?br /><strong>4. NioSelector锛?/strong>Netty3浣跨敤NioSelector?#28533;瀛樻斁Selector锛圫ynchronous Event Demultiplexer锛夛紝姣?#28051;?#37826;?#27996;?#37922;?#37928;凬IO Channel閮藉悜杩?#28051;猄elector娉?#37712;?#38007;?#23480;?#28000;?#29825;?#26473;?#28051;猄elector鐩戝惉杩?#28051;狽IO Channel涓彂鐢?#37928;?#27996;?#28000;讹紝褰?#27996;?#28000;跺彂鐢?#37827;讹紝?#30367;?#25956;甯?#37716;被Channels涓?#37928;勬?#35268;硶鐢?#37812;ChannelEvent瀹?#28186;嬶紝灏?#29831;?#27996;?#28000;跺彂閫佸埌杩?#28051;狽etty Channel瀵?#25652;?#37928;凜hannelPipeline涓紝鑰?#27996;?#32513;?#37722;勭骇ChannelHandler澶勭悊銆?#37711;?#28051;湪鍚慡elector娉?#37712;孨IO Channel鏃讹紝Netty Channel瀹?#28186;?#28000;Attachment鐨勫舰寮?#28028;?#37711;紝璇Netty Channel?#28266;鍏?#37712;?#38318;?#37928;凬IO Channel浜?#28000;跺彂鐢?#37827;讹紝?#32048;浠Attachment鐨勫舰寮忓?#27194;湪浜嶴electionKey涓紝鍥?#38000;屾瘡涓?#27996;?#28000;跺彲浠?#37929;?#37818;粠杩?#28051;狝ttachment涓?#38014;?#37721;?#37929;?#37711;抽摼鐨凬etty Channel?#32029;骞朵粠Netty Channel涓?#38014;?#37721;?#28051;?#28052;?#37929;?#37711;宠仈鐨凜hannelPipeline?#32029;杩?#28051;?#28729;?#37916;板拰Doug Lea鐨?a href="http://gee.cs.oswego.edu/dl/cpjslides/nio.pdf">Scalable IO In Java</a>涓妯?#28051;鏍?#37510;?#37721;?#28598;朜etty3杩?#38322;囩敤?#31777;<a href="http://gee.cs.oswego.edu/dl/cpjslides/nio.pdf">Scalable IO In Java</a>涓?#37929;?#37722;?#37928;凪ain Reactor?#25328;Sub Reactor璁?#29825;紝鍏?#28051;璑ioSelector鐨?#28051;?#28051;?#28729;?#37916;?#38171;Boss鍗?#28051;篗ain Reactor?#32029;NioWorker涓篠ub Reactor銆侭oss?#25956;?#28533;澶勭?#21976;柊杩?#37818;姞鍏?#37928;?#27996;?#28000;讹紝NioWorker?#25956;?#28533;澶勭悊鍚?#28051;?#26473;?#37818;?#28725;筍ocket鐨?#29831;?#37712;欎簨浠讹紝鍏?#28051;Boss閫?#26473;NioWorkerPool鑾?#37721;朜ioWorker瀹?#28186;嬶紝Netty3妯?#23534;?#28003;跨敤RoundRobin鏂?#23534;忔?#24807;洖NioWorker瀹?#28186;?#37510;傛?#26449;舰?#34180;涓?#20579;鐨勶紝?#24434;浠?#38315;?#26473;?a href="http://gee.cs.oswego.edu/dl/cpjslides/nio.pdf">Scalable IO In Java</a>鐨?#26473;?#23534;?#37733;捐杈?#38171;?br /><img alt="" src="http://www.8634070.com/images/blogjava_net/dlevin/Netty_Multi_Reactors.jpg" height="436" width="632" /><br />鑻?#28051;嶳actor妯?#23534;?#28725;?#25652;旓紝NioSelector涓?#37718;?#37722;簡Synchronous Event Demultiplexer?#32029;鑰孋hannelPipeline涓?#32480;?#37918;嗙潃?#22669;鏈塃ventHandler?#32029;鍥?#38000;孨ioSelector?#25328;ChannelPipeline鍏?#37722;屾?#21228;?#24874;簡Initiation Dispatcher銆?br /><strong>5. ChannelSink锛?/strong>?#28266;ChannelHandler澶勭悊瀹屾垚?#22669;鏈?#38315;昏緫?#28214;瑕佸?#25117;鎴风鍐欏搷搴?#37825;?#37817;?#37827;讹紝涓鑸?#28028;氳皟?#25956;Netty Channel涓?#37928;剋rite鏂规硶?#32029;?#21159;鑰屽湪杩?#28051;write鏂规硶瀹?#37916;?#28051;紝?#30048;涓?#37828;?#37929;?#37818;?#37722;?#37711;?#37712;?#38318;?#37928;凷ocket鍐?#37825;?#37817;紝鑰屾槸浜?#32513;機hannels甯?#37716;被?#32029;鍐?#38318;?#37714;涘缓DownstreamMessageEvent?#32029;鍙嶅?#25116;粠ChannelPipeline鐨勭閬?#28051;?#23092;?#26473;囧幓?#32029;鐩村埌绗?#28051;涓狢hannelHandler澶勭悊瀹屾?#26346;紝鏈鍚?#27996;?#32513;機hannelSink澶勭?#21997;紝浠?#38316;?#37711;嶉?#35826;鍐?#38000;屽?#21342;?#23943;?#23338;簭鐨勫?#28850;悙?#22138;銆侰hannelSink灏?#26473;?#28051;狹essageEvent鎻?#27996;?#32513;橬etty Channel涓?#37928;剋riteBufferQueue?#32029;鏈鍚嶯ioWorker?#32048;绛夊埌杩?#28051;狽IO Channel宸?#32513;忓彲浠?#28598;勭悊鍐欎簨浠?#37827;?#37827;犻?#35826;鐨勫悜杩?#28051;狽IO Channel鍐?#37825;?#37817;?#37510;?#26473;欏氨鏄?#28051;婂浘鐨剆end鏄粠SubReactor鐩?#37818;?#37713;哄彂鐨勫師鍥?#37510;?br /><strong>6. Channel锛?/strong>Netty鏈夎嚜宸?#37928;凜hannel鎶借薄?#32029;?#30048;鏄?#28051;涓?#29863;勬簮鐨勫?#29808;櫒?#32029;鍖?#37722;簡?#22669;鏈?#28051;涓?#26473;?#37818;?#23057;?#37721;婂埌鐨勬墍鏈夎?#21228;簮鐨勯?#25956;?#32029;濡?#28751;佽NIO Channel銆丆hannelPipeline銆丅oss銆丯ioWorkerPool绛?#37510;?#37721;?#28598;栧畠杩樻?#24874;?#28055;簡鍚?#37712;?#38318;NIO Channel鍐欏搷搴?#37825;?#37817;?#37928;?#37818;?#37721;write銆?#26473;?#37818;?缁戝畾?#22476;鏌?#28051;湴?#28483;鐨刢onnect/bind鎺?#37721;?#32475;夛紝涓?#27996;烘?#29115;?#22798;?#30028;劧瀵笴hannel鏈?#38892;潵璇达紝鍥?#28051;哄畠灏佽?#21612;簡NIO Channel?#32029;鍥?#38000;?#26473;欎簺鎺?#37721;畾涔夊湪杩?#38322;屾槸鍚堢悊鐨勶紝浣?#37828;?#28641;傛灉鑰?#38095;戝埌Netty鐨勬?#33333;?#21238;紝?#30048;鐨凜hannel鍙?#37828;?#28051;涓?#29863;勬簮瀹瑰櫒?#32029;鏈?#26473;?#28051;狢hannel瀹?#28186;嬪氨?#24434;浠?#23536;楀埌鍜屽畠鐩?#37711;?#37928;勫熀鏈墍鏈夎?#21228;?#24886;紝鍥?#38000;?#26473;?#32457;峸rite銆乧onnect銆乥ind鍔?#28003;?#28051;?#25652;?#29831;?#37712;嶇?#21342;畠璐熻矗?#32029;鑰屾槸搴?#29831;?#37922;?#37711;?#28000;栫被?#28533;璐熻矗?#32029;姣斿?#20634;湪Netty4涓氨?#28266;ChannelHandlerContext娣诲姞?#31777;write鏂规硶?#32029;铏界劧netty4骞舵病鏈?#37714;?#38340;Channel涓?#37928;剋rite鎺?#37721;?#37510;?br /><h2>Netty3涓?#37928;処ntercepting Filter妯?#23534;?/h2>濡傛灉璇碦eactor妯?#23534;?#37828;疦etty3鐨勯鏋讹紝?#20549;涔圛ntercepting Filter妯?#23534;?#37714;?#37828;疦etty鐨?#28051;?#37835;?#37510;俁eactor妯?#23534;?#28051;昏搴旂敤?#28266;Netty3鐨勫唴閮?#28729;?#37916;帮紝?#30048;鏄疦etty3?#21503;鏈夎濂芥兘鐨勫熀纭?#32029;鑰孖ntercepting Filter妯?#23534;?#37714;?#37828;疌hannelHandler缁勫?#22557;疄鐜?#28051;涓?#25652;旂敤绋嬪簭閫昏緫鐨勫熀纭?#32029;鍙?#37832;?#23536;?#28610;?#37928;勭悊瑙簡杩?#28051;?#22959;?#23534;忔墠?#20824;浣跨敤濂Netty?#32029;鐢氳?#23456;兘寰楀績搴?#37813;?#37510;?br /><br />鍏?#27996;嶪ntercepting Filter妯?#23534;?#37928;?#29831;?#32513;?#28000;嬬?#23941;彲浠?#37721;?#38000;?a href="http://www.8634070.com/DLevin/archive/2015/09/03/427086.html">杩欓噷</a>?#32029;鏈?#38010;?#28051;昏?#20343;?#23340;粛Netty3涓?#28725;笽ntercepting Filter妯?#23534;?#37928;勫疄鐜帮紝鍏跺?#28850;氨鏄DefaultChannelPipeline瀵笽ntercepting Filter妯?#23534;?#37928;勫疄鐜?#37510;傚湪涓婃枃鏈夋?#24876;埌Netty3鐨凜hannelPipeline鏄疌hannelHandler鐨勫?#29808;櫒?#32029;?#25956;浜?#28699;樺偍涓庣?#24714;ChannelHandler?#32029;鍚?#37827;跺畠?#28266;Netty3涓?#28052;熻捣?#22476;妗?#22986;?#37928;?#28003;滅敤?#32029;鍗冲畠鏄?#26473;?#37818;Netty3鍐?#38318;埌?#22669;鏈塁hannelHandler鐨勬姊?#37510;?#28003;?#28051;篊hannelPipeline鐨勫疄鐜?#38000;匘efaultChannelPipeline?#32029;?#30048;浣跨敤涓涓狢hannelHandler鐨勫?#23677;悜閾捐?#28533;瀛樺偍?#32029;浠DefaultChannelPipelineContext浣?#28051;鸿?#20636;偣锛?br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">interface</span><span style="color: #000000; "> ChannelHandlerContext {</span><span style="color: #000000;"><br />    Channel getChannel();</span><span style="color: #000000; "><br />    ChannelPipeline getPipeline();</span><span style="color: #000000; "><br />    String getName();</span><span style="color: #000000; "><br />    ChannelHandler getHandler();</span><span style="color: #000000; "><br />    </span><span style="color: #0000FF; ">boolean</span><span style="color: #000000; "> canHandleUpstream();</span><span style="color: #000000; "><br />    </span><span style="color: #0000FF; ">boolean</span><span style="color: #000000; "> canHandleDownstream();</span><span style="color: #000000; "><br />    </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> sendUpstream(ChannelEvent e);</span><span style="color: #000000; "><br />    </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> sendDownstream(ChannelEvent e);</span><span style="color: #000000; "><br />    Object getAttachment();</span><span style="color: #000000; "><br />    </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> setAttachment(Object attachment);<br />}<br /><br /></span><span style="color: #0000FF; ">private</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">final</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">class</span><span style="color: #000000; "> DefaultChannelHandlerContext </span><span style="color: #0000FF; ">implements</span><span style="color: #000000; "> ChannelHandlerContext {<br />    </span><span style="color: #0000FF; ">volatile</span><span style="color: #000000; "> DefaultChannelHandlerContext next;<br />    </span><span style="color: #0000FF; ">volatile</span><span style="color: #000000; "> DefaultChannelHandlerContext prev;<br />    </span><span style="color: #0000FF; ">private</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">final</span><span style="color: #000000; "> String name;<br />    </span><span style="color: #0000FF; ">private</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">final</span><span style="color: #000000; "> ChannelHandler handler;<br />    </span><span style="color: #0000FF; ">private</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">final</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">boolean</span><span style="color: #000000; "> canHandleUpstream;<br />    </span><span style="color: #0000FF; ">private</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">final</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">boolean</span><span style="color: #000000; "> canHandleDownstream;<br />    </span><span style="color: #0000FF; ">private</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">volatile</span><span style="color: #000000; "> Object attachment;<br />.....<br />}</span></div>?#28266;DefaultChannelPipeline涓紝?#30048;瀛樺偍?#31777;鍜屽?#25779;墠ChannelPipeline鐩?#37711;宠仈鐨凜hannel銆丆hannelSink浠?#37721;ChannelHandler閾捐鐨刪ead銆乼ail?#32029;?#22669;鏈塁hannelEvent閫?#26473;sendUpstream銆乻endDownstream涓?#37711;?#37721;?#23092;?#32513;?#37825;?#28051;?#38334;捐锛?br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">class</span><span style="color: #000000; "> DefaultChannelPipeline </span><span style="color: #0000FF; ">implements</span><span style="color: #000000; "> ChannelPipeline {<br />    </span><span style="color: #0000FF; ">private</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">volatile</span><span style="color: #000000; "> Channel channel;<br />    </span><span style="color: #0000FF; ">private</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">volatile</span><span style="color: #000000; "> ChannelSink sink;<br />    </span><span style="color: #0000FF; ">private</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">volatile</span><span style="color: #000000; "> DefaultChannelHandlerContext head;<br />    </span><span style="color: #0000FF; ">private</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">volatile</span><span style="color: #000000; "> DefaultChannelHandlerContext tail;<br />......<br />    </span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> sendUpstream(ChannelEvent e) {<br />        DefaultChannelHandlerContext head </span><span style="color: #000000; ">=</span><span style="color: #000000; "> getActualUpstreamContext(</span><span style="color: #0000FF; ">this</span><span style="color: #000000; ">.head);<br />        </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (head </span><span style="color: #000000; ">==</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">null</span><span style="color: #000000; ">) {</span><span style="color: #000000; "><br />            </span><span style="color: #0000FF; ">return</span><span style="color: #000000; ">;<br />        }<br />        sendUpstream(head, e);<br />    }<br /><br />    </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> sendUpstream(DefaultChannelHandlerContext ctx, ChannelEvent e) {<br />        </span><span style="color: #0000FF; ">try</span><span style="color: #000000; "> {<br />            ((ChannelUpstreamHandler) ctx.getHandler()).handleUpstream(ctx, e);<br />        } </span><span style="color: #0000FF; ">catch</span><span style="color: #000000; "> (Throwable t) {<br />            notifyHandlerException(e, t);<br />        }<br />    }<br /><br />    </span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> sendDownstream(ChannelEvent e) {<br />        DefaultChannelHandlerContext tail </span><span style="color: #000000; ">=</span><span style="color: #000000; "> getActualDownstreamContext(</span><span style="color: #0000FF; ">this</span><span style="color: #000000; ">.tail);<br />        </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (tail </span><span style="color: #000000; ">==</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">null</span><span style="color: #000000; ">) {<br />            </span><span style="color: #0000FF; ">try</span><span style="color: #000000; "> {<br />                getSink().eventSunk(</span><span style="color: #0000FF; ">this</span><span style="color: #000000; ">, e);<br />                </span><span style="color: #0000FF; ">return</span><span style="color: #000000; ">;<br />            } </span><span style="color: #0000FF; ">catch</span><span style="color: #000000; "> (Throwable t) {<br />                notifyHandlerException(e, t);<br />                </span><span style="color: #0000FF; ">return</span><span style="color: #000000;">;<br />            }<br />        }<br />        sendDownstream(tail, e);<br />    }<br /><br />    </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> sendDownstream(DefaultChannelHandlerContext ctx, ChannelEvent e) {<br />        </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (e </span><span style="color: #0000FF; ">instanceof</span><span style="color: #000000; "> UpstreamMessageEvent) {<br />            </span><span style="color: #0000FF; ">throw</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">new</span><span style="color: #000000; "> IllegalArgumentException(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">cannot send an upstream event to downstream</span><span style="color: #000000; ">"</span><span style="color: #000000; ">);<br />        }<br />        </span><span style="color: #0000FF; ">try</span><span style="color: #000000; "> {<br />            ((ChannelDownstreamHandler) ctx.getHandler()).handleDownstream(ctx, e);<br />        } </span><span style="color: #0000FF; ">catch</span><span style="color: #000000; "> (Throwable t) {</span><span style="color: #008000; "><br /></span><span style="color: #000000; ">            e.getFuture().setFailure(t);<br />            notifyHandlerException(e, t);<br />        }<br />    }<br /></span><span style="color: #000000; "></span></div>瀵筓pstream浜?#28000;讹紝鍚戝悗鎵惧埌?#22669;鏈夊疄鐜颁簡ChannelUpstreamHandler鎺?#37721;?#37928;凜hannelHandler缁勬垚?#25724;锛?span style="color: #000000; ">getActualUpstreamContext()锛?/span>?#32029;鑰屽?#31541;ownstream浜?#28000;讹紝鍚戝墠鎵惧埌?#22669;鏈夊疄鐜颁簡ChannelDownstreamHandler鎺?#37721;?#37928;凜hannelHandler缁勬垚?#25724;锛?span style="color: #000000; ">getActualDownstreamContext()</span>锛?#38171;?br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000; ">    </span><span style="color: #0000FF; ">private</span><span style="color: #000000; "> DefaultChannelHandlerContext getActualUpstreamContext(DefaultChannelHandlerContext ctx) {<br />        </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (ctx </span><span style="color: #000000; ">==</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">null</span><span style="color: #000000; ">) {<br />            </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">null</span><span style="color: #000000; ">;<br />        }<br />        DefaultChannelHandlerContext realCtx </span><span style="color: #000000; ">=</span><span style="color: #000000; "> ctx;<br />        </span><span style="color: #0000FF; ">while</span><span style="color: #000000; "> (</span><span style="color: #000000; ">!</span><span style="color: #000000; ">realCtx.canHandleUpstream()) {<br />            realCtx </span><span style="color: #000000; ">=</span><span style="color: #000000; "> realCtx.next;<br />            </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (realCtx </span><span style="color: #000000; ">==</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">null</span><span style="color: #000000; ">) {<br />                </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">null</span><span style="color: #000000; ">;<br />            }<br />        }<br />        </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> realCtx;<br />    }<br />    </span><span style="color: #0000FF; ">private</span><span style="color: #000000; "> DefaultChannelHandlerContext getActualDownstreamContext(DefaultChannelHandlerContext ctx) {<br />        </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (ctx </span><span style="color: #000000; ">==</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">null</span><span style="color: #000000; ">) {<br />            </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">null</span><span style="color: #000000; ">;<br />        }<br />        DefaultChannelHandlerContext realCtx </span><span style="color: #000000; ">=</span><span style="color: #000000; "> ctx;<br />        </span><span style="color: #0000FF; ">while</span><span style="color: #000000; "> (</span><span style="color: #000000; ">!</span><span style="color: #000000; ">realCtx.canHandleDownstream()) {<br />            realCtx </span><span style="color: #000000; ">=</span><span style="color: #000000; "> realCtx.prev;<br />            </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (realCtx </span><span style="color: #000000; ">==</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">null</span><span style="color: #000000; ">) {<br />                </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">null</span><span style="color: #000000; ">;<br />            }<br />        }<br />        </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> realCtx;<br />    }<br /></span></div>?#28266;瀹?#38340;?#28729;?#37916;癈hannelUpstreamHandler鎴朇hannelDownstreamHandler鏃讹紝?#30367;?#25956; ChannelHandlerContext涓?#37928;剆endUpstream鎴杝endDownstream鏂规硶灏?#37818;?#37714;舵?#20346;浜?#32513;?#28051;?#28051;涓? ChannelUpstreamHandler鎴?#28051;?#28051;涓狢hannelDownstreamHandler?#32029;鎴栬皟?#25956;Channel涓?#37928;剋rite鏂规硶?#24386;閫? 鍝?#25652;旀秷鎭?#37510;?br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">class</span><span style="color: #000000; "> MyChannelUpstreamHandler </span><span style="color: #0000FF; ">implements</span><span style="color: #000000; "> ChannelUpstreamHandler {<br />    </span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> handleUpstream(ChannelHandlerContext ctx, ChannelEvent e) </span><span style="color: #0000FF; ">throws</span><span style="color: #000000; "> Exception {<br />        </span><span style="color: #008000; ">//</span><span style="color: #008000; "> handle current logic, use Channel to write response if needed.<br />        </span><span style="color: #008000; ">//</span><span style="color: #008000; "> ctx.getChannel().write(message);</span><span style="color: #008000; "><br /></span><span style="color: #000000; ">        ctx.sendUpstream(e);<br />    }<br />}<br /><br /></span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">class</span><span style="color: #000000; "> MyChannelDownstreamHandler </span><span style="color: #0000FF; ">implements</span><span style="color: #000000; "> ChannelDownstreamHandler {<br />    </span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> handleDownstream(<br />            ChannelHandlerContext ctx, ChannelEvent e) </span><span style="color: #0000FF; ">throws</span><span style="color: #000000; "> Exception {<br />        </span><span style="color: #008000; ">//</span><span style="color: #008000; "> handle current logic</span><span style="color: #008000; "><br /></span><span style="color: #000000; ">        ctx.sendDownstream(e);<br />    }<br />}</span></div>褰揅hannelHandler鍚慍hannelPipelineContext?#24386;閫?#27996;?#28000;?#37827;讹紝鍏?#37712;?#38318;粠褰撳墠ChannelPipelineContext鑺傜偣鍑哄彂鎵惧埌涓?#28051;涓狢hannelUpstreamHandler鎴朇hannelDownstreamHandler瀹?#28186;嬶紝骞?#37722;?#37711;跺彂閫丆hannelEvent?#32029;瀵?#27996;嶥ownstream閾撅紝濡傛灉?#22476;杈?#38334;惧?#25733;紝鍒?#28751;咰hannelEvent?#24386;閫?#32513;機hannelSink锛?br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> sendDownstream(ChannelEvent e) {<br />    DefaultChannelHandlerContext prev </span><span style="color: #000000; ">=</span><span style="color: #000000; "> getActualDownstreamContext(</span><span style="color: #0000FF; ">this</span><span style="color: #000000; ">.prev);<br />    </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (prev </span><span style="color: #000000; ">==</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">null</span><span style="color: #000000; ">) {<br />        </span><span style="color: #0000FF; ">try</span><span style="color: #000000; "> {<br />            getSink().eventSunk(DefaultChannelPipeline.</span><span style="color: #0000FF; ">this</span><span style="color: #000000; ">, e);<br />        } </span><span style="color: #0000FF; ">catch</span><span style="color: #000000; "> (Throwable t) {<br />            notifyHandlerException(e, t);<br />        }<br />    } </span><span style="color: #0000FF; ">else</span><span style="color: #000000; "> {<br />        DefaultChannelPipeline.</span><span style="color: #0000FF; ">this</span><span style="color: #000000; ">.sendDownstream(prev, e);<br />    }<br />}<br /><br /></span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> sendUpstream(ChannelEvent e) {<br />    DefaultChannelHandlerContext next </span><span style="color: #000000; ">=</span><span style="color: #000000; "> getActualUpstreamContext(</span><span style="color: #0000FF; ">this</span><span style="color: #000000; ">.next);<br />    </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (next </span><span style="color: #000000; ">!=</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">null</span><span style="color: #000000; ">) {<br />        DefaultChannelPipeline.</span><span style="color: #0000FF; ">this</span><span style="color: #000000; ">.sendUpstream(next, e);<br />    }<br />}</span></div>姝?#37828;?#37733;?#28051;?#26473;?#28051;?#28729;?#37916;帮紝濡傛灉?#28266;涓涓?#37832;?#28751;?#37928;凜hannelUpstreamHandler涓?#37711;?#32457;婚櫎鑷?#23480;憋紝?#28266;鍚?#37832;?#28751;炬?#35826;姞涓涓?#37826;?#37928;凜hannelUpstreamHandler?#32029;?#30048;鏄?#37827;犳晥鐨勶紝鍥?#28051;哄畠鐨刵ext宸?#32513;忓湪?#30367;?#25956;?#22688;?#27688;鍥哄?#27699;缃?#28051;null?#31777;銆?br /><br />ChannelPipeline浣?#28051;篊hannelHandler鐨勫?#29808;櫒?#32029;?#30048;杩樻?#24874;?#28055;簡鍚?#32457;嶅銆?#37714;?#37510;佹?#31540;hannelHandler閾捐涓?#37928;勬?#35268;硶?#32029;鑰?#28051;斿?#20635;灉鏌?#28051;狢hannelHandler杩樺疄鐜颁簡LifeCycleAwareChannelHandler?#32029;鍒?#29831;ChannelHandler?#28266;琚?#23075;诲姞杩汣hannelPipeline鎴栦粠涓?#37714;?#38340;?#37827;?#38318;戒細寰楀埌鍚屽織锛?br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">interface</span><span style="color: #000000; "> LifeCycleAwareChannelHandler </span><span style="color: #0000FF; ">extends</span><span style="color: #000000; "> ChannelHandler {<br />    </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> beforeAdd(ChannelHandlerContext ctx) </span><span style="color: #0000FF; ">throws</span><span style="color: #000000; "> Exception;<br />    </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> afterAdd(ChannelHandlerContext ctx) </span><span style="color: #0000FF; ">throws</span><span style="color: #000000; "> Exception;<br />    </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> beforeRemove(ChannelHandlerContext ctx) </span><span style="color: #0000FF; ">throws</span><span style="color: #000000; "> Exception;<br />    </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> afterRemove(ChannelHandlerContext ctx) </span><span style="color: #0000FF; ">throws</span><span style="color: #000000; "> Exception;<br />}<br /><br /></span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">interface</span><span style="color: #000000; "> ChannelPipeline {</span><span style="color: #000000; "><br />    </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> addFirst(String name, ChannelHandler handler);</span><span style="color: #000000; "><br />    </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> addLast(String name, ChannelHandler handler);</span><span style="color: #000000; "><br />    </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> addBefore(String baseName, String name, ChannelHandler handler);</span><span style="color: #000000; "><br />    </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> addAfter(String baseName, String name, ChannelHandler handler);</span><span style="color: #000000; "><br />    </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> remove(ChannelHandler handler);</span><span style="color: #000000; "><br />    ChannelHandler remove(String name);</span><span style="color: #000000; "><br />    </span><span style="color: #000000; "><</span><span style="color: #000000; ">T </span><span style="color: #0000FF; ">extends</span><span style="color: #000000; "> ChannelHandler</span><span style="color: #000000; ">></span><span style="color: #000000; "> T remove(Class</span><span style="color: #000000; "><</span><span style="color: #000000; ">T</span><span style="color: #000000; ">></span><span style="color: #000000; "> handlerType);</span><span style="color: #000000; "><br />    ChannelHandler removeFirst();</span><span style="color: #000000; "><br />    ChannelHandler removeLast();</span><span style="color: #000000; "><br />    </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> replace(ChannelHandler oldHandler, String newName, ChannelHandler newHandler);</span><span style="color: #000000; "><br />    ChannelHandler replace(String oldName, String newName, ChannelHandler newHandler);</span><span style="color: #000000; "><br />    </span><span style="color: #000000; "><</span><span style="color: #000000; ">T </span><span style="color: #0000FF; ">extends</span><span style="color: #000000; "> ChannelHandler</span><span style="color: #000000; ">></span><span style="color: #000000; "> T replace(Class</span><span style="color: #000000; "><</span><span style="color: #000000; ">T</span><span style="color: #000000; ">></span><span style="color: #000000; "> oldHandlerType, String newName, ChannelHandler newHandler);</span><span style="color: #000000; "><br />    ChannelHandler getFirst();</span><span style="color: #000000; "><br />    ChannelHandler getLast();</span><span style="color: #000000; "><br />    ChannelHandler get(String name);</span><span style="color: #000000; "><br />    </span><span style="color: #000000; "><</span><span style="color: #000000; ">T </span><span style="color: #0000FF; ">extends</span><span style="color: #000000; "> ChannelHandler</span><span style="color: #000000; ">></span><span style="color: #000000; "> T get(Class</span><span style="color: #000000; "><</span><span style="color: #000000; ">T</span><span style="color: #000000; ">></span><span style="color: #000000; "> handlerType);</span><span style="color: #000000; "><br />    ChannelHandlerContext getContext(ChannelHandler handler);</span><span style="color: #000000; "><br />    ChannelHandlerContext getContext(String name);</span><span style="color: #000000; "><br />    ChannelHandlerContext getContext(Class</span><span style="color: #000000; "><?</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">extends</span><span style="color: #000000; "> ChannelHandler</span><span style="color: #000000; ">></span><span style="color: #000000; "> handlerType);</span><span style="color: #000000; "><br />    </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> sendUpstream(ChannelEvent e);</span><span style="color: #000000; "><br />    </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> sendDownstream(ChannelEvent e);</span><span style="color: #000000; "><br />    ChannelFuture execute(Runnable task);</span><span style="color: #000000; "><br />    Channel getChannel();</span><span style="color: #000000; "><br />    ChannelSink getSink();</span><span style="color: #000000; "><br />    </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> attach(Channel channel, ChannelSink sink);</span><span style="color: #000000; "><br />    </span><span style="color: #0000FF; ">boolean</span><span style="color: #000000; "> isAttached();</span><span style="color: #000000; "><br />    List</span><span style="color: #000000; "><</span><span style="color: #000000; ">String</span><span style="color: #000000; ">></span><span style="color: #000000; "> getNames();</span><span style="color: #000000; "><br />    Map</span><span style="color: #000000; "><</span><span style="color: #000000; ">String, ChannelHandler</span><span style="color: #000000; ">></span><span style="color: #000000; "> toMap();<br />}<br /></span></div><br />?#28266;DefaultChannelPipeline鐨凜hannelHandler?#25724;鏉?#37928;勫?#21229;?#21976;?#20346;涓?#38171;?br /><img alt="" src="http://www.8634070.com/images/blogjava_net/dlevin/Netty_ChannelPipeline.png" height="409" width="968" /><br /><h2>鍙?#38000;?#38171;?/h2><a href="http://netty.io/index.html">銆奛etty涓婚銆?/a><br /><a href="http://ifeve.com/netty-reactor-4/">銆奛etty婧?#37934;?#29785;?#29831;?#38171;?#37733;?#38171;塏etty涓嶳eactor妯?#23534;?#37510;?/a><br /><a href="http://jm-blog.aliapp.com/?p=423">銆奛etty?#21804;鐮?#37714;嗘瀽銆?/a><br /><a href="http://gee.cs.oswego.edu/dl/cpjslides/nio.pdf">Scalable IO In Java</a><br /><a href="http://www.oracle.com/technetwork/java/interceptingfilter-142169.html">Intercepting Filter Pattern</a><br /><a href="http://gee.cs.oswego.edu/dl/cpjslides/nio.pdf"></a><img src ="http://www.8634070.com/DLevin/aggbug/427031.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.8634070.com/DLevin/" target="_blank">DLevin</a> 2015-09-04 09:40 <a href="http://www.8634070.com/DLevin/archive/2015/09/04/427031.html#Feedback" target="_blank" style="text-decoration:none;">?#24386;琛?#29831;勮</a></div>]]></description></item><item><title>Intercepting Filter妯?#23534;?#29831;?#29785;?/title><link>http://www.8634070.com/DLevin/archive/2015/09/03/427086.html</link><dc:creator>DLevin</dc:creator><author>DLevin</author><pubDate>Thu, 03 Sep 2015 14:14:00 GMT</pubDate><guid>http://www.8634070.com/DLevin/archive/2015/09/03/427086.html</guid><wfw:comment>http://www.8634070.com/DLevin/comments/427086.html</wfw:comment><comments>http://www.8634070.com/DLevin/archive/2015/09/03/427086.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.8634070.com/DLevin/comments/commentRss/427086.html</wfw:commentRss><trackback:ping>http://www.8634070.com/DLevin/services/trackbacks/427086.html</trackback:ping><description><![CDATA[<h2>?#26870;棰樻弿杩?/h2>?#28266;鏈嶅姟鍣?#32514;?#32459;?#28051;紝閫氬父?#28214;瑕?#28598;勭悊澶?#32457;?#28051;嶅悓鐨?#29831;?#23033;傦紝?#28266;姝?#23534;?#28598;勭悊璇?#23033;?#28052;嬪墠?#32029;?#28214;瑕?#28725;?#29831;?#23033;?#37707;?#28051;浜?#26864;勫?#21229;?#21997;紝濡?#38171;?br /><ol><li>绾?#35120;?#23011;?#28051;狢lient鐨勬?#24532;璁块棶?#20426;鎭?#37510;?/li><li>瀵笴lient杩涜?#23683;璇佸?#23678;?#22559;潈妫?#29025;锛圓uthentication and Authorization锛?#37510;?/li><li>妫?#29025;褰撳墠Session鏄?#37722;?#37722;堟硶銆?/li><li>妫?#29025;Client鐨処P?#28276;?#28483;鏄?#37722;彲?#20426;璧?#37812;?#28051;嶅彲?#20426;璧?#38171;圛P?#28276;?#28483;鐧藉悕鍗?#37510;?#27035;戝悕鍗?#38171;?#37510;?/li><li>璇?#23033;?#37825;?#37817;?#37828;?#37722;?#37711;堣瑙?#37720;嬫垨瑙?#37934;?#37510;?/li><li>鏄?#37722;敮?#23508;Client璇?#23033;?#37928;勭被鍨?#37510;丅rowser鐗?#37832;?#32475;?#37510;?/li><li>娣诲?#29363;兘鐩?#37818;俊鎭?#37510;?/li><li>娣诲姞?#30367;璇曚俊鎭?#37510;?/li><li>淇?#29831;佹墍鏈?#23534;傚父閮借姝?#32429;?#37817;曡?#23792;埌?#32029;瀵?#37832;?#26864;勬?#27407;埌鐨勫紓?#29238;鍋?#38315;氱敤澶勭?#21997;紝闃?#23005;?#32513;機lient鐪嬪埌鍐?#38318;?#37739;?#37837;堜俊鎭?#37510;?br /></li></ol><p>?#28266;鍝?#25652;?#26473;?#37733;?#32513;欏鎴风涔嬪墠?#32029;鏈?#37827;?#37706;?#28052;熼渶瑕佸仛涓浜?#26864;勫?#21229;悊鍐?#26473;?#37733;?#38171;?/p><ol><li>瀵?#37725;?#25652;旀秷鎭?#32514;?#37934;?#37812;栧?#23340;缉銆?/li><li>涓烘墍鏈夊搷搴旀?#35826;姞鍏?#37711;?#28598;?#37510;佸熬绛夋秷鎭?#37510;?/li><li>杩?#28051;姝Enrich鍝?#25652;旀秷鎭紝濡傛?#35826;姞鍏?#37711;卞瓧娈?#37510;丼ession?#20426;鎭?#37510;丆ookie?#20426;鎭紝鐢氳嚦瀹屽叏鏀?#37721;樺搷搴旀秷鎭?#32475;?#37510;?/li></ol>濡?#28003;?#28729;?#37916;?#26473;?#37837;?#37928;勯渶姹傦紝鍚?#37827;朵?#28615;?#20344;彲鎵?#28766;?#37804;?#37510;佸彲?#22136;?#25956;鎬?#37510;佸彲?#21412;缃?#37510;佺?#32472;鎬?#38171;?br /><h2>?#26870;棰?#29785;?#37712;?/h2>瑕佸疄鐜?#26473;?#32457;嶉渶姹傦紝鏈鐩?#29785;?#37928;勬?#35268;硶?#27688;鏄湪姣?#28051;?#29831;?#23033;?#28598;勭悊杩囩涓?#23075;诲?#29363;墍鏈?#26473;欎簺閫昏緫?#32029;涓轰簡鍑?#28751;戜唬鐮侀噸澶嶏紝?#24434;浠?#28751;嗘墍鏈?#26473;欎?#28057;?#29025;鎻?#37721;?#37812;?#37826;规硶?#32029;杩?#37837;峰湪姣?#28051;?#28598;勭?#21976;?#35268;硶涓皟?#25956;鍗冲彲锛?br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000FF; ">public</span><span style="color: #000000; "> Response service1(Request request) {<br />    validate(request);<br />    request </span><span style="color: #000000; ">=</span><span style="color: #000000; "> transform(request);<br />    Response response </span><span style="color: #000000; ">=</span><span style="color: #000000; "> process1(request);<br />    </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> transform(response);<br />}</span></div>姝?#37827;讹紝濡傛灉鍑?#37916;皊ervice2鏂规硶?#32029;渚濈劧?#28214;瑕?#37815;?#29840;漵ervice1涓?#37928;勫疄鐜帮紝?#21159;鍚?#28751;唒rocess1鎹?#37812;process2鍗冲彲銆?#26473;?#28051;?#37827;?#37706;?#37812;戜滑?#24386;鐜?#23536;?#28598;氶噸澶嶄唬鐮侊紝缁?#32513;?#28725;瑰畠閲嶆?#21238;紝姣斿?#20635;彁鍙?#37711;?#37711;?#38315;昏緫?#22476;鍩虹被鎴愭鐗堟?#35268;硶?#32029;杩?#32457;?#28003;跨敤缁?#37813;跨殑鏂?#23534;忎細寮曡捣瀛愮被瀵圭?#21057;被鐨?#38000;?#37722;堬紝濡傛灉瑕?#29825;?#37836;?#27996;涙鍧楀彉鐨勫彲?#21412;缃渶瑕?#37832;?#28598;?#28598;?#37928;勫垽鏂?#38315;昏緫?#32029;?#21804;鐮?#37721;?#37928;?#38007;?#38002;?#38171;涘洜鑰屽彲浠洿杩?#28051;姝紝灏嗘墍鏈?#28598;勭悊閫昏緫鎶借薄鍑?#28051;涓狿rocessor鎺?#37721;紝?#21159;鍚?#28003;跨敤Decorate妯?#23534;?#38171;?#37719;?#23534;曠敤浼?#27996;庣户鎵?#38171;?#38171;?br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">interface</span><span style="color: #000000; "> Processor {<br />    Response process(Request request);<br />}<br /></span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">class</span><span style="color: #000000; "> CoreProcessor </span><span style="color: #0000FF; ">implements</span><span style="color: #000000; "> Processor {<br />    </span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> Response process(Request request) {<br />        </span><span style="color: #008000; ">//</span><span style="color: #008000; "> do process/calculation</span><span style="color: #008000; "><br /></span><span style="color: #000000; ">    }<br />}<br /></span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">class</span><span style="color: #000000; "> DecoratedProcessor </span><span style="color: #0000FF; ">implements</span><span style="color: #000000; "> Processor {<br />    </span><span style="color: #0000FF; ">private</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">final</span><span style="color: #000000; "> Processor innerProcessor;<br />    </span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> DecoratedProcessor(Processor processor) {<br />        </span><span style="color: #0000FF; ">this</span><span style="color: #000000; ">.innerProcessor </span><span style="color: #000000; ">=</span><span style="color: #000000; "> processor;<br />    }<br /><br />    </span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> Response process(Request request) {<br />        request </span><span style="color: #000000; ">=</span><span style="color: #000000; "> preProcess(request);<br />        Response response </span><span style="color: #000000; ">=</span><span style="color: #000000; "> innerProcessor.process(request);<br />        response </span><span style="color: #000000; ">=</span><span style="color: #000000; "> postProcess(response);<br />        </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> response;<br />    }<br /><br />    </span><span style="color: #0000FF; ">protected</span><span style="color: #000000; "> Request preProcess(Request request) {<br />        </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> request;<br />    }<br />    </span><span style="color: #0000FF; ">protected</span><span style="color: #000000; "> Response postProcess(Response response) {<br />        </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> response;<br />    }<br />}<br /><br /></span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> Transformer </span><span style="color: #0000FF; ">extends</span><span style="color: #000000; "> DecoratedProcessor {<br />    </span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> Transformer(Processor processor) {<br />        </span><span style="color: #0000FF; ">super</span><span style="color: #000000; ">(processor);<br />    }<br /><br />    </span><span style="color: #0000FF; ">protected</span><span style="color: #000000; "> Request preProcess(Request request) {<br />        </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> transformRequest(request);<br />    }<br />    </span><span style="color: #0000FF; ">protected</span><span style="color: #000000; "> Response postProcess(Response response) {<br />        </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> transformResponse(response);<br />    }<br />}</span></div>姝?#37827;讹紝濡傛灉?#28214;瑕佸湪鐪?#23005;?#37928;勫?#21229;悊閫昏?#25116;?#23338;墠?#23006;鍏?#37711;?#28000;?#37928;勯?#21227;?#21229;悊閫昏緫?#32029;鍙渶瑕?#32513;?#37813;緿ecoratedProcessor?#32029;瀹?#37916;皃reProcess鎴杙ostProcess鏂规硶?#32029;鍒?#37714;湪璇?#23033;?#28598;勭悊涔嬪墠?#25328;璇?#23033;?#28598;勭悊涔嬪?#24226;鍚?#37714;囧叆涓浜?#38315;昏緫?#32029;涔熷氨鏄墍璋?#37928;凙OP缂?#32459;?#38171;?#38344;?#37722;?#37714;?#38344;?#37928;勭紪绋嬶紝?#21159;鍚?#37721;渶瑕?#37837;?#37817;渶姹傛?#21227;缓杩?#28051;摼鏉?#38171;?br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000; ">Processor processor </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">new</span><span style="color: #000000; "> MissingExceptionCatcher(</span><span style="color: #0000FF; ">new</span><span style="color: #000000; "> Debugger(</span><span style="color: #0000FF; ">new</span><span style="color: #000000; "> Transformer(</span><span style="color: #0000FF; ">new</span><span style="color: #000000; "> CoreProcessor());<br />Response response </span><span style="color: #000000; ">=</span><span style="color: #000000; "> processor.process(request);<br />......</span></div>杩?#23480;?#32513;?#37828;?#37929;?#28725;?#23011;?#26440;?#28610;?#37928;勮璁簡?#32029;姣?#28051;狿rocessor鍙渶瑕佸叧娉?#38007;?#23480;?#37928;勫疄鐜?#38315;昏緫鍗冲彲?#32029;?#21804;鐮?#37721;?#37928;勭畝娲?#38171;涘苟涓?#23011;?#28051;狿rocessor鍚?#38007;嫭绔嬶紝?#24434;?#22136;?#25956;鎬?#28610;斤紝娴?#29831;?#37826;逛究锛?#37825;?#37833;摼涓婅?#34249;疄鐜?#37928;勫?#29115;?#34249;彧鏄?#37721;?#37712;?#27996;庨摼鐨勬?#21231;狅紝鍥?#38000;屽彧?#28214;瑕?#37832;?#28051;绉嶆?#35268;硶?#21412;缃摼鐨勬?#21231;?#37719;冲彲?#32029;?#24434;?#21412;缃?#37804;?#28052;?#37721;?#23536;楃?#22443;椿锛涚劧鑰?#23536;?#28598;?#37827;?#37706;?#23534;曠敤鏄?#28051;绉?#38344;?#37804;?#37928;勪緷璧栵紝鑰?#37827;犳硶婊?#29906;?#37716;?#37804;?#37928;勯渶姹?#37510;?#29781;?#37835;勯?#26473;欐潯閾撅紝姣?#28051;墠缃甈rocessor?#28214;瑕佺煡閬撳叾鍚?#37928;凱rocessor?#32029;杩欏湪鏌?#27996;涙儏鍐?#28051;嬪苟涓?#37828;湪?#25443;鍒濆氨?#29025;閬?#37928;?#37510;?#23005;?#37827;讹紝鎴戜滑?#28214;瑕?#23534;?#37711;Intercepting Filter妯?#23534;忔潵瀹?#37916;板姩鎬?#37928;勬敼鍙?#37833;摼銆?br /><h2>Intercepting Filter妯?#23534;?/h2>?#28266;?#22688;鏂囧凡缁?#37835;勫?#36720;簡涓鏉?#37922;?#23534;曠敤鑰屾垚鐨凱rocessor閾撅紝?#21159;鑰?#26473;?#37828;?#28051;鏉?#38344;?#37804;侀?#25733;紝骞?#28051;旈渶瑕?#28051;?#32017;濮嬪氨鑳芥?#21231;?#37713;?#26473;欐潯閾撅紝涓轰簡瑙?#37712;?#26473;?#28051;?#38340;?#37714;讹紝鎴戜滑?#24434;浠?#23534;?#37711;?#28051;涓狿rocessorChain?#28533;缁存姢杩欐潯閾撅紝骞?#28051;?#26473;欐潯閾惧彲浠?#37716;?#37804;?#37928;勬?#21227;缓銆?br /><br />鏈?#28598;?#32457;嶆柟寮忓彲浠?#28729;?#37916;?#39582;?#37818;?#37714;?#26473;?#28051;摼锛?br /><ol><li>?#28266;瀛樺偍涓婏紝?#24434;浠?#28003;跨敤鏁?#32513;勬潵瀛樺偍?#22669;鏈?#37928;凱rocessor?#32029;Processor?#28266;鏁?#32513;?#28051;?#37928;?#28003;?#32515;?#29723;?#32448;?#26473;?#28051;狿rocessor?#28266;?#25724;鏉?#28051;?#37928;?#28003;?#32515;?#38171;?#28052;熷彲浠敤閾捐?#28533;瀛樺偍?#22669;鏈?#37928;凱rocessor?#32029;姝?#37827;禤rocessor?#28266;杩?#28051;?#38334;捐涓?#37928;?#28003;?#32515;?#37719;?#37828;湪?#25724;涓?#37928;?#28003;?#32515;?#37510;?/li><li>?#28266;鎶借薄涓婏紝?#24434;浠墍鏈?#37928;勯昏緫閮藉?#20349;?#21613;湪Processor涓紝涔熷彲浠?#28751;?#37837;?#36423;?#38315;昏緫浣跨敤Processor鎶借薄?#32029;鑰?#28598;?#37733;?#38315;昏緫浣跨敤Filter鎶借薄銆?/li><li>?#28266;娴佺鎺?#37714;?#28051;婏紝涓鑸?#38315;?#26473;囧湪Processor瀹?#37916;版?#35268;硶涓?#37929;?#37818;?#28003;跨敤ProcessorChain瀹?#28186;?閫?#26473;囧弬鏁版鍏??#28533;鎺?#37714;舵?#20346;?#23350;紝鍒敤鏂规硶?#30367;?#25956;鐨?#26473;?#37837;?#37713;烘爤鐨?#37911;?#37804;?#28729;?#37916;皃reProcess()?#25328;postProcess()澶勭悊銆?/li></ol>?#28266;瀹?#38340;?#28051;?#28003;跨敤杩?#28051;?#22959;?#23534;?#37928;?#37832;?#38171;歋ervlet鐨凢ilter鏈?#37714;?#37510;丯etty鐨凜hannelPipeline涓?#37510;丼tructs2涓?#37928;処nterceptor涓?#38318;藉疄鐜颁簡杩?#28051;?#22959;?#23534;?#37510;?br /><h2>Intercepting Filter妯?#23534;忓湪Servlet鐨凢ilter涓?#37928;勫疄鐜?#38171;圝etty鐗?#37832;?#38171;?/h2>鍏?#28051;璖ervlet鐨凢ilter?#28266;Jetty鐨勫疄鐜?#28051;?#28003;跨敤鏁?#32513;勫?#27194;偍Filter?#32029;Filter鏈?#28751;惧彲浠?#28003;跨敤Servlet瀹?#28186;嬪?#21229;悊鐪?#23005;?#37928;?#28051;?#37716;?#38315;昏緫?#32029;?#28266;娴佺鎺?#37714;?#28051;婏紝浣跨敤FilterChain鐨刣oFilter鏂规硶?#28533;瀹?#37916;?#37510;?#28641;FilterChain?#28266;Jetty涓?#37928;勫疄鐜?#38171;?br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000; "></span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> doFilter(ServletRequest request, ServletResponse response)</span><span style="color: #0000FF; "> throws</span><span style="color: #000000; "> IOException, ServletException </span><span style="color: #000000; "><br />    </span><span style="color: #008000; ">//</span><span style="color: #008000; "> pass to next filter</span><span style="color: #008000; "><br /></span><span style="color: #000000; ">    </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (_filter </span><span style="color: #000000; "><</span><span style="color: #000000; "> LazyList.size(_chain)) {<br />        FilterHolder holder</span><span style="color: #000000; ">=</span><span style="color: #000000; "> (FilterHolder)LazyList.get(_chain, _filter</span><span style="color: #000000; ">++</span><span style="color: #000000; ">);<br /></span><span style="color: #000000; ">        Filter filter</span><span style="color: #000000; ">=</span><span style="color: #000000; "> holder.getFilter();<br /></span><span style="color: #000000; ">        filter.doFilter(request, response, </span><span style="color: #0000FF; ">this</span><span style="color: #000000; ">);</span><span style="color: #000000; ">                    <br />        </span><span style="color: #0000FF; ">return</span><span style="color: #000000; ">;<br />    }<br /><br />    </span><span style="color: #008000; ">//</span><span style="color: #008000; "> Call servlet</span><span style="color: #008000; "><br /></span><span style="color: #000000; ">    HttpServletRequest srequest </span><span style="color: #000000; ">=</span><span style="color: #000000; "> (HttpServletRequest)request;<br />    </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (_servletHolder </span><span style="color: #000000; ">!=</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">null</span><span style="color: #000000; ">) {</span><span style="color: #000000; "><br />        _servletHolder.handle(_baseRequest,request, response);</span><span style="color: #000000; "><br />    }<br />}</span></div>杩欓噷?#32029;_chain瀹?#38340;?#28051;?#37828;?#28051;涓狥ilter鐨凙rrayList?#32029;鐢FilterChain?#30367;?#25956;doFilter()鍚?#37716;皟?#25956;绗?#28051;涓狥ilter鐨刣oFilter()鏂规硶?#32029;?#28266;瀹?#38340;?#37928;凢ilter瀹?#37916;?#28051;紝?#28214;瑕?#37813;嬪姩鐨勮皟?#25956;FilterChain.doFilter()鏂规硶?#28533;鍚?#37716;?#28051;?#28051;涓狥ilter鐨勮皟?#25956;?#32029;鍒敤鏂规硶?#30367;?#25956;鐨?#26473;?#37837;?#37713;烘爤鐨?#37911;?#37804;?#28729;?#37916;癛equest鐨刾re-process?#25328;Response鐨刾ost-process澶勭悊銆?#28641;傛灉涓嶈皟?#25956;FilterChain.doFilter()鏂规硶?#32029;鍒欒绀?#28051;嶉渶瑕佽皟?#25956;涔嬪悗鐨凢ilter?#32029;娴佺?#23337;粠褰撳墠Filter杩?#37733;烇紝?#28266;?#30048;涔嬪墠鐨凢ilter鐨凢ilterChain.doFilter()?#30367;?#25956;涔嬪悗鐨勯昏緫鍙嶅悜澶勭悊鐩村埌绗?#28051;涓狥ilter澶勭悊瀹屾垚鑰?#26473;?#37733;?#37510;?br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">class</span><span style="color: #000000; "> MyFilter </span><span style="color: #0000FF; ">implements</span><span style="color: #000000; "> Filter {<br />    </span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> doFilter(ServletRequest request, ServletResponse response, FilterChain chain) </span><span style="color: #0000FF; ">throws</span><span style="color: #000000; "> IOException, ServletException {<br />        </span><span style="color: #008000; ">//</span><span style="color: #008000; "> pre-process ServletRequest</span><span style="color: #008000; "><br /></span><span style="color: #000000; ">        chain.doFilter(request, response);<br />        </span><span style="color: #008000; ">//</span><span style="color: #008000; "> post-process Servlet Response</span><span style="color: #008000; "><br /></span><span style="color: #000000; ">    }<br />}</span></div>鏁?#28051;狥ilter?#25724;鐨勫?#21229;?#21976;?#20346;?#23338;涓?#38171;?br /><img src="http://www.8634070.com/images/blogjava_net/dlevin/Servlet_Filter.png" alt="" height="405" border="0" width="805" /><br /><h2>Intercepting Filter妯?#23534;忓湪Netty3涓?#37928;勫疄鐜?/h2>Netty3?#28266;DefaultChannelPipeline涓?#28729;?#37916;颁簡Intercepting Filter妯?#23534;忥紝鍏?#28051;瑿hannelHandler鏄畠鐨凢ilter銆傚湪Netty3鐨凞efaultChannelPipeline涓紝浣跨敤涓涓?#28000;ChannelHandlerContext涓鸿?#20636;偣鐨勫?#23677;悜閾捐?#28533;瀛樺偍ChannelHandler?#32029;?#22669;鏈?#37928;勬鍒?#38344;?#38315;昏緫鍜屽疄闄?#28051;?#37716;?#38315;昏緫閮界敤ChannelHandler琛?#26440;撅紝?#28266;鎺?#37714;舵?#20346;涓?#28003;跨敤ChannelHandlerContext鐨剆endDownstream()?#25328;sendUpstream()鏂规硶?#28533;鎺?#37714;舵?#20346;銆?#28051;嶅悓浜嶴ervlet鐨凢ilter?#32029;ChannelHandler鏈?#28051;?#28051;?#28699;?#37818;?#37721;?#38171;欳hannelUpstreamHandler?#25328;ChannelDownstreamHandler鍒?#37714;敤?#28533;璇?#23033;?#26473;涘叆鏃?#37928;勫?#21229;?#21976;?#20346;?#23338;?#23677;搷搴?#37713;哄幓鏃?#37928;勫?#21229;?#21976;?#20346;銆?#28725;?#27996;嶤lient鐨?#29831;?#23033;傦紝?#31904;DefaultChannelPipeline鐨剆endUpstream()鏂规硶鍏?#37721;?#38171;?br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> sendDownstream(ChannelEvent e) {<br />    DefaultChannelHandlerContext tail </span><span style="color: #000000; ">=</span><span style="color: #000000; "> getActualDownstreamContext(</span><span style="color: #0000FF; ">this</span><span style="color: #000000; ">.tail);<br />    </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (tail </span><span style="color: #000000; ">==</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">null</span><span style="color: #000000; ">) {<br />        </span><span style="color: #0000FF; ">try</span><span style="color: #000000; "> {<br />            getSink().eventSunk(</span><span style="color: #0000FF; ">this</span><span style="color: #000000; ">, e);<br />            </span><span style="color: #0000FF; ">return</span><span style="color: #000000; ">;<br />        } </span><span style="color: #0000FF; ">catch</span><span style="color: #000000; "> (Throwable t) {<br />            notifyHandlerException(e, t);<br />            </span><span style="color: #0000FF; ">return</span><span style="color: #000000; ">;<br />        }<br />    }<br />    sendDownstream(tail, e);<br />}<br /></span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> sendDownstream(DefaultChannelHandlerContext ctx, ChannelEvent e) {<br />    </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (e </span><span style="color: #0000FF; ">instanceof</span><span style="color: #000000; "> UpstreamMessageEvent) {<br />        </span><span style="color: #0000FF; ">throw</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">new</span><span style="color: #000000; "> IllegalArgumentException(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">cannot send an upstream event to downstream</span><span style="color: #000000; ">"</span><span style="color: #000000; ">);<br />    }<br />    </span><span style="color: #0000FF; ">try</span><span style="color: #000000; "> {<br />        ((ChannelDownstreamHandler) ctx.getHandler()).handleDownstream(ctx, e)<br />     } </span><span style="color: #0000FF; ">catch</span><span style="color: #000000; "> (Throwable t) {</span><span style="color: #008000; "><br /></span><span style="color: #000000; ">        e.getFuture().setFailure(t);<br />        notifyHandlerException(e, t);<br />    }<br />}</span></div>濡傛灉鏈夊搷搴旀秷鎭紝璇?#23057;?#37805;粠DefaultChannelPipeline鐨剆endDownstream()鏂规硶涓?#37711;?#37721;?#38171;?br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> sendUpstream(ChannelEvent e) {<br />    DefaultChannelHandlerContext head </span><span style="color: #000000; ">=</span><span style="color: #000000; "> getActualUpstreamContext(</span><span style="color: #0000FF; ">this</span><span style="color: #000000; ">.head);<br />    </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (head </span><span style="color: #000000; ">==</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">null</span><span style="color: #000000; ">) {<br /></span><span style="color: #000000; ">        </span><span style="color: #0000FF; ">return</span><span style="color: #000000; ">;<br />    }<br />    sendUpstream(head, e);<br />}<br /></span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> sendUpstream(DefaultChannelHandlerContext ctx, ChannelEvent e) {<br />    </span><span style="color: #0000FF; ">try</span><span style="color: #000000; "> {<br />        ((ChannelUpstreamHandler) ctx.getHandler()).handleUpstream(ctx, e);<br />    } </span><span style="color: #0000FF; ">catch</span><span style="color: #000000; "> (Throwable t) {<br />        notifyHandlerException(e, t);<br />    }<br />}</span></div>?#28266;瀹?#38340;?#28729;?#37916;癈hannelUpstreamHandler鎴朇hannelDownstreamHandler鏃讹紝?#30367;?#25956;ChannelHandlerContext涓?#37928;剆endUpstream鎴杝endDownstream鏂规硶灏?#37818;?#37714;舵?#20346;浜?#32513;?#28051;?#28051;涓狢hannelUpstreamHandler鎴?#28051;?#28051;涓狢hannelDownstreamHandler?#32029;鎴栬皟?#25956;Channel涓?#37928;剋rite鏂规硶?#24386;閫佸搷搴旀秷鎭?#37510;?br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">class</span><span style="color: #000000; "> MyChannelUpstreamHandler </span><span style="color: #0000FF; ">implements</span><span style="color: #000000; "> ChannelUpstreamHandler {<br />    </span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> handleUpstream(ChannelHandlerContext ctx, ChannelEvent e) </span><span style="color: #0000FF; ">throws</span><span style="color: #000000; "> Exception {<br />        </span><span style="color: #008000; ">//</span><span style="color: #008000; "> handle current logic, use Channel to write response if needed.<br />        </span><span style="color: #008000; ">//</span><span style="color: #008000; "> ctx.getChannel().write(message);</span><span style="color: #008000; "><br /></span><span style="color: #000000; ">        ctx.sendUpstream(e);<br />    }<br />}<br /><br /></span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">class</span><span style="color: #000000; "> MyChannelDownstreamHandler </span><span style="color: #0000FF; ">implements</span><span style="color: #000000; "> ChannelDownstreamHandler {<br />    </span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> handleDownstream(<br />            ChannelHandlerContext ctx, ChannelEvent e) </span><span style="color: #0000FF; ">throws</span><span style="color: #000000; "> Exception {<br />        </span><span style="color: #008000; ">//</span><span style="color: #008000; "> handle current logic</span><span style="color: #008000; "><br /></span><span style="color: #000000; ">        ctx.sendDownstream(e);<br />    }<br />}</span></div>褰揅hannelHandler鍚慍hannelPipelineContext?#24386;閫?#27996;?#28000;?#37827;讹紝鍏?#37712;?#38318;粠褰撳墠ChannelPipelineContext 鑺傜偣鍑哄彂鎵惧埌涓?#28051;涓狢hannelUpstreamHandler鎴朇hannelDownstreamHandler瀹?#28186;嬶紝骞?#37722;?#37711;跺彂閫? ChannelEvent?#32029;瀵?#27996;嶥ownstream閾撅紝濡傛灉?#22476;杈?#38334;惧?#25733;紝鍒?#28751;咰hannelEvent?#24386;閫?#32513;機hannelSink锛?br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> sendDownstream(ChannelEvent e) {<br />    DefaultChannelHandlerContext prev </span><span style="color: #000000; ">=</span><span style="color: #000000; "> getActualDownstreamContext(</span><span style="color: #0000FF; ">this</span><span style="color: #000000; ">.prev);<br />    </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (prev </span><span style="color: #000000; ">==</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">null</span><span style="color: #000000; ">) {<br />        </span><span style="color: #0000FF; ">try</span><span style="color: #000000; "> {<br />            getSink().eventSunk(DefaultChannelPipeline.</span><span style="color: #0000FF; ">this</span><span style="color: #000000; ">, e);<br />        } </span><span style="color: #0000FF; ">catch</span><span style="color: #000000; "> (Throwable t) {<br />            notifyHandlerException(e, t);<br />        }<br />    } </span><span style="color: #0000FF; ">else</span><span style="color: #000000; "> {<br />        DefaultChannelPipeline.</span><span style="color: #0000FF; ">this</span><span style="color: #000000; ">.sendDownstream(prev, e);<br />    }<br />}<br /><br /></span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> sendUpstream(ChannelEvent e) {<br />    DefaultChannelHandlerContext next </span><span style="color: #000000; ">=</span><span style="color: #000000; "> getActualUpstreamContext(</span><span style="color: #0000FF; ">this</span><span style="color: #000000; ">.next);<br />    </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (next </span><span style="color: #000000; ">!=</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">null</span><span style="color: #000000; ">) {<br />        DefaultChannelPipeline.</span><span style="color: #0000FF; ">this</span><span style="color: #000000; ">.sendUpstream(next, e);<br />    }<br />}</span></div>姝?#37828;?#37733;?#28051;?#26473;?#28051;?#28729;?#37916;帮紝濡傛灉?#28266;涓涓?#37832;?#28751;?#37928;凜hannelUpstreamHandler涓?#37711;?#32457;婚櫎鑷?#23480;憋紝?#28266;鍚?#37832;?#28751;炬?#35826;姞涓涓?#37826;?#37928;凜hannelUpstreamHandler?#32029;?#30048;鏄?#37827;犳晥鐨勶紝鍥?#28051;哄畠鐨刵ext宸?#32513;忓湪?#30367;?#25956;?#22688;?#27688;鍥哄?#27699;缃?#28051;null?#31777;銆?br /><br />?#28266;DefaultChannelPipeline鐨凜hannelHandler?#25724;鏉?#37928;勫?#21229;?#21976;?#20346;涓?#38171;?br /><img alt="" src="http://www.8634070.com/images/blogjava_net/dlevin/Netty_ChannelPipeline.png" height="409" width="968" /><br />?#28266;杩?#28051;?#28729;?#37916;?#28051;紝涓嶅?#24288;ervlet鐨凢ilter瀹?#37916;?#37714;敤鏂规硶?#30367;?#25956;鏍?#37928;?#26473;涘?#28888;爤?#28533;瀹屾垚pre-process?#25328;post-process?#32029;鑰屾槸?#28266;杩涘幓鐨勯?#24807;?#23677;?#28888;潵鐨勯?#24807;悇鑷皟?#25956;handleUpstream()?#25328;handleDownstream()鏂规硶?#32029;杩?#37837;蜂細寮曡捣?#30367;?#25956;鏍?#37711;跺疄鏄?#28051;?#37833;摼鐨?#37804;诲拰?#32029;鍥?#38000;岄渶瑕?#23049;?#37808;?#26473;欐潯?#25724;鐨?#37804;婚暱搴?#37510;?#26473;?#37837;?#37707;?#37928;勫?#34249;鏄?#26473;欐潯ChannelHandler鐨勯摼涓嶄緷璧?#27996;庢?#35268;硶?#30367;?#25956;鏍堬紝鑰屾槸?#28266;DefaultChannelPipeline鍐?#38318;?#37832;?#38892;?#37928;勯?#25733;紝鍥?#38000;屽湪handleUpstream()鎴杊andleDownstream()?#24434;浠?#38341;?#37827;?#28751;?#37813;?#29723;屾?#20346;杞彂缁?#37711;?#28000;?#32510;跨?#23339;垨绾跨?#23339;?#29381;紝鍙渶瑕佷?#28616;暀ChannelPipelineContext寮曠敤?#32029;?#28266;澶勭悊瀹屾垚鍚庣敤杩?#28051;狢hannelPipelineContext閲嶆柊鍚?#26473;欐潯?#25724;鐨勫悗涓涓?#38010;傜偣?#24386;閫丆hannelEvent?#32029;?#21159;鑰岀敱浜嶴ervlet鐨凢ilter渚?#29863;?#27996;庢?#35268;硶鐨勮皟?#25956;鏍堬紝鍥?#38000;屾?#35268;硶杩?#37733;炴?#24531;?#23815;潃?#22669;鏈?#37813;?#29723;屽?#23678;垚?#32029;杩?#32457;?#38340;?#37714;跺湪寮?#23005;?#32514;?#32459;?#28051;細寮曡捣?#26870;棰橈紝鍥?#38000;孲ervlet?#28266;3.0鍚?#23534;?#37711;簡Async鐨勬敮?#23508;銆?br /><h2>Intercepting Filter妯?#23534;?#37928;勭?#34425;偣</h2>?#30045;鍗?#37819;?#28051;涓?#26473;?#28051;?#22959;?#23534;?#37928;勭?#34425;偣锛?br />1. 鐩?#28725;?#28028;?#32513;?#37928;勭紪绋嬫鍨嬶紝杩?#28051;?#22959;?#23534;?#37832;?#28051;?#30078;鐨勫涔犳绾匡紝?#28214;瑕?#23536;?#28610;?#37928;勭悊瑙?#29831;?#22959;?#23534;?#37722;庢墠?#20824;鐏垫椿鐨勫簲?#25956;?#30048;?#28533;缂?#32459;?#37510;?br />2. ?#28214;瑕?#37714;?#37714;?#28051;嶅悓鐨勯昏緫?#22476;涓嶅悓鐨凢ilter涓紝杩?#37832;?#27996;?#37827;?#37706;?#39582;?#28051;?#37828;偅涔堝鏄?#37510;?br />3. 鍚?#28051;狥ilter涔?#38338;村叡浜?#37825;?#37817;?#28751;?#37721;?#23536;楀洶闅?#37510;傚湪Netty3涓彲浠?#38007;畾涔夎嚜宸?#37928;凜hannelEvent?#28533;瀹?#37916;拌嚜?#30078;涔夋秷鎭?#37928;勪紶杈擄紝鎴?#38000;?#28003;跨敤ChannelPipelineContext鐨凙ttachment瀛?#23048;垫潵瀹?#37916;版秷鎭?#28028;?#26440;擄紝鑰孲ervlet涓?#37928;凢ilter鍒欐病鏈夋?#24874;?#28058;?#35763;技鐨?#37832;?#37714;讹紝濡傛灉涓?#37828;彲浠厤缃?#37928;?#37825;?#37817;湪Config涓?#28028;?#38315;掞紝鍏?#28000;?#37827;?#37706;?#37928;?#37825;?#37817;?#37711;?#27996;渶瑕佸叾浠?#37832;?#37714;?#38320;嶅?#22557;?#23678;垚銆?br /><h2>鍙?#38000;?/h2><a href="http://www.oracle.com/technetwork/java/interceptingfilter-142169.html">Core J2EE Pattern - Intercepting Filter</a><img src ="http://www.8634070.com/DLevin/aggbug/427086.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.8634070.com/DLevin/" target="_blank">DLevin</a> 2015-09-03 22:14 <a href="http://www.8634070.com/DLevin/archive/2015/09/03/427086.html#Feedback" target="_blank" style="text-decoration:none;">?#24386;琛?#29831;勮</a></div>]]></description></item><item><title>Reactor妯?#23534;?#29831;?#29785;?/title><link>http://www.8634070.com/DLevin/archive/2015/09/02/427045.html</link><dc:creator>DLevin</dc:creator><author>DLevin</author><pubDate>Wed, 02 Sep 2015 07:14:00 GMT</pubDate><guid>http://www.8634070.com/DLevin/archive/2015/09/02/427045.html</guid><wfw:comment>http://www.8634070.com/DLevin/comments/427045.html</wfw:comment><comments>http://www.8634070.com/DLevin/archive/2015/09/02/427045.html#Feedback</comments><slash:comments>5</slash:comments><wfw:commentRss>http://www.8634070.com/DLevin/comments/commentRss/427045.html</wfw:commentRss><trackback:ping>http://www.8634070.com/DLevin/services/trackbacks/427045.html</trackback:ping><description><![CDATA[<h2> ?#22688;璁?/h2> 绗?#28051;娆?#37722;埌Reactor妯?#23534;?#37828;?#28051;?#39582;村墠鐨勬煇涓?#37829;?#28051;婏紝涓涓?#28729;?#37721;嬬?#20346;劧?#31367;杩囨潵?#26870;鎴戜粈涔?#37828;疪eactor妯?#23534;?#38171;熸垜涓?#32515;戞煡?#31777;涓涓嬶紝寰?#28598;?#27996;?#38318;芥槸缁?#37713;篘IO涓?#37928;? Selector鐨勪?#23338;?#24886;紝鑰?#28051;斿氨鏄疦IO?#22135;Selector澶?#29882;?#28598;嶇敤妯?#37736;嬶紝鍙?#37828;?#32513;欏畠?#25443;?#31777;涓涓?#23011;?#26440;僨ancy鐨勫?#23941;瓧鑰屽凡?#32029;铏界劧?#30048;寮?#37711;簡EventLoop姒? 蹇碉紝杩?#28725;规垜?#28533;璇?#37828;?#37826;?#37928;勬蹇碉紝浣?#37828;唬鐮佸疄鐜?#37719;?#37828;?#28051;鏍?#37928;勶紝鍥?#38000;屾垜骞舵病鏈?#23536;堝湪鎰?#26473;?#28051;?#22959;?#23534;?#37510;傜劧鑰?#37832;杩戝紑濮?#29831;籒etty婧?#37934;侊紝鑰孯eactor妯?#23534;?#37828;?#23536;?#28598;?#28000;嬬粛Netty鐨勬?#22249;珷涓?#29722;?#28598;?#38002;?#28729;?#28028;?#37928;勬寮忥紝鍥?#38000;屾垜鍐嶆?#26870;鑷?#23480;憋紝浠涔?#37828;疪eactor妯?#23534;?#38171;?#37832;?#37826;囧氨鏄?#28725;?#26473;?#28051;棶棰?#37711;?#27996;庢垜鐨?#28051;浜涚悊瑙?#37724;屽皾璇曠潃?#28533;瑙?#32475;?#37510;?br /> <br /> <h2>浠涔?#37828;疪eactor妯?#23534;?/h2> 瑕?#37733;?#32475;?#26473;?#28051;棶棰橈紝棣?#37711;?#35120;撶劧鏄?#23033;傚姪Google鎴朩ikipedia?#32029;鍏?#28051;璚ikipedia涓?#29831;?#38171;?#8220;The reactor design pattern is an event handling pattern for handling service requests delivered concurrently by one or more inputs. The service handler then demultiplexes the incoming requests and dispatches them synchronously to associated request handlers.”銆備粠杩?#28051;弿杩?#28051;紝鎴戜滑?#29025;閬Reactor妯?#23534;?#26851;?#37711;?#37828;?strong>浜?#28000;堕?#21342;姩鐨勶紝鏈?#28051;涓?#37812;?#28598;?#28051;?#39582;跺彂杈撳叆婧愶紝鏈?#28051;涓猄ervice Handler?#32029;鏈?#28598;?#28051;猂equest Handlers</strong>锛?#26473;?#28051;猄ervice Handler?#32048;鍚屾鐨勫皢杈撳叆鐨?#29831;?#23033;?#38171;圗vent锛?#28598;?#29882;?#28598;嶇敤鐨勫垎?#24386;缁?#37929;?#25652;?#37928;凴equest Handler銆?#28641;傛灉?#25956;鍥炬潵琛?#26440;?#38171;?br /> <img alt="" src="http://www.8634070.com/images/blogjava_net/dlevin/Reactor_Simple.png" height="247" width="429" /><br /> ?#31904;缁撴瀯涓婏紝杩?#37832;夌偣绫讳技鐢?#27996;?#38000;呮?#22563;垂鑰呮寮忥紝鍗?#37832;?#28051;涓?#37812;?#28598;?#28051;?#37922;?#27996;?#38000;?#28751;?#27996;?#28000;舵?#24807;叆涓涓猀ueue涓紝鑰?#28051;涓?#37812;?#28598;?#28051;?#23057;堣垂鑰?#28051;诲姩鐨勪粠杩?#28051;猀ueue涓璓oll浜?#28000;舵潵澶勭悊锛?#38000;孯eactor妯?#23534;?#37714;?#39582;舵病鏈Queue?#28533;鍋氱紦鍐Ԍ紝姣?#35120;?#28051;涓狤vent杈撳叆?#22476;Service Handler涔嬪悗?#32029;璇Service Handler?#32048;涓诲姩鐨?#37837;?#37817;?#28051;嶅悓鐨凟vent?#34987;鍨嬪皢鍏?#37714;嗗彂缁?#28725;?#25652;?#37928;凴equest Handler?#28533;澶勭悊銆?br /> <br /> 鏇村鏈?#37928;勶紝杩欑瘒鏂囩珷锛?a href="http://www.dre.vanderbilt.edu/%7Eschmidt/PDF/reactor-siemens.pdf">Reactor An Object Behavioral Pattern for Demultiplexing and Dispatching Handles for Synchronous Events</a>锛?#28051;?#29831;?#38171;?#8220;The Reactor design pattern handles service requests that are delivered concurrently to an application by one or more clients. Each service in an application may consistent of several methods and is represented by a separate event handler that is responsible for dispatching service-specific requests. Dispatching of event handlers is performed by an initiation dispatcher, which manages the registered event handlers. Demultiplexing of service requests is performed by a synchronous event demultiplexer. Also known as <strong>Dispatcher, Notifier</strong>”銆?#26473;?#23048;垫弿杩板拰Wikipedia涓?#37928;勬弿杩扮?#35763;技?#32029;鏈?#28598;?#28051;?#26440;撳叆婧愶紝鏈?#28598;?#28051;?#28051;嶅悓鐨凟ventHandler锛圧equestHandler锛夋潵澶勭悊涓嶅悓鐨?#29831;?#23033;傦紝Initiation Dispatcher?#25956;浜庣?#24714;EventHander?#32029;EventHandler棣?#37711;堣娉?#37712;屽埌Initiation Dispatcher涓紝?#21159;鍚嶪nitiation Dispatcher鏍?#37817;?#26440;撳叆鐨凟vent鍒嗗彂缁?#23049;?#37712;?#37928;凟ventHandler锛涚劧鑰孖nitiation Dispatcher骞?#28051;?#37929;戝惉Event鐨勫埌?#28533;?#32029;杩?#28051;?#23480;?#28003;?#27996;?#32513;Synchronous Event Demultiplexer?#28533;澶勭悊銆?br /> <h2>Reactor妯?#23534;?#32513;撴瀯</h2> ?#28266;瑙?#37712;充簡浠涔?#37828;疪eactor妯?#23534;?#37722;庯紝鎴戜滑?#28533;鐪嬬湅Reactor妯?#23534;?#37828;?#37922;?#28000;涔堟鍧?#37835;勬垚銆?#37733;?#37828;?#28051;绉嶆瘮杈冪畝娲佸舰?#34180;鐨勮鐜版柟寮忥紝鍥?#38000;屽厛涓?#28051;寮?#37733;炬潵琛?#26440;惧悇涓?#22959;?#37735;?#37928;勫?#23943;?#26495;拰浠?#28000;?#28052;?#38338;?#37928;勫叧绯?#38171;?br /> <img alt="" src="http://www.8634070.com/images/blogjava_net/dlevin/Reactor_Structures.png" height="266" width="401" /><br /> <strong>Handle锛?/strong>鍗虫搷浣?#32495;?#32513;?#28051;?#37928;勫彞鏌勶紝鏄?#28725;?#29863;勬?#24876;湪鎿?#28003;?#32495;?#32513;熷?#20638;潰涓?#37928;?#28051;绉嶆?#20511;薄?#32029;?#30048;?#24434;浠?#37828;?#37813;撳紑鐨勬枃浠?#37510;?#28051;涓?#26473;?#37818;?Socket)銆乀imer绛?#37510;傜敱浜嶳eactor妯?#23534;?#28051;鑸?#28003;跨敤?#28266;?#32137;缁?#32514;?#32459;?#28051;紝鍥?#38000;?#26473;欓噷涓鑸?#37816;Socket Handle?#32029;鍗?#28051;涓綉缁?#26473;?#37818;?#38171;圕onnection?#32029;?#28266;Java NIO涓?#37928;凜hannel锛?#37510;?#26473;?#28051;狢hannel娉?#37712;屽埌Synchronous Event Demultiplexer涓紝浠?#37929;戝惉Handle涓彂鐢?#37928;?#27996;?#28000;讹紝瀵筍erverSocketChannnel?#24434;浠?#37828;疌ONNECT浜?#28000;讹紝瀵筍ocketChannel?#24434;浠?#37828;疪EAD銆乄RITE銆丆LOSE浜?#28000;?#32475;?#37510;?br /><strong>Synchronous Event Demultiplexer锛?/strong>闃诲绛?#23536;?#28051;绯?#37714;?#37928;凥andle涓?#37928;?#27996;?#28000;跺埌?#28533;?#32029;濡傛灉闃诲绛?#23536;?#26473;?#37733;烇紝鍗?#29723;?#32448;哄湪杩?#37733;?#37928;凥andle涓彲浠?#28051;嶉?#35826;鐨?#37813;?#29723;?#26473;?#37733;?#37928;?#27996;?#28000;剁被鍨?#37510;?#26473;?#28051;?#22959;?#37735;?#28051;鑸?#28003;跨敤鎿?#28003;?#32495;?#32513;?#37928;剆elect?#28533;瀹?#37916;?#37510;傚湪Java NIO涓敤Selector?#28533;灏佽?#21635;紝褰揝elector.select()杩?#37733;?#37827;讹紝?#24434;浠皟?#25956;Selector鐨剆electedKeys()鏂规硶鑾?#37721;朣et<SelectionKey>?#32029;涓涓猄electionKey琛?#26440;?#28051;涓?#37832;?#27996;?#28000;跺彂鐢?#37928;凜hannel浠?#37721;?#29831;Channel涓?#37928;?#27996;?#28000;剁被鍨?#37510;?#28051;婂浘鐨?#8220;Synchronous Event Demultiplexer ---notifies--> Handle”鐨勬?#20346;?#23338;?#20635;灉鏄?#28725;?#37928;勶紝?#20549;鍐?#38318;?#28729;?#37916;?#25652;?#29831;?#37828;痵elect()鏂规硶?#28266;浜?#28000;跺埌?#28533;鍚庝細鍏堣缃Handle鐨勭姸鎬侊紝?#21159;鍚?#26473;?#37733;?#37510;?#28051;嶄簡瑙?#37712;?#38318;?#28729;?#37916;?#37832;?#37714;讹紝鍥?#38000;屼?#28616;?#27407;師鍥?#37510;?br /><strong>Initiation Dispatcher锛?/strong>?#25956;浜庣?#24714;Event Handler?#32029;鍗EventHandler鐨勫?#29808;櫒?#32029;?#25956;浠?#23049;?#37712;?#37510;佺?#23130;櫎EventHandler绛?#38171;涘彟澶栵紝?#30048;杩?#28003;?#28051;篟eactor妯?#23534;?#37928;勫叆鍙皟?#25956;Synchronous Event Demultiplexer鐨剆elect鏂规硶浠?#38339;诲绛?#23536;?#27996;?#28000;?#26473;?#37733;烇紝褰撻?#35826;绛?#23536;?#26473;?#37733;?#37827;讹紝鏍?#37817;?#27996;?#28000;跺彂鐢?#37928;凥andle灏?#37711;?#37714;嗗彂缁?#28725;?#25652;?#37928;凟vent Handler澶勭?#21997;紝鍗?#37733;炶皟EventHandler涓?#37928;刪andle_event()鏂规硶銆?br /><strong>Event Handler锛?/strong>?#30078;涔?#27996;?#28000;?#28598;勭?#21976;?#35268;硶锛歨andle_event()?#32029;浠緵InitiationDispatcher鍥炶皟浣跨敤銆?br /><strong>Concrete Event Handler锛?/strong>浜?#28000;EventHandler鎺?#37721;紝瀹?#37916;?#37911;瑰畾浜?#28000;?#28598;勭悊閫昏緫銆?br /><h2> Reactor妯?#23534;忔鍧?#28052;?#38338;?#37928;?#27996;?#27996;?/h2> ?#30045;鍗曟弿杩?#28051;涓婻eactor鍚?#28051;?#22959;?#37735;?#28052;?#38338;?#37928;?#27996;?#27996;?#23092;佺?#23350;紝鍏堜粠搴?#37714;楀?#24807;紑濮?#38171;?br /><img alt="" src="http://www.8634070.com/images/blogjava_net/dlevin/Reactor_Sequence.png" height="222" width="385" /><br />1. 鍒?#28654;嬪?#26387;nitiationDispatcher?#32029;骞?#37714;?#28654;嬪寲涓涓狧andle?#22476;EventHandler鐨凪ap銆?br />2. 娉?#37712;孍ventHandler?#22476;InitiationDispatcher涓紝姣?#28051;狤ventHandler鍖?#37722;?#28725;?#37929;?#25652;擧andle鐨勫?#26336;敤?#32029;?#31904;鑰屽缓?#29659;Handle?#22476;EventHandler鐨?#37828;?#28751;?#38171;Map锛?#37510;?br />3. ?#30367;?#25956;InitiationDispatcher鐨刪andle_events()鏂规硶浠?#37722;?#37716;Event Loop銆傚湪Event Loop涓紝?#30367;?#25956;select()鏂规硶锛圫ynchronous Event Demultiplexer锛夐?#35826;绛?#23536;匛vent?#24386;鐢?#37510;?br />4. 褰撴煇涓?#37812;?#37836;?#27996;汬andle鐨凟vent?#24386;鐢?#37722;庯紝select()鏂规硶杩?#37733;烇紝InitiationDispatcher鏍?#37817;?#26473;?#37733;?#37928;凥andle鎵惧埌娉?#37712;?#37928;凟ventHandler?#32029;骞?#37733;炶皟璇EventHandler鐨刪andle_events()鏂规硶銆?br />5. ?#28266;EventHandler鐨刪andle_events()鏂规硶涓?#26473;樺彲浠?#37722;慖nitiationDispatcher涓?#23049;?#37712;屾柊鐨凟venthandler?#32029;姣斿瀵AcceptorEventHandler?#28533;?#32029;褰?#37832;夋柊鐨刢lient杩?#37818;?#37827;讹紝?#30048;?#32048;浜?#37922;?#37826;?#37928;凟ventHandler浠?#28598;勭?#21976;柊鐨?#26473;?#37818;紝骞舵敞鍐屽埌InitiationDispatcher涓?#37510;?br /><h2>Reactor妯?#23534;忓疄鐜?/h2>?#28266;<a href="http://www.dre.vanderbilt.edu/%7Eschmidt/PDF/reactor-siemens.pdf">Reactor An Object Behavioral Pattern for Demultiplexing and Dispatching Handles for Synchronous Events</a>涓紝涓鐩?#28000;Logging Server?#28533;鍒嗘瀽Reactor妯?#23534;忥紝杩?#28051;狶ogging Server鐨勫疄鐜板?#23677;叏閬?#23536;?#26473;?#38322;屽?#31562;eactor?#24383;杩帮紝鍥?#38000;屾?#24807;湪杩欓噷浠?#37707;?#37721;?#38000;?#37510;侺ogging Server涓?#37928;凴eactor妯?#23534;忓疄鐜?#37714;?#28051;?#28051;?#38318;?#37714;?#38171;欳lient杩?#37818;埌Logging Server?#25328;Client鍚慙ogging Server鍐橪og銆?#37733;?#38000;屽?#29808;畠鐨勬弿杩?#37714;嗘垚杩?#28051;?#28051;?#23005;?#26976;?#37510;?br /><strong>Client杩?#37818;埌Logging Server</strong><br /><img alt="" src="http://www.8634070.com/images/blogjava_net/dlevin/Reactor_LoggingServer_connect.png" height="220" width="411" /><br />1. Logging Server娉?#37712;孡oggingAcceptor?#22476;InitiationDispatcher銆?br />2. Logging Server?#30367;?#25956;InitiationDispatcher鐨刪andle_events()鏂规硶鍚?#37716;?#37510;?br />3. InitiationDispatcher鍐?#38318;皟?#25956;select()鏂规硶锛圫ynchronous Event Demultiplexer锛夛紝闃诲绛?#23536;匔lient杩?#37818;?#37510;?br />4. Client杩?#37818;埌Logging Server銆?br />5. InitiationDisptcher涓?#37928;剆elect()鏂规硶杩?#37733;烇紝骞?#38315;氱煡LoggingAcceptor鏈夋柊鐨?#26473;?#37818;埌?#28533;銆?<br />6. LoggingAcceptor?#30367;?#25956;accept鏂规硶accept杩?#28051;?#37826;?#26473;?#37818;?#37510;?br />7. LoggingAcceptor鍒涘?#28888;柊鐨凩oggingHandler銆?br />8. 鏂?#37928;凩oggingHandler娉?#37712;屽埌InitiationDispatcher涓?鍚?#37827;?#28052;?#23049;?#37712;屽埌Synchonous Event Demultiplexer涓??#32029;绛?#23536;匔lient?#24386;?#25443;鍐檒og璇?#23033;?#37510;?br /><strong>Client鍚慙ogging Server鍐橪og</strong><br /><img alt="" src="http://www.8634070.com/images/blogjava_net/dlevin/Reactor_LoggingServer_log.png" height="265" width="413" /><br />1. Client?#24386;閫乴og?#22476;Logging server銆?br />2. InitiationDispatcher鐩戞?#23338;埌鐩?#25652;?#37928;凥andle涓?#37832;?#27996;?#28000;跺彂鐢燂紝杩?#37733;為?#35826;绛?#23536;咃紝鏍?#37817;?#26473;?#37733;?#37928;凥andle鎵惧埌LoggingHandler?#32029;骞?#37733;炶皟LoggingHandler涓?#37928;刪andle_event()鏂规硶銆?br />3. LoggingHandler涓?#37928;刪andle_event()鏂规硶涓?#29831;?#37721;朒andle涓?#37928;刲og?#20426;鎭?#37510;?br />4. 灏?#37818;?#37824;跺埌鐨刲og鍐?#37711;埌鏃?#36423;楁枃浠?#37510;?#37825;?#37817;?#25652;?#32475;夎澶?#28051;?#37510;?br />3.4姝?#26976;?#23536;?#37916;?#37929;村埌褰撳墠鏃?#36423;楀?#21229;悊瀹屾垚銆?br />5. 杩?#37733;炲埌InitiationDispatcher绛?#23536;?#28051;?#28051;娆?#37827;?#36423;?#37712;?#29831;?#23033;?#37510;?br /><br />?#28266;<a href="http://www.dre.vanderbilt.edu/%7Eschmidt/PDF/reactor-siemens.pdf">Reactor An Object Behavioral Pattern for Demultiplexing and Dispatching Handles for Synchronous Events</a>鏈?#28725;筊eactor妯?#23534;?#37928;凜++鐨勫疄鐜?#37911;?#37832;紝澶?#39582;?#28051;嶇敤C++?#32029;鍥?#38000;岀暐杩?#37510;?nbsp; <h2>Java NIO瀵筊eactor鐨勫疄鐜?/h2>?#28266;Java鐨凬IO涓紝瀵筊eactor妯?#23534;?#37832;?#37827;?#32514;?#37928;勬敮鎸侊紝鍗?#28003;跨敤Selector?#34987;灏佽?#21612;簡鎿?#28003;?#32495;?#32513;熸?#24874;緵鐨凷ynchronous Event Demultiplexer鍔熻兘銆?#26473;?#28051;狣oug Lea宸?#32513;忓湪<a href="http://gee.cs.oswego.edu/dl/cpjslides/nio.pdf">Scalable IO In Java</a>涓?#37832;夐?#28850;父?#32321;鍏?#37928;勮閲婁簡?#32029;鍥?#38000;?#28051;?#37712;?#29863;?#26473;帮紝鍙?#28598;?a href="http://www.cnblogs.com/luxiaoxun/archive/2015/03/11/4331110.html">杩欑瘒鏂囩珷</a>瀵笵oug Lea鐨?a href="http://gee.cs.oswego.edu/dl/cpjslides/nio.pdf">Scalable IO In Java</a>鏈?#28051;浜涚畝鍗曡?#22132;?#32029;鑷?#28751;戝畠鐨勪唬鐮?#37837;?#23534;忔瘮Doug Lea鐨凱PT瑕?#37825;存涓浜?#37510;?br /><br />?#28214;瑕佹?#22247;嚭鐨?#37828;紝涓嶅悓杩欓噷浣跨敤InitiationDispatcher?#28533;绠悊EventHandler?#32029;?#28266;Doug Lea鐨?#37911;?#37832;?#28051;?#28003;跨敤SelectionKey涓?#37928;凙ttachment?#28533;瀛樺偍瀵?#25652;?#37928;凟ventHandler?#32029;鍥?#38000;?#28051;嶉渶瑕?#23049;?#37712;孍ventHandler杩?#28051;?#23005;?#26976;紝鎴?#38000;?#29825;?#32515;瓵ttachment?#27688;鏄?#26473;欓噷鐨勬敞鍐?#37510;?#38000;?#28051;斿湪杩欑瘒鏂囩珷涓紝Doug Lea?#31904;鍗曠?#36328;鐨凴eactor銆丄cceptor銆丠andler瀹?#37916;?#26473;?#28051;?#22959;?#23534;?#37713;哄彂锛涙?#26047;寲涓?#28751;咹andler涓?#37928;勫?#21229;悊閫昏緫澶?#32510;跨?#23338;?#26677;紝瀹?#37916;扮?#35763;技Proactor妯?#23534;忥紝姝?#37827;舵墍鏈?#37928;処O鎿?#28003;?#26473;?#37828;?#37719;曠?#36328;鐨勶紝鍥?#38000;?#37712;嶆?#26047;寲鍑?#28051;涓狹ain Reactor?#28533;澶勭悊CONNECT浜?#28000;?Acceptor)?#32029;鑰?#28598;?#28051;猄ub Reactor?#28533;澶勭悊READ銆乄RITE绛?#27996;?#28000;?Handler)?#32029;杩欎簺Sub Reactor?#24434;浠?#37714;?#37714;?#37712;?#38007;?#23480;?#37928;?#32510;跨涓?#37813;?#29723;岋紝?#31904;鑰孖O鎿?#28003;?#28052;?#28598;?#32510;跨?#23338;寲銆?#26473;?#28051;?#37832;鍚?#28051;涓?#22959;?#37736;嬫鏄疦etty涓?#28003;跨敤鐨勬鍨?#37510;?#39582;?#28051;斿湪<a href="http://www.dre.vanderbilt.edu/%7Eschmidt/PDF/reactor-siemens.pdf">Reactor An Object Behavioral Pattern for Demultiplexing and Dispatching Handles for Synchronous Events</a>鐨?.5 Determine the Number of Initiation Dispatchers in an Application涓?#28052;?#37832;?#37929;?#25652;?#37928;勬弿杩?#37510;?br /><h2>EventHandler鎺?#37721;畾涔?/h2>瀵笶ventHandler鐨勫畾涔?#37832;?#28051;?#32457;嶈璁?#37804;?#29882;?#38171;歴ingle-method璁?#29825;拰multi-method璁?#29825;?#38171;?br /><strong>A single-method interface锛?/strong>?#30048;灏咵vent灏佽鎴?#28051;涓狤vent Object?#32029;EventHandler鍙畾涔?#28051;涓猦andle_event(Event event)鏂规硶銆?#26473;?#32457;嶈璁?#37928;勫?#34249;鏄?#37832;?#37714;?#27996;?#37813;?#28766;曪紝?#24434;浠?#37722;庢潵鏂逛?#36328;殑娣诲?#29363;柊鐨凟vent?#34987;鍨嬶紝?#21159;鑰屽湪瀛愮被鐨勫疄鐜?#28051;紝?#28214;瑕?#37714;?#37826;?#28051;嶅悓鐨凟vent?#34987;鍨?#38000;?#37712;嶆鎵?#28766;?#37812;? 涓嶅悓鐨勫?#21229;?#21976;?#35268;硶?#32029;?#31904;杩?#28051;?#29785;?#25652;?#28051;婃潵璇达紝?#30048;鍙?#28051;?#37714;?#27996;?#37813;?#28766;?#37510;?#37721;?#28598;栧湪Netty3鐨?#28003;跨敤杩囩涓紝鐢?#27996;庡畠涓嶅仠鐨勫?#28056;缓ChannelEvent?#34987;?#32029;鍥?#38000;屼細寮曡捣GC鐨?#28051;嶇?#20914;畾銆?br /><strong>A multi-method interface锛?/strong>杩?#32457;嶈璁?#37828;?#28751;?#28051;嶅悓鐨凟vent?#34987;鍨嬪湪 EventHandler涓畾涔?#37929;?#25652;?#37928;勬?#35268;硶銆?#26473;?#32457;嶈璁氨鏄疦etty4涓?#28003;跨敤鐨?#32475;栫暐?#32029;鍏?#28051;?#28051;涓?#37929;?#37928;?#37828;?#38316;?#37711;岰hannelEvent鍒涘缓寮曡捣鐨凣C涓嶇?#20914;畾?#32029; 鍙?#28598;?#28051;涓?#28610;藉鏄畠?#24434;浠?#38316;?#37711;嶅湪EventHandler瀹?#37916;?#37827;?#37714;?#37826;?#28051;嶅悓鐨凟vent?#34987;鍨?#38000;?#37832;?#28051;嶅悓鐨勫疄鐜帮紝?#21159;鑰?#26473;?#32457;嶈璁細缁?#37813;?#28766;?#37826;?#37928;凟vent?#34987;鍨?#37827;跺甫?#28533;闈炲父 澶?#37928;勯?#33636;儲?#32029;鍥?#28051;哄畠?#28214;瑕?#29831;?#37818;?#37721;?#37510;?br /><br />鍏?#27996;嶯etty4瀵筃etty3鐨勬敼杩涘彲浠?#37721;?#38000;?a href="http://netty.io/wiki/new-and-noteworthy-in-4.0.html">杩欓噷</a>锛?br /><fieldset><legend><strong>ChannelHandler with no event object</strong></legend>In 3.x, every I/O operation created a <code>ChannelEvent</code> object. For each read / write, it additionally created a new <code>ChannelBuffer</code>. It simplified the internals of Netty quite a lot because it delegates resource management and buffer pooling to the JVM. However, it often was the root cause of GC pressure and uncertainty which are sometimes observed in a Netty-based application under high load. <p>4.0 removes event object creation almost completely by replacing the event objects with strongly typed method invocations. 3.x had catch-all event handler methods such as <code>handleUpstream()</code> and <code>handleDownstream()</code>, but this is not the case anymore. Every event type has its own handler method now:</p></fieldset><h2>涓?#28000;涔?#28003;跨敤Reactor妯?#23534;?/h2>褰掑姛涓嶯etty?#25328;Java NIO瀵筊eactor鐨勫浼狅紝鏈枃鎱?#37722;?#38000;屽涔?#37928;凴eactor妯?#23534;忥紝鍥?#38000;屽凡缁?#27035;?#29825;Reactor?#21503;鏈夐?#28850;父浼?#32457;鐨?#37804;兘?#32029;?#21159;鑰屾厱鍚嶅綊鎱?#37722;嶏紝?#22476;杩欓噷?#32029;?#22428;杩?#37828;?#29781;?#28051;?#23536;?#28051;嶉棶鑷?#23480;盧eactor妯?#23534;?#37928;勫?#34249;?#21227;湪鍝噷锛?#37719;?#28051;?#28000;涔堣浣跨敤杩?#28051;猂eactor妯?#23534;?#38171;熷湪<a href="http://www.dre.vanderbilt.edu/%7Eschmidt/PDF/reactor-siemens.pdf">Reactor An Object Behavioral Pattern for Demultiplexing and Dispatching Handles for Synchronous Events</a>涓?#37828;?#26473;?#28052;?#29831;?#37928;?#38171;?br /><fieldset><legend><strong>Reactor Pattern浼樼偣</strong></legend> <div class="page" title="Page 9"> <div class="layoutArea"> <div class="column"> <p><span style="font-size: 10.000000pt; font-family: 'Times'; font-weight: 700">Separation of concerns: </span><span style="font-size: 10.000000pt; font-family: 'Times'">The Reactor pattern decouples application-independent demultiplexing and dispatching mechanisms from application-specific hook method functionality. The application-independent mechanisms become reusable components that know how to demultiplex events and dispatch the appropriate hook methods defined by </span><span style="font-size: 10.000000pt; font-family: 'Courier'">Event Handlers</span><span style="font-size: 10.000000pt; font-family: 'Times'">. In contrast, the application-specific functionality in a hook method knows how to perform a particular type of service. </span></p> <p><span style="font-size: 10.000000pt; font-family: 'Times'; font-weight: 700">Improve modularity, reusability, and configurability of event-driven applications: </span><span style="font-size: 10.000000pt; font-family: 'Times'">The pattern decouples application functionality into separate classes. For instance, there are two separate classes in the logging server: one for establishing connections and another for receiving and processing logging records. This decoupling enables the reuse of the connection establishment class for different types of connection-oriented services (such as file transfer, remote login, and video-on-demand). Therefore, modifying or extending the functionality of the logging server only affects the implementation of the logging handler class. </span></p> </div> </div> </div> <div class="page" title="Page 9"> <div class="layoutArea"> <div class="column"> <p><span style="font-size: 10.000000pt; font-family: 'Times'; font-weight: 700">Improves application portability: </span><span style="font-size: 10.000000pt; font-family: 'Times'">The </span><span style="font-size: 10.000000pt; font-family: 'Courier'">Initiation Dispatcher</span><span style="font-size: 10.000000pt; font-family: 'Times'">’s interface can be reused independently of the OS system calls that perform event demultiplexing. These system calls detect and report the occurrence of one or more events that may occur simultaneously on multiple sources of events. Common sources of events may in- clude I/O handles, timers, and synchronization objects. On UNIX platforms, the event demultiplexing system calls are called </span><span style="font-size: 10.000000pt; font-family: 'Courier'">select </span><span style="font-size: 10.000000pt; font-family: 'Times'">and </span><span style="font-size: 10.000000pt; font-family: 'Courier'">poll </span><span style="font-size: 10.000000pt; font-family: 'Times'">[1]. In the Win32 API [16], the </span><span style="font-size: 10.000000pt; font-family: 'Courier'">WaitForMultipleObjects </span><span style="font-size: 10.000000pt; font-family: 'Times'">system call performs event demultiplexing. </span></p> <p><span style="font-size: 10.000000pt; font-family: 'Times'; font-weight: 700">Provides coarse-grained concurrency control: </span><span style="font-size: 10.000000pt; font-family: 'Times'">The Reactor pattern serializes the invocation of event handlers at the level of event demultiplexing and dispatching within a process or thread. Serialization at the </span><span style="font-size: 10.000000pt; font-family: 'Courier'">Initiation Dispatcher </span><span style="font-size: 10.000000pt; font-family: 'Times'">level often eliminates the need for more complicated synchronization or locking within an application process. </span></p> </div> </div></div></fieldset>杩欎簺璨屼技鏄?#23536;?#28598;氭寮?#37928;勫叡鎬?#38171;氳鑰?#37510;佹彁鍗囧?#23943;敤鎬?#37510;佹鍧楀寲銆佸彲绉绘鎬?#37510;?#27996;?#28000;堕?#21342;姩銆?#32513;?#37716;?#25652;?#37928;勫?#36346;彂鎺?#37714;?#32475;夛紝鍥?#38000;屽苟涓嶈兘寰?#28610;?#37928;?#29831;?#37828;?#28000;涔堬紝鐗?#37714;?#37828;畠?#32038;鍚?#37928;勫鎬兘鐨勬彁鍗囷紝杩?#38322;屽?#33333;病鏈?#28003;?#37916;?#37713;烘潵銆?#35120;撶劧?#28266;杩欑瘒鏂囩珷鐨勫紑澶?#37832;夋弿杩?#26473;囧彟涓绉?#37929;?#29785;?#37928;勫疄鐜?#38171;歍hread-Per-Connection?#32029;鍗?#28028;?#32513;?#37928;勫疄鐜帮紝鎻愬埌?#31777;杩?#28051;?#28028;?#32513;熷疄鐜?#37928;?#28000;?#28051;嬮棶棰?#38171;?br /><fieldset><legend><strong>Thread Per Connection缂虹偣</strong></legend> <div class="page" title="Page 1"> <div class="layoutArea"> <div class="column"> <p><span style="font-size: 10.000000pt; font-family: 'Times'; font-weight: 700">Efficiency: </span><span style="font-size: 10.000000pt; font-family: 'Times'">Threading may lead to poor performance due to context switching, synchronization, and data movement [2]; </span></p> </div> </div> </div> <div class="page" title="Page 2"> <div class="layoutArea"> <div class="column"> <p><span style="font-size: 10.000000pt; font-family: 'Times'; font-weight: 700">Programming simplicity: </span><span style="font-size: 10.000000pt; font-family: 'Times'">Threading may require complex concurrency control schemes; </span></p> </div> </div> </div><span style="font-size: 10.000000pt; font-family: 'Times'; font-weight: 700">Portability: </span><span style="font-size: 10.000000pt; font-family: 'Times'">Threading is not available on all OS platforms. </span> </fieldset>瀵?#27996;?#37804;兘?#32029;?#30048;鍏跺?#28850;氨鏄?#32471;?#28051;?#20579;鍏?#27996;嶦fficiency鐨勬弿杩帮紝鍗?#32510;跨鐨勫垏鎹?#37510;佸?#23678;銆?#37825;?#37817;?#37928;?#32457;诲姩?#32048;寮曡捣鎬兘?#26870;棰?#37510;?#28052;熷氨鏄?#29831;翠粠鎬兘鐨勮搴?#28051;婏紝?#30048;鏈澶?#37928;勬彁鍗囧氨鏄?#37713;?#28751;戜簡鎬兘鐨?#28003;跨敤?#32029;鍗?#28051;嶉渶瑕?#23011;?#28051;狢lient瀵?#25652;?#28051;涓?#32510;跨銆傛垜鐨勭悊瑙紝鍏?#28000;?#28051;?#37716;?#38315;昏緫澶勭悊寰?#28598;?#37827;?#37706;?#28052;熶細?#25956;?#22476;鐩?#37722;?#37928;?#32510;跨?#23350;紝IO璇?#37712;欐搷浣?#37929;?#28725;笴PU鐨勬搷浣?#26473;?#37828;?#29781;?#37809;?#23536;?#28598;氾紝鍗?#28003;Reactor鏈?#37714;?#28051;?#23011;忔璇?#37712;?#23480;?#32513;忚兘淇?#29831;?#38344;為?#35826;璇?#37712;欙紝杩?#38322;屽彲浠?#37713;?#28751;?#28051;浜?#32510;跨鐨?#28003;跨敤?#32029;浣?#37828;?#26473;?#37713;?#28751;?#37928;?#32510;跨浣跨敤瀵?#37804;兘鏈夐偅涔?#28598;?#37928;勫?#21342;?#23941;悧锛熺?#26048;?#22563;?#23676;技鏄?#38002;畾鐨勶紝杩欑?#22250;?#28888;枃(<a href="http://www.eecs.harvard.edu/~mdw/papers/seda-sosp01.pdf">SEDA: Staged Event-Driven Architecture - An Architecture for Well-Conditioned, Scalable Internet Service</a>)瀵?#38341;忕潃绾跨鐨勫?#28858;暱甯潵鎬兘闄?#28003;?#37707;氫簡涓涓?#32513;熻锛?br /><img alt="" src="http://www.8634070.com/images/blogjava_net/dlevin/Reactor_thread_per_connection.png" height="482" width="407" /><br />?#28266;杩?#28051;?#32513;熻涓紝姣?#28051;?#32510;跨?#23337;粠纾?#37929;?#28051;?#29831;?KB鏁?#37817;紝姣?#28051;?#32510;跨璇?#37722;?#28051;涓枃浠讹紝鍥?#38000;?#37825;?#37817;?#37832;?#38892;?#37828;紦瀛樺湪鎿?#28003;?#32495;?#32513;?#37712;?#38318;?#37928;勶紝鍗?#37713;?#28751;慖O鐨勫?#21342;搷锛涙墍鏈夌?#36328;鏄?#27996;嬪厛鍒嗛厤鐨勶紝涓嶄細鏈夌?#36328;?#23338;惎鍔?#37928;勫?#21342;搷锛涙墍鏈?#28000;诲姟?#28266;娴?#29831;?#37712;?#38318;?#27996;?#37922;燂紝鍥?#38000;?#28051;嶄細鏈夌綉缁?#37928;勫?#21342;搷銆?#29831;?#32513;熻鏁?#37817;?#26473;?#29723;岀幆澧?#38171;歀inux 2.2.14?#32029;2GB鍐?#28699;橈紝4-way 500MHz Pentium III銆備粠鍥?#28051;彲浠?#37930;嬪嚭?#32029;闅忕潃绾跨鐨勫?#28858;暱?#32029;鍚炲悙?#22138;?#28266;绾跨鏁?#28051;?涓?#23480;?#37721;?#37928;?#37827;?#37706;欏紑濮嬬嚎鎬?#28051;?#38340;嶏紝骞?#28051;斿埌64涓?#28000;?#37722;?#38000;?#26473;?#38315;?#28051;?#38340;嶏紝鍏?#37929;?#25652;?#27996;?#28000;?#28052;熷湪绾跨?#23341;?#24807;埌256涓?#37722;庢寚鏁?#28051;婂崌銆?#37719;?+1<2?#32029;鍥?#28051;?#32510;跨?#23338;垏鎹?#37510;佸?#23678;銆?#37825;?#37817;?#32457;诲姩?#32048;鏈?#37804;兘鎹?#28598;憋紝绾跨鏁板?#28850;姞?#22476;涓?#30078;鏁伴噺鏃讹紝杩?#32457;?#37804;?#38003;藉?#21342;搷鏁堟灉?#32048;鏇村?#29363;槑鏄?#37510;?br /><br />瀵?#27996;?#26473;欑偣?#32029;杩樺彲浠?#37721;?#38000;?a href="https://en.wikipedia.org/wiki/C10k_problem">C10K Problem</a>?#32029;?#25956;浠弿杩?#37722;?#37827;?#37832;?0K涓狢lient?#24386;?#25443;杩?#37818;?#37928;勯棶棰橈紝?#22476;2010骞?#37928;?#37827;?#37706;?#23480;?#32513;?#37713;?#37916;?0M Problem?#31777;銆?br /><br />褰撶劧涔?#37832;?#27996;?#29831;?#38171;?a href="http://cometdaily.com/2008/11/21/are-raining-comets-and-threads/">Threads are expensive are no longer valid</a>.?#28266;涓?#28052;?#37928;勫?#21976;潵?#24434;鑳藉?#22556;細?#24386;鐢?#28051;嶅悓鐨勫?#27194;?#26677;紝鎴?#38000;?#26473;?#28051;?#37721;樺寲姝湪銆?#23480;?#32513;忓彂鐢熺潃锛熸病鏈?#37707;?#26473;?#23011;?#26440;?#28000;?#32513;?#37928;勬祴璇曪紝鍥?#38000;?#28051;?#37825;?#38341;忎究鏂?#29815;浠涔堬紝?#21159;鑰?#37832;?#27996;鸿?#20636;偣?#32029;鍗?#28003;跨?#36328;?#23338;彉鐨勫?#21342;搷骞舵病鏈?#28000;墠?#20549;涔?#28598;紝浣跨敤Reactor妯?#23534;忥紝鐢氳嚦鏃SEDA妯?#23534;忔潵鍑?#28751;?#32510;跨鐨?#28003;跨敤?#32029;鍐嶅姞涓婂叾浠?#29785;?#38000;?#37510;佹鍧楀寲銆佹彁鍗囧?#23943;敤鎬?#32475;?#28028;樼偣?#32029;杩?#37828;?#37706;?#23536;?#28003;跨敤鐨?#37510;?br /><h2>Reactor妯?#23534;?#37928;勭?#34425;偣</h2>Reactor妯?#23534;?#37928;勭?#34425;偣璨屼技涔?#37828;?#37828;?#38000;屾槗瑙?#37928;?#38171;?br />1. 鐩?#23011;?#28028;?#32513;?#37928;勭畝鍗曟鍨嬶紝Reactor澧炲姞?#31777;涓?#30078;鐨勫?#23942;潅鎬紝鍥?#38000;?#37832;?#28051;?#30078;鐨勯妲涳紝骞?#28051;?#28051;?#37828;?#27996;庤皟璇?#37510;?br />2. Reactor妯?#23534;忛渶瑕?#25652;曞眰鐨凷ynchronous Event Demultiplexer?#25966;鎸侊紝姣斿?#20408;ava涓?#37928;凷elector?#25966;鎸侊紝鎿?#28003;?#32495;?#32513;?#37928;剆elect绯?#32513;熻皟?#25956;?#25966;鎸侊紝濡傛灉瑕?#38007;?#23480;卞疄鐜癝ynchronous Event Demultiplexer?#24434;?#20824;涓嶄細鏈夐偅涔堥珮鏁?#37510;?br />3. Reactor妯?#23534;忓湪IO璇?#37712;?#37825;?#37817;?#37827;?#26473;?#37828;湪鍚?#28051;涓?#32510;跨涓?#28729;?#37916;?#37928;勶紝鍗?#28003;?#28003;跨敤澶?#28051;猂eactor鏈?#37714;?#37928;勬儏鍐?#28051;嬶紝?#20549;浜涘叡浜?#28051;涓猂eactor鐨凜hannel濡傛灉鍑?#37916;?#28051;涓暱鏃?#38338;?#37928;?#37825;?#37817;?#29831;?#37712;欙紝?#32048;褰卞搷杩?#28051;猂eactor涓?#37711;?#28000;朇hannel鐨?#37929;?#25652;?#37827;?#38338;达紝姣斿?#20634;湪澶枃浠朵紶杈?#37827;讹紝IO鎿?#28003;滃氨?#32048;褰卞?#23941;叾浠朇lient鐨?#37929;?#25652;?#37827;?#38338;达紝鍥?#38000;屽杩?#32457;嶆搷浣滐紝浣跨敤浼?#32513;?#37928;Thread-Per-Connection鎴?#29825;?#37828;?#28051;涓?#37831;村鐨勯夋嫨?#32029;鎴?#37714;?#23005;?#37827;?#28003;跨敤Proactor妯?#23534;?#37510;?br /> <h2> 鍙?#38000;?/h2> <a href="https://en.wikipedia.org/wiki/Reactor_pattern#cite_ref-1">Reactor Pattern WikiPedia</a><br /> <a href="http://www.dre.vanderbilt.edu/~schmidt/PDF/reactor-siemens.pdf">Reactor An Object Behavioral Pattern for Demultiplexing and Dispatching Handles for Synchronous Events</a><br /> <a href="http://gee.cs.oswego.edu/dl/cpjslides/nio.pdf">Scalable IO In Java</a><br /> <a href="https://en.wikipedia.org/wiki/C10k_problem">C10K Problem WikiPedia</a><br /> <a href="http://www.dre.vanderbilt.edu/~schmidt/PDF/reactor-siemens.pdf"></a><img src ="http://www.8634070.com/DLevin/aggbug/427045.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.8634070.com/DLevin/" target="_blank">DLevin</a> 2015-09-02 15:14 <a href="http://www.8634070.com/DLevin/archive/2015/09/02/427045.html#Feedback" target="_blank" style="text-decoration:none;">?#24386;琛?#29831;勮</a></div>]]></description></item><item><title>?#32321;鍏HBase鏋舵?#21230;鏋?#38171;?#27996;?#38171;?/title><link>http://www.8634070.com/DLevin/archive/2015/08/22/426950.html</link><dc:creator>DLevin</dc:creator><author>DLevin</author><pubDate>Sat, 22 Aug 2015 11:40:00 GMT</pubDate><guid>http://www.8634070.com/DLevin/archive/2015/08/22/426950.html</guid><wfw:comment>http://www.8634070.com/DLevin/comments/426950.html</wfw:comment><comments>http://www.8634070.com/DLevin/archive/2015/08/22/426950.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.8634070.com/DLevin/comments/commentRss/426950.html</wfw:commentRss><trackback:ping>http://www.8634070.com/DLevin/services/trackbacks/426950.html</trackback:ping><description><![CDATA[<h2> ?#22688;瑷</h2>杩?#37828;?a href="http://www.8634070.com/DLevin/archive/2015/08/22/426877.html">銆婃繁鍏HBase鏋舵?#21230;鏋?#38171;?#28051;锛?#37510;?/a>鐨?#32513;紝涓?#28598;?#25652;?#29831;濓紝缁?#32513;?#37510;?#37510;?#37510;?#37510;?br /><h2>HBase璇?#37928;勫疄鐜?/h2>閫?#26473;囧墠?#26499;鐨勬弿杩帮紝鎴戜滑?#29025;閬撳湪HBase鍐?#37827;讹紝鐩?#37722;孋ell(RowKey/ColumnFamily/Column鐩?#37722;?骞?#28051;?#28103;?#29831;佸湪涓?#25443;?#32029;鐢氳嚦鍒?#38340;?#28051;涓狢ell涔?#37721;?#37828;?#37712;?#37711;?#28051;涓?#37826;?#37928;凜ell?#32029;?#30048;鍚?#37832;Delete鏍囪?#24110;紝鑰?#28051;?#28051;?#30078;灏?#28051;涓狢ell鐪?#23005;?#37714;?#38340;簡?#32029;鍥?#38000;?#26473;欏氨寮曡捣?#31777;涓涓棶棰橈紝濡?#28003;?#28729;?#37916;?#29831;?#37928;勯棶棰?#38171;熻瑙?#37712;?#26473;?#28051;棶棰橈紝鎴戜滑鍏堟潵鍒嗘瀽涓涓?#37929;?#37722;?#37928;凜ell?#24434;鑳藉?#27194;湪鐨?#28003;?#32515;?#38171;?#26851;?#37711;?#28725;?#37826;?#37712;?#37711;?#37928;凜ell?#32029;?#30048;?#32048;瀛樺湪浜嶮emStore涓?#38171;涚劧鍚?#28725;?#28052;嬪墠宸?#32513;廎lush?#22476;HDFS涓?#37928;凜ell?#32029;?#30048;?#32048;瀛樺湪浜庢煇涓?#37812;?#37836;?#27996;汼toreFile(HFile)涓?#38171;?#37832;鍚庯紝瀵?#37714;?#29831;?#37721;?#26473;?#37928;凜ell?#32029;?#30048;?#24434;鑳藉?#27194;湪浜嶣lockCache涓?#37510;?#37827;劧鐩?#37722;?#37928;凜ell?#24434;鑳藉?#27194;偍?#28266;涓?#28051;?#37734;版?#30328;紝?#28266;璇?#37721;?#37928;?#37827;?#37706;?#37721;渶瑕?#37813;瀯杩?#28051;?#28051;?#37734;版?#30328;紝?#21159;鍚?#28751;?#32513;撴灉鍚?#39582;?#37719;冲彲(Merge Read)?#32029;?#28266;HBase涓?#37813;瀯鐨勯搴忎?#28615;鏄?#38171;BlockCache銆丮emStore銆丼toreFile(HFile)銆?#37711;?#28051;璖toreFile鐨?#37813;?#37932;勫?#22556;細浣跨敤Bloom Filter杩囨护?#20549;浜?#28051;嶅彲?#20824;绗?#37722;堟潯浠?#37928;凥File?#32029;?#21159;鍚?#28003;跨敤Block Index蹇?#38315;熷畾浣岰ell?#32029;骞?#28751;?#37711;跺姞杞藉埌BlockCache涓紝?#21159;鍚庝粠BlockCache涓?#29831;?#37721;?#37510;?#37812;戜滑?#29025;閬?#28051;涓狧Store?#24434;鑳藉?#27194;湪澶?#28051;猄toreFile(HFile)?#32029;姝?#37827;堕渶瑕?#37813;?#37932;勫涓狧File?#32029;濡傛灉HFile杩囧鍙?#37828;細寮曡捣鎬兘?#26870;棰?#37510;?br /><img alt="" src="http://www.8634070.com/images/blogjava_net/dlevin/HBaseArchitecture-Blog-Fig16.png" height="278" width="769" /><br /><h2>Compaction</h2>MemStore姣忔Flush?#32048;鍒涘?#28888;柊鐨凥File?#32029;鑰?#26473;囧鐨凥File?#32048;寮曡捣璇?#37928;?#37804;兘?#26870;棰橈紝?#20549;涔?#28641;?#28003;曡鍐?#26473;?#28051;棶棰樺憿锛烪Base閲囩敤Compaction鏈?#37714;舵潵瑙?#37712;?#26473;?#28051;棶棰橈紝鏈夌偣绫讳技Java涓?#37928;凣C鏈?#37714;讹紝?#25443;鍒滼ava涓嶅仠鐨?#37922;?#29831;?#37712;?#28699;?#38000;?#28051;嶉噴鏀撅紝澧炲?#29363;兘?#32029;?#21159;鑰?#28598;?#28051;嬫病鏈?#37711;嶈垂鐨勫?#22565;?#24886;紝鏈缁堟?#25116;滑杩?#37828;?#29781;佸湪鏌?#28051;?#37833;?#28000;?#28051;嬪幓鏀堕泦鍨?#37734;撅紝寰?#28598;?#37827;?#37706;欓渶瑕丼top-The-World?#32029;杩?#32457;峉top-The-World鏈?#27996;?#37827;?#37706;?#28052;熶細寮曡捣寰?#28598;?#37928;勯棶棰橈紝姣斿鍙?#38000;?#37832;?#27996;?#37712;?#37928;?a href="http://www.8634070.com/DLevin/archive/2015/08/01/426418.html">杩欑瘒鏂囩珷</a>?#32029;鍥?#38000;岃璁?#37828;?#28051;绉嶆潈琛紝?#30149;鏈夊?#23680;編鐨?#37510;?#26473;?#37828;?#32491;讳技Java涓?#37928;凣C?#32029;?#28266;HBase涓瑿ompaction鍒?#28051;?#28051;?#32457;?#38171;歁inor Compaction?#25328;Major Compaction銆?br /><ol><li>Minor Compaction鏄?#37816;?#38315;?#37721;?#28051;浜涘皬鐨?#37510;?#37929;?#38317;?#37928;凷toreFile灏?#28000;?#28000;?#37722;?#39582;舵垚涓涓?#37831;村鐨凷toreFile?#32029;?#28266;杩?#28051;?#26473;囩涓?#28051;嶄細澶勭悊宸?#32513;廌eleted鎴朎xpired鐨凜ell銆?#28051;娆Minor Compaction鐨?#32513;撴灉鏄?#37831;村皯骞?#28051;旀?#26449;鐨凷toreFile銆?#38171;?#26473;?#28051;?#37828;?#28725;?#37928;勫悧锛烞igTable涓?#37828;?#26473;?#37837;锋弿杩Minor Compaction鐨?span style="font-size: 10.000000pt; font-family: 'Times'">锛欰s write operations execute, the size of the memtable in- creases. When the memtable size reaches a threshold, the memtable is frozen, a new memtable is created, and the frozen memtable is converted to an SSTable and written to GFS. This </span><span style="font-size: 10.000000pt; font-family: 'Times'; font-style: italic">minor compaction </span><span style="font-size: 10.000000pt; font-family: 'Times'">process has two goals: it shrinks the memory usage of the tablet server, and it reduces the amount of data that has to be read from the commit log during recovery if this server dies. Incom- ing read and write operations can continue while com- pactions occur. </span>涔熷氨鏄?#29831;村畠灏唌emtable鐨?#37825;?#37817;甪lush鐨?#28051;涓狧File/SSTable绉?#28051;?#28051;娆Minor Compaction锛?/li><li>Major Compaction鏄?#37816;囧?#21976;墍鏈?#37928;凷toreFile鍚?#39582;舵垚涓涓猄toreFile?#32029;?#28266;杩?#28051;?#26473;囩涓紝鏍囪涓篋eleted鐨凜ell浼氳鍒?#38340;紝鑰岄偅浜?#23480;?#32513;廍xpired鐨凜ell浼氳涓?#23534;冿紝?#20549;浜?#23480;?#32513;忚秴杩?#37832;澶?#37911;?#37832;?#37825;?#37928;凜ell浼氳涓?#23534;?#37510;?#28051;娆Major Compaction鐨?#32513;撴灉鏄?#28051;涓狧Store鍙?#37832;?#28051;涓猄toreFile瀛樺湪銆侻ajor Compaction?#24434;浠?#37813;嬪姩鎴?#38007;?#37716;?#29785;彂?#32029;?#21159;鑰岀敱浜庡畠?#32048;寮曡捣寰?#28598;?#37928;処O鎿?#28003;?#38000;屽?#26337;捣鎬兘?#26870;棰橈紝鍥?#38000;屽畠涓鑸?#28028;氳?#30056;鎺掑湪鍛?#37832;?#37510;?#37713;屾櫒绛夐?#21977;兢姣?#26440;?#38338;?#37928;?#37827;?#38338;?#37510;?br /></li></ol>鏇村舰?#34180;涓?#20579;?#32029;濡?#28051;?#38344;?#28051;?#23534;?#37733;?#37714;?#37714;?#29723;?#32448;篗inor Compaction?#25328;Major Compaction銆?br /><img alt="" src="http://www.8634070.com/images/blogjava_net/dlevin/HBaseArchitecture-Blog-Fig18.png" height="329" width="723" /><img alt="" src="http://www.8634070.com/images/blogjava_net/dlevin/HBaseArchitecture-Blog-Fig19.png" height="339" width="653" /><br /><h2>HRegion Split</h2>鏈鍒濓紝涓涓猅able鍙?#37832;?#28051;涓狧Region?#32029;闅忕潃鏁?#37817;?#37712;?#37711;?#28583;炲姞?#32029;濡傛灉涓涓狧Region?#22476;杈?#28051;?#30078;鐨勫灏忥紝?#27688;?#28214;瑕丼plit鎴?#28051;?#28051;狧Region?#32029;杩?#28051;?#28598;?#28751;?#37922;hbase.hregion.max.filesize鎸囧畾?#32029;榛?#29825;?#28051;?0GB銆?#35120;split鏃讹紝涓?#28051;?#37826;?#37928;凥Region?#32048;?#28266;鍚?#28051;涓狧RegionServer涓?#37714;涘缓?#32029;?#30048;浠?#37722;?#38007;?#37718;?#37722;埗HRegion涓鍗?#37928;?#37825;?#37817;紝褰揝plit瀹屾垚鍚庯紝?#22487;HRegion?#32048;涓嬬嚎?#32029;鑰屾柊鐨?#28051;?#28051;?#28699;怘Region?#32048;鍚HMaster娉?#37712;?#28051;?#32510;匡紝澶?#27996;?#29840;?#26462;藉?#22250;鐨?#38000;?#38095;戯紝杩?#28051;?#28051;?#37826;?#37928;凥Region?#24434;?#20824;浼氳HMaster鍒嗛?#23941;埌鍏?#28000;?#37928;凥RegionServer涓?#37510;?#37711;?#27996;嶴plit鐨?#29831;?#32513;嗕俊鎭紝?#24434;浠?#37721;?#38000;?#26473;欑瘒鏂囩珷锛?a href="http://hortonworks.com/blog/apache-hbase-region-splitting-and-merging/">銆夾pache HBase Region Splitting and Merging銆?/a>銆?br /><img alt="" src="http://www.8634070.com/images/blogjava_net/dlevin/HBaseArchitecture-Blog-Fig21.png" height="361" width="675" /><br /><h2>HRegion璐?#26462;藉?#22250;</h2>?#28266;HRegion Split鍚庯紝涓?#28051;?#37826;?#37928;凥Region鏈鍒濅細?#25328;涔嬪墠鐨勭埗HRegion?#28266;鐩?#37722;?#37928;凥RegionServer涓婏紝鍑?#27996;?#29840;?#26462;藉?#22250;鐨?#38000;?#38095;戯紝HMaster?#24434;?#20824;?#32048;灏?#37711;?#28051;?#37928;?#28051;涓?#37922;氳嚦涓?#28051;?#38322;嶆柊鍒嗛厤鐨勫叾浠?#37928;凥RegionServer涓紝姝?#37827;朵細寮曡捣鏈?#27996;汬RegionServer澶勭悊鐨?#37825;?#37817;湪鍏?#28000;?#38010;傜偣涓婏紝鐩村埌涓?#28051;娆Major Compaction灏?#37825;?#37817;粠杩?#32468;?#37928;勮?#20636;偣绉诲姩?#22476;鏈湴鑺傜偣銆?br /><br /><img alt="" src="http://www.8634070.com/images/blogjava_net/dlevin/HBaseArchitecture-Blog-Fig22.png" height="358" width="714" /><br /><h2>HRegionServer Recovery</h2>褰?#28051;鍙癏RegionServer瀹?#37832;?#37827;讹紝鐢?#27996;庡畠涓?#37712;嶅彂閫丠eartbeat缁橺ooKeeper鑰岃鐩戞?#23338;埌?#32029;姝?#37827;ZooKeeper?#32048;閫氱煡HMaster?#32029;HMaster?#32048;妫娴嬪埌鍝?#37721;癏RegionServer瀹?#37832;猴紝?#30048;灏?#28729;?#37832;?#37928;凥RegionServer涓?#37928;凥Region閲嶆柊鍒嗛厤缁?#37711;?#28000;?#37928;凥RegionServer?#32029;鍚?#37827;禜Master?#32048;鎶婂畷鏈?#37928;凥RegionServer鐩?#37711;?#37928;刉AL鎷?#37714;?#37714;嗛厤缁?#37929;?#25652;?#37928;凥RegionServer(灏嗘媶鍒?#37713;?#37928;刉AL?#26499;浠?#37712;?#37711;?#28725;?#25652;?#37928;?#37929;?#37928;凥RegionServer鐨刉AL鐩?#35120;?#28051;紝骞?#39582;?#37712;?#37711;?#28725;?#25652;?#37928;凞ataNode涓?#38171;夛紝?#31904;鑰?#26473;欎簺HRegionServer?#24434;浠Replay鍒嗗埌鐨刉AL?#28533;閲嶅缓MemStore銆?br /><img alt="" src="http://www.8634070.com/images/blogjava_net/dlevin/HBaseArchitecture-Blog-Fig25.png" height="368" width="708" /><br /><img alt="" src="http://www.8634070.com/images/blogjava_net/dlevin/HBaseArchitecture-Blog-Fig26.png" height="378" width="724" /><br /><h2>HBase鏋舵?#21229;畝鍗?#37804;?#32513;?/h2>?#28266;NoSQL涓紝瀛樺湪钁楀悕鐨凜AP?#24714;璁猴紝鍗Consistency銆丄vailability銆丳artition Tolerance涓嶅彲鍏?#23536;楋紝鐩墠甯?#37734;?#28051;婂熀鏈?#28051;?#37928;凬oSQL閮?#38322;囩敤Partition Tolerance浠?#28729;?#37916;?#37825;?#37817;?#23536;楁按骞?#37813;?#28766;曪紝?#28533;澶勭悊Relational DataBase閬囧埌鐨?#37827;犳硶澶勭悊鏁?#37817;噺澶?#28598;?#37928;勯棶棰橈紝鎴?#23534;曡捣鐨?#37804;兘?#26870;棰?#37510;?#37733;?#38000;屽彧鏈?#37715;?#28051;婥?#25328;A?#24434;浠?#38315;夋嫨銆侶Base?#28266;涓?#38000;?#28052;?#38338;?#38315;夋嫨?#31777;Consistency?#32029;?#21159;鍚?#28003;跨敤澶?#28051;狧Master浠?#37721;婃敮?#23508;HRegionServer鐨刦ailure鐩?#37818;?#37510;乑ooKeeper寮?#37711;?#28003;?#28051;?#37719;忚皟鑰?#32475;?#37722;?#32457;?#37813;嬫?#22443;潵瑙?#37712;矨vailability?#26870;棰橈紝?#21159;鑰屽?#25782;綉缁?#37928;凷plit-Brain(Network Partition)?#24386;鐢?#37827;讹紝?#30048;杩?#37828;?#37827;犳硶瀹屽叏瑙?#37712;矨vailability鐨勯棶棰?#37510;備粠杩?#28051;?#29785;?#25652;?#28051;婏紝Cassandra閫夋嫨?#31777;A?#32029;鍗冲畠?#28266;?#32137;缁淪plit-Brain鏃?#26473;?#37828;?#38003;芥?#29238;鍐欙紝鑰?#28003;跨敤鍏?#28000;栨妧鏈潵瑙?#37712;Consistency鐨勯棶棰橈紝濡?#29831;?#37928;?#37827;?#37706;?#29785;彂Consistency鍒?#37826;拰澶勭悊銆?#26473;?#37828;?#29825;?#29825;?#28051;?#37928;勯檺鍒?#37510;?br /><br />?#31904;瀹?#37916;?#28051;?#37928;勪紭?#20579;锛?br /><ol><li>HBase閲囩敤寮?#28051;?#22183;鎬?#22959;?#37736;嬶紝?#28266;涓涓?#37712;?#26473;?#37733;炲悗?#32029;淇?#29831;佹墍鏈?#37928;?#29831;?#38318;借?#35826;埌鐩?#37722;?#37928;?#37825;?#37817;?#37510;?/li><li>閫?#26473;HRegion鍔?#37804;丼plit?#25328;Merge瀹?#37916;拌嚜鍔?#37813;?#28766;曪紝骞?#28003;跨敤HDFS鎻愪緵鐨勫涓?#37825;?#37817;?#28598;?#28000;藉?#29115;兘?#32029;瀹?#37916;?#26946;樺彲?#25956;鎬?#37510;?/li><li>閲囩敤HRegionServer?#25328;DataNode杩?#29723;屽湪鐩?#37722;?#37928;?#37832;嶅姟鍣?#28051;婂疄鐜?#37825;?#37817;?#37928;?#37832;湴鍖栵紝鎻?#37719;?#29831;?#37712;?#37804;兘?#32029;骞?#37713;?#28751;戠綉缁?#37720;嬪姏銆?/li><li>鍐呭缓HRegionServer鐨勫畷鏈鸿嚜鍔?#37805;?#28598;?#37510;傞?#22249;敤WAL?#28533;Replay杩?#37832;寔涔?#37718;栧埌HDFS鐨?#37825;?#37817;?#37510;?/li><li>?#24434;浠?#37827;?#32514;?#37928;勫拰Hadoop/MapReduce闆嗘垚銆?br /></li></ol>瀹?#37916;?#28051;?#37928;勭?#34425;偣锛?br /><ol><li>WAL鐨凴eplay杩囩?#23338;彲?#20824;?#32048;寰堟參銆?/li><li>鐏?#38341;?#37805;?#28598;嶆瘮杈?#28598;嶆?#20646;紝涔熶細姣?#26440;?#37809;?#37510;?/li><li>Major Compaction?#32048;寮曡捣IO Storm銆?/li><li>銆?#37510;?#37510;?#37510;?br /></li></ol><h2>鍙?#38000;?#38171;?/h2> https://www.mapr.com/blog/in-depth-look-hbase-architecture#.VdNSN6Yp3qx<br /> http://jimbojw.com/wiki/index.php?title=Understanding_Hbase_and_BigTable<br /> http://hbase.apache.org/book.html <br /> http://www.searchtb.com/2011/01/understanding-hbase.html <br /> http://research.google.com/archive/bigtable-osdi06.pdf<img src ="http://www.8634070.com/DLevin/aggbug/426950.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.8634070.com/DLevin/" target="_blank">DLevin</a> 2015-08-22 19:40 <a href="http://www.8634070.com/DLevin/archive/2015/08/22/426950.html#Feedback" target="_blank" style="text-decoration:none;">?#24386;琛?#29831;勮</a></div>]]></description></item><item><title>?#32321;鍏HBase鏋舵?#21230;鏋?#38171;?#28051;锛?/title><link>http://www.8634070.com/DLevin/archive/2015/08/22/426877.html</link><dc:creator>DLevin</dc:creator><author>DLevin</author><pubDate>Sat, 22 Aug 2015 09:44:00 GMT</pubDate><guid>http://www.8634070.com/DLevin/archive/2015/08/22/426877.html</guid><wfw:comment>http://www.8634070.com/DLevin/comments/426877.html</wfw:comment><comments>http://www.8634070.com/DLevin/archive/2015/08/22/426877.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.8634070.com/DLevin/comments/commentRss/426877.html</wfw:commentRss><trackback:ping>http://www.8634070.com/DLevin/services/trackbacks/426877.html</trackback:ping><description><![CDATA[<h2>?#22688;璁?/h2> 鍏?#37721;?#37712;?#38318;?#28003;跨敤鐨?#37828;疢apR鐗?#37832;?#37928;凥adoop鐢?#37804;佺郴缁燂紝鍥?#38000;屼粠MapR鐨勫?#27196;綉鐪嬪埌?#31777;杩欑瘒?#26499;鏂囩珷锛?a href="https://www.mapr.com/blog/in-depth-look-hbase-architecture#.VdMxvWSqqko">An In-Depth Look at the HBase Architecture</a>?#32029;鍘?#37832;兂?#28821;璇?#37711;枃?#32029;?#21159;鑰屽?#20635;灉?#28821;璇戝氨?#28214;瑕佸悇绉嶅挰鏂囧毤瀛楋紝澶?#27001;荤儲?#32029;鍥?#38000;?#37832;?#37826;囧閮?#37714;?#28003;跨敤?#31777;鑷?#23480;?#37928;?#29831;?#29815;?#32029;骞?#28051;斿姞鍏簡鍏?#28000;?#29863;勬簮鐨勫弬鑰冪悊瑙?#28000;?#37721;?#37832;?#27996;鸿嚜宸?#29831;?#23143;?#37934;?#37827;?#28725;?#37711;?#37928;勭悊瑙紝灞?#27996;?#37719;婄炕璇?#37510;?#37719;婂師鍒涘銆?br /> <h2>HBase鏋舵瀯缁勬垚</h2> HBase閲囩敤Master/Slave鏋舵?#21228;寤洪?#21977;兢?#32029;?#30048;闅跺睘浜嶩adoop鐢?#37804;佺郴缁燂紝鐢?#28051;涓嬬被鍨嬭?#20636;偣缁勬垚锛欻Master鑺傜偣銆丠RegionServer鑺傜偣銆乑ooKeeper闆嗙兢?#32029;鑰屽湪搴曞眰?#32029;?#30048;灏?#37825;?#37817;?#28699;樺偍浜嶩DFS涓紝鍥?#38000;屾秹鍙婂埌HDFS鐨凬ameNode銆丏ataNode绛夛紝鎬?#28003;?#32513;撴?#21227;涓?#38171;?br /> <img alt="" src="http://www.8634070.com/images/blogjava_net/dlevin/HBaseArch1.jpg" height="389" width="603" /><br /> 鍏?#28051;?strong>HMaster鑺傜偣</strong>?#25956;浜?#38171;?br /> <ol> <li>绠悊HRegionServer?#32029;瀹?#37916;?#37711;惰礋杞藉?#22250;銆?/li> <li>绠悊鍜屽?#21979;厤HRegion?#32029;姣斿?#20634;湪HRegion split鏃?#37714;嗛?#23942;柊鐨凥Region锛涘湪HRegionServer閫鍑?#37827;?#26473;佺鍏?#37712;?#37928;凥Region?#22476;鍏?#28000;朒RegionServer涓?#37510;?/li> <li>瀹?#37916;DDL鎿?#28003;?#38171;圖ata Definition Language?#32029;namespace?#25328;table鐨勫鍒犳?#30328;紝column familiy鐨勫鍒犳敼绛?#38171;?#37510;?/li> <li>绠悊namespace?#25328;table鐨勫厓鏁?#37817;?#38171;堝疄闄?#28699;樺偍?#28266;HDFS涓?#38171;?#37510;?/li> <li>鏉?#38340;?#37818;?#37714;?#38171;圓CL锛?#37510;?/li> </ol> <strong>HRegionServer鑺傜偣</strong>?#25956;浜?#38171;?br /> <ol> <li>瀛樻?#24807;?#23680;?#24714;鏈湴HRegion銆?/li> <li>璇?#37712;橦DFS?#32029;绠悊Table涓?#37928;?#37825;?#37817;?#37510;?/li> <li>Client鐩?#37818;?#38315;?#26473;HRegionServer璇?#37712;?#37825;?#37817;?#38171;堜粠HMaster涓?#38014;?#37721;?#37711;?#37825;?#37817;紝鎵惧埌RowKey?#22669;?#28266;鐨凥Region/HRegionServer鍚?#38171;?#37510;?/li> </ol> <strong>ZooKeeper闆嗙兢鏄?#37719;忚皟绯?#32513;?/strong>?#32029;?#25956;浜?#38171;?br /> <ol> <li>瀛樻斁鏁?#28051;? HBase闆嗙兢鐨勫厓鏁?#37817;?#28000;?#37721;婇?#21977;兢鐨勭姸鎬佷俊鎭?#37510;?/li> <li>瀹?#37916;癏Master涓讳粠鑺傜偣鐨刦ailover銆?/li> </ol> HBase Client閫?#26473;RPC鏂?#23534;忓拰HMaster銆丠RegionServer閫氫俊锛?#28051;涓狧RegionServer?#24434;浠?#28699;樻斁1000涓狧Region锛?#25652;曞?#20422;able鏁?#37817;?#28699;樺偍浜嶩DFS涓紝鑰孒Region?#22669;澶勭悊鐨?#37825;?#37817;?#28751;介噺?#25328;鏁?#37817;墍?#28266;鐨凞ataNode?#28266;涓?#25443;?#32029;瀹?#37916;?#37825;?#37817;?#37928;?#37832;湴鍖?#38171;?#37825;?#37817;?#37832;湴鍖?#39582;?#28051;?#37828;?#37804;昏?#34249;疄鐜帮紝姣斿?#20634;湪HRegion绉诲姩(濡?#37733;Split)鏃讹紝?#28214;瑕佺瓑涓?#28051;娆Compact?#22688;?#20824;缁?#32513;?#37733;炲埌鏈湴鍖?#37510;?br /> <br /> 鏈潃鍗婄炕璇?#37928;勫師鍒欙紝鍐嶈涓涓?#37510;夾n In-Depth Look At The HBase Architecture銆?#37928;勬?#33333;?#21227;浘锛?br /> <img alt="" src="http://www.8634070.com/images/blogjava_net/dlevin/HBaseArchitecture-Blog-Fig1.png" height="343" width="632" /><br /> 杩?#28051;?#37835;舵?#21227;浘姣?#26440;冩竻鏅?#37928;勮杈句簡HMaster?#25328;NameNode閮芥敮?#23508;澶?#28051;?#37905;?#28598;?#28000;斤紝浣跨敤ZooKeeper?#28533;鍋?#37719;忚皟锛沍ooKeeper骞?#28051;?#37828;?#27996;戣?#32457;橈紝?#30048;涓鑸?#37922;?#28051;?#37721;?#37832;哄櫒缁勬垚涓涓?#38342;嗙兢?#32029;鍐?#38318;?#28003;跨敤PAXOS绠楁硶?#25966;?#23508;涓?#37721;癝erver涓?#37928;?#28051;鍙板畷鏈猴紝涔?#37832;?#28003;跨敤浜斿彴鏈哄櫒鐨勶紝姝?#37827;?#37714;欏彲浠敮鎸佸悓鏃?#28051;?#37721;板畷鏈猴紝鏃?#28751;戜簬鍗?#37825;?#37928;勫畷鏈猴紝?#21159;鑰岄?#24533;潃鏈哄櫒鐨勫?#28850;姞?#32029;?#30048;鐨?#37804;兘涔熶細涓?#38340;?#38171;汻egionServer?#25328;DataNode涓鑸細鏀惧湪鐩?#37722;?#37928;凷erver涓婂疄鐜?#37825;?#37817;?#37928;?#37832;湴鍖?#37510;?br /> <h2>HRegion</h2> HBase浣跨敤RowKey灏嗚?#25353;骞?#37714;囧壊鎴?#28598;?#28051;狧Region?#32029;?#31904;HMaster鐨勮搴紝姣?#28051;狧Region閮?#32510;?#35120;曚簡?#30048;鐨凷tartKey?#25328;EndKey锛堢涓涓狧Region鐨凷tartKey涓?#32460;猴紝鏈鍚?#28051;涓狧Region鐨凟ndKey涓?#32460;?#38171;夛紝鐢?#27996;嶳owKey鏄?#37818;?#25652;?#37928;勶紝鍥?#38000;孋lient?#24434;浠?#38315;?#26473;HMaster蹇?#38315;?#37928;勫畾浣嶆瘡涓猂owKey?#28266;鍝?#28051;狧Region涓?#37510;侶Region鐢盚Master鍒嗛?#23941;埌鐩?#25652;?#37928;凥RegionServer涓紝?#21159;鍚庣敱HRegionServer璐熻矗HRegion鐨勫惎鍔?#37724;岀鐞嗭紝?#25328;Client鐨勯氫俊?#32029;璐熻矗鏁?#37817;?#37928;?#29831;?浣跨敤HDFS)銆傛瘡涓狧RegionServer?#24434;浠?#37722;?#37827;剁?#24714;1000涓?#23480;?#37721;?#37928;凥Region锛?#26473;?#28051;?#37825;板瓧鎬?#28052;堟潵鐨?#38171;熸病鏈変粠?#21804;鐮?#28051;?#37930;嬪埌闄?#37714;讹紝闅?#38316;?#37828;?#37713;?#27996;庣粡楠?#38171;熻秴杩?000涓細寮曡捣鎬兘?#26870;棰?#38171;?strong>?#28533;鍥?#32475;?#26473;?#28051;棶棰?/strong>锛?#37808;熻杩?#28051;?000鐨?#37825;板瓧鏄粠BigTable鐨勮?#28888;枃涓潵鐨?#38171;? Implementation鑺?#38171;?#38171;欵ach tablet server manages a set of tablets(typically we have somewhere between ten to a thousand tablets per tablet server)锛?#37510;?br /> <img alt="" src="http://www.8634070.com/images/blogjava_net/dlevin/HBaseArchitecture-Blog-Fig2.png" height="337" width="724" /><br /> <h2>HMaster</h2> HMaster?#30149;鏈?#37719;曠偣鏁呴?#28360;棶棰橈紝?#24434;浠?#37722;?#37716;?#28598;?#28051;狧Master?#32029;閫?#26473;ZooKeeper鐨凪aster Election鏈?#37714;朵繚璇佸悓鏃?#37721;?#37832;?#28051;涓狧Master鍑?#27996;Active?#23032;鎬侊紝鍏?#28000;?#37928;凥Master鍒?#28598;?#27996;庣儹澶?#28000;界姸鎬?#37510;?#28051;鑸儏鍐?#28051;嬩細鍚?#37716;?#28051;?#28051;狧Master?#32029;闈Active鐨凥Master?#32048;?#30078;鏈?#37928;勫拰Active HMaster閫氫俊浠?#38014;?#37721;?#37711;?#37832;鏂扮姸鎬侊紝?#31904;鑰屼繚璇佸畠鏄?#28729;?#37827;舵洿鏂?#37928;勶紝鍥?#38000;屽?#20635;灉鍚?#37716;簡澶?#28051;狧Master鍙?#38000;屽?#28850;姞?#31777;Active HMaster鐨勮礋鎷?#37510;傚墠鏂囧凡缁?#28000;嬬粛杩囦簡HMaster鐨?#28051;昏?#20346;敤浜嶩Region鐨勫?#21979;?#23941;?#23680;鐞嗭紝DDL(Data Definition Language?#32029;鏃Table鐨勬?#26495;缓銆?#37714;?#38340;?#37510;佷慨鏀?#32475;?鐨勫疄鐜扮?#22811;紝鏃畠涓昏鏈?#28051;?#37826;?#38344;?#37928;勮?#23683;矗锛?br /> <ol> <li>鍗忚皟HRegionServer <ol> <li>鍚?#37716;?#37827;禜Region鐨勫?#21979;厤?#32029;浠?#37721;婅礋杞藉?#22250;鍜屼慨澶?#37827;禜Region鐨勯?#23942;柊鍒嗛厤銆?/li> <li>鐩?#37818;?#38342;嗙兢涓墍鏈塇RegionServer鐨勭姸鎬?閫?#26473;Heartbeat?#25328;鐩戝惉ZooKeeper涓?#37928;勭姸鎬?銆?br /> </li> </ol> </li> <li>Admin鑱岃兘 <ol> <li>鍒涘缓銆?#37714;?#38340;?#37510;佷慨鏀筎able鐨勫畾涔?#37510;?br /> </li> </ol> </li> </ol> <img alt="" src="http://www.8634070.com/images/blogjava_net/dlevin/HBaseArchitecture-Blog-Fig3.png" /><br /> <h2> ZooKeeper锛?#37719;忚皟鑰?/h2> ZooKeeper涓篐Base闆嗙兢鎻愪?#28056;?#24538;皟鏈嶅姟?#32029;?#30048;绠?#37918;嗙潃HMaster?#25328;HRegionServer鐨勭姸鎬?available/alive绛??#32029;骞?#28051;斾細?#28266;?#30048;浠?#28729;?#37832;?#37827;?#38315;氱煡缁橦Master?#32029;?#31904;鑰孒Master?#24434;浠?#28729;?#37916;癏Master涔?#38338;?#37928;刦ailover?#32029;鎴?#28725;?#28729;?#37832;?#37928;凥RegionServer涓?#37928;凥Region闆?#37722;?#37928;勪慨澶?灏嗗畠浠?#37714;嗛厤缁?#37711;?#28000;?#37928;凥RegionServer)銆俍ooKeeper闆嗙兢鏈?#38892;?#28003;跨敤涓?#22183;鎬?#37719;?#29825;?PAXOS鍗?#29825;?淇?#29831;?#23011;?#28051;?#38010;傜偣?#23032;鎬?#37928;?#28051;?#22183;鎬?#37510;?br /> <img alt="" src="http://www.8634070.com/images/blogjava_net/dlevin/HBaseArchitecture-Blog-Fig4.png" height="318" width="703" /><br /> <h2>How The Components Work Together</h2> ZooKeeper鍗忚皟闆嗙兢?#22669;鏈夎?#20636;偣鐨勫叡浜俊鎭紝?#28266;HMaster?#25328;HRegionServer杩?#37818;埌ZooKeeper鍚?#37714;涘缓Ephemeral鑺傜偣?#32029;骞?#28003;跨敤Heartbeat鏈?#37714;?#32513;存寔杩?#28051;?#38010;傜偣鐨勫?#27195;?#33636;姸鎬侊紝濡傛灉鏌?#28051;狤phemeral鑺傜偣瀹?#37825;堬紝鍒橦Master?#32048;鏀跺埌閫氱煡?#32029;骞?#37707;?#37929;?#25652;?#37928;勫?#21229;悊銆?br /> <img alt="" src="http://www.8634070.com/images/blogjava_net/dlevin/HBaseArchitecture-Blog-Fig5.png" height="329" width="722" /><br /> 鍙?#28598;栵紝HMaster閫?#26473;?#37929;戝惉ZooKeeper涓?#37928;凟phemeral鑺傜偣(榛?#29825;?#38171;?hbase/rs/*)?#28533;鐩?#37818;HRegionServer鐨勫姞鍏?#37724;屽畷鏈?#37510;傚湪绗?#28051;涓狧Master杩?#37818;埌ZooKeeper鏃朵細鍒涘缓Ephemeral鑺傜偣(榛?#29825;?#38171;?hbasae/master)?#28533;琛?#32448;篈ctive鐨凥Master?#32029;鍏?#37722;庡姞杩涙潵鐨凥Master鍒?#37929;戝惉璇Ephemeral鑺傜偣?#32029;濡傛灉褰撳墠Active鐨凥Master瀹?#37832;猴紝鍒?#29831;?#38010;傜偣娑?#28598;憋紝鍥?#38000;屽叾浠朒Master寰楀埌閫氱煡?#32029;鑰屽皢鑷?#38892;?#26462;?#37817;?#37812;怉ctive鐨凥Master?#32029;?#28266;鍙?#28051;篈ctive鐨凥Master涔嬪墠?#32029;?#30048;?#32048;鍒涘?#21700;湪/hbase/back-masters/涓嬪?#28056;?#40511;嚜宸?#37928;凟phemeral鑺傜偣銆?br /> <h3> HBase鐨勭涓娆?#29831;?#37712;?/h3> ?#28266;HBase 0.96浠墠?#32029;HBase鏈?#28051;?#28051;?#37911;?#23048;?#37928;Table锛?ROOT-?#25328;.META.锛?#28641;?a href="http://research.google.com/archive/bigtable-osdi06.pdf">BigTable</a>涓?#37928;勮璁?#38171;夛紝鍏?#28051;?ROOT- Table鐨?#28003;?#32515;?#28699;樺偍?#28266;ZooKeeper?#32029;?#30048;瀛樺偍?#31777;.META. Table鐨凴egionInfo?#20426;鎭紝骞?#28051;斿畠鍙?#38003;藉?#27194;湪涓涓狧Region?#32029;鑰?META. Table鍒欏?#27194;偍?#31777;?#25956;鎴稵able鐨凴egionInfo?#20426;鎭紝?#30048;?#24434;浠?#29722;?#37714;囧?#21976;垚澶?#28051;狧Region?#32029;鍥?#38000;屽?#22317;涓娆?#29825;块棶?#25956;鎴稵able鏃讹紝棣?#37711;堜粠ZooKeeper涓?#29831;?#37721;?ROOT- Table?#22669;?#28266;HRegionServer锛涚劧鍚庝粠璇HRegionServer涓?#37837;?#37817;?#29831;?#23033;?#37928;TableName?#32029;RowKey璇?#37721;?META. Table?#22669;?#28266;HRegionServer锛?#37832;鍚庝粠璇HRegionServer涓?#29831;?#37721;?META. Table鐨勫唴瀹?#38000;岃幏鍙?#23005;?#23046;?#29831;?#23033;傞渶瑕?#29825;块棶鐨凥Region?#22669;?#28266;鐨?#28003;?#32515;紝?#21159;鍚?#29825;块棶璇HRegionSever鑾?#37721;?#29831;?#23033;?#37928;?#37825;?#37817;紝杩欓渶瑕?#28051;夋璇?#23033;傛墠鑳芥壘?#22476;?#25956;鎴稵able?#22669;?#28266;鐨?#28003;?#32515;紝?#21159;鍚庣鍥涙璇?#23033;傚紑濮嬭幏鍙?#37930;?#23005;?#37928;?#37825;?#37817;?#37510;?#35120;撶劧涓轰簡鎻?#37719;?#37804;兘?#32029;瀹?#37812;风?#32048;?#32038;瀛?ROOT- Table浣?#32515;?#28000;?#37721;?ROOT-/.META. Table鐨勫唴瀹?#37510;?#28641;?#28051;嬪?#28844;墍绀?#38171;?br /> <img alt="" src="http://www.8634070.com/images/blogjava_net/dlevin/image0030.jpg" height="228" width="399" /><br /> ?#24434;鏄?#37719;?#28003;?#28729;?#37812;风鏈夌紦瀛橈紝?#28266;鍒?#28654;?#38339;舵?#28214;瑕?#28051;夋璇?#23033;傛墠?#20824;鐩村埌?#25956;鎴稵able鐪?#23005;墍?#28266;鐨?#28003;?#32515;?#28052;?#37828;?#37804;兘浣?#28051;?#37928;勶紝鑰?#28051;?#37930;?#37928;?#37832;?#36423;?#29781;佹敮?#23508;?#20549;涔?#28598;?#37928;凥Region鍚?#38171;?#37812;?#29825;?#28725;笹oogle杩?#37837;?#37928;勫叕鍙告潵璇?#37828;渶瑕?#37928;勶紝浣?#37828;?#28725;?#28051;鑸?#37928;勯?#21977;兢?#28533;璇村?#34249;儚骞舵病鏈?#26473;?#28051;?#36423;?#29781;?#37510;傚湪BigTable鐨勮?#28888;枃涓?#29831;达紝姣忚?#23399;ETADATA瀛樺偍1KB宸?#37721;?#37825;?#37817;紝涓?#32475;?#28598;?#28751;?#37928;Tablet(HRegion)?#28266;128MB宸?#37721;筹紝3?#30512;浣?#32515;?#37928;凷chema璁?#29825;彲浠敮?#23508;2^34涓猅ablet(HRegion)銆?#37719;?#28003;垮幓鎺?ROOT- Table?#32029;涔?#26473;樺彲浠敮?#23508;2^17(131072)涓狧Region?#32029; 濡傛灉姣?#28051;狧Region杩?#37828;?28MB?#32029;?#20549;?#27688;鏄?6TB?#32029;杩?#28051;?#29864;屼技涓?#28598;?#28598;紝浣?#37828;?#37916;板湪鐨凥Region鐨?#37832;澶?#28598;?#28751;?#38318;?#28028;氳缃?#37928;勬瘮杈?#28598;紝姣斿鎴戜滑璁?#32515;簡2GB?#32029;姝?#37827;舵敮?#23508;鐨勫灏?#37714;?#37721;?#37812;愪簡4PB?#32029;瀵?#28051;鑸?#37928;勯?#21977;兢?#28533;璇村凡缁?#28598;熶簡?#32029;鍥?#38000;屽湪HBase 0.96浠?#37722;庡幓鎺変簡-ROOT- Table?#32029;鍙?#37715;?#28051;?#26473;?#28051;?#37911;?#23048;?#37928;?#37929;?#35120;曡鍙?#37707;歁eta Table(hbase:meta)?#32029;?#30048;瀛樺偍?#31777;闆嗙兢涓墍鏈夌敤鎴稨Region鐨?#28003;?#32515;俊鎭紝鑰孼ooKeeper鐨勮?#20636;偣涓?/hbase/meta-region-server)瀛樺偍鐨勫垯鐩?#37818;?#37828;?#26473;?#28051;狹eta Table鐨?#28003;?#32515;紝骞?#28051;?#26473;?#28051;狹eta Table濡?#28000;墠鐨?ROOT- Table涓鏍?#37828;?#28051;嶅彲split鐨?#37510;?#26473;?#37837;凤紝瀹?#37812;风?#28266;绗?#28051;娆?#29825;块棶?#25956;鎴稵able鐨勬?#20346;?#23338;氨鍙?#37812;愪簡锛?br /> <ol> <li>?#31904;ZooKeeper(/hbase/meta-region-server)涓?#38014;?#37721;杊base:meta鐨?#28003;?#32515;?#38171;圚RegionServer鐨?#28003;?#32515;?#38171;夛紝?#32038;瀛?#29831;?#28003;?#32515;俊鎭?#37510;?/li> <li>?#31904;HRegionServer涓煡璇敤鎴稵able瀵?#25652;?#29831;?#23033;?#37928;凴owKey?#22669;?#28266;鐨凥RegionServer?#32029;?#32038;瀛?#29831;?#28003;?#32515;俊鎭?#37510;?/li> <li>?#31904;?#29025;璇埌HRegionServer涓?#29831;?#37721;朢ow銆?/li> </ol> ?#31904;杩?#28051;?#26473;囩涓紝鎴戜滑?#24386;鐜板鎴蜂細?#32038;瀛?#26473;欎簺浣?#32515;俊鎭紝?#21159;鑰岀浜屾?#30048;鍙?#37828;紦瀛?#35120;撳墠RowKey瀵?#25652;?#37928;凥Region鐨?#28003;?#32515;紝鍥?#38000;屽?#20635;灉涓?#28051;涓?#29781;佹煡鐨凴owKey涓嶅湪鍚?#28051;涓狧Region涓紝鍒欓渶瑕?#32513;?#32513;煡璇hbase:meta?#22669;?#28266;鐨凥Region?#32029;?#21159;鑰岄?#24533;潃鏃?#38338;?#37928;?#37818;?#32457;伙紝瀹?#37812;风?#32038;瀛?#37928;?#28003;?#32515;俊鎭?#29906;婃潵瓒婂?#27710;紝浠?#38007;?#27996;?#28051;嶉渶瑕?#37712;嶆?#29025;鎵緃base:meta Table鐨勪俊鎭紝闄?#38344;炴煇涓狧Region鍥?#28051;哄畷鏈烘垨Split琚?#32457;诲姩?#32029;姝?#37827;堕渶瑕?#38322;嶆?#29256;煡璇?#39582;?#28051;旀洿鏂扮紦瀛?#37510;?br /> <img alt="" src="http://www.8634070.com/images/blogjava_net/dlevin/HBaseArchitecture-Blog-Fig6.png" height="356" width="590" /><br /> <h3> hbase:meta琛?/h3> hbase:meta琛?#28699;樺偍?#31777;?#22669;鏈夌敤鎴稨Region鐨?#28003;?#32515;俊鎭紝?#30048;鐨凴owKey鏄?#38171;tableName,regionStartKey,regionId,replicaId绛夛紝?#30048;鍙?#37832;塱nfo鍒?#37827;忥紝杩?#28051;?#37714;?#37827;忓寘鍚?#28051;?#28051;?#37714;楋紝浠?#28000;?#37714;?#37714;?#37828;?#38171;歩nfo:regioninfo鍒?#37828;疪egionInfo鐨刾roto鏍?#23534;?#38171;歳egionId,tableName,startKey,endKey,offline,split,replicaId锛沬nfo:server鏍?#23534;?#38171;欻RegionServer瀵?#25652;?#37928;剆erver:port锛沬nfo:serverstartcode鏍?#23534;?#37828;疕RegionServer鐨勫惎鍔?#37827;?#38338;?#37812;?#37510;?br /> <img alt="" src="http://www.8634070.com/images/blogjava_net/dlevin/HBaseArchitecture-Blog-Fig7.png" height="362" width="736" /><br /> <h2>HRegionServer璇?#29785;?/h2> HRegionServer涓鑸拰DataNode?#28266;鍚?#28051;鍙?#37832;哄櫒涓?#26473;?#29723;岋紝瀹?#37916;?#37825;?#37817;?#37928;?#37832;?#37734;版?#37510;侶RegionServer鍖?#37722;?#28598;?#28051;狧Region?#32029;鐢WAL(HLog)銆丅lockCache銆丮emStore銆丠File缁勬垚銆?br /> <ol> <li><strong>WAL鍗砏rite Ahead Log</strong>?#32029;?#28266;鏃?#37832;?#37911;?#37832;?#28051;?#32457;?#28051;篐Log?#32029;?#30048;鏄疕DFS涓?#37928;?#28051;涓枃浠讹紝濡?#37711;?#37722;嶅?#26945;墍琛?#32448;?#37928;勶紝?#22669;鏈?#37712;欐搷浣?#38318;戒細鍏堜繚璇佸皢鏁?#37817;?#37712;?#37711;?#26473;?#28051;狶og?#26499;浠?#37722;庯紝?#22688;?#32048;鐪?#23005;洿鏂MemStore?#32029;鏈鍚?#37712;?#37711;HFile涓?#37510;傞?#22249;敤杩?#32457;嶆寮忥紝?#24434;浠?#28103;?#29831;丠RegionServer瀹?#37832;?#37722;庯紝鎴戜滑渚濈劧?#24434;浠粠璇Log?#26499;浠?#28051;?#29831;?#37721;?#37825;?#37817;紝Replay?#22669;鏈?#37928;勬搷浣滐紝鑰?#28051;?#38007;?#27996;?#37825;?#37817;?#28051;?#28598;?#37510;?#26473;?#28051;狶og?#26499;浠朵細?#30078;鏈烺oll鍑烘柊鐨勬枃浠?#38000;屽垹闄?#37827;?#37928;勬枃浠??#20549;浜?#23480;叉寔涔?#37718;栧埌HFile涓?#37928;凩og?#24434;浠?#37714;?#38340;?銆俉AL?#26499;浠?#28699;樺偍?#28266;/hbase/WALs/${HRegionServer_Name}鐨?#37929;?#35120;?#28051;??#28266;0.94涔嬪墠?#32029;瀛樺偍?#28266;/hbase/.logs/鐩?#35120;?#28051;??#32029;涓鑸?#28051;涓狧RegionServer鍙?#37832;?#28051;涓猈AL瀹?#28186;嬶紝涔熷氨鏄?#29831;?#28051;涓狧RegionServer鐨勬墍鏈塛AL鍐?#38318;芥槸涓?#29723;?#37928;??#27688;鍍log4j鐨?#37827;?#36423;?#37712;?#28052;?#37828;?#28051;?#29723;?#37928;??#32029;杩?#35120;撶劧?#32048;寮曡捣鎬兘?#26870;棰橈紝鍥?#38000;屽湪HBase 1.0涔嬪悗?#32029;閫?#26473;?a href="https://issues.apache.org/jira/browse/HBASE-5699">HBASE-5699</a>瀹?#37916;颁簡澶?#28051;猈AL骞惰鍐?MultiWAL)?#32029;璇?#28729;?#37916;?#38322;囩敤HDFS鐨勫涓?#32480;?#38316;?#37712;欙紝浠?#37719;?#28051;狧Region涓?#37719;?#28003;?#37510;?#37711;?#27996;嶹AL?#24434;浠?#37721;?#38000;僕ikipedia鐨?a href="https://en.wikipedia.org/wiki/Write-ahead_logging">Write-Ahead Logging</a>銆傞?#36720;究鍚愭涓鍙紝鑻?#37826;囩増鐨?#32513;村熀鐧?#32457;戠?#29114;劧鑳芥鏃犲?#23338;姏鐨?#23005;父璁块棶?#31777;?#32029;杩?#37828;?#37836;?#28051;狦FW鐨?#37924;?#36423;?#26473;?#37828;?#28000;?#37722;?#37928;勫父鎬?#38171;?/li> <li><strong>BlockCache鏄?#28051;涓?#29831;荤紦瀛?/strong>?#32029;鍗?#8220;寮曠敤灞閮?#37804;?#8221;鍘熺悊锛?#28052;?#25652;旂敤浜嶤PU?#32029;<a href="http://baike.baidu.com/link?url=Dh2u9KvowXcl2PloHJFTB9vEOoVS3WxPhDCVvbQGL_piyKFQ2iTHYAYf5mLAsFrcBUig6NC7A4-Aki61qnGCTK#3_1">鍒嗙闂村眬閮?#37804;拰鏃?#38338;村眬閮?#37804;?/a>?#32029;绌?#38338;村眬閮?#37804;?#37828;?#37816;嘋PU?#28266;鏌?#28051;鏃?#37714;婚渶瑕佹煇涓?#37825;?#37817;紝?#20549;涔?#37832;?#23536;?#28598;?#37928;勬鐜囧湪涓涓?#37827;?#37714;诲畠?#28214;瑕?#37928;?#37825;?#37817;湪鍏?#38340;?#26473;?#38171;?#37827;?#38338;村眬閮?#37804;?#37828;?#37816;?#37836;?#28051;?#37825;?#37817;湪琚?#29825;块棶杩?#28051;娆?#37722;庯紝?#30048;鏈?#23536;?#28598;?#37928;勬鐜囧湪涓?#28052;?#37928;勫?#21976;潵浼氳鍐嶆鐨勮?#22359;棶锛夛紝灏?#37825;?#37817;?#26864;?#29831;?#37721;栧埌鍐?#28699;?#28051;紝浠?#37819;?#37719;?#29831;?#37928;?#37804;兘銆侶Base涓?#37819;愪緵涓?#32457;岯lockCache鐨勫疄鐜?#38171;?#27035;?#29825;on-heap LruBlockCache?#25328;BucketCache(閫氬父鏄痮ff-heap)銆?#38315;氬父BucketCache鐨?#37804;?#38003;借宸?#27996;嶭ruBlockCache?#32029;?#21159;鑰岀敱浜嶨C鐨勫?#21342;搷?#32029;LruBlockCache鐨勫欢杩熶細鍙?#37928;?#28051;嶇?#20914;畾?#32029;鑰孊ucketCache鐢?#27996;?#37828;?#38007;?#23480;?#32480;悊BlockCache?#32029;鑰?#28051;嶉渶瑕丟C?#32029;鍥?#38000;屽畠鐨勫欢杩?#38315;氬父姣?#26440;?#32459;冲畾?#32029;杩?#28052;?#37828;?#37832;?#27996;?#37827;?#37706;欓渶瑕?#38315;夌敤BucketCache鐨勫師鍥?#37510;?#26473;欑瘒鏂囩珷<a href="http://www.n10k.com/blog/blockcache-101/">BlockCache101</a>瀵筼n-heap?#25328;off-heap鐨凚lockCache鍋氫簡璇?#32513;?#37928;勬瘮杈?#37510;?/li><strong> </strong><li><strong>HRegion鏄?#28051;涓猅able涓?#37928;?#28051;涓猂egion?#28266;涓涓狧RegionServer涓?#37928;勮杈?/strong>銆?#28051;涓猅able?#24434;浠?#37832;?#28051;涓?#37812;?#28598;?#28051;猂egion?#32029;浠?#28000;彲浠湪涓涓?#37929;?#37722;?#37928;凥RegionServer涓婏紝涔熷彲浠?#37714;嗗?#20904;湪涓嶅悓鐨凥RegionServer涓婏紝涓涓狧RegionServer?#24434;浠?#37832;?#28598;?#28051;狧Region?#32029;浠?#28000;?#37714;?#37714;?#28766;?#27996;?#28051;嶅悓鐨Table銆侶Region鐢?#28598;?#28051;猄tore(HStore)鏋勬垚?#32029;姣?#28051;狧Store瀵?#25652;斾簡涓涓猅able?#28266;杩?#28051;狧Region涓?#37928;?#28051;涓狢olumn Family?#32029;鍗?#23011;?#28051;狢olumn Family?#27688;鏄?#28051;涓?#38342;?#28051;?#37928;勫?#27194;偍鍗?#37711;冿紝鍥?#38000;?#37832;濂藉?#21975;叿鏈?#37929;?#26473;慖O鐗?#37804;?#37928;凜olumn瀛樺偍?#28266;涓涓狢olumn Family?#32029;浠?#28729;?#37916;?#26946;?#37825;?#29831;?#37721;?鏁?#37817;?#28766;閮?#37804;?#37720;熺?#21997;紝?#24434;浠?#37819;?#26946;樼紦瀛?#37928;勫懡涓?#37916;?銆侶Store鏄疕Base涓?#28699;樺偍鐨?#37837;?#36423;冿紝?#30048;瀹?#37916;颁簡璇?#37712;橦DFS鍔熻兘?#32029;涓涓狧Store鐢?#28051;涓狹emStore ?#25328;0涓?#37812;?#28598;?#28051;猄toreFile缁勬垚銆?br /> <ol> <li><strong>MemStore鏄?#28051;涓?#37712;欑紦瀛?/strong>(In Memory Sorted Buffer)?#32029;?#22669;鏈?#37825;?#37817;?#37928;勫?#27407;湪瀹屾垚WAL鏃?#36423;?#37712;?#37722;庯紝?#32048; 鍐?#37711;MemStore涓紝鐢盡emStore鏍?#37817;?#28051;?#30078;鐨勭?#26945;硶灏?#37825;?#37817;瓼lush?#22476;?#28276;灞侶DFS?#26499;浠?#28051;?HFile)?#32029;閫氬父姣?#28051;狧Region涓?#37928;勬瘡涓? Column Family鏈?#28051;涓?#38007;?#23480;?#37928;凪emStore銆?/li> <li><strong>HFile(StoreFile) ?#25956;浜?#28699;樺偍HBase鐨?#37825;?#37817;?Cell/KeyValue)</strong>銆傚湪HFile涓?#37928;?#37825;?#37817;?#37828;?#37816;RowKey銆丆olumn Family銆丆olumn鎺?#25652;忥紝瀵?#37929;?#37722;?#37928;凜ell(鍗?#26473;?#28051;?#28051;?#37706;?#38318;?#28051;鏍??#32029;鍒欐寜timestamp鍊?#25652;?#37818;?#37714;?#37510;?/li> </ol> </li> </ol> <img alt="" src="http://www.8634070.com/images/blogjava_net/dlevin/HBaseArchitecture-Blog-Fig8.png" /><br /> 铏界劧涓?#38344;?#26473;?#23534;?#37733;惧?#26336;幇鐨?#37828;?#37832;鏂?#37928;凥RegionServer鐨勬?#33333;瀯(浣?#37828;?#39582;?#28051;?#37828;偅涔?#37928;勭?#21095;)?#32029;浣?#37828;垜涓鐩?#23011;?#26440;?#37728;?#23046;?#37930;?#28000;?#28051;?#26473;?#23534;?#37733;撅紝鍗?#28003;垮畠灞曠幇鐨勫簲璇?#37828;?.94浠墠鐨勬?#33333;瀯銆?br /> <img alt="" src="http://www.8634070.com/images/blogjava_net/dlevin/image0060.jpg" height="347" width="553" /><br /> <h3> HRegionServer涓?#37825;?#37817;?#37712;欐?#20346;?#23338;浘瑙?/h3> 褰撳鎴风?#24386;?#25443;涓涓狿ut璇?#23033;?#37827;讹紝棣?#37711;堝畠?#31904;hbase:meta琛?#28051;煡鍑?#29831;Put鏁?#37817;?#37832;缁堥渶瑕佸幓鐨凥RegionServer銆傜劧鍚庡鎴风灏哖ut璇?#23033;傚彂閫?#32513;?#37929;?#25652;?#37928;凥RegionServer?#32029;?#28266;HRegionServer涓畠棣?#37711;堜細灏?#29831;Put鎿?#28003;?#37712;?#37711;WAL鏃?#36423;楁枃浠?#28051;?Flush?#22476;纾?#37929;?#28051;?銆?br /><img alt="" src="http://www.8634070.com/images/blogjava_net/dlevin/HBaseArchitecture-Blog-Fig9.png" height="363" width="716" /><br /> 鍐欏畬WAL鏃?#36423;楁枃浠?#37722;庯紝HRegionServer鏍?#37817;甈ut涓?#37928;TableName?#25328;RowKey鎵惧埌瀵?#25652;?#37928;凥Region?#32029;骞?#37837;?#37817;Column Family鎵惧埌瀵?#25652;?#37928;凥Store?#32029;骞?#28751;哖ut鍐?#37711;埌璇HStore鐨凪emStore涓?#37510;?#23005;?#37827;?#37712;?#37812;?#37716;燂紝骞?#26473;?#37733;?#38315;氱煡瀹?#37812;风銆?br /><img alt="" src="http://www.8634070.com/images/blogjava_net/dlevin/HBaseArchitecture-Blog-Fig10.png" height="298" width="664" /><br /><h3>MemStore Flush<br /></h3>MemStore鏄?#28051;涓狪n Memory Sorted Buffer?#32029;?#28266;姣?#28051;狧Store涓?#38318;?#37832;?#28051;涓狹emStore?#32029;鍗冲畠鏄?#28051;涓狧Region鐨?#28051;涓狢olumn Family瀵?#25652;?#28051;涓?#28729;?#28186;?#37510;傚畠鐨?#37818;?#37714;?#26916;?#25652;?#28000;RowKey銆丆olumn Family銆丆olumn鐨勯搴?#28000;?#37721;奣imestamp鐨勫?#25652;忥紝濡?#28051;嬫墍绀?#38171;?br /><img alt="" src="http://www.8634070.com/images/blogjava_net/dlevin/HBaseArchitecture-Blog-Fig11.png" height="351" width="719" /><br />姣?#28051;娆Put/Delete璇?#23033;?#38318;芥槸鍏?#37712;?#37711;埌MemStore涓紝褰揗emStore婊?#37722;庝細Flush鎴?#28051;涓?#37826;?#37928;凷toreFile(搴曞?#20634;疄鐜版槸HFile)?#32029;鍗?#28051;涓狧Store(Column Family)?#24434;浠?#37832;?涓?#37812;?#28598;?#28051;猄toreFile(HFile)銆?#37832;?#28000;?#28051;?#28051;夌?#23942;儏鍐靛彲浠?#29785;彂MemStore鐨凢lush鍔?#28003;滐紝<strong>?#28214;瑕?#23049;?#37808;?#37928;?#37828;疢emStore鐨?#37832;灏廎lush鍗?#37711;?#37828;疕Region鑰?#28051;?#37828;?#37719;?#28051;狹emStore</strong>銆?#37817;?#29831;?#26473;?#37828;疌olumn Family鏈?#28051;?#37825;?#38340;?#37714;?#37928;勫叾涓?#28051;涓?#37720;?#37733;狅紝浼拌鏄?#37733;?#28051;?#28598;?#28598;?#37928;凜olumn Family涓?#25443;Flush?#32048;寮曡捣鎬兘?#26870;棰?#38171;熷叿浣撳師鍥犳湁寰?#38000;?#29831;?#37510;?br /><ol><li>褰?#28051;涓狧Region涓?#37928;勬墍鏈塎emStore鐨勫灏?#37804;诲拰?#31220;杩囦簡hbase.hregion.memstore.flush.size鐨勫灏忥紝榛?#29825;?28MB銆?#23005;?#37827;?#35120;撳墠鐨凥Region涓墍鏈?#37928;凪emStore?#32048;Flush?#22476;HDFS涓?#37510;?/li><li>褰撳叏灞MemStore鐨勫灏忚秴杩囦簡hbase.regionserver.global.memstore.upperLimit鐨勫灏忥紝榛?#29825;?0锛?#37928;勫唴瀛?#28003;跨敤?#22138;銆?#23005;?#37827;?#35120;撳墠HRegionServer涓墍鏈塇Region涓?#37928;凪emStore閮戒細Flush?#22476;HDFS涓紝Flush椤?#25652;?#37828;疢emStore澶?#28751;?#37928;勫?#25652;?#38171;?#28051;涓狧Region涓墍鏈塎emStore鎬诲拰浣?#28051;?#29831;HRegion鐨凪emStore鐨勫灏?#26473;?#37828;?#38315;?#37721;?#37832;澶?#37928;凪emStore浣?#28051;?#37721;?#38000;?#38171;?#37832;?#23536;?#38000;?#29831;?#38171;夛紝鐩村埌鎬?#28003;?#37928;凪emStore浣跨敤?#22138;浣?#27996;巋base.regionserver.global.memstore.lowerLimit?#32029;榛?#29825;?8%鐨勫唴瀛?#28003;跨敤?#22138;銆?/li><li>褰撳墠HRegionServer涓璚AL鐨勫灏忚秴杩囦簡hbase.regionserver.hlog.blocksize * hbase.regionserver.max.logs鐨?#37825;伴噺?#32029;褰撳墠HRegionServer涓墍鏈塇Region涓?#37928;凪emStore閮戒細Flush?#22476;HDFS涓紝Flush浣跨敤鏃?#38338;?#26916;?#25652;忥紝鏈鏃?#37928;凪emStore鍏團lush鐩村埌WAL鐨?#37825;伴噺灏戜簬hbase.regionserver.hlog.blocksize * hbase.regionserver.max.logs銆?a href="http://blog.sematext.com/2012/07/16/hbase-memstore-what-you-should-know/">杩欓噷</a>璇?#26473;?#28051;?#28051;?#37929;?#28052;?#37928;勯粯璁?#28598;?#28751;?#37828;?GB?#32029;?#29025;?#21804;鐮侊紝hbase.regionserver.max.logs榛?#29825;?#37706;?#37828;?2?#32029;鑰宧base.regionserver.hlog.blocksize鏄疕DFS鐨勯粯璁blocksize?#32029;32MB銆?#28003;?#28051;嶇鎬?#28052;?#37837;凤紝鍥?#28051;?#26473;?#28051;?#28598;?#28751;忚秴杩?#38340;?#37714;?#23534;曡捣鐨凢lush涓?#37828;?#28051;浠?#28610;?#27996;嬶紝?#24434;鑳藉?#26337;捣?#26289;鏃?#38338;?#37928;勫欢杩燂紝鍥?#38000;?#26473;欑瘒鏂囩珷缁?#37928;勫?#40511;锛?#8220;<strong style="color: #339966; font-family: STHeiti; font-size: medium; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px;">Hint</strong><span style="color: #339966; font-family: STHeiti; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; display: inline !important; float: none;">: keep hbase.regionserver.hlog.blocksize * hbase.regionserver.maxlogs just a bit above hbase.regionserver.global.memstore.lowerLimit * HBASE_HEAPSIZE.</span>”銆?#39582;?#28051;旈渶瑕?#23049;?#37808;忥紝<a href="http://hbase.apache.org/book.html#_memstore_flush">杩欓噷</a>缁?#37928;勬弿杩版槸鏈夐鐨?铏界劧?#30048;鏄?#28729;?#37826;?#37928;勬枃妗?銆?br /></li></ol>?#28266;MemStore Flush杩囩涓紝杩樹細?#28266;灏?#38318;?#26473;藉姞涓浜沵eta鏁?#37817;紝鍏?#28051;氨鍖?#37815;Flush鏃?#37832;澶?#37928;刉AL sequence鍊硷紝浠?#37723;?#29831;塇Base杩?#28051;猄toreFile鍐?#37711;?#37928;?#37832;鏂?#37825;?#37817;?#37928;勫簭鍒楋紝?#20549;涔堝湪Recover鏃跺氨鐩村埌?#31904;鍝?#38322;屽紑濮?#37510;傚湪HRegion鍚?#37716;?#37827;讹紝杩?#28051;猻equence浼氳璇?#37721;栵紝骞?#37721;?#37832;澶?#37928;?#28003;?#28051;?#28051;?#28051;娆洿鏂?#37827;?#37928;勮捣濮媠equence銆?br /><img alt="" src="http://www.8634070.com/images/blogjava_net/dlevin/HBaseArchitecture-Blog-Fig12.png" height="248" width="622" /><br /><h2> HFile鏍?#23534;?/h2>HBase鐨?#37825;?#37817;?#28000;KeyValue(Cell)鐨勫舰寮?#26916;?#25652;?#37928;勫?#27194;偍?#28266;HFile涓紝?#28266;MemStore鐨凢lush杩囩涓?#37922;?#37812;怘File?#32029;鐢?#27996;嶮emStore涓?#28699;樺偍鐨凜ell閬?#23536;?#37929;?#37722;?#37928;?#37818;?#37714;?#26916;?#25652;忥紝鍥?#38000;孎lush杩囩鏄?#26916;?#25652;?#37712;欙紝鎴戜滑鐩村埌纾?#37929;?#37928;勯搴?#37712;?#37804;兘寰堥?#27208;紝鍥?#28051;?#28051;嶉渶瑕?#28051;嶅仠鐨?#32457;诲姩纾?#37929;樻?#22252;拡銆?br /><img alt="" src="http://www.8634070.com/images/blogjava_net/dlevin/HBaseArchitecture-Blog-Fig13.png" height="351" width="698" /><br />HFile鍙?#38000;傿igTable鐨凷STable?#25328;Hadoop鐨?a href="http://hadoop.apache.org/docs/current/api/org/apache/hadoop/io/file/tfile/TFile.html">TFile</a>瀹?#37916;帮紝?#31904;HBase?#32017;濮嬪埌鐜板湪?#32029;HFile缁忓?#21973;簡涓?#28051;?#37911;?#37832;紝鍏?#28051;V2?#28266;0.92寮?#37711;紝V3?#28266;0.98寮?#37711;?#37510;傞鍏堟?#25116;滑?#28533;鐪?#28051;涓媀1鐨?#37837;?#23534;?#38171;?br /><img src="http://www.8634070.com/images/blogjava_net/dlevin/image0080.jpg" alt="" height="160" border="0" width="554" /><br />V1鐨凥File鐢?#28598;?#28051;狣ata Block銆丮eta Block銆丗ileInfo銆丏ata Index銆丮eta Index銆乀railer缁勬垚?#32029;鍏?#28051;璂ata Block鏄疕Base鐨?#37832;灏忓?#27194;偍鍗?#37711;冿紝?#28266;?#22688;?#26499;涓?#37819;愬埌鐨凚lockCache?#27688;鏄?#37737;?#27996;嶥ata Block鐨勭紦瀛?#37928;?#37510;?#28051;涓狣ata Block鐢?#28051;涓瓟鏁板拰涓绯?#37714;?#37928;凨eyValue(Cell)缁勬垚?#32029;?#29919;鏁版槸涓涓?#38341;?#37832;?#37928;?#37825;板?#26955;紝?#25956;浜?#29723;?#32448;?#26473;?#37828;?#28051;涓狣ata Block?#34987;鍨嬶紝浠?#36423;?#38315;?#37929;戞祴杩?#28051;狣ata Block鐨?#37837;?#23534;忥紝闃?#23005;?#37825;?#37817;?#37928;?#37934;村銆侱ata Block鐨勫灏忓彲浠湪鍒涘缓Column Family鏃惰缃?HColumnDescriptor.setBlockSize())?#32029;榛?#29825;?#37706;?#37828;?4KB?#32029;澶?#37721;?#37928;凚lock鏈?#37714;?#27996;?#26916;?#25652;廠can?#32029;灏?#37721;稡lock鍒?#27996;?#38341;?#37832;烘煡璇紝鍥?#38000;岄渶瑕?#37833;?#29723;?#37510;侻eta鍧?#37828;彲閫?#37928;勶紝FileInfo鏄?#37733;哄畾?#26289;搴?#37928;勫?#26955;紝?#30048;绾?#35120;曚簡?#26499;浠?#37928;?#28051;浜汳eta?#20426;鎭紝渚嬪锛欰VG_KEY_LEN, AVG_VALUE_LEN, LAST_KEY, COMPARATOR, MAX_SEQ_ID_KEY绛?#37510;侱ata Index?#25328;Meta Index绾?#35120;曚簡姣?#28051;狣ata鍧楀拰Meta鍧?#37928;勫叾瀹炵偣銆?#37832;?#37720;嬬缉鏃?#28598;?#28751;?#37510;並ey(?#25443;濮婻owKey锛?绛?#37510;俆railer绾?#35120;曚簡FileInfo銆丏ata Index銆丮eta Index鍧?#37928;勮捣濮?#28003;?#32515;紝Data Index?#25328;Meta Index绱?#23534;?#37928;?#37825;伴噺绛?#37510;?#37711;?#28051;璅ileInfo?#25328;Trailer鏄?#37733;哄畾?#26289;搴?#37928;?#37510;?br /><br />HFile閲岄潰鐨勬瘡涓狵eyValue瀵瑰氨鏄?#28051;涓畝鍗?#37928;刡yte鏁?#32513;?#37510;?#28003;?#37828;?#26473;?#28051;猙yte鏁?#32513;勯?#23684;潰鍖?#37722;簡寰?#28598;?#26916;癸紝骞?#28051;?#37832;?#37733;哄畾鐨?#32513;撴瀯銆?#37812;戜滑?#28533;鐪嬬湅閲岄潰鐨勫叿浣?#32513;撴瀯锛?br /><img src="http://www.8634070.com/images/blogjava_net/dlevin/image0090.jpg" alt="" height="93" border="0" width="553" /><br />?#32017;濮?#37828;?#28051;?#28051;?#37733;哄畾?#26289;搴?#37928;?#37825;?#37706;硷紝鍒?#37714;?#29723;?#32448;Key鐨勯暱搴拰Value鐨勯暱搴?#37510;傜揣鎺潃鏄疜ey?#32029;?#32017;濮?#37828;?#37733;哄畾?#26289;搴?#37928;?#37825;?#37706;硷紝琛?#32448;篟owKey鐨勯暱搴紝绱?#37818;潃鏄? RowKey?#32029;?#21159;鍚?#37828;?#37733;哄畾?#26289;搴?#37928;?#37825;?#37706;硷紝琛?#32448;Family鐨勯暱搴紝?#21159;鍚?#37828;疐amily?#32029;鎺潃鏄疩ualifier?#32029;?#21159;鍚?#37828;?#28051;?#28051;?#37733;哄畾?#26289;搴?#37928;?#37825;?#37706;硷紝琛?#32448;Time Stamp?#25328;Key Type锛圥ut/Delete锛?#37510;俈alue閮?#37714;嗘病鏈?#26473;?#28052;?#28598;嶆潅鐨?#32513;撴?#21238;紝?#27688;鏄?#32510;?#32494;?#37928;?#27996;?#26473;?#37714;?#37825;?#37817;簡銆?strong>闅忕潃HFile鐗?#37832;?#26473;佺?#20249;紝KeyValue(Cell)鐨?#37837;?#23534;?#39582;?#37832;彂鐢?#28598;?#28598;?#37721;樺?#26677;紝鍙?#37828;湪V3鐗?#37832;紝灏?#38318;?#23075;诲姞?#31777;涓涓彲閫?#37928;Tag鏁?#32513;?/strong>銆?br /> <br />HFileV1鐗?#37832;?#37928;勫湪瀹?#38340;?#28003;跨敤杩囩涓彂鐜板畠鍗犵敤鍐?#28699;?#28598;氾紝骞?#28051;Bloom File?#25328;Block Index?#32048;鍙?#37928;勫緢澶紝鑰屽?#26337;捣鍚?#37716;?#37827;?#38338;村彉?#26289;銆?#37711;?#28051;?#23011;?#28051;狧File鐨凚loom Filter?#24434;浠?#28583;為暱?#22476;100MB?#32029;杩欏湪?#29025;璇?#37827;朵細寮曡捣鎬兘?#26870;棰橈紝鍥?#28051;烘?#24532;?#29025;璇?#37827;堕渶瑕佸姞杞藉?#33333;煡璇Bloom Filter?#32029;100MB鐨凚loom Filer?#32048;寮曡捣寰?#28598;?#37928;勫欢杩?#38171;涘彟涓涓紝Block Index?#28266;涓涓狧RegionServer?#24434;?#20824;?#32048;澧為暱?#22476;鎬?#37711;?GB?#32029;HRegionServer?#28266;鍚?#37716;?#37827;堕渶瑕佸?#22557;姞杞芥墍鏈?#26473;欎簺Block Index?#32029;鍥?#38000;屽?#28850;姞?#31777;鍚?#37716;?#37827;?#38338;?#37510;?#28051;轰簡瑙?#37712;?#26473;欎?#28066;棶棰橈紝?#28266;0.92鐗?#37832;?#28051;?#23534;?#37711;HFileV2鐗?#37832;?#38171;?br /><img src="http://www.8634070.com/images/blogjava_net/dlevin/hfilev2.png" alt="" height="418" border="0" width="566" /><br />?#28266;杩?#28051;?#37911;?#37832;?#28051;紝Block Index?#25328;Bloom Filter娣诲姞?#22476;?#31777;Data Block涓?#38338;达紝鑰?#26473;?#32457;嶈璁?#37722;?#37827;?#28052;?#37713;?#28751;戜簡鍐?#37928;勫唴瀛?#28003;跨敤?#22138;锛涘彟澶栵紝涓轰簡鎻?#37719;囧惎鍔?#38315;?#25652;紝?#28266;杩?#28051;?#37911;?#37832;?#28051;?#26473;?#23534;?#37711;簡寤?#26473;?#29831;?#37928;勫?#29115;兘?#32029;鍗冲湪HFile鐪?#23005;?#29722;?#28003;跨敤鏃舵墠瀵?#37711;?#26473;涜瑙?#37835;?#37510;?br /><br />FileV3鐗?#37832;?#37737;?#37832;拰V2鐗?#37832;?#37929;?#23011;旓紝骞舵病鏈?#28598;?#28598;?#37928;勬敼鍙橈紝?#30048;?#28266;KeyValue(Cell)灞傞潰涓?#23075;诲姞?#31777;Tag鏁?#32513;?#37928;勬敮?#23508;锛涘?#36346;湪FileInfo缁撴瀯涓?#23075;诲姞?#31777;?#25328;Tag鐩?#37711;?#37928;?#28051;?#28051;?#28699;?#23048;?#37510;?#37711;?#27996;庡叿浣揌File鏍?#23534;忔?#26047;寲浠嬬?#23951;紝?#24434;浠?#37721;?#38000;?a href="http://hbase.apache.org/book.html#_hfile_format_2">杩欓噷</a>銆?br /><br />瀵笻FileV2鏍?#23534;忓叿浣撳?#21976;瀽?#32029;?#30048;鏄?#28051;涓?#28598;氬眰鐨勭被B+鏍戠储寮曪紝閲囩敤杩?#32457;嶈璁紝?#24434;浠?#28729;?#37916;版煡鎵?#28051;嶉渶瑕?#29831;?#37721;?#37825;?#28051;枃浠?#38171;?br /><img alt="" src="http://www.8634070.com/images/blogjava_net/dlevin/HBaseArchitecture-Blog-Fig14.png" height="349" width="688" /><br />Data Block涓?#37928;凜ell閮芥槸鍗囧簭鎺?#37714;楋紝姣?#28051;猙lock閮?#37832;夊畠鑷?#23480;?#37928;凩eaf-Index?#32029;姣?#28051;狟lock鐨?#37832;鍚?#28051;涓狵ey琚?#37824;惧叆Intermediate-Index涓紝Root-Index鎸囧悜Intermediate-Index銆傚湪HFile鐨?#37832;?#28751;?#26473;?#37832;塀loom Filter?#25956;浜?#36423;?#38315;熷畾浣嶉偅涔堟病鏈夊湪鏌?#28051;狣ata Block涓?#37928;凴ow锛汿imeRange?#20426;鎭敤浜庣?#27411;偅浜?#28003;跨敤鏃?#38338;存煡璇?#37928;勫弬鑰?#37510;傚湪HFile鎵撳紑鏃讹紝杩欎簺绱?#23534;曚俊鎭?#38318;借?#23006;杞藉?#26421;繚瀛樺湪鍐?#28699;?#28051;紝浠?#28583;炲姞浠?#37722;?#37928;?#29831;?#37721;?#37804;兘銆?br /><br />杩欑?#22247;氨鍏?#37712;欏埌杩欓噷?#32029;鏈畬寰?#32513;?#37510;?#37510;?#37510;?#37510;?br /><br /> <h2>鍙?#38000;?#38171;?/h2> https://www.mapr.com/blog/in-depth-look-hbase-architecture#.VdNSN6Yp3qx<br /> http://jimbojw.com/wiki/index.php?title=Understanding_Hbase_and_BigTable<br /> http://hbase.apache.org/book.html <br /> http://www.searchtb.com/2011/01/understanding-hbase.html <br /> http://research.google.com/archive/bigtable-osdi06.pdf<img src ="http://www.8634070.com/DLevin/aggbug/426877.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.8634070.com/DLevin/" target="_blank">DLevin</a> 2015-08-22 17:44 <a href="http://www.8634070.com/DLevin/archive/2015/08/22/426877.html#Feedback" target="_blank" style="text-decoration:none;">?#24386;琛?#29831;勮</a></div>]]></description></item><item><title>Log4J寮曡捣鐨?#32459;嬪簭滆姝烩?/title><link>http://www.8634070.com/DLevin/archive/2015/08/13/426751.html</link><dc:creator>DLevin</dc:creator><author>DLevin</author><pubDate>Thu, 13 Aug 2015 08:28:00 GMT</pubDate><guid>http://www.8634070.com/DLevin/archive/2015/08/13/426751.html</guid><wfw:comment>http://www.8634070.com/DLevin/comments/426751.html</wfw:comment><comments>http://www.8634070.com/DLevin/archive/2015/08/13/426751.html#Feedback</comments><slash:comments>4</slash:comments><wfw:commentRss>http://www.8634070.com/DLevin/comments/commentRss/426751.html</wfw:commentRss><trackback:ping>http://www.8634070.com/DLevin/services/trackbacks/426751.html</trackback:ping><description><![CDATA[<h2>?#26870;棰樿捣鍥?/h2> 渚濈劧鏄湪浣跨敤GemFire鐨勯?#21977;兢涓紝鎴戜滑?#24386;鐜?#37707;?#28751;斾細鍑?#37916;?#28051;浜汫emFire鐨凢unction鎵?#29723;岀壒鍒?#37809;紝骞?#28051;旇秴杩囦簡涓?#37714;嗛挓锛?#28051;轰簡淇?#29831;?#37825;?#37817;?#37928;?#28051;?#22183;鎬紝鎴戜滑?#28266;鍐?#28052;嬪墠?#28214;瑕佸?#22559;嬁涓涓狶ock?#32029;鍥?#28051;?#28051;嶈?#33445;瘡涓狵ey閮藉搴?#28051;涓狶ock?#32029;鍥?#38000;屾?#25116;滑浣跨敤?#31777;Guava鐨凷tripe Lock锛?#37711;?#27996;嶴tripe Lock?#24434;浠?#37721;?#38000;?a href="http://www.8634070.com/DLevin/archive/2013/12/25/407990.html">杩欓噷</a>锛夛紝鑰?#28051;?#26473;?#28051;狶ock鏈?#38892;?#37812;戜滑鎸囧?#27691;簡2鍒嗛挓鐨勮秴鏃?#37827;?#38338;达紝鍥?#38000;屽?#20635;灉鍐欒秴杩?#28051;?#37714;嗛?#29122;紝鎴戜滑?#27688;?#32048;鏀跺埌Exception锛?#37510;?#26473;?#28051;棶棰?#37711;跺疄宸?#32513;?#37733;版?#39041;簡鎴戜滑濂藉嚑骞翠簡?#32029;鍒氬墠娈?#37827;?#38338;达紝鎴戜滑?#24386;鐜伴暱鏃?#38338;?#37928;凷top-The-World GC?#32048;寮曡捣杩?#28051;棶棰橈紝鑰?#28051;?#26473;?#32457;?#37827;?#37706;?#23536;?#28598;?#37827;?#37706;欎細寮曡捣?#20549;涓?#38010;傜偣?#31904;闆嗙兢涓?#38315;鍑猴紝骞?#28051;?#37828;墍鏈?#37928;?#26473;?#32457;嶉璇?#38318;?#37832;GC鐨勯棶棰橈紝?#22428;鐗?#37734;版煡?#31777;GC鐨?#37827;?#36423;楋紝鏈?#27996;?#26473;?#32457;?#37712;欒秴杩?#28051;?#37714;嗛挓鐨勬儏鍐?#28051;嬶紝GC涓鐩村浜?#38344;炲父鍋?#25652;?#37928;勭姸鎬侊紝鑰?#28051;旀煡?#31777;GemFire鐨?#37827;?#36423;楀?#23678;?#25116;滑鑷?#23480;?#37928;?#37827;?#36423;楋紝涔熸病鏈夊彂鐜?#28000;?#28003;?#23534;傚父銆傜敱浜?#37812;戜滑姣?#28051;?#37825;?#37817;?#28103;濈暀涓?#37714;?#28000;芥嫹璐濓紝涔熷氨鏄?#29831;?#23011;忔鏁?#37817;?#37712;?#38318;借鍐?#28051;?#28051;?#38010;傜偣?#32029;涓?#37714;嗛挓瀵笴PU?#28533;璇村彲浠?#37707;?#28598;?#28598;?#37928;?#27996;嬫儏?#32029;鍥?#38000;屽彧鏈塈O?#22688;鑳藉湪鏌?#27996;?#37827;?#37706;欎骇鐢?#26473;?#32457;嶉棶棰橈紝?#28266;?#26870;棰樺彂鐢?#37928;?#37827;?#37706;?#28052;熸病鏈?#28000;?#28003;overflow鏁?#37817;紝鑰?#28051;?#37832;?#37734;版搷浣滐紝鍗?#28003;?#28725;笽O?#28533;璇?鍒嗛挓涔?#37828;?#28051;涓?#38344;炲父闀跨殑鏃?#38338;翠簡?#32029;鍥?#38000;屾?#25116;滑鍙?#38003;芥鐤?#26473;?#37828;?#37712;?#37721;?#28051;涓?#38010;傜偣寮曡捣鐨勶紝瀵?#37721;?#28051;涓?#38010;傜偣?#32029;?#30048;鏄湪鍚?#28051;涓狣ata Center涓紝鑰?#28051;斿熀鏈?#37828;湪鍚?#28051;涓狢hasis鍐?#38318;紝鍥?#38000;屽畠浠?#28052;?#38338;村皬浜?M鐨?#37825;?#37817;噺閫氫俊涔?#28051;?#28598;彲鑳借?#21342;幓2鍒嗛挓鐨?#37827;?#38338;达紝?#22669;浠?#37715;?#28051;?#37928;勬?#25116;滑?#27688;鍙?#38003;芥鐤戠綉缁?#37928;勯棶棰樹簡?#32029;姣斿鏁?#37817;?#28051;?#37718;?#37510;佺綉缁?#37814;栧姩銆佺綉缁?#23092;侀噺澶?#28598;?#28051;?#25443;浼?#26440;撳彉鎱?#32475;夛紝浣?#37828;?#37812;戜滑?#30149;鏈?#37813;惧埌浠?#28003;?#37929;?#37711;?#37928;勯棶棰?#37510;傛墍浠?#37812;戜滑寰堥暱涓娈?#37827;?#38338;寸?#29363;墜鏃?#32475;栵紝鍙?#38003;芥狦emFire闂?#23143;愶紝鎴戜滑涓嶇煡閬?#26473;?#28051;?#37714;嗛挓鏄?#28051;?#37828;疓emFire鑷?#23480;?#37712;?#38318;湪鍋?#28051;浜?#28051;?#28051;?#27996;虹煡鐨?#27996;嬫儏?#32029;鍥?#38000;?#28598;?#36423;欎簡鑰屾?#24532;潵寰楀?#23106;?#21229;?#21976;?#25116;滑鐨勫啓璇?#23033;?#37510;傝?#30028;劧?#22428;涓鐩?#29785;?#23536;?#28051;嶇?#28266;澶勭悊浠涔?#37904;?#28003;滐紝涓?#37714;嗛挓閮芥病鏈夊搷搴?#37837;?#37832;?#37827;犳硶瑙噴鐨勯氾紝?#27967;浣?#37712;礕emFire鑺傜偣涔?#38338;村?#33333;病鏈夋姤鍛?#37832;?#28000;?#28003;?#23534;傚父?#32029;鎴?#38000;呭儚浠墠?#24386;鐜?#37928;?#28051;涓?#38010;傜偣鍚慙ocator涓?#37814;?#37721;?#28051;涓?#38010;傜偣?#30149;鏈夊搷搴?#37928;勯棶棰橈紝Locator鑷?#23480;?#28052;熻兘寰?#23005;父鐨勫悜?#20549;涓?#38010;傜偣?#24386;閫?#37826;?#37928;勬垚鍛樹俊鎭?#38171;View锛夛紝鍥?#38000;岀?#23341;捣?#28533;鍚?#37828;?#26473;?#28051;?#38010;傜偣铏界劧鑺变簡涓?#37714;嗛挓澶氭潵鍐?#28051;涓?#37825;?#37817;紝浣?#37828;畠杩?#37828;?#37832;夊搷搴?#37928;勶紝鏈夌偣“鍋?#23005;?#8221;鐨勮?#24816;銆?br /> <br /> <h2>?#26870;棰樺彂鐜?/h2> 杩?#28051;棶棰?#26473;?#28052;?#37713;?#39582;?#28000;潵鏃?#28051;?#37827;?#37928;勫氨?#32048;?#24386;鐢燂紝鑰?#28051;?#37733;?#28051;?#28000;墠鑺?#37928;?#37827;?#38338;村澶氫簡?#32029;鑰?#28051;?#28052;熸病鏈?#37813;惧埌浠?#28003;?#37713;洪鐨勫?#29256;?#30328;紝鐜板湪绱?#37804;?#28051;嶅?#26127;姳澶?#28598;?#37827;?#38338;村湪涓?#38344;簡?#32029;?#27967;浣?#37712;?#26473;?#28051;畠寰堥暱鏃?#38338;存墠?#24386;鐢?#28051;娆紝骞?#28051;?#28000;婂勾浠潵?#27688;涓鐩存病?#24386;鐢?#26473;囷紝鐩村埌?#22688;鍑犲鍑?#37916;?#28051;娆紝?#22428;鏈夌偣涓嶄俊閭?#37928;勯?#23942;?#26495;幓鐪?#26473;?#28051;棶棰橈紝渚濈劧?#30149;鏈?#37813;惧埌浠?#28003;曞彲鐤?#37928;勫?#29256;?#30328;紝GC鏃?#36423;?#37510;?#25652;旂敤绋嬪簭鏃?#36423;?#37510;丟emFire鑷?#23480;?#37928;?#37827;?#36423;?#37510;佺綉缁?#37510;丆PU浣跨敤?#20751;鍐?#32475;夋墍鏈?#37928;勯?#33445;槸姝父鐨勶紝闄簡?#26870;棰樺彂鐢?#37928;勯偅涓?#37827;?#37714;伙紝搴旂敤绋嬪?#24532;病鏈?#28000;?#28003;?#37827;?#36423;楋紝鍙?#28598;栧湪?#26870;棰樺彂鐢?#28052;嬪墠鍑?#37916;?#26473;嘗og4J鏃?#36423;楁枃浠?#37928;凴olling锛堟?#25116;滑浣跨敤RollingFileAppender?#32029;骞?#28051;斿彧淇濈暀20涓?#37827;?#36423;楁枃浠?#38171;夛紝浣?#37828;疞og4J鏃?#36423;楁枃浠Roll鐨?#37827;?#36423;楀嚭鐜颁簡鏂?#32513;擄紝?#28266;?#32017;濮?#29781;Roll?#22476;鐪?#23005;?#28729;屾垚Roll涓?#38338;?#26473;?#37832;?#37713;犺?#23375;emFire鑷?#38892;?#37928;?#37827;?#36423;楋紝姝?#37827;舵垜骞舵病鏈夎寰?#26473;?#28051;?#37828;?#37832;?#23536;?#28598;棶棰?#37928;勶紝鍥?#28051;烘垜濮嬬?#22563;寰桳og4J闄簡?#30048;鑷?#23480;辨?#24876;埌骞?#37735;囧鎬兘鏈?0%鐨勫?#21342;搷浠?#28598;栵紝?#30048;?#27688;鏄?#28051;涓畝鍗?#37928;勬妸鏃?#36423;?#37712;欏埌?#26499;浠?#37928;?#26473;囩?#23350;紝涓嶄細褰卞搷鐨?#37825;?#28051;?#25652;旂敤绋嬪簭鏈?#38892;紝鍥?#28051;哄畠澶畝鍗曚簡?#32029;鐩村埌浠婂杩?#28051;棶棰?#37712;嶆鍑?#37916;帮紝渚濈劧?#30149;鏈?#28000;?#28003;?#37711;?#28000;?#37826;?#38344;?#37928;勬?#24816;幏?#32029;?#22669;鏈?#37928;勫?#29256;柟閮芥?#21095;?#28888;?#29238;?#23032;鎬侊紝鐢氳嚦鎴戜滑涔嬪墠?#24386;鐜?#37928;勭綉?#23857;?#26870;棰?#28000;婂涔熸病鏈夊彂鐢燂紝?#21159;鑰屽?#23678;牱鏄?#37713;洪棶棰?#37928;?#28051;?#37714;嗛?#29112;病鏈?#37713;?#37916;?#25652;旂敤绋嬪簭鏃?#36423;楋紝鏃?#36423;楁枃浠Roll鐨?#37827;?#36423;楀拰涓?#23046;?#32491;讳技?#32029;?#32017;濮婻oll?#22476;缁撴鍑?#37916;癎emFire鏃?#36423;?#37928;?#27996;?#37721;?#37510;? <div><fieldset><legend>鏈杩?#28051;娆彂鐢?#37928;?#37827;?#36423;?/legend> <div>[info 2015/08/12 01:56:07.736 BST …] ClientHealthMonitor: Registering client with member id …</div> <div>log4j: rolling over count=20971801</div> <div>log4j: maxBackupIndex=20</div> <div>[info 2015/08/12 01:56:12.265 BST …] ClientHealthMonitor: Unregistering client with member id …</div> <div>……</div> <div>[info 2015/08/12 01:56:23.773 BST …] ClientHealthMonitor: Registering client with member id …</div> <div>log4j: Renaming file logs/….log.19 to logs/….log.20</div> </fieldset></div> <div><fieldset><legend>涓鍛墠?#24386;鐢?#37928;?#37827;?#36423;?/legend> <div>[info 2015/08/04 01:43:45.761 BST …] ClientHealthMonitor: Registering client with member id …</div> <div>log4j: rolling over count=20971665</div> <div>log4j: maxBackupIndex=20</div> <div>……</div> <div>[info 2015/08/04 01:45:25.506 BST …] ClientHealthMonitor: Registering client with member id …</div> <div>log4j: Renaming file logs/….log.19 to logs/….log.20</div> </fieldset></div> <div>鐪嬩技杩?#28051;?#37828;?#28051;涓?#29785;勫緥锛?#28610;楃敤鍚?#27996;?#37928;?#28051;鍙?#29831;?#38171;?#28051;娆彂鐢?#37827;?#37707;剁劧?#32029;涓?#23046;彂鐢熷氨鏄?#32457;戝?#31777;锛?#37510;傜劧鑰屾鏃舵垜鍏跺疄渚濈劧涓?#28598;?#37929;镐俊Log4J鏄?#8220;鍑?#37813;?#8221;?#32029;鍥?#28051;烘垜涓鐩?#29785;?#23536;桳og4J鏄?#28051;涓畝鍗?#37928;?#37827;?#36423;?#26440;撳?#28888;?#21976;?#35769;紝?#30048;瑕?#37828;?#37713;洪棶棰?#28052;?#37721;?#37828;畠鑷?#23480;?#37928;勯棶棰橈紝鏄?#28766;閮?#37928;勶紝鑰?#26473;?#28051;棶棰?#37928;勫嚭鐜版槑鏄?#37828;?#37711;?#28766;鐨勶紝鐩村埌?#22428;H佺劧鑴戝瓙涓闂?#38000;?#26473;囷紝<strong>鏃?#36423;?#37813;撳嵃鐨勬搷浣?#37828;痵ynchronized?#32029;涔熷氨鏄?#29831;村湪鏃?#36423;楁枃浠Roll鐨?#37827;?#37706;欙紝?#22669;鏈?#37711;跺畠?#28214;瑕?#37813;?#37827;?#36423;?#37928;?#32510;跨閮借?#20346;瓑寰?#37929;村埌Roll瀹屾垚?#32029;濡傛灉杩?#28051;猂oll杩囩?#23341;秴杩囦簡2鍒嗛?#29122;紝?#20549;涔堝氨?#32048;?#24386;鐢?#37812;戜滑鐪嬪埌鐨凷tripe Lock?#31220;鏃讹紝涔熷氨鏄彂鐢熶簡绋嬪簭“鍋?#23005;?#8221;鐨勭姸鎬?#37510;?/strong>閲嶆?#29256;煡鐪Log4J鎵撳嵃鏃?#36423;?#37928;勬?#35268;硶?#30367;?#25956;鏍堬紝?#30048;?#32048;?#28266;涓?#28051;?#37734;版?#22317;敤synchronized?#32029;鍗?#37722;?#28051;涓狢ategory锛圠ogger锛夌被瀹?#28186;?#38171;?br /> <div style="font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%; word-break: break-all; background-color: #eeeeee;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->    <span style="color: #0000FF; ">public</span> <span style="color: #0000FF; ">void</span> callAppenders(LoggingEvent event) {<br />         <span style="color: #0000FF; ">int</span> writes = 0;<br />         <span style="color: #0000FF; ">for</span>(Category c = <span style="color: #0000FF; ">this</span>; c != <span style="color: #0000FF; ">null</span>; c=c.parent) {<br />             <span style="color: #008000; ">//</span><span style="color: #008000;"> Protected against simultaneous call to addAppender, removeAppender,<img src="http://www.8634070.com/Images/dot.gif" alt="" /></span><span style="color: #008000; "><br /> </span>            <span style="color: #0000FF; ">synchronized</span>(c) {<br />                 <span style="color: #0000FF; ">if</span>(c.aai != <span style="color: #0000FF; ">null</span>) {<br />                     writes += c.aai.appendLoopOnAppenders(event);<br />                 }<br />                 <span style="color: #0000FF; ">if</span>(!c.additive) {<br />                     <span style="color: #0000FF; ">break</span>;<br />                 }<br />             }<br />         }<br /> 銆?#37510;?#37510;?br />     }</div> </div>浠?#37721;婂悓涓涓狝ppender?#28266;doApppend鏃?#38171;?br /><div style="font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%; word-break: break-all; background-color: #eeeeee;"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->    <span style="color: #0000FF; ">public</span> <span style="color: #0000FF; ">synchronized</span> <span style="color: #0000FF; ">void</span> doAppend(LoggingEvent event) {<br />      銆?#37510;?#37510;?br />      <span style="color: #0000FF; ">this</span>.append(event);<br />    }</div><div>鑰孯oll鐨?#26473;囩?#23338;氨鏄湪append鏂规硶涓紝杩?#28051;姝?#37714;嗘瀽?#32029;?#28266;涓?#38344;?#28051;?#37721;?#29831;?#28052;?#38338;达紝浠?#28000;?#37714;?#37714;?#38010;辫?#36891;簡?#31220;杩?00s?#25328;?#31220;杩?1s鐨?#37827;?#38338;?#38171;?/div><div>log4j: maxBackupIndex=20<br />銆?#37510;?#37510;?/div><div>log4j: Renaming file logs/….log.19 to logs/….log.20</div><div>鑰?#26473;?#28051;?#37721;?#28052;?#38338;村彧鍖?#37722;簡涓?#28051;狥ile.exists()?#32029;涓涓狥ile.delete()?#32029;涓涓狥ile.rename()鎿?#28003;?#38171;?/div><div style="font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%; word-break: break-all; background-color: #eeeeee;"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->    <span style="color: #0000FF; ">public</span> <span style="color: #0000FF; ">void</span> rollOver() {<br />      銆?#37510;?#37510;?br />      <span style="color: #0000FF; ">if</span>(maxBackupIndex > 0) {<br />        <span style="color: #008000; ">//</span><span style="color: #008000;"> Delete the oldest file, to keep Windows happy.</span><span style="color: #008000; "><br /></span>        file = <span style="color: #0000FF; ">new</span> File(fileName + '.' + maxBackupIndex);<br />        <span style="color: #0000FF; ">if</span> (file.exists())<br />            renameSucceeded = file.delete();<br />        <span style="color: #0000FF; ">for</span> (<span style="color: #0000FF; ">int</span> i = maxBackupIndex - 1; i >= 1 && renameSucceeded; i--) {<br />            file = <span style="color: #0000FF; ">new</span> File(fileName + "." + i);<br />            <span style="color: #0000FF; ">if</span> (file.exists()) {<br />                target = <span style="color: #0000FF; ">new</span> File(fileName + '.' + (i + 1));<br />                LogLog.debug("Renaming file " + file + " to " + target);<br />                renameSucceeded = file.renameTo(target);<br />            }<br />        }<br />      銆?#37510;?#37510;?br />      }<br />    }</div><div><h2>NFS?#30045;鍗?#37804;?#38003;芥祴璇?#37724;屽?#21976;瀽</h2>鍥?#38000;屾垜瀵筃FS鐨?#37804;兘浣滀簡涓浜涚畝鍗曟祴璇?#38171;?/div><div>鍙?#37832;?#28051;涓?#32510;跨鏃讹紝?#28266;NFS涓媟ename鎬兘锛?/div><div>1 file:                    3ms</div><div>10 files:                48ms</div><div>20 files:                114ms</div><div>鐩?#23011;?#26440;冿紝?#28266;鏈湴纾?#37929;榬ename鐨?#37804;兘锛?/div><div>1 file:                    1ms</div><div>3 files:                  1ms</div><div>10 files:                3ms</div><div>瀵筃FS?#25328;鏈湴纾?#37929;?#37712;?#37928;?#37804;兘锛堟鎷?#37827;?#36423;楋紝姣忚閮戒細flush锛?#38171;?/div><table border="0" cellspacing="0" cellpadding="0" style="border-collapse:collapse;"> <tbody><tr style="height:13.4pt"> <td width="139" valign="top" style="width: 1.45in; border: 1pt solid windowtext; padding: 0in 5.4pt; height: 13.4pt;"> <p> </p> </td> <td width="78" valign="top" style="width: 58.5pt; border-style: solid solid solid none; border-top-color: windowtext; border-right-color: windowtext; border-bottom-color: windowtext; border-top-width: 1pt; border-right-width: 1pt; border-bottom-width: 1pt; padding: 0in 5.4pt; height: 13.4pt;"> <p><span style="color:#1F497D">NFS</span></p> </td> <td width="78" valign="top" style="width: 58.5pt; border-style: solid solid solid none; border-top-color: windowtext; border-right-color: windowtext; border-bottom-color: windowtext; border-top-width: 1pt; border-right-width: 1pt; border-bottom-width: 1pt; padding: 0in 5.4pt; height: 13.4pt;"> <p><span style="color:#1F497D">LOCAL</span></p> </td> </tr> <tr> <td width="139" valign="top" style="width: 1.45in; border-style: none solid solid; border-right-color: windowtext; border-bottom-color: windowtext; border-left-color: windowtext; border-right-width: 1pt; border-bottom-width: 1pt; border-left-width: 1pt; padding: 0in 5.4pt;"> <p><span style="color:#1F497D">1 writer, 11M</span></p> </td> <td width="78" valign="top" style="width:58.5pt;border-top:none;border-left:none; border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt; padding:0in 5.4pt 0in 5.4pt"> <p><span style="color:#1F497D">443ms</span></p> </td> <td width="78" valign="top" style="width:58.5pt;border-top:none;border-left:none; border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt; padding:0in 5.4pt 0in 5.4pt"> <p><span style="color:#1F497D">238ms</span></p> </td> </tr> <tr> <td width="139" valign="top" style="width: 1.45in; border-style: none solid solid; border-right-color: windowtext; border-bottom-color: windowtext; border-left-color: windowtext; border-right-width: 1pt; border-bottom-width: 1pt; border-left-width: 1pt; padding: 0in 5.4pt;"> <p><span style="color:#1F497D">1 writer, 101M</span></p> </td> <td width="78" valign="top" style="width:58.5pt;border-top:none;border-left:none; border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt; padding:0in 5.4pt 0in 5.4pt"> <p><span style="color:#1F497D">2793ms</span></p> </td> <td width="78" valign="top" style="width:58.5pt;border-top:none;border-left:none; border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt; padding:0in 5.4pt 0in 5.4pt"> <p><span style="color:#1F497D">992ms</span></p> </td> </tr> <tr> <td width="139" valign="top" style="width: 1.45in; border-style: none solid solid; border-right-color: windowtext; border-bottom-color: windowtext; border-left-color: windowtext; border-right-width: 1pt; border-bottom-width: 1pt; border-left-width: 1pt; padding: 0in 5.4pt;"> <p><span style="color:#1F497D">10 writers, 11M</span></p> </td> <td width="78" valign="top" style="width:58.5pt;border-top:none;border-left:none; border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt; padding:0in 5.4pt 0in 5.4pt"> <p><span style="color:#1F497D">~4400ms</span></p> </td> <td width="78" valign="top" style="width:58.5pt;border-top:none;border-left:none; border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt; padding:0in 5.4pt 0in 5.4pt"> <p><span style="color:#1F497D">~950ms</span></p> </td> </tr> <tr> <td width="139" valign="top" style="width: 1.45in; border-style: none solid solid; border-right-color: windowtext; border-bottom-color: windowtext; border-left-color: windowtext; border-right-width: 1pt; border-bottom-width: 1pt; border-left-width: 1pt; padding: 0in 5.4pt;"> <p><span style="color:#1F497D">10 writers, 101M</span></p> </td> <td width="78" valign="top" style="width:58.5pt;border-top:none;border-left:none; border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt; padding:0in 5.4pt 0in 5.4pt"> <p><span style="color:#1F497D">~30157ms</span></p> </td> <td width="78" valign="top" style="width:58.5pt;border-top:none;border-left:none; border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt; padding:0in 5.4pt 0in 5.4pt"> <p><span style="color:#1F497D">~5500ms</span></p> </td> </tr></tbody></table><div><br />涓浜涘叾浠?#37928;?#32513;熻锛?/div><div><strong>100鍚?#37827;?#37712;?</strong></div><div>Create 20 files spend: 301ms</div><div>Renaming 20 files spends: 333ms</div><div>Delete 20 files spends: 329ms</div><div></div><div><strong>1000鍚?#37827;?#37712;?</strong></div><div>Create 20 files spend: 40145ms</div><div>Renaming 20 files spends: 39273ms<br /></div><div><strong>鑰屽湪1000涓?#37722;?#37827;?#37712;?#37928;?#26473;囩涓紝閲嶅?#34249;悕锛?/strong></div><div>Rename file: LogTest1.50 take: <strong>36434ms</strong></div><div>Rename file: LogTest1.51 take: <strong>39ms</strong></div><div>Rename file: LogTest1.52 take: <strong>34ms</strong><br /></div><div>涔熷氨鏄?#29831;村湪杩?#28051;?#22959;?#37815;?#26473;囩涓紝涓涓枃浠?#37928;剅ename?#31220;杩?6s?#32029;鑰屽?#25118;?#25116;滑鏈?#37719;?#37713;?#37721;?#37832;哄櫒鍚?#37827;?#28003;跨敤鐩?#37722;?#37928;凬FS?#32029;骞?#28051;?#23011;?#37721;?#37832;哄櫒涓?#38318;借?#25116;簩涓?#37719;?#28051;?#32459;嬪?#24549;紝濡傛灉?#20549;娈?#37827;?#38338;村悓鏃?#37832;?#28051;?#28051;?#28051;?#37928;?#37827;?#36423;?#37712;欙紝?#24434;浠?#26864;勮杈惧埌100s?#20751;鍐?#37828;彲鑳藉彂鐢?#37928;?#37510;?/div><div>鍏?#27996;嶯FS鎬兘鐨勯棶棰橈紝?#28266;銆?#37835;勫缓楂?#37804;兘WEB绔欑偣銆?#37928;?#28052;?#38171;?30椤?#38171;?#28051;?#28052;?#37832;夋秹鍙?#37510;傜畝鍗?#37928;?#28000;嬬?#23951;紝NFS鐢盨un?#28266;1984骞村紑?#24386;?#32029;鏄?#28051;绘祦寮傛?#21227;钩鍙板疄鐜版枃浠?#37711;?#27996;?#37928;勯閫夋?#35268;銆傚畠骞舵病鏈夎嚜宸?#37928;勪紶杈撳崗璁紝鑰屾槸浣跨敤RPC锛圧emote Procedure Call锛?#37719;?#29825;?#38171;?#25652;旂敤?#30512;锛夛紝RPC鍗?#29825;?#27035;?#29825;?#25652;曞?#20634;熀浜UDP浼?#26440;擄紝浣?#37828;?#38007;?#23480;卞疄鐜板湪涓?#37718;?#37827;?#37928;勯噸浼犳満鍒讹紝鑰?#28051;擭FS鏈嶅姟鍣?#38322;囩敤澶?#26473;?#32459;嬫鍨嬶紝榛?#29825;?#26473;?#32459;?#28051;??#32029;浣?#37828;?#28051;鑸?#38318;?#28028;氳皟浼樺?#28850;?#29363;?#23941;姟杩?#32459;?#37825;帮紝?#21159;鑰?#8220;涓嶇鎬?#28052;?#28725;筃FS杩涜?#23678;兘浼樺?#26677;紝NFS娉畾涓?#38315;?#37722;?#28003;?#28051;篒/O瀵嗛泦鍨嬫枃浠?#37711;?#27996;?#37826;规?#22572;紝浣嗗彲浠?#28003;?#28051;?#28051;鑸敤閫旓紝姣斿?#20635;?#24874;?#28058;?#27409;偣鍐?#38318;?#37928;勮?#21228;簮鍏?#27996;紝?#30048;鐨勪?#27194;?#22446;湪浜庡鏄撴寤猴紝鑰?#28051;斿彲浠?#37713;?#28751;?#28051;嶅繀瑕?#37928;?#37825;?#37817;?#37712;?#28003;?#37510;?#8221;</div><div>?#24434;浠?#28003;跨敤鍛?#28000;?#38171;?#8220;nfsstat -c”鑾?#37721;?#28725;筃FS鏈嶅姟鍣?#37928;勬搷浣?#37928;勭畝鍗曠?#29115;?#32029;?#21503;浣撳彲浠?#37721;?#38000;?#37510;?#37835;勫缓楂?#37804;兘WEB绔欑偣銆?#37928;?#37929;?#37711;崇珷鑺傦紝閲岄潰杩?#37832;夋洿璇?#32513;?#37928;勫?#31555;FS鏈嶅姟鍣?#37804;兘鐨勬祴璇?#37510;?/div><div><br /><h2>鎬?#32513;?/h2><strong>?#31904;杩?#28051;?#27996;?#28000;舵垜鎬?#32513;撲簡涓?#28000;?#27996;嬫儏锛?/strong></div><div>1. 鏃?#36423;?#37928;勫?#21342;?#23941;彲鑳芥槸鍏?#28766;鎬?#37928;勶紝鍥?#38000;?#29781;?#38344;炲父灏?#36423;冿紝涓涓?#38000;?#37827;?#37928;勬搷浣滃彲鑳藉?#26337;?#39118;?#23338;簭鐨?#8220;鍋?#23005;?#8221;?#32029;鍥?#38000;?#29781;?#38344;炲父灏?#36423;?#37510;?/div><div>2. 铏界劧?#22968;鏃?#36423;?#37813;撳嵃?#28266;NFS涓婏紝瀵?#28598;噺鐨?#37827;?#36423;楁枃浠舵煡鎵句細鏂逛究寰?#28598;氾紝浣?#37828;?#26473;?#37828;?#28051;涓?#23536;?#38000;?#37804;兘鐨勮璁紝鐗?#37714;?#37828;?#35120;撳?#22138;鐨?#32459;嬪簭鍏?#27996;?#26473;?#28051;狽FS鐨?#37827;?#37706;欙紝鍥?#38000;?#29781;佸?#20171;噺閬?#37711;?#37510;?/div><img src ="http://www.8634070.com/DLevin/aggbug/426751.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.8634070.com/DLevin/" target="_blank">DLevin</a> 2015-08-13 16:28 <a href="http://www.8634070.com/DLevin/archive/2015/08/13/426751.html#Feedback" target="_blank" style="text-decoration:none;">?#24386;琛?#29831;勮</a></div>]]></description></item><item><title>瀹?#37916;拌嚜宸?#37928;凩ock瀵硅薄http://www.8634070.com/DLevin/archive/2015/08/11/426723.htmlDLevinDLevinMon, 10 Aug 2015 22:08:00 GMThttp://www.8634070.com/DLevin/archive/2015/08/11/426723.htmlhttp://www.8634070.com/DLevin/comments/426723.htmlhttp://www.8634070.com/DLevin/archive/2015/08/11/426723.html#Feedback0http://www.8634070.com/DLevin/comments/commentRss/426723.htmlhttp://www.8634070.com/DLevin/services/trackbacks/426723.html涓鐩存兂濂?#28610;藉涔concurrent鍖?#28051;?#37928;勫悇涓被鐨勫疄鐜帮紝?#21159;鑰岀粡?#29238;鐪嬩簡涓?#20579;?#27688;鍥?#28051;?#37711;?#28000;?#27996;嬫儏骞?#37813;?#38000;屾斁涓嬩簡銆傚彂鐜?#26473;?#37837;?#28598;?#28051;?#37714;?#27996;?#38007;?#23480;?#37928;勬垚闀夸簡?#32029;鍥?#38000;?#37832;杩?#37813;?#32480;楁綔蹇?#28051;浠?#28051;浠?#37928;勫?#23678;垚鑷?#23480;辨兂瀛?#28052;?#37928;?#28051;滆銆?br />
瀵筩oncurrent鍖?#37928;勫涔犳墦绠楀?#22556;粠Lock鐨勫疄鐜板紑濮嬶紝鍥?#38000;?#38007;劧鑰岀劧鐨勫氨绔捣?#31777;AbstractQueuedSynchronizer?#32029;?#21159;鑰?#29781;?#29831;绘噦杩?#28051;被鐨勬簮鐮?#39582;?#28051;?#37828;偅涔堝鏄擄紝鍥?#38000;屾垜?#27688;?#32017;濮嬮棶鑷?#23480;?#28051;涓棶棰?#38171;?#28641;傛灉鑷?#23480;?#29781;佸幓瀹?#37916;?#26473;?#28051;?#28051;涓狶ock瀵硅薄?#32029;搴?#29831;?#28641;?#28003;?#28729;?#37916;板憿锛?br />
瑕佸疄鐜癓ock瀵硅薄?#32029;棣?#37711;堢悊瑙?#28000;涔?#37828;攣锛熸垜鑷?#23480;变粠缂?#32459;嬭搴畝鍗?#37928;勭悊瑙紝?#22669;璋撻攣瀵硅薄锛?#27996;?#37826;攣锛夊氨鏄畠?#20824;淇?#29831;?#28051;娆?#37721;?#37832;?#28051;涓?#32510;跨?#23341;兘杩涘叆?#30048;淇濇姢鐨?#28051;寸?#23677;?#29492;紝濡傛灉鏈?#28051;涓?#32510;跨?#23338;凡缁忔?#22446;埌?#25891;瀵硅薄?#32029;?#20549;涔?#37711;?#28000;?#28725;硅薄蹇?#26916;昏鏉?#32475;?#23536;咃紝鑰屽湪璇?#32510;跨閫鍑?#26473;?#28051;?#28051;寸?#23677;尯鏃堕渶瑕佸敜?#21835;绛?#23536;?#37714;?#29723;?#28051;?#37928;勫叾浠?#32510;跨銆傛?#26449;鏈?#28051;浜涳紝銆?#29825;?#32480;?#37832;烘搷浣?#32495;?#32513;?#37510;?/a>涓?#28725;?#37722;屾鏈?#37714;?#37713;?#37714;?#37928;勫綊绾?#38171;圥50锛?#38171;?

  1. 绌?#38338;?#29825;?#26473;?#37510;?#35120;?#37827;?#26473;?#32459;嬪浜?#28051;寸?#23677;尯鏃讹紝琛?#37828;?#28051;寸晫璧勬簮澶?#27996;庣闂茬姸鎬侊紝搴斿厑璁?#28051;涓?#29831;?#23033;?#26473;涘叆涓寸?#23677;尯鐨?#26473;?#32459;嬬?#23338;嵆杩涘叆鑷?#23480;?#37928;?#28051;寸?#23677;?#29492;紝浠?#37832;?#37825;?#37928;勫埄?#25956;涓寸晫璧勬簮銆?/li>
  2. ?#32342;鍒?#32475;?#23536;?#37510;?#35120;撳凡鏈?#26473;?#32459;?#26473;涘叆涓寸?#23677;尯鏃讹紝琛?#37828;?#28051;寸晫璧勬簮姝湪琚?#29825;块棶?#32029;鍥?#38000;屽叾浠?#29831;?#37733;?#26473;涘叆涓寸?#23677;尯鐨?#26473;?#32459;嬪繀椤荤瓑寰咃紝浠?#28103;?#29831;?#28725;?#28051;寸?#23677;?#40511;?#21228;簮鐨?#27996;?#37826;?#29825;块棶銆?/li>
  3. 鏈夐檺绛?#23536;?#37510;?#28725;?#29781;佹眰璁块棶涓寸晫璧勬簮鐨?#26473;?#32459;嬶紝搴?#28103;?#29831;佸湪鏈夐檺鏃?#38338;村唴?#20824;杩涘叆鑷?#23480;?#37928;?#28051;寸?#23677;?#29492;紝浠?#37711;?#38340;?#37711;?#8220;姝荤瓑”?#23032;鎬?#37510;?/li>
  4. 璁?#37833;?#32475;?#23536;?#37510;?#35120;?#26473;?#32459;?#28051;嶈兘杩涘叆鑷?#23480;?#37928;?#28051;寸?#23677;尯鏃讹紝搴?#29831;噴鏀?#28598;勭悊鏈猴紝浠?#37711;?#26473;?#32459;?#38340;?#37711;?#8220;?#32342;绛?#8221;?#23032;鎬?#37510;?/li>

璇翠簡?#20549;涔?#28598;氾紝鍏跺疄瀵?#27996;?#37826;攣寰堢畝鍗曪紝鍙渶瑕?#28051;涓?#37837;囪浣嶏紝濡傛灉璇?#37837;囪浣?#28051;??#32029;琛?#32448;烘病鏈夎鍗犵敤?#32029;鍥?#38000;?#37929;?#37818;?#38014;?#23536;楅攣?#32029;?#21159;鍚庢妸璇?#37837;囪浣?#32515;?#28051;??#32029;姝?#37827;?#37711;?#28000;?#32510;跨?#23338;彂鐜?#29831;?#37837;囪浣?#23480;?#32513;?#37828;??#32029;鍥?#38000;岄渶瑕佺瓑寰?#37510;?#26473;?#38322;屽杩?#28051;?#37837;囪浣?#37928;勬瘮杈?#39582;惰?#24807;?#36423;?#26916;?#37828;?#37720;熷瓙鎿?#28003;滐紝鑰屽湪JDK5浠?#37722;庢?#24874;緵鐨刟tomic鍖呴噷鐨勫伐?#21503;?#34987;?#24434;浠?#23536;堟柟渚跨殑鎻愪緵杩?#28051;?#37720;熷瓙鎿?#28003;?#37510;傜劧鑰?#28051;?#38344;?#37928;勫洓涓?#37713;?#37714;?#25652;?#29831;紡?#31777;涓?#20579;?#32029;鍗抽噴鏀鹃攣鐨?#32510;跨锛?#26473;?#32459;?#38171;夊拰寰楀埌?#25891;鐨?#32510;跨锛?#26473;?#32459;?#38171;?#25652;?#29831;?#37828;?#37722;?#28051;涓紝?#27688;鍍?#28051;?#22968;閽?#37718;?#28725;?#25652;?#28051;?#22968;?#25891;锛堢?#21976;兂鐨?#38171;夛紝?#22669;浠?#28051;涓?#38344;炲父?#30045;鍗?#37928;凩ock?#34987;?#24434;浠?#26473;?#28052;堝疄鐜?#38171;?br />

public class SpinLockV1 {
    
private final AtomicInteger state = new AtomicInteger(0);
    
private volatile Thread owner; // 杩欓噷owner瀛?#23048;靛彲鑳藉?#27194;湪涓?#38338;村硷紝涓嶅彲闈狅紝鍥?#38000;屽叾浠?#32510;跨涓嶅彲浠?#28186;?#29863;?#26473;?#28051;?#28699;?#23048;?#37928;勫?/span>
    
    
public void lock() {
        
while (!state.compareAndSet(01)) { }
        owner 
= Thread.currentThread();
    }
    
    
public void unlock() {
        Thread currentThread 
= Thread.currentThread();
        
if (owner != currentThread || !state.compareAndSet(10)) {
            
throw new IllegalStateException("The lock is not owned by thread: " + currentThread);
        }
        owner 
= null;
    }
}

涓涓畝鍗?#37928;勬祴璇?#37826;规硶锛?br />

    @Test
    
public void testLockCorrectly() throws InterruptedException {
        
final int COUNT = 100;
        Thread[] threads 
= new Thread[COUNT];
        SpinLockV1 lock 
= new SpinLockV1();
        AddRunner runner 
= new AddRunner(lock);
        
for (int i = 0; i < COUNT; i++) { 
            threads[i] 
= new Thread(runner, "thread-" + i);
            threads[i].start();
        }
        
        
for (int i = 0; i < COUNT; i++) {
            threads[i].join();
        }
        
        assertEquals(COUNT, runner.getState());
    }
    
    
private static class AddRunner implements Runnable {
        
private final SpinLockV1 lock;
        
private int state = 0;

        
public AddRunner(SpinLockV1 lock) {
            
this.lock = lock;
        }
        
        
public void run() {
            lock.lock();
            
try {
                quietSleep(
10);
                state
++;
                System.out.println(Thread.currentThread().getName() 
+ "" + state);
            } 
finally {
                lock.unlock();
            }
        }
        
        
public int getState() {
            
return state;
        }
    }

?#21159;鑰?#26473;?#28051;猄pinLock鍏跺疄骞?#28051;嶉渶瑕乻tate杩?#28051;?#28699;?#23048;碉紝鍥?#28051;簅wner鐨勮?#23338;?#28051;?#37722;?#28052;?#37828;?#28051;绉嶇姸鎬侊紝鍥?#38000;屽彲浠敤?#30048;浣?#28051;?#28051;绉?#27996;?#37826;姸鎬?#38171;?br />

public class SpinLockV2 {
    
private final AtomicReference<Thread> owner = new AtomicReference<Thread>(null);
    
    
public void lock() {
        
final Thread currentThread = Thread.currentThread();
        
while (!owner.compareAndSet(null, currentThread)) { }
    }
    
    
public void unlock() {
        Thread currentThread 
= Thread.currentThread();
        
if (!owner.compareAndSet(currentThread, null)) {
            
throw new IllegalStateException("The lock is not owned by thread: " + currentThread);
        }
    }
}

杩欏湪鎿?#28003;?#32495;?#32513;?#28051;?#29722;畾涔?#28051;?#37825;村舰?#20426;鍙烽噺?#32029;?#21159;鑰?#37825;村舰?#20426;鍙烽噺濡傛灉?#30149;鎷垮埌閿佷細涓鐩村浜?#8220;?#32342;绛?#8221;?#23032;鎬?#38171;堟病鏈?#38316;?#23536;?#37832;夐檺绛?#23536;呭拰璁?#37833;?#32475;?#23536;?#37928;勫噯鍒?#38171;夛紝鍥?#38000;?#26473;?#32457;嶉攣涔?#37721;Spin Lock?#32029;?#28266;鐭殏鐨?#32475;?#23536;?#28051;畠?#24434;浠?#37819;?#37719;?#37804;兘?#32029;鍥?#28051;哄彲浠?#37713;?#28751;?#32510;跨鐨勫垏鎹紝concurrent鍖?#28051;?#37928;凙tomic澶?#38318;?#37714;?#38318;?#38322;囩敤杩?#32457;?#37832;?#37714;跺疄鐜帮紝?#21159;鑰屽?#20635;灉?#28214;瑕侀暱鏃?#38338;?#37928;?#32475;?#23536;咃紝“?#32342;绛?#8221;?#32048;鍗犵敤涓嶅繀瑕?#37928;凜PU鏃?#38338;达紝?#31904;鑰屾兘?#32048;鍙?#37928;勫緢宸紝杩?#28051;?#37827;?#37706;欏氨?#28214;瑕佸?#21976;病鏈夋?#22446;埌?#25891;鐨?#32510;跨?#23339;?#24807;埌绛?#23536;?#37714;?#29723;?#28051;紝杩?#32457;嶆柟寮忓湪鎿?#28003;?#32495;?#32513;?#28051;?#28052;?#37721;?#29825;?#35120;?#37736;嬩俊鍙烽噺?#32029;?#30048;閬?#23536;簡璁?#37833;?#32475;?#23536;?#37713;?#37714;?#38171;?#35120;撳墠?#30149;鏈夊疄鐜?#37832;夐檺绛?#23536;?#37713;?#37714;?#38171;?#37510;傚湪JDK6浠?#37722;庢?#24874;?#28055;簡LockSupport.park()/LockSupport.unpark()鎿?#28003;滐紝?#24434;浠?#28751;?#35120;撳墠绾跨?#23339;?#24807;叆涓涓?#32475;?#23536;?#37714;?#29723;?#37812;?#28751;?#28051;涓?#32510;跨?#23337;粠杩?#28051;?#32475;?#23536;?#37714;?#29723;?#28051;敜?#21835;銆傜劧鑰?#26473;?#28051;猵ark/unpark鐨?#32475;?#23536;?#37714;?#29723;?#37828;?#28051;涓?#37711;?#28766;鐨?#32475;?#23536;?#37714;?#29723;紝?#28266;unpartk鐨?#37827;?#37706;?#26473;?#37828;渶瑕佹?#24874;?#28066;渶瑕佸敜?#21835;鐨Thread瀵硅薄?#32029;鍥?#38000;屾?#25116;滑?#28214;瑕?#32513;存姢鑷?#23480;?#37928;?#32475;?#23536;?#37714;?#29723;紝浣?#37828;?#28641;傛灉鎴戜滑?#24434;浠敤JDK鎻愪緵鐨勫伐?#21503;?#34987;ConcurrentLinkedQueue?#32029;?#27688;闈炲父瀹?#37828;撳疄鐜帮紝濡侺ockSupport?#26499;妗?#28051;?#32513;?#37713;烘潵鐨?a href="http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/LockSupport.html">?#21804;鐮?#27996;嬩緥锛?br />

class FIFOMutex {
   
private final AtomicBoolean locked = new AtomicBoolean(false);
   
private final Queue<Thread> waiters = new ConcurrentLinkedQueue<Thread>();

   
public void lock() {
     
boolean wasInterrupted = false;
     Thread current 
= Thread.currentThread();
     waiters.add(current);

     
// Block while not first in queue or cannot acquire lock
     while (waiters.peek() != current || !locked.compareAndSet(falsetrue)) {
        LockSupport.park(
this);
        
if (Thread.interrupted()) // ignore interrupts while waiting
          wasInterrupted = true;
     }

     waiters.remove();
     
if (wasInterrupted)          // reassert interrupt status on exit
        current.interrupt();
   }

   
public void unlock() {
     locked.set(
false);
     LockSupport.unpark(waiters.peek());
   }
 }

?#28266;璇唬鐮?#27996;嬩緥涓紝鏈?#28051;涓?#32510;跨?#23340;瓑寰呴槦鍒楀?#23684;攣鏍囪?#26495;瓧娈碉紝姣忔?#30367;?#25956;lock鏃?#37711;?#28751;?#35120;撳墠绾跨?#23339;?#24807;叆杩?#28051;?#32475;?#23536;呴槦鍒?#28051;紝?#21159;鍚庢嬁鍑?#38339;?#37714;楀绾跨?#23338;?#30789;薄?#32029;濡傛灉璇?#32510;跨?#23338;?#30789;薄姝?#28610;芥槸褰撳墠绾跨?#23350;紝骞?#28051;?#37812;?#37716;? 浣跨敤CAS鏂?#23534;?#29825;?#32515;locked瀛?#23048;?#38171;?#26473;?#38322;岄渶瑕?#28051;?#28051;?#37722;?#37827;舵弧瓒筹紝鍥?#28051;哄彲鑳藉嚭鐜?#28051;涓?#32510;跨?#23338;凡缁忎粠闃?#37714;?#28051;?#32457;婚櫎?#31777;浣?#26473;樻病鏈塽nlock?#32029;姝?#37827;?#37721;?#28051;涓?#32510;跨?#23341;皟?#25956;lock鏂规硶?#32029;姝?#37827;堕槦鍒楀鐨?#32510;跨?#23338;氨鏄?#32471;?#27996;?#28051;?#32510;跨?#23350;紝?#21159;鑰岀敱浜庣涓涓?#32510;跨杩樻病鏈塽nlock鎴?#38000;?#23005;湪unlock?#32029;鍥?#38000;岄渶瑕?#28003;跨敤CAS鍘熷瓙鎿?#28003;滄潵鍒?#37826;?#37828;?#37722;?#29781;park锛夛紝琛?#32448;?#29831;?#32510;跨?#23340;珵浜?#37812;?#37716;燂紝鑾?#23536;楅攣?#32029;鍚?#37714;?#28751;?#35120;撳墠绾跨?#23197;ark?#32029;杩欓噷涔嬫墍浠?#29781;佹?#24807;湪 while寰?#37916;?#28051;紝鍥?#28051;park鎿?#28003;滃彲?#20824;鏃犵悊鐢?#26473;?#37733;?spuriously)?#32029;濡傛枃妗?#28051;?#32513;?#37713;?#37928;勬弿杩?#38171;?br />

LockSupport.park()
public static void park(Object blocker)
Disables the current thread for thread scheduling purposes unless the permit is available.

If the permit is available then it is consumed and the call returns immediately; otherwise the current thread becomes disabled for thread scheduling purposes and lies dormant until one of three things happens:

  • Some other thread invokes unpark with the current thread as the target; or
  • Some other thread interrupts the current thread; or
  • The call spuriously (that is, for no reason) returns.

This method does not report which of these caused the method to return. Callers should re-check the conditions which caused the thread to park in the first place. Callers may also determine, for example, the interrupt status of the thread upon return.

Parameters:
blocker - the synchronization object responsible for this thread parking
Since:
1.6
?#22428;?#28266;瀹?#37916;拌嚜宸?#37928;勭被鏃跺氨琚?#26473;?#28051;?#8220;鏃犵悊鐢?#26473;?#37733;?#8221;?#28505;?#31777;濂?#28052;?#37510;?#28725;?#27996;?#23480;?#32513;忚幏寰楅攣鐨?#32510;跨?#23350;紝灏?#29831;?#32510;跨?#23337;粠绛?#23536;呴槦鍒?#28051;?#32457;婚櫎?#32029;杩?#38322;岀敱浜嶤oncurrentLinkedQueue鏄?#32510;跨?#23338;畨鍏?#37928;勶紝鍥?#38000;岃兘淇?#29831;?#23011;忔閮芥槸闃?#37714;楀鐨?#32510;跨?#23338;?#26944;埌?#25891;?#32029;鍥?#38000;屽湪寰楀埌?#25891;鍖?#28751;嗛槦鍒楀?#23544;?#23130;櫎銆倁nlock閫昏緫姣?#26440;冪畝鍗曪紝鍙渶瑕佸皢locked瀛?#23048;?#37813;撳紑锛堣缃?#28051;false锛夛紝?#25948;?#21835;锛坲npark锛夐槦鍒楀鐨?#32510;跨?#23338;嵆?#24434;?#32029;?#21159;鍚?#29831;?#32510;跨?#23337;細缁?#32513;湪lock鏂规硶鐨剋hile寰?#37916;?#28051;?#32513;?#32513;珵浜塽nlocked瀛?#23048;碉紝骞?#28751;嗗畠鑷?#23480;变粠绾跨闃?#37714;?#28051;?#32457;婚櫎琛?#32448;鸿幏寰楅攣鎴?#37716;?#37510;?#35120;撶劧?#30056;鍏捣瑙侊紝鏈濂藉湪unlock涓姞鍏?#28051;浜涢獙璇?#38315;昏緫?#32029;濡?#29785;攣鐨?#32510;跨?#23338;?#23677;姞?#25891;鐨?#32510;跨?#23342;渶瑕?#37929;?#37722;?#37510;?br />
?#21159;鑰?#37832;枃鐨?#37929;?#37928;?#37828;?#38007;?#23480;卞疄鐜?#28051;涓狶ock瀵硅薄?#32029;鍗?#37721;?#28003;跨敤涓浜涘熀鏈?#37928;勬搷浣滐紝鑰?#28051;?#28003;跨敤JDK鎻愪緵鐨凙tomic?#34987;?#25328;ConcurrentLinkedQueue銆傜?#35763;技鐨勯鍏堟?#25116;滑涔熼渶瑕?#28051;涓?#38339;?#37714;楀?#27195;斁绛?#23536;?#32510;跨闃?#37714;?#38171;?#37711;?#39582;宠捣瑙侊紝浣跨敤鍏?#26473;涘厛鍑?#38339;?#37714;?#38171;夛紝鍥?#38000;屽?#22557;畾涔?#28051;涓狽ode瀵硅薄?#25956;浠?#37835;勬垚杩?#28051;?#38339;?#37714;?#38171;?br />

 

    protected static class Node {
        
volatile Thread owner;
        
volatile Node prev;
        
volatile Node next;
        
        
public Node(Thread owner) {
            
this.owner = owner;
            
this.state = INIT;
        }
        
        
public Node() {
            
this(Thread.currentThread());
        }
    }

?#30045;鍗曡捣瑙侊紝闃?#37714;楀鏄?#28051;涓?#29863;风偣鐨刾laceholder?#32029;姣?#28051;皟?#25956;lock鐨?#32510;跨閮藉厛灏?#38007;?#23480;辩珵浜夋?#24807;叆杩?#28051;?#38339;?#37714;楀?#25733;紝姣?#28051;?#38339;?#37714;楀?#26449;悗涓涓?#32510;跨锛圢ode锛?#37719;?#37828;?#38014;?#23536;楅攣鐨?#32510;跨?#23350;紝?#22669;浠?#37812;戜滑?#28214;瑕?#37832;head Node瀛?#23048;电敤浠?#36423;?#38315;熻幏鍙栭槦鍒楀鐨勫悗涓涓狽ode?#32029;鑰宼ail Node瀛?#23048;电敤?#28533;蹇?#38315;熸彃鍏?#37826;?#37928;凬ode?#32029;?#22669;浠?#37711;抽?#28266;浜?#28641;?#28003;曠?#36328;?#23338;畨鍏?#37928;勬?#21227;缓杩?#28051;?#38339;?#37714;楋紝鏂规硶杩?#37828;?#28051;鏍?#37928;勶紝浣跨敤CAS鎿?#28003;滐紝鍗CAS鏂规硶灏?#38007;?#23480;?#29825;?#32515;?#37812;tail鍊硷紝?#21159;鍚?#38322;嶆?#29256;?#21227;缓杩?#28051;?#37714;?#29723;?#38171;?br />

    protected boolean enqueue(Node node) {
        
while (true) {
            
final Node preTail = tail;
            node.prev 
= preTail;
            
if (compareAndSetTail(preTail, node)) {
                preTail.next 
= node;
                
return node.prev == head;
            }
        }
    }

?#28266;褰撳墠绾跨?#23154;ode浠?#32510;跨?#23338;畨鍏?#37928;勬柟寮忔?#24807;叆杩?#28051;?#38339;?#37714;楀悗?#32029;lock瀹?#37916;?#37929;?#28725;瑰氨姣?#26440;冪畝鍗曚簡?#32029;濡傛灉褰撳墠Node鏄?#37928;勫墠椹?#37828;痟ead?#32029;璇?#32510;跨?#23341;幏寰楅攣?#32029;鍚?#37714;park褰撳墠绾跨?#23350;紝澶勭悊park鏃犵悊鐢?#26473;?#37733;?#37928;勯棶棰橈紝鍥?#38000;屽皢park鏀惧叆while寰?#37916;?#28051;?#38171;?#29831;?#28729;?#37916;版槸涓涓?#28051;嶅彲閲嶅叆鐨勫疄鐜?#38171;?#38171;?br />

    public void lock() {
        
// Put the latest node to a queue first, then check if the it is the first node
        
// this way, the list is the only shared resource to deal with
        Node node = new Node();
        
if (enqueue(node)) {
            current 
= node.owner;
        } 
else {
            
while (node.prev != head) {
                LockSupport.park(
this); // This may return "spuriously"!!, so put it to while
            }

            current 
= node.owner;
        }
    }

unlock鐨勫疄鐜伴渶瑕?#38000;?#38095;?#28598;?#32457;嶆儏鍐碉紝濡傛灉褰撳墠Node(head.next)鏈?#37722;庨?#24971;紝?#20549;涔?#37929;?#37818;unpark璇?#37722;庨鍗冲彲锛涘?#20635;灉?#30149;鏈夛紝琛?#32448;?#35120;撳墠宸?#32513;忔病鏈?#37711;?#28000;?#32510;跨?#23338;湪绛?#23536;呴槦鍒?#28051;紝?#21159;鑰屽湪杩?#28051;?#37714;?#37826;?#26473;囩涓彲?#20824;?#32048;鏈?#37711;?#28000;?#32510;跨杩涘叆?#32029;鍥?#38000;岄渶瑕佺敤CAS鐨勬柟寮?#29825;?#32515;畉ail?#32029;濡傛灉璁?#32515;?#28598;辫|?#32029;琛?#32448;烘鏃?#37832;?#37711;?#28000;?#32510;跨杩涘叆?#32029;鍥?#38000;岄渶瑕佸皢璇?#37826;?#26473;涘叆鐨?#32510;跨?#23202;npark?#31904;鑰?#29831;?#37826;?#26473;涘叆鐨?#32510;跨?#23338;湪?#30367;?#25956;park鍚庡彲浠?#32468;嬪嵆杩?#37733;?#38171;?#26473;欓噷鐨凜AS?#25328;enqueue鐨凜AS閮芥槸瀵箃ail鎿?#28003;滐紝鍥?#38000;岃兘淇?#29831;佺姸鎬?#28051;?#22183;锛?#38171;?br />

    public void unlock() {
        Node curNode 
= unlockValidate();
        Node next 
= curNode.next;
        
if (next != null) {
           
head.next = next;
            next.prev 
= head;
            LockSupport.unpark(next.owner);
        } 
else {
            
if (!compareAndSetTail(curNode, head)) {
               
while (curNode.next == null) { } // Wait until the next available
                // Another node queued during the time, so we have to unlock that, or else, this node can never unparked
                unlock();
            } 
else {
               
compareAndSetNext(head, curNode, null); // Still use CAS here as the head.next may already been changed
            }
        }
    }

?#21503;浣?#37928;勪唬鐮佸?#23678;祴璇曠被?#24434;浠?#37721;?#38000;冩煡鐪?a href="https://github.com/dinglevin/levin-learn/tree/master/levin-learn-corejava/src/main/java/levin/learn/corejava/concurrent/locks">杩欓噷銆?br />


鍏跺疄鐩村埌鑷?#23480;?#37712;欏畬杩?#28051;被鍚庢墠鐩村埌鑰?#37711;跺疄杩?#37828;?#28051;涓狹CS?#25891;鐨勫彉绉嶏紝鍥?#38000;?#26473;?#28051;?#28729;?#37916;版瘡涓?#32510;跨?#23197;ark?#28266;鑷?#38892;?#28725;?#25652;?#37928;刵ode涓婏紝鑰岀?#21342;墠涓涓?#32510;跨?#23202;npark?#30048;锛?#38000;孉bstractQueuedSynchronizer鏄疌LH?#25891;?#32029;鍥?#28051;哄畠鐨刾ark鐢卞墠椹辩姸鎬?#37712;冲畾?#32029;铏界劧?#30048;涔?#37828;?#37922;卞墠涓涓?#32510;跨?#23202;npark?#30048;銆傚叿浣撳彲浠?#37721;?#38000;?a href="http://coderbee.net/index.php/concurrent/20131115/577">杩欓噷銆?/p>

DLevin 2015-08-11 06:08 ?#24386;琛?#29831;勮
]]>
˫ɫֱ
<div id="k0jz6"><tr id="k0jz6"></tr></div>
    <sup id="k0jz6"><menu id="k0jz6"><small id="k0jz6"></small></menu></sup>
    <div id="k0jz6"><tr id="k0jz6"><mark id="k0jz6"></mark></tr></div><em id="k0jz6"></em>
    <dl id="k0jz6"><ins id="k0jz6"></ins></dl><dl id="k0jz6"></dl>
      <div id="k0jz6"></div>

      <sup id="k0jz6"></sup><dl id="k0jz6"><ins id="k0jz6"><thead id="k0jz6"></thead></ins></dl>
        <div id="k0jz6"><tr id="k0jz6"></tr></div>
          <sup id="k0jz6"><menu id="k0jz6"><small id="k0jz6"></small></menu></sup>
          <div id="k0jz6"><tr id="k0jz6"><mark id="k0jz6"></mark></tr></div><em id="k0jz6"></em>
          <dl id="k0jz6"><ins id="k0jz6"></ins></dl><dl id="k0jz6"></dl>
            <div id="k0jz6"></div>

            <sup id="k0jz6"></sup><dl id="k0jz6"><ins id="k0jz6"><thead id="k0jz6"></thead></ins></dl>