Permalink
Browse files

Fixed an Hourglass index trimming bug.

  • Loading branch information...
1 parent 1a57cb8 commit 016fec83a0a4c7f639641dfd48fff0c55f37042d Baoqiu Cui committed Jun 1, 2011
View
@@ -10,3 +10,6 @@ lib
MANIFEST.MF
!lib/tools
.settings
+*~
+TAGS
+cscope.*
@@ -153,32 +153,47 @@ public long getDiskIndexSizeBytes()
now.setTimeInMillis(timenow);
Calendar threshold = _scheduler.getTrimTime(now);
log.info("getAllArchivedDirectories loading time threshold: " + _scheduler.getFolderName(threshold));
- for(File file : files)
+
+ boolean foundOldestToKeep = false;
+
+ for (int i = files.length - 1; i >= 0; --i)
{
+ File file = files[i];
String name = file.getName();
log.debug("getAllArchivedDirectories: " + name + " " + (file.equals(_location)?"*":""));
- Calendar time = null;
- try
- {
- time = getCalendarTime(name);
- } catch (ParseException e)
- {
- log.warn("potential index corruption. we skip folder: " + name, e);
- continue;
- }
- if (time.before(threshold))
+
+ if (foundOldestToKeep)
{
log.info("getAllArchivedDirectories: skipping " + name + " for being too old");
- continue;
}
- if (!file.equals(_location))
- { // don't add the current one
+ else
+ {
+ Calendar time = null;
try
{
- list.add(getFSDirectoryFromFile(file));
- } catch (IOException e)
+ time = getCalendarTime(name);
+ }
+ catch (ParseException e)
+ {
+ log.warn("potential index corruption. we skip folder: " + name, e);
+ continue;
+ }
+
+ if (!file.equals(_location))
+ { // don't add the current one
+ try
+ {
+ list.add(getFSDirectoryFromFile(file));
+ }
+ catch (IOException e)
+ {
+ log.error("potential index corruption", e);
+ }
+ }
+
+ if (time.before(threshold))
{
- log.error("potential index corruption", e);
+ foundOldestToKeep = true;
}
}
}
@@ -4,6 +4,7 @@
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
@@ -111,29 +112,57 @@ private void trim(List<ZoieIndexReader<R>> toRemove)
Calendar now = Calendar.getInstance();
now.setTimeInMillis(timenow);
Calendar threshold = hg._scheduler.getTrimTime(now);
- for(int i=0; i<toRemove.size(); i++)
+
+ ZoieIndexReader<R>[] readerArray = toRemove.toArray(new ZoieIndexReader[toRemove.size()]);
+ Arrays.sort(readerArray,
+ new Comparator<ZoieIndexReader<R>>()
+ {
+ @Override
+ public int compare(ZoieIndexReader<R> r1, ZoieIndexReader<R> r2)
+ {
+ String name1 = ((SimpleFSDirectory) r1.directory()).getFile().getName();
+ String name2 = ((SimpleFSDirectory) r2.directory()).getFile().getName();
+ return -1 * name1.compareTo(name2);
+ }
+ });
+
+ boolean foundOldestToKeep = false;
+
+ for (ZoieIndexReader<R> reader: readerArray)
{
- SimpleFSDirectory dir = (SimpleFSDirectory) toRemove.get(i).directory();
+ SimpleFSDirectory dir = (SimpleFSDirectory) reader.directory();
String path = dir.getFile().getName();
- Calendar archivetime = null;
- try
- {
- archivetime = HourglassDirectoryManagerFactory.getCalendarTime(path);
- } catch (ParseException e)
- {
- log.error("index directory name bad. potential corruption. Move on without trimming.", e);
- toKeep.add(toRemove.get(i));
- continue;
- }
- if (archivetime.before(threshold))
+
+ if (foundOldestToKeep)
{
log.info("trimming: remove " + path);
log.info(dir.getFile() + " -before--" + (dir.getFile().exists()?" not deleted ":" deleted"));
FileUtil.rmDir(dir.getFile());
log.info(dir.getFile() + " -after--" + (dir.getFile().exists()?" not deleted ":" deleted"));
- } else
+ continue;
+ }
+ else
{
- toKeep.add(toRemove.get(i));
+ // Always keep this reader (when the oldest one to keep has not
+ // been found), no matter the index directory name can be parsed
+ // or not.
+ toKeep.add(reader);
+
+ Calendar archivetime = null;
+ try
+ {
+ archivetime = HourglassDirectoryManagerFactory.getCalendarTime(path);
+ }
+ catch (ParseException e)
+ {
+ log.error("index directory name bad. potential corruption. Move on without trimming.", e);
+ continue;
+ }
+
+ if (archivetime.before(threshold))
+ {
+ foundOldestToKeep = true;
+ }
}
}
toRemove.removeAll(toKeep);

0 comments on commit 016fec8

Please sign in to comment.