[infinispan-commits] Infinispan SVN: r1924 - in trunk/lucene-directory/src: test/java/org/infinispan/lucene/profiling and 1 other directory.

infinispan-commits at lists.jboss.org infinispan-commits at lists.jboss.org
Mon Jun 21 10:43:28 EDT 2010


Author: sannegrinovero
Date: 2010-06-21 10:43:28 -0400 (Mon, 21 Jun 2010)
New Revision: 1924

Added:
   trunk/lucene-directory/src/test/java/org/infinispan/lucene/profiling/LuceneReaderThread.java
   trunk/lucene-directory/src/test/java/org/infinispan/lucene/profiling/LuceneWriterThread.java
Modified:
   trunk/lucene-directory/src/main/java/org/infinispan/lucene/InfinispanDirectory.java
   trunk/lucene-directory/src/test/java/org/infinispan/lucene/profiling/LuceneUserThread.java
   trunk/lucene-directory/src/test/java/org/infinispan/lucene/profiling/PerformanceCompareStressTest.java
Log:
[ISPN-498] (Lucene Directory race condition on segments iterator has NPE thrown by Lucene index opening) - trunk


Modified: trunk/lucene-directory/src/main/java/org/infinispan/lucene/InfinispanDirectory.java
===================================================================
--- trunk/lucene-directory/src/main/java/org/infinispan/lucene/InfinispanDirectory.java	2010-06-21 11:31:43 UTC (rev 1923)
+++ trunk/lucene-directory/src/main/java/org/infinispan/lucene/InfinispanDirectory.java	2010-06-21 14:43:28 UTC (rev 1924)
@@ -23,6 +23,7 @@
 
 import java.io.FileNotFoundException;
 import java.io.IOException;
+import java.util.Arrays;
 import java.util.Set;
 
 import org.apache.lucene.store.AlreadyClosedException;
@@ -94,7 +95,8 @@
    public String[] list() throws IOException {
       checkIsOpen();
       Set<String> filesList = getFileList();
-      return filesList.toArray(new String[filesList.size()]);
+      String[] array = filesList.toArray(new String[0]);
+      return array;
    }
 
    /**

Added: trunk/lucene-directory/src/test/java/org/infinispan/lucene/profiling/LuceneReaderThread.java
===================================================================
--- trunk/lucene-directory/src/test/java/org/infinispan/lucene/profiling/LuceneReaderThread.java	                        (rev 0)
+++ trunk/lucene-directory/src/test/java/org/infinispan/lucene/profiling/LuceneReaderThread.java	2010-06-21 14:43:28 UTC (rev 1924)
@@ -0,0 +1,86 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.infinispan.lucene.profiling;
+
+import java.io.IOException;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.apache.lucene.index.CorruptIndexException;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.Term;
+import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.search.Query;
+import org.apache.lucene.search.TermQuery;
+import org.apache.lucene.search.TopDocs;
+import org.apache.lucene.store.Directory;
+
+/**
+ * LuceneReaderThread is going to perform searches on the Directory until it's interrupted.
+ * Good for performance comparisons and stress tests.
+ * It needs a SharedState object to be shared with other readers and writers on the same directory to
+ * be able to throw exceptions in case it's able to detect an illegal state.
+ * 
+ * @author Sanne Grinovero
+ * @since 4.0
+ */
+public class LuceneReaderThread extends LuceneUserThread {
+
+   private IndexSearcher searcher;
+   private IndexReader indexReader;
+
+   LuceneReaderThread(Directory dir, SharedState state) {
+      super(dir, state);
+   }
+
+   @Override
+   protected void testLoop() throws IOException {
+      // take ownership of some strings, so that no other thread will change status for these:
+      Set<String> strings = new HashSet<String>();
+      int numElements = state.stringsInIndex.drainTo(strings, 50);
+      refreshIndexReader();
+      for (String term : strings) {
+         Query query = new TermQuery(new Term("main", term));
+         TopDocs docs = searcher.search(query, null, 1);
+         if (docs.totalHits != 1) {
+            throw new RuntimeException("String '" + term + "' should exist but was not found in index");
+         }
+      }
+      // put the strings back at their place:
+      state.stringsInIndex.addAll(strings);
+      state.incrementIndexSearchesCount(numElements);
+   }
+
+   private void refreshIndexReader() throws CorruptIndexException, IOException {
+      if (indexReader==null) {
+         indexReader = IndexReader.open(directory, true);
+      }
+      else {
+         indexReader = indexReader.reopen();
+      }
+      if (searcher!=null) {
+         searcher.close();
+      }
+      searcher = new IndexSearcher(directory, true);
+   }
+
+}

Modified: trunk/lucene-directory/src/test/java/org/infinispan/lucene/profiling/LuceneUserThread.java
===================================================================
--- trunk/lucene-directory/src/test/java/org/infinispan/lucene/profiling/LuceneUserThread.java	2010-06-21 11:31:43 UTC (rev 1923)
+++ trunk/lucene-directory/src/test/java/org/infinispan/lucene/profiling/LuceneUserThread.java	2010-06-21 14:43:28 UTC (rev 1924)
@@ -22,34 +22,20 @@
 package org.infinispan.lucene.profiling;
 
 import java.io.IOException;
-import java.util.HashSet;
-import java.util.Set;
 
-import org.apache.lucene.document.Document;
-import org.apache.lucene.document.Field;
-import org.apache.lucene.document.Field.Index;
-import org.apache.lucene.document.Field.Store;
-import org.apache.lucene.index.IndexWriter;
-import org.apache.lucene.index.Term;
-import org.apache.lucene.search.IndexSearcher;
-import org.apache.lucene.search.Query;
-import org.apache.lucene.search.TermQuery;
-import org.apache.lucene.search.TopDocs;
 import org.apache.lucene.store.Directory;
-import org.infinispan.lucene.testutils.LuceneSettings;
 
 /**
- * LuceneUserThread: does several activities on the index, including searching, adding to index and
- * deleting. It checks for some expected state in index, like known strings the index should contain
- * or should not contain at any time.
+ * LuceneUserThread: base class to perform activities on the index, as searching, adding to index and
+ * deleting.
  * 
  * @author Sanne Grinovero
  * @since 4.0
  */
-public class LuceneUserThread implements Runnable {
+public abstract class LuceneUserThread implements Runnable {
 
-   private final Directory directory;
-   private final SharedState state;
+   protected final Directory directory;
+   protected final SharedState state;
    
    LuceneUserThread(Directory dir, SharedState state) {
       this.directory = dir;
@@ -57,7 +43,7 @@
    }
 
    @Override
-   public void run() {
+   public final void run() {
       try {
          state.waitForStart();
       } catch (InterruptedException e1) {
@@ -73,41 +59,6 @@
       }
    }
 
-   private void testLoop() throws IOException {
-      addSomeStrings();
-      verifyStringsExistInIndex();
-   }
+   protected abstract void testLoop() throws IOException;
 
-   private void addSomeStrings() throws IOException {
-      Set<String> strings = new HashSet<String>();
-      state.stringsOutOfIndex.drainTo(strings, 5);
-      IndexWriter iwriter = LuceneSettings.openWriter(directory);
-      for (String term : strings) {
-         Document doc = new Document();
-         doc.add(new Field("main", term, Store.NO, Index.NOT_ANALYZED));
-         iwriter.addDocument(doc);
-      }
-      iwriter.commit();
-      iwriter.close();
-      state.stringsInIndex.addAll(strings);
-      state.incrementIndexWriterTaskCount(5);
-   }
-
-   private void verifyStringsExistInIndex() throws IOException {
-      // take ownership of some strings, so that no other thread will change status for them:
-      Set<String> strings = new HashSet<String>();
-      state.stringsInIndex.drainTo(strings, 50);
-      IndexSearcher searcher = new IndexSearcher(directory, true);
-      for (String term : strings) {
-         Query query = new TermQuery(new Term("main", term));
-         TopDocs docs = searcher.search(query, null, 1);
-         if (docs.totalHits != 1) {
-            throw new RuntimeException("String '" + term + "' should exist but was not found in index");
-         }
-      }
-      // put the strings back at their place:
-      state.stringsInIndex.addAll(strings);
-      state.incrementIndexSearchesCount(50);
-   }
-
 }

Added: trunk/lucene-directory/src/test/java/org/infinispan/lucene/profiling/LuceneWriterThread.java
===================================================================
--- trunk/lucene-directory/src/test/java/org/infinispan/lucene/profiling/LuceneWriterThread.java	                        (rev 0)
+++ trunk/lucene-directory/src/test/java/org/infinispan/lucene/profiling/LuceneWriterThread.java	2010-06-21 14:43:28 UTC (rev 1924)
@@ -0,0 +1,67 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.infinispan.lucene.profiling;
+
+import java.io.IOException;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.apache.lucene.document.Document;
+import org.apache.lucene.document.Field;
+import org.apache.lucene.document.Field.Index;
+import org.apache.lucene.document.Field.Store;
+import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.store.Directory;
+import org.infinispan.lucene.testutils.LuceneSettings;
+
+/**
+ * LuceneWriterThread is going to perform searches on the Directory until it's interrupted.
+ * Good for performance comparisons and stress tests.
+ * It needs a SharedState object to be shared with other readers and writers on the same directory to
+ * be able to throw exceptions in case it's able to detect an illegal state.
+ * 
+ * @author Sanne Grinovero
+ * @since 4.0
+ */
+public class LuceneWriterThread extends LuceneUserThread {
+   
+   LuceneWriterThread(Directory dir, SharedState state) {
+      super(dir, state);
+   }
+
+   @Override
+   protected void testLoop() throws IOException {
+      Set<String> strings = new HashSet<String>();
+      int numElements = state.stringsOutOfIndex.drainTo(strings, 5);
+      IndexWriter iwriter = LuceneSettings.openWriter(directory);
+      for (String term : strings) {
+         Document doc = new Document();
+         doc.add(new Field("main", term, Store.NO, Index.NOT_ANALYZED));
+         iwriter.addDocument(doc);
+      }
+      iwriter.commit();
+      iwriter.close();
+      state.stringsInIndex.addAll(strings);
+      state.incrementIndexWriterTaskCount(numElements);
+   }
+
+}

Modified: trunk/lucene-directory/src/test/java/org/infinispan/lucene/profiling/PerformanceCompareStressTest.java
===================================================================
--- trunk/lucene-directory/src/test/java/org/infinispan/lucene/profiling/PerformanceCompareStressTest.java	2010-06-21 11:31:43 UTC (rev 1923)
+++ trunk/lucene-directory/src/test/java/org/infinispan/lucene/profiling/PerformanceCompareStressTest.java	2010-06-21 14:43:28 UTC (rev 1924)
@@ -53,9 +53,9 @@
 public class PerformanceCompareStressTest {
    
    /** Concurrent Threads in tests */
-   private static final int THREADS = 1;
+   private static final int THREADS = 10;
    
-   private static final long DURATION_MS = 100000;
+   private static final long DURATION_MS = 10000;
    
    private static final ClusteredCacheFactory cacheFactory = new ClusteredCacheFactory(CacheTestSupport.createTestConfiguration());
 
@@ -88,16 +88,17 @@
    private void testDirectory(Directory dir, String testLabel) throws InterruptedException, IOException {
       SharedState state = new SharedState(1000);
       CacheTestSupport.initializeDirectory(dir);
-      ExecutorService e = Executors.newFixedThreadPool(THREADS);
+      ExecutorService e = Executors.newFixedThreadPool(THREADS+1);
       for (int i=0; i<THREADS; i++) {
-         e.execute(new LuceneUserThread(dir, state));
+         e.execute(new LuceneReaderThread(dir, state));
       }
+      e.execute(new LuceneWriterThread(dir, state));
       e.shutdown();
       state.startWaitingThreads();
       Thread.sleep(DURATION_MS);
-      state.quit();
       long searchesCount = state.incrementIndexSearchesCount(0);
       long writerTaskCount = state.incrementIndexWriterTaskCount(0);
+      state.quit();
       e.awaitTermination(10, TimeUnit.SECONDS);
       System.out.println(
                "Test " + testLabel +" run in " + DURATION_MS + "ms:\n\tSearches: " + searchesCount + "\n\t" + "Writes: " + writerTaskCount);



More information about the infinispan-commits mailing list