Lucene's multi-thread access principles and synchronization and lock mechanisms

Source: Internet
Author: User

 

This article introduces the usage principles of Lucene in a multi-threaded environment and the lock mechanisms implemented by commit. Lock and write. Lock.

It is designed to serve the multi-threaded environment. In most cases, the index will be accessed by no thread. An index is a key resource. When you access such resources, synchronization access is inevitable. Therefore, a good policy is required to handle these concurrent accesses to ensure reasonable use of resources. Illegal access to the index may result in an index data exception and destroy important data, leading to the failure of the entire system.

The following are the principles of index access:

1. At the same time, a Lucene index allows a process to add, delete, and update a document.

2. At the same time, Lucene indexes allow multiple threads to search for them at the same time.

In Lucene, the classes for modifying indexes are mainly in indexwriter and indexreader. Indexwriter is mainly responsible for writing indexes and maintaining indexes, such as merging and optimization. indexreader is responsible for deleting documents from indexes.

 

According to the first sentence above, the following conclusions can be drawn:

1. At any time, there can be only one indexwriter instance in the system to operate on the index. Multiple indexwriter cannot add document to the index, or optimize the index and merge the index.

2. At any time, there cannot be multiple indexreader to delete the document. The next inderreader should run after the close method is executed in the previous inderreader.

3. Before using indexwriter to add documents to an index, you must disable the indexreader instance that performs the delete operation.

4. Before using indexreader to delete an object, you must close the instance that adds the document operation to indexwriter.

In general, any operation to modify the index cannot occur at the same time; or, if the previous modification operation is not saved, use a new instance to perform the next Modification Operation on the index.

Lucene Lock Mechanism

Lucene uses commit. Lock

Implement the lock mechanism with write. Lock. The so-called lock is actually a file stored in the temporary directory of the system. For example, when creating an index, you can see that a write. Lock is stored in a temporary directory. Another example is that when the index is merged, a commit. Lock is stored in the temporary directory.

 

So when will these locks appear? As mentioned above, Lucene's synchronization problem only occurs when you add, delete, merge segment, and optimize the index, so Lucene locks also basically appear at this time.

1. Write. Lock

Writer. Lock appears when a document is added to an index or deleted from the index. Writer. Lock is created when indexwriter is initialized and then released when the close () method of indexwriter is called. In addition, it is created when indexreader deletes a document using the delete method and released when indexreader's close () method is called.

2. Commit. Lock

Commit. Lock is mainly related to segment merge and read operations. For example, it appears in the initialization of indexwriter, but once the segment information is read, it will be released immediately. In addition, this lock is generated when the addindexs () or mergesegment () method of indexwriter is called.

In fact, because of the two locks, Lucene has the function of protecting indexes against illegal operations.

 

It is recommended that indexwriter be used as the singleton mode and be locked when indexreader is deleted.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.