Example usage for org.apache.lucene.store LockObtainFailedException LockObtainFailedException

List of usage examples for org.apache.lucene.store LockObtainFailedException LockObtainFailedException

Introduction

In this page you can find the example usage for org.apache.lucene.store LockObtainFailedException LockObtainFailedException.

Prototype

public LockObtainFailedException(String message) 

Source Link

Usage

From source file:cn.codepub.redis.directory.RedisLockFactory.java

License:Apache License

@Override
public Lock obtainLock(@NonNull Directory dir, String lockName) throws IOException {
    if (!(dir instanceof RedisDirectory)) {
        throw new IllegalArgumentException("Expect argument of type [" + RedisDirectory.class.getName() + "]!");
    }/*from w  ww.j  a  v a 2 s.  c o  m*/
    Path lockFile = lockFileDirectory.resolve(lockName);
    try {
        Files.createFile(lockFile);
        log.debug("Lock file path = {}", lockFile.toFile().getAbsolutePath());
    } catch (IOException ignore) {
        //ignore
        log.debug("Lock file already exists!");
    }
    final Path realPath = lockFile.toRealPath();
    final FileTime creationTime = Files.readAttributes(realPath, BasicFileAttributes.class).creationTime();
    if (LOCK_HELD.add(realPath.toString())) {
        FileChannel fileChannel = null;
        FileLock lock = null;
        try {
            fileChannel = FileChannel.open(realPath, StandardOpenOption.CREATE, StandardOpenOption.WRITE);
            lock = fileChannel.tryLock();
            if (lock != null) {
                return new RedisLock(lock, fileChannel, realPath, creationTime);
            } else {
                throw new LockObtainFailedException("Lock held by another program: " + realPath);
            }
        } finally {
            if (lock == null) {
                IOUtils.closeQuietly(fileChannel);
                clearLockHeld(realPath);
            }
        }
    } else {
        throw new LockObtainFailedException("Lock held by this virtual machine: " + realPath);
    }
}

From source file:com.browseengine.bobo.geosearch.solo.index.impl.GeoOnlyIndexer.java

License:Apache License

public GeoOnlyIndexer(GeoSearchConfig config, Directory directory, String indexName) throws IOException {
    this.config = config;
    this.directory = directory;
    this.indexName = indexName;

    lock = directory.makeLock(indexName);
    if (!lock.obtain()) {
        throw new LockObtainFailedException("Index locked for write: " + indexName);
    }/*  www.j ava 2s .  c  o  m*/
}

From source file:com.esri.gpt.catalog.lucene.LuceneIndexAdapter.java

License:Apache License

/**
 * Obtains a write lock intended for background synchronization and 
 * optimization processes./*from w w  w  . j  a  v a  2 s .  c o m*/
 * <br/>There will be no wait time for this type of lock, if not obtained 
 * immediately an exception will be thrown.
 * <br/>It is recommended to use LuceneIndexAdapter.touck() prior to
 * obtaining the background lock to ensure a proper directory structure.
 * <br/>The lock must be closed when the background process is complete.
 * @return the obtained lock
 * @throws LockObtainFailedException if the lock wwas not obtained
 */
public synchronized Lock obtainBackgroundLock() throws LockObtainFailedException {
    Lock lock = null;
    String pfx = "Unable to obtain background lock.";
    if (this.luceneConfig.getUseNativeFSLockFactory()) {
        try {
            NativeFSLockFactory nativeLockFactory = this.getNativeLockFactory();
            lock = nativeLockFactory.makeLock(BACKGROUND_LOCKNAME);
        } catch (IOException e) {
            String msg = pfx + " " + e.getMessage();
            getLogger().log(Level.WARNING, pfx, e);
            throw new LockObtainFailedException(msg);
        }
    } else {
        try {
            File fDir = new File(this.luceneConfig.getIndexLocation());
            SimpleFSLockFactory factory = new SimpleFSLockFactory(fDir);
            factory.setLockPrefix("lucene-simple");
            lock = factory.makeLock(BACKGROUND_LOCKNAME);
        } catch (IOException e) {
            String msg = pfx + " " + e.getMessage();
            getLogger().log(Level.WARNING, pfx, e);
            throw new LockObtainFailedException(msg);
        }
    }
    try {
        boolean wasObtained = lock.obtain();
        if (!wasObtained) {
            String msg = "Unable to obtain background lock for: " + lock.toString();
            throw new LockObtainFailedException(msg);
        }
        return lock;
    } catch (LockObtainFailedException e) {
        throw e;
    } catch (IOException e) {
        String msg = pfx + " " + e.getMessage();
        getLogger().log(Level.WARNING, pfx, e);
        throw new LockObtainFailedException(msg);
    }
}

From source file:com.github.lucene.store.jdbc.lock.PhantomReadLock.java

License:Apache License

@Override
public void obtain() throws IOException {
    if (jdbcDirectory.getDialect().useExistsBeforeInsertLock()) {
        // there are databases where the fact that an exception was
        // thrown invalidates the connection. So first we check if it
        // exists, and then insert it.
        if (jdbcDirectory.fileExists(name)) {
            throw new LockObtainFailedException("Lock instance already obtained: " + this);
        }//from   w w  w .ja  v  a2 s  .  c  o m
    }
    jdbcDirectory.getJdbcTemplate().executeUpdate(jdbcDirectory.getTable().sqlInsert(),
            new JdbcTemplate.PrepateStatementAwareCallback() {
                @Override
                public void fillPrepareStatement(final PreparedStatement ps) throws Exception {
                    ps.setFetchSize(1);
                    ps.setString(1, name);
                    ps.setNull(2, Types.BLOB);
                    ps.setLong(3, 0);
                    ps.setBoolean(4, false);
                }
            });
}

From source file:com.github.lucene.store.jdbc.lock.SelectForUpdateLock.java

License:Apache License

@Override
public void obtain() throws IOException {
    jdbcDirectory.getJdbcTemplate().executeSelect(jdbcDirectory.getTable().sqlSelectNameForUpdateNoWait(),
            new JdbcTemplate.ExecuteSelectCallback() {

                @Override//from  w  w  w.  j  a  v a 2 s .co m
                public void fillPrepareStatement(final PreparedStatement ps) throws Exception {
                    ps.setFetchSize(1);
                    ps.setString(1, name);
                }

                @Override
                public Object execute(final ResultSet rs) throws Exception {
                    if (!rs.next()) {
                        throw new LockObtainFailedException("Lock instance already obtained: " + this);
                    }
                    return null;
                }
            });
}

From source file:org.apache.maven.index.context.DefaultIndexingContext.java

License:Apache License

private static void unlockForcibly(final TrackingLockFactory lockFactory, final Directory dir)
        throws IOException {
    //Warning: Not doable in lucene >= 5.3 consider to remove it as IndexWriter.unlock
    //was always strongly non recommended by Lucene.
    //For now try to do the best to simulate the IndexWriter.unlock at least on FSDirectory
    //using FSLockFactory, the RAMDirectory uses SingleInstanceLockFactory.
    //custom lock factory?
    if (lockFactory != null) {
        final Set<? extends Lock> emittedLocks = lockFactory.getEmittedLocks(IndexWriter.WRITE_LOCK_NAME);
        for (Lock emittedLock : emittedLocks) {
            emittedLock.close();/*from   w ww  .  j a v  a  2  s  .  c o  m*/
        }
    }
    if (dir instanceof FSDirectory) {
        final FSDirectory fsdir = (FSDirectory) dir;
        final Path dirPath = fsdir.getDirectory();
        if (Files.isDirectory(dirPath)) {
            Path lockPath = dirPath.resolve(IndexWriter.WRITE_LOCK_NAME);
            try {
                lockPath = lockPath.toRealPath();
            } catch (IOException ioe) {
                // Not locked
                return;
            }
            try (final FileChannel fc = FileChannel.open(lockPath, StandardOpenOption.CREATE,
                    StandardOpenOption.WRITE)) {
                final FileLock lck = fc.tryLock();
                if (lck == null) {
                    // Still active
                    throw new LockObtainFailedException("Lock held by another process: " + lockPath);
                } else {
                    // Not held fine to release
                    lck.close();
                }
            }
            Files.delete(lockPath);
        }
    }
}

From source file:org.apache.solr.core.SolrCore.java

License:Apache License

void initIndex(boolean reload) throws IOException {

    String indexDir = getNewIndexDir();
    boolean indexExists = getDirectoryFactory().exists(indexDir);
    boolean firstTime;
    synchronized (SolrCore.class) {
        firstTime = dirs.add(getDirectoryFactory().normalize(indexDir));
    }/*from   ww w .  j ava 2  s  .  c  om*/
    boolean removeLocks = solrConfig.unlockOnStartup;

    initIndexReaderFactory();

    if (indexExists && firstTime && !reload) {

        Directory dir = directoryFactory.get(indexDir, DirContext.DEFAULT,
                getSolrConfig().indexConfig.lockType);
        try {
            if (IndexWriter.isLocked(dir)) {
                if (removeLocks) {
                    log.warn(logid + "WARNING: Solr index directory '{}' is locked.  Unlocking...", indexDir);
                    IndexWriter.unlock(dir);
                } else {
                    log.error(logid + "Solr index directory '{}' is locked.  Throwing exception", indexDir);
                    throw new LockObtainFailedException("Index locked for write for core " + name);
                }

            }
        } finally {
            directoryFactory.release(dir);
        }
    }

    // Create the index if it doesn't exist.
    if (!indexExists) {
        log.warn(logid + "Solr index directory '" + new File(indexDir) + "' doesn't exist."
                + " Creating new index...");

        SolrIndexWriter writer = SolrIndexWriter.create("SolrCore.initIndex", indexDir, getDirectoryFactory(),
                true, getLatestSchema(), solrConfig.indexConfig, solrDelPolicy, codec);
        writer.close();
    }

}