Skip to content

Commit

Permalink
Ensure project is not cached in session #934
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisala committed May 13, 2024
1 parent 10d227b commit fbdd2c6
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 17 deletions.
36 changes: 19 additions & 17 deletions grails-app/services/au/org/ala/ecodata/ProjectService.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -1100,28 +1100,30 @@ class ProjectService {
*/
Map updateDataSets(String projectId, List dataSets) {
synchronized (PROJECT_UPDATE_LOCKS.get(projectId)) {
Project project = Project.findByProjectId(projectId)
if (!project) {
return [status: 'error', error: "No project exists with projectId=${projectId}"]
}
for (Map dataSet in dataSets) {
if (!dataSet.dataSetId) {
dataSet.dataSetId = Identifiers.getNew(true, '')
Project.withNewSession { // Ensure that the queried Project is not cached in the current session which can cause stale data
Project project = Project.findByProjectId(projectId)
if (!project) {
return [status: 'error', error: "No project exists with projectId=${projectId}"]
}
Map matchingDataSet = project.custom?.dataSets?.find { it.dataSetId == dataSet.dataSetId }
if (matchingDataSet) {
matchingDataSet.putAll(dataSet)
} else {
if (!project.custom) {
project.custom = [:]
for (Map dataSet in dataSets) {
if (!dataSet.dataSetId) {
dataSet.dataSetId = Identifiers.getNew(true, '')
}
if (!project.custom?.dataSets) {
project.custom.dataSets = []
Map matchingDataSet = project.custom?.dataSets?.find { it.dataSetId == dataSet.dataSetId }
if (matchingDataSet) {
matchingDataSet.putAll(dataSet)
} else {
if (!project.custom) {
project.custom = [:]
}
if (!project.custom?.dataSets) {
project.custom.dataSets = []
}
project.custom.dataSets.add(dataSet)
}
project.custom.dataSets.add(dataSet)
}
update([custom: project.custom], project.projectId, false)
}
update([custom: project.custom], project.projectId, false)
}
}

Expand Down
40 changes: 40 additions & 0 deletions src/test/groovy/au/org/ala/ecodata/ProjectServiceSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import org.grails.web.converters.marshaller.json.CollectionMarshaller
import org.grails.web.converters.marshaller.json.MapMarshaller
import spock.lang.Ignore

import java.util.concurrent.Callable
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors

class ProjectServiceSpec extends MongoSpec implements ServiceUnitTest<ProjectService> {

ProjectActivityService projectActivityServiceStub = Stub(ProjectActivityService)
Expand Down Expand Up @@ -850,4 +854,40 @@ class ProjectServiceSpec extends MongoSpec implements ServiceUnitTest<ProjectSer
}
void "The updateDataSet method is safe for concurrent access of different data sets"() {
setup:
Project project = new Project(projectId: 'p1', name: "Project 1", hubId:"12345")
project.save(flush: true, failOnError: true)
ExecutorService executor = Executors.newFixedThreadPool(20)
Project project2
when:
List callables = []
for (int i = 0; i < 100; i++) {
Map dataSet = [name: 'Test Data Set', description: 'Test Description', dataSetId:'d' + i]
Callable callable = new Callable() {
@Override
Object call() throws Exception {
service.updateDataSet(project.projectId, dataSet)
println "Updated data set ${dataSet.dataSetId}"
return null
}
}
callables.add(callable)
}
executor.invokeAll(callables)
Project.withNewSession {
project2 = Project.findByProjectId(project.projectId)
}
then:
project2.custom.dataSets.size() == 100
for (int i = 0; i < 100; i++) {
project2.custom.dataSets.find { it.dataSetId == 'd' + i } != null
}
}
}

0 comments on commit fbdd2c6

Please sign in to comment.