Java Code Examples for org.apache.lucene.store.Lock

The following examples show how to use org.apache.lucene.store.Lock. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: netbeans   Source File: RecordOwnerLockFactoryTest.java    License: Apache License 2.0 6 votes vote down vote up
public void testHasLocks() throws IOException {
    assertFalse(lockFactory.hasLocks());
    final Lock lock1 = lockFactory.makeLock("test1");   //NOI18N
    final Lock lock2 = lockFactory.makeLock("test2");   //NOI18N
    final Lock lock3 = lockFactory.makeLock("test3");   //NOI18N
    final Lock lock4 = lockFactory.makeLock("test4");   //NOI18N
    assertFalse(lockFactory.hasLocks());
    assertTrue(lock2.obtain());
    assertTrue(lockFactory.hasLocks());
    lock2.release();
    assertFalse(lockFactory.hasLocks());
    assertTrue(lock3.obtain());
    assertTrue(lockFactory.hasLocks());
    assertTrue(lock4.obtain());
    assertTrue(lockFactory.hasLocks());
    lockFactory.clearLock("test3"); //NOI18N
    assertTrue(lockFactory.hasLocks());
    assertTrue(lock2.obtain());
    lockFactory.clearLock("test4"); //NOI18N
    assertTrue(lockFactory.hasLocks());
    lock2.release();;
    assertFalse(lockFactory.hasLocks());
}
 
Example 2
Source Project: Elasticsearch   Source File: NodeEnvironment.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Acquires, then releases, all {@code write.lock} files in the given
 * shard paths. The "write.lock" file is assumed to be under the shard
 * path's "index" directory as used by Elasticsearch.
 *
 * @throws LockObtainFailedException if any of the locks could not be acquired
 */
public static void acquireFSLockForPaths(Settings indexSettings, Path... shardPaths) throws IOException {
    Lock[] locks = new Lock[shardPaths.length];
    Directory[] dirs = new Directory[shardPaths.length];
    try {
        for (int i = 0; i < shardPaths.length; i++) {
            // resolve the directory the shard actually lives in
            Path p = shardPaths[i].resolve("index");
            // open a directory (will be immediately closed) on the shard's location
            dirs[i] = new SimpleFSDirectory(p, FsDirectoryService.buildLockFactory(indexSettings));
            // create a lock for the "write.lock" file
            try {
                locks[i] = dirs[i].obtainLock(IndexWriter.WRITE_LOCK_NAME);
            } catch (IOException ex) {
                throw new LockObtainFailedException("unable to acquire " +
                        IndexWriter.WRITE_LOCK_NAME + " for " + p);
            }
        }
    } finally {
        IOUtils.closeWhileHandlingException(locks);
        IOUtils.closeWhileHandlingException(dirs);
    }
}
 
Example 3
Source Project: linden   Source File: FileSystemDirectory.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Lock makeLock(final String name) {
  return new Lock() {
    @Override
    public boolean obtain() {
      return true;
    }

    @Override public void close() throws IOException {
    }

    @Override
    public boolean isLocked() {
      throw new UnsupportedOperationException();
    }

    @Override
    public String toString() {
      return "[email protected]" + new Path(directory, name);
    }
  };
}
 
Example 4
/** Constructs an IndexWriter for the index in <code>d</code>.  Text will be
   analyzed with <code>a</code>.  If <code>create</code> is true, then a new,
   empty index will be created in <code>d</code>, replacing the index already
   there, if any. */
 public IndexWriter(Directory d, Analyzer a, final boolean create)
      throws IOException {
   directory = d;
   analyzer = a;

   Lock writeLock = directory.makeLock("write.lock");
   if (!writeLock.obtain())			  // obtain write lock
     throw new IOException("Index locked for write: " + writeLock);

   synchronized (directory) {			  // in- & inter-process sync
     new Lock.With(directory.makeLock("commit.lock")) {
  public Object doBody() throws IOException {
    if (create)
      segmentInfos.write(directory);
    else
      segmentInfos.read(directory);
    return null;
  }
}.run();
   }
 }
 
Example 5
/** Constructs an IndexWriter for the index in <code>d</code>.  Text will be
   analyzed with <code>a</code>.  If <code>create</code> is true, then a new,
   empty index will be created in <code>d</code>, replacing the index already
   there, if any. */
 public IndexWriter(Directory d, Analyzer a, final boolean create)
      throws IOException {
   directory = d;
   analyzer = a;

   Lock writeLock = directory.makeLock("write.lock");
   if (!writeLock.obtain())			  // obtain write lock
     throw new IOException("Index locked for write: " + writeLock);

   synchronized (directory) {			  // in- & inter-process sync
     new Lock.With(directory.makeLock("commit.lock")) {
  public Object doBody() throws IOException {
    if (create)
      segmentInfos.write(directory);
    else
      segmentInfos.read(directory);
    return null;
  }
}.run();
   }
 }
 
Example 6
/** Constructs an IndexWriter for the index in <code>d</code>.  Text will be
  analyzed with <code>a</code>.  If <code>create</code> is true, then a new,
  empty index will be created in <code>d</code>, replacing the index already
  there, if any. */
public IndexWriter(Directory d, Analyzer a, final boolean create)
     throws IOException {
  directory = d;
  analyzer = a;

  Lock writeLock = directory.makeLock(IndexWriter.WRITE_LOCK_NAME);
  if (!writeLock.obtain(WRITE_LOCK_TIMEOUT)) // obtain write lock
    throw new IOException("Index locked for write: " + writeLock);
  this.writeLock = writeLock;                   // save it

  synchronized (directory) {			  // in- & inter-process sync
    new Lock.With(directory.makeLock(IndexWriter.COMMIT_LOCK_NAME), COMMIT_LOCK_TIMEOUT) {
        public Object doBody() throws IOException {
          if (create)
            segmentInfos.write(directory);
          else
            segmentInfos.read(directory);
          return null;
        }
      }.run();
  }
}
 
Example 7
/** Constructs an IndexWriter for the index in <code>d</code>.  Text will be
  analyzed with <code>a</code>.  If <code>create</code> is true, then a new,
  empty index will be created in <code>d</code>, replacing the index already
  there, if any. */
public IndexWriter(Directory d, Analyzer a, final boolean create)
     throws IOException {
  directory = d;
  analyzer = a;

  Lock writeLock = directory.makeLock(IndexWriter.WRITE_LOCK_NAME);
  if (!writeLock.obtain(WRITE_LOCK_TIMEOUT)) // obtain write lock
    throw new IOException("Index locked for write: " + writeLock);
  this.writeLock = writeLock;                   // save it

  synchronized (directory) {			  // in- & inter-process sync
    new Lock.With(directory.makeLock(IndexWriter.COMMIT_LOCK_NAME), COMMIT_LOCK_TIMEOUT) {
        public Object doBody() throws IOException {
          if (create)
            segmentInfos.write(directory);
          else
            segmentInfos.read(directory);
          return null;
        }
      }.run();
  }
}
 
Example 8
private IndexWriter(Directory d, Analyzer a, final boolean create, boolean closeDir)
  throws IOException {
    this.closeDir = closeDir;
    directory = d;
    analyzer = a;

    Lock writeLock = directory.makeLock(IndexWriter.WRITE_LOCK_NAME);
    if (!writeLock.obtain(WRITE_LOCK_TIMEOUT)) // obtain write lock
      throw new IOException("Index locked for write: " + writeLock);
    this.writeLock = writeLock;                   // save it

    synchronized (directory) {        // in- & inter-process sync
      new Lock.With(directory.makeLock(IndexWriter.COMMIT_LOCK_NAME), COMMIT_LOCK_TIMEOUT) {
          public Object doBody() throws IOException {
            if (create)
              segmentInfos.write(directory);
            else
              segmentInfos.read(directory);
            return null;
          }
        }.run();
    }
}
 
Example 9
private IndexWriter(Directory d, Analyzer a, final boolean create, boolean closeDir)
  throws IOException {
    this.closeDir = closeDir;
    directory = d;
    analyzer = a;

    Lock writeLock = directory.makeLock(IndexWriter.WRITE_LOCK_NAME);
    if (!writeLock.obtain(WRITE_LOCK_TIMEOUT)) // obtain write lock
      throw new IOException("Index locked for write: " + writeLock);
    this.writeLock = writeLock;                   // save it

    synchronized (directory) {        // in- & inter-process sync
      new Lock.With(directory.makeLock(IndexWriter.COMMIT_LOCK_NAME), COMMIT_LOCK_TIMEOUT) {
          public Object doBody() throws IOException {
            if (create)
              segmentInfos.write(directory);
            else
              segmentInfos.read(directory);
            return null;
          }
        }.run();
    }
}
 
Example 10
Source Project: RDFS   Source File: FileSystemDirectory.java    License: Apache License 2.0 6 votes vote down vote up
public Lock makeLock(final String name) {
  return new Lock() {
    public boolean obtain() {
      return true;
    }

    public void release() {
    }

    public boolean isLocked() {
      throw new UnsupportedOperationException();
    }

    public String toString() {
      return "[email protected]" + new Path(directory, name);
    }
  };
}
 
Example 11
Source Project: hadoop-gpu   Source File: FileSystemDirectory.java    License: Apache License 2.0 6 votes vote down vote up
public Lock makeLock(final String name) {
  return new Lock() {
    public boolean obtain() {
      return true;
    }

    public void release() {
    }

    public boolean isLocked() {
      throw new UnsupportedOperationException();
    }

    public String toString() {
      return "[email protected]" + new Path(directory, name);
    }
  };
}
 
Example 12
Source Project: crate   Source File: NodeEnvironment.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Acquires, then releases, all {@code write.lock} files in the given
 * shard paths. The "write.lock" file is assumed to be under the shard
 * path's "index" directory as used by Elasticsearch.
 *
 * @throws LockObtainFailedException if any of the locks could not be acquired
 */
public static void acquireFSLockForPaths(IndexSettings indexSettings, Path... shardPaths) throws IOException {
    Lock[] locks = new Lock[shardPaths.length];
    Directory[] dirs = new Directory[shardPaths.length];
    try {
        for (int i = 0; i < shardPaths.length; i++) {
            // resolve the directory the shard actually lives in
            Path p = shardPaths[i].resolve("index");
            // open a directory (will be immediately closed) on the shard's location
            dirs[i] = new SimpleFSDirectory(p, indexSettings.getValue(FsDirectoryService.INDEX_LOCK_FACTOR_SETTING));
            // create a lock for the "write.lock" file
            try {
                locks[i] = dirs[i].obtainLock(IndexWriter.WRITE_LOCK_NAME);
            } catch (IOException ex) {
                throw new LockObtainFailedException("unable to acquire " +
                                IndexWriter.WRITE_LOCK_NAME + " for " + p, ex);
            }
        }
    } finally {
        IOUtils.closeWhileHandlingException(locks);
        IOUtils.closeWhileHandlingException(dirs);
    }
}
 
Example 13
Source Project: crate   Source File: Lucene.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * This method removes all lucene files from the given directory. It will first try to delete all commit points / segments
 * files to ensure broken commits or corrupted indices will not be opened in the future. If any of the segment files can't be deleted
 * this operation fails.
 */
public static void cleanLuceneIndex(Directory directory) throws IOException {
    try (Lock writeLock = directory.obtainLock(IndexWriter.WRITE_LOCK_NAME)) {
        for (final String file : directory.listAll()) {
            if (file.startsWith(IndexFileNames.SEGMENTS) || file.equals(IndexFileNames.OLD_SEGMENTS_GEN)) {
                directory.deleteFile(file); // remove all segment_N files
            }
        }
    }
    try (IndexWriter writer = new IndexWriter(directory, new IndexWriterConfig(Lucene.STANDARD_ANALYZER)
            .setSoftDeletesField(Lucene.SOFT_DELETES_FIELD)
            .setMergePolicy(NoMergePolicy.INSTANCE) // no merges
            .setCommitOnClose(false) // no commits
            .setOpenMode(IndexWriterConfig.OpenMode.CREATE))) { // force creation - don't append...
        // do nothing and close this will kick of IndexFileDeleter which will remove all pending files
    }
}
 
Example 14
Source Project: netbeans   Source File: RecordOwnerLockFactory.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Lock makeLock(String lockName) {
    synchronized (locks) {
        RecordOwnerLock res = locks.get(lockName);
        if (res == null) {
            res = new RecordOwnerLock();
            locks.put(lockName, res);
        }
        return res;
    }
}
 
Example 15
Source Project: netbeans   Source File: RecordOwnerLockFactory.java    License: Apache License 2.0 5 votes vote down vote up
Collection<? extends Lock> forceClearLocks() {
    synchronized (locks) {
        final Queue<RecordOwnerLock> locked = new ArrayDeque<>();
        for (Iterator<RecordOwnerLock> it = locks.values().iterator();
            it.hasNext();) {
            RecordOwnerLock lock = it.next();
            if (lock.isLocked()) {
                it.remove();
                locked.offer(lock);
            }
        }
        return locked;
    }
}
 
Example 16
Source Project: netbeans   Source File: RecordOwnerLockFactoryTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testLock() throws IOException {
    final Lock lock = lockFactory.makeLock("test"); //NOI18N
    assertFalse(lock.isLocked());
    lock.obtain();
    assertTrue(lock.isLocked());
    lock.release();
    assertFalse(lock.isLocked());
}
 
Example 17
Source Project: netbeans   Source File: RecordOwnerLockFactoryTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testLockInstances() throws IOException {
    final Lock lock1 = lockFactory.makeLock("test"); //NOI18N
    final Lock lock2 = lockFactory.makeLock("test"); //NOI18N
    assertFalse(lock1.isLocked());
    assertFalse(lock2.isLocked());
    lock1.obtain();
    assertTrue(lock1.isLocked());
    assertTrue(lock2.isLocked());
    lock2.release();
    assertFalse(lock1.isLocked());
    assertFalse(lock2.isLocked());
}
 
Example 18
Source Project: netbeans   Source File: RecordOwnerLockFactoryTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testClearLock() throws IOException {
    Lock lock = lockFactory.makeLock("test"); //NOI18N
    assertFalse(lock.isLocked());
    lock.obtain();
    assertTrue(lock.isLocked());
    lockFactory.clearLock("test");  //NOI18N
    assertFalse(lock.isLocked());
}
 
Example 19
Source Project: netbeans   Source File: RecordOwnerLockFactoryTest.java    License: Apache License 2.0 5 votes vote down vote up
public void testForceClearLocks() throws IOException {
    final Lock lock1 = lockFactory.makeLock("test1");   //NOI18N
    assertTrue(lock1.obtain());
    assertTrue(lockFactory.hasLocks());
    lockFactory.makeLock("test2");  //NOI18N
    assertTrue(lockFactory.makeLock("test3").obtain()); //NOI18N
    lockFactory.makeLock("test3").release();    //NOI18N
    assertTrue(lockFactory.hasLocks());
    Collection<? extends Lock> locks = lockFactory.forceClearLocks();
    assertEquals(1, locks.size());
    assertEquals(lock1, locks.iterator().next());
    assertFalse(lockFactory.hasLocks());
}
 
Example 20
Source Project: Elasticsearch   Source File: NodeEnvironment.java    License: Apache License 2.0 5 votes vote down vote up
private static void releaseAndNullLocks(Lock[] locks) {
    for (int i = 0; i < locks.length; i++) {
        if (locks[i] != null) {
            IOUtils.closeWhileHandlingException(locks[i]);
        }
        locks[i] = null;
    }
}
 
Example 21
Source Project: Elasticsearch   Source File: NodeEnvironment.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void close() {
    if (closed.compareAndSet(false, true) && locks != null) {
        for (Lock lock : locks) {
            try {
                logger.trace("releasing lock [{}]", lock);
                lock.close();
            } catch (IOException e) {
                logger.trace("failed to release lock [{}]", e, lock);
            }
        }
    }
}
 
Example 22
Source Project: Elasticsearch   Source File: NodeEnvironment.java    License: Apache License 2.0 5 votes vote down vote up
private boolean assertEnvIsLocked() {
    if (!closed.get() && locks != null) {
        for (Lock lock : locks) {
            try {
                lock.ensureValid();
            } catch (IOException e) {
                logger.warn("lock assertion failed", e);
                return false;
            }
        }
    }
    return true;
}
 
Example 23
Source Project: ignite   Source File: GridLuceneLockFactory.java    License: Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override public Lock obtainLock(Directory dir, String lockName) throws IOException {
    if (locks.add(lockName))
        return new LockImpl(lockName);
    else
        throw new LockObtainFailedException("lock instance already obtained: (dir=" + dir + ", lockName=" + lockName + ")");
}
 
Example 24
Source Project: incubator-retired-blur   Source File: FencedDirectory.java    License: Apache License 2.0 5 votes vote down vote up
public Lock makeLock(String name) {
  if (name.equals(WRITE_LOCK)) {
    if (_writeLock == null) {
      return _writeLock = _directory.makeLock(name);
    }
    return _writeLock;
  } else {
    throw new RuntimeException("Locks with name [" + name + "] not supported.");
  }
}
 
Example 25
/** Pops segments off of segmentInfos stack down to minSegment, merges them,
   and pushes the merged index onto the top of the segmentInfos stack. */
 private final void mergeSegments(int minSegment, boolean delete)
     throws IOException {
   String mergedName = newSegmentName();
   int mergedDocCount = 0;
   if (infoStream != null) infoStream.print("merging segments");
   SegmentMerger merger = new SegmentMerger(directory, mergedName);
   final Vector segmentsToDelete = new Vector();
   for (int i = minSegment; i < segmentInfos.size(); i++) {
     SegmentInfo si = segmentInfos.info(i);
     if (infoStream != null)
infoStream.print(" " + si.name + " (" + si.docCount + " docs)");
     SegmentReader reader = new SegmentReader(si);
     merger.add(reader);
     if (delete)
segmentsToDelete.addElement(reader);	  // queue for deletion
     mergedDocCount += si.docCount;
   }
   if (infoStream != null) {
     infoStream.println();
     infoStream.println(" into "+mergedName+" ("+mergedDocCount+" docs)");
   }
   merger.merge();

   segmentInfos.setSize(minSegment);		  // pop old infos & add new
   segmentInfos.addElement(new SegmentInfo(mergedName, mergedDocCount,
				    directory));
   
   synchronized (directory) {			  // in- & inter-process sync
     new Lock.With(directory.makeLock("commit.lock")) {
  public Object doBody() throws IOException {
    segmentInfos.write(directory);	  // commit before deleting
    deleteSegments(segmentsToDelete);	  // delete now-unused segments
    return null;
  }
}.run();
   }
 }
 
Example 26
/** Pops segments off of segmentInfos stack down to minSegment, merges them,
   and pushes the merged index onto the top of the segmentInfos stack. */
 private final void mergeSegments(int minSegment, boolean delete)
     throws IOException {
   String mergedName = newSegmentName();
   int mergedDocCount = 0;
   if (infoStream != null) infoStream.print("merging segments");
   SegmentMerger merger = new SegmentMerger(directory, mergedName);
   final Vector segmentsToDelete = new Vector();
   for (int i = minSegment; i < segmentInfos.size(); i++) {
     SegmentInfo si = segmentInfos.info(i);
     if (infoStream != null)
infoStream.print(" " + si.name + " (" + si.docCount + " docs)");
     SegmentReader reader = new SegmentReader(si);
     merger.add(reader);
     if (delete)
segmentsToDelete.addElement(reader);	  // queue for deletion
     mergedDocCount += si.docCount;
   }
   if (infoStream != null) {
     infoStream.println();
     infoStream.println(" into "+mergedName+" ("+mergedDocCount+" docs)");
   }
   merger.merge();

   segmentInfos.setSize(minSegment);		  // pop old infos & add new
   segmentInfos.addElement(new SegmentInfo(mergedName, mergedDocCount,
				    directory));
   
   synchronized (directory) {			  // in- & inter-process sync
     new Lock.With(directory.makeLock("commit.lock")) {
  public Object doBody() throws IOException {
    segmentInfos.write(directory);	  // commit before deleting
    deleteSegments(segmentsToDelete);	  // delete now-unused segments
    return null;
  }
}.run();
   }
 }
 
Example 27
/** Merges the provided indexes into this index.
  * <p>After this completes, the index is optimized. */
 public synchronized void addIndexes(IndexReader[] readers)
   throws IOException {

   optimize();					  // start with zero or 1 seg

   String mergedName = newSegmentName();
   SegmentMerger merger = new SegmentMerger(directory, mergedName, false);

   if (segmentInfos.size() == 1)                 // add existing index, if any
     merger.add(new SegmentReader(segmentInfos.info(0)));

   for (int i = 0; i < readers.length; i++)      // add new indexes
     merger.add(readers[i]);

   int docCount = merger.merge();                // merge 'em

   segmentInfos.setSize(0);                      // pop old infos & add new
   segmentInfos.addElement(new SegmentInfo(mergedName, docCount, directory));

   synchronized (directory) {			  // in- & inter-process sync
     new Lock.With(directory.makeLock("commit.lock"), COMMIT_LOCK_TIMEOUT) {
  public Object doBody() throws IOException {
    segmentInfos.write(directory);	  // commit changes
    return null;
  }
}.run();
   }
 }
 
Example 28
/** Merges the provided indexes into this index.
  * <p>After this completes, the index is optimized. */
 public synchronized void addIndexes(IndexReader[] readers)
   throws IOException {

   optimize();					  // start with zero or 1 seg

   String mergedName = newSegmentName();
   SegmentMerger merger = new SegmentMerger(directory, mergedName, false);

   if (segmentInfos.size() == 1)                 // add existing index, if any
     merger.add(new SegmentReader(segmentInfos.info(0)));

   for (int i = 0; i < readers.length; i++)      // add new indexes
     merger.add(readers[i]);

   int docCount = merger.merge();                // merge 'em

   segmentInfos.setSize(0);                      // pop old infos & add new
   segmentInfos.addElement(new SegmentInfo(mergedName, docCount, directory));

   synchronized (directory) {			  // in- & inter-process sync
     new Lock.With(directory.makeLock("commit.lock")) {
  public Object doBody() throws IOException {
    segmentInfos.write(directory);	  // commit changes
    return null;
  }
}.run();
   }
 }
 
Example 29
Source Project: crate   Source File: NodeEnvironment.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void close() {
    if (closed.compareAndSet(false, true) && locks != null) {
        for (Lock lock : locks) {
            try {
                logger.trace("releasing lock [{}]", lock);
                lock.close();
            } catch (IOException e) {
                logger.trace(() -> new ParameterizedMessage("failed to release lock [{}]", lock), e);
            }
        }
    }
}
 
Example 30
Source Project: crate   Source File: NodeEnvironment.java    License: Apache License 2.0 5 votes vote down vote up
private void assertEnvIsLocked() {
    if (!closed.get() && locks != null) {
        for (Lock lock : locks) {
            try {
                lock.ensureValid();
            } catch (IOException e) {
                logger.warn("lock assertion failed", e);
                throw new IllegalStateException("environment is not locked", e);
            }
        }
    }
}