Share
Embed
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
82 slides
-
Channels in Kotlin Coroutines* Nikita Koval, Joker 2018 * A
look into the future -
Speaker: Nikita Koval • Graduated at ITMO University • Previously
worked as developer and researcher at Devexperts • Teaches concurrent programming at ITMO University • PhD student at IST Austria • Researcher at JetBrains 3 @nkoval_ -
What coroutines are • Lightweight threads, can be suspended and
resumed for free ◦ You can run millions of coroutines and not die! 4 -
What coroutines are • Lightweight threads, can be suspended and
resumed for free ◦ You can run millions of coroutines and not die! • Support writing an asynchronous code like a synchronous one fun postItem(item: Item) { val token = requestToken() val post = createPost(token, item) processPost(post) } 5 suspend functions -
Producer-consumer problem * Both clients and consumers are coroutines 7
... Worker 1 Worker M ... Send a task Receive a task Client 1 Client 2 Client N -
Producer-consumer problem solution 1. Let’s create a channel val tasks
= Channel<Task>() 8 -
Producer-consumer problem solution 1. Let’s create a channel val tasks
= Channel<Task>() 2. Clients send tasks to workers through this channel val task = Task(...) tasks.send(task) 9 -
Producer-consumer problem solution 1. Let’s create a channel val tasks
= Channel<Task>() 2. Clients send tasks to workers through this channel val task = Task(...) tasks.send(task) 3. Workers receive tasks in infinite loop while(true) { val task = tasks.receive() processTask(task) } 10 -
Channel semantics Client 1 val task = Task(...) tasks.send(task) 11
Client 2 val task = Task(...) tasks.send(task) Worker while(true) { val task = tasks.receive() processTask(task) } val tasks = Channel<Task>() -
Channel semantics Client 1 val task = Task(...) tasks.send(task) 12
Client 2 val task = Task(...) tasks.send(task) Worker while(true) { val task = tasks.receive() processTask(task) } val tasks = Channel<Task>() Have to wait for send 1 -
Channel semantics Client 1 val task = Task(...) tasks.send(task) 13
Client 2 val task = Task(...) tasks.send(task) Worker while(true) { val task = tasks.receive() processTask(task) } val tasks = Channel<Task>() 1 -
Channel semantics Client 1 val task = Task(...) tasks.send(task) 14
Client 2 val task = Task(...) tasks.send(task) Worker while(true) { val task = tasks.receive() processTask(task) } val tasks = Channel<Task>() 1 -
Channel semantics Client 1 val task = Task(...) tasks.send(task) 15
Client 2 val task = Task(...) tasks.send(task) Worker while(true) { val task = tasks.receive() processTask(task) } val tasks = Channel<Task>() Rendezvous! 1 2 -
Channel semantics Client 1 val task = Task(...) tasks.send(task) 16
Client 2 val task = Task(...) tasks.send(task) Worker while(true) { val task = tasks.receive() processTask(task) } 1 val tasks = Channel<Task>() 3 2 -
Channel semantics Client 1 val task = Task(...) tasks.send(task) 17
Client 2 val task = Task(...) tasks.send(task) Worker while(true) { val task = tasks.receive() processTask(task) } 1 val tasks = Channel<Task>() 3 2 4 Have to wait for receive -
Channel semantics Client 1 val task = Task(...) tasks.send(task) 18
Client 2 val task = Task(...) tasks.send(task) Worker while(true) { val task = tasks.receive() processTask(task) } 1 val tasks = Channel<Task>() 3 2 4 -
Channel semantics Client 1 val task = Task(...) tasks.send(task) 19
Client 2 val task = Task(...) tasks.send(task) Worker while(true) { val task = tasks.receive() processTask(task) } 1 val tasks = Channel<Task>() 3 2 4 5 Rendezvous! -
Rendezvous channel class Coroutine { var element: Any? ... }
fun curCoroutine(): Coroutine { ... } suspend fun suspend(c: Coroutine) { ... } fun resume(c: Coroutine) { ... } 21 Stores an element to be sent Returns the current coroutine Functions to manipulate with coroutines -
Rendezvous channel class Coroutine { var element: Any? ... }
fun curCoroutine(): Coroutine { ... } suspend fun suspend(c: Coroutine) { ... } fun resume(c: Coroutine) { ... } val senders = Queue<Coroutine>() val receivers = Queue<Coroutine>() 22 Queues of suspended send and receive invocations -
Rendezvous channel class Coroutine { var element: Any? ... }
fun curCoroutine(): Coroutine { ... } suspend fun suspend(c: Coroutine) { ... } fun resume(c: Coroutine) { ... } val senders = Queue<Coroutine>() val receivers = Queue<Coroutine>() 23 suspend fun send(element: T) { if (receivers.isEmpty()) { val curCor = curCoroutine() curCor.element = element senders.enqueue(curCor) suspend(curCor) } else { val r = receivers.dequeue() r.element = element resume(receiver) } } Checks if there is no receiver and suspends Rendezvous: retrieves the first receiver -
Rendezvous channel: Golang Uses per-channel locks 25 suspend fun send(element:
T) = channelLock.withLock { if (receivers.isEmpty()) { val curCor = curCoroutine() curCor.element = element senders.enqueue(curCor) suspend(curCor) } else { val r = receivers.dequeue() r.element = element resume(receiver) } } -
Rendezvous channel: Golang Uses per-channel locks 26 Non-scalable, no progress
guarantee... suspend fun send(element: T) = channelLock.withLock { if (receivers.isEmpty()) { val curCor = curCoroutine() curCor.element = element senders.enqueue(curCor) suspend(curCor) } else { val r = receivers.dequeue() r.element = element resume(receiver) } } -
Modern queues use Fetch-And-Add... Let’s try to use the same
ideas for channels! PPoPP’13 PPoPP’16 -
Concurrent primitives • Fetch-And-Add(p, val): Long Atomically increments the located
by address p register by val and returns the new value • Compare-And-Swap(p, old, new): Boolean Atomically checks if the located by address p value equals old and replaces it with new 28 -
Rendezvous channel: Kotlin • Assume we have an atomic 128-bit
register ◦ That is not true; we will fix this later -
Rendezvous channel: Kotlin • Assume we have an atomic 128-bit
register ◦ That is not true; we will fix this later senders receivers 64 bits 64 bits The total number of send and receive operations 30 sendersAndReceivers -
Rendezvous channel: Kotlin • Assume we have an atomic 128-bit
register ◦ That is not true; we will fix this later senders receivers 64 bits 64 bits The total number of send and receive operations • Every send and receive increments its counter using FAA ◦ send increments the register by (1 << 64) ◦ receive increments the register by 1 31 sendersAndReceivers -
Rendezvous channel: Kotlin • Each send-receive pair works with an
unique cell • This cell id is either senders or receivers counter after the increment (for send and receive respectively) • How to understand if we can make a rendezvous? 32 -
Rendezvous channel: Kotlin 33 when { senders < receivers ->
// make a rendezvous senders >= receivers -> // suspend } The balance before the send operation -
Rendezvous channel: Kotlin 34 EMPTY coroutine DONE suspend rendezvous when
{ senders < receivers -> // make a rendezvous senders >= receivers -> // suspend } Cell life cycle The balance before the send operation -
Rendezvous channel: Kotlin 35 EMPTY coroutine DONE BROKEN suspend rendezvous
failed rendezvous when { senders < receivers -> // make a rendezvous senders >= receivers -> // suspend } The balance before the send operation Cell life cycle This helps not to block -
Rendezvous channel: Kotlin 1. How to implement an atomic 128-bit
counter using 64-bit ones? 2. How to organize the cell storage? 36 -
Rendezvous channel: Kotlin 1. How to implement an atomic 128-bit
counter using 64-bit ones? 2. How to organize the cell storage? 37 -
Rendezvous channel: Kotlin 38 senders_L receivers_L 1/0 1/0 senders_H receivers_H
1 bit 31 bits 1 bit 31 bits 32 bits 32 bits We maintain highest and lowest parts separately 0000...001111...11 highest part lowest part L H -
Rendezvous channel: Kotlin 39 senders_L receivers_L 1/0 1/0 senders_H receivers_H
1 bit 31 bits 1 bit 31 bits 32 bits 32 bits We maintain highest and lowest parts separately Indicates that the lowest part is overflowed L H -
Rendezvous channel: Kotlin 40 senders_L receivers_L 1/0 1/0 senders_H receivers_H
1 bit 31 bits 1 bit 31 bits 32 bits 32 bits Read-write lock for highest parts H_rwlock L H -
Rendezvous channel: Kotlin 41 senders_H receivers_H 32 bits 32 bits
H_rwlock senders_L receivers_L 1/0 1/0 1 bit 31 bits 1 bit 31 bits Increment algorithm: 1. Acquire H_rwlock for read 2. Read H 3. Inc L by FAA 4. Release the lock L H -
Rendezvous channel: Kotlin 42 senders_H receivers_H 32 bits 32 bits
H_rwlock senders_L receivers_L 1/0 1/0 1 bit 31 bits 1 bit 31 bits Increment algorithm: 1. Acquire H_rwlock for read 2. Read H 3. Inc L by FAA 4. Release the lock L H Just a FAA -
Rendezvous channel: Kotlin 43 senders_H receivers_H 32 bits 32 bits
H_rwlock senders_L receivers_L 1/0 1/0 1 bit 31 bits 1 bit 31 bits Increment algorithm: 1. Acquire H_rwlock for read 2. Read H 3. Inc L by FAA 4. Release the lock 5. If the lowest part is overflowed 5.1. Acquire H_rwlock for write 5.2. Reset the bit 5.3. Inc H 5.4. Release the lock L H -
Rendezvous channel: Kotlin 1. How to implement an atomic 128-bit
counter using 64-bit ones? 2. How to organize the cell storage? 44 -
Rendezvous channel: Kotlin 45 0 N ... 1 N ...
K N ... ... Michael-Scott queue of segments with the fixed number of cells in each Segments HEAD TAIL -
Rendezvous channel: Kotlin 46 0 N ... 1 N ...
K N ... ... Michael-Scott queue of segments with the fixed number of cells in each Cells HEAD TAIL -
Rendezvous channel: Kotlin 47 0 N ... 1 N ...
K N ... ... Michael-Scott queue of segments with the fixed number of cells in each Pointer to the next segment HEAD TAIL Segment id -
Rendezvous channel: Kotlin 48 0 N ... 1 N ...
K N ... ... HEAD TAIL 1. Read both HEAD and TAIL 2. Increment the counter -
Rendezvous channel: Kotlin 49 0 N ... 1 N ...
K N ... ... HEAD TAIL 1. Read both HEAD and TAIL 2. Increment the counter 3. Either make a rendezvous 3.1. Find the cell starting from the head 3.2. Move HEAD forward if needed -
Rendezvous channel: Kotlin 50 0 N ... 1 N ...
K N ... ... HEAD TAIL 1. Read both HEAD and TAIL 2. Increment the counter 3. Either make a rendezvous 3.1. Find the cell starting from the head 3.2. Move HEAD forward if needed 4. or suspend 4.1. Find the cell starting from the tail 4.2. Create new segments if needed -
Rendezvous channel: Kotlin vs Golang 51 4 x 2 x
Intel Xeon Gold 6150 (Skylake) 2.7GHz = (144 virtual cores) -
Producer-consumer problem: buffering 52 Client 1 Client 2 Client N
... Worker 1 Worker M ... Send a task Receive a task We don’t want to wait on every send... -
Producer-consumer problem: buffering 53 Client 1 Client 2 Client N
... Worker 1 Worker M ... Send a task Receive a task Let’s use a fixed-size buffer! -
Buffered channel semantics Client 1 val task = Task(...) tasks.send(task)
54 Client 2 val task = Task(...) tasks.send(task) Worker while(true) { val task = tasks.receive() processTask(task) } val tasks = Channel<Task>(capacity = 1) One element can be sent without suspension -
Buffered channel semantics Client 1 val task = Task(...) tasks.send(task)
55 Client 2 val task = Task(...) tasks.send(task) Worker while(true) { val task = tasks.receive() processTask(task) } val tasks = Channel<Task>(capacity = 1) 1 Does not suspend! -
Buffered channel semantics Client 1 val task = Task(...) tasks.send(task)
56 Client 2 val task = Task(...) tasks.send(task) Worker while(true) { val task = tasks.receive() processTask(task) } val tasks = Channel<Task>(capacity = 1) 1 The buffer is full, has to suspend 2 -
Buffered channel semantics Client 1 val task = Task(...) tasks.send(task)
57 Client 2 val task = Task(...) tasks.send(task) Worker while(true) { val task = tasks.receive() processTask(task) } val tasks = Channel<Task>(capacity = 1) 1 Receives the buffered element at first 2 3 -
Buffered channel semantics Client 1 val task = Task(...) tasks.send(task)
58 Client 2 val task = Task(...) tasks.send(task) Worker while(true) { val task = tasks.receive() processTask(task) } val tasks = Channel<Task>(capacity = 1) 1 Makes a rendezvous with the 2nd client 2 3 4 -
Buffered channel: Golang • Maintains an additional fixed-size buffer ◦
Tries to send to this buffer instead of suspending • Performs all operations under the channel lock 59 -
Buffered channel: Kotlin Rendezvous channel when { senders - receivers
< 0 -> // make a rendezvous senders - receivers >= 0 -> // suspend } 60 The balance before the send operation -
Buffered channel: Kotlin Rendezvous channel when { senders - receivers
< 0 -> // make a rendezvous senders - receivers >= 0 -> // suspend } Buffered channel when { senders - receivers < 0 -> // make a rendezvous 0 <= senders - receivers < capacity -> // send the element without suspension senders - receivers >= capacity -> // suspend } 61 -
Buffered channel: Kotlin vs Golang 62 4 x 2 x
Intel Xeon Gold 6150 (Skylake) 2.7GHz = (144 virtual cores) -
The select expression 65 Client val task = Task(...) tasks.send(task)
The client was interrupted while waiting for a worker -
The select expression 66 Client val task = Task(...) tasks.send(task)
The client was interrupted while waiting for a worker Do we need to process the task anymore? -
The select expression 67 Client val task = Task(...) tasks.send(task)
The client was interrupted while waiting for a worker Do we need to process the task anymore? It would be better to cancel the request and detect this -
The select expression Client val task = Task(...) val cancelled
= Channel<Unit>() 68 Unit is sent to this channel if the client is interrupted -
The select expression Client val task = Task(...) val cancelled
= Channel<Unit>() select<Unit> { tasks.onSend(task) { println("Task has been sent") } cancelled.onReceive { println("Cancelled") } } 69 Waits simultaneously, at most one clause is selected atomically. -
The select expression: Golang • Fine-grained locking • Acquires all
involved channels locks to register into the queues ◦ Uses hierarchical order to avoid deadlocks • Acquires all these locks again to resume the coroutine ◦ Otherwise, two select clauses could interfere 70 -
For each channel The select expression: Kotlin 72 SelectInstance alternatives
state PENDING or SELECTED Get the counters snapshot Try to make a rendezvous Try to store the SelectInstance Waiting phase Remove the stored SelectInstance-s senders < receivers senders >= receivers -
The select expression: Kotlin EMPTY coroutine DONE BROKEN suspend rendezvous
failed rendezvous Cell life cycle Select Desc rendezvous try to make a rendezvous rendezvous failed 73 -
The select expression: Kotlin EMPTY coroutine DONE BROKEN suspend rendezvous
failed rendezvous Cell life cycle Select Desc rendezvous try to make a rendezvous rendezvous failed Does not update counters here! Increment the counter on success 74 -
The select expression: Kotlin EMPTY coroutine DONE BROKEN suspend rendezvous
failed rendezvous Cell life cycle Select Desc rendezvous try to make a rendezvous rendezvous failed Does not update counters here! Increment the counter on success Tries to increment the counter from the snapshot by CAS 75 -
The select expression: Kotlin A rendezvous between two selects: 77
SelectInstance alternatives state PENDING or SELECTED SelectInstance alternatives state PENDING or SELECTED Need to update them atomically Let’s update them similarly to the Harris multiword CAS* * ”A practical multi-word compare-and-swap operation” by Harris et al. @ DISC’02 -
The select expression: Kotlin vs Golang 78 4 x 2
x Intel Xeon Gold 6150 (Skylake) 2.7GHz = (144 virtual cores) -
Cancellation in Kotlin • Cancellation is a built-in feature in
Kotlin ◦ However, the previous pattern is widely used in Golang val job = GlobalScope.launch { ... } job.cancel() 79 Removes the coroutine from all channels as well -
Cancellation in Kotlin • Cancellation is a built-in feature in
Kotlin ◦ However, the previous pattern is widely used in Golang val job = GlobalScope.launch { ... } job.cancel() GlobalScope.launch { withTimeout(time = 1, unit = TimeUnit.SECONDS) { ... } } 80 Invokes job.cancel() after the timeout -
There are more message passing primitives • BroadcastChannel Sends to
multiple receivers • ConflatedChannel Receivers always get the most recently sent element • ConflatedBroadcastChannel Mix of the previous ones • Mutex 81