標籤:body ext round 使用者 優先 非同步 啟動 top 狀態切換
轉自 1190000006079389?from=groupmessage&isappinstalled=0簡介
說到協程(Coroutine),很多人會想到go,lua,erlang等語言,其實JVM上也有蠻多的實現,如PicoThread,Kilim,Quasar等,本文主要介紹其中一種Coroutine實現 -- Quasar Fiber,Quasar Fiber相對來說流行度更好一些,如果之前沒有接觸過協程(使用者級輕量級線程),可以看下What are fibers、Coroutine
那麼為什麼要使用協程?
協程可以用同步的編程方式達到或接近於純非同步效能,而沒有非同步帶來的Callback hell,雖然有很多機制或模式解決或解耦callback hell的問題, 但同步的編程方式更容易維護和理解(風格之爭是另外一個話題了,有興趣可以看下akka跟fiber的比較)
相比於os thread,fiber不管在記憶體資源還是調度上都比前者輕量的多,相對於thread blocking, fiber blocking可以達到比前者大幾個數量級的並發度,更有效利用CPU資源(運行fiber的worker線程並沒有block)
具體大家可以看下Why and When use Fiber
好像是個神奇的東西呢,咋實現的
相比於callback介面回調的非同步架構,Coroutine這個暫停和恢複在沒有JVM支援下,比較難以理解,是怎麼做到的?有沒有什麼魔法?其實JVM中Coroutine的實現方式有很多(implementing-coroutines-in-java),Quasar Fiber則是通過位元組碼修改技術在編譯或載入時織入必要的上下文儲存/恢複代碼,通過拋異常來暫停,恢複的時候根據儲存的上下文(Continuation),恢複jvm的方法調用棧和局部變數,Quasar Fiber提供相應的Java類庫來實現,對應用有一定的侵入性(很小)
Quasar Fiber 主要有 Instrument + Continuation + Scheduler幾個部分組成
Instrument 做一些代碼的植入,如park前後內容相關的儲存/恢複等
Continuation 儲存方法調用的資訊,如局部變數,引用等,使用者態的stack,這個也是跟akka等基於固定callback介面的非同步架構最大的區別
Scheduler 調度器,負責將fiber分配到具體的os thread執行
下面具體介紹下Quasar Fiber的實現細節,最好先閱讀下quasar官方文檔,不是很長
InstrumentWeaving
quasar fiber的運行需要織入一些指令,用於調用棧的儲存和恢複,quasar提供了三種方式進行織入(AOT、javaagent、ClassLoader)
quasar 會對我們的代碼進行static call-site分析,在必要的地方織入用於儲存和恢複調用棧的代碼。
哪些方法需要call site分析?這裡需要顯式的mark(jdk9不需要),如下
方法帶有Suspendable 註解
方法帶有SuspendExecution
方法為classpath下/META-INF/suspendables、/META-INF/suspendable-supers指定的類或介面,或子類
符合上麵條件的method,quasar會對其做call site分析,也許為了效率,quasar並沒有對所有方法做call site分析
方法內哪些指令需要instrument(在其前後織入相關指令)?
調用方法帶有Suspendable 註解
調用方法帶有SuspendExecution
調用方法為classpath下/META-INF/suspendables、/META-INF/suspendable-supers指定的類或介面,或子類
主要為瞭解決第三方庫無法添加Suspendable註解的問題
通過反射調用的方法
動態方法引動過程 MethodHandle.invoke
Java動態代理InvocationHandler.invoke
Java 8 lambdas調用
注意,instrument是在class loading的時候,不是runtime,所以這裡call site分析的比如virtual invoke指令是編譯期決定的,這裡比較容易犯錯,我總結了如下兩點
1.基於介面或基類編譯的代碼,如果實作類別有可能suspend,那麼需要在介面或基類中添加suspendable annotation或suspend異常
2.如果實作類別會suspend,需要添加suspendable annotation或suspend異常,當然可以把所有實作類別都聲明成suspendable(如果方法裡找不到suspend的調用,該方法將不被instrument,所以也沒有overhead,儘管這個overhead非常微小)
接下來我們簡單看下quasar instrument都織入了哪些代碼
從可以看出,quasar instrument主要在park()前儲存相關的局部變數和pc,再fiber恢複執行的時候通過pc(switch case跳轉的程式計數器,非寄存器pc) jump到park()之後的代碼並恢複局部變數,另外在方法調用前後還會push/pop相關的Contiuation
instrument還會對JUC(java.util.concurrent)中的Thread.park,替換成Fiber.park,這樣park to thread就變成park to fiber,所以使用juc的代碼,可以不用修改的跑在Fiber上
quasar在織入代碼的同時,會對處理的類和方法加上Instrumented註解,以在運行期檢查是否Instrumented,Instrumented註解包含了一個suspendableCallSites數組,用來存放方法體內suspendable call的line number
contiuations/stack詳細請看contiuations章節
QuasarInstrumentor
不管哪種織入方式,都是通過建立QuasarInstrumentor來處理Class的位元組流
QuasarInstrumentor內部使用ASM來處理Class的位元組流,通過SuspendableClassifier類來判斷是否需要instrument
SuspendableClassifier有兩個子類,分別為DefaultSuspendableClassifier和SimpleSuspendableClassifier
DefaultSuspendableClassifier 掃描classpath下SuspendableClassifier的實現,並且調用其介面判斷是否需要instrument,也會調用SimpleSuspendableClassifier
SimpleSuspendableClassifier 通過/META-INF/suspendables、/META-INF/suspendable-supers判斷
Quasar-core.jar包中suspendable-supers包含java nio及juc lock/future等介面,因為這些介面無法改變簽名,而quasar織入是在編譯或載入時,無法知道具體實作類別是否Suspendable,所以需顯式指定
Method Instrument實現細節
這裡是整個Quasar Fiber是實現原理中最為關鍵的地方,也是大家疑問最多的地方,大家有興趣可以看下原始碼,大概1000多行的ASM操作,既可以鞏固JVM知識又能深入原理理解Fiber,這裡我不打算引入過多ASM的知識,主要從實現邏輯上進行介紹
InstrumentClass 繼承ASM的ClassVisitor,對Suspendable的方法前後進行織入
InstrumentClass visitEnd中會建立InstrumentMethod,具體織入的指令在InstrumentMethod中處理
結合上面的instrument範例程式碼圖,不妨先思考幾個問題
1.怎麼找到suspend call
InstrumentMethod.callsSuspendables這個方法會遍曆方法的instructions,
如果instruction是method invoke,則判斷是否為suspend call(判斷邏輯見上面章節)
如果instruction為suspend call,則把instrunction序號和source line number分別紀錄到suspCallsBcis及suspCallsSourceLines這兩個數組,供後面邏輯使用
2.switch case跳轉織入是如何?的
現在我們知道了怎麼找到method中的suspend call,那如何把這些suspend calls拆分成instrument樣本圖中那樣呢(switch case,pc...)
這個拆分過程在InstrumentMethod.collectCodeBlocks
根據上面計算的suspend call的數組,分配label數組,然後根據pc計數器(詳細見後續章節)進行跳轉label
label是JVM裡用於jump類指令,如(GOTO,IFEQ,TABLESWITCH等)
quasar會把織入的上下文儲存恢複指令及代碼原始的指令產生到對應label
3.怎麼儲存、恢複局部變數,棧幀
- 在方法開始執行1.調用Stack.nextMethodEntry,開啟新的method frame- 在方法結束執行1.Stack.popMethod, 進行出棧- 在調用Suspendable方法之前,增加以下邏輯1.調用Stack.pushMethod 儲存棧幀資訊2.依次調用Stack.put儲存運算元棧資料3.依次調用Stack.put儲存局部變數- 在Suspendable方法調用後1.依次調用Stack.get恢複局部變數2.依次調用Stack.get恢複運算元棧 恢複局部變數和運算元棧的區別是前者在get後調用istore
因為Stack.put有3個參數,所以這裡每個put其實是多條jvm指令
aload_x //如果是儲存運算元棧,這條指令不需要,因為值已經在運算元棧了aload_x //load Stack引用iconst_x //load Stack idxinvokestatic co/paralleluniverse/fibers/Stack:push (Ljava/lang/Object;Lco/paralleluniverse/fibers/Stack;I)V
/** Stack.put會根據不同類型進行處理,Object或Array儲存到dataObject[],其他儲存到dataLong[]**/public static void push(long value, Stack s, int idx) public static void push(float value, Stack s, int idx)public static void push(double value, Stack s, int idx) public static void push(Object value, Stack s, int idx)public static void push(int value, Stack s, int idx)
java編譯期可知局部變數表和運算元棧個數,上面put或get依賴這些資訊,Stack具體邏輯見後面章節
4.什麼情況下在suspend call前後可以不織入也能正常運行
這裡其實是一個最佳化,就是如果method內部只有一個suspend call,且前後沒有如下指令
side effects,包括方法調用,屬性設定
向前jump
monitor enter/exit
那麼,quasar並不會對其instrument,也就不需要collectCodeBlocks分析,因為不需要儲存、恢複局部變數
5.suspend call在try catch塊中,如何處理
如果suspend call在一個大的try catch中,而我們又需要在中間用switch case切分,似乎是個比較棘手的問題,
所以在織入代碼前,需要對包含suspend call的try catch做切分,將suspend call單獨包含在try catch當中,通過ASM MethodNode.tryCatchBlocks.add添加新try catch塊,
quasar先擷取MethodNode的tryCatchBlocks進行遍曆,如果suspend call的指令序號在try catch塊內,那麼就需要切分,以便織入代碼
Fiber
下面介紹下Quasar Fiber中的提供給使用者的類和介面
Strand是quasar裡對Thread和Fiber統一的抽象,Fiber是Strand的使用者級線程實現,Thread是Strand核心級線程的實現
Fiber主要有幾下幾個功能
new
@SuppressWarnings("LeakingThisInConstructor")public Fiber(String name, FiberScheduler scheduler, int stackSize, SuspendableCallable<V> target)
| 屬性 |
類型 |
說明 |
| name |
String |
fiber名稱 |
| scheduler |
FiberScheduler |
調度器,預設為FiberForkJoinScheduler |
| stackSize |
int |
stack大小,預設32 |
| target |
SuspendableCallable<V> |
具體業務代碼,在SuspendableCallable.run()裡 |
建構函式主要完成以下幾件事情
設定state為State.NEW
初始化Stack(用於儲存fiber調用棧資訊,Continuations的具體實現)
校正target是否Instrumented
將當前fiber封裝成一個可以由scheduler調度的task,預設為FiberForkJoinTask
儲存Thread的inheritableThreadLocals和contextClassLoader到Fiber
start
Fiber.start() 邏輯比較簡單,如下
exec
fiber scheduler的worker thread從work quere擷取到task,並調用fiber.exec()
fiber.exec()主要步驟如下
cancel timeout task
將Thread的threadlocals、inheritableThreadLocals、contextClassLoader分別與fiber的互換,實現了local to fiber而不是local to thread,這裡需要特別注意
所以基於thread local和context classloader的代碼基本上都能運行在fiber上
state = State.RUNNING;
運行商務邏輯(方法fiber.run())
state = State.TERMINATED;
Fiber暫停時如何處理
fiber task切換有兩種方式,一種是fiber task正常結束, 一種是fiber task拋SuspendExecution
fiber.exec()裡會catch SuspendExecution,並交出執行許可權,具體步驟如下
stack sp = 0; // fiber恢複執行需要從最開始的frame恢複
設定fiber狀態 TIMED_WAITING/WAITING
恢複線程的Thread的threadlocals、inheritableThreadLocals、contextClassLoader
調用棧資訊已經在park()之前儲存到stack中(見instrument章節),所以這裡無需處理
park
暫停當前fiber的執行,並交出執行權
fiber task狀態: RUNNABLE -> PARKING -> PARKED
fiber狀態: RUNNING -> WAITING
Fiber.park方法如下,只能在當前fiber調用
static boolean park(Object blocker, ParkAction postParkActions, long timeout, TimeUnit unit) throws SuspendExecution
park主要邏輯如下
設定fiber狀態
如果設定了timeout,則向FiberTimedScheduler新增ScheduledFutureTask,用於逾時檢查
設定fiber.postPark = postParkActions,用於上面exec方法捕獲異常後執行
拋異常,移交執行許可權, 後續邏輯見exec章節移交執行許可權
unpark
恢複fiber的執行
fiber task狀態: PARKED -> RUNNABLE
fiber狀態: WAITING -> RUNNING
unpark主要也是做兩件事情,一是設定狀態,二是把fiber task重新submit到scheduler
這裡除了手工調用fiber的park,unpark來暫停和恢複fiber外,可以用FiberAsync類來將基於callback的非同步呼叫封裝成fiber blocking,基於fiber的第三方庫comsat就是通過將bio替換成nio,然後再封裝成FiberAsync來實現的,FiberAsync可參考http://blog.paralleluniverse....
狀態切換
fiber運行狀態由兩部分組成,一個是fiber本身的狀態,一個是scheduler task的狀態
fiber狀態
| 狀態 |
描述 |
| NEW |
Strand created but not started |
| STARTED |
Strand started but not yet running |
| RUNNING |
Strand is running |
| WAITING |
Strand is blocked |
| TIMED_WAITING |
Strand is blocked with a timeout |
| TERMINATED |
Strand has terminated |
task狀態,這裡以預設的FiberForkJoinTask為例
| 狀態 |
描述 |
| RUNNABLE |
可運行 |
| LEASED |
unpark時狀態是RUNNABLE,設定為LEASED |
| PARKED |
停止 |
| PARKING |
停止中 |
運行狀態切換圖
Continuation
Fiber/Coroutine = Continuation + scheduler可以看出,Continuation在Fiber中是至關重要的,他儲存了fiber恢複執行時的必要資料,如pc,sp等
Quasar 中Continuation的實現為Stack類
Stack
Stack類是quasar 對Fiber Continuation的實作類別,該類由quasar instrument調用,以儲存和恢複方法調用棧資訊
| 屬性 |
類型 |
說明 |
| sp |
int |
代表當前操作的frame序號 |
| dataLong |
long[] |
holds primitives on stack as well as each method‘s entry |
| dataObject |
Object[] |
holds refs on stack,防止jvm gc回收方法局部對象 |
dataLong中每一個long,代表一個method frame,具體定義如下
entry (PC) : 14 bits, 程式計數器,用於swich case跳轉
num slots : 16 bits, 當前method frame佔用多少個slot
prev method slots : 16 bits , 上一個method frame佔用多少個slot,主要用於pop跳轉
我簡單畫了一個stack例子,其中pc,slots,prev slots用逗號分隔,xxxxxx代表method frame額外的一些資料
下面idx和data分別代碼dataLong的序號和內容
| idx |
data |
說明 |
| 5 |
0L |
下一個frame的儲存位置,sp指向該節點 |
| 4 |
xxxxxxx |
方法2局部變數c |
| 3 |
xxxxxxx |
方法2局部變數b |
| 2 |
7 , 3 , 2 |
方法2,pc計數器為7,佔用3個slot,上一個方法佔用2個slots |
| 1 |
xxxxxxx |
方法1局部變數a |
| 0 |
1,2 , 0 |
方法1,pc計數器為1,佔用2個slot,上一個方法佔用0個slots |
quasar會在instrument階段織入stack/continuation邏輯,具體如下
調用Suspendable方法之前,調用Stack.pushMethod
在Suspendable方法開始, 調用Stack.nextMethodEntry
在Suspendable方法結束, 調用Stack.popMethod
下面我們依次看下這幾個方法的邏輯
Stack.pushMethod
儲存當前pc
儲存當前slots數量
將下一個frame設定成0L
Stack.nextMethodEntry
Stack.popMethod
Scheduler
scheduler顧名思義,是執行fiber代碼的地方,quasar裡用ForkJoinPool做為預設scheduler的線程池,
ForkJoinPool的優勢這裡不再強調,我們主要關注下Quasar中如何使用ForkJoinPool來調度fiber task
FiberForkJoinScheduler
quasar裡預設的fiber task scheduler,是JUC ForkJoinPool的wrapper類, ForkJoinPool具體細節參考ForkJoinPool
//主要屬性private final ForkJoinPool fjPool;//具體執行task的線程池private final FiberTimedScheduler timer;//監控fiber timeout的schedulerprivate final Set<FiberWorkerThread> activeThreads;//儲存fiber worker線程
FiberForkJoinTask
wrapper了fiber的ForkJoinTask
//主要屬性private final ForkJoinPool fjPool;private final Fiber<V> fiber;
FiberTimedScheduler
quasar自實現的timeout scheduler,用於fiber timeout的處理
FiberTimedScheduler預設的work queue為SingleConsumerNonblockingProducerDelayQueue,這是一個多生產單消費的無鎖隊列,內部是一個lock-free的基於skip list的優先順序鏈表,有興趣可以看下具體的實現,也值得一看
scheduler實現邏輯就比較簡單了,從SingleConsumerNonblockingProducerDelayQueue內部的優先順序隊列取資料,如果逾時了則調用fiber.unpark()
monitor
可以通過JMX監控fiber的運行狀態,work queue的堆積,fiber的數量,調度延遲等
comsat
comsat在quasar fiber基礎上提供了一些庫,使得跟fiber的整合更加容易,比如與servlet、springboot、drapwizard整合
https://github.com/puniverse/...
COMSAT (or Comsat) is a set of open source libraries that integrate Quasar with various web or enterprise technologies (like HTTP services and database access). With Comsat, you can write web applications that are scalable and performing and, at the same time, are simple to code and maintain.
Comsat is not a web framework. In fact, it does not add new APIs at all (with one exception, Web Actors, mentioned later). It provides implementation to popular (and often, standard) APIs like Servlet, JAX-RS, and JDBC, that can be used efficiently within Quasar fibers.
遇到的問題與解決
本人在應用中整合Fiber的時候遇到了不少問題,有些問題也反映了Quasar Fiber不是很完善,這裡列出來供大家參考下
Netty PoolByteBufAllocator 在 Fiber調用 導致Memory Leak
由於Quasar位元組碼的處理,ThreadLocal在fiber上調用,實際是"local to Fiber",而不是"local to Thread", 如果要繞過Fiber取underlying的ThreadLocal,需要用TrueThreadLocal
Netty的PoolByteBufAllocator$PoolThreadLocalCache用到了ThreadLocal,如果運行在fiber上,每次PoolThreadLocalCache.get()都會返回新的PoolThreadCache對象(因為每個請求起一個新的fiber處理,非WebActor模式)
而在PoolThreadCache的建構函式裡,會調用ThreadDeathWatcher.watch,把當前線程和PoolThreadLocalCache.get()返回的對象 add到全域ThreadDeathWatcher列表,以便相關線程停止的時候能釋放記憶體池
但是對於fiber就會有問題了, PoolThreadLocalCache.get()不斷的返回新的對象,然後add到ThreadDeathWatcher,而正真運行fiber的fiber-Fork/JoinPool的worker線程並不會終止,最終導致ThreadDeathWatcher裡watcher列表越來越多,導致memory leak,100% full gc time
問題總結:fiber上ThreadLocal返回的對象,逃逸到了全域對象裡,而netty只會在真正的線程(os thread)終止時釋放記憶體
解決辦法: 不使用Netty的對象池,或則mock netty代碼換成用TrueThreadLocal
啟動的時候會有[quasar] WARNING: Can’t determine super class of xxx
Quasar這個警示只會在啟動的時候出現,可以忽略,Quasar暫時沒有開關可以swith off
Fabio: As for the first warning, this is the relevant code and it basically means Quasar’s instrumentor couldn’t load a class’ superclass. This can happen because the class is not present or, more likely, because the classloader where that instrumentation code is running doesn’t allow to access it. Adding to that the strange warning about the agent not being running, I think the latter is most probably the case.
If the application runs you can just ignore the warnings (they should be printed only at instrumentation time, so bootstrap/warming stage) or if you can share a minimal project setup I could help having a deeper look to figure out what’s happening exactly.
https://groups.google.com/for...
FJP worker運行時如果有疑似blocking,會有WARNING hogging the CPU or blocking a thread
you can disable the warning by setting a system property with "-Dco.paralleluniverse.fibers.detectRunawayFibers=false”
獨立Tomcat + Quasar Agent FiberHttpServlet報NPE
[quasar] ERROR: Unable to instrument class co/paralleluniverse/fibers/servlet/FiberHttpServlet
From the full logs I see that my setup and your setup are different though: I’m using an embedded Tomcat while you’re running Tomcat as a standalone servlet container and using the agent for instrumentation. Unfortunately the agent doesn’t currently work with standalone Tomcat and you need to use the instrumenting loader.
官方推薦:獨立Tomcat + QuasarWebAppClassLoader 或者 內嵌容器 + Quasar Agent
WARNING: Uninstrumented methods on the call stack (marked with **)
Quasar不能修改第三方庫為@Suspend, 可以顯式的把相關的方法放入META-INF/suspendables
獨立Tomcat + QuasarWebAppClassLoader UnableToInstrumentException (harmless)
這是個Comsat的bug,但是無害,可以忽略
UnableToInstrumentException: Unable to instrument co/paralleluniverse/fibers/Fiber#onResume()V because of catch for SuspendExecution
google group comsat issues 25
Coroutine in Java - Quasar Fiber實現--轉載