环境:MAC OS X 10.11.5 + ActiveMQ 5.13.3
核心代码是 SharedFileLocker.java 和 LockFile.java,核心方法是 doStart 和 keepAlive。
其原理就是 java 文件锁,只要 ActiveMQ 进程在就锁住该文件,没有类似心跳或每隔一段时间更新 lock 时间的机制。
1. SharedFileLocker.java
package org.apache.activemq.store;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.apache.activemq.broker.AbstractLocker;
import org.apache.activemq.util.LockFile;
import org.apache.activemq.util.ServiceStopper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Represents an exclusive lock on a database to avoid multiple brokers running
* against the same logical database.
*
* @org.apache.xbean.XBean element="shared-file-locker"
*
*/
public class SharedFileLocker extends AbstractLocker {
public static final File DEFAULT_DIRECTORY = new File("KahaDB");
private static final Logger LOG = LoggerFactory.getLogger(SharedFileLocker.class);
private LockFile lockFile;
protected File directory = DEFAULT_DIRECTORY;
@Override
public void doStart() throws Exception {
if (lockFile == null) {
File lockFileName = new File(directory, "lock");
lockFile = new LockFile(lockFileName, false);
if (failIfLocked) {
lockFile.lock();
} else {
// Print a warning only once
boolean warned = false;
boolean locked = false;
while ((!isStopped()) && (!isStopping())) {
try {
lockFile.lock();
if (warned) {
// ensure lockHolder has released; wait for one keepAlive iteration
try {
TimeUnit.MILLISECONDS.sleep(lockable != null ? lockable.getLockKeepAlivePeriod() : 0l);
} catch (InterruptedException e1) {
}
}
locked = keepAlive();
break;
} catch (IOException e) {
if (!warned) {
LOG.info("Database "
+ lockFileName
+ " is locked by another server. This broker is now in slave mode waiting a lock to be acquired");
warned = true;
}
LOG.debug("Database "
+ lockFileName
+ " is locked... waiting "
+ (lockAcquireSleepInterval / 1000)
+ " seconds for the database to be unlocked. Reason: "
+ e);
try {
TimeUnit.MILLISECONDS.sleep(lockAcquireSleepInterval);
} catch (InterruptedException e1) {
}
}
}
if (!locked) {
throw new IOException("attempt to obtain lock aborted due to shutdown");
}
}
}
}
@Override
public boolean keepAlive() {
boolean result = lockFile != null && lockFile.keepAlive();
LOG.trace("keepAlive result: " + result + (name != null ? ", name: " + name : ""));
return result;
}
@Override
public void doStop(ServiceStopper stopper) throws Exception {
if (lockFile != null) {
lockFile.unlock();
lockFile = null;
}
}
public File getDirectory() {
return directory;
}
public void setDirectory(File directory) {
this.directory = directory;
}
@Override
public void configure(PersistenceAdapter persistenceAdapter) throws IOException {
this.setDirectory(persistenceAdapter.getDirectory());
if (name == null) {
name = getDirectory().toString();
}
}
}
2. LockFile.java
package org.apache.activemq.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileLock;
import java.nio.channels.OverlappingFileLockException;
import java.util.Date;
/**
* Used to lock a File.
*
* @author chirino
*/
public class LockFile {
private static final boolean DISABLE_FILE_LOCK = Boolean.getBoolean("java.nio.channels.FileLock.broken");
final private File file;
private long lastModified;
private FileLock lock;
private RandomAccessFile randomAccessLockFile;
private int lockCounter;
private final boolean deleteOnUnlock;
private volatile boolean locked;
private String lockSystemPropertyName = "";
private static final Logger LOG = LoggerFactory.getLogger(LockFile.class);
public LockFile(File file, boolean deleteOnUnlock) {
this.file = file;
this.deleteOnUnlock = deleteOnUnlock;
}
/**
* @throws IOException
*/
synchronized public void lock() throws IOException {
if (DISABLE_FILE_LOCK) {
return;
}
if (lockCounter > 0) {
return;
}
IOHelper.mkdirs(file.getParentFile());
synchronized (LockFile.class) {
lockSystemPropertyName = getVmLockKey();
if (System.getProperty(lockSystemPropertyName) != null) {
throw new IOException("File '" + file + "' could not be locked as lock is already held for this jvm. Value: " + System.getProperty(lockSystemPropertyName));
}
System.setProperty(lockSystemPropertyName, new Date().toString());
}
try {
if (lock == null) {
randomAccessLockFile = new RandomAccessFile(file, "rw");
IOException reason = null;
try {
lock = randomAccessLockFile.getChannel().tryLock(0, Math.max(1, randomAccessLockFile.getChannel().size()), false);
} catch (OverlappingFileLockException e) {
reason = IOExceptionSupport.create("File '" + file + "' could not be locked.", e);
} catch (IOException ioe) {
reason = ioe;
}
if (lock != null) {
//track lastModified only if we are able to successfully obtain the lock.
randomAccessLockFile.writeLong(System.currentTimeMillis());
randomAccessLockFile.getChannel().force(true);
lastModified = file.lastModified();
lockCounter++;
System.setProperty(lockSystemPropertyName, new Date().toString());
locked = true;
} else {
// new read file for next attempt
closeReadFile();
if (reason != null) {
throw reason;
}
throw new IOException("File '" + file + "' could not be locked.");
}
}
} finally {
synchronized (LockFile.class) {
if (lock == null) {
System.getProperties().remove(lockSystemPropertyName);
}
}
}
}
/**
*/
synchronized public void unlock() {
if (DISABLE_FILE_LOCK) {
return;
}
lockCounter--;
if (lockCounter != 0) {
return;
}
// release the lock..
if (lock != null) {
try {
lock.release();
} catch (Throwable ignore) {
} finally {
if (lockSystemPropertyName != null) {
System.getProperties().remove(lockSystemPropertyName);
}
lock = null;
}
}
closeReadFile();
if (locked && deleteOnUnlock) {
file.delete();
}
}
private String getVmLockKey() throws IOException {
return getClass().getName() + ".lock." + file.getCanonicalPath();
}
private void closeReadFile() {
// close the file.
if (randomAccessLockFile != null) {
try {
randomAccessLockFile.close();
} catch (Throwable ignore) {
}
randomAccessLockFile = null;
}
}
/**
* @return true if the lock file's last modified does not match the locally
* cached lastModified, false otherwise
*/
private boolean hasBeenModified() {
boolean modified = false;
//Create a new instance of the File object so we can get the most up to date information on the file.
File localFile = new File(file.getAbsolutePath());
if (localFile.exists()) {
if (localFile.lastModified() != lastModified) {
LOG.info("Lock file " + file.getAbsolutePath() + ", locked at " + new Date(lastModified) + ", has been modified at " + new Date(localFile.lastModified()));
modified = true;
}
} else {
//The lock file is missing
LOG.info("Lock file " + file.getAbsolutePath() + ", does not exist");
modified = true;
}
return modified;
}
public boolean keepAlive() {
locked = locked && lock != null && lock.isValid() && !hasBeenModified();
return locked;
}
}
参考文献:
1. https://github.com/apache/activemq/blob/master/activemq-broker/src/main/java/org/apache/activemq/store/SharedFileLocker.java
2. https://github.com/apache/activemq/blob/master/activemq-broker/src/main/java/org/apache/activemq/util/LockFile.java
核心代码是 SharedFileLocker.java 和 LockFile.java,核心方法是 doStart 和 keepAlive。
其原理就是 java 文件锁,只要 ActiveMQ 进程在就锁住该文件,没有类似心跳或每隔一段时间更新 lock 时间的机制。
1. SharedFileLocker.java
package org.apache.activemq.store;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.apache.activemq.broker.AbstractLocker;
import org.apache.activemq.util.LockFile;
import org.apache.activemq.util.ServiceStopper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Represents an exclusive lock on a database to avoid multiple brokers running
* against the same logical database.
*
* @org.apache.xbean.XBean element="shared-file-locker"
*
*/
public class SharedFileLocker extends AbstractLocker {
public static final File DEFAULT_DIRECTORY = new File("KahaDB");
private static final Logger LOG = LoggerFactory.getLogger(SharedFileLocker.class);
private LockFile lockFile;
protected File directory = DEFAULT_DIRECTORY;
@Override
public void doStart() throws Exception {
if (lockFile == null) {
File lockFileName = new File(directory, "lock");
lockFile = new LockFile(lockFileName, false);
if (failIfLocked) {
lockFile.lock();
} else {
// Print a warning only once
boolean warned = false;
boolean locked = false;
while ((!isStopped()) && (!isStopping())) {
try {
lockFile.lock();
if (warned) {
// ensure lockHolder has released; wait for one keepAlive iteration
try {
TimeUnit.MILLISECONDS.sleep(lockable != null ? lockable.getLockKeepAlivePeriod() : 0l);
} catch (InterruptedException e1) {
}
}
locked = keepAlive();
break;
} catch (IOException e) {
if (!warned) {
LOG.info("Database "
+ lockFileName
+ " is locked by another server. This broker is now in slave mode waiting a lock to be acquired");
warned = true;
}
LOG.debug("Database "
+ lockFileName
+ " is locked... waiting "
+ (lockAcquireSleepInterval / 1000)
+ " seconds for the database to be unlocked. Reason: "
+ e);
try {
TimeUnit.MILLISECONDS.sleep(lockAcquireSleepInterval);
} catch (InterruptedException e1) {
}
}
}
if (!locked) {
throw new IOException("attempt to obtain lock aborted due to shutdown");
}
}
}
}
@Override
public boolean keepAlive() {
boolean result = lockFile != null && lockFile.keepAlive();
LOG.trace("keepAlive result: " + result + (name != null ? ", name: " + name : ""));
return result;
}
@Override
public void doStop(ServiceStopper stopper) throws Exception {
if (lockFile != null) {
lockFile.unlock();
lockFile = null;
}
}
public File getDirectory() {
return directory;
}
public void setDirectory(File directory) {
this.directory = directory;
}
@Override
public void configure(PersistenceAdapter persistenceAdapter) throws IOException {
this.setDirectory(persistenceAdapter.getDirectory());
if (name == null) {
name = getDirectory().toString();
}
}
}
2. LockFile.java
package org.apache.activemq.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileLock;
import java.nio.channels.OverlappingFileLockException;
import java.util.Date;
/**
* Used to lock a File.
*
* @author chirino
*/
public class LockFile {
private static final boolean DISABLE_FILE_LOCK = Boolean.getBoolean("java.nio.channels.FileLock.broken");
final private File file;
private long lastModified;
private FileLock lock;
private RandomAccessFile randomAccessLockFile;
private int lockCounter;
private final boolean deleteOnUnlock;
private volatile boolean locked;
private String lockSystemPropertyName = "";
private static final Logger LOG = LoggerFactory.getLogger(LockFile.class);
public LockFile(File file, boolean deleteOnUnlock) {
this.file = file;
this.deleteOnUnlock = deleteOnUnlock;
}
/**
* @throws IOException
*/
synchronized public void lock() throws IOException {
if (DISABLE_FILE_LOCK) {
return;
}
if (lockCounter > 0) {
return;
}
IOHelper.mkdirs(file.getParentFile());
synchronized (LockFile.class) {
lockSystemPropertyName = getVmLockKey();
if (System.getProperty(lockSystemPropertyName) != null) {
throw new IOException("File '" + file + "' could not be locked as lock is already held for this jvm. Value: " + System.getProperty(lockSystemPropertyName));
}
System.setProperty(lockSystemPropertyName, new Date().toString());
}
try {
if (lock == null) {
randomAccessLockFile = new RandomAccessFile(file, "rw");
IOException reason = null;
try {
lock = randomAccessLockFile.getChannel().tryLock(0, Math.max(1, randomAccessLockFile.getChannel().size()), false);
} catch (OverlappingFileLockException e) {
reason = IOExceptionSupport.create("File '" + file + "' could not be locked.", e);
} catch (IOException ioe) {
reason = ioe;
}
if (lock != null) {
//track lastModified only if we are able to successfully obtain the lock.
randomAccessLockFile.writeLong(System.currentTimeMillis());
randomAccessLockFile.getChannel().force(true);
lastModified = file.lastModified();
lockCounter++;
System.setProperty(lockSystemPropertyName, new Date().toString());
locked = true;
} else {
// new read file for next attempt
closeReadFile();
if (reason != null) {
throw reason;
}
throw new IOException("File '" + file + "' could not be locked.");
}
}
} finally {
synchronized (LockFile.class) {
if (lock == null) {
System.getProperties().remove(lockSystemPropertyName);
}
}
}
}
/**
*/
synchronized public void unlock() {
if (DISABLE_FILE_LOCK) {
return;
}
lockCounter--;
if (lockCounter != 0) {
return;
}
// release the lock..
if (lock != null) {
try {
lock.release();
} catch (Throwable ignore) {
} finally {
if (lockSystemPropertyName != null) {
System.getProperties().remove(lockSystemPropertyName);
}
lock = null;
}
}
closeReadFile();
if (locked && deleteOnUnlock) {
file.delete();
}
}
private String getVmLockKey() throws IOException {
return getClass().getName() + ".lock." + file.getCanonicalPath();
}
private void closeReadFile() {
// close the file.
if (randomAccessLockFile != null) {
try {
randomAccessLockFile.close();
} catch (Throwable ignore) {
}
randomAccessLockFile = null;
}
}
/**
* @return true if the lock file's last modified does not match the locally
* cached lastModified, false otherwise
*/
private boolean hasBeenModified() {
boolean modified = false;
//Create a new instance of the File object so we can get the most up to date information on the file.
File localFile = new File(file.getAbsolutePath());
if (localFile.exists()) {
if (localFile.lastModified() != lastModified) {
LOG.info("Lock file " + file.getAbsolutePath() + ", locked at " + new Date(lastModified) + ", has been modified at " + new Date(localFile.lastModified()));
modified = true;
}
} else {
//The lock file is missing
LOG.info("Lock file " + file.getAbsolutePath() + ", does not exist");
modified = true;
}
return modified;
}
public boolean keepAlive() {
locked = locked && lock != null && lock.isValid() && !hasBeenModified();
return locked;
}
}
参考文献:
1. https://github.com/apache/activemq/blob/master/activemq-broker/src/main/java/org/apache/activemq/store/SharedFileLocker.java
2. https://github.com/apache/activemq/blob/master/activemq-broker/src/main/java/org/apache/activemq/util/LockFile.java
没有评论:
发表评论