Commit dc33b770 authored by Matija Obreza's avatar Matija Obreza
Browse files

HZ config, filtering page limit

parent 66ee2c95
......@@ -148,6 +148,16 @@ public class WebAppInitializer implements WebApplicationInitializer {
localeURLFilter.addMappingForUrlPatterns(null, false, "/*");
// Hazelcast web filter configuration
FilterRegistration.Dynamic hazelcastWebFilter = servletContext
.addFilter("hazelcastWebFilter", new DelegatingFilterProxy("hazelcastWebFilter"));
hazelcastWebFilter.setInitParameter("targetFilterLifecycle", "true");
hazelcastWebFilter.addMappingForUrlPatterns(
EnumSet.of(DispatcherType.FORWARD, DispatcherType.INCLUDE, DispatcherType.REQUEST), false, "/*");
// EnvVar filter configuration
// Adds variables to all requests
FilterRegistration.Dynamic envVarFilter = servletContext
......
......@@ -35,7 +35,14 @@ import org.genesys2.server.model.impl.Crop;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.persistence.domain.AccessionRepository;
import org.genesys2.server.persistence.domain.MethodRepository;
import org.genesys2.server.service.*;
import org.genesys2.server.service.CropService;
import org.genesys2.server.service.ElasticService;
import org.genesys2.server.service.FilterConstants;
import org.genesys2.server.service.GenesysFilterService;
import org.genesys2.server.service.GenesysService;
import org.genesys2.server.service.GeoService;
import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.TaxonomyService;
import org.genesys2.server.service.impl.DirectMysqlQuery.MethodResolver;
import org.genesys2.server.service.impl.FilterHandler.AppliedFilter;
import org.genesys2.server.service.impl.FilterHandler.AppliedFilters;
......@@ -59,6 +66,8 @@ import org.springframework.transaction.annotation.Transactional;
@Transactional(readOnly = true)
public class GenesysFilterServiceImpl implements GenesysFilterService {
private static final int PAGINATION_MAXPAGE_LIMIT = 500;
private static final Log LOG = LogFactory.getLog(GenesysFilterServiceImpl.class);
// using this loop to make @Cacheable work
......@@ -104,6 +113,10 @@ public class GenesysFilterServiceImpl implements GenesysFilterService {
}
}
if (pageable.getPageNumber() > PAGINATION_MAXPAGE_LIMIT) {
throw new RuntimeException("Genesys does not support requests for data beyond " + PAGINATION_MAXPAGE_LIMIT + "th page.");
}
final DirectMysqlQuery directQuery = new DirectMysqlQuery("accession", "a");
directQuery.jsonFilter(filters, new MethodResolver() {
@Override
......@@ -121,10 +134,12 @@ public class GenesysFilterServiceImpl implements GenesysFilterService {
final List<Long> results = this.jdbcTemplate.queryForList(directQuery.getQuery("distinct a.id"), directQuery.getParameters(), Long.class);
LOG.debug("Getting accessions " + results.size());
return new PageImpl<Accession>(results.size() == 0 ? new ArrayList<Accession>() : accessionRepository.listById(results,
// TODO Consider processing the pageable.getSort
new Sort("accessionName")),
// -- TODO
return new PageImpl<Accession>(
results.size() == 0 ? new ArrayList<Accession>()
: accessionRepository.listById(results,
// TODO Consider processing the pageable.getSort
new Sort("accessionName")),
// -- TODO
pageable, totalCount);
} else {
return new PageImpl<Accession>(new ArrayList<Accession>(), pageable, totalCount);
......@@ -132,9 +147,8 @@ public class GenesysFilterServiceImpl implements GenesysFilterService {
}
/**
* Copy of
* {@link GenesysFilterServiceImpl#listAccessions(AppliedFilters, Pageable)
* Copy of {@link GenesysFilterServiceImpl#listAccessions(AppliedFilters, Pageable)
*
*/
@Override
public Page<AccessionDetails> listAccessionDetails(AppliedFilters filters, Pageable pageable) {
......@@ -144,6 +158,10 @@ public class GenesysFilterServiceImpl implements GenesysFilterService {
}
}
if (pageable.getPageNumber() > PAGINATION_MAXPAGE_LIMIT) {
throw new RuntimeException("Genesys does not support requests for data beyond " + PAGINATION_MAXPAGE_LIMIT + "th page.");
}
final DirectMysqlQuery directQuery = new DirectMysqlQuery("accession", "a");
directQuery.jsonFilter(filters, new MethodResolver() {
@Override
......@@ -201,7 +219,7 @@ public class GenesysFilterServiceImpl implements GenesysFilterService {
if (termResult != null) {
for (final FaoInstitute inst : faoInst) {
String label = inst.getCode() + ", " + inst.getFullName();
for (Term term: termResult.getTerms()) {
for (Term term : termResult.getTerms()) {
if (term.getTerm().equals(inst.getCode()) && term.getCount() > 0) {
label = label.concat(" (" + term.getCount() + ")");
completed.add(new LabelValue<String>(inst.getCode(), label));
......@@ -222,7 +240,7 @@ public class GenesysFilterServiceImpl implements GenesysFilterService {
if (termResult != null) {
for (final Country c : countries) {
String label = c.getCode3() + ", " + c.getName();
for (Term term: termResult.getTerms()) {
for (Term term : termResult.getTerms()) {
if (term.getTerm().equals(c.getCode3()) && term.getCount() > 0) {
label = label.concat(" (" + term.getCount() + ")");
completed.add(new LabelValue<String>(c.getCode3(), label));
......@@ -243,7 +261,7 @@ public class GenesysFilterServiceImpl implements GenesysFilterService {
if (termResult != null) {
for (final String value : genera) {
String label = value;
for (Term term: termResult.getTerms()) {
for (Term term : termResult.getTerms()) {
if (term.getTerm().equals(value) && term.getCount() > 0) {
label = label.concat(" (" + term.getCount() + ")");
completed.add(new LabelValue<String>(value, label));
......@@ -273,7 +291,7 @@ public class GenesysFilterServiceImpl implements GenesysFilterService {
if (termResult != null) {
for (final String value : species) {
String label = value;
for (Term term: termResult.getTerms()) {
for (Term term : termResult.getTerms()) {
if (term.getTerm().equals(value) && term.getCount() > 0) {
label = label.concat(" (" + term.getCount() + ")");
completed.add(new LabelValue<String>(value, label));
......@@ -310,7 +328,7 @@ public class GenesysFilterServiceImpl implements GenesysFilterService {
if (termResult != null) {
for (final String value : subtaxa) {
String label = value;
for (Term term: termResult.getTerms()) {
for (Term term : termResult.getTerms()) {
if (term.getTerm().equals(value) && term.getCount() > 0) {
label = label.concat(" (" + term.getCount() + ")");
completed.add(new LabelValue<String>(value, label));
......@@ -331,7 +349,7 @@ public class GenesysFilterServiceImpl implements GenesysFilterService {
if (termResult != null) {
for (final String taxonomy : taxa) {
String label = taxonomy;
for (Term term: termResult.getTerms()) {
for (Term term : termResult.getTerms()) {
if (term.getTerm().equals(taxonomy) && term.getCount() > 0) {
label = label.concat(" (" + term.getCount() + ")");
completed.add(new LabelValue<String>(taxonomy, label));
......@@ -399,8 +417,8 @@ public class GenesysFilterServiceImpl implements GenesysFilterService {
this.jdbcTemplate.query(new PreparedStatementCreator() {
@Override
public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
final PreparedStatement stmt = con.prepareStatement(directQuery.getQuery(distinct ? "distinct geo.longitude, geo.latitude"
: "a.id, a.acceNumb, a.instCode, geo.longitude, geo.latitude, geo.datum, geo.uncertainty "));
final PreparedStatement stmt = con.prepareStatement(directQuery
.getQuery(distinct ? "distinct geo.longitude, geo.latitude" : "a.id, a.acceNumb, a.instCode, geo.longitude, geo.latitude, geo.datum, geo.uncertainty "));
// Set mysql JConnector to stream results
stmt.setFetchSize(Integer.MIN_VALUE);
new ArgumentPreparedStatementSetter(directQuery.getParameters()).setValues(stmt);
......
......@@ -48,6 +48,10 @@ public class HazelcastConfigAWS extends HazelcastConfig {
private String secretKey;
@Value("${hazelcast.aws.security-group}")
private String securityGroupName;
@Value("${cache.time-to-live-seconds}")
private int defaultTimeToLiveSeconds;
@Value("${cache.max-idle-seconds}")
private int defaultMaxIdleSeconds;
@Bean
public HazelcastInstance hazelcast(ManagedContext managedContext) {
......@@ -93,9 +97,9 @@ public class HazelcastConfigAWS extends HazelcastConfig {
MapConfig defaultMapConfig = new MapConfig();
defaultMapConfig.setName("default");
// defaultMapConfig.setTimeToLiveSeconds(0);
defaultMapConfig.setTimeToLiveSeconds(defaultTimeToLiveSeconds);
defaultMapConfig.setMaxIdleSeconds(defaultMaxIdleSeconds);
defaultMapConfig.setEvictionPolicy(EvictionPolicy.LFU);
// defaultMapConfig.setMaxIdleSeconds();
MaxSizeConfig defaultMaxSizeConfig = new MaxSizeConfig();
defaultMaxSizeConfig.setSize(defaultCacheSize);
defaultMaxSizeConfig.setMaxSizePolicy(MaxSizePolicy.PER_NODE);
......
......@@ -16,6 +16,7 @@
package org.genesys2.spring.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
......@@ -38,6 +39,10 @@ import com.hazelcast.core.ManagedContext;
@Configuration
@Profile({ "dev" })
public class HazelcastConfigDev extends HazelcastConfig {
@Value("${cache.time-to-live-seconds}")
private int defaultTimeToLiveSeconds;
@Value("${cache.max-idle-seconds}")
private int defaultMaxIdleSeconds;
@Bean
public HazelcastInstance hazelcast(ManagedContext managedContext) {
......@@ -64,9 +69,9 @@ public class HazelcastConfigDev extends HazelcastConfig {
MapConfig defaultMapConfig = new MapConfig();
defaultMapConfig.setName("default");
// defaultMapConfig.setTimeToLiveSeconds(0);
defaultMapConfig.setTimeToLiveSeconds(defaultTimeToLiveSeconds);
defaultMapConfig.setMaxIdleSeconds(defaultMaxIdleSeconds);
defaultMapConfig.setEvictionPolicy(EvictionPolicy.LFU);
// defaultMapConfig.setMaxIdleSeconds();
MaxSizeConfig defaultMaxSizeConfig = new MaxSizeConfig();
defaultMaxSizeConfig.setSize(defaultCacheSize);
defaultMaxSizeConfig.setMaxSizePolicy(MaxSizePolicy.PER_NODE);
......
......@@ -32,6 +32,8 @@ import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.security.core.session.SessionRegistry;
import org.springframework.security.core.session.SessionRegistryImpl;
@Configuration
@EnableCaching
......@@ -87,6 +89,11 @@ public class SpringCacheConfig {
public WebFilter hazelcastWebFilter() {
return new SpringAwareWebFilter(filterProps());
}
@Bean
public SessionRegistry SessionRegistry() {
return new SessionRegistryImpl();
}
private Properties filterProps() {
final Properties properties = new Properties();
......
......@@ -134,6 +134,10 @@ tileserver.cdn='${base.url}'
# TileServer Cache
cache.defaultCacheSize=5000
# 300 / 60 = 5min
cache.time-to-live-seconds=300
# 120 / 60 = 2min
cache.max-idle-seconds=120
cache.tileserver.max-size=1000
cache.tileserver.time-to-live-seconds=300
cache.tileserver.max-idle-seconds=0
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment