Skip to content
Snippets Groups Projects
Unverified Commit 5a1c12f6 authored by Matthias Urhahn's avatar Matthias Urhahn Committed by GitHub
Browse files

Improve TaskController logging and limit state history (DEV) (#1717)


* Improve TaskController.kt logging

* Cap TaskController's state history to 50 entries.

* Determine oldest tasks to prune based on `finishedAt` instead of `startedAt`

Co-authored-by: default avatarKolya Opahle <k.opahle@sap.com>
parent 661d092c
No related branches found
No related tags found
No related merge requests found
...@@ -123,7 +123,6 @@ class TaskController @Inject constructor( ...@@ -123,7 +123,6 @@ class TaskController @Inject constructor(
private suspend fun processMap() = internalTaskData.updateSafely { private suspend fun processMap() = internalTaskData.updateSafely {
Timber.tag(TAG).d("Processing task data (count=%d)", size) Timber.tag(TAG).d("Processing task data (count=%d)", size)
Timber.tag(TAG).v("Tasks before processing: %s", this.values)
// Procress all unprocessed finished tasks // Procress all unprocessed finished tasks
procressFinishedTasks(this).let { procressFinishedTasks(this).let {
...@@ -137,7 +136,19 @@ class TaskController @Inject constructor( ...@@ -137,7 +136,19 @@ class TaskController @Inject constructor(
this.putAll(it) this.putAll(it)
} }
Timber.tag(TAG).v("Tasks after processing: %s", this.values) if (size > TASK_HISTORY_LIMIT) {
Timber.v("Enforcing history limits (%d), need to remove %d.", TASK_HISTORY_LIMIT, size - TASK_HISTORY_LIMIT)
values
.filter { it.isFinished }
.sortedBy { it.finishedAt }
.take(size - TASK_HISTORY_LIMIT)
.forEach {
Timber.v("Removing from history: %s", get(it.id))
remove(it.id)
}
}
Timber.tag(TAG).v("Tasks after processing (count=%d):\n%s", size, values.joinToString("\n"))
} }
private fun procressFinishedTasks(data: Map<UUID, InternalTaskState>): Map<UUID, InternalTaskState> { private fun procressFinishedTasks(data: Map<UUID, InternalTaskState>): Map<UUID, InternalTaskState> {
...@@ -178,9 +189,9 @@ class TaskController @Inject constructor( ...@@ -178,9 +189,9 @@ class TaskController @Inject constructor(
it.id != state.id it.id != state.id
} }
Timber.tag(TAG).d("Task has %d siblings", siblingTasks.size) Timber.tag(TAG).d("Task has %d siblings", siblingTasks.size)
Timber.tag(TAG).v( if (siblingTasks.isNotEmpty()) {
"Sibling are:\n%s", siblingTasks.joinToString("\n") Timber.tag(TAG).v("Sibling are:\n%s", siblingTasks.joinToString("\n"))
) }
// Handle collision behavior for tasks of same type // Handle collision behavior for tasks of same type
when { when {
...@@ -237,5 +248,6 @@ class TaskController @Inject constructor( ...@@ -237,5 +248,6 @@ class TaskController @Inject constructor(
companion object { companion object {
private const val TAG = "TaskController" private const val TAG = "TaskController"
private const val TASK_HISTORY_LIMIT = 50
} }
} }
...@@ -539,4 +539,44 @@ class TaskControllerTest : BaseIOTest() { ...@@ -539,4 +539,44 @@ class TaskControllerTest : BaseIOTest() {
instance.close() instance.close()
} }
@Test
fun `old tasks are pruned from history`() = runBlockingTest {
val instance = createInstance(scope = this)
val expectedFiles = mutableListOf<File>()
repeat(100) {
val arguments = QueueingTask.Arguments(
delay = 5,
values = listOf("TestText"),
path = File(testDir, UUID.randomUUID().toString())
)
expectedFiles.add(arguments.path)
val request = DefaultTaskRequest(type = QueueingTask::class, arguments = arguments)
instance.submit(request)
delay(5)
}
this.advanceUntilIdle()
expectedFiles.forEach {
it.exists() shouldBe true
}
val taskHistory = instance.tasks.first()
taskHistory.size shouldBe 50
expectedFiles.size shouldBe 100
val sortedHistory = taskHistory.sortedBy { it.taskState.startedAt }.apply {
first().taskState.startedAt!!.isBefore(last().taskState.startedAt) shouldBe true
}
expectedFiles.subList(50, 100) shouldBe sortedHistory.map {
(it.taskState.request.arguments as QueueingTask.Arguments).path
}
instance.close()
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment