[Java] oscache 多线程 工作流程的分析

Oscache analysis and  possible suspend scenario    
    
    
Global Note:   
1Set the "cache.blocking" option to false, in oscache.properties. If not "cache.blocking" option, default is false.
Basicly, the analysis is based on the non-blocking mode, which is also the mode WebEx used.
2Convertion:
  Threads are named as T1, T2, T3, etc, and we assume T1 reach the Critcial Section before before other threads,
  T2 is 2th, and so on.
  Keys are marked as K1, K2, .., etc,  corresponding value are V1, V2, ..etc. If the value of K1 need be updated,
  the new values are V1a, V1b, .. and so on.


               
3let's call the cancelUpdate & putInCache  unlock methods.
  
  
Oscache getFromCache data flow for multi-threads   
  Several Threads T1, T2, … etc call getFromCache(K1) to retrieve the value from Cache.
L1If the Entry (K1, V1) exists , and isn't stale, Then all threads return the V1 directly , without any suspend operation.
L2If the Entry (K1, V) doesn't exists. Then
T1 throw NeedRefreshException (NRE), and all other threads  will be
suspended (T2, T3,) etc, until T1 call one of cancelUpdate & putInCache methods with K1.
    A). If T1 catch NRE and call putInCache(K1,V1),  then
ALL followed threads resume immediately
         and the getFromCache(K1) method in these thread return V1.
    B). If T1 catch NRE and call cancelUpdate(K1),  then
         one of other threads wake up, assume it's T2.  T2
resume and its getFromCache(K1) throw NRE.
         the other threads expect T1, T2 remain
suspended.  
         this is because oscache think T1 give up to update new value of cache, so let T2 throw NRE, expect T2 to
         update (put) new value.  
            If T2 call putInCache,  things go like A).
            If T2 call cancelUpdate,   things go like B). another thread is wake up. until some Thread call putInCache.
    C). If T1 catch NRE but call none of both methods,  then all threads followed are
suspended for ever.
        
This is the reason of Production Thread Lock.
   


L3If the Entry (K1, V1) exists and is stale.(which is caused by time expiration, cron expiration, flush ,etc).
then,
T1 throw NeedRefreshException (NRE), and  other threads T2, T3, etc, will return the stale value V1 directly.
    A). T1 call putInCache(K1, V1a),  then new getFromCache call will return V1a.
    B). T1 call cancelUpdate(K1, V1a), then first one of new getFromCache(K1) call  throw NRE.
    C). T1 call none of both methods,   any getFromCache(K1) will return stale value V1. and a warning log recorded.

You may find none of A).B).C).of L3 cause thread block,  because  current cache is in non-blocking mode.
In blocking mode, the other threads will wait for ever  until former thread putInCache  new value. 
the logic is the same as L2. 
  
  
Possible Context  which may cause Thread Lock.   
1Refer to C) of L2,   if we can't make sure  to call one of cancelUpdate & putInCache methods after
the first getFromCache call, the block issue appear definitively.   And in non-blocking mode, only this case, i.e the first update of non-existing entry,  can cause the block.
2If in blocking mode,  the thread lock occurs too, if  both methods isn't called after getFromCache.  
    
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值