r110155 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r110154‎ | r110155 | r110156 >
Date:18:32, 27 January 2012
Author:oren
Status:deferred
Tags:
Comment:
updated to use ConcurrentHashMap and fixed unsafe cast and pmd issues. This class may have threading issues
Modified paths:
  • /trunk/lucene-search-3/src/main/java/org/wikimedia/lsearch/frontend/HttpMonitor.java (modified) (history)

Diff [purge]

Index: trunk/lucene-search-3/src/main/java/org/wikimedia/lsearch/frontend/HttpMonitor.java
@@ -3,24 +3,25 @@
44 import java.util.ArrayList;
55 import java.util.Collections;
66 import java.util.Comparator;
7 -import java.util.Hashtable;
87 import java.util.Map.Entry;
 8+import java.util.concurrent.ConcurrentHashMap;
99
 10+import org.apache.log4j.Level;
1011 import org.apache.log4j.Logger;
1112
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>();
1718
1819 /** threshold in milliseconds for reporting */
19 - protected long threshold = 10000;
 20+ private final static long THREASHOLD = 10000;
2021
21 - private HttpMonitor(){}
 22+ private HttpMonitor(){ super();}
2223
2324 /** 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
2526 if(instance == null){
2627 instance = new HttpMonitor();
2728 instance.start();
@@ -30,58 +31,58 @@
3132 }
3233
3334 @Override
34 - public void run() {
35 - log.info("HttpMonitor thread started");
 35+ public void run() {
 36+ LOGGER.info("HttpMonitor thread started");
 37+
3638 for(;;){
3739 try {
3840 // sleep until next check
39 - Thread.sleep(threshold);
40 - long cur = System.currentTimeMillis();
 41+ Thread.sleep(THREASHOLD);
 42+ final long cur = System.currentTimeMillis();
4143
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; //
4446 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);
4850 }
4951 }
5052 } catch (InterruptedException e) {
51 - log.error("HttpMonitor thread interrupted",e);
 53+ LOGGER.error("HttpMonitor thread interrupted",e);
5254 }
5355 }
5456 }
5557
56 - /** Mark http request start */
57 - public void requestStart(HttpHandler thread){
 58+ /** Mark HTTP request start */
 59+ public void requestStart(final HttpHandler thread){
5860 startTimes.put(thread,System.currentTimeMillis());
5961 }
6062
61 - /** Mark http request end */
62 - public void requestEnd(HttpHandler thread){
 63+ /** Mark HTTP request end */
 64+ public void requestEnd(final HttpHandler thread){
6365 startTimes.remove(thread);
6466 }
6567
6668 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());
7172 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());
7576 }
7677 });
7778
78 - long cur = System.currentTimeMillis();
 79+ final long cur = System.currentTimeMillis();
7980
8081 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");
8384 }
8485
85 - return sb.toString();
 86+ return stringBuilder.toString();
8687 }
8788
8889 }

Status & tagging log