From bef6d34c0e9c3899c7ace25e82aa6683fc907a36 Mon Sep 17 00:00:00 2001 From: "EP\\hunng" Date: Fri, 6 May 2022 15:01:12 +0700 Subject: [PATCH] Fix media data indexed fail Fixes: CMS-21661 Story: CMS-19870 --- .../Helpers/DocumentHelper.cs | 11 +++++------ .../Helpers/LuceneHelper.cs | 6 +++--- .../IndexingServiceSettings.cs | 10 ++++++++++ .../Helpers/DocumentHelper/DocumentHelperTestBase.cs | 3 +++ .../Helpers/LuceneHelper/LuceneHelperTestBase.cs | 8 +++++++- 5 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/EPiServer.Search.IndexingService/Helpers/DocumentHelper.cs b/src/EPiServer.Search.IndexingService/Helpers/DocumentHelper.cs index 70c81d7..e6f95a9 100644 --- a/src/EPiServer.Search.IndexingService/Helpers/DocumentHelper.cs +++ b/src/EPiServer.Search.IndexingService/Helpers/DocumentHelper.cs @@ -24,7 +24,7 @@ public Collection SingleIndexSearch(string q, NamedIndex namedInd { var scoreDocuments = new Collection(); totalHits = 0; - var rwl = new ReaderWriterLockSlim(); + var rwl = IndexingServiceSettings.ReaderWriterLocks[namedIndex.Name]; rwl.EnterReadLock(); try @@ -58,7 +58,7 @@ public Collection SingleIndexSearch(string q, NamedIndex namedInd } public void OptimizeIndex(NamedIndex namedIndex) { - var rwl = new ReaderWriterLockSlim(); + var rwl = IndexingServiceSettings.ReaderWriterLocks[namedIndex.Name]; rwl.EnterWriteLock(); @@ -103,7 +103,7 @@ public Collection MultiIndexSearch(string q, Collection GetScoreDocuments(string q, bool excludeNotPubl public bool DeleteFromIndex(NamedIndex namedIndex, string itemId, bool deleteRef) { - var rwl = new ReaderWriterLockSlim(); + var rwl = IndexingServiceSettings.ReaderWriterLocks[namedIndex.Name]; var isOk = true; Term term = null; @@ -453,7 +453,7 @@ public bool DeleteFromIndex(NamedIndex namedIndex, string itemId, bool deleteRef { IndexingServiceSettings.IndexingServiceServiceLog.LogDebug(string.Format("Start deleting reference documents for id '{0}'", itemId.ToString())); - var rwlRef = new ReaderWriterLockSlim(); + var rwlRef = IndexingServiceSettings.ReaderWriterLocks[namedIndex.ReferenceName]; rwlRef.EnterWriteLock(); try diff --git a/src/EPiServer.Search.IndexingService/IndexingServiceSettings.cs b/src/EPiServer.Search.IndexingService/IndexingServiceSettings.cs index a02d6d4..5c2b77b 100644 --- a/src/EPiServer.Search.IndexingService/IndexingServiceSettings.cs +++ b/src/EPiServer.Search.IndexingService/IndexingServiceSettings.cs @@ -1,5 +1,7 @@ using System; +using System.Collections.Concurrent; using System.Collections.Generic; +using System.Threading; using EPiServer.Search.IndexingService.Configuration; using EPiServer.Search.IndexingService.Controllers; using EPiServer.Search.IndexingService.Helpers; @@ -194,6 +196,11 @@ public static Analyzer Analyzer } } + /// + /// Gets ReaderWriterLocks for named indexes + /// + public static ConcurrentDictionary ReaderWriterLocks { get; } = new ConcurrentDictionary(); + /// /// Gets named indexes config elements /// @@ -278,6 +285,9 @@ private void LoadIndexes() var directoryMain = new System.IO.DirectoryInfo(System.IO.Path.Combine(GetDirectoryPath(e.DirectoryPath), "Main")); var directoryRef = new System.IO.DirectoryInfo(System.IO.Path.Combine(GetDirectoryPath(e.DirectoryPath), "Ref")); + ReaderWriterLocks.TryAdd(e.Name, new ReaderWriterLockSlim()); + ReaderWriterLocks.TryAdd(e.Name + RefIndexSuffix, new ReaderWriterLockSlim()); + try { if (!directoryMain.Exists) diff --git a/test/EPiServer.Search.IndexingService.Test/Helpers/DocumentHelper/DocumentHelperTestBase.cs b/test/EPiServer.Search.IndexingService.Test/Helpers/DocumentHelper/DocumentHelperTestBase.cs index 01cfec2..50f518b 100644 --- a/test/EPiServer.Search.IndexingService.Test/Helpers/DocumentHelper/DocumentHelperTestBase.cs +++ b/test/EPiServer.Search.IndexingService.Test/Helpers/DocumentHelper/DocumentHelperTestBase.cs @@ -1,5 +1,6 @@ using System; using System.Collections.ObjectModel; +using System.Threading; using EPiServer.Search.IndexingService.Helpers; using Microsoft.Extensions.Logging; using Moq; @@ -11,6 +12,8 @@ public class DocumentHelperTestBase protected readonly Mock _responseExceptionHelperMock; public DocumentHelperTestBase() { + IndexingServiceSettings.ReaderWriterLocks.TryAdd("testindex1", new ReaderWriterLockSlim()); + IndexingServiceSettings.ReaderWriterLocks.TryAdd("testindex2", new ReaderWriterLockSlim()); _responseExceptionHelperMock = new Mock(); var logMock = new Mock(); diff --git a/test/EPiServer.Search.IndexingService.Test/Helpers/LuceneHelper/LuceneHelperTestBase.cs b/test/EPiServer.Search.IndexingService.Test/Helpers/LuceneHelper/LuceneHelperTestBase.cs index afa642c..58b9b97 100644 --- a/test/EPiServer.Search.IndexingService.Test/Helpers/LuceneHelper/LuceneHelperTestBase.cs +++ b/test/EPiServer.Search.IndexingService.Test/Helpers/LuceneHelper/LuceneHelperTestBase.cs @@ -1,4 +1,6 @@ -using EPiServer.Search.IndexingService.Helpers; +using System; +using System.Threading; +using EPiServer.Search.IndexingService.Helpers; using Moq; namespace EPiServer.Search.IndexingService.Test.Helpers.LuceneHelper @@ -11,6 +13,10 @@ public class LuceneHelperTestBase protected readonly Mock _documentHelperMock; public LuceneHelperTestBase() { + IndexingServiceSettings.ReaderWriterLocks.TryAdd("testindex1", new ReaderWriterLockSlim()); + IndexingServiceSettings.ReaderWriterLocks.TryAdd("testindex1" + IndexingServiceSettings.RefIndexSuffix, new ReaderWriterLockSlim()); + IndexingServiceSettings.ReaderWriterLocks.TryAdd("testindex2", new ReaderWriterLockSlim()); + _feedHelperMock = new Mock(); _commonFuncMock = new Mock(); _responseExceptionHelperMock = new Mock();