JBoss series twenty-three: JBossCache architecture

abstract

This section deeper into the Introduction of the JBoss Cache architecture, it is applicable to the use of higher desired level cache function, enhanced or expanded cache, write compiled plug or a bottom layer solution running machine the use of the system.

JBossCache in the data structure

JBossCache collection Composition Examples by the Node tree-type structure of the tissue. Each of which contains a Node saved to the cache data object table. Note that, a structure which is a kind of mathematical trees, and non-graphics; are each one and only Node We have a parent node, and the root node is represented by an invariant Fqn.ROOT fully qualified name table.


On the surface of the table in FIG., Each block represents both a JVM. You can see the two cache located in a different JVM, the data is copied to each other. Wherein any of a cache in any of the modifications are copied to a cache Further in . Since then, the cluster system can have multiple cache. According to the transactional root set, or copying the end of transaction after each modification occurs (when submitted) into the line. When a new cache is created, it can obtain a two existing cache at startup content.

SPI Interface

In addition to Cache and Node interfaces, JBossCache also open more powerful and NodeSPI of CacheSPI interface, they provide for internal JBossCache more and more control. These interfaces are not used for ordinary uses, which are applicable to expansion and increase of JBossCache, ed write custom interceptor (Interceptor) or a class loader (the CacheLoader) instance.


CacheSPI interface can not be created, but it relies on these interfaces setCache (CacheSPI cache) method injected into Interceptor and CacheLoader achieving in. CacheSPI inherits Cache, as to the basic API functions are all available. Similar to, NodeSPI interface also can not be created. Instead, it is performed through the operation to obtain the CacheSPI. For example, Cache.getRoot (): Node is covered as CacheSPI.getRoot (): NodeSPI.

      Note that both natural interface inheritance is not able to guarantee to maintain the contracts before, we not recommended direct conversion Cache or Node to its corresponding SPI interface, which is not good for practices. From a further aspect to say the outer, open public API is used to maintain guaranteed to be.

Method call on the node

既​然​缓​存​基​本​上​是​一​个​节​点​的​集​合​,当​作​为​整​体​或​单​个​节​点​调​用​缓​存​上​的​操​作​时​,集群​、​持​久​化​、​逐​出​等​方面​都​需​要​应​用​到​这​些​节​点​上​。​要​以​一​种​清​洁​、​模​块​化​和​可​扩​展​的​方​式​实​现​这​一​点​,我​们​使​用​了​一​种​拦​截​器​链。​这​个​链​由​一​系​列​的​拦​截​器​组​成​,每​个​都​添​加​了​一​种​方​面​或​特​定​的​功​能​。​当​缓​存​创​建​时​,这​个​链​将​基​于​所​用的​配​置​构​建​。

请​注​意​,NodeSPI 提​供​一​些​直​接​在​节​点​上​操​作​而​无​需​通​过​拦​截​器​链​的​方​法​(如​ xxxDirect() 方​法​族​)。插​件​作​者​应​该​注​意​到​使​用​这​样​的​方​法​将​影​响​到​缓​存​可​能​需​要​应​用​的​方​面​,如​锁​、​复​制​等​。​为​简​便​起​见​,请​不要​使​用​这​些​方​法​,除​非​你真的​知​道​自​己​在​干​什​么​。

拦截器

JBossCache 基​本​上​是​一​个​核​心​的​数​据​结​构​ - 对​ DataContainer 的​实​现​ - 方​面​和​功​能​在​此​数​据​结​构​之​上用​拦​截​器​实​现​。​CommandInterceptor 是​一​个​抽​象​类​,拦​截​器​实​现​继​承​了​它​。CommandInterceptor 实​现​了​ Visitor 接​口​,所​以​它​能​够​以​一​种​强​类​型​的​方​式​修​改​命​令​。​下​节​将​介​绍关​于​ Visitor 和​ Command 的​更​多​内​容​。拦​截​器​实​现​在​ InterceptorChain 类​里​被​链​接​在​一​起​,它​在​整​个​链​里​分​发​一​个​命​令​。CallInterceptor 是​一​个​特​殊​的​拦​截​器​,它​总​是​位​于​链​的​末​端​来​调​用​通​过​ process() 方​法​传​递​的​命​令。JBossCache 附​带​几​个​拦​截​器​,代​表​着​不​同​的​行​为​方​面​,例​如​:

  • TxInterceptor - 查找正在进行的事务并注册事务管理者以参与同步事件
  • ReplicationInterceptor - 用 RpcManager 类在群集里复制状态
  • CacheLoaderInterceptor - 如果内存里没有请求的数据时,从持久性存储加载数据

针​对​你​的​缓​存​实​例​配​置​的​拦​截​器​链​可​以​通​过​调​用​ CacheSPI.getInterceptorChain() 获​得​和​检​查​,这个​方​法​返​回​一​个​已​排​序​的​拦​截​器​List,它​是​以​命​令​将​遇​到​的​顺​序​进​行​排​序​的​。

      当然我们也可以编​写​自​定​义​的​拦​截​器,通​过​继​承​CommandInterceptor 并​基​于​你​感​兴​趣​拦​截​的​命​令​来​覆​盖​相​关​的​ visitXXX() 方​法​,你​可​以编​写​自​定​义​的​拦​截​器​以​添​加​特​殊​的​方​面​或​功​能​。​你​也​可​以​继​承​一​些​其​他​的​抽​象​拦​截​器​,如PrePostProcessingCommandInterceptor和​SkipCheckChainedInterceptor。​关​于​其​他​功能​的​细​节​,请​参​考​相​关​的​ Javadoc。自​定​义​拦​截​器​需​要​使​用​Cache.addInterceptor() 方​法​添​加​到​拦​截​器​链​中​。JBossCache 也​支​持​通​过​XML 配置自​定​义​拦​截​器。

Command 和 Visitor

JBossCache 在​内​部​使​用​一​种​command/visitor 模​式​来​执​行​ API 类​。​每​当​在​缓​存​接​口​上​调​用​一​个​方​法​,实​现了​ Cache 接​口​的​CacheInvocationDelegate 将​创​建​一​个​ VisitableCommand 实​例​并​将​这​个​命​令分​发​到​拦​截​器​链​里​。​而​实​现​了​Visitor 接​口​的​拦​截​器​能​够​处​理​它​们​所​感​兴​趣​的​ VisitableCommand 并添​加​行​为​到​这​个​命​令​里​。

      每​个​命​令​都​包​含​了​正​在​执​行​的​命​令​的​全​部​知​识​,如​所​使​用​的​参​数​和​封​装​在​process() 方​法​里​的​处​理​行​为​。例​如​,当​调​用​ Cache.removeNode() 时​,RemoveNodeCommand 将​被​创​建​并​传​递​到​拦​截​器​链​里​,而 RemoveNodeCommand.process() 知​道​如​何​从​数​据​结​构​里​删​除​节​点​。

     除​了​可​被​访​问​以​外​,命​令​也​是​可​以​复​制​的​。​JBossCache marshaller 知​道​如​何​高​效​地​将​命​令​编​码​并​使​用​内​部的​基​于​ JGroups 的​RPC 机​制​在​远​程​缓​存​实​例​上​调​用​它​们​。

InvocationContexts

InvocationContext保​留​着​单​次​调​用​期​间​的​中​间​状​态​,且​由​位​于​拦​截​器​链​前​端​的 InvocationContextInterceptor 设​置​和​销​毁​。InvocationContext,顾​名​思​义​,持​有​和​单​次​方​法​调​用​相​关​联​的​上​下​文​信​息​。​上​下​文​信​息​包​含​相​关​联​的 javax.transaction.Transaction 或 org.jboss.cache.transaction.GlobalTransaction、​方​法​调​用​起​始​者(InvocationContext.isOriginLocal())、​以​及被​锁​定​的​节​点​相​关​的​信​息​等​。InvocationContext 可​以​通​过​调​用​ Cache.getInvocationContext() 获​取​。

用于子系统的管理者

有​些​方​面​和​功​能​是​由​多​个​拦​截​器​共​享​的​。​其​中​一​些​已​经​封​装​成​管​理​者​,由​不​同​的​拦​截​器​所​使​用​,且​通​过 CacheSPI 接​口​被​访​问​。

  • RpcManager - 这个类负责通过 JGroups通道的对远程缓存的 RPC 调用,它封装了所使用的 JGroups 通道
  • BuddyManager - 这个类管理 Buddy 组并调用组远程调用以将缓存群集组织为更小的子组
  • CacheLoaderManager - 设立和配置缓存加载器。这个类在委托类里包裹了单独的 CacheLoader 实例, 如 SingletonStoreCacheLoader 或 AsyncCacheLoader,或者用 ChainingCacheLoader 在链里添加 CacheLoader

编码(Marshalling) 和线格式(Wire Format)

JBossCache 的​早​期​版​本​只​是​简​单​地​通​过​ ObjectOutputStream 在​复​制​时​将​缓​存​数​据​写​入​到​网​络​里​。​在JBoss Cache 1.x.x 系​列​的​几​个​版​本​里​,这​种​方​法​逐​渐​被​取​消​而​采​用​了​一​种​更​为​成​熟​的​编​码​框​架​。​在​ JBoss Cache 2.x.x 系​列​里​,这​是​官​方​支​持​和​推​荐​的​写​入​对​象​到​数​据​流​里​的​唯​一​机​制。


Marshaller接​口​从​ JGroups 继​承​了​RpcDispatcher.Marshaller。​这​个​接​口​有​两​个​主​要​的​实​现​- 委​托​的​ VersionAwareMarshaller 和​具​体​的​ CacheMarshaller300。 通​过​调​用​ CacheSPI.getMarshaller() 可​获​得​ marshaller,缺​省​是​ VersionAwareMarshaller。用​户​也​可​以​通​过​实​现​ Marshaller 接​口​或​继​承​ AbstractMarshaller 类​编​写​自​己​的​ marshaller,并​通过​Configuration.setMarshallerClass() setter 将​其​添​加​到​配​置​中​。

      VersionAwareMarshaller, 顾​名​思​义​,这​个​ marshaller 在​写​入​时​添​加​版​本​ short 到​任​何​流​里​,启​用​相​似​的VersionAwareMarshaller 实​例​来​读​取​版​本​short 并​知​道​哪​个​专​有​的​ marshaller 实​现​来​委​托​调​用​。​例​如, CacheMarshaller200 是​用​于​ JBoss Cache 2.0.x 的​ marshaller。​JBoss Cache 3.0.x 附​带​具​有​改​进的​ wire 协​议​的​CacheMarshaller300。​使​用​ VersionAwareMarshaller 帮​助​实​现​次​要​版​本​间​的 wire 协​议​的​兼​容​性​,但​仍​然​让​我​们​可​以​灵​活​地​调​整​和​改​进​次​要​和​micro 版​本​间​的​ wire 协​议​。

类​加​载​和​区

When used in the server should be used when the cluster state, the deployed Applications tends to be designed to there are examples of objects put into the complex required to be made in the cache (or objects are HttpSession). To be made by each deployment server ClassLoader instance allocated independent of Applications in but by the ClassLoader application servers to JBoss Cache libraries referenced, it is very often see.

      To successfully loaded from the class pair object encoding and decoding, we use the one called Almost region (region) is read. A zone is a part of the cache, which share a well-used class loader (which he has regions Uses - see next eviction (eviction) introduction).

      Area is through the call Cache.getRegion (Fqn fqn, booleancreateIfNotExists) method and return the Region to achieve the interfaces created . Obtained in the region, the region class loader may set or cancel the setting, the region may activate and to cancel activation. In the default case, a region of activity, unless InactiveOnStartup configuration attribute is set to true.


Reproduced in: https: //my.oschina.net/iwuyang/blog/197193

Guess you like

Origin blog.csdn.net/weixin_33898876/article/details/91897329