org.apache.lucene.store.LockObtainFailedException Java Examples

The following examples show how to use org.apache.lucene.store.LockObtainFailedException. 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 File: ElasticsearchNodeCommand.java    From crate with Apache License 2.0 6 votes vote down vote up
protected void processNodePathsWithLock(Terminal terminal, OptionSet options, Environment env) throws IOException {
    terminal.println(Terminal.Verbosity.VERBOSE, "Obtaining lock for node");
    Integer nodeOrdinal = nodeOrdinalOption.value(options);
    if (nodeOrdinal == null) {
        nodeOrdinal = 0;
    }
    try (NodeEnvironment.NodeLock lock = new NodeEnvironment.NodeLock(nodeOrdinal, LOGGER, env, Files::exists)) {
        final Path[] dataPaths =
                Arrays.stream(lock.getNodePaths()).filter(Objects::nonNull).map(p -> p.path).toArray(Path[]::new);
        if (dataPaths.length == 0) {
            throw new ElasticsearchException(NO_NODE_FOLDER_FOUND_MSG);
        }
        processNodePaths(terminal, dataPaths, env);
    } catch (LockObtainFailedException ex) {
        throw new ElasticsearchException(
                FAILED_TO_OBTAIN_NODE_LOCK_MSG + " [" + ex.getMessage() + "]");
    }
}
 
Example #2
Source File: StandardSearchEngine.java    From document-management-software with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public boolean isLocked() {
	boolean result = false;

	try {
		Directory directory = getIndexDataDirectory();
		try {
			directory.obtainLock(IndexWriter.WRITE_LOCK_NAME).close();
			result = false;
		} catch (LockObtainFailedException failed) {
			result = true;
		}
	} catch (Throwable e) {
		log.warn("isLocked {}", e.getMessage(), e);
	}

	return result;
}
 
Example #3
Source File: NodeEnvironment.java    From crate with 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 #4
Source File: BlurUtilsTest.java    From incubator-retired-blur with Apache License 2.0 6 votes vote down vote up
@Test
public void testFetchDocuments() throws CorruptIndexException, LockObtainFailedException, IOException {
  Selector selector = new Selector();
  selector.setLocationId("shard/0");
  Set<String> columnFamiliesToFetch = new HashSet<String>();
  columnFamiliesToFetch.add("f1");
  columnFamiliesToFetch.add("f2");
  selector.setColumnFamiliesToFetch(columnFamiliesToFetch);

  ResetableDocumentStoredFieldVisitor resetableDocumentStoredFieldVisitor = new ResetableDocumentStoredFieldVisitor();
  // List<Document> docs = BlurUtil.fetchDocuments(getReader(), new
  // Term("a","b"), resetableDocumentStoredFieldVisitor, selector, 10000000,
  // "test-context", new
  // Term(BlurConstants.PRIME_DOC,BlurConstants.PRIME_DOC_VALUE));
  AtomicBoolean moreDocsToFetch = new AtomicBoolean(false);
  AtomicInteger totalRecords = new AtomicInteger();
  List<Document> docs = BlurUtil.fetchDocuments(getReader(), resetableDocumentStoredFieldVisitor, selector, 10000000,
      "test-context", new Term(BlurConstants.PRIME_DOC, BlurConstants.PRIME_DOC_VALUE), null, moreDocsToFetch,
      totalRecords, null);
  assertEquals(docs.size(), 1);
  assertFalse(moreDocsToFetch.get());
  assertEquals(1, totalRecords.get());
}
 
Example #5
Source File: NodeEnvironment.java    From Elasticsearch with 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 #6
Source File: SolrCoreCheckLockOnStartupTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testSimpleLockErrorOnStartup() throws Exception {

  Directory directory = newFSDirectory(new File(initAndGetDataDir(), "index").toPath(), SimpleFSLockFactory.INSTANCE);
  //creates a new IndexWriter without releasing the lock yet
  IndexWriter indexWriter = new IndexWriter(directory, new IndexWriterConfig(null));

  ignoreException("locked");
  try {
    System.setProperty("solr.tests.lockType",DirectoryFactory.LOCK_TYPE_SIMPLE);
    //opening a new core on the same index
    initCore("solrconfig-basic.xml", "schema.xml");
    if (checkForCoreInitException(LockObtainFailedException.class))
      return;
    fail("Expected " + LockObtainFailedException.class.getSimpleName());
  } finally {
    System.clearProperty("solr.tests.lockType");
    unIgnoreException("locked");
    indexWriter.close();
    directory.close();
    deleteCore();
  }
}
 
Example #7
Source File: BlurUtilsTest.java    From incubator-retired-blur with Apache License 2.0 6 votes vote down vote up
@Test
public void testFetchDocumentsStrictFamilyOrder() throws CorruptIndexException, LockObtainFailedException,
    IOException {
  Selector selector = new Selector();
  selector.setLocationId("shard/0");
  Set<String> columnFamiliesToFetch = new HashSet<String>();
  columnFamiliesToFetch.add("f1");
  columnFamiliesToFetch.add("f2");
  selector.setColumnFamiliesToFetch(columnFamiliesToFetch);
  selector.addToOrderOfFamiliesToFetch("f1");
  selector.addToOrderOfFamiliesToFetch("f2");

  ResetableDocumentStoredFieldVisitor resetableDocumentStoredFieldVisitor = new ResetableDocumentStoredFieldVisitor();
  AtomicBoolean moreDocsToFetch = new AtomicBoolean(false);
  AtomicInteger totalRecords = new AtomicInteger();
  List<Document> docs = BlurUtil.fetchDocuments(getReaderWithDocsHavingFamily(), resetableDocumentStoredFieldVisitor,
      selector, 10000000, "test-context", new Term(BlurConstants.PRIME_DOC, BlurConstants.PRIME_DOC_VALUE), null,
      moreDocsToFetch, totalRecords, null);
  assertEquals(docs.size(), 2);
  assertEquals(docs.get(0).getField("family").stringValue(), "f1");
  assertEquals(docs.get(1).getField("family").stringValue(), "f2");
  assertFalse(moreDocsToFetch.get());
  assertEquals(2, totalRecords.get());
}
 
Example #8
Source File: BlurUtilsTest.java    From incubator-retired-blur with Apache License 2.0 6 votes vote down vote up
private IndexReader getReader() throws CorruptIndexException, LockObtainFailedException, IOException {
  RAMDirectory directory = new RAMDirectory();
  IndexWriterConfig conf = new IndexWriterConfig(LUCENE_VERSION, new KeywordAnalyzer());
  IndexWriter writer = new IndexWriter(directory, conf);
  Document doc = new Document();
  doc.add(new StringField(BlurConstants.PRIME_DOC, BlurConstants.PRIME_DOC_VALUE, Store.NO));
  doc.add(new StringField("a", "b", Store.YES));
  doc.add(new StringField("family", "f1", Store.YES));

  Document doc1 = new Document();
  doc.add(new StringField("a", "b", Store.YES));
  writer.addDocument(doc);
  writer.addDocument(doc1);
  writer.close();
  return DirectoryReader.open(directory);
}
 
Example #9
Source File: TestAddIndexes.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/** Make sure an open IndexWriter on an incoming Directory
 *  causes a LockObtainFailedException */
public void testLocksBlock() throws Exception {
  Directory src = newDirectory();
  RandomIndexWriter w1 = new RandomIndexWriter(random(), src);
  w1.addDocument(new Document());
  w1.commit();

  Directory dest = newDirectory();

  IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random()));
  RandomIndexWriter w2 = new RandomIndexWriter(random(), dest, iwc);

  expectThrows(LockObtainFailedException.class, () -> {
    w2.addIndexes(src);
  });

  w1.close();
  w2.close();
  IOUtils.close(src, dest);
}
 
Example #10
Source File: BlurUtilsTest.java    From incubator-retired-blur with Apache License 2.0 6 votes vote down vote up
private IndexReader getReaderWithDocsHavingFamily() throws CorruptIndexException, LockObtainFailedException,
    IOException {
  RAMDirectory directory = new RAMDirectory();
  IndexWriterConfig conf = new IndexWriterConfig(LUCENE_VERSION, new KeywordAnalyzer());
  IndexWriter writer = new IndexWriter(directory, conf);
  Document doc = new Document();
  doc.add(new StringField(BlurConstants.PRIME_DOC, BlurConstants.PRIME_DOC_VALUE, Store.NO));
  doc.add(new StringField("a", "b", Store.YES));
  doc.add(new StringField("family", "f2", Store.YES));

  Document doc1 = new Document();
  doc1.add(new StringField("a", "b", Store.YES));
  doc1.add(new StringField("family", "f1", Store.YES));
  writer.addDocument(doc);
  writer.addDocument(doc1);
  writer.close();
  return DirectoryReader.open(directory);
}
 
Example #11
Source File: Collection.java    From openbd-core with GNU General Public License v3.0 5 votes vote down vote up
public synchronized DocumentWriter getDocumentWriter() throws CorruptIndexException, LockObtainFailedException, IOException {
	if ( documentwriter != null )
		return documentwriter;
	
	documentwriter	= new DocumentWriter( this );
	return documentwriter;
}
 
Example #12
Source File: SolrCoreCheckLockOnStartupTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testNativeLockErrorOnStartup() throws Exception {

  File indexDir = new File(initAndGetDataDir(), "index");
  if (log.isInfoEnabled()) {
    log.info("Acquiring lock on {}", indexDir.getAbsolutePath());
  }
  Directory directory = newFSDirectory(indexDir.toPath(), NativeFSLockFactory.INSTANCE);
  //creates a new IndexWriter without releasing the lock yet
  IndexWriter indexWriter = new IndexWriter(directory, new IndexWriterConfig(null));

  ignoreException("locked");
  try {
    System.setProperty("solr.tests.lockType",DirectoryFactory.LOCK_TYPE_NATIVE);
    //opening a new core on the same index
    initCore("solrconfig-basic.xml", "schema.xml");
    CoreContainer cc = h.getCoreContainer();
    if (checkForCoreInitException(LockObtainFailedException.class))
      return;
    fail("Expected " + LockObtainFailedException.class.getSimpleName());
  } finally {
    System.clearProperty("solr.tests.lockType");
    unIgnoreException("locked");
    indexWriter.close();
    directory.close();
    deleteCore();
  }
}
 
Example #13
Source File: BlurUtilsTest.java    From incubator-retired-blur with Apache License 2.0 5 votes vote down vote up
@Test
public void testFetchDocumentsWithoutFamily() throws CorruptIndexException, LockObtainFailedException, IOException {
  Selector selector = new Selector();
  selector.setLocationId("shard/0");
  ResetableDocumentStoredFieldVisitor resetableDocumentStoredFieldVisitor = new ResetableDocumentStoredFieldVisitor();
  AtomicBoolean moreDocsToFetch = new AtomicBoolean(false);
  AtomicInteger totalRecords = new AtomicInteger();
  List<Document> docs = BlurUtil.fetchDocuments(getReader(), resetableDocumentStoredFieldVisitor, selector, 10000000,
      "test-context", new Term(BlurConstants.PRIME_DOC, BlurConstants.PRIME_DOC_VALUE), null, moreDocsToFetch,
      totalRecords, null);
  assertEquals(docs.size(), 2);
  assertFalse(moreDocsToFetch.get());
  assertEquals(2, totalRecords.get());
}
 
Example #14
Source File: BlurIndexWriter.java    From incubator-retired-blur with Apache License 2.0 5 votes vote down vote up
public BlurIndexWriter(Directory d, IndexWriterConfig conf, boolean makeReaderExitable) throws CorruptIndexException,
    LockObtainFailedException, IOException {
  super(fence(d), conf);
  try {
    internalLock = findInternalLock();
  } catch (Exception e) {
    throw new RuntimeException("Could not get the write lock instance.", e);
  }
  _makeReaderExitable = makeReaderExitable;
  deleteUnusedFiles();
}
 
Example #15
Source File: SuperQueryTest.java    From incubator-retired-blur with Apache License 2.0 5 votes vote down vote up
public static Directory createIndex() throws CorruptIndexException, LockObtainFailedException, IOException {
  Directory directory = new RAMDirectory();
  IndexWriter writer = new IndexWriter(directory, new IndexWriterConfig(LUCENE_VERSION, new StandardAnalyzer(LUCENE_VERSION)));
  writer.addDocuments(addPrime(Arrays.asList(newDocument(newStringField(ROW_ID, "1"), newStringField(PERSON_NAME, NAME1)),
      newDocument(newStringField(ROW_ID, "1"), newStringField(PERSON_NAME, NAME1)), newDocument(newStringField(ROW_ID, "1"), newStringField(ADDRESS_STREET, STREET1)))));
  writer.addDocuments(addPrime(Arrays.asList(newDocument(newStringField(ROW_ID, "2"), newStringField(PERSON_NAME, NAME2)),
      newDocument(newStringField(ROW_ID, "2"), newStringField(ADDRESS_STREET, STREET1)))));
  writer.addDocuments(addPrime(Arrays.asList(newDocument(newStringField(ROW_ID, "3"), newStringField(PERSON_NAME, NAME1)),
      newDocument(newStringField(ROW_ID, "3"), newStringField(ADDRESS_STREET, STREET1)), newDocument(newStringField(ROW_ID, "3"), newStringField(ADDRESS_STREET, STREET2)))));
  writer.close();
  return directory;
}
 
Example #16
Source File: RandomSuperQueryTest.java    From incubator-retired-blur with Apache License 2.0 5 votes vote down vote up
private Directory createIndex(Random random, Collection<Query> sampler) throws CorruptIndexException, LockObtainFailedException, IOException {
  Directory directory = new RAMDirectory();
  String[] columnFamilies = genWords(random, MIN_NUM_COL_FAM, MAX_NUM_COL_FAM, "colfam");
  Map<String, String[]> columns = new HashMap<String, String[]>();
  for (int i = 0; i < columnFamilies.length; i++) {
    columns.put(columnFamilies[i], genWords(random, MIN_NUM_COLS, MAX_NUM_COLS, "col"));
  }
  IndexWriter writer = new IndexWriter(directory, new IndexWriterConfig(LUCENE_VERSION, new StandardAnalyzer(LUCENE_VERSION)));
  int numberOfDocs = random.nextInt(MAX_NUM_OF_DOCS) + 1;
  for (int i = 0; i < numberOfDocs; i++) {
    writer.addDocuments(generatSuperDoc(random, columns, sampler));
  }
  writer.close();
  return directory;
}
 
Example #17
Source File: VerificationHost_s.java    From gumtree-spoon-ast-diff with Apache License 2.0 5 votes vote down vote up
public static boolean restartStatefulHost(ServiceHost host, boolean failOnIndexDeletion)
        throws Throwable {
    long exp = Utils.fromNowMicrosUtc(host.getOperationTimeoutMicros());

    do {
        Thread.sleep(2000);
        try {
            if (host.isAuthorizationEnabled()) {
                host.setAuthenticationService(new AuthorizationContextService());
            }
            host.start();
            return true;
        } catch (Throwable e) {
            Logger.getAnonymousLogger().warning(String
                    .format("exception on host restart: %s", e.getMessage()));
            try {
                host.stop();
            } catch (Throwable e1) {
                return false;
            }
            if (e instanceof LockObtainFailedException && !failOnIndexDeletion) {
                Logger.getAnonymousLogger()
                        .warning("Lock held exception on host restart, retrying");
                continue;
            }
            return false;
        }
    } while (Utils.getSystemNowMicrosUtc() < exp);
    return false;
}
 
Example #18
Source File: VerificationHost_t.java    From gumtree-spoon-ast-diff with Apache License 2.0 5 votes vote down vote up
public static boolean restartStatefulHost(ServiceHost host, boolean failOnIndexDeletion)
        throws Throwable {
    long exp = Utils.fromNowMicrosUtc(host.getOperationTimeoutMicros());

    do {
        Thread.sleep(2000);
        try {
            if (host.isAuthorizationEnabled()) {
                host.setAuthenticationService(new AuthorizationContextService());
            }
            host.start();
            return true;
        } catch (Throwable e) {
            Logger.getAnonymousLogger().warning(String
                    .format("exception on host restart: %s", e.getMessage()));
            try {
                host.stop();
            } catch (Throwable e1) {
                return false;
            }
            if (e instanceof LockObtainFailedException && !failOnIndexDeletion) {
                Logger.getAnonymousLogger()
                        .warning("Lock held exception on host restart, retrying");
                continue;
            }
            return false;
        }
    } while (Utils.getSystemNowMicrosUtc() < exp);
    return false;
}
 
Example #19
Source File: IndexManager.java    From spacewalk with GNU General Public License v2.0 5 votes vote down vote up
private IndexWriter getIndexWriter(String name, String lang)
        throws CorruptIndexException, LockObtainFailedException,
        IOException {
    String path = indexWorkDir + name;
    File f = new File(path);
    f.mkdirs();
    Analyzer analyzer = getAnalyzer(name, lang);
    IndexWriter writer = new IndexWriter(path, analyzer);
    writer.setUseCompoundFile(true);
    return writer;
}
 
Example #20
Source File: InternalEngine.java    From crate with Apache License 2.0 5 votes vote down vote up
private IndexWriter createWriter() throws IOException {
    try {
        final IndexWriterConfig iwc = getIndexWriterConfig();
        return createWriter(store.directory(), iwc);
    } catch (LockObtainFailedException ex) {
        logger.warn("could not lock IndexWriter", ex);
        throw ex;
    }
}
 
Example #21
Source File: BaseTestCheckIndex.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testObtainsLock(Directory dir) throws IOException {
  IndexWriter iw = new IndexWriter(dir, newIndexWriterConfig(null));
  iw.addDocument(new Document());
  iw.commit();
  
  // keep IW open... should not be able to obtain write lock
  expectThrows(LockObtainFailedException.class, () -> {
    new CheckIndex(dir);
  });
  
  iw.close();
}
 
Example #22
Source File: IndexManager.java    From uyuni with GNU General Public License v2.0 5 votes vote down vote up
private IndexWriter getIndexWriter(String name, String lang)
        throws CorruptIndexException, LockObtainFailedException,
        IOException {
    String path = indexWorkDir + name;
    File f = new File(path);
    f.mkdirs();
    Analyzer analyzer = getAnalyzer(name, lang);
    IndexWriter writer = new IndexWriter(path, analyzer);
    writer.setUseCompoundFile(true);
    return writer;
}
 
Example #23
Source File: RecordOwnerLockFactory.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public boolean obtain(long lockWaitTimeout) throws LockObtainFailedException, IOException {
    try {
        return super.obtain(lockWaitTimeout);
    } catch (LockObtainFailedException e) {
        throw annotateException(
            e,
            (File) null,
            Thread.getAllStackTraces(),
            RecordOwnerLockFactory.this);
    }
}
 
Example #24
Source File: LuceneIndex.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * The writer operates under readLock(!) since we do not want to lock out readers,
 * but just close, clear and commit operations. 
 * 
 * @return
 * @throws IOException 
 */
IndexWriter acquireWriter () throws IOException {
    checkPreconditions();
    hit();
    boolean ok = false;
    rwLock.readLock().lock();
    try {
        try {
            final IndexWriter writer = indexWriterRef.acquire(new Callable<IndexWriter>() {
                @NonNull
                public IndexWriter call() throws IOException {
                    final IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_35, analyzer);
                    //Linux: The posix::fsync(int) is very slow on Linux ext3,
                    //minimize number of files sync is done on.
                    //http://netbeans.org/bugzilla/show_bug.cgi?id=208224
                    //All OS: The CFS is better for SSD disks.
                    final TieredMergePolicy mergePolicy = new TieredMergePolicy();
                    mergePolicy.setNoCFSRatio(1.0);
                    iwc.setMergePolicy(mergePolicy);
                    return new FlushIndexWriter (fsDir, iwc);
                }
            });
            ok = true;
            return writer;
        } catch (LockObtainFailedException lf) {
            //Already annotated
            throw lf;
        } catch (IOException ioe) {
            //Issue #149757 - logging
            throw RecordOwnerLockFactory.annotateException (
                ioe,
                folder,
                null);
        }
    } finally {
        if (!ok) {
            rwLock.readLock().unlock();
        }
    }
}
 
Example #25
Source File: NodeEnvironment.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
void acquire(long timeoutInMillis) throws LockObtainFailedException{
    try {
        if (mutex.tryAcquire(timeoutInMillis, TimeUnit.MILLISECONDS) == false) {
            throw new LockObtainFailedException("Can't lock shard " + shardId + ", timed out after " + timeoutInMillis + "ms");
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new LockObtainFailedException("Can't lock shard " + shardId + ", interrupted", e);
    }
}
 
Example #26
Source File: NodeIndexDeletedAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private void lockIndexAndAck(String index, DiscoveryNodes nodes, String nodeId, ClusterState clusterState, Settings indexSettings) throws IOException {
    try {
        // we are waiting until we can lock the index / all shards on the node and then we ack the delete of the store to the
        // master. If we can't acquire the locks here immediately there might be a shard of this index still holding on to the lock
        // due to a "currently canceled recovery" or so. The shard will delete itself BEFORE the lock is released so it's guaranteed to be
        // deleted by the time we get the lock
        indicesService.processPendingDeletes(new Index(index), indexSettings, new TimeValue(30, TimeUnit.MINUTES));
        transportService.sendRequest(clusterState.nodes().masterNode(),
                INDEX_STORE_DELETED_ACTION_NAME, new NodeIndexStoreDeletedMessage(index, nodeId), EmptyTransportResponseHandler.INSTANCE_SAME);
    } catch (LockObtainFailedException exc) {
        logger.warn("[{}] failed to lock all shards for index - timed out after 30 seconds", index);
    } catch (InterruptedException e) {
        logger.warn("[{}] failed to lock all shards for index - interrupted", index);
    }
}
 
Example #27
Source File: ReadmeSimilarityCalculator.java    From scava with Eclipse Public License 2.0 5 votes vote down vote up
private void createIndex(List<Artifact> prjs) throws LockObtainFailedException, IOException {

		File indexDirectory = new File(luceneIndex);
		org.apache.lucene.store.Directory dir = FSDirectory.open(Paths.get(indexDirectory.getAbsolutePath()));
		Analyzer analyzer = new EnglishAnalyzer(StandardAnalyzer.STOP_WORDS_SET); // using
																					// stop
																					// words
		IndexWriterConfig iwc = new IndexWriterConfig(analyzer);

		if (indexDirectory.exists()) {
			iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
		} else {
			// Add new documents to an existing index:
			iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
		}

		IndexWriter writer = new IndexWriter(dir, iwc);
		for (Artifact project : prjs) {
			Document doc = new Document();
			FieldType fieldType = new FieldType();

			fieldType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
			fieldType.setStored(true);
			fieldType.setStoreTermVectors(true);
			fieldType.setTokenized(true);
			Field contentField = new Field(FIELD_CONTENT, project.getReadmeText(), fieldType);
			doc.add(contentField);
			writer.addDocument(doc);
		}
		

		writer.close();
	}
 
Example #28
Source File: TestIndexWriter.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testWhetherDeleteAllDeletesWriteLock() throws Exception {
  // Must use SimpleFSLockFactory... NativeFSLockFactory
  // somehow "knows" a lock is held against write.lock
  // even if you remove that file:
  Directory d = newFSDirectory(createTempDir("TestIndexWriter.testWhetherDeleteAllDeletesWriteLock"), SimpleFSLockFactory.INSTANCE);
  RandomIndexWriter w1 = new RandomIndexWriter(random(), d);
  w1.deleteAll();
  expectThrows(LockObtainFailedException.class, () -> {
    new RandomIndexWriter(random(), d, newIndexWriterConfig(null));
  });

  w1.close();
  d.close();
}
 
Example #29
Source File: TestIndexWriterWithThreads.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testOpenTwoIndexWritersOnDifferentThreads() throws IOException, InterruptedException {
   try (final Directory dir = newDirectory()) {
     CyclicBarrier syncStart = new CyclicBarrier(2);
     DelayedIndexAndCloseRunnable thread1 = new DelayedIndexAndCloseRunnable(dir, syncStart);
     DelayedIndexAndCloseRunnable thread2 = new DelayedIndexAndCloseRunnable(dir, syncStart);
     thread1.start();
     thread2.start();
     thread1.join();
     thread2.join();

     if (thread1.failure instanceof LockObtainFailedException ||
         thread2.failure instanceof LockObtainFailedException) {
       // We only care about the situation when the two writers succeeded.
       return;
     }

     assertFalse("Failed due to: " + thread1.failure, thread1.failed);
     assertFalse("Failed due to: " + thread2.failure, thread2.failed);

     // now verify that we have two documents in the index
     IndexReader reader = DirectoryReader.open(dir);
     assertEquals("IndexReader should have one document per thread running", 2,
       reader.numDocs());

     reader.close();
   }
}
 
Example #30
Source File: SolrCore.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Returns <code>true</code> iff the index in the named directory is
 * currently locked.
 *
 * @param directory the directory to check for a lock
 * @throws IOException if there is a low-level IO error
 * @deprecated Use of this method can only lead to race conditions. Try
 * to actually obtain a lock instead.
 */
@Deprecated
private static boolean isWriterLocked(Directory directory) throws IOException {
  try {
    directory.obtainLock(IndexWriter.WRITE_LOCK_NAME).close();
    return false;
  } catch (LockObtainFailedException failed) {
    return true;
  }
}