Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add endpoint to clear the cdm_cache and achilles_cache #2406

Merged
merged 19 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# EditorConfig is awesome: https://EditorConfig.org

# top-most EditorConfig file
root = true

[*]
indent_style = space
indent_size = 4
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we're doing this let's do 2 space indent.

end_of_line = crlf
charset = utf-8
trim_trailing_whitespace = false
insert_final_newline = false
66 changes: 34 additions & 32 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<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/maven-v4_0_0.xsd">
<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/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

Expand Down Expand Up @@ -122,7 +122,7 @@
<security.ad.system.password></security.ad.system.password>
<security.ad.searchFilter>(&amp;(objectClass=person)(cn=%s))</security.ad.searchFilter>
<security.ad.ignore.partial.result.exception>true</security.ad.ignore.partial.result.exception>
<security.ad.result.count.limit>30000</security.ad.result.count.limit> <!-- 0 means no limit -->
<security.ad.result.count.limit>30000</security.ad.result.count.limit> <!-- 0 means no limit -->
<security.ad.default.import.group>public</security.ad.default.import.group>
<security.ad.searchString>(&amp;(objectClass=person)(userPrincipalName=%s))</security.ad.searchString>
<security.ad.userMapping.displaynameAttr>displayname</security.ad.userMapping.displaynameAttr>
Expand Down Expand Up @@ -191,8 +191,8 @@
<!-- If defaultGlobalReadPermissions is set to true (default), then all users can see every artifact. -->
<!-- If it is set to false, WebAPI will filter out the artifacts that a user does not explicitly have -->
<!-- read permissions to -->
<security.defaultGlobalReadPermissions>true</security.defaultGlobalReadPermissions>
<security.defaultGlobalReadPermissions>true</security.defaultGlobalReadPermissions>

<!-- EMBEDDED SERVER CONFIGURATION (ServerProperties) -->
<server.port>8080</server.port>
<server.ssl.key-store></server.ssl.key-store>
Expand All @@ -216,7 +216,7 @@
<jasypt.encryptor.enabled>false</jasypt.encryptor.enabled>
<jasypt.encryptor.password></jasypt.encryptor.password>
<jasypt.encryptor.algorithm>PBEWithMD5AndDES</jasypt.encryptor.algorithm>

<!-- ORGANIZATION SETTINGS -->
<organization.name>OHDSI</organization.name>

Expand Down Expand Up @@ -252,7 +252,6 @@
<!-- Cache Config -->
<cdm.result.cache.warming.enable>true</cdm.result.cache.warming.enable>
<cdm.cache.achilles.warming.enable>false</cdm.cache.achilles.warming.enable>
<cdm.result.clear.cache.enable>true</cdm.result.clear.cache.enable>
<!-- Enable or disable warming cdm cache by cron -->
<cdm.cache.cron.warming.enable>false</cdm.cache.cron.warming.enable>
<!-- cron expression to warm cdm cache -->
Expand Down Expand Up @@ -299,6 +298,7 @@
<audit.trail.log.file>/tmp/atlas/audit/audit.log</audit.trail.log.file>
<audit.trail.log.extraFile>/tmp/atlas/audit/audit-extra.log</audit.trail.log.extraFile>
</properties>

<build>
<finalName>WebAPI</finalName>
<sourceDirectory>${basedir}/src/main/java</sourceDirectory>
Expand Down Expand Up @@ -369,8 +369,8 @@
<!-- only include properties to speed up plugin -->
<!-- ref: https://github.com/git-commit-id/git-commit-id-maven-plugin/issues/462 -->
<includeOnlyProperties>
<includeOnlyProperty>git.branch</includeOnlyProperty>
<includeOnlyProperty>git.commit.id.abbrev</includeOnlyProperty>
<includeOnlyProperty>git.branch</includeOnlyProperty>
<includeOnlyProperty>git.commit.id.abbrev</includeOnlyProperty>
</includeOnlyProperties>
</configuration>
</plugin>
Expand Down Expand Up @@ -576,6 +576,7 @@
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
Expand Down Expand Up @@ -809,12 +810,12 @@
<artifactId>jackson-databind</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
Expand Down Expand Up @@ -1160,7 +1161,7 @@
<version>${pac4j.version}</version>
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</exclusion>
</exclusions>
Expand Down Expand Up @@ -1206,12 +1207,12 @@
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring.boot.version}</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>com.vaadin.external.google</groupId>
<artifactId>android-json</artifactId>
</exclusion>
</exclusions>
<exclusions>
<exclusion>
<groupId>com.vaadin.external.google</groupId>
<artifactId>android-json</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.dbunit</groupId>
Expand All @@ -1232,12 +1233,13 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.mjeanroy</groupId>
<artifactId>dbunit-plus</artifactId>
<version>2.0.1</version>
<scope>test</scope>
<groupId>com.github.mjeanroy</groupId>
<artifactId>dbunit-plus</artifactId>
<version>2.0.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<profiles>
<profile>
<id>webapi-oracle</id>
Expand Down Expand Up @@ -1330,17 +1332,17 @@
lower(email) = lower(?)</security.db.datasource.authenticationQuery>
</properties>
<repositories>
<repository>
<repository>
<id>ohdsi.snapshots</id>
<name>repo.ohdsi.org-snapshots</name>
<url>https://repo.ohdsi.org/nexus/content/repositories/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</snapshots>
</repository>
</repositories>
</profile>
<profile>
Expand Down Expand Up @@ -1413,7 +1415,7 @@
<impala.enabled>true</impala.enabled>
<impala.driver.version>2.6.15</impala.driver.version>
<!-- Impala JDBC driver path -->
<impala.classpath>...path/to/impala/jdbc/drivers...</impala.classpath>
<impala.classpath>...path/to/impala/jdbc/drivers...</impala.classpath>
</properties>
<dependencies>
<dependency>
Expand Down Expand Up @@ -1518,9 +1520,9 @@
<version>v2-rev20220326-1.32.1</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-bigquery</artifactId>
<version>1.2.15</version>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-bigquery</artifactId>
<version>1.2.15</version>
</dependency>
<dependency>
<groupId>com.google.http-client</groupId>
Expand Down

This file was deleted.

This file was deleted.

80 changes: 13 additions & 67 deletions src/main/java/org/ohdsi/webapi/service/CDMResultsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
import org.ohdsi.webapi.achilles.aspect.AchillesCache;
import org.ohdsi.webapi.achilles.service.AchillesCacheService;
import org.ohdsi.webapi.cdmresults.AchillesCacheTasklet;
import org.ohdsi.webapi.cdmresults.AchillesClearCacheTasklet;
import org.ohdsi.webapi.cdmresults.CDMResultsCacheTasklet;
import org.ohdsi.webapi.cdmresults.CDMResultsClearCacheTasklet;
import org.ohdsi.webapi.cdmresults.DescendantRecordAndPersonCount;
import org.ohdsi.webapi.cdmresults.DescendantRecordCount;
import org.ohdsi.webapi.cdmresults.domain.CDMCacheEntity;
Expand Down Expand Up @@ -48,6 +46,7 @@
import org.springframework.stereotype.Component;

import javax.ws.rs.Consumes;
import javax.ws.rs.ForbiddenException;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
Expand Down Expand Up @@ -118,9 +117,6 @@ public class CDMResultsService extends AbstractDaoService implements Initializin
@Value("${cdm.cache.achilles.warming.enable}")
private boolean cdmAchillesCacheWarmingEnable;

@Value("${cdm.result.clear.cache.enable}")
private boolean cdmResultClearCacheEnable;

@Value("${cache.achilles.usePersonCount:false}")
private boolean usePersonCount;

Expand Down Expand Up @@ -292,40 +288,17 @@ public JobExecutionResource refreshCache(@PathParam("sourceKey") final String so
* Clear the cdm_cache and achilles_cache for all sources
*
* @summary Clear the cdm_cache and achilles_cache for all sources
* @return List of JobExecutionResources
* @return void
* @throws ForbiddenException if the user is not an admin
*/
@GET
@POST
@Path("clearCache")
@Produces(MediaType.APPLICATION_JSON)
public List<JobExecutionResource> clearCache() {
List<JobExecutionResource> jobs = new ArrayList<>();

if (!cdmResultClearCacheEnable) {
logger.info("Clearing cache is disabled for CDM results");
return jobs;
}

public void clearCache() {
if (!isSecured() || !isAdmin()) {
return jobs;
throw new ForbiddenException();
}

List<Source> sources = getSourceRepository().findAll();
for (Source source : sources) {
if (!sourceAccessor.hasAccess(source)) {
continue;
}
JobExecutionResource jobExecutionResource = jobService.findJobByName(Constants.CLEAR_CACHE,
getClearCacheJobName(String.valueOf(source.getSourceId()), source.getSourceKey()));
if (jobExecutionResource == null) {
if (source.getDaimons().stream()
.anyMatch(sd -> Objects.equals(sd.getDaimonType(), SourceDaimon.DaimonType.Results))) {
jobs.add(clearCache(source));
}
} else {
jobs.add(jobExecutionResource);
}
}
return jobs;
sources.parallelStream().forEach(this::clearCache);
}

/**
Expand Down Expand Up @@ -549,12 +522,12 @@ private void warmCaches(Collection<Source> sources) {
/*
* Clear cache for a single source
*/
private JobExecutionResource clearCache(Source source) {
String jobName = getClearCacheJobName(String.valueOf(source.getSourceId()), source.getSourceKey());
List<Step> jobSteps = createClearCacheJobSteps(source, jobName);
SimpleJobBuilder builder = createJob(jobName,
jobSteps);
return runJob(source.getSourceKey(), source.getSourceId(), jobName, builder);
private void clearCache(Source source) {
if (!sourceAccessor.hasAccess(source)) {
return;
}
cacheService.clearCache(source);
cdmCacheService.clearCache(source);
}

private SimpleJobBuilder createJob(String jobName, List<Step> steps) {
Expand Down Expand Up @@ -619,29 +592,6 @@ private Step getCountStep(Source source, String jobStepName) {
.build();
}

private List<Step> createClearCacheJobSteps(Source source, String jobName) {
SimpleJob job = new SimpleJob(jobName);
job.setJobRepository(jobRepository);
List<Step> steps = new ArrayList<>();
steps.add(getAchillesClearCacheStep(source, jobName));
steps.add(getCountClearCacheStep(source, jobName));
return steps;
}

private Step getAchillesClearCacheStep(Source source, String jobStepName) {
AchillesClearCacheTasklet achillesTasklet = new AchillesClearCacheTasklet(source, cacheService);
return stepBuilderFactory.get(jobStepName + " achilles")
.tasklet(achillesTasklet)
.build();
}

private Step getCountClearCacheStep(Source source, String jobStepName) {
CDMResultsClearCacheTasklet countTasklet = new CDMResultsClearCacheTasklet(source, cdmCacheService);
return stepBuilderFactory.get(jobStepName + " counts")
.tasklet(countTasklet)
.build();
}

private int getResultsDaimonPriority(Source source) {
Optional<Integer> resultsPriority = source.getDaimons().stream()
.filter(d -> d.getDaimonType().equals(SourceDaimon.DaimonType.Results))
Expand All @@ -655,10 +605,6 @@ private String getWarmCacheJobName(String sourceIds, String sourceKeys) {
return getJobName("warming cache", sourceIds, sourceKeys);
}

private String getClearCacheJobName(String sourceIds, String sourceKeys) {
return getJobName("clearing cache", sourceIds, sourceKeys);
}

private String getJobName(String jobType, String sourceIds, String sourceKeys) {
// for multiple sources: try to compose a job name from source keys, and if it is too long - use source ids
String jobName = String.format("%s: %s", jobType, sourceKeys);
Expand Down
1 change: 0 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,6 @@ analysis.result.zipVolumeSizeMb=${analysis.result.zipVolumeSizeMb}
#Cache Config
cdm.result.cache.warming.enable=${cdm.result.cache.warming.enable}
cdm.cache.achilles.warming.enable=${cdm.cache.achilles.warming.enable}
cdm.result.clear.cache.enable=${cdm.result.clear.cache.enable}
cdm.cache.cron.warming.enable=${cdm.cache.cron.warming.enable}
cdm.cache.cron.expression=${cdm.cache.cron.expression}

Expand Down
3 changes: 2 additions & 1 deletion src/test/java/org/ohdsi/webapi/test/ITStarter.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
SecurityIT.class,
JobServiceIT.class,
CohortAnalysisServiceIT.class,
VocabularyServiceIT.class
VocabularyServiceIT.class,
CDMResultsServiceIT.class
})
@TestPropertySource(locations = "/application-test.properties")
public class ITStarter extends AbstractShiro {
Expand Down
Loading
Loading