[hg] main-silver: #211808:org.apache.lucene.store.LockObtainFail...

  • From: Tomas Zezula < >
  • To:
  • Subject: [hg] main-silver: #211808:org.apache.lucene.store.LockObtainFail...
  • Date: Mon, 03 Dec 2012 13:52:56 -0800

changeset 24f6928dca82 in main-silver ((none))
details: http://hg.netbeans.org/main-silver/rev/24f6928dca82
description:
        #211808:org.apache.lucene.store.LockObtainFailedException: Lock 
obtain timed out: 
org.netbeans.modules.parsing.lucene.RecordOwnerLockFactory$RecordOwnerLock@357fa07b

diffstat:

 parsing.lucene/src/org/netbeans/modules/parsing/lucene/LuceneIndex.java      
                 |   14 +-
 
parsing.lucene/src/org/netbeans/modules/parsing/lucene/RecordOwnerLockFactory.java
            |   67 +++-
 
parsing.lucene/test/unit/src/org/netbeans/modules/parsing/lucene/NativeFSLockFactoryTest.java
 |  200 ++++++++++
 3 files changed, 275 insertions(+), 6 deletions(-)

diffs (358 lines):

diff --git 
a/parsing.lucene/src/org/netbeans/modules/parsing/lucene/LuceneIndex.java 
b/parsing.lucene/src/org/netbeans/modules/parsing/lucene/LuceneIndex.java
--- a/parsing.lucene/src/org/netbeans/modules/parsing/lucene/LuceneIndex.java
+++ b/parsing.lucene/src/org/netbeans/modules/parsing/lucene/LuceneIndex.java
@@ -699,14 +699,22 @@
                     txWriter.remove();
                     owner.clear();
                     try {
-                        if (!success && IndexWriter.isLocked(fsDir)) {
+                        if (!success) {
+                            if ((lockFactory instanceof 
RecordOwnerLockFactory) &&
+                                
((RecordOwnerLockFactory)lockFactory).getOwner() == Thread.currentThread()) {
+                                
((RecordOwnerLockFactory)lockFactory).forceRemoveLock();
+                            } else if (IndexWriter.isLocked(fsDir)) {
                             IndexWriter.unlock(fsDir);
                         }
+                        }
                     } catch (IOException ioe) {
                         LOGGER.log(
                            Level.WARNING,
-                           "Cannot unlock index {0} while recovering.",  
//NOI18N
-                           folder.getAbsolutePath());
+                           "Cannot unlock index {0} while recovering, {1}.", 
 //NOI18N
+                           new Object[] {
+                               folder.getAbsolutePath(),
+                            ioe.getMessage()
+                           });
                     } finally {
                         refreshReader();
                     }
diff --git 
a/parsing.lucene/src/org/netbeans/modules/parsing/lucene/RecordOwnerLockFactory.java
 
b/parsing.lucene/src/org/netbeans/modules/parsing/lucene/RecordOwnerLockFactory.java
--- 
a/parsing.lucene/src/org/netbeans/modules/parsing/lucene/RecordOwnerLockFactory.java
+++ 
b/parsing.lucene/src/org/netbeans/modules/parsing/lucene/RecordOwnerLockFactory.java
@@ -41,11 +41,18 @@
  */
 package org.netbeans.modules.parsing.lucene;
 
+import java.io.File;
 import java.io.IOException;
+import java.lang.reflect.Field;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.IdentityHashMap;
+import java.util.Set;
 import org.apache.lucene.store.Lock;
 import org.apache.lucene.store.NativeFSLockFactory;
 import org.netbeans.api.annotations.common.CheckForNull;
 import org.netbeans.api.annotations.common.NonNull;
+import org.openide.util.Parameters;
 
 /**
  *
@@ -53,7 +60,9 @@
  */
 class RecordOwnerLockFactory extends NativeFSLockFactory {
     
-    private volatile Thread owner;
+    private final Set</*@GuardedBy("this")*/RecordOwnerLock> locked = 
Collections.newSetFromMap(new IdentityHashMap<RecordOwnerLock, Boolean>());
+    //@GuardedBy("this")
+    private Thread owner;
     
     RecordOwnerLockFactory() throws IOException {
         super();
@@ -64,6 +73,22 @@
         return owner;
     }
     
+    /**
+     * Force freeing of lock file.
+     * Lucene IndexWriter.closeInternal does not free lock file when 
exception
+     * happens in it. This method tries to do the best to do free it.
+     * @throws IOException if lock(s) cannot be freed.
+     */
+    synchronized void forceRemoveLock() throws IOException {
+        try {
+            for (RecordOwnerLock l : locked) {
+                l.forceRemoveLock();
+            }
+        } finally {
+            locked.clear();
+        }
+    }
+    
     
     @Override
     public Lock makeLock(String lockName) {
@@ -77,6 +102,22 @@
     }
     
     
+    private synchronized void recordOwner(
+        @NonNull final Thread t,
+        @NonNull final RecordOwnerLock l) {
+        Parameters.notNull("t", t); //NOI18N
+        Parameters.notNull("l", l); //NOI18N
+        owner = t;
+        locked.add(l);
+    }
+
+    private synchronized void clearOwner(
+        @NonNull final RecordOwnerLock l) {
+        Parameters.notNull("l", l); //NOI18N
+        locked.remove(l);
+        owner = null;
+    }
+    
     private class RecordOwnerLock extends Lock {
         
         private final Lock delegate;
@@ -90,7 +131,7 @@
         public boolean obtain() throws IOException {
             final boolean result = delegate.obtain();
             if (result) {
-                owner = Thread.currentThread();
+                recordOwner(Thread.currentThread(), this);
             }
             return result;
         }
@@ -98,7 +139,7 @@
         @Override
         public void release() throws IOException {
             delegate.release();
-            owner = null;
+            clearOwner(this);
         }
 
         @Override
@@ -106,6 +147,26 @@
             return delegate.isLocked();
         }
     
+        private void forceRemoveLock() throws IOException {
+            try {
+                final Class<? extends Lock> delegateClass = 
delegate.getClass();
+                final Field lockPathField = 
delegateClass.getDeclaredField("path"); //NOI18N
+                lockPathField.setAccessible(true);
+                final File path = (File) lockPathField.get(delegate);
+                final Field lockHeldField = 
delegateClass.getDeclaredField("LOCK_HELD"); //NOI18N
+                lockHeldField.setAccessible(true);
+                Collection lockHeld = (Collection) lockHeldField.get(null);
+                synchronized (lockHeld) {
+                    lockHeld.remove(path.getCanonicalPath());
+                }
+                path.delete();
+            } catch (NoSuchFieldException nfe) {
+                throw new IOException(nfe);
+            } catch (IllegalAccessException iae) {
+                throw new IOException(iae);
+            }
     }
     
 }
+    
+}
diff --git 
a/parsing.lucene/test/unit/src/org/netbeans/modules/parsing/lucene/NativeFSLockFactoryTest.java
 
b/parsing.lucene/test/unit/src/org/netbeans/modules/parsing/lucene/NativeFSLockFactoryTest.java
new file mode 100644
--- /dev/null
+++ 
b/parsing.lucene/test/unit/src/org/netbeans/modules/parsing/lucene/NativeFSLockFactoryTest.java
@@ -0,0 +1,200 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2012 Oracle and/or its affiliates. All rights reserved.
+ *
+ * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
+ * Other names may be trademarks of their respective owners.
+ *
+ * The contents of this file are subject to the terms of either the GNU
+ * General Public License Version 2 only ("GPL") or the Common
+ * Development and Distribution License("CDDL") (collectively, the
+ * "License"). You may not use this file except in compliance with the
+ * License. You can obtain a copy of the License at
+ * http://www.netbeans.org/cddl-gplv2.html
+ * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
+ * specific language governing permissions and limitations under the
+ * License.  When distributing the software, include this License Header
+ * Notice in each file and include the License file at
+ * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the GPL Version 2 section of the License file that
+ * accompanied this code. If applicable, add the following below the
+ * License Header, with the fields enclosed by brackets [] replaced by
+ * your own identifying information:
+ * "Portions Copyrighted [year] [name of copyright owner]"
+ *
+ * If you wish your version of this file to be governed by only the CDDL
+ * or only the GPL Version 2, indicate your decision by adding
+ * "[Contributor] elects to include this software in this distribution
+ * under the [CDDL or GPL Version 2] license." If you do not indicate a
+ * single choice of license, a recipient has the option to distribute
+ * your version of this file under either the CDDL, the GPL Version 2 or
+ * to extend the choice of license to its licensees as provided above.
+ * However, if you add GPL Version 2 code and therefore, elected the GPL
+ * Version 2 license, then the option applies only if the new code is
+ * made subject to such option by the copyright holder.
+ *
+ * Contributor(s):
+ *
+ * Portions Copyrighted 2012 Sun Microsystems, Inc.
+ */
+package org.netbeans.modules.parsing.lucene;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.logging.Handler;
+import java.util.logging.Level;
+import java.util.logging.LogRecord;
+import java.util.logging.Logger;
+import org.apache.lucene.analysis.KeywordAnalyzer;
+import org.apache.lucene.document.Document;
+import org.apache.lucene.document.Field;
+import org.apache.lucene.search.Query;
+import org.netbeans.api.annotations.common.NonNull;
+import org.netbeans.junit.NbTestCase;
+import org.netbeans.modules.parsing.lucene.support.Convertor;
+import org.openide.util.Parameters;
+
+/**
+ *
+ * @author Tomas Zezula
+ */
+public class NativeFSLockFactoryTest extends NbTestCase {
+    
+    private File indexFolder;
+
+    public NativeFSLockFactoryTest(@NonNull final String name) {
+        super(name);
+    }
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        clearWorkDir();
+        indexFolder = new File(getWorkDir(),"index");   //NOI18N
+        assertTrue(indexFolder.mkdirs());
+    }
+
+
+    public void testLockFreedDuringStoreError() throws Exception {
+        final LuceneIndex index = LuceneIndex.create(indexFolder, new 
KeywordAnalyzer());
+        final Collection<? extends Integer> dataSet = generateDataSet(1000);
+        final Logger log = Logger.getLogger(LuceneIndex.class.getName());
+        final TestHandler handler = new TestHandler(
+            new Runnable() {
+                @Override
+                public void run() {
+                    //Break index a bit ;-)
+                    for (File f : indexFolder.listFiles()) {
+                        if (f.getName().startsWith("nb-lock")) {    //NOI18N
+                            continue;
+                        }
+                        f.delete();
+                    }                    
+                }
+        });
+        log.setLevel(Level.FINE);
+        log.addHandler(handler);
+        boolean success = false;
+        try {
+            index.store(
+                    dataSet,
+                    Collections.<String>emptySet(),
+                    new Convertor<Integer, Document>() {
+                        @Override
+                        public Document convert(Integer p) {
+                            final Document doc = new Document();
+                            doc.add(new Field(
+                                    "val",                  //NOI18N
+                                    Integer.toString(p),
+                                    Field.Store.YES,
+                                    Field.Index.ANALYZED_NO_NORMS));
+                            return doc;
+                        }
+                    },
+                    new Convertor<String, Query>() {
+                        @Override
+                        public Query convert(String p) {
+                            throw new UnsupportedOperationException();
+                        }
+                    },
+                    true);
+            success = true;
+        } catch (Throwable t) {
+            //Ignore - should be thrown and success should be false
+        } finally {
+            log.removeHandler(handler);
+        }
+        assertFalse(success);
+        success = false;
+        try {
+            index.store(
+                dataSet,
+                Collections.<String>emptySet(),
+                new Convertor<Integer, Document>() {
+                    @Override
+                    public Document convert(Integer p) {
+                        final Document doc = new Document();
+                        doc.add(new Field(
+                                "val",                  //NOI18N
+                                Integer.toString(p),
+                                Field.Store.YES,
+                                Field.Index.ANALYZED_NO_NORMS));
+                        return doc;
+                    }
+                },
+                new Convertor<String, Query>() {
+                    @Override
+                    public Query convert(String p) {
+                        throw new UnsupportedOperationException();
+                    }
+                },
+                true);
+            success = true;
+        } catch (Throwable t) {
+            //Should not be thrown and success should be true
+            t.printStackTrace();
+        }
+        assertTrue(success);
+    }
+
+
+
+    private static Collection<? extends Integer> generateDataSet(final int 
count) {
+        final List<Integer> res = new ArrayList<Integer>(count);
+        for (int i=0; i< count; i++) {
+            res.add(i);
+        }
+        return res;
+    }
+
+    private static class TestHandler extends Handler {
+
+        private final Runnable action;
+
+        TestHandler(@NonNull final Runnable action) {
+            Parameters.notNull("action", action);   //NOI18N
+            this.action = action;
+        }
+
+        @Override
+        public void publish(LogRecord record) {
+            if ("Committing {0}".equals(record.getMessage())) { //NOI18N
+                action.run();
+            }
+        }
+
+        @Override
+        public void flush() {
+        }
+
+        @Override
+        public void close() throws SecurityException {
+        }
+
+    }
+}

[hg] main-silver: #211808:org.apache.lucene.store.LockObtainFail...

Tomas Zezula 12/03/2012

Project Features

About this Project

Editor was started in November 2009, is owned by Martin Ryzl, and has 147 members.
By use of this website, you agree to the NetBeans Policies and Terms of Use (revision 20140418.2d69abc). © 2013, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo
 
 
Close
loading
Please Confirm
Close