zhi li
04/25/2023, 2:03 AMcoroutines
I am developing tracing for kotlin-coroutines
and I had a test case to assert the collecting spans
val coroutineCount = (System.getenv("COROUTINE_COUNT") ?: "200").toInt()
val executor = Executors.newFixedThreadPool(5).asCoroutineDispatcher()
runBlocking(executor) {
repeat(coroutineCount) {
launch {
runBlocking {
withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
sampleAndStart(INTERMEDIATE, "io")
delay(Random.nextLong(5))
endAndCommit(INTERMEDIATE)
}
}
}
}
}
In kotlin-coroutine 1.5 the span's count is right.
but I update to 1.6 it will miss some spans.
do you have any ideas on this issue ❤️uli
04/25/2023, 12:01 PMcoroutineScope { }
do what you intend?zhi li
04/26/2023, 3:29 AMuli
04/26/2023, 5:47 AMzhi li
04/26/2023, 9:27 AMval coroutineCount = (System.getenv("COROUTINE_COUNT") ?: "200").toInt()
val executor = Executors.newFixedThreadPool(5).asCoroutineDispatcher()
runBlocking(executor) {
repeat(coroutineCount) {
launch {
withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
sampleAndStart(INTERMEDIATE, "io")
delay(Random.nextLong(5))
endAndCommit(INTERMEDIATE)
}
}
}
}
I remove inner runBlocking
but found the count is not right neither. Do I miss something?zhi li
04/28/2023, 1:44 AMoverride fun dispatch(context: CoroutineContext, block: Runnable) {
dispatchInternal(block) {
dispatcher.dispatch(this, this)
}
}
and
private inline fun dispatchInternal(block: Runnable, dispatch: () -> Unit) {
// Add task to queue so running workers will be able to see that
if (addAndTryDispatching(block)) return
/*
* Protect against the race when the number of workers is enough,
* but one (because of synchronized serialization) attempts to complete,
* and we just observed the number of running workers smaller than the actual
* number (hit right between `--runningWorkers` and `++runningWorkers` in `run()`)
*/
if (!tryAllocateWorker()) return
dispatch()
}
I am not sure why they call each inner the function?uli
04/28/2023, 9:25 AMzhi li
05/02/2023, 11:09 PM"INTERMEDIATE"
"io"
So that in tracer we can collect the tracing.zhi li
05/24/2023, 2:34 PMzhi li
05/24/2023, 2:34 PM