您的位置:首页 > 编程语言 > Java开发

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

2007-02-07 11:03 579 查看
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.  
    
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息