From 01d48eef30c6b6b76d7e4cc09491ac35a462de1b Mon Sep 17 00:00:00 2001 From: Kostiantyn Dementiev Date: Sun, 15 Nov 2026 14:26:57 +0000 Subject: [PATCH 1/2] Lock the concept set row while saving a renamed concept set (#2430) updateConceptSet loads the concept set and then saves its version in a separate REQUIRES_NEW transaction. If the concept set is deleted between the load and the version insert, the insert violates fk_concept_set_version_asset_id. Depending on timing, the request then fails with "Error during saving version" after VersionService has retried the failing insert ten times, or with an optimistic locking error when the update of the concept set finds the row gone. This is one way to get the error reported in #2430, not necessarily the steps of that report. Load the concept set with a pessimistic write lock (FOR NO KEY UPDATE on PostgreSQL). A concurrent delete now waits until the update has finished, and the version insert in the inner transaction is not blocked by it. If the delete wins, the update returns 404. Concurrent renames of one concept set now wait for each other instead of racing on the version number. Add ConceptSetConcurrentSaveTest: a rename racing with a delete, and concurrent renames and item saves of one concept set (this one passes without the lock and checks that the lock does not deadlock). --- .../conceptset/ConceptSetRepository.java | 10 ++ .../webapi/conceptset/ConceptSetService.java | 2 +- .../ConceptSetConcurrentSaveTest.java | 132 ++++++++++++++++++ 3 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 src/test/java/org/ohdsi/webapi/conceptset/ConceptSetConcurrentSaveTest.java diff --git a/src/main/java/org/ohdsi/webapi/conceptset/ConceptSetRepository.java b/src/main/java/org/ohdsi/webapi/conceptset/ConceptSetRepository.java index d6842029dc..8081ceb843 100644 --- a/src/main/java/org/ohdsi/webapi/conceptset/ConceptSetRepository.java +++ b/src/main/java/org/ohdsi/webapi/conceptset/ConceptSetRepository.java @@ -15,8 +15,10 @@ */ package org.ohdsi.webapi.conceptset; +import jakarta.persistence.LockModeType; import org.ohdsi.webapi.tag.domain.TagType; import org.ohdsi.webapi.tag.dto.TagDTO; +import org.springframework.data.jpa.repository.Lock; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.query.Param; @@ -49,6 +51,14 @@ public interface ConceptSetRepository extends CrudRepository findAllByNameStartsWith(String pattern); Optional findByName(String name); + + /** + * Loads the concept set and locks its row until the current transaction ends, so a concurrent + * delete waits instead of removing the row between the load and the version insert. + */ + @Lock(LockModeType.PESSIMISTIC_WRITE) + @Query("SELECT cs FROM ConceptSet cs WHERE cs.id = :conceptSetId") + Optional findByIdForUpdate(@Param("conceptSetId") Integer conceptSetId); @Query("SELECT DISTINCT cs FROM ConceptSet cs JOIN FETCH cs.tags t WHERE lower(t.name) in :tagNames") List findByTags(@Param("tagNames") List tagNames); diff --git a/src/main/java/org/ohdsi/webapi/conceptset/ConceptSetService.java b/src/main/java/org/ohdsi/webapi/conceptset/ConceptSetService.java index 36f68d9f8d..9794193ee7 100644 --- a/src/main/java/org/ohdsi/webapi/conceptset/ConceptSetService.java +++ b/src/main/java/org/ohdsi/webapi/conceptset/ConceptSetService.java @@ -576,7 +576,7 @@ public ConceptSetDTO updateConceptSet( @PathVariable("id") final int id, @RequestBody ConceptSetDTO conceptSetDTO) { - ConceptSet updated = getConceptSetRepository().findById(id) + ConceptSet updated = getConceptSetRepository().findByIdForUpdate(id) .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, String.format("Concept Set with id = %d does not exist.", id))); diff --git a/src/test/java/org/ohdsi/webapi/conceptset/ConceptSetConcurrentSaveTest.java b/src/test/java/org/ohdsi/webapi/conceptset/ConceptSetConcurrentSaveTest.java new file mode 100644 index 0000000000..6ced740847 --- /dev/null +++ b/src/test/java/org/ohdsi/webapi/conceptset/ConceptSetConcurrentSaveTest.java @@ -0,0 +1,132 @@ +package org.ohdsi.webapi.conceptset; + +import org.junit.After; +import org.junit.Test; +import org.ohdsi.webapi.AbstractDatabaseTest; +import org.ohdsi.webapi.service.dto.ConceptSetDTO; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.security.core.context.SecurityContext; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.web.server.ResponseStatusException; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.CyclicBarrier; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.function.BiConsumer; + +import static org.junit.Assert.assertTrue; + +public class ConceptSetConcurrentSaveTest extends AbstractDatabaseTest { + + @Autowired + private ConceptSetService service; + + @Autowired + private ConceptSetRepository conceptSetRepository; + + private final List createdIds = new CopyOnWriteArrayList<>(); + + @After + public void cleanUp() { + for (Integer id : createdIds) { + conceptSetRepository.findById(id).ifPresent(conceptSetRepository::delete); + } + createdIds.clear(); + } + + private ConceptSetDTO create(String name) { + ConceptSetDTO dto = new ConceptSetDTO(); + dto.setName(name); + ConceptSetDTO created = service.createConceptSet(dto); + createdIds.add(created.getId()); + return created; + } + + private List runAll(int threads, int rounds, BiConsumer body) throws Exception { + SecurityContext ctx = SecurityContextHolder.getContext(); + ExecutorService pool = Executors.newFixedThreadPool(threads); + try { + CyclicBarrier barrier = new CyclicBarrier(threads); + List> futures = new ArrayList<>(); + List errors = new CopyOnWriteArrayList<>(); + for (int t = 0; t < threads; t++) { + final int tid = t; + futures.add(pool.submit(() -> { + SecurityContextHolder.setContext(ctx); + for (int r = 0; r < rounds; r++) { + try { + barrier.await(30, TimeUnit.SECONDS); + body.accept(tid, r); + } catch (Throwable e) { + errors.add(e); + barrier.reset(); + } + } + })); + } + for (Future f : futures) { + f.get(300, TimeUnit.SECONDS); + } + return errors; + } finally { + pool.shutdownNow(); + } + } + + /** + * Several users rename the same concept set while another request replaces its items. This passes + * without the row lock as well. It checks that the lock does not deadlock or fail concurrent saves. + */ + @Test + public void concurrentRenameAndItemSave() throws Exception { + ConceptSetDTO base = create("base"); + List errors = runAll(4, 50, (tid, r) -> { + if (tid == 0) { + service.saveConceptSetItems(base.getId(), new ConceptSetItem[0]); + } else { + ConceptSetDTO dto = new ConceptSetDTO(); + dto.setId(base.getId()); + dto.setName("rename-" + tid + "-" + r); + service.updateConceptSet(base.getId(), dto); + } + }); + errors.forEach(Throwable::printStackTrace); + assertTrue("errors: " + errors.size(), errors.isEmpty()); + } + + /** + * One request deletes the concept set while another one renames it. The rename either wins or fails + * with 404, it must not fail while saving the version of a concept set that was just deleted. + */ + @Test + public void renameWhileDeleting() throws Exception { + List unexpected = new CopyOnWriteArrayList<>(); + for (int i = 0; i < 40; i++) { + ConceptSetDTO cs = create("victim-" + i); + List errors = runAll(2, 1, (tid, r) -> { + if (tid == 0) { + service.deleteConceptSet(cs.getId()); + } else { + ConceptSetDTO dto = new ConceptSetDTO(); + dto.setId(cs.getId()); + dto.setName("renamed"); + service.updateConceptSet(cs.getId(), dto); + } + }); + for (Throwable e : errors) { + if (!(e instanceof ResponseStatusException) + || ((ResponseStatusException) e).getStatusCode() != HttpStatus.NOT_FOUND) { + unexpected.add(e); + } + } + } + unexpected.forEach(t -> System.out.println("RENAME-DELETE ERROR: " + t)); + assertTrue("unexpected errors: " + unexpected.size(), unexpected.isEmpty()); + } +} From ad89547b3cf57bcc49b1b0c9b7e6af2a083a5c5f Mon Sep 17 00:00:00 2001 From: Kostiantyn Dementiev Date: Mon, 16 Nov 2026 00:41:03 +0100 Subject: [PATCH 2/2] Minor refactoring --- .../ohdsi/webapi/conceptset/ConceptSetRepository.java | 5 +---- .../conceptset/ConceptSetConcurrentSaveTest.java | 10 ++-------- 2 files changed, 3 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/ohdsi/webapi/conceptset/ConceptSetRepository.java b/src/main/java/org/ohdsi/webapi/conceptset/ConceptSetRepository.java index 8081ceb843..5f1b9a9619 100644 --- a/src/main/java/org/ohdsi/webapi/conceptset/ConceptSetRepository.java +++ b/src/main/java/org/ohdsi/webapi/conceptset/ConceptSetRepository.java @@ -52,10 +52,7 @@ public interface ConceptSetRepository extends CrudRepository findByName(String name); - /** - * Loads the concept set and locks its row until the current transaction ends, so a concurrent - * delete waits instead of removing the row between the load and the version insert. - */ + /** Loads the concept set and locks its row until the transaction ends. */ @Lock(LockModeType.PESSIMISTIC_WRITE) @Query("SELECT cs FROM ConceptSet cs WHERE cs.id = :conceptSetId") Optional findByIdForUpdate(@Param("conceptSetId") Integer conceptSetId); diff --git a/src/test/java/org/ohdsi/webapi/conceptset/ConceptSetConcurrentSaveTest.java b/src/test/java/org/ohdsi/webapi/conceptset/ConceptSetConcurrentSaveTest.java index 6ced740847..e9f72cb2dd 100644 --- a/src/test/java/org/ohdsi/webapi/conceptset/ConceptSetConcurrentSaveTest.java +++ b/src/test/java/org/ohdsi/webapi/conceptset/ConceptSetConcurrentSaveTest.java @@ -79,10 +79,7 @@ private List runAll(int threads, int rounds, BiConsumer unexpected = new CopyOnWriteArrayList<>();