LUCENE-1050: detect errors on releasing the lock (if delete of the lock file fails)s

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@595448 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2007-11-15 20:42:54 +00:00
parent 5992c5a1cf
commit c5c1913410
8 changed files with 66 additions and 28 deletions

View File

@ -144,6 +144,10 @@ Bug fixes
timeout argument is very large (eg Long.MAX_VALUE). Also added timeout argument is very large (eg Long.MAX_VALUE). Also added
Lock.LOCK_OBTAIN_WAIT_FOREVER constant to never timeout. (Nikolay Lock.LOCK_OBTAIN_WAIT_FOREVER constant to never timeout. (Nikolay
Diakov via Mike McCandless) Diakov via Mike McCandless)
23. LUCENE-1050: Throw LockReleaseFailedException in
Simple/NativeFSLockFactory if we fail to delete the lock file when
releasing the lock. (Nikolay Diakov via Mike McCandless)
New features New features

View File

@ -1220,11 +1220,6 @@ public class IndexWriter {
if (infoStream != null) if (infoStream != null)
message("at close: " + segString()); message("at close: " + segString());
if (writeLock != null) {
writeLock.release(); // release write lock
writeLock = null;
}
closed = true;
docWriter = null; docWriter = null;
synchronized(this) { synchronized(this) {
@ -1233,6 +1228,13 @@ public class IndexWriter {
if (closeDir) if (closeDir)
directory.close(); directory.close();
if (writeLock != null) {
writeLock.release(); // release write lock
writeLock = null;
}
closed = true;
} finally { } finally {
synchronized(this) { synchronized(this) {
if (!closed) if (!closed)

View File

@ -99,7 +99,7 @@ public abstract class Lock {
} }
/** Releases exclusive access. */ /** Releases exclusive access. */
public abstract void release(); public abstract void release() throws IOException;
/** Returns true if the resource is currently locked. Note that one must /** Returns true if the resource is currently locked. Note that one must
* still call {@link #obtain()} before using the resource. */ * still call {@link #obtain()} before using the resource. */

View File

@ -0,0 +1,31 @@
/**
* 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.lucene.store;
import java.io.IOException;
/**
* This exception is thrown when the <code>write.lock</code>
* could not be released.
* @see Lock#release().
*/
public class LockReleaseFailedException extends IOException {
public LockReleaseFailedException(String message) {
super(message);
}
}

View File

@ -287,33 +287,28 @@ class NativeFSLock extends Lock {
return isLocked(); return isLocked();
} }
public synchronized void release() { public synchronized void release() throws IOException {
try { if (isLocked()) {
if (isLocked()) { try {
lock.release();
} finally {
lock = null;
try { try {
lock.release(); channel.close();
} finally { } finally {
lock = null; channel = null;
try { try {
channel.close(); f.close();
} finally { } finally {
channel = null; f = null;
try { synchronized(LOCK_HELD) {
f.close(); LOCK_HELD.remove(path.getCanonicalPath());
} finally {
f = null;
synchronized(LOCK_HELD) {
LOCK_HELD.remove(path.getCanonicalPath());
}
} }
} }
} }
path.delete();
} }
} catch (IOException e) { if (!path.delete())
// Not sure how to better message/handle this without throw new LockReleaseFailedException("failed to delete " + path);
// changing API?
throw new RuntimeException(e);
} }
} }

View File

@ -144,8 +144,9 @@ class SimpleFSLock extends Lock {
return lockFile.createNewFile(); return lockFile.createNewFile();
} }
public void release() { public void release() throws LockReleaseFailedException {
lockFile.delete(); if (!lockFile.delete())
throw new LockReleaseFailedException("failed to delete " + lockFile);
} }
public boolean isLocked() { public boolean isLocked() {

View File

@ -85,7 +85,7 @@ public class VerifyingLockFactory extends LockFactory {
return lock.isLocked(); return lock.isLocked();
} }
public synchronized void release() { public synchronized void release() throws IOException {
if (isLocked()) { if (isLocked()) {
verify((byte) 0); verify((byte) 0);
lock.release(); lock.release();

View File

@ -188,7 +188,12 @@ public class TestLockFactory extends LuceneTestCase {
writer.close(); writer.close();
if (writer2 != null) { if (writer2 != null) {
try {
writer2.close(); writer2.close();
fail("writer2.close() should have hit LockReleaseFailedException");
} catch (LockReleaseFailedException e) {
// expected
}
} }
// Cleanup // Cleanup