Return-Path: X-Original-To: apmail-jackrabbit-commits-archive@www.apache.org Delivered-To: apmail-jackrabbit-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id B012C90B4 for ; Thu, 19 Apr 2012 12:03:20 +0000 (UTC) Received: (qmail 67231 invoked by uid 500); 19 Apr 2012 12:03:20 -0000 Delivered-To: apmail-jackrabbit-commits-archive@jackrabbit.apache.org Received: (qmail 67163 invoked by uid 500); 19 Apr 2012 12:03:19 -0000 Mailing-List: contact commits-help@jackrabbit.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@jackrabbit.apache.org Delivered-To: mailing list commits@jackrabbit.apache.org Received: (qmail 67154 invoked by uid 99); 19 Apr 2012 12:03:19 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 19 Apr 2012 12:03:19 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 19 Apr 2012 12:03:12 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id CC8872388847; Thu, 19 Apr 2012 12:02:50 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: commits@jackrabbit.apache.org From: mreutegg@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20120419120250.CC8872388847@eris.apache.org> 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; + +/** + * IndexInfosTest check if IndexInfos 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; }