You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@kafka.apache.org by "Javed, Haseeb" <ja...@buckeyemail.osu.edu> on 2017/09/12 01:08:52 UTC

Understanding the semantics of Selector.poll()

Hello,

I was looking into Kafka's network layer code have a few questions regarding the Selector class, particularly who the poll() method is implemented. The poll() method goes something like this:


        /* check ready keys */
        long startSelect = time.nanoseconds();
        int readyKeys = select(timeout);
        long endSelect = time.nanoseconds();
        this.sensors.selectTime.record(endSelect - startSelect, time.milliseconds());

        if (readyKeys > 0 || !immediatelyConnectedKeys.isEmpty()) {
            pollSelectionKeys(this.nioSelector.selectedKeys(), false, endSelect);
            pollSelectionKeys(immediatelyConnectedKeys, true, endSelect);
        }

        addToCompletedReceives();

Is there a specific requirement because of which we call pollSelectionKeys() method first for the keys returned by the select() method and then on the immediately connected keys? Is it just for clarity that we perform these operations seperately, or are there some specific requirements involved?

Secondly, in the pollSelectionKeys() method, we have:

/* if channel is ready write to any sockets that have space in their buffer and for which we have data */
if (channel.ready() && key.isWritable()) {
    Send send = channel.write();
    if (send != null) {
        this.completedSends.add(send);
        this.sensors.recordBytesSent(channel.id(), send.size());
    }
}

From what I understand, we only ever write to a KafkaChannel when either it belonged to the keySet we obatined from the earlier call to the select() method, or if the KafkaChannel associated with one of the immediatelyConnectedKeys. My question is, why do we go about the business of writing to the KafkaChannels this way? More specifically, we don't we just iterate over all the KafkaChannels that have been connected, and write to them if they have a Send object associated with them? In this way, we write to the KafkaChannel as soon as we can, without waiting for it to belong to the immediatelyConnectedKeys or readyKeys.

Thanks,
Haseeb