@Test
public void fail_to_cancel_if_in_progress() {
submit(CeTaskTypes.REPORT, newComponent(randomAlphabetic(11)));
- CeQueueDto ceQueueDto = db.getDbClient().ceQueueDao().peek(session, WORKER_UUID, false).get();
+ CeQueueDto ceQueueDto = db.getDbClient().ceQueueDao().peek(session, WORKER_UUID, false, false).get();
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage(startsWith("Task is in progress and can't be canceled"));
CeTask pendingTask1 = submit(CeTaskTypes.REPORT, newComponent(randomAlphabetic(12)));
CeTask pendingTask2 = submit(CeTaskTypes.REPORT, newComponent(randomAlphabetic(12)));
- db.getDbClient().ceQueueDao().peek(session, WORKER_UUID, false);
+ db.getDbClient().ceQueueDao().peek(session, WORKER_UUID, false, false);
int canceledCount = underTest.cancelAll();
assertThat(canceledCount).isEqualTo(2);
@Test
public void pauseWorkers_marks_workers_as_pausing_if_some_tasks_in_progress() {
submit(CeTaskTypes.REPORT, newComponent(randomAlphabetic(12)));
- db.getDbClient().ceQueueDao().peek(session, WORKER_UUID, false);
+ db.getDbClient().ceQueueDao().peek(session, WORKER_UUID, false, false);
// task is in-progress
assertThat(underTest.getWorkersPauseStatus()).isEqualTo(CeQueue.WorkersPauseStatus.RESUMED);
@Test
public void resumeWorkers_resumes_pausing_workers() {
submit(CeTaskTypes.REPORT, newComponent(randomAlphabetic(12)));
- db.getDbClient().ceQueueDao().peek(session, WORKER_UUID, false);
+ db.getDbClient().ceQueueDao().peek(session, WORKER_UUID, false, false);
// task is in-progress
underTest.pauseWorkers();
@Test
public void fail_in_progress_task() {
CeTask task = submit(CeTaskTypes.REPORT, newComponent(randomAlphabetic(12)));
- CeQueueDto queueDto = db.getDbClient().ceQueueDao().peek(db.getSession(), WORKER_UUID, false).get();
+ CeQueueDto queueDto = db.getDbClient().ceQueueDao().peek(db.getSession(), WORKER_UUID, false, false).get();
underTest.fail(db.getSession(), queueDto, "TIMEOUT", "Failed on timeout");
* Does not return anything if workers are paused or being paused (see {@link #getWorkersPauseStatus()}.
*
* @param excludeIndexationJob change the underlying request to exclude indexation tasks.
+ * @param excludeViewRefresh change the underlying request to exclude portfolios (but still include APP)
*
* <p>Only a single task can be peeked by project.</p>
*
* <p>Tasks which have been executed twice already but are still {@link org.sonar.db.ce.CeQueueDto.Status#PENDING}
* are ignored</p>
*/
- Optional<CeTask> peek(String workerUuid, boolean excludeIndexationJob);
+ Optional<CeTask> peek(String workerUuid, boolean excludeIndexationJob, boolean excludeViewRefresh);
/**
* Removes a task from the queue and registers it to past activities. This method
}
@Override
- public Optional<CeTask> peek(String workerUuid, boolean excludeIndexationJob) {
+ public Optional<CeTask> peek(String workerUuid, boolean excludeIndexationJob, boolean excludeViewRefresh) {
requireNonNull(workerUuid, "workerUuid can't be null");
if (computeEngineStatus.getStatus() != ComputeEngineStatus.Status.STARTED || getWorkersPauseStatus() != WorkersPauseStatus.RESUMED) {
dbSession.commit();
LOG.debug("{} in progress tasks reset for worker uuid {}", i, workerUuid);
}
- Optional<CeQueueDto> opt = ceQueueDao.peek(dbSession, workerUuid, excludeIndexationJob);
+ Optional<CeQueueDto> opt = ceQueueDao.peek(dbSession, workerUuid, excludeIndexationJob, excludeViewRefresh);
if (!opt.isPresent()) {
return Optional.empty();
}
if (indexationTaskLookupEnabled) {
return tryAndFindTaskToExecuteIncludingIndexation();
} else {
- return queue.peek(uuid, true);
+ return queue.peek(uuid, true, false);
}
} catch (Exception e) {
LOG.error("Failed to pop the queue of analysis reports", e);
private Optional<CeTask> tryAndFindTaskToExecuteIncludingIndexation() {
excludeIndexationJob = !excludeIndexationJob;
- Optional<CeTask> peek = queue.peek(uuid, excludeIndexationJob);
+ Optional<CeTask> peek = queue.peek(uuid, excludeIndexationJob, true);
if (peek.isPresent()) {
return peek;
}
if (excludeIndexationJob) {
- peek = queue.peek(uuid, false);
+ peek = queue.peek(uuid, false, true);
if (peek.isPresent()) {
return peek;
}
expectedException.expect(NullPointerException.class);
expectedException.expectMessage("workerUuid can't be null");
- underTest.peek(null, false);
+ underTest.peek(null, false, false);
}
@Test
public void test_remove() {
CeTask task = submit(CeTaskTypes.REPORT, newProjectDto("PROJECT_1"));
- Optional<CeTask> peek = underTest.peek(WORKER_UUID_1, false);
+ Optional<CeTask> peek = underTest.peek(WORKER_UUID_1, false, false);
underTest.remove(peek.get(), CeActivityDto.Status.SUCCESS, null, null);
// queue is empty
assertThat(db.getDbClient().ceQueueDao().selectByUuid(db.getSession(), task.getUuid()).isPresent()).isFalse();
- assertThat(underTest.peek(WORKER_UUID_2, false).isPresent()).isFalse();
+ assertThat(underTest.peek(WORKER_UUID_2, false, false).isPresent()).isFalse();
// available in history
Optional<CeActivityDto> history = db.getDbClient().ceActivityDao().selectByUuid(db.getSession(), task.getUuid());
@Test
public void remove_does_not_set_analysisUuid_in_CeActivity_when_CeTaskResult_has_no_analysis_uuid() {
CeTask task = submit(CeTaskTypes.REPORT, newProjectDto("PROJECT_1"));
- Optional<CeTask> peek = underTest.peek(WORKER_UUID_1, false);
+ Optional<CeTask> peek = underTest.peek(WORKER_UUID_1, false, false);
underTest.remove(peek.get(), CeActivityDto.Status.SUCCESS, newTaskResult(null), null);
// available in history
public void remove_sets_analysisUuid_in_CeActivity_when_CeTaskResult_has_analysis_uuid() {
CeTask task = submit(CeTaskTypes.REPORT, newProjectDto("PROJECT_1"));
- Optional<CeTask> peek = underTest.peek(WORKER_UUID_2, false);
+ Optional<CeTask> peek = underTest.peek(WORKER_UUID_2, false, false);
underTest.remove(peek.get(), CeActivityDto.Status.SUCCESS, newTaskResult(AN_ANALYSIS_UUID), null);
// available in history
Throwable error = new NullPointerException("Fake NPE to test persistence to DB");
CeTask task = submit(CeTaskTypes.REPORT, newProjectDto("PROJECT_1"));
- Optional<CeTask> peek = underTest.peek(WORKER_UUID_1, false);
+ Optional<CeTask> peek = underTest.peek(WORKER_UUID_1, false, false);
underTest.remove(peek.get(), CeActivityDto.Status.FAILED, null, error);
Optional<CeActivityDto> activityDto = db.getDbClient().ceActivityDao().selectByUuid(session, task.getUuid());
Throwable error = new TypedExceptionImpl("aType", "aMessage");
CeTask task = submit(CeTaskTypes.REPORT, newProjectDto("PROJECT_1"));
- Optional<CeTask> peek = underTest.peek(WORKER_UUID_1, false);
+ Optional<CeTask> peek = underTest.peek(WORKER_UUID_1, false, false);
underTest.remove(peek.get(), CeActivityDto.Status.FAILED, null, error);
CeActivityDto activityDto = db.getDbClient().ceActivityDao().selectByUuid(session, task.getUuid()).get();
public void test_peek() {
CeTask task = submit(CeTaskTypes.REPORT, newProjectDto("PROJECT_1"));
- Optional<CeTask> peek = underTest.peek(WORKER_UUID_1, false);
+ Optional<CeTask> peek = underTest.peek(WORKER_UUID_1, false, false);
assertThat(peek.isPresent()).isTrue();
assertThat(peek.get().getUuid()).isEqualTo(task.getUuid());
assertThat(peek.get().getType()).isEqualTo(CeTaskTypes.REPORT);
assertThat(peek.get().getMainComponent()).contains(peek.get().getComponent().get());
// no more pending tasks
- peek = underTest.peek(WORKER_UUID_2, false);
+ peek = underTest.peek(WORKER_UUID_2, false, false);
assertThat(peek.isPresent()).isFalse();
}
ComponentDto branch = db.components().insertProjectBranch(project);
CeTask task = submit(CeTaskTypes.REPORT, branch);
- Optional<CeTask> peek = underTest.peek(WORKER_UUID_1, false);
+ Optional<CeTask> peek = underTest.peek(WORKER_UUID_1, false, false);
assertThat(peek.isPresent()).isTrue();
assertThat(peek.get().getUuid()).isEqualTo(task.getUuid());
assertThat(peek.get().getType()).isEqualTo(CeTaskTypes.REPORT);
assertThat(peek.get().getMainComponent()).contains(new CeTask.Component(project.uuid(), project.getDbKey(), project.name()));
// no more pending tasks
- peek = underTest.peek(WORKER_UUID_2, false);
+ peek = underTest.peek(WORKER_UUID_2, false, false);
assertThat(peek.isPresent()).isFalse();
}
CeTask task = submit(CeTaskTypes.REPORT, newProjectDto("PROJECT_1"));
underTest.pauseWorkers();
- Optional<CeTask> peek = underTest.peek(WORKER_UUID_1, false);
+ Optional<CeTask> peek = underTest.peek(WORKER_UUID_1, false, false);
assertThat(peek).isEmpty();
underTest.resumeWorkers();
- peek = underTest.peek(WORKER_UUID_1, false);
+ peek = underTest.peek(WORKER_UUID_1, false, false);
assertThat(peek).isPresent();
assertThat(peek.get().getUuid()).isEqualTo(task.getUuid());
}
makeInProgress(dto, "foo");
db.commit();
- assertThat(underTest.peek(WORKER_UUID_1, false)).isEmpty();
+ assertThat(underTest.peek(WORKER_UUID_1, false, false)).isEmpty();
}
@Test
submit(CeTaskTypes.REPORT, newProjectDto("PROJECT_1"));
when(computeEngineStatus.getStatus()).thenReturn(STOPPING);
- Optional<CeTask> peek = underTest.peek(WORKER_UUID_1, false);
+ Optional<CeTask> peek = underTest.peek(WORKER_UUID_1, false, false);
assertThat(peek.isPresent()).isFalse();
}
.setStatus(CeQueueDto.Status.PENDING));
db.commit();
- assertThat(underTest.peek(WORKER_UUID_1, false).get().getUuid()).isEqualTo("uuid");
+ assertThat(underTest.peek(WORKER_UUID_1, false, false).get().getUuid()).isEqualTo("uuid");
}
@Test
CeQueueDto u1 = insertPending("u1");// will be picked-because older than any of the reset ones
CeQueueDto u2 = insertInProgress("u2", WORKER_UUID_1);// will be reset
- assertThat(underTest.peek(WORKER_UUID_1, false).get().getUuid()).isEqualTo("u0");
+ assertThat(underTest.peek(WORKER_UUID_1, false, false).get().getUuid()).isEqualTo("u0");
verifyUnmodifiedTask(u1);
verifyResetTask(u2);
CeQueueDto u3 = insertInProgress("u3", WORKER_UUID_1);
CeQueueDto u4 = insertInProgress("u4", WORKER_UUID_2);
- assertThat(underTest.peek(WORKER_UUID_1, false).get().getUuid()).isEqualTo("u0");
+ assertThat(underTest.peek(WORKER_UUID_1, false, false).get().getUuid()).isEqualTo("u0");
verifyResetTask(u1);
verifyUnmodifiedTask(u2);
@Test
public void fail_to_cancel_if_in_progress() {
CeTask task = submit(CeTaskTypes.REPORT, newProjectDto("PROJECT_1"));
- underTest.peek(WORKER_UUID_2, false);
+ underTest.peek(WORKER_UUID_2, false, false);
CeQueueDto queueDto = db.getDbClient().ceQueueDao().selectByUuid(db.getSession(), task.getUuid()).get();
expectedException.expect(IllegalStateException.class);
CeTask inProgressTask = submit(CeTaskTypes.REPORT, newProjectDto("PROJECT_1"));
CeTask pendingTask1 = submit(CeTaskTypes.REPORT, newProjectDto("PROJECT_2"));
CeTask pendingTask2 = submit(CeTaskTypes.REPORT, newProjectDto("PROJECT_3"));
- underTest.peek(WORKER_UUID_2, false);
+ underTest.peek(WORKER_UUID_2, false, false);
int canceledCount = underTest.cancelAll();
assertThat(canceledCount).isEqualTo(2);
*/
package org.sonar.ce.taskprocessor;
+import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Test
public void no_pending_tasks_in_queue() throws Exception {
- when(queue.peek(anyString(), anyBoolean())).thenReturn(Optional.empty());
+ when(queue.peek(anyString(), anyBoolean(), anyBoolean())).thenReturn(Optional.empty());
assertThat(underTest.call()).isEqualTo(NO_TASK);
@Test
public void no_pending_tasks_in_queue_without_listener() throws Exception {
- when(queue.peek(anyString(), anyBoolean())).thenReturn(Optional.empty());
+ when(queue.peek(anyString(), anyBoolean(), anyBoolean())).thenReturn(Optional.empty());
assertThat(underTestNoListener.call()).isEqualTo(NO_TASK);
public void fail_when_no_CeTaskProcessor_is_found_in_repository() throws Exception {
CeTask task = createCeTask(null);
taskProcessorRepository.setNoProcessorForTask(CeTaskTypes.REPORT);
- when(queue.peek(anyString(), anyBoolean())).thenReturn(Optional.of(task));
+ when(queue.peek(anyString(), anyBoolean(), anyBoolean())).thenReturn(Optional.of(task));
assertThat(underTest.call()).isEqualTo(TASK_PROCESSED);
public void fail_when_no_CeTaskProcessor_is_found_in_repository_without_listener() throws Exception {
CeTask task = createCeTask(null);
taskProcessorRepository.setNoProcessorForTask(CeTaskTypes.REPORT);
- when(queue.peek(anyString(), anyBoolean())).thenReturn(Optional.of(task));
+ when(queue.peek(anyString(), anyBoolean(), anyBoolean())).thenReturn(Optional.of(task));
assertThat(underTestNoListener.call()).isEqualTo(TASK_PROCESSED);
public void peek_and_process_task() throws Exception {
CeTask task = createCeTask(null);
taskProcessorRepository.setProcessorForTask(task.getType(), taskProcessor);
- when(queue.peek(anyString(), anyBoolean())).thenReturn(Optional.of(task));
+ when(queue.peek(anyString(), anyBoolean(), anyBoolean())).thenReturn(Optional.of(task));
assertThat(underTest.call()).isEqualTo(TASK_PROCESSED);
public void peek_and_process_task_without_listeners() throws Exception {
CeTask task = createCeTask(null);
taskProcessorRepository.setProcessorForTask(task.getType(), taskProcessor);
- when(queue.peek(anyString(), anyBoolean())).thenReturn(Optional.of(task));
+ when(queue.peek(anyString(), anyBoolean(), anyBoolean())).thenReturn(Optional.of(task));
assertThat(underTestNoListener.call()).isEqualTo(TASK_PROCESSED);
@Test
public void fail_to_process_task() throws Exception {
CeTask task = createCeTask(null);
- when(queue.peek(anyString(), anyBoolean())).thenReturn(Optional.of(task));
+ when(queue.peek(anyString(), anyBoolean(), anyBoolean())).thenReturn(Optional.of(task));
taskProcessorRepository.setProcessorForTask(task.getType(), taskProcessor);
Throwable error = makeTaskProcessorFail(task);
@Test
public void fail_to_process_task_without_listeners() throws Exception {
CeTask task = createCeTask(null);
- when(queue.peek(anyString(), anyBoolean())).thenReturn(Optional.of(task));
+ when(queue.peek(anyString(), anyBoolean(), anyBoolean())).thenReturn(Optional.of(task));
taskProcessorRepository.setProcessorForTask(task.getType(), taskProcessor);
Throwable error = makeTaskProcessorFail(task);
@Test
public void log_task_characteristics() throws Exception {
- when(queue.peek(anyString(), anyBoolean())).thenReturn(Optional.of(createCeTask(null, "pullRequest", "123", "branch", "foo")));
+ when(queue.peek(anyString(), anyBoolean(), anyBoolean())).thenReturn(Optional.of(createCeTask(null, "pullRequest", "123", "branch", "foo")));
taskProcessorRepository.setProcessorForTask(CeTaskTypes.REPORT, taskProcessor);
underTest.call();
@Test
public void do_not_log_submitter_param_if_anonymous_and_success() throws Exception {
- when(queue.peek(anyString(), anyBoolean())).thenReturn(Optional.of(createCeTask(null)));
+ when(queue.peek(anyString(), anyBoolean(), anyBoolean())).thenReturn(Optional.of(createCeTask(null)));
taskProcessorRepository.setProcessorForTask(CeTaskTypes.REPORT, taskProcessor);
underTest.call();
@Test
public void do_not_log_submitter_param_if_anonymous_and_error() throws Exception {
CeTask ceTask = createCeTask(null);
- when(queue.peek(anyString(), anyBoolean())).thenReturn(Optional.of(ceTask));
+ when(queue.peek(anyString(), anyBoolean(), anyBoolean())).thenReturn(Optional.of(ceTask));
taskProcessorRepository.setProcessorForTask(ceTask.getType(), taskProcessor);
makeTaskProcessorFail(ceTask);
@Test
public void log_submitter_login_if_authenticated_and_success() throws Exception {
UserDto userDto = insertRandomUser();
- when(queue.peek(anyString(), anyBoolean())).thenReturn(Optional.of(createCeTask(toTaskSubmitter(userDto))));
+ when(queue.peek(anyString(), anyBoolean(), anyBoolean())).thenReturn(Optional.of(createCeTask(toTaskSubmitter(userDto))));
taskProcessorRepository.setProcessorForTask(CeTaskTypes.REPORT, taskProcessor);
underTest.call();
@Test
public void log_submitterUuid_if_user_matching_submitterUuid_can_not_be_found() throws Exception {
- when(queue.peek(anyString(), anyBoolean())).thenReturn(Optional.of(createCeTask(new CeTask.User("UUID_USER", null))));
+ when(queue.peek(anyString(), anyBoolean(), anyBoolean())).thenReturn(Optional.of(createCeTask(new CeTask.User("UUID_USER", null))));
taskProcessorRepository.setProcessorForTask(CeTaskTypes.REPORT, taskProcessor);
underTest.call();
public void display_submitterLogin_in_logs_when_set_in_case_of_error() throws Exception {
UserDto userDto = insertRandomUser();
CeTask ceTask = createCeTask(toTaskSubmitter(userDto));
- when(queue.peek(anyString(), anyBoolean())).thenReturn(Optional.of(ceTask));
+ when(queue.peek(anyString(), anyBoolean(), anyBoolean())).thenReturn(Optional.of(ceTask));
taskProcessorRepository.setProcessorForTask(ceTask.getType(), taskProcessor);
makeTaskProcessorFail(ceTask);
public void display_start_stop_at_debug_level_for_console_if_DEBUG_is_enabled_and_task_successful() throws Exception {
logTester.setLevel(LoggerLevel.DEBUG);
- when(queue.peek(anyString(), anyBoolean())).thenReturn(Optional.of(createCeTask(submitter)));
+ when(queue.peek(anyString(), anyBoolean(), anyBoolean())).thenReturn(Optional.of(createCeTask(submitter)));
taskProcessorRepository.setProcessorForTask(CeTaskTypes.REPORT, taskProcessor);
underTest.call();
logTester.setLevel(LoggerLevel.DEBUG);
CeTask ceTask = createCeTask(submitter);
- when(queue.peek(anyString(), anyBoolean())).thenReturn(Optional.of(ceTask));
+ when(queue.peek(anyString(), anyBoolean(), anyBoolean())).thenReturn(Optional.of(ceTask));
taskProcessorRepository.setProcessorForTask(CeTaskTypes.REPORT, taskProcessor);
makeTaskProcessorFail(ceTask);
@Test
public void call_sets_and_restores_thread_name_with_information_of_worker_when_there_is_no_task_to_process() throws Exception {
String threadName = randomAlphabetic(3);
- when(queue.peek(anyString(), anyBoolean())).thenAnswer(invocation -> {
+ when(queue.peek(anyString(), anyBoolean(), anyBoolean())).thenAnswer(invocation -> {
assertThat(Thread.currentThread().getName())
.isEqualTo("Worker " + randomOrdinal + " (UUID=" + workerUuid + ") on " + threadName);
return Optional.empty();
@Test
public void call_sets_and_restores_thread_name_with_information_of_worker_when_a_task_is_processed() throws Exception {
String threadName = randomAlphabetic(3);
- when(queue.peek(anyString(), anyBoolean())).thenAnswer(invocation -> {
+ when(queue.peek(anyString(), anyBoolean(), anyBoolean())).thenAnswer(invocation -> {
assertThat(Thread.currentThread().getName())
.isEqualTo("Worker " + randomOrdinal + " (UUID=" + workerUuid + ") on " + threadName);
return Optional.of(createCeTask(submitter));
public void call_sets_and_restores_thread_name_with_information_of_worker_when_an_error_occurs() throws Exception {
String threadName = randomAlphabetic(3);
CeTask ceTask = createCeTask(submitter);
- when(queue.peek(anyString(), anyBoolean())).thenAnswer(invocation -> {
+ when(queue.peek(anyString(), anyBoolean(), anyBoolean())).thenAnswer(invocation -> {
assertThat(Thread.currentThread().getName())
.isEqualTo("Worker " + randomOrdinal + " (UUID=" + workerUuid + ") on " + threadName);
return Optional.of(ceTask);
@Test
public void log_error_when_task_fails_with_not_MessageException() throws Exception {
CeTask ceTask = createCeTask(submitter);
- when(queue.peek(anyString(), anyBoolean())).thenReturn(Optional.of(ceTask));
+ when(queue.peek(anyString(), anyBoolean(), anyBoolean())).thenReturn(Optional.of(ceTask));
taskProcessorRepository.setProcessorForTask(CeTaskTypes.REPORT, taskProcessor);
makeTaskProcessorFail(ceTask);
@Test
public void do_no_log_error_when_task_fails_with_MessageException() throws Exception {
CeTask ceTask = createCeTask(submitter);
- when(queue.peek(anyString(), anyBoolean())).thenReturn(Optional.of(ceTask));
+ when(queue.peek(anyString(), anyBoolean(), anyBoolean())).thenReturn(Optional.of(ceTask));
taskProcessorRepository.setProcessorForTask(CeTaskTypes.REPORT, taskProcessor);
makeTaskProcessorFail(ceTask, MessageException.of("simulate MessageException thrown by TaskProcessor#process"));
@Test
public void do_no_log_error_when_task_fails_with_BillingValidationsException() throws Exception {
CeTask ceTask = createCeTask(submitter);
- when(queue.peek(anyString(), anyBoolean())).thenReturn(Optional.of(ceTask));
+ when(queue.peek(anyString(), anyBoolean(), anyBoolean())).thenReturn(Optional.of(ceTask));
taskProcessorRepository.setProcessorForTask(CeTaskTypes.REPORT, taskProcessor);
makeTaskProcessorFail(ceTask, new BillingValidations.BillingValidationsException("simulate MessageException thrown by TaskProcessor#process"));
@Test
public void log_error_when_task_was_successful_but_ending_state_can_not_be_persisted_to_db() throws Exception {
CeTask ceTask = createCeTask(submitter);
- when(queue.peek(anyString(), anyBoolean())).thenReturn(Optional.of(ceTask));
+ when(queue.peek(anyString(), anyBoolean(), anyBoolean())).thenReturn(Optional.of(ceTask));
taskProcessorRepository.setProcessorForTask(CeTaskTypes.REPORT, taskProcessor);
doThrow(new RuntimeException("Simulate queue#remove failing")).when(queue).remove(ceTask, CeActivityDto.Status.SUCCESS, null, null);
@Test
public void log_error_when_task_failed_and_ending_state_can_not_be_persisted_to_db() throws Exception {
CeTask ceTask = createCeTask(submitter);
- when(queue.peek(anyString(), anyBoolean())).thenReturn(Optional.of(ceTask));
+ when(queue.peek(anyString(), anyBoolean(), anyBoolean())).thenReturn(Optional.of(ceTask));
taskProcessorRepository.setProcessorForTask(CeTaskTypes.REPORT, taskProcessor);
IllegalStateException ex = makeTaskProcessorFail(ceTask);
RuntimeException runtimeException = new RuntimeException("Simulate queue#remove failing");
@Test
public void log_error_as_suppressed_when_task_failed_with_MessageException_and_ending_state_can_not_be_persisted_to_db() throws Exception {
CeTask ceTask = createCeTask(submitter);
- when(queue.peek(anyString(), anyBoolean())).thenReturn(Optional.of(ceTask));
+ when(queue.peek(anyString(), anyBoolean(), anyBoolean())).thenReturn(Optional.of(ceTask));
taskProcessorRepository.setProcessorForTask(CeTaskTypes.REPORT, taskProcessor);
MessageException ex = makeTaskProcessorFail(ceTask, MessageException.of("simulate MessageException thrown by TaskProcessor#process"));
RuntimeException runtimeException = new RuntimeException("Simulate queue#remove failing");
CountDownLatch inCallLatch = new CountDownLatch(1);
CountDownLatch assertionsDoneLatch = new CountDownLatch(1);
// mock long running peek(String) call => Thread is executing call() but not running a task
- when(queue.peek(anyString(), anyBoolean())).thenAnswer((Answer<Optional<CeTask>>) invocation -> {
+ when(queue.peek(anyString(), anyBoolean(), anyBoolean())).thenAnswer((Answer<Optional<CeTask>>) invocation -> {
inCallLatch.countDown();
try {
assertionsDoneLatch.await(10, TimeUnit.SECONDS);
String taskType = randomAlphabetic(12);
CeTask ceTask = mock(CeTask.class);
when(ceTask.getType()).thenReturn(taskType);
- when(queue.peek(anyString(), anyBoolean())).thenReturn(Optional.of(ceTask));
+ when(queue.peek(anyString(), anyBoolean(), anyBoolean())).thenReturn(Optional.of(ceTask));
taskProcessorRepository.setProcessorForTask(taskType, new SimpleCeTaskProcessor() {
@CheckForNull
@Override
assertThat(underTest.getCurrentTask()).isEmpty();
}
+ @Test
+ public void do_not_exclude_portfolio_when_indexation_task_lookup_is_disabled() throws Exception {
+ // first call with empty queue to disable indexationTaskLookupEnabled
+ when(queue.peek(anyString(), anyBoolean(), anyBoolean())).thenReturn(Optional.empty());
+ assertThat(underTest.call()).isEqualTo(NO_TASK);
+
+ ArgumentCaptor<Boolean> booleanCaptor = ArgumentCaptor.forClass(Boolean.class);
+ // following calls should not exclude portfolios
+ when(queue.peek(anyString(), anyBoolean(), anyBoolean())).thenReturn(Optional.of(createCeTask(submitter)));
+ assertThat(underTest.call()).isEqualTo(TASK_PROCESSED);
+ verify(queue, times(3)).peek(anyString(), anyBoolean(), booleanCaptor.capture());
+ assertThat(booleanCaptor.getAllValues()).containsExactly(true, true, false);
+ }
+
@Test
public void getCurrentTask_returns_empty_when_a_thread_is_currently_calling_call_but_not_executing_a_task() throws InterruptedException {
CountDownLatch inCallLatch = new CountDownLatch(1);
CountDownLatch assertionsDoneLatch = new CountDownLatch(1);
// mock long running peek(String) call => Thread is executing call() but not running a task
- when(queue.peek(anyString(), anyBoolean())).thenAnswer((Answer<Optional<CeTask>>) invocation -> {
+ when(queue.peek(anyString(), anyBoolean(), anyBoolean())).thenAnswer((Answer<Optional<CeTask>>) invocation -> {
inCallLatch.countDown();
try {
assertionsDoneLatch.await(10, TimeUnit.SECONDS);
String taskType = randomAlphabetic(12);
CeTask ceTask = mock(CeTask.class);
when(ceTask.getType()).thenReturn(taskType);
- when(queue.peek(anyString(), anyBoolean())).thenReturn(Optional.of(ceTask));
+ when(queue.peek(anyString(), anyBoolean(), anyBoolean())).thenReturn(Optional.of(ceTask));
taskProcessorRepository.setProcessorForTask(taskType, new SimpleCeTaskProcessor() {
@CheckForNull
}
private void verifyWorkerUuid() {
- verify(queue, atLeastOnce()).peek(workerUuidCaptor.capture(), anyBoolean());
+ verify(queue, atLeastOnce()).peek(workerUuidCaptor.capture(), anyBoolean(), anyBoolean());
assertThat(workerUuidCaptor.getValue()).isEqualTo(workerUuid);
}
return builder.build();
}
- public Optional<CeQueueDto> peek(DbSession session, String workerUuid, boolean excludeIndexationJob) {
- List<String> eligibles = mapper(session).selectEligibleForPeek(ONE_RESULT_PAGINATION, excludeIndexationJob);
+ public Optional<CeQueueDto> peek(DbSession session, String workerUuid, boolean excludeIndexationJob, boolean excludeViewRefresh) {
+ List<String> eligibles = mapper(session).selectEligibleForPeek(ONE_RESULT_PAGINATION, excludeIndexationJob, excludeViewRefresh);
if (eligibles.isEmpty()) {
return Optional.empty();
}
int countByQuery(@Param("query") CeTaskQuery query);
- List<String> selectEligibleForPeek(@Param("pagination") Pagination pagination, @Param("excludeIndexationJob") boolean excludeIndexationJob);
+ List<String> selectEligibleForPeek(@Param("pagination") Pagination pagination,
+ @Param("excludeIndexationJob") boolean excludeIndexationJob,
+ @Param("excludeViewRefresh") boolean excludeViewRefresh);
@CheckForNull
CeQueueDto selectByUuid(@Param("uuid") String uuid);
<sql id="sqlSelectEligibleForPeek">
from
ce_queue cq
+ <if test="excludeViewRefresh">
+ inner join components c on c.uuid = cq.main_component_uuid and c.qualifier <> 'VW'
+ </if>
where
cq.status='PENDING'
and cq.started_at is null
<sql id="orderBySelectEligibleForPeek">
order by
- created_at asc,
- uuid asc
+ cq.created_at asc,
+ cq.uuid asc
</sql>
<select id="selectPending" resultType="org.sonar.db.ce.CeQueueDto">
import java.util.Optional;
import java.util.Random;
import java.util.function.Consumer;
+import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.Nullable;
import org.junit.Rule;
import org.sonar.api.impl.utils.TestSystem2;
import org.sonar.api.utils.System2;
import org.sonar.db.DbTester;
+import org.sonar.db.component.ComponentDto;
-import static com.google.common.collect.FluentIterable.from;
import static com.google.common.collect.Lists.newArrayList;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
@Test
public void peek_none_if_no_pendings() {
- assertThat(underTest.peek(db.getSession(), WORKER_UUID_1, false).isPresent()).isFalse();
+ assertThat(underTest.peek(db.getSession(), WORKER_UUID_1, false, false).isPresent()).isFalse();
// not pending, but in progress
makeInProgress(WORKER_UUID_1, 2_232_222L, insertPending(TASK_UUID_1, MAIN_COMPONENT_UUID_1));
- assertThat(underTest.peek(db.getSession(), WORKER_UUID_1, false).isPresent()).isFalse();
+ assertThat(underTest.peek(db.getSession(), WORKER_UUID_1, false, false).isPresent()).isFalse();
}
@Test
verifyCeQueueStatuses(TASK_UUID_1, PENDING, TASK_UUID_2, PENDING);
// peek first one
- Optional<CeQueueDto> peek = underTest.peek(db.getSession(), WORKER_UUID_1, false);
+ Optional<CeQueueDto> peek = underTest.peek(db.getSession(), WORKER_UUID_1, false, false);
assertThat(peek).isPresent();
assertThat(peek.get().getUuid()).isEqualTo(TASK_UUID_1);
assertThat(peek.get().getStatus()).isEqualTo(IN_PROGRESS);
verifyCeQueueStatuses(TASK_UUID_1, IN_PROGRESS, TASK_UUID_2, PENDING);
// peek second one
- peek = underTest.peek(db.getSession(), WORKER_UUID_2, false);
+ peek = underTest.peek(db.getSession(), WORKER_UUID_2, false, false);
assertThat(peek).isPresent();
assertThat(peek.get().getUuid()).isEqualTo(TASK_UUID_2);
assertThat(peek.get().getStatus()).isEqualTo(IN_PROGRESS);
verifyCeQueueStatuses(TASK_UUID_1, IN_PROGRESS, TASK_UUID_2, IN_PROGRESS);
// no more pendings
- assertThat(underTest.peek(db.getSession(), WORKER_UUID_1, false).isPresent()).isFalse();
+ assertThat(underTest.peek(db.getSession(), WORKER_UUID_1, false, false).isPresent()).isFalse();
}
@Test
system2.setNow(INIT_TIME + 3_000_000);
insertPending(TASK_UUID_2, MAIN_COMPONENT_UUID_1);
- Optional<CeQueueDto> peek = underTest.peek(db.getSession(), WORKER_UUID_1, false);
+ Optional<CeQueueDto> peek = underTest.peek(db.getSession(), WORKER_UUID_1, false, false);
assertThat(peek).isPresent();
assertThat(peek.get().getUuid()).isEqualTo(TASK_UUID_1);
assertThat(peek.get().getMainComponentUuid()).isEqualTo(MAIN_COMPONENT_UUID_1);
verifyCeQueueStatuses(TASK_UUID_1, IN_PROGRESS, TASK_UUID_2, PENDING);
// do not peek second task as long as the first one is in progress
- peek = underTest.peek(db.getSession(), WORKER_UUID_1, false);
+ peek = underTest.peek(db.getSession(), WORKER_UUID_1, false, false);
assertThat(peek.isPresent()).isFalse();
// first one is finished
underTest.deleteByUuid(db.getSession(), TASK_UUID_1);
- peek = underTest.peek(db.getSession(), WORKER_UUID_2, false);
+ peek = underTest.peek(db.getSession(), WORKER_UUID_2, false, false);
assertThat(peek.get().getUuid()).isEqualTo(TASK_UUID_2);
assertThat(peek.get().getWorkerUuid()).isEqualTo(WORKER_UUID_2);
}
assertThat(underTest.selectInProgressStartedBefore(db.getSession(), 3_000L)).extracting(CeQueueDto::getUuid).containsExactlyInAnyOrder(TASK_UUID_2, TASK_UUID_3);
}
+ @Test
+ public void exclude_portfolios_computation_when_indexing_issues() {
+ insertBranch(MAIN_COMPONENT_UUID_1);
+ insertPending(newCeQueueDto(TASK_UUID_1)
+ .setComponentUuid(MAIN_COMPONENT_UUID_1)
+ .setMainComponentUuid(MAIN_COMPONENT_UUID_1)
+ .setStatus(PENDING)
+ .setTaskType(CeTaskTypes.BRANCH_ISSUE_SYNC)
+ .setCreatedAt(100_000L));
+
+ String view_uuid = "view_uuid";
+ insertView(view_uuid);
+ insertPending(newCeQueueDto(TASK_UUID_2)
+ .setComponentUuid(view_uuid)
+ .setMainComponentUuid(view_uuid)
+ .setStatus(PENDING)
+ .setTaskType(CeTaskTypes.REPORT)
+ .setCreatedAt(100_000L));
+
+ Optional<CeQueueDto> peek = underTest.peek(db.getSession(), WORKER_UUID_1, false, true);
+ assertThat(peek).isPresent();
+ assertThat(peek.get().getUuid()).isEqualTo(TASK_UUID_1);
+
+ Optional<CeQueueDto> peek2 = underTest.peek(db.getSession(), WORKER_UUID_1, false, false);
+ assertThat(peek2).isPresent();
+ assertThat(peek2.get().getUuid()).isEqualTo(TASK_UUID_2);
+ }
+
+ private void insertView(String view_uuid) {
+ ComponentDto view = new ComponentDto();
+ view.setQualifier("VW");
+ view.setUuid(view_uuid);
+ view.setOrganizationUuid("org_uuid");
+ view.setPrivate(false);
+ view.setRootUuid(view_uuid);
+ view.setUuidPath("uuid_path");
+ view.setProjectUuid(view_uuid);
+ db.components().insertViewAndSnapshot(view);
+ db.commit();
+ }
+
+ private void insertBranch(String uuid) {
+ ComponentDto branch = new ComponentDto();
+ branch.setQualifier("TRK");
+ branch.setUuid(uuid);
+ branch.setOrganizationUuid("org_uuid");
+ branch.setPrivate(false);
+ branch.setRootUuid(uuid);
+ branch.setUuidPath("uuid_path");
+ branch.setProjectUuid(uuid);
+ db.components().insertComponent(branch);
+ db.commit();
+ }
+
private void insertPending(CeQueueDto dto) {
underTest.insert(db.getSession(), dto);
db.commit();
}
private static Iterable<Map<String, Object>> upperizeKeys(List<Map<String, Object>> select) {
- return from(select).transform(new Function<Map<String, Object>, Map<String, Object>>() {
+ return select.stream().map(new Function<Map<String, Object>, Map<String, Object>>() {
@Nullable
@Override
public Map<String, Object> apply(Map<String, Object> input) {
}
return res;
}
- });
+ }).collect(Collectors.toList());
}
private void verifyCeQueueStatuses(String taskUuid1, CeQueueDto.Status taskStatus1, String taskUuid2, CeQueueDto.Status taskStatus2) {