rschildmeijer / deft

High performance non blocking web framework
http://www.deftserver.org
Apache License 2.0
196 stars 25 forks source link

Thread safe JMX #102

Open rschildmeijer opened 13 years ago

rschildmeijer commented 13 years ago

The current JMX usage is not thread safe. (The "jmx thread" is accessing methods on e.g IOLoop.)

One way to solve this is to use IOLoop.addCallback and explicit locking. e.g (semi pseudo, missing some try catch etc) @Override public int getNumberOfRegisteredIOHandlers() { //return handlers.size(); final Object lock = new Object(); // maybe a ReentrantLock is more appropriate final int[] rv = new int[1]; IOLoop.INSTANCE.addCallback(new AsyncCallback() { @Override public void onCallback() { rv[0] = handlers.size(); lock.notifyAll(); } } ); synchronized (lock) { lock.wait(); } return rv[0]; }

rschildmeijer commented 13 years ago

one drawback -- its very dangerous to invoke this from the IOLoop. So maybe we need to check which thread is invoking the method.

rschildmeijer commented 13 years ago

Tried with j.u.c.CountDownLatch, works fine. Request for comments

https://gist.github.com/813636

rschildmeijer commented 13 years ago

Patch ready for review: https://gist.github.com/856549

Drawbacks. 1, Extremely dangerous to invoke the public jmx methods from the io loop thread. 2, premature optimization? I havent seen a CME in action (yet) because of interleaving threads in the jmx apis.