[关闭]
@aloxc 2017-12-05T06:19:47.000000Z 字数 3817 阅读 581

我们一起学ignite之一些系统缓存

一起学 源码分析 ignite cache


分析代码IgnitionEx中有如下一些代码

  1. public void initializeDefaultCacheConfiguration(IgniteConfiguration cfg) throws IgniteCheckedException {
  2. List<CacheConfiguration> cacheCfgs = new ArrayList<>();
  3. cacheCfgs.add(marshallerSystemCache());
  4. cacheCfgs.add(utilitySystemCache());
  5. if (IgniteComponentType.HADOOP.inClassPath())
  6. cacheCfgs.add(CU.hadoopSystemCache());
  7. cacheCfgs.add(atomicsSystemCache(cfg.getAtomicConfiguration()));
  8. CacheConfiguration[] userCaches = cfg.getCacheConfiguration();
  9. if (userCaches != null && userCaches.length > 0) {
  10. if (!IgniteUtils.discoOrdered(cfg.getDiscoverySpi()) && !IgniteUtils.relaxDiscoveryOrdered())
  11. throw new IgniteCheckedException("Discovery SPI implementation does not support node ordering and " +
  12. "cannot be used with cache (use SPI with @GridDiscoverySpiOrderSupport annotation, " +
  13. "like TcpDiscoverySpi)");
  14. for (CacheConfiguration ccfg : userCaches) {
  15. if (GridCacheUtils.isAtomicsCache(ccfg.getName()))
  16. throw new IgniteCheckedException("Cache name cannot be \"" + GridCacheUtils.ATOMICS_CACHE_NAME +
  17. "\" because it is reserved for internal purposes.");
  18. if (GridCacheUtils.isUtilityCache(ccfg.getName()))
  19. throw new IgniteCheckedException("Cache name cannot be \"" + GridCacheUtils.UTILITY_CACHE_NAME +
  20. "\" because it is reserved for internal purposes.");
  21. if (GridCacheUtils.isMarshallerCache(ccfg.getName()))
  22. throw new IgniteCheckedException("Cache name cannot be \"" + GridCacheUtils.MARSH_CACHE_NAME +
  23. "\" because it is reserved for internal purposes.");
  24. cacheCfgs.add(ccfg);
  25. }
  26. }
  27. cfg.setCacheConfiguration(cacheCfgs.toArray(new CacheConfiguration[cacheCfgs.size()]));
  28. assert cfg.getCacheConfiguration() != null;
  29. }

这个方法ignite服务启动的时候要初始化一些系统用的缓存,这些缓存明付出在GridCacheUtils中有定义,

  1. /** System cache name. */
  2. public static final String UTILITY_CACHE_NAME = "ignite-sys-cache";
  3. /** Atomics system cache name. */
  4. public static final String ATOMICS_CACHE_NAME = "ignite-atomics-sys-cache";
  5. /** Marshaller system cache name. */
  6. public static final String MARSH_CACHE_NAME = "ignite-marshaller-sys-cache";

这些缓存名称是预定义的,不能作为我们应用的缓存名称。

进一步分析下面三个方法,发现三个关键的系统缓存的缓存模式都是REPLICATED,也就是说这三个系统缓存都是复制缓存,每个ignite节点上的数据都是一样的,这样做就不用每次涉及这三个系统缓存的数据读写都只需要从本节点存取即可

  1. private static CacheConfiguration marshallerSystemCache() {
  2. CacheConfiguration cache = new CacheConfiguration();
  3. cache.setName(GridCacheUtils.MARSH_CACHE_NAME);
  4. cache.setCacheMode(REPLICATED);
  5. cache.setAtomicityMode(ATOMIC);
  6. cache.setSwapEnabled(false);
  7. cache.setRebalanceMode(SYNC);
  8. cache.setWriteSynchronizationMode(FULL_SYNC);
  9. cache.setAffinity(new RendezvousAffinityFunction(false, 20));
  10. cache.setNodeFilter(CacheConfiguration.ALL_NODES);
  11. cache.setStartSize(300);
  12. cache.setRebalanceOrder(-2);//Prior to other system caches.
  13. cache.setCopyOnRead(false);
  14. return cache;
  15. }
  16. /**
  17. * Creates utility system cache configuration.
  18. *
  19. * @return Utility system cache configuration.
  20. */
  21. private static CacheConfiguration utilitySystemCache() {
  22. CacheConfiguration cache = new CacheConfiguration();
  23. cache.setName(GridCacheUtils.UTILITY_CACHE_NAME);
  24. cache.setCacheMode(REPLICATED);
  25. cache.setAtomicityMode(TRANSACTIONAL);
  26. cache.setSwapEnabled(false);
  27. cache.setRebalanceMode(SYNC);
  28. cache.setWriteSynchronizationMode(FULL_SYNC);
  29. cache.setAffinity(new RendezvousAffinityFunction(false, 100));
  30. cache.setNodeFilter(CacheConfiguration.ALL_NODES);
  31. cache.setRebalanceOrder(-2); //Prior to user caches.
  32. cache.setCopyOnRead(false);
  33. return cache;
  34. }
  35. /**
  36. * Creates cache configuration for atomic data structures.
  37. *
  38. * @param cfg Atomic configuration.
  39. * @return Cache configuration for atomic data structures.
  40. */
  41. private static CacheConfiguration atomicsSystemCache(AtomicConfiguration cfg) {
  42. CacheConfiguration ccfg = new CacheConfiguration();
  43. ccfg.setName(GridCacheUtils.ATOMICS_CACHE_NAME);
  44. ccfg.setAtomicityMode(TRANSACTIONAL);
  45. ccfg.setSwapEnabled(false);
  46. ccfg.setRebalanceMode(SYNC);
  47. ccfg.setWriteSynchronizationMode(FULL_SYNC);
  48. ccfg.setCacheMode(cfg.getCacheMode());
  49. ccfg.setNodeFilter(CacheConfiguration.ALL_NODES);
  50. ccfg.setRebalanceOrder(-1); //Prior to user caches.
  51. if (cfg.getCacheMode() == PARTITIONED)
  52. ccfg.setBackups(cfg.getBackups());
  53. return ccfg;
  54. }

x

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注