You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by "Emmanuel Lecharny (JIRA)" <ji...@apache.org> on 2014/09/05 12:53:28 UTC

[jira] [Comment Edited] (DIRMINA-845) ProtocolEncoderOutputImpl isn't thread-safe

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

Emmanuel Lecharny edited comment on DIRMINA-845 at 9/5/14 10:52 AM:
--------------------------------------------------------------------

I understand, there are reasons to leave class partially thread-safe, but I think this should be reflected in java doc at least.

"What you are saying is that the frames coming from a session1 in the order (A,B) are pushed in this order to session 3, but are sometime transmitted in the reverse order (B,A), right?"

Not exactly but closer to true. A and B aren't two frames they are chunks of one frame. Chunk equals to message in MINA terms.
One thread splits frame onto chunks and writes in to output (chunks order is correct here). Another concurrent thread does the same with another frame but writes to the same output. All correct until both thread run flush on the same output.
{code}
public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws ProtocolCodecException { 
  // ...
  final IoBuffer buf = encoder.encode(state, message); 
  if (buf != null) { 
    Chunker chunker = new Chunker(buf, state.getWriteChunkSize(), 2048); 
    while (chunker.hasNext()) { 
      out.write(chunker.next()); 
    }
  }
  // ...
  out.flush(); // <- problem HERE
}
{code}
Here 'session' is session 3 and encode runs concurrently in session 1's thread and session 2's thread. 


was (Author: ilya.a.ivanov):
I understand, there are reasons to leave class partially thread-safe, but I think this should be reflected in java doc at least.

"What you are saying is that the frames coming from a session1 in the order (A,B) are pushed in this order to session 3, but are sometime transmitted in the reverse order (B,A), right?"

Not exactly but closer to true. A and B aren't two frames they are chunks of one frame. Chunk equals to message in MINA terms.
One thread splits frame onto chunks and writes in to output (chunks order is correct here). Another concurrent thread does the same with another frame but writes to the same output. All correct until both thread run flush on the same output.

public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws ProtocolCodecException { 
  // ...
  final IoBuffer buf = encoder.encode(state, message); 
  if (buf != null) { 
    Chunker chunker = new Chunker(buf, state.getWriteChunkSize(), 2048); 
    while (chunker.hasNext()) { 
      out.write(chunker.next()); 
    }
  }
  // ...
  out.flush(); // <- problem HERE
}

Here 'session' is session 3 and encode runs concurrently in session 1's thread and session 2's thread. 

> ProtocolEncoderOutputImpl isn't thread-safe
> -------------------------------------------
>
>                 Key: DIRMINA-845
>                 URL: https://issues.apache.org/jira/browse/DIRMINA-845
>             Project: MINA
>          Issue Type: Bug
>          Components: Filter
>    Affects Versions: 2.0.4
>            Reporter: Ilya Ivanov
>             Fix For: 2.0.8
>
>
> ProtocolEncoderOutputImpl uses ConcurrentLinkedQueue and at first look it seems to be thread-safe. But really concurrent execution of flush method isn't thread-safe (and write-mergeAll also).
> E.g. in RTMP several channels multiplexed in single connection. According protocol specification it's possible to write to different channels concurrently. But it doesn't work with MINA.
> I've synchronized channel writing, but it doesn't prevent concurrent run of flushing (in 2.0.4 it's done directly in ProtocolCodecFilter.filterWrite, but ProtocolEncoderOutputImpl.flush has the same problem).
> Here the fragment of flushing code:
> while (!bufferQueue.isEmpty()) {
>   Object encodedMessage = bufferQueue.poll();
>                 
>   if (encodedMessage == null) {
>     break;
>   }
>   // Flush only when the buffer has remaining.
>   if (!(encodedMessage instanceof IoBuffer) || ((IoBuffer) encodedMessage).hasRemaining()) {
>     SocketAddress destination = writeRequest.getDestination();
>     WriteRequest encodedWriteRequest = new EncodedWriteRequest(encodedMessage, null, destination); 
>     nextFilter.filterWrite(session, encodedWriteRequest);
>   }
> } 
> Suppose original packets sequence is A, B, ...
> Concurrent run of flushing may proceed as following:
> thread-1: Object encodedMessage = bufferQueue.poll(); // gets A packet
> thread-2: Object encodedMessage = bufferQueue.poll(); // gets B packet
> ...
> thread-2: nextFilter.filterWrite(...); // writes B packet
> thread-1: nextFilter.filterWrite(...); // writes A packet
> so, resulting sequence will B, A
> It's quite confusing result especially when documentation doesn't contain any explanation about such behavior.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)