You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by Trustin Lee <tr...@gmail.com> on 2007/11/03 12:42:45 UTC
Please review a whole new traffic throttling filters! :D
Hi community,
Recently, I've been working on traffic throttling filters, which
prevents unwanted OutOfMemoryError due to high bandwidth asynchronous
operations, and I want to get as much feed back as possible from you.
Currently, you can specify pre-programmed on-flood policy and set
per-session, per-service and global threshold both for reads and for
writes.
I am still thinking out loudly about integrating traffic shaping into
these filters. Any idea is welcome!
Please browse the following directory, and feel free to give us some feed back:
http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/filter/traffic/
Because the documentation is not ready yet, let me show you some examples:
ReadThrottleFilterTest.java:
public class ReadThrottleFilterTest {
// Try to beat with the following command:
// while true; do echo 1234567890; done | nc localhost 1234
public static void main(String[] args) throws Exception {
SocketAcceptor acceptor = new NioSocketAcceptor();
acceptor.getFilterChain().addLast("executor", new ExecutorFilter());
acceptor.getFilterChain().addLast("readThrottle", new
ReadThrottleFilter(ReadThrottlePolicy.BLOCK));
acceptor.setHandler(new IoHandlerAdapter() {
@Override
public void messageReceived(IoSession session, Object message)
throws Exception {
Thread.sleep(1000);
}
});
acceptor.setLocalAddress(new InetSocketAddress(1234));
acceptor.bind();
}
}
SlowServer.java and WriteThrottleFilterTest.java: (Launch the
SlowServer and see if WriteThrottleFilterTest doesn't flood.)
public class SlowServer {
public static void main(String[] args) throws Exception {
SocketAcceptor acceptor = new NioSocketAcceptor();
acceptor.getSessionConfig().setReceiveBufferSize(1024);
acceptor.setLocalAddress(new InetSocketAddress(1234));
acceptor.setHandler(new IoHandlerAdapter() {
@Override
public void messageReceived(IoSession session, Object message)
throws Exception {
Thread.sleep(1000);
}
});
acceptor.bind();
}
}
--- and ---
public class WriteThrottleFilterTest {
public static void main(String[] args) throws Exception {
SocketConnector connector = new NioSocketConnector();
connector.getFilterChain().addLast(
"codec", new ProtocolCodecFilter(
new TextLineCodecFactory()));
connector.getSessionConfig().setSendBufferSize(2048);
connector.getFilterChain().addLast(
"writeThrottle", new
WriteThrottleFilter(WriteThrottlePolicy.BLOCK, 10, 0, 10, 0, 20, 0));
connector.setHandler(new IoHandlerAdapter());
IoSession session1 = connector.connect(new
InetSocketAddress("localhost",
1234)).awaitUninterruptibly().getSession();
IoSession session2 = connector.connect(new
InetSocketAddress("localhost",
1234)).awaitUninterruptibly().getSession();
String data = "";
for (int i = 0; i < 1024; i ++) {
data += ".";
}
for (;;) {
session1.write(data);
session2.write(data);
System.out.println(session1.getScheduledWriteMessages());
Thread.sleep(100);
}
}
}
--
what we call human nature is actually human habit
--
http://gleamynode.net/
--
PGP Key ID: 0x0255ECA6
Re: Please review a whole new traffic throttling filters! :D
Posted by Trustin Lee <tr...@gmail.com>.
On Nov 11, 2007 6:36 AM, Luis Neves <lu...@co.sapo.pt> wrote:
> There is a "timing" issue with the "resumeOhters()" function.
> If a client is blocked and all others client sessions are closed the
> resumeOthers won't be called and the client doesn't resume.
Thanks for keep testing the filter first of all. :)
I thought about reviving stalled sessions without any help of external
threads but it seems like its overly complicated to resolve in that
way. So I added a ScheduledExecutorService parameter to constructors.
resumeOthers() is invoked from the scheduled executor every 3 seconds
to take care of the case you mentioned. I think 3 seconds is fine
because the case won't happen that often. Please suggest me saner
default value if it doesn't work very well.
Cheers,
Trustin
--
what we call human nature is actually human habit
--
http://gleamynode.net/
--
PGP Key ID: 0x0255ECA6
Re: Please review a whole new traffic throttling filters! :D
Posted by Luis Neves <lu...@co.sapo.pt>.
Trustin Lee wrote:
> On 11/8/07, Trustin Lee <tr...@gmail.com> wrote:
>> On 11/8/07, Luis Neves <lu...@co.sapo.pt> wrote:
>>> Hello,
>>>
>>> Trustin Lee wrote:
>>>> Hi community,
>>>>
>>>> Recently, I've been working on traffic throttling filters, which
>>>> prevents unwanted OutOfMemoryError due to high bandwidth asynchronous
>>>> operations, and I want to get as much feed back as possible from you.
>>> Looking at this again...
>>>
>>> I'm having issues with multiple clients. It seems that using the
>>> ReadThrottleFilter "attached" to a SocketAcceptor makes it impossible for the
>>> server to process messages from more that one client (when all the clients are
>>> "blasting" the server).
>>> What I'm seeing is that a blocked client doesn't resume message production
>>> (ever). I will try to track the reason for this. Looking at the code it seems
>>> that it would make sense to pass a reference to the IoService, so that we could
>>> call "session.resumeRead()" for all "getManagedSessions()"... but i could be way
>>> off, what do you think?
>> I think it is the only solution to fix this bug and support
>> per-service and global read throttling, but it's really really
>> expensive. The filter will have to iterate a set of sessions for
>> every messageReceived event, which is an overkill. We probably might
>> need to drop the current per-service and global read throttling and
>> find an alternative way. WDYT?
>
> Hmm.. however, read suspension seems to work pretty well. We might be
> able to reduce the overhead by reducing the execution frequency of
> resume check code.
>
> Trustin
There is a "timing" issue with the "resumeOhters()" function.
If a client is blocked and all others client sessions are closed the
resumeOthers won't be called and the client doesn't resume.
--
Luis Neves
Re: Please review a whole new traffic throttling filters! :D
Posted by Luis Neves <lu...@co.sapo.pt>.
Trustin Lee wrote:
> On 11/8/07, Luis Neves <lu...@co.sapo.pt> wrote:
>> Trustin Lee wrote:
>>> Please try the trunk right now. It should work fine now.
>> I had to make the same small change as previously, but other than that it works
>> perfect.
>
> What was the change?
>
In the exit() function
change this:
if (state.suspendedRead &&
(maxGlobalBufferSize == 0 || globalBufferSize < maxGlobalBufferSize) &&
(maxServiceBufferSize == 0 || serviceBufferSize < maxServiceBufferSize) &&
(maxSessionBufferSize == 0 || sessionBufferSize < maxSessionBufferSize)) {
state.suspendedRead = false;
enforcePolicy = true;
}
to:
if (
(maxGlobalBufferSize == 0 || globalBufferSize < maxGlobalBufferSize) &&
(maxServiceBufferSize == 0 || serviceBufferSize < maxServiceBufferSize) &&
(maxSessionBufferSize == 0 || sessionBufferSize < maxSessionBufferSize)) {
state.suspendedRead = false;
enforcePolicy = true;
}
--
Luis Neves
Re: Please review a whole new traffic throttling filters! :D
Posted by Trustin Lee <tr...@gmail.com>.
On 11/8/07, Luis Neves <lu...@co.sapo.pt> wrote:
> Trustin Lee wrote:
> > Please try the trunk right now. It should work fine now.
>
> I had to make the same small change as previously, but other than that it works
> perfect.
What was the change?
Thanks,
Trustin
--
what we call human nature is actually human habit
--
http://gleamynode.net/
--
PGP Key ID: 0x0255ECA6
Re: Please review a whole new traffic throttling filters! :D
Posted by Luis Neves <lu...@co.sapo.pt>.
Trustin Lee wrote:
> Please try the trunk right now. It should work fine now.
I had to make the same small change as previously, but other than that it works
perfect.
Thanks for your work!
--
Luis Neves
Re: Please review a whole new traffic throttling filters! :D
Posted by Trustin Lee <tr...@gmail.com>.
Please try the trunk right now. It should work fine now.
HTH,
Trustin
On 11/8/07, Luis Neves <lu...@co.sapo.pt> wrote:
> Trustin Lee wrote:
>
> >>> Looking at this again...
> >>>
> >>> I'm having issues with multiple clients. It seems that using the
> >>> ReadThrottleFilter "attached" to a SocketAcceptor makes it impossible for the
> >>> server to process messages from more that one client (when all the clients are
> >>> "blasting" the server).
> >>> What I'm seeing is that a blocked client doesn't resume message production
> >>> (ever). I will try to track the reason for this. Looking at the code it seems
> >>> that it would make sense to pass a reference to the IoService, so that we could
> >>> call "session.resumeRead()" for all "getManagedSessions()"... but i could be way
> >>> off, what do you think?
> >> I think it is the only solution to fix this bug and support
> >> per-service and global read throttling, but it's really really
> >> expensive. The filter will have to iterate a set of sessions for
> >> every messageReceived event, which is an overkill. We probably might
> >> need to drop the current per-service and global read throttling and
> >> find an alternative way. WDYT?
>
> Yeah... it seems hideously expensive.
> I think that I don't need that kind of control (session, service, global), my
> only use for the ReadThrottleFilter is to make the JVM not crash with OOM errors :-)
> Although I understand the arguments for fine grained control, I can live happily
> with only a global limit (shared for all the sessions)... so to solve this
> problem I would probably set the limit per session to something like:
>
> min(512K, (globalBufferSize /number of sessions))
>
> or something similar.
>
>
> > Hmm.. however, read suspension seems to work pretty well. We might be
> > able to reduce the overhead by reducing the execution frequency of
> > resume check code.
>
> That is also a possibility but I'm leaning to the simpler solution (limit only
> for session), but you know you baby better than me.
>
> --
> Luis Neves
>
>
--
what we call human nature is actually human habit
--
http://gleamynode.net/
--
PGP Key ID: 0x0255ECA6
Re: Please review a whole new traffic throttling filters! :D
Posted by Luis Neves <lu...@co.sapo.pt>.
Trustin Lee wrote:
>>> Looking at this again...
>>>
>>> I'm having issues with multiple clients. It seems that using the
>>> ReadThrottleFilter "attached" to a SocketAcceptor makes it impossible for the
>>> server to process messages from more that one client (when all the clients are
>>> "blasting" the server).
>>> What I'm seeing is that a blocked client doesn't resume message production
>>> (ever). I will try to track the reason for this. Looking at the code it seems
>>> that it would make sense to pass a reference to the IoService, so that we could
>>> call "session.resumeRead()" for all "getManagedSessions()"... but i could be way
>>> off, what do you think?
>> I think it is the only solution to fix this bug and support
>> per-service and global read throttling, but it's really really
>> expensive. The filter will have to iterate a set of sessions for
>> every messageReceived event, which is an overkill. We probably might
>> need to drop the current per-service and global read throttling and
>> find an alternative way. WDYT?
Yeah... it seems hideously expensive.
I think that I don't need that kind of control (session, service, global), my
only use for the ReadThrottleFilter is to make the JVM not crash with OOM errors :-)
Although I understand the arguments for fine grained control, I can live happily
with only a global limit (shared for all the sessions)... so to solve this
problem I would probably set the limit per session to something like:
min(512K, (globalBufferSize /number of sessions))
or something similar.
> Hmm.. however, read suspension seems to work pretty well. We might be
> able to reduce the overhead by reducing the execution frequency of
> resume check code.
That is also a possibility but I'm leaning to the simpler solution (limit only
for session), but you know you baby better than me.
--
Luis Neves
Re: Please review a whole new traffic throttling filters! :D
Posted by Trustin Lee <tr...@gmail.com>.
On 11/8/07, Trustin Lee <tr...@gmail.com> wrote:
> On 11/8/07, Luis Neves <lu...@co.sapo.pt> wrote:
> > Hello,
> >
> > Trustin Lee wrote:
> > > Hi community,
> > >
> > > Recently, I've been working on traffic throttling filters, which
> > > prevents unwanted OutOfMemoryError due to high bandwidth asynchronous
> > > operations, and I want to get as much feed back as possible from you.
> >
> > Looking at this again...
> >
> > I'm having issues with multiple clients. It seems that using the
> > ReadThrottleFilter "attached" to a SocketAcceptor makes it impossible for the
> > server to process messages from more that one client (when all the clients are
> > "blasting" the server).
> > What I'm seeing is that a blocked client doesn't resume message production
> > (ever). I will try to track the reason for this. Looking at the code it seems
> > that it would make sense to pass a reference to the IoService, so that we could
> > call "session.resumeRead()" for all "getManagedSessions()"... but i could be way
> > off, what do you think?
>
> I think it is the only solution to fix this bug and support
> per-service and global read throttling, but it's really really
> expensive. The filter will have to iterate a set of sessions for
> every messageReceived event, which is an overkill. We probably might
> need to drop the current per-service and global read throttling and
> find an alternative way. WDYT?
Hmm.. however, read suspension seems to work pretty well. We might be
able to reduce the overhead by reducing the execution frequency of
resume check code.
Trustin
--
what we call human nature is actually human habit
--
http://gleamynode.net/
--
PGP Key ID: 0x0255ECA6
Re: Please review a whole new traffic throttling filters! :D
Posted by Trustin Lee <tr...@gmail.com>.
On 11/8/07, Luis Neves <lu...@co.sapo.pt> wrote:
> Hello,
>
> Trustin Lee wrote:
> > Hi community,
> >
> > Recently, I've been working on traffic throttling filters, which
> > prevents unwanted OutOfMemoryError due to high bandwidth asynchronous
> > operations, and I want to get as much feed back as possible from you.
>
> Looking at this again...
>
> I'm having issues with multiple clients. It seems that using the
> ReadThrottleFilter "attached" to a SocketAcceptor makes it impossible for the
> server to process messages from more that one client (when all the clients are
> "blasting" the server).
> What I'm seeing is that a blocked client doesn't resume message production
> (ever). I will try to track the reason for this. Looking at the code it seems
> that it would make sense to pass a reference to the IoService, so that we could
> call "session.resumeRead()" for all "getManagedSessions()"... but i could be way
> off, what do you think?
I think it is the only solution to fix this bug and support
per-service and global read throttling, but it's really really
expensive. The filter will have to iterate a set of sessions for
every messageReceived event, which is an overkill. We probably might
need to drop the current per-service and global read throttling and
find an alternative way. WDYT?
Trustin
--
what we call human nature is actually human habit
--
http://gleamynode.net/
--
PGP Key ID: 0x0255ECA6
Re: Please review a whole new traffic throttling filters! :D
Posted by Luis Neves <lu...@co.sapo.pt>.
Hello,
Trustin Lee wrote:
> Hi community,
>
> Recently, I've been working on traffic throttling filters, which
> prevents unwanted OutOfMemoryError due to high bandwidth asynchronous
> operations, and I want to get as much feed back as possible from you.
Looking at this again...
I'm having issues with multiple clients. It seems that using the
ReadThrottleFilter "attached" to a SocketAcceptor makes it impossible for the
server to process messages from more that one client (when all the clients are
"blasting" the server).
What I'm seeing is that a blocked client doesn't resume message production
(ever). I will try to track the reason for this. Looking at the code it seems
that it would make sense to pass a reference to the IoService, so that we could
call "session.resumeRead()" for all "getManagedSessions()"... but i could be way
off, what do you think?
--
Luis Neves
Re: Please review a whole new traffic throttling filters! :D
Posted by Trustin Lee <tr...@gmail.com>.
Hi Luis,
I got a response finally. :)
Thanks for the patch, and I've just checked it in.
HTH,
Trustin
On 11/6/07, Luis Neves <lu...@co.sapo.pt> wrote:
>
> Hello.
>
> Trustin Lee wrote:
> > Hi community,
> >
> > Recently, I've been working on traffic throttling filters, which
> > prevents unwanted OutOfMemoryError due to high bandwidth asynchronous
> > operations, and I want to get as much feed back as possible from you.
>
> Since you've asked :-)
> I had to make a small change in the exit() method ohterwise my Connector would
> not resume reading:
>
> --- /trunk/src/org/apache/mina/filter/traffic/ReadThrottleFilter.java Tue Nov
> 06 12:47:18 2007
> +++ /labs/src/org/apache/mina/filter/traffic/ReadThrottleFilter.java Tue Nov
> 06 12:49:08 2007
> @@ -440,7 +440,7 @@
> throw new IllegalStateException("sessionBufferSize < 0");
> }
>
> - if (state.suspendedRead &&
> + if (
> (maxSessionBufferSize == 0 || sessionBufferSize <
> maxSessionBufferSize) &&
> (maxServiceBufferSize == 0 || serviceBufferSize <
> maxServiceBufferSize) &&
> (maxGlobalBufferSize == 0 || globalBufferSize <
> maxGlobalBufferSize)) {
>
>
>
>
> --
> Luis Neves
>
--
what we call human nature is actually human habit
--
http://gleamynode.net/
--
PGP Key ID: 0x0255ECA6
Re: Please review a whole new traffic throttling filters! :D
Posted by Luis Neves <lu...@co.sapo.pt>.
Hello.
Trustin Lee wrote:
> Hi community,
>
> Recently, I've been working on traffic throttling filters, which
> prevents unwanted OutOfMemoryError due to high bandwidth asynchronous
> operations, and I want to get as much feed back as possible from you.
Since you've asked :-)
I had to make a small change in the exit() method ohterwise my Connector would
not resume reading:
--- /trunk/src/org/apache/mina/filter/traffic/ReadThrottleFilter.java Tue Nov
06 12:47:18 2007
+++ /labs/src/org/apache/mina/filter/traffic/ReadThrottleFilter.java Tue Nov
06 12:49:08 2007
@@ -440,7 +440,7 @@
throw new IllegalStateException("sessionBufferSize < 0");
}
- if (state.suspendedRead &&
+ if (
(maxSessionBufferSize == 0 || sessionBufferSize <
maxSessionBufferSize) &&
(maxServiceBufferSize == 0 || serviceBufferSize <
maxServiceBufferSize) &&
(maxGlobalBufferSize == 0 || globalBufferSize <
maxGlobalBufferSize)) {
--
Luis Neves