You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by "li-libo (Jira)" <ji...@apache.org> on 2020/01/11 12:47:00 UTC
[jira] [Updated] (DIRMINA-1121) Find the bug(Mina v2.1.1 -v2.1.3
unable to receive writeRequest message at sometimes) and fix the bug
[ https://issues.apache.org/jira/browse/DIRMINA-1121?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
li-libo updated DIRMINA-1121:
-----------------------------
Description:
mina v2.1.1-v2.1.3 unable to receive writeRequest message, the following code is bug and my bug fix in ProtocolCodecFilter
{color:#ff0000}Note: bug2 is major bug!{color}
/**
* {@inheritDoc}
*/
@Override
public void filterWrite(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception {
Object message = writeRequest.getMessage();
// Bypass the encoding if the message is contained in a IoBuffer,
// as it has already been encoded before
if ((message instanceof IoBuffer) || (message instanceof FileRegion)) \{ nextFilter.filterWrite(session, writeRequest); return; }
// Get the encoder in the session
ProtocolEncoder encoder = factory.getEncoder(session);
{color:#ff0000}// bug 1:{color} {color:#00875a}the EncoderOut is not thread safe that may lead to duplicated message sent...{color}
{color:#00875a} // ProtocolEncoderOutput encoderOut = getEncoderOut(session, nextFilter, writeRequest);{color}
{color:#ff0000}// bug 1 fix:{color}
ProtocolEncoderOutput encoderOut = new ProtocolEncoderOutputImpl(session, nextFilter, writeRequest);
if (encoder == null)
{ throw new ProtocolEncoderException("The encoder is null for the session " + session); }
try {
// Now we can try to encode the response
encoder.encode(session, message, encoderOut);
// Send it directly
Queue<Object> bufferQueue = ((AbstractProtocolEncoderOutput) encoderOut).getMessageQueue();
// Write all the encoded messages now
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()) {
{color:#ff0000}// major bug 2:{color} {color:#00875a}the writeRequest resets when the code enters while loop sencondly,{color}
{color:#00875a}// it will lead to some writeRequest is missed...{color}
{color:#00875a}// writeRequest.setMessage(encodedMessage);{color}
{color:#00875a} // nextFilter.filterWrite(session, writeRequest);{color}
{color:#ff0000}// bug 2 fix:{color}
DefaultWriteRequest defaultWriteRequest = new DefaultWriteRequest(writeRequest.getOriginalMessage(),
writeRequest.getFuture(), writeRequest.getDestination());
defaultWriteRequest.setMessage(encodedMessage);
nextFilter.filterWrite(session, defaultWriteRequest);
}
}
} catch (Exception e) {
ProtocolEncoderException pee;
// Generate the correct exception
if (e instanceof ProtocolEncoderException)
{ pee = (ProtocolEncoderException) e; }
else
{ pee = new ProtocolEncoderException(e); }
throw pee;
}
}
was:
mina v2.1.1-v2.1.3 unable to receive writeRequest message, the following code is bug and my bug fix in ProtocolCodecFilter
{color:#FF0000}Note: bug2 is major bug!{color}
/**
* \{@inheritDoc}
*/
@Override
public void filterWrite(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception {
Object message = writeRequest.getMessage();
// Bypass the encoding if the message is contained in a IoBuffer,
// as it has already been encoded before
if ((message instanceof IoBuffer) || (message instanceof FileRegion)) {
nextFilter.filterWrite(session, writeRequest);
return;
}
// Get the encoder in the session
ProtocolEncoder encoder = factory.getEncoder(session);
{color:#FF0000}// bug 1:{color} {color:#00875a}the EncoderOut is not thread safe that may lead to duplicated message sent...{color}
{color:#00875a} // ProtocolEncoderOutput encoderOut = getEncoderOut(session, nextFilter, writeRequest);{color}
{color:#FF0000}// bug 1 fix:{color}
ProtocolEncoderOutput encoderOut = new ProtocolEncoderOutputImpl(session, nextFilter, writeRequest);
if (encoder == null) {
throw new ProtocolEncoderException("The encoder is null for the session " + session);
}
try {
// Now we can try to encode the response
encoder.encode(session, message, encoderOut);
// Send it directly
Queue<Object> bufferQueue = ((AbstractProtocolEncoderOutput) encoderOut).getMessageQueue();
// Write all the encoded messages now
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()) {
{color:#FF0000}// major bug 2:{color} {color:#00875a}the writeRequest resets when the code enters while loop sencondly,{color}
{color:#00875a}// it will lead to some writeRequest is missed...{color}
{color:#00875a} // nextFilter.filterWrite(session, writeRequest);{color}
{color:#FF0000}// bug 2 fix:{color}
DefaultWriteRequest defaultWriteRequest = new DefaultWriteRequest(writeRequest.getOriginalMessage(),
writeRequest.getFuture(), writeRequest.getDestination());
defaultWriteRequest.setMessage(encodedMessage);
nextFilter.filterWrite(session, defaultWriteRequest);
}
}
} catch (Exception e) {
ProtocolEncoderException pee;
// Generate the correct exception
if (e instanceof ProtocolEncoderException) {
pee = (ProtocolEncoderException) e;
} else {
pee = new ProtocolEncoderException(e);
}
throw pee;
}
}
> Find the bug(Mina v2.1.1 -v2.1.3 unable to receive writeRequest message at sometimes) and fix the bug
> -----------------------------------------------------------------------------------------------------
>
> Key: DIRMINA-1121
> URL: https://issues.apache.org/jira/browse/DIRMINA-1121
> Project: MINA
> Issue Type: Bug
> Components: Filter
> Affects Versions: 2.1.3
> Reporter: li-libo
> Priority: Major
>
> mina v2.1.1-v2.1.3 unable to receive writeRequest message, the following code is bug and my bug fix in ProtocolCodecFilter
> {color:#ff0000}Note: bug2 is major bug!{color}
> /**
> * {@inheritDoc}
> */
> @Override
> public void filterWrite(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception {
> Object message = writeRequest.getMessage();
> // Bypass the encoding if the message is contained in a IoBuffer,
> // as it has already been encoded before
> if ((message instanceof IoBuffer) || (message instanceof FileRegion)) \{ nextFilter.filterWrite(session, writeRequest); return; }
> // Get the encoder in the session
> ProtocolEncoder encoder = factory.getEncoder(session);
> {color:#ff0000}// bug 1:{color} {color:#00875a}the EncoderOut is not thread safe that may lead to duplicated message sent...{color}
> {color:#00875a} // ProtocolEncoderOutput encoderOut = getEncoderOut(session, nextFilter, writeRequest);{color}
> {color:#ff0000}// bug 1 fix:{color}
> ProtocolEncoderOutput encoderOut = new ProtocolEncoderOutputImpl(session, nextFilter, writeRequest);
> if (encoder == null)
> { throw new ProtocolEncoderException("The encoder is null for the session " + session); }
> try {
> // Now we can try to encode the response
> encoder.encode(session, message, encoderOut);
> // Send it directly
> Queue<Object> bufferQueue = ((AbstractProtocolEncoderOutput) encoderOut).getMessageQueue();
> // Write all the encoded messages now
> 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()) {
> {color:#ff0000}// major bug 2:{color} {color:#00875a}the writeRequest resets when the code enters while loop sencondly,{color}
> {color:#00875a}// it will lead to some writeRequest is missed...{color}
> {color:#00875a}// writeRequest.setMessage(encodedMessage);{color}
> {color:#00875a} // nextFilter.filterWrite(session, writeRequest);{color}
> {color:#ff0000}// bug 2 fix:{color}
> DefaultWriteRequest defaultWriteRequest = new DefaultWriteRequest(writeRequest.getOriginalMessage(),
> writeRequest.getFuture(), writeRequest.getDestination());
> defaultWriteRequest.setMessage(encodedMessage);
> nextFilter.filterWrite(session, defaultWriteRequest);
> }
> }
> } catch (Exception e) {
> ProtocolEncoderException pee;
> // Generate the correct exception
> if (e instanceof ProtocolEncoderException)
> { pee = (ProtocolEncoderException) e; }
> else
> { pee = new ProtocolEncoderException(e); }
> throw pee;
> }
> }
--
This message was sent by Atlassian Jira
(v8.3.4#803005)
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@mina.apache.org
For additional commands, e-mail: dev-help@mina.apache.org