You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by pingvishal <pi...@gmail.com> on 2007/10/12 12:25:41 UTC

ByteBuffer capacity shrinks

Hello All,

am facing this strange issue of the socket's receiving Bytebuffer capacity
reducing into half (start from 4096) with each request/response. (For ex. i
start with 4096 capacity, recv a message->send a message back, when i
receive again, its reduced to half ..)
i have a SocketConnector and am using a ProtocolCodec (tried using a
CumulativeProtocolDecoder but with same result).
what am i missing and Is there any way i can handle this in my program ?

Thanks,
vishal
-- 
View this message in context: http://www.nabble.com/ByteBuffer-capacity-shrinks-tf4612562s16868.html#a13172536
Sent from the Apache MINA Support Forum mailing list archive at Nabble.com.


Re: ByteBuffer capacity shrinks with every new connection

Posted by pingvishal <pi...@gmail.com>.
:) looks like you hit that bang on !
am using version 1.1.2


Trustin Lee wrote:
> 
> On 10/15/07, pingvishal <pi...@gmail.com> wrote:
>>
>> Am using a connector actually (but i guess that doesn't matter :) )
>> when i was debugging i did notice the capacity dropping to 64 bytes.
>> However, since the time i posted the query i have been constantly
>> tinkering
>> with the code and luckily with all the inputs i got (and read in the
>> forums)
>> .. i have managed to use the CumulativeProtocol codec to decode the
>> messages
>> appropriately.
>>
>> I would definitely try to reproduce the issue for someone who can fix the
>> Mina code (if at all it turns out to be an bug :)
>>
>> Thanks much for the prompt help offered !
> 
> No problem!  I found something related:
> 
> https://issues.apache.org/jira/browse/DIRMINA-458
> 
> Thanks,
> Trustin
> -- 
> what we call human nature is actually human habit
> --
> http://gleamynode.net/
> --
> PGP Key ID: 0x0255ECA6
> 
> 

-- 
View this message in context: http://www.nabble.com/ByteBuffer-capacity-shrinks-tf4612562s16868.html#a13209066
Sent from the Apache MINA Support Forum mailing list archive at Nabble.com.


Re: ByteBuffer capacity shrinks with every new connection

Posted by Trustin Lee <tr...@gmail.com>.
On 10/15/07, pingvishal <pi...@gmail.com> wrote:
>
> Am using a connector actually (but i guess that doesn't matter :) )
> when i was debugging i did notice the capacity dropping to 64 bytes.
> However, since the time i posted the query i have been constantly tinkering
> with the code and luckily with all the inputs i got (and read in the forums)
> .. i have managed to use the CumulativeProtocol codec to decode the messages
> appropriately.
>
> I would definitely try to reproduce the issue for someone who can fix the
> Mina code (if at all it turns out to be an bug :)
>
> Thanks much for the prompt help offered !

No problem!  I found something related:

https://issues.apache.org/jira/browse/DIRMINA-458

Thanks,
Trustin
-- 
what we call human nature is actually human habit
--
http://gleamynode.net/
--
PGP Key ID: 0x0255ECA6

Re: ByteBuffer capacity shrinks with every new connection

Posted by pingvishal <pi...@gmail.com>.
Am using a connector actually (but i guess that doesn't matter :) )
when i was debugging i did notice the capacity dropping to 64 bytes.
However, since the time i posted the query i have been constantly tinkering
with the code and luckily with all the inputs i got (and read in the forums)
.. i have managed to use the CumulativeProtocol codec to decode the messages
appropriately.

I would definitely try to reproduce the issue for someone who can fix the
Mina code (if at all it turns out to be an bug :)

Thanks much for the prompt help offered !


Trustin Lee wrote:
> 
> Hi,
> 
> On 10/12/07, pingvishal <pi...@gmail.com> wrote:
>>
>> Hi,
>> its for every new message received (ignore my prev mail .. i did some
>> check
>> to post this again) ..
>> however, whats happening is .. the server has dispatched the entire
>> packet
>> and i dont receive it that way .. (the packet sizes are really small ..
>> abt
>> 200 bytes .. and the ByteBuffer capacity goes down to 64 bytes :| )
>>
>> how should i be tackling this ? store all the incoming chunks in my own
>> object (CumulativeProctocolDecoder) or is there a better way ?
> 
> I tried to reproduce your problem with the following example without
> success:
> 
> 
> 
> import java.io.OutputStream;
> import java.net.InetSocketAddress;
> import java.net.Socket;
> 
> import org.apache.mina.common.ByteBuffer;
> import org.apache.mina.common.IoHandlerAdapter;
> import org.apache.mina.common.IoSession;
> import org.apache.mina.transport.socket.nio.SocketAcceptor;
> 
> public class Main {
> 
>     private static final int SERVER_PORT = 1234;
> 
>     public static void main(String args[]) throws Exception {
>         SocketAcceptor acceptor = new SocketAcceptor();
>         acceptor.bind(new InetSocketAddress(SERVER_PORT), new
> IoHandlerAdapter() {
>             @Override
>             public void messageReceived(IoSession session, Object o) {
>                 System.out.println(((ByteBuffer) o).capacity());
>             }
>         });
> 
>         Socket s = new Socket("localhost", SERVER_PORT);
>         OutputStream out = s.getOutputStream();
>         for (int i = 0; i < 100; i++) {
>             out.write(new byte[200]);
>             Thread.sleep(100);
>         }
>     }
> }
> 
> 
> As you see, MINA doesn't decrease the read buffer size to under 256.
> Please feel free to modify the example code above to reproduce the
> problem.
> 
> HTH,
> Trustin
> -- 
> what we call human nature is actually human habit
> --
> http://gleamynode.net/
> --
> PGP Key ID: 0x0255ECA6
> 
> 

-- 
View this message in context: http://www.nabble.com/ByteBuffer-capacity-shrinks-tf4612562s16868.html#a13208541
Sent from the Apache MINA Support Forum mailing list archive at Nabble.com.


Re: ByteBuffer capacity shrinks with every new connection

Posted by Trustin Lee <tr...@gmail.com>.
Hi,

On 10/12/07, pingvishal <pi...@gmail.com> wrote:
>
> Hi,
> its for every new message received (ignore my prev mail .. i did some check
> to post this again) ..
> however, whats happening is .. the server has dispatched the entire packet
> and i dont receive it that way .. (the packet sizes are really small .. abt
> 200 bytes .. and the ByteBuffer capacity goes down to 64 bytes :| )
>
> how should i be tackling this ? store all the incoming chunks in my own
> object (CumulativeProctocolDecoder) or is there a better way ?

I tried to reproduce your problem with the following example without success:



import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;

import org.apache.mina.common.ByteBuffer;
import org.apache.mina.common.IoHandlerAdapter;
import org.apache.mina.common.IoSession;
import org.apache.mina.transport.socket.nio.SocketAcceptor;

public class Main {

    private static final int SERVER_PORT = 1234;

    public static void main(String args[]) throws Exception {
        SocketAcceptor acceptor = new SocketAcceptor();
        acceptor.bind(new InetSocketAddress(SERVER_PORT), new
IoHandlerAdapter() {
            @Override
            public void messageReceived(IoSession session, Object o) {
                System.out.println(((ByteBuffer) o).capacity());
            }
        });

        Socket s = new Socket("localhost", SERVER_PORT);
        OutputStream out = s.getOutputStream();
        for (int i = 0; i < 100; i++) {
            out.write(new byte[200]);
            Thread.sleep(100);
        }
    }
}


As you see, MINA doesn't decrease the read buffer size to under 256.
Please feel free to modify the example code above to reproduce the
problem.

HTH,
Trustin
-- 
what we call human nature is actually human habit
--
http://gleamynode.net/
--
PGP Key ID: 0x0255ECA6

Re: ByteBuffer capacity shrinks with every new connection

Posted by steve_taylor <st...@atulos.com>.
Oh dear, silly me. I didn't realise that CumulativeProtocolDecoder is
something that actually exists in MINA. How embarassing!


steve_taylor wrote:
> 
> Wow, this is really bad behaviour on the part of MINA. What were the
> developers thinking? As for having a CumulativeProtocolDecoder, this is
> always a good practice as you just can't predict what TCP/IP will do with
> packets. The best way to treat incoming TCP/IP data is as a stream. When
> implementing something like a CumulativeProtocolDecoder, bear in mind that
> one of MINA's gotchas is that you can only have one ProtocolCodecFilter
> per filter chain. So you will need to implement your own IoFilter to do
> this. Dividing an incoming stream into discrete messages is such a common
> requirement that some sort of framework for it should exist in MINA 2.0.
> Here's hoping.
> 
> - Steve
> 
> 
> 
> pingvishal wrote:
>> 
>> Hi,
>> its for every new message received (ignore my prev mail .. i did some
>> check to post this again) .. 
>> however, whats happening is .. the server has dispatched the entire
>> packet and i dont receive it that way .. (the packet sizes are really
>> small .. abt 200 bytes .. and the ByteBuffer capacity goes down to 64
>> bytes :| )
>> 
>> how should i be tackling this ? store all the incoming chunks in my own
>> object (CumulativeProctocolDecoder) or is there a better way ? 
>> 
>> vishal
>> 
>> 
>> Trustin Lee wrote:
>>> 
>>> On 10/12/07, pingvishal <pi...@gmail.com> wrote:
>>>>
>>>> Hello All,
>>>>
>>>> am facing this strange issue of the socket's receiving Bytebuffer
>>>> capacity
>>>> reducing into half (start from 4096) with every new connection ..
>>>> i have a SocketConnector and am using a ProtocolCodec (tried using a
>>>> CumulativeProtocolDecoder but with same result).
>>>> what am i missing and Is there any way i can handle this in my program
>>>> ?
>>> 
>>> With every new connection?  You mean with every new message?  If it
>>> decreases for every new connection, it's a bug.  However, if it's for
>>> every new message received, it's a normal behavior.  MINA
>>> automatically adjusts the buffer capacity per read, and the capacity
>>> will decrease to the half if the amount of bytes received is too
>>> small.  Please clarify if it's for every new connection or message.
>>> 
>>> Trustin
>>> -- 
>>> what we call human nature is actually human habit
>>> --
>>> http://gleamynode.net/
>>> --
>>> PGP Key ID: 0x0255ECA6
>>> 
>>> 
>> 
>> 
> 
> 

-- 
View this message in context: http://www.nabble.com/ByteBuffer-capacity-shrinks-tf4612562s16868.html#a13197914
Sent from the Apache MINA Support Forum mailing list archive at Nabble.com.


Re: ByteBuffer capacity shrinks with every new connection

Posted by steve_taylor <st...@atulos.com>.
Wow, this is really bad behaviour on the part of MINA. What were the
developers thinking? As for having a CumulativeProtocolDecoder, this is
always a good practice as you just can't predict what TCP/IP will do with
packets. The best way to treat incoming TCP/IP data is as a stream. When
implementing something like a CumulativeProtocolDecoder, bear in mind that
one of MINA's gotchas is that you can only have one ProtocolCodecFilter per
filter chain. So you will need to implement your own IoFilter to do this.
Dividing an incoming stream into discrete messages is such a common
requirement that some sort of framework for it should exist in MINA 2.0.
Here's hoping.

- Steve



pingvishal wrote:
> 
> Hi,
> its for every new message received (ignore my prev mail .. i did some
> check to post this again) .. 
> however, whats happening is .. the server has dispatched the entire packet
> and i dont receive it that way .. (the packet sizes are really small ..
> abt 200 bytes .. and the ByteBuffer capacity goes down to 64 bytes :| )
> 
> how should i be tackling this ? store all the incoming chunks in my own
> object (CumulativeProctocolDecoder) or is there a better way ? 
> 
> vishal
> 
> 
> Trustin Lee wrote:
>> 
>> On 10/12/07, pingvishal <pi...@gmail.com> wrote:
>>>
>>> Hello All,
>>>
>>> am facing this strange issue of the socket's receiving Bytebuffer
>>> capacity
>>> reducing into half (start from 4096) with every new connection ..
>>> i have a SocketConnector and am using a ProtocolCodec (tried using a
>>> CumulativeProtocolDecoder but with same result).
>>> what am i missing and Is there any way i can handle this in my program ?
>> 
>> With every new connection?  You mean with every new message?  If it
>> decreases for every new connection, it's a bug.  However, if it's for
>> every new message received, it's a normal behavior.  MINA
>> automatically adjusts the buffer capacity per read, and the capacity
>> will decrease to the half if the amount of bytes received is too
>> small.  Please clarify if it's for every new connection or message.
>> 
>> Trustin
>> -- 
>> what we call human nature is actually human habit
>> --
>> http://gleamynode.net/
>> --
>> PGP Key ID: 0x0255ECA6
>> 
>> 
> 
> 

-- 
View this message in context: http://www.nabble.com/ByteBuffer-capacity-shrinks-tf4612562s16868.html#a13197657
Sent from the Apache MINA Support Forum mailing list archive at Nabble.com.


Re: ByteBuffer capacity shrinks with every new connection

Posted by pingvishal <pi...@gmail.com>.
Hi,
its for every new message received (ignore my prev mail .. i did some check
to post this again) .. 
however, whats happening is .. the server has dispatched the entire packet
and i dont receive it that way .. (the packet sizes are really small .. abt
200 bytes .. and the ByteBuffer capacity goes down to 64 bytes :| )

how should i be tackling this ? store all the incoming chunks in my own
object (CumulativeProctocolDecoder) or is there a better way ? 

vishal


Trustin Lee wrote:
> 
> On 10/12/07, pingvishal <pi...@gmail.com> wrote:
>>
>> Hello All,
>>
>> am facing this strange issue of the socket's receiving Bytebuffer
>> capacity
>> reducing into half (start from 4096) with every new connection ..
>> i have a SocketConnector and am using a ProtocolCodec (tried using a
>> CumulativeProtocolDecoder but with same result).
>> what am i missing and Is there any way i can handle this in my program ?
> 
> With every new connection?  You mean with every new message?  If it
> decreases for every new connection, it's a bug.  However, if it's for
> every new message received, it's a normal behavior.  MINA
> automatically adjusts the buffer capacity per read, and the capacity
> will decrease to the half if the amount of bytes received is too
> small.  Please clarify if it's for every new connection or message.
> 
> Trustin
> -- 
> what we call human nature is actually human habit
> --
> http://gleamynode.net/
> --
> PGP Key ID: 0x0255ECA6
> 
> 

-- 
View this message in context: http://www.nabble.com/ByteBuffer-capacity-shrinks-tf4612562s16868.html#a13174647
Sent from the Apache MINA Support Forum mailing list archive at Nabble.com.


Re: ByteBuffer capacity shrinks with every new connection

Posted by Trustin Lee <tr...@gmail.com>.
On 10/12/07, pingvishal <pi...@gmail.com> wrote:
>
> Hello All,
>
> am facing this strange issue of the socket's receiving Bytebuffer capacity
> reducing into half (start from 4096) with every new connection ..
> i have a SocketConnector and am using a ProtocolCodec (tried using a
> CumulativeProtocolDecoder but with same result).
> what am i missing and Is there any way i can handle this in my program ?

With every new connection?  You mean with every new message?  If it
decreases for every new connection, it's a bug.  However, if it's for
every new message received, it's a normal behavior.  MINA
automatically adjusts the buffer capacity per read, and the capacity
will decrease to the half if the amount of bytes received is too
small.  Please clarify if it's for every new connection or message.

Trustin
-- 
what we call human nature is actually human habit
--
http://gleamynode.net/
--
PGP Key ID: 0x0255ECA6