From 89f753a728f6de3baf39b8dae9f160889a145c94 Mon Sep 17 00:00:00 2001 From: j2blake Date: Fri, 25 Nov 2011 18:48:21 +0000 Subject: [PATCH] Improve thread-safety in IndexWorkerThread counter. Add a "to-do" count. Make both counts publicly accessible. --- .../webapp/search/indexing/IndexBuilder.java | 17 ++++++--- .../search/indexing/IndexWorkerThread.java | 37 +++++++++++-------- 2 files changed, 33 insertions(+), 21 deletions(-) diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/search/indexing/IndexBuilder.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/search/indexing/IndexBuilder.java index 4a2e99108..0cb6e3637 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/search/indexing/IndexBuilder.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/search/indexing/IndexBuilder.java @@ -339,8 +339,6 @@ public class IndexBuilder extends VitroBackgroundThread { if( numberOfThreads > MAX_THREADS ) numberOfThreads = MAX_THREADS; - IndexWorkerThread.setStartTime(System.currentTimeMillis()); - //make lists of work URIs for workers List> workLists = makeWorkerUriLists(updateUris, numberOfThreads); @@ -349,7 +347,10 @@ public class IndexBuilder extends VitroBackgroundThread { for(int i = 0; i< numberOfThreads ;i++){ Iterator workToDo = new UriToIndividualIterator(workLists.get(i), wdf); workers.add( new IndexWorkerThread(indexer, i, workToDo) ); - } + } + + // reset the counters so we can monitor the progress + IndexWorkerThread.resetCounters(System.currentTimeMillis(), figureWorkLoad(workLists)); log.debug("Starting the building and indexing of documents in worker threads"); // starting worker threads @@ -371,8 +372,6 @@ public class IndexBuilder extends VitroBackgroundThread { return; } } - - IndexWorkerThread.resetCount(); } /* maybe ObjectSourceIface should be replaced with just an iterator. */ @@ -423,6 +422,14 @@ public class IndexBuilder extends VitroBackgroundThread { return work; } + private long figureWorkLoad(List> workLists) { + long load = 0; + for (List list: workLists) { + load += list.size(); + } + return load; + } + private static class UriLists { private final List updatedUris = new ArrayList(); private final List deletedUris = new ArrayList(); diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/search/indexing/IndexWorkerThread.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/search/indexing/IndexWorkerThread.java index 4b2bf950f..d7f75d184 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/search/indexing/IndexWorkerThread.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/search/indexing/IndexWorkerThread.java @@ -3,6 +3,7 @@ package edu.cornell.mannlib.vitro.webapp.search.indexing; import java.util.Iterator; +import java.util.concurrent.atomic.AtomicLong; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -21,7 +22,8 @@ class IndexWorkerThread extends Thread{ protected boolean stopRequested = false; private Log log = LogFactory.getLog(IndexWorkerThread.class); - private static long count=0; + private static AtomicLong countCompleted= new AtomicLong(); + private static AtomicLong countToIndex= new AtomicLong(); private static long starttime = 0; public IndexWorkerThread(IndexerIface indexer, int threadNum , Iterator individualsToIndex){ @@ -71,16 +73,14 @@ class IndexWorkerThread extends Thread{ } - synchronized(this){ - count++; - if( log.isInfoEnabled() ){ - if( (count % 100 ) == 0 && count > 0 ){ - long dt = (System.currentTimeMillis() - starttime); - log.info("individuals indexed: " + count + " in " + dt + " msec " + - " time per individual = " + (dt / count) + " msec" ); - } - } - } + long countNow = countCompleted.incrementAndGet(); + if( log.isInfoEnabled() ){ + if( (countNow % 100 ) == 0 ){ + long dt = (System.currentTimeMillis() - starttime); + log.info("individuals indexed: " + countNow + " in " + dt + " msec " + + " time per individual = " + (dt / countNow) + " msec" ); + } + } }catch(Throwable th){ //on tomcat shutdown odd exceptions get thrown and log can be null if( log != null ) @@ -88,13 +88,18 @@ class IndexWorkerThread extends Thread{ } } } - - public static void resetCount(){ - count = 0; + + public static void resetCounters(long time, long workload) { + IndexWorkerThread.starttime = time; + IndexWorkerThread.countToIndex.set(workload); + IndexWorkerThread.countCompleted.set(0); } - public static void setStartTime(long startTime){ - starttime = startTime; + public static long getCount() { + return countCompleted.get(); } + public static long getCountToIndex() { + return countToIndex.get(); + } }