-
Notifications
You must be signed in to change notification settings - Fork 2
Abstract away the Examine commit monitoring #100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| namespace Umbraco.Cms.Search.Provider.Examine.Services; | ||
|
|
||
| public interface IIndexCommitMonitor | ||
| { | ||
| Task<bool> WaitForCommitAsync(string indexAlias, CancellationToken cancellationToken); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| using Examine; | ||
| using Examine.Lucene.Providers; | ||
|
|
||
| namespace Umbraco.Cms.Search.Provider.Examine.Services; | ||
|
|
||
| internal sealed class IndexCommitMonitor : IIndexCommitMonitor | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From what I can tell, this i sort of what is proposed here umbraco/Umbraco-CMS#16546 which links on to here Shazwazza/Examine#372 (comment) Using the event can be problematic, but depends on the use case and outcome. If this is to just wait for a single commit than its probably ok but if its to wait for a rebuild, then the strategy mentioned in the links would be far better where a final special document/record is indexed after the populators run to indicate that rebuild is complete. |
||
| { | ||
| private static readonly TimeSpan CommitTimeout = TimeSpan.FromSeconds(30); | ||
|
|
||
| private readonly IExamineManager _examineManager; | ||
|
|
||
| public IndexCommitMonitor(IExamineManager examineManager) | ||
| => _examineManager = examineManager; | ||
|
|
||
| public async Task<bool> WaitForCommitAsync(string indexAlias, CancellationToken cancellationToken) | ||
| { | ||
| if (_examineManager.TryGetIndex(indexAlias, out IIndex? index) is false || index is not LuceneIndex luceneIndex) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| if (index is IIndexStats stats && stats.GetDocumentCount() > 0) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| var committed = false; | ||
| EventHandler onCommitted = (_, _) => committed = true; | ||
|
|
||
| try | ||
| { | ||
| luceneIndex.IndexCommitted += onCommitted; | ||
|
|
||
| // Re-check after subscribing to avoid a race where the commit happened | ||
| // between the initial check and subscribing to the event. | ||
| if (index is IIndexStats statsAfterSubscribe && statsAfterSubscribe.GetDocumentCount() > 0) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| var stopwatch = System.Diagnostics.Stopwatch.StartNew(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This never gets stopped? |
||
| while (!committed && stopwatch.Elapsed < CommitTimeout) | ||
| { | ||
| await Task.Delay(CommitTimeout); | ||
| } | ||
|
|
||
| return committed; | ||
| } | ||
| finally | ||
| { | ||
| luceneIndex.IndexCommitted -= onCommitted; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,7 +20,7 @@ public static IUmbracoBuilder AddExamineSearchProviderForTest<TIndex, TDirectory | |
| builder.AddNotificationHandler<MemberSavedNotification, MemberSavedDistributedCacheNotificationHandler>(); | ||
| builder.AddNotificationHandler<MemberDeletedNotification, MemberDeletedDistributedCacheNotificationHandler>(); | ||
| builder.AddNotificationHandler<IndexRebuildStartingNotification, ZeroDowntimeRebuildNotificationHandler>(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the expectation that the ZeroDowntimeRebuildNotificationHandler is only for Lucene indexes or for any Examine based index? |
||
| builder.AddNotificationHandler<IndexRebuildCompletedNotification, ZeroDowntimeRebuildNotificationHandler>(); | ||
| builder.AddNotificationAsyncHandler<IndexRebuildCompletedNotification, ZeroDowntimeRebuildNotificationHandler>(); | ||
|
|
||
| return builder; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens when this occurs? is the expectation that maybe the shadow index will still be healthy? Or that the committing/monitor has failed and this needs to exit?