Skip to content

Commit

Permalink
Merge pull request #630 from ao508/crdb-timeout
Browse files Browse the repository at this point in the history
CRDB service query timeout
  • Loading branch information
divyamadala30 authored Mar 3, 2022
2 parents df74364 + 6609e5b commit 76d0d59
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
package org.mskcc.cmo.metadb.service.impl;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import org.mskcc.cmo.metadb.persistence.jpa.CrdbRepository;
import org.mskcc.cmo.metadb.service.CrdbMappingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class CrdbMappingServiceImpl implements CrdbMappingService {
@Value("${crdb.query_timeout_seconds:5}")
private int crdbQueryTimeoutSeconds;

private ExecutorService executor = Executors.newCachedThreadPool();

@Autowired
private CrdbRepository crdbRepository;
Expand All @@ -19,11 +29,40 @@ public CrdbMappingServiceImpl(CrdbRepository crdbRepository) {

@Override
public String getCmoPatientIdbyDmpId(String dmpId) {
return crdbRepository.getCmoPatientIdbyDmpId(dmpId).toString();
Callable<Object> task = new Callable<Object>() {
@Override
public Object call() {
return crdbRepository.getCmoPatientIdbyDmpId(dmpId);
}
};
Object result = runQueryWithForcedTimeout(task);
if (result != null) {
return result.toString();
}
return null;
}

@Override
public String getCmoPatientIdByInputId(String inputId) {
return crdbRepository.getCmoPatientIdByInputId(inputId).toString();
Callable<Object> task = new Callable<Object>() {
@Override
public Object call() {
return crdbRepository.getCmoPatientIdByInputId(inputId);
}
};
Object result = runQueryWithForcedTimeout(task);
if (result != null) {
return result.toString();
}
return null;
}

private Object runQueryWithForcedTimeout(Callable<Object> task) {
Future<Object> future = executor.submit(task);
try {
return future.get(crdbQueryTimeoutSeconds, TimeUnit.SECONDS);
} catch (Exception e) {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class CrdbMappingServiceTest {
@Autowired
private CrdbRepository crdbRepository;

@Autowired
private CrdbMappingServiceImpl crdbMappingService;

/**
Expand All @@ -34,10 +35,9 @@ public void before() {
for (Map.Entry<String, String> entry : mockDataUtils.mockedDmpPatientMapping.entrySet()) {
String dmpId = entry.getKey();
String cmoId = entry.getValue();
Mockito.when(crdbRepository.getCmoPatientIdbyDmpId(dmpId))
Mockito.when(crdbMappingService.getCmoPatientIdbyDmpId(dmpId))
.thenReturn(cmoId);
}
this.crdbMappingService = new CrdbMappingServiceImpl(crdbRepository);
}

/**
Expand All @@ -46,9 +46,8 @@ public void before() {
*/
@Test
public void testMadeUpDmpIdThrowsNullPointerException() {
Assertions.assertThatExceptionOfType(NullPointerException.class).isThrownBy(() -> {
crdbMappingService.getCmoPatientIdbyDmpId("MADEUPVALUE");
});
Assertions.assertThat(crdbMappingService.getCmoPatientIdbyDmpId("MADEUPVALUE"))
.isNull();
}

/**
Expand All @@ -59,10 +58,7 @@ public void testDmpToCmoIdMapping() {
for (Map.Entry<String, String> entry : mockDataUtils.mockedDmpPatientMapping.entrySet()) {
String dmpId = entry.getKey();
String cmoId = entry.getValue();
if (dmpId != null) {
Assertions.assertThat(cmoId)
.isEqualTo(crdbMappingService.getCmoPatientIdbyDmpId(dmpId));
}
Assertions.assertThat(crdbMappingService.getCmoPatientIdbyDmpId(dmpId)).isEqualTo(cmoId);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.mskcc.cmo.metadb.persistence.jpa.CrdbRepository;
import org.mskcc.cmo.metadb.service.impl.ClinicalMessageHandlingServiceImpl;
import org.mskcc.cmo.metadb.service.impl.CorrectCmoPatientHandlingServiceImpl;
import org.mskcc.cmo.metadb.service.impl.CrdbMappingServiceImpl;
import org.mskcc.cmo.metadb.service.impl.PatientServiceImpl;
import org.mskcc.cmo.metadb.service.impl.RequestReplyHandlingServiceImpl;
import org.mskcc.cmo.metadb.service.impl.RequestServiceImpl;
Expand Down Expand Up @@ -51,6 +52,9 @@ public MetadbJsonComparator metadbJsonComparator() {
@MockBean
public CrdbRepository crdbRepository;

@MockBean
public CrdbMappingServiceImpl crdbMappingService;

@MockBean
public RequestStatusLogger requestStatusLogger;

Expand All @@ -62,10 +66,10 @@ public MetadbJsonComparator metadbJsonComparator() {

@MockBean
public ResearchMessageHandlingServiceImpl researchMessageHandlingService;

@MockBean
public ClinicalMessageHandlingServiceImpl clinicalMessageHandlingService;

@MockBean
public CorrectCmoPatientHandlingServiceImpl patientCorrectionHandlingService;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ public void mockedDmpPatientMapping() throws IOException {
} catch (ArrayIndexOutOfBoundsException e) {
// do nothing
}
mockedDmpPatientMapping.put(dmpPatientId, cmoPatientId);
if (dmpPatientId != null) {
mockedDmpPatientMapping.put(dmpPatientId, cmoPatientId);
}
}
reader.close();
}
Expand Down
20 changes: 20 additions & 0 deletions src/main/resources/application.properties.EXAMPLE
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,26 @@ spring.data.neo4j.username=
spring.data.neo4j.password=
spring.data.neo4j.uri=

# OracleDB connection settings
spring.datasource.url=
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=

# HikariCP settings
spring.datasource.hikari.minimumIdle=
spring.datasource.hikari.maximumPoolSize=
spring.datasource.hikari.idleTimeout=
spring.datasource.hikari.maxLifetime=
spring.datasource.hikari.connectionTimeout=
spring.datasource.hikari.poolName=

# JPA settings
spring.jpa.database-platform=
spring.jpa.hibernate.use-new-id-generator-mappings=
spring.jpa.hibernate.ddl-auto=
crdb.query_timeout_seconds=

# new request topics
num.new_request_handler_threads=
igo.new_request_topic=
Expand Down

0 comments on commit 76d0d59

Please sign in to comment.