Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cdm/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<relativePath>..</relativePath>
</parent>
<properties>
<netcdfversion>5.10.0-SNAPSHOT</netcdfversion>
<netcdfversion>5.10.0</netcdfversion>
</properties>
<artifactId>edal-cdm</artifactId>
<packaging>jar</packaging>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,9 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;
import net.sf.ehcache.config.CacheConfiguration;
import net.sf.ehcache.config.PersistenceConfiguration;
import net.sf.ehcache.config.CacheConfiguration.TransactionalMode;
import net.sf.ehcache.config.PersistenceConfiguration.Strategy;
import net.sf.ehcache.store.MemoryStoreEvictionPolicy;
import org.ehcache.Cache;
import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.ResourcePoolsBuilder;
import uk.ac.rdg.resc.edal.cache.EdalCache;
import uk.ac.rdg.resc.edal.dataset.DataSource;
import uk.ac.rdg.resc.edal.dataset.vtk.HydromodelVtkDatasetFactory.TimestepInfo;
Expand All @@ -68,9 +64,10 @@ protected Number[] getData1D(TimestepInfo timestepInfo, String variableId) throw
*/
Number[] data1d;
DataCacheKey key = new DataCacheKey(timestepInfo.file, variableId);
if (vtkGridDatasetCache.isKeyInCache(key)) {
Number[] cached = vtkGridDatasetCache.get(key);
if (cached != null) {
log.debug("Getting timestep data from cache");
data1d = (Number[]) vtkGridDatasetCache.get(key).getObjectValue();
data1d = cached;
} else {
log.debug("Data not in cache, reading from VTK file: "
+ timestepInfo.file.getAbsolutePath());
Expand Down Expand Up @@ -138,7 +135,7 @@ protected Number[] getData1D(TimestepInfo timestepInfo, String variableId) throw
if (dataStr != null) {
data1d = VtkUtils.parseDataString(dataStr, dataFormat, dataType,
timestepInfo.fillValues);
vtkGridDatasetCache.put(new Element(key, data1d));
vtkGridDatasetCache.put(key, data1d);
} else {
throw new DataReadingException("No data for variable " + variableId
+ " found in file: " + timestepInfo.file);
Expand All @@ -164,27 +161,19 @@ public void close() throws DataReadingException {
*/
private static final String CACHE_NAME = "vtkDataCache";
private static final int MAX_HEAP_ENTRIES = 50;
private static final MemoryStoreEvictionPolicy EVICTION_POLICY = MemoryStoreEvictionPolicy.LFU;
private static final Strategy PERSISTENCE_STRATEGY = Strategy.NONE;
private static final TransactionalMode TRANSACTIONAL_MODE = TransactionalMode.OFF;
private static Cache vtkGridDatasetCache = null;
private static final Cache<DataCacheKey, Number[]> vtkGridDatasetCache;

static {
if (EdalCache.cacheManager.cacheExists(CACHE_NAME) == false) {
/*
* Configure cache
*/
log.debug(
"Creating vtkDataCache, with maximum " + MAX_HEAP_ENTRIES + " entries");
CacheConfiguration config = new CacheConfiguration(CACHE_NAME, MAX_HEAP_ENTRIES)
.eternal(true).memoryStoreEvictionPolicy(EVICTION_POLICY)
.persistence(new PersistenceConfiguration().strategy(PERSISTENCE_STRATEGY))
.transactionalMode(TRANSACTIONAL_MODE);
vtkGridDatasetCache = new Cache(config);
EdalCache.cacheManager.addCache(vtkGridDatasetCache);
Cache<DataCacheKey, Number[]> existing = EdalCache.cacheManager.getCache(CACHE_NAME,
DataCacheKey.class, Number[].class);
if (existing == null) {
log.debug("Creating vtkDataCache, with maximum " + MAX_HEAP_ENTRIES + " entries");
vtkGridDatasetCache = EdalCache.cacheManager.createCache(CACHE_NAME,
CacheConfigurationBuilder.newCacheConfigurationBuilder(DataCacheKey.class,
Number[].class, ResourcePoolsBuilder.heap(MAX_HEAP_ENTRIES)));
} else {
log.debug("Loading existing vtkGridDatasetCache");
vtkGridDatasetCache = EdalCache.cacheManager.getCache(CACHE_NAME);
vtkGridDatasetCache = existing;
}
}

Expand All @@ -204,7 +193,7 @@ public int hashCode() {
int result = 1;
result = prime * result + ((file == null) ? 0 : file.hashCode());
result = prime * result + ((varId == null) ? 0 : varId.hashCode());
return result;
return EdalCache.murmur3Finalize(result);
}

@Override
Expand Down
11 changes: 9 additions & 2 deletions common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,16 @@
<version>2.2.220</version>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.10.6</version>
<version>3.12.0</version>
<classifier>jakarta</classifier>
<exclusions>
<exclusion>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<repositories>
Expand Down
84 changes: 51 additions & 33 deletions common/src/main/java/uk/ac/rdg/resc/edal/cache/EdalCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,40 +28,58 @@

package uk.ac.rdg.resc.edal.cache;

import net.sf.ehcache.CacheManager;
import net.sf.ehcache.config.Configuration;
import net.sf.ehcache.config.SizeOfPolicyConfiguration;
import org.ehcache.CacheManager;
import org.ehcache.config.builders.CacheManagerBuilder;

/**
* Holds the singleton Ehcache 3.x {@link CacheManager} used by EDAL.
*
* <p>
* In Ehcache 2.x the {@code CacheManager} could be configured with a name and a
* global "size of policy" (so the cache could be sized by bytes-on-heap rather
* than entry count). Ehcache 3.x has a different model: caches are typed
* ({@code Cache<K,V>}), and any optional XML configuration is loaded directly
* into a {@link CacheManager} via
* {@link org.ehcache.xml.XmlConfiguration}. Therefore this class simply
* exposes a singleton {@link CacheManager} which can be augmented at runtime by
* the various EDAL components (Domain2DMapper, HorizontalMesh4dDataset,
* OnDemandVtkDataSource, DataCatalogue, ...).
*
* <p>
* The cached objects are typically large (gridded map features with
* 256*256 ~= 65,000 values, or collections of point features containing tens
* of thousands of features). Cache sizing for these objects is therefore
* handled per-cache, in units of MB on heap, rather than by entry count.
*/
public class EdalCache {
private static final String CACHE_MANAGER = "EDAL-CacheManager";
private static final int MAX_CACHE_DEPTH = 4_000_000;

/*
* We are using an in-memory cache with a configured memory size (as opposed
* to a configured number of items in memory). This has the advantage that
* we will get a hard limit on the amount of memory the cache consumes. The
* disadvantage is that the size of each object needs to be calculated prior
* to inserting it into the cache.
*
* The maxDepth property specified the maximum number of object references
* to count before a warning is given.
*
* Now, we are generally caching 2 things:
*
* 1) Gridded map features which will generally have 256*256 ~= 65,000
* values, but could easily be bigger
*
* 2) Collections of point features. A year's worth of EN3 data could
* typically contain >15,000 features, each with a number of properties
*
* These can need to count a very large number of object references.
* However, this calculation is actually pretty quick. Setting the max depth
* to 4,000,000 seems to suppress the vast majority of warnings, and doesn't
* impact performance noticeably.
*
* Cache configuration specified in resources/ehcache.xml
/**
* The shared, application-wide Ehcache 3 {@link CacheManager}. Caches are
* registered against this manager by the various EDAL modules. It is built
* (and initialised) eagerly so that callers can rely on it being usable
* immediately.
*/
public static final CacheManager cacheManager = CacheManager
.newInstance(new Configuration().name(CACHE_MANAGER)
.sizeOfPolicy(new SizeOfPolicyConfiguration().maxDepth(MAX_CACHE_DEPTH)));
public static final CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
.build(true);

/**
* The finalization step from MurmurHash3, used to scramble an integer hash
* so that the resulting bits are well distributed.
*
* <p>
* This is the same finalizer used by Spring's {@code SimpleKey} (see
* <a href="https://github.com/spring-projects/spring-framework/blob/c74f897facf830471e2524252a4f46ded05be895/spring-context/src/main/java/org/springframework/cache/interceptor/SimpleKey.java#L92-L94">SimpleKey.java</a>).
* See <a href="https://github.com/Reading-eScience-Centre/edal-java/issues/171#issuecomment-2708650091">
* issue #171, comment 2708650091</a> for the discussion that motivated this.
*
* @param hash the input combined hash code
* @return a strongly mixed hash code derived from {@code hash}
*/
public static int murmur3Finalize(int hash) {
hash ^= (hash >>> 16);
hash *= 0x85ebca6b;
hash ^= (hash >>> 13);
hash *= 0xc2b2ae35;
hash ^= (hash >>> 16);
return hash;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,9 @@

import java.util.List;

import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;
import net.sf.ehcache.config.CacheConfiguration;
import net.sf.ehcache.config.CacheConfiguration.TransactionalMode;
import net.sf.ehcache.config.PersistenceConfiguration;
import net.sf.ehcache.config.PersistenceConfiguration.Strategy;
import net.sf.ehcache.store.MemoryStoreEvictionPolicy;
import org.ehcache.Cache;
import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.ResourcePoolsBuilder;
import uk.ac.rdg.resc.edal.cache.EdalCache;
import uk.ac.rdg.resc.edal.grid.GridCell2D;
import uk.ac.rdg.resc.edal.grid.HorizontalGrid;
Expand Down Expand Up @@ -114,8 +110,9 @@ public int getTargetYSize() {
*/
public static Domain2DMapper forGrid(HorizontalGrid sourceGrid, final HorizontalGrid targetGrid) {
Domain2DMapperCacheKey key = new Domain2DMapperCacheKey(sourceGrid, targetGrid);
if (domainMapperCache.isKeyInCache(key)) {
return (Domain2DMapper) domainMapperCache.get(key).getObjectValue();
Domain2DMapper cached = domainMapperCache.get(key);
if (cached != null) {
return cached;
}
Domain2DMapper ret;
if (sourceGrid instanceof RectilinearGrid
Expand All @@ -140,7 +137,7 @@ public static Domain2DMapper forGrid(HorizontalGrid sourceGrid, final Horizontal
*/
ret = forGeneralGrids(sourceGrid, targetGrid);
}
domainMapperCache.put(new Element(key, ret));
domainMapperCache.put(key, ret);
return ret;
}

Expand Down Expand Up @@ -225,27 +222,20 @@ private static Domain2DMapper forGeneralGrids(HorizontalGrid sourceGrid,
*/
private static final String CACHE_NAME = "domainMapperCache";
private static final int MAX_HEAP_ENTRIES = 100;
private static final MemoryStoreEvictionPolicy EVICTION_POLICY = MemoryStoreEvictionPolicy.LFU;
private static final Strategy PERSISTENCE_STRATEGY = Strategy.NONE;
private static final TransactionalMode TRANSACTIONAL_MODE = TransactionalMode.OFF;
private static Cache domainMapperCache;
private static final Cache<Domain2DMapperCacheKey, Domain2DMapper> domainMapperCache;

static {
if (EdalCache.cacheManager.cacheExists(CACHE_NAME) == false) {
/*
* Configure cache
*/
log.debug("Creating domainMapperCache, with maximum "+MAX_HEAP_ENTRIES+" entries");
CacheConfiguration config = new CacheConfiguration(CACHE_NAME, MAX_HEAP_ENTRIES)
.eternal(true)
.memoryStoreEvictionPolicy(EVICTION_POLICY)
.persistence(new PersistenceConfiguration().strategy(PERSISTENCE_STRATEGY))
.transactionalMode(TRANSACTIONAL_MODE);
domainMapperCache = new Cache(config);
EdalCache.cacheManager.addCache(domainMapperCache);
Cache<Domain2DMapperCacheKey, Domain2DMapper> existing = EdalCache.cacheManager
.getCache(CACHE_NAME, Domain2DMapperCacheKey.class, Domain2DMapper.class);
if (existing == null) {
log.debug("Creating domainMapperCache, with maximum " + MAX_HEAP_ENTRIES + " entries");
domainMapperCache = EdalCache.cacheManager.createCache(CACHE_NAME,
CacheConfigurationBuilder.newCacheConfigurationBuilder(
Domain2DMapperCacheKey.class, Domain2DMapper.class,
ResourcePoolsBuilder.heap(MAX_HEAP_ENTRIES)));
} else {
log.debug("Loading existing domainMapperCache");
domainMapperCache = EdalCache.cacheManager.getCache(CACHE_NAME);
domainMapperCache = existing;
}
}

Expand All @@ -265,7 +255,7 @@ public int hashCode() {
int result = 1;
result = prime * result + ((source == null) ? 0 : source.hashCode());
result = prime * result + ((target == null) ? 0 : target.hashCode());
return result;
return EdalCache.murmur3Finalize(result);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,9 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;
import net.sf.ehcache.config.CacheConfiguration;
import net.sf.ehcache.config.CacheConfiguration.TransactionalMode;
import net.sf.ehcache.config.PersistenceConfiguration;
import net.sf.ehcache.config.PersistenceConfiguration.Strategy;
import net.sf.ehcache.store.MemoryStoreEvictionPolicy;
import org.ehcache.Cache;
import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.ResourcePoolsBuilder;
import uk.ac.rdg.resc.edal.cache.EdalCache;
import uk.ac.rdg.resc.edal.dataset.HZTDataSource.MeshCoordinates3D;
import uk.ac.rdg.resc.edal.exceptions.DataReadingException;
Expand Down Expand Up @@ -115,9 +111,9 @@ protected Array2D<Number> extractHorizontalData(HorizontalMesh4dVariableMetadata
MeshDatasetCacheElement meshDatasetCacheElement;

MeshCacheKey key = new MeshCacheKey(targetGrid, grid);
if (meshDatasetCache.isKeyInCache(key)) {
meshDatasetCacheElement = (MeshDatasetCacheElement) meshDatasetCache.get(key)
.getObjectValue();
MeshDatasetCacheElement cached = meshDatasetCache.get(key);
if (cached != null) {
meshDatasetCacheElement = cached;
outputCoords = meshDatasetCacheElement.getOutputCoords();
coordsToRead = meshDatasetCacheElement.getCoordsToRead();
} else {
Expand All @@ -132,7 +128,7 @@ protected Array2D<Number> extractHorizontalData(HorizontalMesh4dVariableMetadata
coordsToRead.add(meshCoords);
}
meshDatasetCacheElement = new MeshDatasetCacheElement(outputCoords, coordsToRead);
meshDatasetCache.put(new Element(key, meshDatasetCacheElement));
meshDatasetCache.put(key, meshDatasetCacheElement);
}

/*
Expand Down Expand Up @@ -230,10 +226,7 @@ protected Number extractPoint(HorizontalMesh4dVariableMetadata metadata, int t,
*/
private static final String CACHE_NAME = "meshDatasetCache";
private static final int MAX_HEAP_ENTRIES = 50;
private static final MemoryStoreEvictionPolicy EVICTION_POLICY = MemoryStoreEvictionPolicy.LFU;
private static final Strategy PERSISTENCE_STRATEGY = Strategy.NONE;
private static final TransactionalMode TRANSACTIONAL_MODE = TransactionalMode.OFF;
private static Cache meshDatasetCache = null;
private static final Cache<MeshCacheKey, MeshDatasetCacheElement> meshDatasetCache;

private static class MeshCacheKey {
private HorizontalGrid target;
Expand All @@ -251,7 +244,7 @@ public int hashCode() {
int result = 1;
result = prime * result + ((source == null) ? 0 : source.hashCode());
result = prime * result + ((target == null) ? 0 : target.hashCode());
return result;
return EdalCache.murmur3Finalize(result);
}

@Override
Expand All @@ -278,20 +271,17 @@ public boolean equals(Object obj) {
}

static {
if (EdalCache.cacheManager.cacheExists(CACHE_NAME) == false) {
/*
* Configure cache
*/
Cache<MeshCacheKey, MeshDatasetCacheElement> existing = EdalCache.cacheManager
.getCache(CACHE_NAME, MeshCacheKey.class, MeshDatasetCacheElement.class);
if (existing == null) {
log.debug("Creating meshDatasetCache, with maximum " + MAX_HEAP_ENTRIES + " entries");
CacheConfiguration config = new CacheConfiguration(CACHE_NAME, MAX_HEAP_ENTRIES)
.eternal(true).memoryStoreEvictionPolicy(EVICTION_POLICY)
.persistence(new PersistenceConfiguration().strategy(PERSISTENCE_STRATEGY))
.transactionalMode(TRANSACTIONAL_MODE);
meshDatasetCache = new Cache(config);
EdalCache.cacheManager.addCache(meshDatasetCache);
meshDatasetCache = EdalCache.cacheManager.createCache(CACHE_NAME,
CacheConfigurationBuilder.newCacheConfigurationBuilder(MeshCacheKey.class,
MeshDatasetCacheElement.class,
ResourcePoolsBuilder.heap(MAX_HEAP_ENTRIES)));
} else {
log.debug("Loading existing meshDatasetCache");
meshDatasetCache = EdalCache.cacheManager.getCache(CACHE_NAME);
meshDatasetCache = existing;
}
}
}
2 changes: 1 addition & 1 deletion graphics/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<version>3.6.2</version>
<executions>
<execution>
<phase>package</phase>
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>11</source>
<target>11</target>
<source>17</source>
<target>17</target>
</configuration>
<version>3.8.1</version>
</plugin>
Expand Down
Loading
Loading