@aloxc
2017-12-05T06:19:47.000000Z
字数 3817
阅读 581
一起学
源码分析
ignite
cache
分析代码IgnitionEx中有如下一些代码
public void initializeDefaultCacheConfiguration(IgniteConfiguration cfg) throws IgniteCheckedException {
List<CacheConfiguration> cacheCfgs = new ArrayList<>();
cacheCfgs.add(marshallerSystemCache());
cacheCfgs.add(utilitySystemCache());
if (IgniteComponentType.HADOOP.inClassPath())
cacheCfgs.add(CU.hadoopSystemCache());
cacheCfgs.add(atomicsSystemCache(cfg.getAtomicConfiguration()));
CacheConfiguration[] userCaches = cfg.getCacheConfiguration();
if (userCaches != null && userCaches.length > 0) {
if (!IgniteUtils.discoOrdered(cfg.getDiscoverySpi()) && !IgniteUtils.relaxDiscoveryOrdered())
throw new IgniteCheckedException("Discovery SPI implementation does not support node ordering and " +
"cannot be used with cache (use SPI with @GridDiscoverySpiOrderSupport annotation, " +
"like TcpDiscoverySpi)");
for (CacheConfiguration ccfg : userCaches) {
if (GridCacheUtils.isAtomicsCache(ccfg.getName()))
throw new IgniteCheckedException("Cache name cannot be \"" + GridCacheUtils.ATOMICS_CACHE_NAME +
"\" because it is reserved for internal purposes.");
if (GridCacheUtils.isUtilityCache(ccfg.getName()))
throw new IgniteCheckedException("Cache name cannot be \"" + GridCacheUtils.UTILITY_CACHE_NAME +
"\" because it is reserved for internal purposes.");
if (GridCacheUtils.isMarshallerCache(ccfg.getName()))
throw new IgniteCheckedException("Cache name cannot be \"" + GridCacheUtils.MARSH_CACHE_NAME +
"\" because it is reserved for internal purposes.");
cacheCfgs.add(ccfg);
}
}
cfg.setCacheConfiguration(cacheCfgs.toArray(new CacheConfiguration[cacheCfgs.size()]));
assert cfg.getCacheConfiguration() != null;
}
这个方法ignite服务启动的时候要初始化一些系统用的缓存,这些缓存明付出在GridCacheUtils中有定义,
/** System cache name. */
public static final String UTILITY_CACHE_NAME = "ignite-sys-cache";
/** Atomics system cache name. */
public static final String ATOMICS_CACHE_NAME = "ignite-atomics-sys-cache";
/** Marshaller system cache name. */
public static final String MARSH_CACHE_NAME = "ignite-marshaller-sys-cache";
这些缓存名称是预定义的,不能作为我们应用的缓存名称。
进一步分析下面三个方法,发现三个关键的系统缓存的缓存模式都是REPLICATED,也就是说这三个系统缓存都是复制缓存,每个ignite节点上的数据都是一样的,这样做就不用每次涉及这三个系统缓存的数据读写都只需要从本节点存取即可
private static CacheConfiguration marshallerSystemCache() {
CacheConfiguration cache = new CacheConfiguration();
cache.setName(GridCacheUtils.MARSH_CACHE_NAME);
cache.setCacheMode(REPLICATED);
cache.setAtomicityMode(ATOMIC);
cache.setSwapEnabled(false);
cache.setRebalanceMode(SYNC);
cache.setWriteSynchronizationMode(FULL_SYNC);
cache.setAffinity(new RendezvousAffinityFunction(false, 20));
cache.setNodeFilter(CacheConfiguration.ALL_NODES);
cache.setStartSize(300);
cache.setRebalanceOrder(-2);//Prior to other system caches.
cache.setCopyOnRead(false);
return cache;
}
/**
* Creates utility system cache configuration.
*
* @return Utility system cache configuration.
*/
private static CacheConfiguration utilitySystemCache() {
CacheConfiguration cache = new CacheConfiguration();
cache.setName(GridCacheUtils.UTILITY_CACHE_NAME);
cache.setCacheMode(REPLICATED);
cache.setAtomicityMode(TRANSACTIONAL);
cache.setSwapEnabled(false);
cache.setRebalanceMode(SYNC);
cache.setWriteSynchronizationMode(FULL_SYNC);
cache.setAffinity(new RendezvousAffinityFunction(false, 100));
cache.setNodeFilter(CacheConfiguration.ALL_NODES);
cache.setRebalanceOrder(-2); //Prior to user caches.
cache.setCopyOnRead(false);
return cache;
}
/**
* Creates cache configuration for atomic data structures.
*
* @param cfg Atomic configuration.
* @return Cache configuration for atomic data structures.
*/
private static CacheConfiguration atomicsSystemCache(AtomicConfiguration cfg) {
CacheConfiguration ccfg = new CacheConfiguration();
ccfg.setName(GridCacheUtils.ATOMICS_CACHE_NAME);
ccfg.setAtomicityMode(TRANSACTIONAL);
ccfg.setSwapEnabled(false);
ccfg.setRebalanceMode(SYNC);
ccfg.setWriteSynchronizationMode(FULL_SYNC);
ccfg.setCacheMode(cfg.getCacheMode());
ccfg.setNodeFilter(CacheConfiguration.ALL_NODES);
ccfg.setRebalanceOrder(-1); //Prior to user caches.
if (cfg.getCacheMode() == PARTITIONED)
ccfg.setBackups(cfg.getBackups());
return ccfg;
}