Skip to content

Commit a7bbffb

Browse files
authored
Activate Hibernate second-level cache (#474)
* Activate Hibernate second-level cache Some requests which trigger many database queries have been running very slowly. To help with this we activate the Hibernate "second-level" cache which persists between database transactions and is shared between threads. In simple local experiments this can increase the speed of fully cached requests by over 3x. * Fix EHCache config * Use Embedded Postgres DB in all tests * Fix formatting * Fix dependencies for Zonky * Simplify and reorder dependency specification * Configure klass-dev for OpenSearch * Fix template syntax * Fix logging conflict * Remove some conflicting dependencies * Remove unused file * Caching expires after 1 day * Revert "Remove some conflicting dependencies" This reverts commit f50b06b.
1 parent 0954168 commit a7bbffb

21 files changed

+257
-282
lines changed

.nais/test/klass-api-dev.yaml

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ metadata:
99
team: dapla-metadata
1010
spec:
1111
port: 8080
12-
ttl: {{ time-to-live }}
12+
ttl: "{{ time-to-live }}"
1313
ingresses:
1414
- https://klass-dev-{{ app-name }}.intern.test.ssb.no/api/klass
1515
replicas:
@@ -18,18 +18,25 @@ spec:
1818
resources:
1919
requests:
2020
cpu: 200m
21-
memory: 1200Mi
21+
memory: 1000Mi
2222
limits:
23-
memory: 1400Mi
23+
memory: 1920Mi
2424
env:
2525
- name: SPRING_PROFILES_ACTIVE
26-
value: api, postgres, solr-remote, skip-indexing
27-
- name: SPRING_DATA_SOLR_HOST
28-
value: http://klass-solr/solr
26+
value: api, postgres, remote-open-search, skip-indexing
27+
- name: OPENSEARCH_USERNAME
28+
value: ${OPEN_SEARCH_USERNAME}
29+
- name: OPENSEARCH_URL
30+
value: ${OPEN_SEARCH_URI}
31+
- name: OPENSEARCH_PASSWORD
32+
value: ${OPEN_SEARCH_PASSWORD}
33+
# Structured logging in cluster
34+
- name: LOGGING_STRUCTURED_FORMAT_CONSOLE
35+
value: logstash
2936
- name: KLASS_ENV_CLIENT_KLASS_MAIL_URL
3037
value: http://klass-mail
3138
- name: JAVA_TOOL_OPTIONS
32-
value: "-XX:InitialRAMPercentage=50.0 -XX:MaxRAMPercentage=75.0"
39+
value: "-XX:InitialRAMPercentage=50.0 -XX:MaxRAMPercentage=50.0"
3340

3441
envFrom:
3542
- secret: google-sql-klass
@@ -41,6 +48,9 @@ spec:
4148
rules:
4249
- application: klass-solr
4350
- application: klass-mail
51+
openSearch:
52+
access: admin
53+
instance: klass-search
4454

4555
prometheus:
4656
enabled: true
@@ -62,9 +72,3 @@ spec:
6272
port: 8090
6373
initialDelay: 60
6474
timeout: 180
65-
66-
67-
68-
69-
70-

klass-api/pom.xml

Lines changed: 49 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
34
<modelVersion>4.0.0</modelVersion>
45

56
<name>Klass API</name>
@@ -25,6 +26,7 @@
2526
<artifactId>klass-shared</artifactId>
2627
<version>${project.parent.version}</version>
2728
</dependency>
29+
2830
<!--Spring-->
2931
<dependency>
3032
<groupId>org.springframework.boot</groupId>
@@ -43,6 +45,11 @@
4345
<artifactId>spring-boot-configuration-processor</artifactId>
4446
<optional>true</optional>
4547
</dependency>
48+
<dependency>
49+
<groupId>org.springframework.boot</groupId>
50+
<artifactId>spring-boot-starter-tomcat</artifactId>
51+
<scope>provided</scope>
52+
</dependency>
4653
<dependency>
4754
<groupId>org.springdoc</groupId>
4855
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
@@ -53,6 +60,8 @@
5360
<artifactId>spring-data-opensearch-starter</artifactId>
5461
<version>1.7.1</version>
5562
</dependency>
63+
64+
<!--Non-Spring-->
5665
<dependency>
5766
<groupId>jakarta.xml.bind</groupId>
5867
<artifactId>jakarta.xml.bind-api</artifactId>
@@ -62,30 +71,10 @@
6271
<groupId>org.glassfish.jaxb</groupId>
6372
<artifactId>jaxb-runtime</artifactId>
6473
</dependency>
65-
<dependency>
66-
<groupId>org.testcontainers</groupId>
67-
<artifactId>testcontainers</artifactId>
68-
<version>1.19.8</version>
69-
<scope>test</scope>
70-
</dependency>
71-
<dependency>
72-
<groupId>org.testcontainers</groupId>
73-
<artifactId>junit-jupiter</artifactId>
74-
<version>1.21.3</version>
75-
<scope>test</scope>
76-
</dependency>
77-
<dependency>
78-
<groupId>org.opensearch</groupId>
79-
<artifactId>opensearch-testcontainers</artifactId>
80-
<version>2.0.1</version>
81-
<scope>test</scope>
82-
</dependency>
8374
<dependency>
8475
<groupId>org.postgresql</groupId>
8576
<artifactId>postgresql</artifactId>
8677
</dependency>
87-
88-
<!--Flyway-->
8978
<dependency>
9079
<groupId>org.flywaydb</groupId>
9180
<artifactId>flyway-database-postgresql</artifactId>
@@ -97,14 +86,6 @@
9786
<artifactId>flyway-core</artifactId>
9887
<version>11.17.0</version>
9988
</dependency>
100-
101-
<!--TOMCAT-->
102-
<dependency>
103-
<groupId>org.springframework.boot</groupId>
104-
<artifactId>spring-boot-starter-tomcat</artifactId>
105-
<scope>provided</scope>
106-
</dependency>
107-
10889
<dependency>
10990
<groupId>ch.qos.logback.access</groupId>
11091
<artifactId>logback-access-common</artifactId>
@@ -120,48 +101,33 @@
120101
<artifactId>logstash-logback-encoder</artifactId>
121102
<version>9.0</version>
122103
</dependency>
123-
124104
<dependency>
125105
<groupId>org.apache.tomcat.embed</groupId>
126106
<artifactId>tomcat-embed-jasper</artifactId>
127107
<scope>provided</scope>
128108
</dependency>
129-
130-
<!--TEST SCOPE-->
131109
<dependency>
132-
<groupId>no.ssb.klass</groupId>
133-
<artifactId>klass-shared</artifactId>
134-
<type>test-jar</type>
135-
<scope>test</scope>
136-
<version>${project.parent.version}</version>
110+
<groupId>com.jayway.jsonpath</groupId>
111+
<artifactId>json-path</artifactId>
137112
</dependency>
138-
139113
<dependency>
140114
<groupId>jakarta.ws.rs</groupId>
141115
<artifactId>jakarta.ws.rs-api</artifactId>
142116
<version>${jakarta-rs-api-version}</version>
143117
</dependency>
144-
145118
<dependency>
146119
<groupId>io.micrometer</groupId>
147120
<artifactId>micrometer-registry-prometheus</artifactId>
148121
</dependency>
149122

150-
<!-- Zonky test -->
151-
<dependency>
152-
<groupId>io.zonky.test</groupId>
153-
<artifactId>embedded-postgres</artifactId>
154-
<version>2.2.0</version>
155-
<scope>test</scope>
156-
</dependency>
123+
<!--TEST SCOPE-->
157124
<dependency>
158-
<groupId>io.zonky.test</groupId>
159-
<artifactId>embedded-database-spring-test</artifactId>
160-
<version>2.7.1</version>
125+
<groupId>no.ssb.klass</groupId>
126+
<artifactId>klass-shared</artifactId>
127+
<type>test-jar</type>
161128
<scope>test</scope>
129+
<version>${project.parent.version}</version>
162130
</dependency>
163-
164-
<!--Spring TEST-->
165131
<dependency>
166132
<groupId>org.springframework.boot</groupId>
167133
<artifactId>spring-boot-starter-test</artifactId>
@@ -185,32 +151,51 @@
185151
<version>${spring-restdocs-version}</version>
186152
<scope>test</scope>
187153
</dependency>
188-
<dependency>
189-
<groupId>com.jayway.jsonpath</groupId>
190-
<artifactId>json-path</artifactId>
191-
</dependency>
192154
<dependency>
193155
<groupId>io.rest-assured</groupId>
194156
<artifactId>rest-assured</artifactId>
195157
<scope>test</scope>
196158
<exclusions>
197-
<!-- remove conflicting dependency because declared in 2 differents versions (3 & 4)
198-
See: https://github.com/rest-assured/rest-assured/issues/1612 -->
199159
<exclusion>
200-
<groupId>org.apache.groovy</groupId>
201-
<artifactId>groovy-xml</artifactId>
160+
<!--Conflicts with Spring's logging setup-->
161+
<groupId>commons-logging</groupId>
162+
<artifactId>commons-logging</artifactId>
202163
</exclusion>
203164
</exclusions>
204165
</dependency>
205166
<dependency>
206-
<!-- add this dep which bring back the groovy-xml in a single and last version (4) -->
207-
<groupId>io.rest-assured</groupId>
208-
<artifactId>xml-path</artifactId>
167+
<groupId>org.springframework</groupId>
168+
<artifactId>spring-test</artifactId>
209169
<scope>test</scope>
210170
</dependency>
211171
<dependency>
212-
<groupId>org.springframework</groupId>
213-
<artifactId>spring-test</artifactId>
172+
<groupId>io.zonky.test</groupId>
173+
<artifactId>embedded-postgres</artifactId>
174+
<version>${zonky.version}</version>
175+
<scope>test</scope>
176+
</dependency>
177+
<dependency>
178+
<groupId>io.zonky.test</groupId>
179+
<artifactId>embedded-database-spring-test</artifactId>
180+
<version>${zonky.pg.version}</version>
181+
<scope>test</scope>
182+
</dependency>
183+
<dependency>
184+
<groupId>org.testcontainers</groupId>
185+
<artifactId>testcontainers</artifactId>
186+
<version>1.19.8</version>
187+
<scope>test</scope>
188+
</dependency>
189+
<dependency>
190+
<groupId>org.testcontainers</groupId>
191+
<artifactId>junit-jupiter</artifactId>
192+
<version>1.21.3</version>
193+
<scope>test</scope>
194+
</dependency>
195+
<dependency>
196+
<groupId>org.opensearch</groupId>
197+
<artifactId>opensearch-testcontainers</artifactId>
198+
<version>2.0.1</version>
214199
<scope>test</scope>
215200
</dependency>
216201
</dependencies>

klass-api/src/test/java/no/ssb/klass/api/ApiDocumentation.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package no.ssb.klass.api;
22

3+
import static io.zonky.test.db.AutoConfigureEmbeddedDatabase.DatabaseProvider.EMBEDDED;
4+
35
import static org.mockito.Mockito.*;
46
import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.*;
57
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
@@ -15,6 +17,8 @@
1517
import com.google.common.collect.Lists;
1618
import com.google.common.collect.Sets;
1719

20+
import io.zonky.test.db.AutoConfigureEmbeddedDatabase;
21+
1822
import no.ssb.klass.api.config.MockConfig;
1923
import no.ssb.klass.api.config.TestConfig;
2024
import no.ssb.klass.api.services.OpenSearchResult;
@@ -88,6 +92,9 @@
8892
ElasticsearchRestClientAutoConfiguration.class,
8993
ElasticsearchDataAutoConfiguration.class
9094
})
95+
@AutoConfigureEmbeddedDatabase(
96+
provider = EMBEDDED,
97+
type = AutoConfigureEmbeddedDatabase.DatabaseType.POSTGRES)
9198
public class ApiDocumentation {
9299
private static final int CLASS_ID_FAMILIEGRUPPERING = 17;
93100
private static final int CLASS_ID_GREENHOUSE_GASES = 84;

klass-api/src/test/java/no/ssb/klass/api/applicationtest/AbstractRestApiApplicationTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package no.ssb.klass.api.applicationtest;
22

3-
import static io.zonky.test.db.AutoConfigureEmbeddedDatabase.DatabaseProvider.ZONKY;
3+
import static io.zonky.test.db.AutoConfigureEmbeddedDatabase.DatabaseProvider.EMBEDDED;
44

55
import io.zonky.test.db.AutoConfigureEmbeddedDatabase;
66

@@ -57,7 +57,7 @@
5757
ConfigurationProfiles.MOCK_SEARCH
5858
})
5959
@AutoConfigureEmbeddedDatabase(
60-
provider = ZONKY,
60+
provider = EMBEDDED,
6161
type = AutoConfigureEmbeddedDatabase.DatabaseType.POSTGRES)
6262
@ComponentScan(basePackageClasses = TranslatablePersistenceConverter.class)
6363
public abstract class AbstractRestApiApplicationTest {

0 commit comments

Comments
 (0)