Skip to content

Commit 87eac0e

Browse files
juanluisrpjosegar74
authored andcommitted
Fix deadlock threads while indexing (#2740)
* wait call must be inside a synchronized block. In other cases it throws IllegalMonitorStateException. * Use a specific lock to synchronize the modification of indexing set. Port of PR #2548 to 3.4.x branch.
1 parent 523815e commit 87eac0e

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

core/src/main/java/org/fao/geonet/kernel/DataManager.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@
200200
public class DataManager implements ApplicationEventPublisherAware {
201201

202202
private static final int METADATA_BATCH_PAGE_SIZE = 100000;
203-
Lock indexLock = new ReentrantLock();
203+
Lock waitLoopLock = new ReentrantLock();
204+
Lock indexingLock = new ReentrantLock();
204205
Set<String> waitForIndexing = new HashSet<String>();
205206
Set<String> indexing = new HashSet<String>();
206207
Set<IndexMetadataTask> batchIndex = new ConcurrentHashSet<IndexMetadataTask>();
@@ -595,11 +596,11 @@ public void batchIndexInThreadPool(ServiceContext context, List<?> metadataIds)
595596
}
596597

597598
public boolean isIndexing() {
598-
indexLock.lock();
599+
indexingLock.lock();
599600
try {
600601
return !indexing.isEmpty() || !batchIndex.isEmpty();
601602
} finally {
602-
indexLock.unlock();
603+
indexingLock.unlock();
603604
}
604605
}
605606

@@ -622,7 +623,7 @@ public void indexMetadata(final List<String> metadataIds) throws Exception {
622623
* TODO javadoc.
623624
*/
624625
public void indexMetadata(final String metadataId, boolean forceRefreshReaders, ISearchManager searchManager) throws Exception {
625-
indexLock.lock();
626+
waitLoopLock.lock();
626627
try {
627628
if (waitForIndexing.contains(metadataId)) {
628629
return;
@@ -631,16 +632,23 @@ public void indexMetadata(final String metadataId, boolean forceRefreshReaders,
631632
try {
632633
waitForIndexing.add(metadataId);
633634
// don't index the same metadata 2x
634-
wait(200);
635+
synchronized (this) {
636+
wait(200);
637+
}
635638
} catch (InterruptedException e) {
636639
return;
637640
} finally {
638641
waitForIndexing.remove(metadataId);
639642
}
640643
}
641-
indexing.add(metadataId);
644+
indexingLock.lock();
645+
try {
646+
indexing.add(metadataId);
647+
} finally {
648+
indexingLock.unlock();
649+
}
642650
} finally {
643-
indexLock.unlock();
651+
waitLoopLock.unlock();
644652
}
645653
Metadata fullMd;
646654

@@ -844,11 +852,13 @@ public void indexMetadata(final String metadataId, boolean forceRefreshReaders,
844852
Log.error(Geonet.DATA_MANAGER, "The metadata document index with id=" + metadataId + " is corrupt/invalid - ignoring it. Error: " + x.getMessage(), x);
845853
fullMd = null;
846854
} finally {
847-
indexLock.lock();
855+
indexingLock.lock();
848856
try {
849857
indexing.remove(metadataId);
858+
} catch (Exception e) {
859+
Log.warning(Geonet.INDEX_ENGINE, "Exception removing " + metadataId + " from indexing set", e);
850860
} finally {
851-
indexLock.unlock();
861+
indexingLock.unlock();
852862
}
853863
}
854864
if (fullMd != null) {

0 commit comments

Comments
 (0)