accessing servlet filters

E

eti

Hi everybody,

Is there a way for accessing a specific method of a tomcat servlet
filter ?
i.e. I'd like to develop some sensors to measure certain parameters
regarding my web application, e.g. throughput, latency. At sampling
time a "sensor" thread wakes up and accesses certain current data in
filters, well those spawned at that moment.

So is there possible, within a context, read data from all filters
instances that in a specific moment of time are instantiated ?

Best regards
Etienne
 
W

Wendy S

eti said:
Is there a way for accessing a specific method of a tomcat servlet
filter ?
i.e. I'd like to develop some sensors to measure certain parameters
regarding my web application, e.g. throughput, latency. At sampling
time a "sensor" thread wakes up and accesses certain current data in
filters, well those spawned at that moment.
So is there possible, within a context, read data from all filters
instances that in a specific moment of time are instantiated ?

Have you considered having the Filter put the information in a place where
it can be accessed by whatever needs it? You say you need to access it
within the context, so you could save it as context (application scope)
attributes.

But given the info you're interested in, I'd probably write it to a database
(or text file) so you can import it into a spreadsheet and make graphs, etc.
This could be as easy as logging statements in your Filter, depending on how
you're set up. Then parse the logs and analyze the data.
 
E

eti

Dear Wendy,
Have you considered having the Filter put the information in a place where
it can be accessed by whatever needs it? You say you need to access it
within the context, so you could save it as context (application scope)
attributes.

Yap, of course !
Performance is of great concern. I think that sinchronized operations
to write a context variable or a sensor variable in a high concurrent
environment, would be a bottleneck and jam the system.
But given the info you're interested in, I'd probably write it to a database
(or text file) so you can import it into a spreadsheet and make graphs, etc.
This could be as easy as logging statements in your Filter, depending on how
you're set up. Then parse the logs and analyze the data.

Nope, need them at runtime, as part of a adaptive behaviour I want my
WebApp to achieve.

Thanks a lot
Best Regards
Etienne
 
W

William Brogden

eti said:
Dear Wendy,


Yap, of course !
Performance is of great concern. I think that sinchronized operations
to write a context variable or a sensor variable in a high concurrent
environment, would be a bottleneck and jam the system.

How did you come to that conclusion? Did you actually measure it?
There are so many other time consuming things going on in the
servlet environment that I doubt you could detect the effect of
synchronizing this very fast operation.
 
E

eti

Dear William,
How did you come to that conclusion? Did you actually measure it?
There are so many other time consuming things going on in the
servlet environment that I doubt you could detect the effect of
synchronizing this very fast operation.

I did the following:
- given a high number of threads, i.e. 100;
- given an object with a synchronized method accessed concurrently by
the above threads;
- that method implements a sleep, randomly set between 5 to 10 ms;

Result;
- the overall latency due by all concurrent threads accessing the
object synchronized method grows with the number of accessing threads,
well far beyond 100 ms, that is more that 10 times the espected time
to execute the very same method with no synchronization constraints !
As I can suppose this is due to the "long" queue of all threads
waiting to access that method: they are all queued up becaue of the
synch lock, and they need to wait previous threads to release it
before accessing the object synch method.

Notice that in the same context with the same object but with NO
synchronization on that method, the overall latency of all accessing
threads is as aspected: the mean value of the random sequence of sleep
timeouts, between 5 and 10 ms !! -:)

Now in our filter sevlet context: the filters are the concurrent
threads that access a webapp context synchronized variable or a
"sensor" synchronized method to store their current latency value.
They are all queued up and the overall latency, as perceived by the
client, would be excessively high !!!!

On the other hand, if I can act in a reverse fashion and access from
my sensor the actual existing filter threads to read in their "latency
field", NO synchronization is needed. The problem is just that I have
to know at any given (sampling) time the curren number and istances of
the spawned filter threads.

Regards
Etienne
 
J

John C. Bollinger

eti said:
Dear William,




I did the following:
- given a high number of threads, i.e. 100;
- given an object with a synchronized method accessed concurrently by
the above threads;
- that method implements a sleep, randomly set between 5 to 10 ms;

Result;
- the overall latency due by all concurrent threads accessing the
object synchronized method grows with the number of accessing threads,
well far beyond 100 ms, that is more that 10 times the espected time
to execute the very same method with no synchronization constraints !
As I can suppose this is due to the "long" queue of all threads
waiting to access that method: they are all queued up becaue of the
synch lock, and they need to wait previous threads to release it
before accessing the object synch method.

This kind of synthetic test is only meaningful if it models your
application well. I would strongly suspect that it does not.

In the first place, 5-10 ms may well be an excessive estimate of the
time that the critical sections would consume. In fact, depending on
how you do it, I think it could be extremely excessive.

In the second place, in your actual app you are unlikely to have all the
threads contending for the same resource at precisely the same time. In
fact, if there is one thread per sensor and one store per sensor then
the sensor threads don't need to contend with each other at all. Even
if some or all of the threads did contend for the same resource(s),
staggering their access in time would reduce the apparent mean
synchronization delay that each would see, and you'll likely get such
staggering for free.
Notice that in the same context with the same object but with NO
synchronization on that method, the overall latency of all accessing
threads is as aspected: the mean value of the random sequence of sleep
timeouts, between 5 and 10 ms !! -:)

But that's irrelevant if your app won't work correctly unsynchronized.
If it will then you are foolish to worry about synchronization in the
first place.
Now in our filter sevlet context: the filters are the concurrent
threads that access a webapp context synchronized variable or a
"sensor" synchronized method to store their current latency value.
They are all queued up and the overall latency, as perceived by the
client, would be excessively high !!!!

You tread very uncertain ground making performance predictions, and
using multiple exclamation points doesn't improve your likelihood of
being right. There is cerainly a potential performance problem here,
but whether it would be realized as an actual problem is impossible to
say without trying it.

I think you would be wise to avoid having a large number of threads all
frequently synchronizing on the same object, but that is by no means the
only way it could be done.
On the other hand, if I can act in a reverse fashion and access from
my sensor the actual existing filter threads to read in their "latency
field", NO synchronization is needed.

Sorry, but that's incorrect, or at least unwise. You still need to
synchronize the acts of updating the filters' data and reading the
filters' data. There is not so much potential for contention here, however.
The problem is just that I have
to know at any given (sampling) time the curren number and istances of
the spawned filter threads.

And that is part of the reason why it's a poor design choice. The
application context is where information shared among disconnected
application components should go. Just what you store there and how you
access it is the key to solving your problem.

You could proceed more or less as you seem to want by putting some sort
of registry of your filters in the app context. There would still be
synchronization requirements there, but not with the same degree of lock
contention you anticipate.

It is much cleaner, though, to just store some kind of data cubbyholes
in the app context. For instance, put a List in the app context, and
each filter puts an int[1] in the List, which it will use to expose its
current data. (The List takes the place of a filter registry, and has
similar synchronization requirements; the arrays are the shared data
exposed by the filters.) Accesses to the int[]s should still be
synchronized, but you only have two threads contending for each one, and
no more synchronization requirement than if you were executing methods
on the filters themselves. (The actual form of the data objects used is
pretty open; int[] was used as an example, but it is not the best choice
from an OO standpoint.)


John Bollinger
(e-mail address removed)
 
C

Chris Smith

Etienne,

Unfortunately, you're thinking too hard and not checking your
assumptions. Specifically:

1. Your test of performance eliminated all of the other mitigating
variables in a servlet environment. For goodness sake, in the real
problem you're also doing network I/O! You can't just ignore factors
like that when assessing the probable performance of the system.

2. Your performance model includes a Thread.sleep in synchronized code
on a contested monitor. That's widely known to be very poor decision
for threading performance... but no one suggested you do it. The
synchronized methods that you're actually considering using are going to
be far less costly than a Thread.sleep (which on most platforms puts you
out for at LEAST about a 50th of a second regardless of the delay you
specify). I don't understand why you're so concerned about
synchronizing something like ServletContext.getAttribute, which will
probably take something on the order of 50 processor cycles -- about a
26-millionth of a second on my 1.3 GHz system here -- as an absolute
max.

3. You've managed to recreate a "thundering herd" problem in your
performance model. I see no reason to believe that this pathological
case would occur in your real application.

4. MOST IMPORTANTLY, you're wrong that no synchronization would be
necessary to access a field of the ServletFilter. Synchronization is
needed when accessing ANY value that is shared by threads and mutated by
even one of them, regardless of whether the variable access in question
is a mutation or not.

The general moral of the story is that: (a) performance problems are
hard, andyou shouldn't try to answer them without being *very* careful
about the variables involved, and (b) multithreading is hard, and
dangerous, and you should check that what you're doing is actually
correct before you depart from accepted practice. The latter lesson is
particularly important because making a mistake will cause those
frustrating and elusive race conditions that only ever happen in
production, aren't reproducible, and thus can take months to fix and
cause you to lose customers.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top