java org.apache.lucene.store.LockObtainFailedException:锁获取超时:
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15470674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
org.apache.lucene.store.LockObtainFailedException: Lock obtain timed out:
提问by Maclean Pinto
I am trying to index a large set of log files obtained from a tomcat server. I have written the code to open each file, create an index for each line and then store each line using Apache lucene. All of this is done using multi-threading.
我正在尝试索引从 tomcat 服务器获取的大量日志文件。我编写了打开每个文件的代码,为每一行创建一个索引,然后使用 Apache lucene 存储每一行。所有这些都是使用多线程完成的。
I get the this exception when i try to this code
当我尝试使用此代码时出现此异常
org.apache.lucene.store.LockObtainFailedException: Lock obtain timed out:
Code
代码
if (indexWriter.getConfig().getOpenMode() == IndexWriterConfig.OpenMode.CREATE)
{
// New index, so we just add the document (no old document can be there):
System.out.println("adding " + path);
indexWriter.addDocument(doc);
} else {
// Existing index (an old copy of this document may have been indexed) so
// we use updateDocument instead to replace the old one matching the exact
// path, if present:
System.out.println("updating " + path);
indexWriter.updateDocument(new Term("path", path), doc);
}
indexWriter.commit();
indexWriter.close();
Now I thought since i am committing the index every time, it might cause a write lock. so i removed indexWriter.commit();
:
现在我想既然我每次都提交索引,它可能会导致写锁。所以我删除了indexWriter.commit();
:
if (indexWriter.getConfig().getOpenMode() == IndexWriterConfig.OpenMode.CREATE)
{
// New index, so we just add the document (no old document can be there):
System.out.println("adding " + path);
indexWriter.addDocument(doc);
} else {
// Existing index (an old copy of this document may have been indexed) so
// we use updateDocument instead to replace the old one matching the exact
// path, if present:
System.out.println("updating " + path);
indexWriter.updateDocument(new Term("path", path), doc);
}
indexWriter.close();
Now i get no exception
现在我也不例外
Q. So my question is why indexWriter.commit(); causes the exception. And even if I remove indexWriter.commit(); i do not get any problem while searching. That is I get the exact result I intended to have. Then why to use indexWriter.commit(); ?
问:所以我的问题是为什么 indexWriter.commit(); 导致异常。即使我删除 indexWriter.commit(); 我在搜索时没有遇到任何问题。那就是我得到了我想要的确切结果。那为什么要使用 indexWriter.commit(); ?
采纳答案by Jayendra
In short, it is similar to the DB commit, unless you commit the transactions, the document add to Solr are just held in Memory. Only on commit would the Document be persisted in the index.
If the Solr crashes when the documents are in memory, you may lose these documents.
简而言之,它类似于DB commit,除非您提交事务,否则添加到Solr的文档只是保存在Memory中。只有在提交时,文档才会被持久化在索引中。
如果当文档在内存中时 Solr 崩溃,您可能会丢失这些文档。
解释:-
One of the principles in Lucene since day one is the write-once policy. We never write a file twice. When you add a document via IndexWriter it gets indexed into the memory and once we have reached a certain threshold (max buffered documents or RAM buffer size) we write all the documents from the main memory to disk; you can find out more about this here and here. Writing documents to disk produces an entire new index called a segment. Now, when you index a bunch of documents or you run incremental indexing in production here you can see the number of segments changing frequently. However, once you call commit Lucene flushes its entire RAM buffer into segments, syncs them and writes pointers to all segments belonging to this commit into the SEGMENTS file.
从第一天起,Lucene 的原则之一就是一次写入策略。我们从不两次写入文件。当您通过 IndexWriter 添加文档时,它会被索引到内存中,一旦我们达到某个阈值(最大缓冲文档或 RAM 缓冲区大小),我们就会将所有文档从主内存写入磁盘;你可以在这里和这里找到更多关于这个的信息。将文档写入磁盘会产生一个全新的索引,称为段。现在,当您索引一堆文档或在生产中运行增量索引时,您可以看到段的数量经常变化。但是,一旦您调用 commit Lucene 会将其整个 RAM 缓冲区刷新到段中,同步它们并将指向属于此提交的所有段的指针写入 SEGMENTS 文件。
If the document already exists in Solr, it would just be overwritten (determined by the unique id).
Hence your search may still work fine, but the latest document is not available for search unless you commit.
如果该文档已存在于 Solr 中,则它只会被覆盖(由唯一 ID 确定)。
因此,您的搜索可能仍然正常工作,但除非您提交,否则无法搜索最新的文档。
Also, once you open and indexwriter it would obtain a lock on the index and you should close the writer to have the lock released.
此外,一旦您打开和 indexwriter 它将获得对索引的锁定,您应该关闭 writer 以释放锁定。