コード例 #1
0
 /**
  * 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 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] = Lucene.acquireWriteLock(dirs[i]);
       } catch (IOException ex) {
         throw new LockObtainFailedException(
             "unable to acquire " + IndexWriter.WRITE_LOCK_NAME + " for " + p);
       }
     }
   } finally {
     IOUtils.closeWhileHandlingException(locks);
     IOUtils.closeWhileHandlingException(dirs);
   }
 }