NIHVIVO-3302 Clarify whether the search index is being rebuilt or updated, and how long it has taken so far.

This commit is contained in:
j2blake 2011-11-11 23:06:38 +00:00
parent 6d8494f7f6
commit 8e3c156f9e
4 changed files with 58 additions and 13 deletions

View file

@ -3,6 +3,8 @@
package edu.cornell.mannlib.vitro.webapp.search.controller; package edu.cornell.mannlib.vitro.webapp.search.controller;
import java.io.IOException; import java.io.IOException;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -140,9 +142,29 @@ public class IndexController extends FreemarkerHttpServlet {
Map<String, Object> body) { Map<String, Object> body) {
WorkLevelStamp stamp = builder.getWorkLevel(); WorkLevelStamp stamp = builder.getWorkLevel();
body.put("worklevel", stamp.getLevel().toString()); body.put("worklevel", stamp.getLevel().toString());
body.put("currentTask", figureCurrentTask(stamp.getFlags()));
body.put("since", stamp.getSince()); body.put("since", stamp.getSince());
body.put("elapsed", formatElapsedTime(stamp.getSince()));
body.put("hasPreviousBuild", stamp.getSince().getTime() > 0L); body.put("hasPreviousBuild", stamp.getSince().getTime() > 0L);
return new TemplateResponseValues(TEMPLATE_NAME, body); return new TemplateResponseValues(TEMPLATE_NAME, body);
} }
private String formatElapsedTime(Date since) {
long elapsedMillis = System.currentTimeMillis() - since.getTime();
long seconds = (elapsedMillis / 1000L) % 60L;
long minutes = (elapsedMillis / 60000L) % 60L;
long hours = elapsedMillis / 3600000L;
return String.format("%02d:%02d:%02d", hours, minutes, seconds);
}
private String figureCurrentTask(Collection<String> flags) {
if (flags.contains(IndexBuilder.FLAG_REBUILDING)) {
return "rebuilt";
} else if (flags.contains(IndexBuilder.FLAG_UPDATING)) {
return "updated";
} else {
return "";
}
}
} }

View file

@ -64,6 +64,12 @@ public class IndexBuilder extends VitroBackgroundThread {
/** Length of pause between when work comes into queue to when indexing starts */ /** Length of pause between when work comes into queue to when indexing starts */
public static final long WAIT_AFTER_NEW_WORK_INTERVAL = 500; //msec public static final long WAIT_AFTER_NEW_WORK_INTERVAL = 500; //msec
/** Flag so we can tell that the index is being updated. */
public static final String FLAG_UPDATING = "updating";
/** Flag so we can tell that the index is being rebuilt. */
public static final String FLAG_REBUILDING = "rebuilding";
/** Number of threads to use during indexing. */ /** Number of threads to use during indexing. */
protected int numberOfThreads = 10; protected int numberOfThreads = 10;
@ -152,12 +158,12 @@ public class IndexBuilder extends VitroBackgroundThread {
while(! stopRequested ){ while(! stopRequested ){
try{ try{
if( reindexRequested ){ if( reindexRequested ){
setWorkLevel(WorkLevel.WORKING); setWorkLevel(WorkLevel.WORKING, FLAG_REBUILDING);
log.debug("full re-index requested"); log.debug("full re-index requested");
indexRebuild(); indexRebuild();
setWorkLevel(WorkLevel.IDLE); setWorkLevel(WorkLevel.IDLE);
}else if( !changedStmtQueue.isEmpty() ){ }else if( !changedStmtQueue.isEmpty() ){
setWorkLevel(WorkLevel.WORKING); setWorkLevel(WorkLevel.WORKING, FLAG_UPDATING);
Thread.sleep(WAIT_AFTER_NEW_WORK_INTERVAL); //wait a bit to let a bit more work to come into the queue Thread.sleep(WAIT_AFTER_NEW_WORK_INTERVAL); //wait a bit to let a bit more work to come into the queue
log.debug("work found for IndexBuilder, starting update"); log.debug("work found for IndexBuilder, starting update");
updatedIndex(); updatedIndex();

View file

@ -4,6 +4,9 @@ package edu.cornell.mannlib.vitro.webapp.utils.threads;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ConcurrentLinkedQueue;
@ -47,9 +50,10 @@ public class VitroBackgroundThread extends Thread {
allThreads.add(new WeakReference<VitroBackgroundThread>(this)); allThreads.add(new WeakReference<VitroBackgroundThread>(this));
} }
protected void setWorkLevel(WorkLevel level) { protected void setWorkLevel(WorkLevel level, String... flags) {
log.debug("Set work level on '" + this.getName() + "' to " + level); log.debug("Set work level on '" + this.getName() + "' to " + level
stamp = new WorkLevelStamp(level); + ", flags=" + flags);
stamp = new WorkLevelStamp(level, flags);
} }
public WorkLevelStamp getWorkLevel() { public WorkLevelStamp getWorkLevel() {
@ -59,14 +63,19 @@ public class VitroBackgroundThread extends Thread {
/** /**
* An immutable object that holds both the current work level and the time * An immutable object that holds both the current work level and the time
* that it was set. * that it was set.
*
* Particular threads may want to assign additional state using zero or more
* "flags".
*/ */
public static class WorkLevelStamp { public static class WorkLevelStamp {
private final WorkLevel level; private final WorkLevel level;
private final long since; private final long since;
private final List<String> flags;
public WorkLevelStamp(WorkLevel level) { public WorkLevelStamp(WorkLevel level, String... flags) {
this.level = level; this.level = level;
this.since = System.currentTimeMillis(); this.since = System.currentTimeMillis();
this.flags = Collections.unmodifiableList(Arrays.asList(flags));
} }
public WorkLevel getLevel() { public WorkLevel getLevel() {
@ -76,5 +85,9 @@ public class VitroBackgroundThread extends Thread {
public Date getSince() { public Date getSince() {
return new Date(since); return new Date(since);
} }
public Collection<String> getFlags() {
return flags;
}
} }
} }

View file

@ -4,7 +4,7 @@
Template for the page that controls the updating or rebuilding of the Search Index. Template for the page that controls the updating or rebuilding of the Search Index.
--> -->
<h1>Search Index Status</h1> <h2>Search Index Status</h2>
<#if worklevel == "IDLE"> <#if worklevel == "IDLE">
<#if hasPreviousBuild??> <#if hasPreviousBuild??>
@ -12,12 +12,16 @@
</#if> </#if>
<form action="${actionUrl}" method="POST"> <form action="${actionUrl}" method="POST">
<input type="submit" name="update" value="Update"> <p>
Add the latest changes to the index. <input type="submit" name="update" value="Update">
<br> Add the latest changes to the index.
<input type="submit" name="rebuild" value="Rebuild"> </p>
Start with an empty index and build it completely. <p>
<input type="submit" name="rebuild" value="Rebuild">
Start with an empty index and build it completely.
</p>
</form> </form>
<#else> <#else>
<p>Active since ${since?string("hh:mm:ss a, MMMM dd, yyyy")}</p> <h3>The search index is currently being ${currentTask}.</h3>
<p>since ${since?string("hh:mm:ss a, MMMM dd, yyyy")}, elapsed time ${elapsed}</p>
</#if> </#if>