From f01745f4663f2ebd4bd9e5b55e98335739a700a2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Wed, 12 Jul 2017 11:39:23 +0200 Subject: [PATCH] SONAR-9540 log MessageException message if task can't be finalized so that the cause of the failure of the task is not lost in the void --- .../sonar/ce/taskprocessor/CeWorkerImpl.java | 7 ++- .../ce/taskprocessor/CeWorkerImplTest.java | 48 +++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/taskprocessor/CeWorkerImpl.java b/server/sonar-ce/src/main/java/org/sonar/ce/taskprocessor/CeWorkerImpl.java index b5db945cb96..d52bed437f9 100644 --- a/server/sonar-ce/src/main/java/org/sonar/ce/taskprocessor/CeWorkerImpl.java +++ b/server/sonar-ce/src/main/java/org/sonar/ce/taskprocessor/CeWorkerImpl.java @@ -149,7 +149,12 @@ public class CeWorkerImpl implements CeWorker { try { queue.remove(task, status, taskResult, error); } catch (Exception e) { - LOG.error(format("Failed to finalize task with uuid '%s' and persist its state to db", task.getUuid()), e); + String errorMessage = format("Failed to finalize task with uuid '%s' and persist its state to db", task.getUuid()); + if (error instanceof MessageException) { + LOG.error(format("%s. Task failed with MessageException \"%s\"", errorMessage, error.getMessage()), e); + } else { + LOG.error(errorMessage, e); + } } finally { stopActivityProfiler(ceProfiler, task, status); ceLogging.clearForTask(); diff --git a/server/sonar-ce/src/test/java/org/sonar/ce/taskprocessor/CeWorkerImplTest.java b/server/sonar-ce/src/test/java/org/sonar/ce/taskprocessor/CeWorkerImplTest.java index dfef7253872..e30a15b116e 100644 --- a/server/sonar-ce/src/test/java/org/sonar/ce/taskprocessor/CeWorkerImplTest.java +++ b/server/sonar-ce/src/test/java/org/sonar/ce/taskprocessor/CeWorkerImplTest.java @@ -336,6 +336,7 @@ public class CeWorkerImplTest { makeTaskProcessorFail(ceTask); underTest.call(); + List logs = logTester.logs(LoggerLevel.ERROR); assertThat(logs).hasSize(2); assertThat(logs.get(0)).isEqualTo("Failed to execute task " + ceTask.getUuid()); @@ -350,11 +351,58 @@ public class CeWorkerImplTest { makeTaskProcessorFail(ceTask, MessageException.of("simulate MessageException thrown by TaskProcessor#process")); underTest.call(); + List logs = logTester.logs(LoggerLevel.ERROR); assertThat(logs).hasSize(1); assertThat(logs.get(0)).contains(" | submitter=FooBar | time="); } + @Test + public void log_error_when_task_was_successful_but_ending_state_can_not_be_persisted_to_db() throws Exception { + CeTask ceTask = createCeTask("FooBar"); + when(queue.peek(anyString())).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); + + underTest.call(); + + assertThat(logTester.logs(LoggerLevel.ERROR)).containsOnly("Failed to finalize task with uuid '" + ceTask.getUuid() + "' and persist its state to db"); + } + + @Test + public void log_error_when_task_failed_and_ending_state_can_not_be_persisted_to_db() throws Exception { + CeTask ceTask = createCeTask("FooBar"); + when(queue.peek(anyString())).thenReturn(Optional.of(ceTask)); + taskProcessorRepository.setProcessorForTask(CeTaskTypes.REPORT, taskProcessor); + IllegalStateException ex = makeTaskProcessorFail(ceTask); + doThrow(new RuntimeException("Simulate queue#remove failing")).when(queue).remove(ceTask, CeActivityDto.Status.FAILED, null, ex); + + underTest.call(); + + List logs = logTester.logs(LoggerLevel.ERROR); + assertThat(logs).hasSize(3); + assertThat(logs.get(0)).isEqualTo("Failed to execute task " + ceTask.getUuid()); + assertThat(logs.get(1)).isEqualTo("Failed to finalize task with uuid '" + ceTask.getUuid() + "' and persist its state to db"); + assertThat(logs.get(2)).contains(" | submitter=FooBar | time="); + } + + @Test + public void log_error_when_task_failed_with_MessageException_and_ending_state_can_not_be_persisted_to_db() throws Exception { + CeTask ceTask = createCeTask("FooBar"); + when(queue.peek(anyString())).thenReturn(Optional.of(ceTask)); + taskProcessorRepository.setProcessorForTask(CeTaskTypes.REPORT, taskProcessor); + MessageException ex = makeTaskProcessorFail(ceTask, MessageException.of("simulate MessageException thrown by TaskProcessor#process")); + doThrow(new RuntimeException("Simulate queue#remove failing")).when(queue).remove(ceTask, CeActivityDto.Status.FAILED, null, ex); + + underTest.call(); + + List logs = logTester.logs(LoggerLevel.ERROR); + assertThat(logs).hasSize(2); + assertThat(logs.get(0)).isEqualTo("Failed to finalize task with uuid '" + ceTask.getUuid() + "' and persist its state to db. " + + "Task failed with MessageException \"" + ex.getMessage() + "\""); + assertThat(logs.get(1)).contains(" | submitter=FooBar | time="); + } + private Thread createThreadNameVerifyingThread(String threadName) { return new Thread(() -> { verifyUnchangedThreadName(threadName); -- 2.39.5