Skip to content

Commit

Permalink
Closes #2599 - add locking to Query to enable adapter clustering
Browse files Browse the repository at this point in the history
  • Loading branch information
ryzheboka committed Aug 2, 2024
1 parent 31100a5 commit aa72688
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 8 deletions.
6 changes: 6 additions & 0 deletions lib/taskana-core-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@
<version>${version.equalsverifier}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>pro.taskana</groupId>
<artifactId>taskana-common-test</artifactId>
<version>8.0.2-SNAPSHOT</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@
import static pro.taskana.testapi.DefaultTestEntities.defaultTestObjectReference;
import static pro.taskana.testapi.DefaultTestEntities.defaultTestWorkbasket;

import java.security.PrivilegedAction;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.security.auth.Subject;
import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DynamicTest;
Expand All @@ -26,8 +32,13 @@
import pro.taskana.common.api.IntInterval;
import pro.taskana.common.api.KeyDomain;
import pro.taskana.common.api.TimeInterval;
import pro.taskana.common.api.exceptions.SystemException;
import pro.taskana.common.api.security.CurrentUserContext;
import pro.taskana.common.api.security.UserPrincipal;
import pro.taskana.common.internal.InternalTaskanaEngine;
import pro.taskana.common.internal.util.CheckedConsumer;
import pro.taskana.common.internal.util.Pair;
import pro.taskana.common.test.util.ParallelThreadHelper;
import pro.taskana.task.api.CallbackState;
import pro.taskana.task.api.TaskCustomField;
import pro.taskana.task.api.TaskCustomIntField;
Expand All @@ -54,6 +65,7 @@
class TaskQueryImplAccTest {

@TaskanaInject TaskService taskService;
@TaskanaInject InternalTaskanaEngine internalTaskanaEngine;
@TaskanaInject WorkbasketService workbasketService;
@TaskanaInject CurrentUserContext currentUserContext;
@TaskanaInject ClassificationService classificationService;
Expand Down Expand Up @@ -98,6 +110,91 @@ private void persistPermission(WorkbasketSummary workbasketSummary) throws Excep
.buildAndStore(workbasketService, "businessadmin");
}

@Nested
@TestInstance(Lifecycle.PER_CLASS)
class LockResultsEqualsTest {
private static final Integer LOCK_RESULTS_EQUALS = 2;
WorkbasketSummary wb1;
TaskSummary taskSummary1;
TaskSummary taskSummary2;
TaskSummary taskSummary3;
TaskSummary taskSummary4;

@WithAccessId(user = "user-1-1")
@BeforeAll
void setup() throws Exception {
wb1 = createWorkbasketWithPermission();

taskSummary1 = taskInWorkbasket(wb1).state(TaskState.READY)
.buildAndStoreAsSummary(taskService);
taskSummary2 = taskInWorkbasket(wb1).state(TaskState.READY)
.buildAndStoreAsSummary(taskService);
taskSummary3 =
taskInWorkbasket(wb1).state(TaskState.READY)
.buildAndStoreAsSummary(taskService);
taskSummary4 = taskInWorkbasket(wb1).state(TaskState.READY)
.buildAndStoreAsSummary(taskService);

}

@Test
void should_ReturnDifferentTasks_For_LockResultsEqualsTwo() throws Exception {
if (System.getenv("DB") != null && (System.getenv("DB").equals("POSTGRES")
|| System.getenv("DB").equals("DB2"))) {

List<TaskSummary> returnedTasks = Collections.synchronizedList(new ArrayList<>());
List<String> accessIds =
Collections.synchronizedList(
Stream.of("admin", "admin")
.collect(Collectors.toList()));

ParallelThreadHelper.runInThread(
getRunnableTest(returnedTasks, accessIds), accessIds.size());

assertThat(returnedTasks)
.extracting(TaskSummary::getId)
.containsExactlyInAnyOrder(
taskSummary1.getId(), taskSummary2.getId(), taskSummary3.getId(),
taskSummary4.getId());
}
}

private Runnable getRunnableTest(List<TaskSummary> listedTasks, List<String> accessIds) {
return () -> {
Subject subject = new Subject();
subject.getPrincipals().add(new UserPrincipal(accessIds.remove(0)));

Consumer<TaskService> consumer =
CheckedConsumer.wrap(
taskService -> {
internalTaskanaEngine.executeInDatabaseConnection(() -> {
List<TaskSummary> results = taskService
.createTaskQuery()
.workbasketIdIn(wb1.getId())
.stateIn(TaskState.READY)
.lockResultsEquals(LOCK_RESULTS_EQUALS).list();
listedTasks.addAll(results);
for (TaskSummary task : results) {
try {
taskService.claim(task.getId());
} catch (Exception e) {
throw new SystemException(e.getMessage());
}
}
});
});


PrivilegedAction<Void> action =
() -> {
consumer.accept(taskService);
return null;
};
Subject.doAs(subject, action);
};
}
}

@Nested
@TestInstance(Lifecycle.PER_CLASS)
class PermissionsTest {
Expand Down
10 changes: 10 additions & 0 deletions lib/taskana-core/src/main/java/pro/taskana/task/api/TaskQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -1905,4 +1905,14 @@ TaskQuery orderByCustomIntAttribute(
* @return the query
*/
TaskQuery orderByWorkbasketName(SortDirection sortDirection);

/**
* This method locks the returned rows until the end of the transaction using for update.
*
* @param lockResults determines the number of returned and locked results;
* if zero, no results are locked, but the number of returned results is not
* limited
* @return the query
*/
TaskQuery lockResultsEquals(Integer lockResults);
}
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ public class TaskQueryImpl implements TaskQuery {
private CallbackState[] callbackStateNotIn;
private WildcardSearchField[] wildcardSearchFieldIn;
private String wildcardSearchValueLike;
private Integer lockResults;

TaskQueryImpl(InternalTaskanaEngine taskanaEngine) {
this.taskanaEngine = taskanaEngine;
Expand All @@ -345,6 +346,7 @@ public class TaskQueryImpl implements TaskQuery {
this.orderByInner = new ArrayList<>();
this.filterByAccessIdIn = true;
this.withoutAttachment = false;
this.lockResults = 0;
this.joinWithUserInfo = taskanaEngine.getEngine().getConfiguration().isAddAdditionalUserInfo();
}

Expand Down Expand Up @@ -2116,11 +2118,17 @@ public TaskQuery selectAndClaimEquals(boolean selectAndClaim) {
return this;
}

public TaskQuery lockResultsEquals(Integer lockResults) {
this.lockResults = lockResults;
return this;
}

// optimized query for db2 can't be used for now in case of selectAndClaim because of temporary
// tables and the "for update" clause clashing in db2
private String getLinkToMapperScript() {
if (DB.DB2 == getDB()
&& !selectAndClaim
&& lockResults == 0
&& taskanaEngine.getEngine().getConfiguration().isUseSpecificDb2Taskquery()) {
return LINK_TO_MAPPER_DB2;
} else if (selectAndClaim && DB.ORACLE == getDB()) {
Expand Down Expand Up @@ -2810,8 +2818,8 @@ public String toString() {
+ Arrays.toString(wildcardSearchFieldIn)
+ ", wildcardSearchValueLike="
+ wildcardSearchValueLike
+ ", joinWithUserInfo="
+ joinWithUserInfo
+ ", lockResults="
+ lockResults
+ "]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,18 @@ public static String queryTaskSummaries() {
+ "<if test='selectAndClaim == true'> "
+ "FETCH FIRST ROW ONLY FOR UPDATE "
+ "</if>"
+ "<if test=\"_databaseId == 'db2' and selectAndClaim \">WITH RS USE "
+ "<if test='lockResults and lockResults != 0'> "
+ "FETCH FIRST ${lockResults} ROWS ONLY FOR UPDATE "
+ "<if test=\"_databaseId == 'postgres'\">"
+ "SKIP LOCKED "
+ "</if>"
+ "<if test=\"_databaseId == 'db2'\">"
+ "SKIP LOCKED DATA "
+ "</if>"
+ "</if>"
+ "<if test=\"_databaseId == 'db2' and (selectAndClaim or lockResults != 0) \">WITH RS USE "
+ "AND KEEP UPDATE LOCKS </if>"
+ "<if test=\"_databaseId == 'db2' and !selectAndClaim \">WITH UR </if>"
+ "<if test=\"_databaseId == 'db2' and !selectAndClaim and lockResults==0 \">WITH UR </if>"
+ CLOSING_SCRIPT_TAG;
}

Expand Down Expand Up @@ -143,10 +152,7 @@ public static String queryTaskSummariesDb2() {
+ "<if test='!orderByOuter.isEmpty()'>"
+ "ORDER BY <foreach item='item' collection='orderByOuter' separator=',' >${item}</foreach>"
+ "</if> "
+ "<if test='selectAndClaim == true'>"
+ "FETCH FIRST ROW ONLY FOR UPDATE WITH RS USE AND KEEP UPDATE LOCKS"
+ "</if>"
+ "<if test='selectAndClaim == false'> with UR</if>"
+ "with UR g"
+ CLOSING_SCRIPT_TAG;
}

Expand Down

0 comments on commit aa72688

Please sign in to comment.