Index: trunk/lucene-search-3/src/main/java/org/wikimedia/lsearch/frontend/HttpMonitor.java |
— | — | @@ -3,24 +3,25 @@ |
4 | 4 | import java.util.ArrayList; |
5 | 5 | import java.util.Collections; |
6 | 6 | import java.util.Comparator; |
7 | | -import java.util.Hashtable; |
8 | 7 | import java.util.Map.Entry; |
| 8 | +import java.util.concurrent.ConcurrentHashMap; |
9 | 9 | |
| 10 | +import org.apache.log4j.Level; |
10 | 11 | import org.apache.log4j.Logger; |
11 | 12 | |
12 | | -public class HttpMonitor extends Thread { |
13 | | - static Logger log = Logger.getLogger(HttpMonitor.class); |
14 | | - protected static HttpMonitor instance; |
15 | | - /** times when http request have been started */ |
16 | | - protected Hashtable<HttpHandler,Long> startTimes = new Hashtable<HttpHandler,Long>(); |
| 13 | +final public class HttpMonitor extends Thread { // NOPMD by oren on 1/27/12 6:58 PM |
| 14 | + private static final Logger LOGGER = Logger.getLogger(HttpMonitor.class); |
| 15 | + private static HttpMonitor instance; |
| 16 | + /** times when HTTP request have been started */ |
| 17 | + private final transient ConcurrentHashMap<HttpHandler,Long> startTimes = new ConcurrentHashMap<HttpHandler,Long>(); |
17 | 18 | |
18 | 19 | /** threshold in milliseconds for reporting */ |
19 | | - protected long threshold = 10000; |
| 20 | + private final static long THREASHOLD = 10000; |
20 | 21 | |
21 | | - private HttpMonitor(){} |
| 22 | + private HttpMonitor(){ super();} |
22 | 23 | |
23 | 24 | /** Get a running HttpMonitor instance */ |
24 | | - public synchronized static HttpMonitor getInstance(){ |
| 25 | + public synchronized static HttpMonitor getInstance(){ // NOPMD by oren on 1/27/12 7:09 PM |
25 | 26 | if(instance == null){ |
26 | 27 | instance = new HttpMonitor(); |
27 | 28 | instance.start(); |
— | — | @@ -30,58 +31,58 @@ |
31 | 32 | } |
32 | 33 | |
33 | 34 | @Override |
34 | | - public void run() { |
35 | | - log.info("HttpMonitor thread started"); |
| 35 | + public void run() { |
| 36 | + LOGGER.info("HttpMonitor thread started"); |
| 37 | + |
36 | 38 | for(;;){ |
37 | 39 | try { |
38 | 40 | // sleep until next check |
39 | | - Thread.sleep(threshold); |
40 | | - long cur = System.currentTimeMillis(); |
| 41 | + Thread.sleep(THREASHOLD); |
| 42 | + final long cur = System.currentTimeMillis(); |
41 | 43 | |
42 | | - // check for long-running http request |
43 | | - Hashtable<HttpHandler,Long> times = (Hashtable<HttpHandler, Long>) startTimes.clone(); // clone to avoid sync |
| 44 | + // check for long-running HTTP request |
| 45 | + final ConcurrentHashMap<HttpHandler,Long> times = (ConcurrentHashMap<HttpHandler, Long>) startTimes; // |
44 | 46 | for(Entry<HttpHandler,Long> e : times.entrySet()){ |
45 | | - long timeWait = cur - e.getValue(); |
46 | | - if(timeWait > threshold){ |
47 | | - log.warn(e.getKey()+" is waiting for "+timeWait+" ms on "+e.getKey().rawUri); |
| 47 | + final long timeWait = cur - e.getValue(); |
| 48 | + if(timeWait > THREASHOLD && LOGGER.isEnabledFor(Level.WARN) ){ |
| 49 | + LOGGER.warn(e.getKey()+" is waiting for "+timeWait+" ms on "+e.getKey().rawUri); |
48 | 50 | } |
49 | 51 | } |
50 | 52 | } catch (InterruptedException e) { |
51 | | - log.error("HttpMonitor thread interrupted",e); |
| 53 | + LOGGER.error("HttpMonitor thread interrupted",e); |
52 | 54 | } |
53 | 55 | } |
54 | 56 | } |
55 | 57 | |
56 | | - /** Mark http request start */ |
57 | | - public void requestStart(HttpHandler thread){ |
| 58 | + /** Mark HTTP request start */ |
| 59 | + public void requestStart(final HttpHandler thread){ |
58 | 60 | startTimes.put(thread,System.currentTimeMillis()); |
59 | 61 | } |
60 | 62 | |
61 | | - /** Mark http request end */ |
62 | | - public void requestEnd(HttpHandler thread){ |
| 63 | + /** Mark HTTP request end */ |
| 64 | + public void requestEnd(final HttpHandler thread){ |
63 | 65 | startTimes.remove(thread); |
64 | 66 | } |
65 | 67 | |
66 | 68 | public String printReport(){ |
67 | | - StringBuilder sb = new StringBuilder(); |
68 | | - |
69 | | - Hashtable<HttpHandler,Long> times = (Hashtable<HttpHandler, Long>) startTimes.clone(); // clone to avoid sync |
70 | | - ArrayList<Entry<HttpHandler, Long>> sorted = new ArrayList<Entry<HttpHandler,Long>>(times.entrySet()); |
| 69 | + final StringBuilder stringBuilder = new StringBuilder(); |
| 70 | + final ConcurrentHashMap<HttpHandler,Long> times = (ConcurrentHashMap<HttpHandler, Long>) startTimes; |
| 71 | + final ArrayList<Entry<HttpHandler, Long>> sorted = new ArrayList<Entry<HttpHandler,Long>>(times.entrySet()); |
71 | 72 | Collections.sort(sorted, new Comparator<Entry<HttpHandler,Long>>() { |
72 | | - public int compare(Entry<HttpHandler, Long> o1, |
73 | | - Entry<HttpHandler, Long> o2) { |
74 | | - return (int) (o2.getValue() - o1.getValue()); |
| 73 | + public int compare(final Entry<HttpHandler, Long> originalEntry, |
| 74 | + final Entry<HttpHandler, Long> otherEntry) { |
| 75 | + return (int) (otherEntry.getValue() - originalEntry.getValue()); |
75 | 76 | } |
76 | 77 | }); |
77 | 78 | |
78 | | - long cur = System.currentTimeMillis(); |
| 79 | + final long cur = System.currentTimeMillis(); |
79 | 80 | |
80 | 81 | for(Entry<HttpHandler,Long> e : sorted){ |
81 | | - long timeWait = cur - e.getValue(); |
82 | | - sb.append("[ "+timeWait+" ms ] "+ e.getKey().rawUri +"\n"); |
| 82 | + final long timeWait = cur - e.getValue(); |
| 83 | + stringBuilder.append("[ "+timeWait+" ms ] "+ e.getKey().rawUri +"\n"); |
83 | 84 | } |
84 | 85 | |
85 | | - return sb.toString(); |
| 86 | + return stringBuilder.toString(); |
86 | 87 | } |
87 | 88 | |
88 | 89 | } |