[ 
https://issues.apache.org/jira/browse/DIRMINA-1095?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16833112#comment-16833112
 ] 

Jonathan Valliere commented on DIRMINA-1095:
--------------------------------------------

Function {{processReadySessions()}} in NioDatagramAcceptor.java only iterates 
on a {{Set<SelectionKey>}} of active keys returned by NIO. This is no different 
than how TCP works, you select then get the {{Set<SelectionKey>}} of the 
current active channels.

For each Address Pair a Virtual NioSession is created using the 
{{remoteAddress}} as the lookup key within the {{IoSessionRecycler}}. This 
NioSession cannot have a {{SelectionKey}} therefore the only way to know if 
data is available to be written is to loop across every single one checking the 
Queue. This is not optimal but there is no other way to do this.

Because there is no association between the {{localAddress}} and these created 
Virtual NioSessions, there is no way to iterate over a specific sub-group of 
Virtual NioSessions belonging to a specific {{localAddress}}.

This could be accomplished by creating a 2D association map between the 
{{localAddress}} and all the {{remoteAddress}} Virtual NioSessions however it 
would create complications with many other components to handle all edge cases 
to prevent memory leaks. This is not a simple task to do correctly and might 
require large rewrites to the NioDatagram system. That would be a lot of work 
for a situation that might yield very little practical benefit.

What I did was add a filter within the for loop to only schedule flush of 
Virtual NioSessions that belong to the target DatagramChannel. This will 
prevent the {{flushingSessions}} queue from filling with duplicates and 
unnecessary NioSessions. For right now, this is a good compromise to achieve 
better efficiency.

> Seems like the management f UDP sessions is really unneficient
> --------------------------------------------------------------
>
>                 Key: DIRMINA-1095
>                 URL: https://issues.apache.org/jira/browse/DIRMINA-1095
>             Project: MINA
>          Issue Type: Improvement
>    Affects Versions: 2.0.19
>            Reporter: Emmanuel Lecharny
>            Assignee: Jonathan Valliere
>            Priority: Major
>             Fix For: 2.1.1
>
>
> When we process incoming UDP messages, we iterate over the activated 
> {{SelectionKey}}s. That's ok, but for each one of them, we read the data and 
> try to flush the scheduled messages. The loop where it's done seems to 
> iterate on *all* the managed sessions, for each keys we are processing :
> {code:java}
>    private void processReadySessions(Set<SelectionKey> handles) {
>         Iterator<SelectionKey> iterator = handles.iterator();
>         while (iterator.hasNext()) {
>             SelectionKey key = iterator.next();
>             DatagramChannel handle = (DatagramChannel) key.channel();
>             iterator.remove();
>             try {
>                 if (key.isValid() && key.isReadable()) {
>                     readHandle(handle);
>                 }
>                 if (key.isValid() && key.isWritable()) {
>                     for (IoSession session : getManagedSessions().values()) {
>                         scheduleFlush((NioSession) session);
> ...
> {code}
> There is no reason to do so. First, we should not iterate on all the managed 
> sessions (we may have thousands), but only the sessions which have something 
> to write, and we certainly should not do that for every active key...



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to