一個包子鋪看懂 I/O 模型演變

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"小眼睛打算開個包子鋪,拜訪了 N 個餐飲界大佬後,決定直接搞 O2O 模式。依據大佬們透露的數據,省掉了房租、水電、工資,結合平臺的優勢只要按照每年 10% 的增速,用不了多久就能迎娶白富美走上人生巔峯了。在美好的幻想中,包子托拉斯開業了……"}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#494949","name":"user"}}],"text":"BIO"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"開張第一個月,下單量短暫增長,一週之後開始迅速滑落。持續收到大量投訴:客戶投訴送餐太慢,騎手投訴取餐等待太久。眼看着剛開張,就有倒閉的預兆。包子托拉斯請來了著名諮詢公司鷹邦邦給大家支招。鷹邦邦的基爾,梳理出來了整個運轉的流程:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"收到外賣訂單後("},{"type":"text","marks":[{"type":"bgcolor","attrs":{"color":"#FFBEBF","name":"pink"}}],"text":"socket 建立,生成 FD"},{"type":"text","text":"),爲了不影響包子口感是不能提前打包的。外賣騎手到達後,報出自己的訂單號("},{"type":"text","marks":[{"type":"bgcolor","attrs":{"color":"#FFBEBF","name":"pink"}}],"text":"可讀事件到來"},{"type":"text","text":")。後廚根據訂單號找到對應訂單,根據訂單數據,配餐、打包、交給騎手("},{"type":"text","marks":[{"type":"bgcolor","attrs":{"color":"#FFBEBF","name":"pink"}}],"text":"寫事件"},{"type":"text","text":"),確認訂單已配送("},{"type":"text","marks":[{"type":"bgcolor","attrs":{"color":"#FFBEBF","name":"pink"}}],"text":"socket 關閉"},{"type":"text","text":"),再繼續處理下一個騎手拿到的訂單。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"後廚同一時刻只能處理一筆訂單,某筆訂單配餐的過程較長時,其餘騎手的訂單都需要等待("},{"type":"text","marks":[{"type":"bgcolor","attrs":{"color":"#FFBEBF","name":"pink"}}],"text":"阻塞"},{"type":"text","text":")前面的訂單處理完畢。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"問題:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一個時間只能處理一個訂單,即便是後面的訂單是個準備工作很簡單的訂單,也必須等待。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/c1/c1d9eb25a45cd3e2c8dd686fd8e14c2a.png","alt":"IO 多路複用.png","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#494949","name":"user"}}],"text":"BIO + 多線程"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"鷹邦邦的人果然是高手,短短 1 天時間給出了提升方案。根據每份外賣的平均準備時長、高峯期的訂單數等數據,建議把後廚分成 4 組("},{"type":"text","marks":[{"type":"bgcolor","attrs":{"color":"#FFBEBF","name":"pink"}}],"text":"線程池"},{"type":"text","text":")。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"騎手到達後("},{"type":"text","marks":[{"type":"bgcolor","attrs":{"color":"#FFBEBF","name":"pink"}}],"text":"可讀事件到達"},{"type":"text","text":"),隨機從空閒的 4 組裏面選擇一組處理訂單。4 組都沒有空閒下來時,騎手等待。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"改進:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"同一時刻,處理訂單的能力比之前增加"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"問題:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"每一個訂單使用一個線程,線程數量有限(包子托拉斯只有 4 個),沒訂單時,後廚資源都在等待"}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#494949","name":"user"}}],"text":"NIO + 多線程"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"又過了兩個月,後廚的老於約小眼睛聊這段時間的工作情況。言談中,老於流露出的意思是,分了組整個配餐的速度確實提升了很多,但是後廚的兄弟們除了配餐還有其他工作。高峯期間總是盯着訂單,耽誤其他工作。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"有問題就改,經過內部討論,決定招聘一個導購MM。招聘通知發出去沒幾天,身材高挑的麗麗就上崗了。麗麗的職責是負責分配所有的騎手訂單給到後廚。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當外賣騎手到達後,麗麗從所有訂單中找出當前騎手負責的訂單("},{"type":"text","marks":[{"type":"bgcolor","attrs":{"color":"#FFBEBF","name":"pink"}}],"text":"selector 從所有 FD 中找到發生事件的 FD"},{"type":"text","text":"),把訂單給到後廚,後廚的 4 個小組中空閒的小組,根據訂單信息配餐。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"麗麗去個衛生間,所有騎手的單子就不能處理("},{"type":"text","marks":[{"type":"bgcolor","attrs":{"color":"#FFBEBF","name":"pink"}}],"text":"selector 阻塞"},{"type":"text","text":")"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"改進:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"麗麗專職處理訂單的委派,後廚專職配餐,各司其職互不影響"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"問題:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"麗麗要一直守着門口("},{"type":"text","marks":[{"type":"bgcolor","attrs":{"color":"#FFBEBF","name":"pink"}}],"text":"selector 阻塞"},{"type":"text","text":"),不工作或者工作怠慢,後面就沒有訂單處理"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/dd/dd6c4ae83108c0e5882c769c84a25db9.png","alt":"IO 多路複用 (1).png","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#494949","name":"user"}}],"text":"NIO 加強(epoll)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"麗麗和後廚吵架了,後廚老於覺得麗麗可以幫忙乾點小事兒,打掃個衛生、幫忙拿拿材料啥的。麗麗很委屈,離開了門口外賣騎手來了她不知道,會被大家各種催促。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"鷹邦邦的基爾又出手了。當騎手進入包子托拉斯的等待區,騎手點擊「到店取貨」,麗麗手中的終端設備會發出信號。("},{"type":"text","marks":[{"type":"bgcolor","attrs":{"color":"#FFBEBF","name":"pink"}}],"text":"事件到達會發出通知"},{"type":"text","text":")終端上展示等待取貨的訂單,麗麗只需要盯着終端設備的消息就可以了。麗麗平時可以做一些其他的事情,根據設備上的通知隨時的分配訂單給後廚。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"改進:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"麗麗專職處理訂單的委派,基於事件的通知而不是盲目等待"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"問題:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"麗麗的問題解決了,後廚是不是最高效地在運轉呢"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#494949","name":"user"}}],"text":"Reactor 模型"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"又過了一段時間,包子托拉斯又進行重量級改革。鷹邦邦給出終極解決方案:後廚按照流水線作業,有人負責配餐、有人負責包裝、有人負責覈驗訂單。負責配餐又細分爲負責包子、負責飲料、負責小菜的。("},{"type":"text","marks":[{"type":"bgcolor","attrs":{"color":"#FFBEBF","name":"pink"}}],"text":"處理線程職責單一"},{"type":"text","text":")"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"外賣騎手到達後,麗麗根據系統中顯示的訂單信息("},{"type":"text","marks":[{"type":"bgcolor","attrs":{"color":"#FFBEBF","name":"pink"}}],"text":"事件到達後通知"},{"type":"text","text":"),把訂單交給後廚。後廚按照約定的流程順序("},{"type":"text","marks":[{"type":"bgcolor","attrs":{"color":"#FFBEBF","name":"pink"}}],"text":"責任鏈"},{"type":"text","text":"),完成自己的工作,轉交下一個人。最後外賣被打包,交給騎手。("},{"type":"text","marks":[{"type":"bgcolor","attrs":{"color":"#FFBEBF","name":"pink"}}],"text":"socket 寫事件完成"},{"type":"text","text":")"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/7e/7e273546638b8b005d3b3e6852fec6e0.png","alt":"IO 多路複用 (2).png","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"本次分享先到這裏,歡迎關注我一起交流,隨時指出各種錯誤和不足。"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/38/3874eef202c2e5d161059dcf58e1430f.png","alt":null,"title":"","style":[{"key":"width","value":"25%"},{"key":"bordertype","value":"boxShadow"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章