LinkedHashMap<String, Object> json = new LinkedHashMap<>(); json.put("Name", config.getName()); json.put("Maximum Elements in Memory", config.getMaxEntriesLocalHeap()); json.put("Maximum Elements on Disk", config.getMaxBytesLocalDisk());
/** * Accessor */ public String getName() { return cacheConfiguration.getName(); }
private boolean isEternalValueConflictingWithTTIOrTTL(boolean newEternalValue, long newTTLValue, long newTTIValue) { boolean conflicting = false; if (newEternalValue && (newTTLValue != 0 || newTTIValue != 0)) { conflicting = true; } if (conflicting && !conflictingEternalValuesWarningLogged) { conflictingEternalValuesWarningLogged = true; LOG.warn("Cache '" + getName() + "' is set to eternal but also has TTI/TTL set. " + " To avoid this warning, clean up the config " + "removing conflicting values of eternal," + " TTI and TTL. Effective configuration for Cache '" + getName() + "' will be eternal='" + newEternalValue + "', timeToIdleSeconds='0', timeToLiveSeconds='0'."); } return conflicting; }
public void checkAlive(CacheConfiguration configuration) { final Status readStatus = status; if (readStatus != Status.STATUS_ALIVE) { throw new IllegalStateException("The " + configuration.getName() + " Cache is not alive (" + readStatus + ")"); } }
/** * Gets the cache name. */ @ContextAttribute("name") public final String getName() { return configuration.getName(); }
/** * {@inheritDoc} */ public void unregisterCacheWriter() { if (cacheWriterManagerInitFlag.get()) { throw new CacheException("Cache: " + configuration.getName() + " has its cache writer being unregistered " + "after it was already initialised."); } this.registeredCacheWriter = null; }
private void warnSizeOfPolicyConfiguration() { if (isTerracottaClustered() && getSizeOfPolicyConfiguration() != null) { LOG.warn("Terracotta clustered cache: " + getName() + " has a sizeOf policy configuration specificed. " + "SizeOfPolicyConfiguration is unsupported for Terracotta clustered caches."); } }
private void verifyLocalDisk(final long managerMaxBytesLocalDisk, final List<ConfigError> configErrors, final long totalOnDiskAssignedMemory) { if ((isMaxBytesLocalDiskPercentageSet() || getMaxBytesLocalDisk() > 0) && managerMaxBytesLocalDisk > 0 && managerMaxBytesLocalDisk - totalOnDiskAssignedMemory < 0) { configErrors.add(new ConfigError("Cache '" + getName() + "' over-allocates CacheManager's localOnDisk limit!")); } }
private void verifyLocalOffHeap(final long managerMaxBytesLocalOffHeap, final List<ConfigError> configErrors, final long totalOffHeapAssignedMemory) { if ((isMaxBytesLocalOffHeapPercentageSet() || getMaxBytesLocalOffHeap() > 0) && managerMaxBytesLocalOffHeap > 0 && managerMaxBytesLocalOffHeap - totalOffHeapAssignedMemory < 0) { configErrors.add(new ConfigError("Cache '" + getName() + "' over-allocates CacheManager's localOffHeap limit!")); } }
private void warnMaxEntriesForOverflowToOffHeap(final boolean register) { if (overflowToOffHeap != null && overflowToOffHeap && register) { if (getMaxEntriesLocalHeap() > 0 && getMaxEntriesLocalHeap() < MINIMUM_RECOMMENDED_IN_MEMORY) { LOG.warn("The " + getName() + " cache is configured for off-heap and has a maxEntriesLocalHeap/maxElementsInMemory of " + getMaxEntriesLocalHeap() + ". It is recommended to set maxEntriesLocalHeap/maxElementsInMemory to at least " + MINIMUM_RECOMMENDED_IN_MEMORY + " elements when using an off-heap store, otherwise performance " + "will be seriously degraded."); } } }
private void verifyLocalHeap(final long managerMaxBytesLocalHeap, final List<ConfigError> configErrors, final long totalOnHeapAssignedMemory) { if ((isMaxBytesLocalHeapPercentageSet() || getMaxBytesLocalHeap() > 0) && managerMaxBytesLocalHeap > 0 && managerMaxBytesLocalHeap - totalOnHeapAssignedMemory < 0) { configErrors.add(new ConfigError("Cache '" + getName() + "' over-allocates CacheManager's localOnHeap limit!")); } }
private void verifyClusteredCacheConfiguration(final Collection<ConfigError> errors) { if (!isTerracottaClustered()) { return; } if (getPinningConfiguration() != null && getPinningConfiguration().getStore() == PinningConfiguration.Store.INCACHE && getMaxElementsOnDisk() != 0) { errors.add(new CacheConfigError("maxElementsOnDisk may not be used on a pinned cache.", getName())); } if (maxEntriesLocalDiskExplicitlySet) { errors.add(new CacheConfigError("You can't set maxEntriesLocalDisk when clustering your cache with Terracotta, " + "local disks won't be used! To control elements going in the cache cluster wide, " + "use maxEntriesInCache instead", getName())); } if (maxBytesLocalDiskExplicitlySet) { errors.add(new CacheConfigError("You can't set maxBytesLocalDisk when clustering your cache with Terracotta", getName())); } if (maxElementsOnDisk != DEFAULT_MAX_ELEMENTS_ON_DISK) { errors.add(new CacheConfigError("maxElementsOnDisk is not used with clustered caches. Use maxEntriesInCache " + "to set maximum cache size.", getName())); } }
/** * Flushes all cache items from memory to the disk store, and from the DiskStore to disk. * * @throws IllegalStateException if the cache is not {@link Status#STATUS_ALIVE} */ public final synchronized void flush() throws IllegalStateException, CacheException { checkStatus(); try { compoundStore.flush(); } catch (IOException e) { throw new CacheException("Unable to flush cache: " + configuration.getName() + ". Initial cause was " + e.getMessage(), e); } }
@Override public CacheManager getObject() { CacheManager cacheManager = super.getObject(); Map<String, CacheConfiguration> cacheConfig = cacheManager.getConfiguration().getCacheConfigurations(); List<CacheConfiguration> cacheConfigurations = CachePropertiesUtil.getCacheConfigurations(); cacheConfigurations.stream() .filter(cc -> cacheConfig.get(cc.getName()) == null) .forEach(cc -> cacheManager.addCache(new Cache(cc))); return cacheManager; } }
private void warnMaxEntriesLocalHeap(final boolean register, CacheManager cacheManager) { if (getMaxEntriesLocalHeap() == 0 && register) { if (getMaxBytesLocalHeap() == 0 && (!cacheManager.getConfiguration().isMaxBytesLocalHeapSet())) { LOG.warn("Cache: " + getName() + " has a maxElementsInMemory of 0. This might lead to performance degradation or OutOfMemoryError at Terracotta client." + "From Ehcache 2.0 onwards this has been changed to mean a store" + " with no capacity limit. Set it to 1 if you want" + " no elements cached in memory"); } } }
private void registerCacheConfiguration(final CacheManager cacheManager) { Map<String, CacheConfiguration> configMap = cacheManager.getConfiguration().getCacheConfigurations(); if (!configMap.containsKey(getName())) { cacheManager.getConfiguration().addCache(this, false); } }
private void warnTieredSizing() { if (isOverflowToOffHeap()) { if (getMaxBytesLocalHeap() >= getMaxBytesLocalOffHeap() && getMaxBytesLocalOffHeap() != 0) { LOG.warn("Configuration problem for cache " + getName() + ": MaxBytesLocalHeap equal or greater than MaxBytesLocalOffHeap. " + "This will result in useless off heap storage."); } if (isOverflowToDisk()) { if (getMaxBytesLocalOffHeap() >= getMaxBytesLocalDisk() && getMaxBytesLocalDisk() != 0) { LOG.warn("Configuration problem for cache " + getName() + ": MaxBytesLocalOffHeap equal or greater than MaxBytesLocalDisk. " + "This will result in useless disk storage."); } } } if (isOverflowToDisk()) { if (getMaxEntriesLocalHeap() >= getMaxEntriesLocalDisk() && getMaxEntriesLocalDisk() != 0) { LOG.warn("Configuration problem for cache " + getName() + ": MaxEntriesLocalHeap equal or greater than MaxEntriesLocalDisk. " + "This will result in useless disk storage."); } } }
/** * Sets the bootstrap cache loader. * * @param bootstrapCacheLoader the loader to be used * @throws CacheException if this method is called after the cache is initialized */ public void setBootstrapCacheLoader(BootstrapCacheLoader bootstrapCacheLoader) throws CacheException { if (!cacheStatus.isUninitialized()) { throw new CacheException("A bootstrap cache loader can only be set before the cache is initialized. " + configuration.getName()); } BootstrapCacheLoader oldValue = getBootstrapCacheLoader(); this.bootstrapCacheLoader = bootstrapCacheLoader; firePropertyChange("BootstrapCacheLoader", oldValue, bootstrapCacheLoader); }
@Override public void maxBytesLocalHeapChanged(final long oldValue, final long newValue) { if (getMaxBytesLocalHeap() > 0 && cacheManager.getConfiguration().getCacheConfigurations().keySet().contains(getName()) && cacheManager.getConfiguration().isMaxBytesLocalHeapSet()) { long oldCacheManagerPoolSize = cacheManager.getOnHeapPool().getMaxSize(); long newPoolFreeSize = oldCacheManagerPoolSize + oldValue - newValue; //handle case of overallocation of cache //Only resize the cache manager pool cache pool resizing will be handled by cache if (newPoolFreeSize >= 0) { cacheManager.getOnHeapPool().setMaxSize(newPoolFreeSize); } else { maxBytesLocalHeap = oldValue; throw new InvalidConfigurationException("Cannot allocate heap size more " + "than the cache pool size reverting to previous size " + maxBytesLocalHeap); } } }
/** * Removes a cache from the known list * @param cacheConfiguration the cacheConfiguration to be removed */ public void removeCache(final CacheConfiguration cacheConfiguration) { if (cacheManager.getOnHeapPool() != null) { cacheManager.getOnHeapPool().setMaxSize(cacheManager.getOnHeapPool() .getMaxSize() + cacheConfiguration.getMaxBytesLocalHeap()); } if (cacheManager.getOnDiskPool() != null) { cacheManager.getOnDiskPool().setMaxSize(cacheManager.getOnDiskPool() .getMaxSize() + cacheConfiguration.getMaxBytesLocalDisk()); } getConfiguration().getCacheConfigurations().remove(cacheConfiguration.getName()); }