jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mreut...@apache.org
Subject svn commit: r1327914 - in /jackrabbit/trunk/jackrabbit-core/src: main/java/org/apache/jackrabbit/core/query/lucene/ test/java/org/apache/jackrabbit/core/query/lucene/
Date Thu, 19 Apr 2012 12:02:50 GMT
Author: mreutegg
Date: Thu Apr 19 12:02:50 2012
New Revision: 1327914

URL: http://svn.apache.org/viewvc?rev=1327914&view=rev
Log:
JCR-3299: Adding new index infos generation is not atomic

Added:
    jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/query/lucene/IndexInfosTest.java
  (with props)
Modified:
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/IndexHistory.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/IndexInfos.java
    jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/query/lucene/TestAll.java

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/IndexHistory.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/IndexHistory.java?rev=1327914&r1=1327913&r2=1327914&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/IndexHistory.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/IndexHistory.java
Thu Apr 19 12:02:50 2012
@@ -84,8 +84,12 @@ class IndexHistory {
                     } else {
                         continue;
                     }
-                    IndexInfos infos = new IndexInfos(dir, INDEXES, gen);
-                    indexInfosMap.put(gen, infos);
+                    try {
+                        IndexInfos infos = new IndexInfos(dir, INDEXES, gen);
+                        indexInfosMap.put(gen, infos);
+                    } catch (IOException e) {
+                        log.warn("ignoring invalid index infos file: " + name);
+                    }
                 }
             }
         }

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/IndexInfos.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/IndexInfos.java?rev=1327914&r1=1327913&r2=1327914&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/IndexInfos.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/IndexInfos.java
Thu Apr 19 12:02:50 2012
@@ -19,11 +19,13 @@ package org.apache.jackrabbit.core.query
 import java.io.BufferedInputStream;
 import java.io.DataInputStream;
 import java.io.DataOutputStream;
+import java.io.EOFException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.BufferedOutputStream;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.List;
@@ -98,13 +100,25 @@ class IndexInfos implements Cloneable {
     IndexInfos(Directory dir, String baseName) throws IOException {
         this.directory = dir;
         this.name = baseName;
-        long gen = getCurrentGeneration(getFileNames(dir, baseName), baseName);
-        if (gen == -1) {
+        long gens[] = getGenerations(getFileNames(dir, baseName), baseName);
+        if (gens.length == 0) {
             // write initial infos
             write();
         } else {
-            this.generation = gen;
-            read();
+            // read most recent generation
+            for (int i = gens.length - 1; i >= 0; i--) {
+                try {
+                    this.generation = gens[i];
+                    read();
+                    break;
+                } catch (EOFException e) {
+                    String fileName = getFileName(gens[i]);
+                    log.warn("deleting invalid index infos file: " + fileName);
+                    dir.deleteFile(fileName);
+                    // reset generation
+                    this.generation = 0;
+                }
+            }
         }
     }
 
@@ -378,22 +392,18 @@ class IndexInfos implements Cloneable {
     }
 
     /**
-     * Returns the most current generation of the given files.
+     * Returns the generations fo the given files in ascending order.
      *
-     * @param fileNames the file names from where to obtain the generation.
+     * @param fileNames the file names from where to obtain the generations.
      * @param base the base name.
-     * @return the most current generation.
+     * @return the generations in ascending order.
      */
-    private static long getCurrentGeneration(String[] fileNames, String base) {
-        long max = -1;
-        int i = 0;
-        while (i < fileNames.length) {
-            long gen = generationFromFileName(fileNames[i], base);
-            if (gen > max) {
-                max = gen;
-            }
-            i++;
+    private static long[] getGenerations(String[] fileNames, String base) {
+        long[] gens = new long[fileNames.length];
+        for (int i = 0; i < fileNames.length; i++) {
+            gens[i] = generationFromFileName(fileNames[i], base);
         }
-        return max;
+        Arrays.sort(gens);
+        return gens;
     }
 }

Added: jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/query/lucene/IndexInfosTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/query/lucene/IndexInfosTest.java?rev=1327914&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/query/lucene/IndexInfosTest.java
(added)
+++ jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/query/lucene/IndexInfosTest.java
Thu Apr 19 12:02:50 2012
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.core.query.lucene;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.store.FSDirectory;
+
+import junit.framework.TestCase;
+
+/**
+ * <code>IndexInfosTest</code> check if <code>IndexInfos</code> can
deal with
+ * invalid info files. See also JCR-3299.
+ */
+public class IndexInfosTest extends TestCase {
+
+    private static final File TEST_DIR = new File(new File("target"), "indexInfosTest");
+
+    private Directory dir;
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        FileUtils.deleteDirectory(TEST_DIR);
+        TEST_DIR.mkdirs();
+        dir = FSDirectory.open(TEST_DIR);
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        dir.close();
+        FileUtils.deleteDirectory(TEST_DIR);
+        super.tearDown();
+    }
+
+    public void testEmptyIndexesFile() throws IOException {
+        // creates initial generation of infos
+        IndexInfos infos = new IndexInfos(dir, "indexes");
+        long initialGeneration = infos.getGeneration();
+
+        // create second generation
+        infos.addName("index1", 23432);
+        infos.write();
+
+        // replace second generation file with an empty one
+        String fileName = infos.getFileName();
+        dir.deleteFile(fileName);
+        dir.createOutput(fileName).close();
+
+        new IndexHistory(dir, Integer.MAX_VALUE); // must succeed
+
+        // read index infos again
+        infos = new IndexInfos(dir, "indexes");
+        assertEquals("must read initial generation", initialGeneration, infos.getGeneration());
+
+        // create new generation
+        infos.addName("index1", 39854);
+        infos.write(); // must succeed
+    }
+}

Propchange: jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/query/lucene/IndexInfosTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/query/lucene/IndexInfosTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Modified: jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/query/lucene/TestAll.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/query/lucene/TestAll.java?rev=1327914&r1=1327913&r2=1327914&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/query/lucene/TestAll.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/query/lucene/TestAll.java
Thu Apr 19 12:02:50 2012
@@ -45,6 +45,7 @@ public class TestAll extends TestCase {
         suite.addTestSuite(SQL2IndexingAggregateTest.class);
         suite.addTestSuite(SQL2IndexingAggregateTest2.class);
         suite.addTestSuite(LazyTextExtractorFieldTest.class);
+        suite.addTestSuite(IndexInfosTest.class);
 
         return suite;
     }



Mime
View raw message