ap4r on rubyconf2007

230
AP4R Kiwamu Kato & Shun’ichi Shinohara Future Architect, Inc. RubyConf 07 ( 2 Nov. 2007 ) 1

Upload: kato-kiwamu

Post on 21-Jan-2015

2.895 views

Category:

Business


0 download

DESCRIPTION

The presentation material at RubyConf2007 at Chrlotte

TRANSCRIPT

  • 1. RubyConf 07 ( 2 Nov. 2007 ) AP4R Kiwamu Kato & Shunichi Shinohara Future Architect, Inc.1

2. Introduction2 3. kiwamu Majored in Civil Engineering Have enhanced and maintainedmessaging middle-warein our company 3 4. kiwamu Started Ruby last year Leader of the AP4R team http://d.hatena.ne.jp/kiwamu/ The rst month of marriage4 5. shino Programmer, I love coding ! The language of the year is C or Haskell 5 6. shino Made a debut to the OSS worldlast year http://d.hatena.ne.jp/ita-wasa Sunday physicist,interested in the string theory6 7. Corporate prole Future Architect, Inc. http://www.future.co.jp/en/index.html Aiming for optimization and synergy of business and IT Headquarters: Tokyo, Japan Foundation: November 28, 1989 Number of employees: 594 (as of June 2007) Revenue: 14,831M (2006)7 8. Agenda 1. Overview ( the most important part. ) 2. Lightweight 3. Demo 4. Robust 5. Other features 6. Conclusion 8 9. 1. Overview 9 10. 1-1. What is AP4R ? 1-2. Typical use cases 1-3. What are advantages of AP4R ? 1-4. Case examples 10 11. What is AP4R ?11 12. What is AP4R ? A messaging library by Ruby Asynchronous Processing for Ruby Loose coupling by messaging12 13. Why did we start AP4R ? Developed a messaging middle-warein house Named RtFA 13 14. RtFA Pure Java Proprietary protocol and API ( JMS) Time-proven on various systems 14 15. One of illustrative cases of RtFA Mission critical system in transportation business http://japan.internet.com/linuxtoday/20061228/5.html 15 16. Project summary Over 100 million package data per day Parallel processing Load distribution 16 17. Project summary Approx. 100 servers Used RtFA 17 18. http://www.pragmaticprogrammer.com/titles/fr_j2r/http://www.oreilly.co.jp/books/9784873113203/18 19. From RtFA to AP4R Based on experiences of Java, carefully designed to keep good concepts and added agility Focused on ease of useAPI, conguration19 20. Open source Released as OSS last year Take advantages of the community For Enterprise20 21. Typical use cases of AP4R Quicker response to users Load distribution21 22. Quicker response to users22 23. If quick response is not needed,execute it later 23 24. Synchronously Client Server Sequential tasks24 25. Synchronously ClientServer A B C D ESequential tasks24 26. Better solution Client Server Messaging Server25 27. Better solution Client Server Messaging Server A B25 28. Asynchronously Client Server Messaging Server A B26 29. Asynchronously Client Server Messaging Server A B C D E26 30. Typical cases Logging Sending mail Creating heavy summary data27 31. Advanced case Intersystem connection 28 32. Moreover, load distribution 29 33. Easy to scale out Client Server Messaging Server 30 34. Easy to scale out Client Server Messaging Servers 31 35. There are some solutions that also use messaging servicein the ( Ruby ) world. 32 36. What are AP4Rsadvantages?33 37. Lightweight andRobust34 38. Lightweight ?35 39. 36 40. Rubys advantages Dynamic Succinct Open class Meta programming etc.. 37 41. AP4R realized Ease of use Simple API / conguration Seamless collaboration with Rails All-in-one support Not only development But also test and operation support 38 42. One of the advantages isLightweight 39 43. Robust ? 40 44. AP4R guarantees a message delivery 41 45. If a message delivery is NOT guaranteed,... 42 46. it may causemessage lost or message duplication 43 47. In other words... 44 48. No items despite payment ! Double withdrawals ! 45 49. Messages should be delivered in a reliable way, 46 50. No matter what happens Database trouble Network trouble Server down Busy and timeout ... etc 47 51. The other advantage isRobust 48 52. AP4R is ..49 53. Lightweight andRobust50 54. LightweightRobust Simple API RecoveryTest support FlexibleSAF system design 51 55. Case examples52 56. Online game (not opened yet to the public) Interactivemediums.com 53 57. Online game54 58. What is it ? From England Massively multiplayer web-based strategy game Late alpha now; private beta testing in January 200855 59. They use AP4R for Mail sending Report generation Data analysis routines A variety of processor-intensive processes including database access 56 60. Scale Two dedicated AP4R servers Four Rails processes per AP4R A few hundred messages an hour MySQL to store messages 57 61. Interactivemediums.com58 62. http://interactivemediums.com/ 59 63. What is it ? From the U.S. Provide mobile messaging solutions for clients, as well as internal products like TextMe for Business ( http://textmeforbusiness.com ) In production since July 200760 64. http://textmeforbusiness.com/ 61 65. They use AP4R for Intersystem connection Inbound queue Outbound queue Routing queue Text processing 62 66. Alert service on SMSAP4R queues acceptanceinbound Text processingdeliverydeliveryoutbounddelivery processingThird Partyroutingapplication63 67. Scale Three AP4R processes Four mongrel clusters and so forth Now at least a thousand messages a day ( Several thousand messages a day soon ) MySQL to store messages 64 68. What did they choose AP4R ?65 69. Questionnaire results Easy to distribute a kind of load acrossa number of servers Easy to scale Development is active Development team had real-world experiencewith the technology 66 70. Coffee break 67 71. :Firefox => (c) 2007 Mozilla Japan68 72. Only in Japan ?Foxkeh :Firefox => (c) 2007 Mozilla Japan 69 73. Duke :Java =>https://duke.dev.java.net/70 74. cute ! / cute ? 71 75. Mascot matters72 76. :AP4R => ...73 77. Copyright 2007 by KanicanLicensed under Creative Commons License.74 78. What / Why is that ? > AP4R.sub(/4/, A)> APAR.downcase > apar Three-banded armadilloDictionaryhttp://www.alc.co.jp/ Photo http://www.ickr.com/photos/jeffclow/29738818/75 79. Copyright 2007 by KanicanLicensed under Creative Commons License. 76 80. Copyright 2007 by Kanican 77 81. His name is Maro 78 82. Coffee break is done :-) 79 83. 2. Lightweight80 84. LightweightRobust Simple API RecoveryTest support FlexibleSAF system design 81 85. 2-1. Simple API2-2. Flexible system design 82 86. Simple API83 87. API to put messages 84 88. ap4r.async_to({destination}, {data} [,{options}])Theres also a block stylec.f. RDoc: http://ap4r.rubyforge.org/doc/ 85 89. Class AsyncShopController < ApplicationController def order # synchronous parto = Order.new(:name => params[:name])o.save!ap4r.async_to({:action => payment},{:order_id => o.id}) redirect_to ...end def payment # asynchronous partPayment.new(:order_id => params[:order_id]).save!...render :text => trueendend86 90. Process ow87 91. User Apache Rails AP4R 88 92. User Apache Rails AP4Rorder message put 89 93. User Apache RailsAP4R dispatch 90 94. User ApacheRails AP4Rpayment 91 95. Flexible system design92 96. Typical design 93 97. User Apache Rails AP4R 94 98. Load distribution for AP4R servers95 99. User Apache Rails AP4R 96 100. Separate serversfor sync/async 97 101. User ApacheRails AP4R syncasync98 102. Separate servers including reverse proxy 99 103. User Apache RailsAP4R URL rewrite 100 104. User ApacheRailsAP4RURL rewrite HTTP100 105. User ApacheRailsAP4RURL rewrite HTTP101 106. Dispatcher ProducerMessagingConsumer AP4R putdispatch Generallyputget 102 107. Protocols Default is HTTP POST Now supported SOAP XML-RPC dRuby 103 108. Flow management 104 109. Two kinds of tasks processingnumbertimeTask busymany shortTask heavyfew very long105 110. Conguration dispatchers: - targets: queue.very_busy.* threads: 10 modify_rules: url: proc {|url| url.host = quot;busy.async.hostquot;} - targets: queue.very_heavy.* threads: 1 modify_rules: url: proc {|url| url.host = quot;heavy.async.hostquot;}106 111. busy URL rewriteheavy107 112. Lightweight Robust Simple APIRecoveryTest support Flexible SAF system design 108 113. 3. Demo 109 114. Outline Shopping store application Order and payment110 115. Shopping store app.Client ServerA B order payment111 116. Better solution AP4R ClientRailsRails 3 112 117. Synchly orderAP4R Client RailsRails 3Aorder 113 118. Asyncly paymentAP4R Client RailsRails 3ABorderpayment 114 119. Monitor message statusAP4R Client RailsRails 3 100755025 0115 120. Outline Use mongrel cluster Stress by ab Monitor queue retaining status 116 121. 117 122. 118 123. Agenda 1. Overview ( the most important part. ) 2. Lightweight 3. Demo 4. Robust 5. Other features 6. Conclusion 119 124. 4. Robust120 125. Lightweight Robust Simple APIRecoveryTest support Flexible SAF system design 121 126. Stored And Forward122 127. EnsureAtomicity of application transaction and putting messages123 128. Rails app-DB AP4Rmsg-DBCRUDStore SAF createcommitForwardmessage put insert /commitSAF delete 124 129. Enterprise Integration Patterns: Designing, Building, and Deploying Messaging Solutionsby Gregor Hohpe,Bobby Woolf Guaranteed Delivery [122] 125 130. Details 126 131. Typical ow to create async. messages127 132. start end 128 133. start CRUD end128 134. start CRUDcommit end128 135. start CRUDcommitmessage put / commit end128 136. start CRUDcommitmessage put / commit enddata128 137. start CRUDcommitmessage put / commit enddatamessages128 138. EnsureAtomicity of application transaction and putting messages129 139. On updating database If error occurs, Any following messagesmust NOT be queued 130 140. On updating database If everything succeeded, All following messagesmust be queued 131 141. Relief132 142. Lightweight andRobust133 143. Ap4r::Client#transaction Class ShopController < ApplicationController def order # synchronous side ap4r.transaction do o = Order.new(:name => params[:order_id]) o.save! ap4r.async_to({:action => payment},{:order_id => o.id})endredirect_to ... end end134 144. Process ow changes in queueing sequence135 145. Behind ap4r.transactionap4r.transaction do ... # CRUD on database ap4r.async_to(...) end 136 146. Behind ap4r.transactionap4r.transaction do ... # CRUD on database Insert into SAF tableap4r.async_to(...) end 136 147. Behind ap4r.transactionap4r.transaction do ... # CRUD on database Insert into SAF tableap4r.async_to(...) end Commit database136 148. Behind ap4r.transactionap4r.transaction do ... # CRUD on database Insert into SAF tableap4r.async_to(...) end Commit database Queue messages 136 149. Behind ap4r.transactionap4r.transaction do ... # CRUD on database Insert into SAF tableap4r.async_to(...)Store end Commit database Queue messages 136 150. Behind ap4r.transactionap4r.transaction do ... # CRUD on database Insert into SAF tableap4r.async_to(...)Store end Commit database Queue messagesForward 136 151. Sequence in detail Without SAF137 152. start CRUDcommitmessage put / commit enddatamessages 138 153. start CRUDcommitmessage put / commit enddatamessages 138 154. start CRUDcommitmessage put / commit enddatamessages 139 155. start CRUDcommitmessage put / commit enddatamessages 139 156. start CRUDcommitmessage put / commit enddatamessages 139 157. start CRUDcommitmessage put / commit enddatamessages 139 158. start CRUDcommitmessage put / commit enddatamessages 139 159. start CRUDcommit counterchange message put / commit enddatamessages 140 160. start CRUD message put / commit counterchange commit enddata messages 141 161. start CRUD message put / commitcommit enddata messages 142 162. start CRUD message put / commitcommit enddata messages 142 163. start CRUD message put / commitcommit enddata messages 143 164. start CRUD message put / commitcommit enddata messages 143 165. start CRUD message put / commitcommit enddatamessages 143 166. start CRUD message put / commitcommit enddatamessages 143 167. start CRUD message put / commitcommit enddatamessages 143 168. Better Safe than Sorry Database trouble Network trouble Server down busy and timeout ... etc144 169. Embrace every exceptions 145 170. Sequence in detailWith SAF146 171. CRUD start SAF create commit message put / commitSAF update end data messages 147 172. CRUD start SAF create commit message put / commitSAF update end data messages 148 173. CRUD start SAF create commit message put / commitSAF update end data messages 148 174. CRUD start SAF create commit message put / commitSAF update end data messages 149 175. CRUD start SAF create commit message put / commitSAF update end data messages 149 176. CRUD start SAF create commit message put / commitSAF update end data messages 149 177. CRUD start SAF create commit message put / commitSAF update end data messages 149 178. CRUD start SAF create commit message put / commitSAF update end data messages 149 179. CRUD start SAF create commit message put / commitSAF update end data messages 149 180. CRUD start SAF create commit message put / commitSAF update end data messages 150 181. CRUD start SAF create commit message put / commitSAF update end data messages 150 182. CRUD start SAF create commit message put / commitSAF update end data messages 151 183. CRUD start SAF create commit message put / commitSAF update end data messages 151 184. CRUD start SAF create commit message put / commitSAF update enddatamessagesmessages151 185. CRUD start SAF create commit message put / commitSAF update enddatamessagesmessages151 186. CRUD start SAF create commit message put / commitRecoverable SAF update enddata messagesmessages151 187. CRUD start SAF create commit message put / commitSAF update enddatamessagesmessages151 188. CRUD start SAF create commit message put / commitSAF update enddatamessagesmessages151 189. CRUD start SAF create commit message put / commitSAF update end data messages 152 190. CRUD start SAF create commit message put / commitSAF update end data messages 152 191. CRUD start SAF create commit message put / commitSAF update end data messages 153 192. CRUD start SAF create commit message put / commitSAF update enddatamessagesmessages153 193. CRUD start SAF create commit message put / commitSAF update enddatamessagesmessages153 194. CRUD start SAF create commit message put / commit Duplicated SAF update enddata messagesmessages153 195. CRUD start SAF create commit message put / commitSAF update enddatamessagesmessages153 196. CRUD start SAF create commit message put / commitSAF update enddatamessagesmessages153 197. EnsureAtomicity of application transactionand putting message154 198. at-least-once between Producer and Channel 155 199. Lightweight Robust Simple APIRecoveryTest support Flexible SAF system design 156 200. 5. Other features157 201. Messages recoverywhen some troubles happen Collaboration with Capistrano (2.x) Test support 158 202. Test support 159 203. TDD / BDD160 204. It makes us feel relieved indeveloping an application 161 205. On async app. Test matters also, but .. Multiple processescommunicating via N/W take time annoying to setup162 206. Two ways of testing Functional test no N/W communication fast but restricted Async test using N/W communication slow but close to actual163 207. Functional test Provided a queue stub Executed in memory, quickly164 208. code sample test code here is against a ctional on-line shop application, which has an order action (sync)and payment action (async).165 209. def test_order post :order, :order => {:item => quot;introduction to AP4Rquot;} assert_response :redirect assert_redirected_to :action => 'index'messages = @controller.ap4r.queued_messages # ... (1) assert_equal 1, messages.keys.size, quot;should have messages in just ONE queuequot; assert messages.key?(quot;queue.async_shop.paymentquot;), quot;queue name is INCORRECTquot; # ... (2) assert_equal 1, messages[quot;queue.async_shop.paymentquot;].size, quot;should have just ONE message for paymentquot; assert_match /order_id=d+/, messages[quot;queue.async_shop.paymentquot;].first[:body], quot;parameter order_id should be included with a numeric valuequot;# ... (3) end1. get (stubbed)queued messages2. assert the queue name3. assert the message content 166 210. functional test rake .. CRUD message put Stub assertassert 167 211. Async test Get on reality Test everything, e.gserialization, cooperation. 168 212. code sample test code here is against a ctional application.169 213. [RAILS_ROOT]/test/async/ap4r_test_helper.rbENV[quot;RAILS_ENVquot;] = quot;testquot;require File.expand_path(File.dirname(__FILE__) + quot;/../../config/environmentquot;)require quot;ap4r/service_handlerquot; ap4r_test_helper = Ap4r::ServiceHandler.new require 'test_help' class Test::Unit::TestCaseself.use_transactional_fixtures = falseself.use_instantiated_fixtures = false # Add more helper methods to be used by all tests here...cattr_accessor :ap4r_helper def ap4r_helper@@ap4r_helperend def with_services(&block)ap4r_helper.with_services(&block)endendTest::Unit::TestCase.ap4r_helper = ap4r_test_helper 170 214. [RAILS_ROOT]/test/async/async_shop_test.rb require quot;#{File.dirname(__FILE__)}/ap4r_test_helperquot; require 'net/http'class AsyncShopTest < Test::Unit::TestCasedef test_http_dispatch ap4r_helper.stop_dispatchers# ... (1)assert_rows_added(Order, 1) { # ... (3) do_order# ... (2) } assert_rows_added(Payment, 1) { # ... (6) ap4r_helper.start_dispatchers # ... (4) ap4r_helper.wait_all_done # ... (5) } endprivate # Requests to async_shop/order. def do_order(item_name = quot;test itemquot;) Net::HTTP.start(quot;localhostquot;, 3000, nil, nil) do |http| http.request_post(quot;/async_shop/orderquot;, quot;order[item]=#{item_name}quot;) do |res| #nop end end enddef assert_rows_added(model, rows) rows_before = model.count yield rows_after = model.count assert_equal rows, rows_after - rows_before, quot;table '#{model.table_name}' should count up by #{rows}quot; end end 171 215. stop dispatcher threads 1.request to Rails with HTTP POST 2.assert one row added to orders table 3.start dispatcher threads 4.wait for async action is completed 5.assert one row added to payments table 6. 172 216. async test rake ..service control HTTP CRUDmessage putassert HTTP CRUDassert173 217. Summary Two test methods. Functional test Async test It makes us feel relieved todevelop an application 174 218. 6. Conclusion175 219. AP4R is ..176 220. Lightweight andRobust177 221. Lightweight Robust Simple APIRecoveryTest support Flexible SAF system design 178 222. Messaging with simple API,simple conguration Seamless collaboration with Rails Flexible system design 179 223. Automatically invocation ofasync. processing by dispatcher Guaranteed delivery by SAF Test support Recovery and deployment 180 224. Future Plans181 225. Smooth operation ver.0.3.x Daemonize URL-rewrite lter DLQ / SAF recovery Protocols: Stomp, HTTP182 226. Monitoring ver.0.4.x Coordinate with Cacti, ZABBIX, etc. Many AP4R processes Interactive management tool183 227. Step to large scale ver.0.5.x Dynamic conguration Automatic recovery Long run 24/7 184 228. Others Security Application testing support Blocking queues Ruby off Rails 185 229. AP4R@RubyForge wiki: http://ap4r.rubyforge.org/wiki/wiki.pl?HomePage source:svn co http://ap4r.rubyforge.org/svn/trunk/ap4r ML: http://rubyforge.org/mailman/listinfo/ap4r-user Your patches are welcome! Thank you.by Kanican 186 230. Future Architect logo belongs to Future Architect, Inc. Japan. All rights reserved. Rails logo is trademarks of David Heinemeier Hansson. All rights reserved. The Ruby logo is copyrighted (c) 2006, Yukihiro Matsumoto. It is released under the terms of the Creative Commons Attribution-ShareAlike 2.5 License. 187