Search in sources :

Example 1 with ReadOnlyException

use of org.exist.util.ReadOnlyException in project exist by eXist-db.

the class FileLock method tryLock.

/**
 * Attempt to create the lock file and thus acquire a lock.
 *
 * @return false if another process holds the lock
 * @throws ReadOnlyException if the lock file could not be created or saved
 * due to IO errors. The caller may want to switch to read-only mode.
 */
public boolean tryLock() throws ReadOnlyException {
    int attempt = 0;
    while (Files.exists(lockFile)) {
        if (++attempt > 2) {
            return false;
        }
        try {
            read();
        } catch (final IOException e) {
            message("Failed to read lock file", null);
            e.printStackTrace();
        }
        // Check if there's a heart-beat. If not, remove the stale .lck file and try again
        if (checkHeartbeat()) {
            // before we check the heart-beat a second time.
            synchronized (this) {
                try {
                    message("Waiting a short time for the lock to be released...", null);
                    wait(HEARTBEAT + 100);
                } catch (final InterruptedException e) {
                // Nothing to do
                }
            }
            try {
                // Close the open channel, so it can be read again
                if (channel.isOpen()) {
                    channel.close();
                }
                channel = null;
            } catch (final IOException e) {
            // Nothing to do
            }
        }
    }
    try {
        this.lockFile = Files.createFile(lockFile);
    } catch (final IOException e) {
        throw new ReadOnlyException(message("Could not create lock file", e));
    }
    try {
        save();
    } catch (final IOException e) {
        throw new ReadOnlyException(message("Caught exception while trying to write lock file", e));
    }
    // Schedule the heart-beat for the file lock
    final Properties params = new Properties();
    params.put(FileLock.class.getName(), this);
    pool.getScheduler().createPeriodicJob(HEARTBEAT, new FileLockHeartBeat(lockFile.toAbsolutePath().toString()), -1, params);
    return true;
}
Also used : IOException(java.io.IOException) Properties(java.util.Properties) ReadOnlyException(org.exist.util.ReadOnlyException)

Example 2 with ReadOnlyException

use of org.exist.util.ReadOnlyException in project exist by eXist-db.

the class FileLockService method prepare.

@Override
public void prepare(final BrokerPool brokerPool) throws BrokerPoolServiceException {
    // try to acquire lock on the data dir
    final FileLock fileLock = new FileLock(brokerPool, dataDir.resolve(lockFileName));
    this.dataLock.compareAndSet(null, fileLock);
    try {
        final boolean locked = fileLock.tryLock();
        if (!locked) {
            throw new BrokerPoolServiceException(new EXistException("The directory seems to be locked by another " + "database instance. Found a valid lock file: " + fileLock.getFile().toAbsolutePath().toString()));
        }
    } catch (final ReadOnlyException e) {
        LOG.warn(e);
        writable = false;
    }
    if (!writable) {
        brokerPool.setReadOnly();
    }
}
Also used : BrokerPoolServiceException(org.exist.storage.BrokerPoolServiceException) EXistException(org.exist.EXistException) ReadOnlyException(org.exist.util.ReadOnlyException)

Example 3 with ReadOnlyException

use of org.exist.util.ReadOnlyException in project exist by eXist-db.

the class JournalManager method prepare.

@Override
public synchronized void prepare(final BrokerPool pool) throws BrokerPoolServiceException {
    if (!journallingDisabled) {
        try {
            this.journal = new Journal(pool, journalDir);
            this.journal.initialize();
            this.initialized = true;
        } catch (final EXistException | ReadOnlyException e) {
            throw new BrokerPoolServiceException(e);
        }
    }
}
Also used : BrokerPoolServiceException(org.exist.storage.BrokerPoolServiceException) EXistException(org.exist.EXistException) ReadOnlyException(org.exist.util.ReadOnlyException)

Aggregations

ReadOnlyException (org.exist.util.ReadOnlyException)3 EXistException (org.exist.EXistException)2 BrokerPoolServiceException (org.exist.storage.BrokerPoolServiceException)2 IOException (java.io.IOException)1 Properties (java.util.Properties)1