https://issues.apache.org/jira/browse/AMQ-5568 - fix and test - only delete lock file if lock was held on stop

This commit is contained in:
gtully 2015-03-05 13:22:58 +00:00
parent 113a3815b0
commit 8c66fba0aa
2 changed files with 72 additions and 2 deletions

View File

@ -37,6 +37,7 @@ public class LockFile {
private RandomAccessFile readFile; private RandomAccessFile readFile;
private int lockCounter; private int lockCounter;
private final boolean deleteOnUnlock; private final boolean deleteOnUnlock;
private volatile boolean locked;
public LockFile(File file, boolean deleteOnUnlock) { public LockFile(File file, boolean deleteOnUnlock) {
this.file = file; this.file = file;
@ -76,6 +77,7 @@ public class LockFile {
if (lock != null) { if (lock != null) {
lockCounter++; lockCounter++;
System.setProperty(getVmLockKey(), new Date().toString()); System.setProperty(getVmLockKey(), new Date().toString());
locked = true;
} else { } else {
// new read file for next attempt // new read file for next attempt
closeReadFile(); closeReadFile();
@ -118,7 +120,7 @@ public class LockFile {
} }
closeReadFile(); closeReadFile();
if (deleteOnUnlock) { if (locked && deleteOnUnlock) {
file.delete(); file.delete();
} }
} }
@ -140,7 +142,8 @@ public class LockFile {
} }
public boolean keepAlive() { public boolean keepAlive() {
return lock != null && lock.isValid() && file.exists(); locked = locked && lock != null && lock.isValid() && file.exists();
return locked;
} }
} }

View File

@ -0,0 +1,67 @@
/**
* 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.activemq.util;
import java.io.File;
import org.junit.Test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class LockFileTest {
@Test
public void testNoDeleteOnUnlockIfNotLocked() throws Exception {
File lockFile = new File(IOHelper.getDefaultDataDirectory(), "lockToTest1");
lockFile.createNewFile();
LockFile underTest = new LockFile(lockFile, true);
underTest.lock();
lockFile.delete();
assertFalse("no longer valid", underTest.keepAlive());
// a slave gets in
lockFile.createNewFile();
underTest.unlock();
assertTrue("file still exists after unlock when not locked", lockFile.exists());
}
@Test
public void testDeleteOnUnlockIfLocked() throws Exception {
File lockFile = new File(IOHelper.getDefaultDataDirectory(), "lockToTest2");
lockFile.createNewFile();
LockFile underTest = new LockFile(lockFile, true);
underTest.lock();
assertTrue("valid", underTest.keepAlive());
underTest.unlock();
assertFalse("file deleted on unlock", lockFile.exists());
}
}