Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
243 changes: 142 additions & 101 deletions src/Umbraco.Infrastructure/Examine/ExamineIndexRebuilder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Umbraco.

Check notice on line 1 in src/Umbraco.Infrastructure/Examine/ExamineIndexRebuilder.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Code Health Review (main)

✅ No longer an issue: Code Duplication

The module no longer contains too many functions with similar structure

Check notice on line 1 in src/Umbraco.Infrastructure/Examine/ExamineIndexRebuilder.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Code Health Review (main)

✅ Getting better: Primitive Obsession

The ratio of primitive types in function arguments decreases from 44.83% to 42.86%, threshold = 30.0%. The functions in this file have too many primitive types (e.g. int, double, float) in their function argument lists. Using many primitive types lead to the code smell Primitive Obsession. Avoid adding more primitive arguments.
// See LICENSE for more details.

using Examine;
Expand All @@ -6,21 +6,19 @@
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Runtime;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Services.OperationStatus;
using Umbraco.Cms.Core.HostedServices;
using Umbraco.Cms.Infrastructure.Models;
using Umbraco.Extensions;

namespace Umbraco.Cms.Infrastructure.Examine;

internal class ExamineIndexRebuilder : IIndexRebuilder
{
private const string RebuildAllOperationTypeName = "RebuildAllExamineIndexes";

private readonly IBackgroundTaskQueue _backgroundTaskQueue;
private readonly IExamineManager _examineManager;
private readonly ILogger<ExamineIndexRebuilder> _logger;
private readonly IMainDom _mainDom;
private readonly IEnumerable<IIndexPopulator> _populators;
private readonly ILongRunningOperationService _longRunningOperationService;
private readonly object _rebuildLocker = new();
private readonly IRuntimeState _runtimeState;

/// <summary>
Expand All @@ -32,14 +30,14 @@
ILogger<ExamineIndexRebuilder> logger,
IExamineManager examineManager,
IEnumerable<IIndexPopulator> populators,
ILongRunningOperationService longRunningOperationService)
IBackgroundTaskQueue backgroundTaskQueue)
{
_mainDom = mainDom;
_runtimeState = runtimeState;
_logger = logger;
_examineManager = examineManager;
_populators = populators;
_longRunningOperationService = longRunningOperationService;
_backgroundTaskQueue = backgroundTaskQueue;
}

/// <inheritdoc/>
Expand All @@ -50,161 +48,204 @@
throw new InvalidOperationException("No index found by name " + indexName);
}

return HasRegisteredPopulator(index);
return _populators.Any(x => x.IsRegistered(index));
}

/// <inheritdoc/>
[Obsolete("Use RebuildIndexAsync() instead. Scheduled for removal in Umbraco 19.")]
public virtual void RebuildIndex(string indexName, TimeSpan? delay = null, bool useBackgroundThread = true)
=> RebuildIndexAsync(indexName, delay, useBackgroundThread).GetAwaiter().GetResult();

/// <inheritdoc/>
public virtual async Task<Attempt<IndexRebuildResult>> RebuildIndexAsync(string indexName, TimeSpan? delay = null, bool useBackgroundThread = true)
{
delay ??= TimeSpan.Zero;
if (delay == null)
{
delay = TimeSpan.Zero;
}

if (!CanRun())
{
return Attempt.Fail(IndexRebuildResult.NotAllowedToRun);
return;
}

Attempt<Guid, LongRunningOperationEnqueueStatus> attempt = await _longRunningOperationService.RunAsync(
GetRebuildOperationTypeName(indexName),
async ct =>
{
await RebuildIndex(indexName, delay.Value, ct);
return Task.CompletedTask;
},
allowConcurrentExecution: false,
runInBackground: useBackgroundThread);

if (attempt.Success)
if (useBackgroundThread)
{
return Attempt.Succeed(IndexRebuildResult.Success);
_logger.LogInformation("Starting async background thread for rebuilding index {indexName}.", indexName);

_backgroundTaskQueue.QueueBackgroundWorkItem(
cancellationToken =>
{
// Do not flow AsyncLocal to the child thread
using (ExecutionContext.SuppressFlow())
{
Task.Run(() => RebuildIndex(indexName, delay.Value, cancellationToken));

// immediately return so the queue isn't waiting.
return Task.CompletedTask;
}
});
}

return attempt.Status switch
else
{
LongRunningOperationEnqueueStatus.AlreadyRunning => Attempt.Fail(IndexRebuildResult.AlreadyRebuilding),
_ => Attempt.Fail(IndexRebuildResult.Unknown),
};
RebuildIndex(indexName, delay.Value, CancellationToken.None);
}
}

/// <inheritdoc/>
[Obsolete("Use RebuildIndexesAsync() instead. Scheduled for removal in Umbraco 19.")]
public virtual void RebuildIndexes(bool onlyEmptyIndexes, TimeSpan? delay = null, bool useBackgroundThread = true)
=> RebuildIndexesAsync(onlyEmptyIndexes, delay, useBackgroundThread).GetAwaiter().GetResult();
public virtual Task<Attempt<IndexRebuildResult>> RebuildIndexAsync(string indexName, TimeSpan? delay = null, bool useBackgroundThread = true)
{
RebuildIndex(indexName, delay, useBackgroundThread);
return Task.FromResult(Attempt.Succeed(IndexRebuildResult.Success));
}

/// <inheritdoc/>
public virtual async Task<Attempt<IndexRebuildResult>> RebuildIndexesAsync(bool onlyEmptyIndexes, TimeSpan? delay = null, bool useBackgroundThread = true)
[Obsolete("Use RebuildIndexesAsync() instead. Scheduled for removal in Umbraco 19.")]
public virtual void RebuildIndexes(bool onlyEmptyIndexes, TimeSpan? delay = null, bool useBackgroundThread = true)
{
delay ??= TimeSpan.Zero;
if (delay == null)
{
delay = TimeSpan.Zero;
}

if (!CanRun())
{
return Attempt.Fail(IndexRebuildResult.NotAllowedToRun);
return;
}

Attempt<Guid, LongRunningOperationEnqueueStatus> attempt = await _longRunningOperationService.RunAsync(
RebuildAllOperationTypeName,
async ct =>
if (useBackgroundThread)
{
if (_logger.IsEnabled(Microsoft.Extensions.Logging.LogLevel.Debug))
{
await RebuildIndexes(onlyEmptyIndexes, delay.Value, ct);
return Task.CompletedTask;
},
allowConcurrentExecution: false,
runInBackground: useBackgroundThread);
_logger.LogDebug($"Queuing background job for {nameof(RebuildIndexes)}.");
}

if (attempt.Success)
{
return Attempt.Succeed(IndexRebuildResult.Success);
_backgroundTaskQueue.QueueBackgroundWorkItem(
cancellationToken =>
{
// Do not flow AsyncLocal to the child thread
using (ExecutionContext.SuppressFlow())
{
// This is a fire/forget task spawned by the background thread queue (which means we
// don't need to worry about ExecutionContext flowing).
Task.Run(() => RebuildIndexes(onlyEmptyIndexes, delay.Value, cancellationToken));

// immediately return so the queue isn't waiting.
return Task.CompletedTask;
}
});
}

return attempt.Status switch
else
{
LongRunningOperationEnqueueStatus.AlreadyRunning => Attempt.Fail(IndexRebuildResult.AlreadyRebuilding),
_ => Attempt.Fail(IndexRebuildResult.Unknown),
};
RebuildIndexes(onlyEmptyIndexes, delay.Value, CancellationToken.None);
}
}

/// <inheritdoc/>
public async Task<bool> IsRebuildingAsync(string indexName)
=> (await _longRunningOperationService.GetByTypeAsync(GetRebuildOperationTypeName(indexName), 0, 0)).Total != 0;

private static string GetRebuildOperationTypeName(string indexName)
public virtual Task<Attempt<IndexRebuildResult>> RebuildIndexesAsync(bool onlyEmptyIndexes, TimeSpan? delay = null, bool useBackgroundThread = true)
{
// Truncate to a maximum of 200 characters to ensure the type name doesn't overflow the database field.
const int TypeFieldSize = 200;
return $"RebuildExamineIndex-{indexName}".TruncateWithUniqueHash(TypeFieldSize);
RebuildIndexes(onlyEmptyIndexes, delay, useBackgroundThread);
return Task.FromResult(Attempt.Succeed(IndexRebuildResult.Success));
}

/// <inheritdoc/>
public Task<bool> IsRebuildingAsync(string indexName) => Task.FromResult(false);

private bool CanRun() => _mainDom.IsMainDom && _runtimeState.Level == RuntimeLevel.Run;

private async Task RebuildIndex(string indexName, TimeSpan delay, CancellationToken cancellationToken)
private void RebuildIndex(string indexName, TimeSpan delay, CancellationToken cancellationToken)
{
if (delay > TimeSpan.Zero)
{
await Task.Delay(delay, cancellationToken);
Thread.Sleep(delay);
}

if (!_examineManager.TryGetIndex(indexName, out IIndex index))
try
{
throw new InvalidOperationException($"No index found with name {indexName}");
if (!Monitor.TryEnter(_rebuildLocker))
{
_logger.LogWarning("Call was made to RebuildIndexes but the task runner for rebuilding is already running");
}
else
{
if (!_examineManager.TryGetIndex(indexName, out IIndex index))
{
throw new InvalidOperationException($"No index found with name {indexName}");
}

index.CreateIndex(); // clear the index
foreach (IIndexPopulator populator in _populators)
{
if (cancellationToken.IsCancellationRequested)
{
return;
}

populator.Populate(index);
}
}
}

index.CreateIndex(); // clear the index
foreach (IIndexPopulator populator in _populators)
finally
{
if (cancellationToken.IsCancellationRequested)
if (Monitor.IsEntered(_rebuildLocker))
{
return;
Monitor.Exit(_rebuildLocker);
}

Check warning on line 189 in src/Umbraco.Infrastructure/Examine/ExamineIndexRebuilder.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Code Health Review (main)

❌ New issue: Bumpy Road Ahead

RebuildIndex has 2 blocks with nested conditional logic. Any nesting of 2 or deeper is considered. Threshold is 2 blocks per function. The Bumpy Road code smell is a function that contains multiple chunks of nested conditional logic. The deeper the nesting and the more bumps, the lower the code health.

populator.Populate(index);
}
}

private async Task RebuildIndexes(bool onlyEmptyIndexes, TimeSpan delay, CancellationToken cancellationToken)
private void RebuildIndexes(bool onlyEmptyIndexes, TimeSpan delay, CancellationToken cancellationToken)
{
if (delay > TimeSpan.Zero)
{
await Task.Delay(delay, cancellationToken);
Thread.Sleep(delay);
}

// If an index exists but it has zero docs we'll consider it empty and rebuild
// Only include indexes that have at least one populator registered, this is to avoid emptying out indexes
// that we have no chance of repopulating, for example our own search package
IIndex[] indexes = (onlyEmptyIndexes
? _examineManager.Indexes.Where(ShouldRebuild)
: _examineManager.Indexes)
.Where(HasRegisteredPopulator)
.ToArray();

if (indexes.Length == 0)
{
return;
}

foreach (IIndex index in indexes)
{
index.CreateIndex(); // clear the index
}

// run each populator over the indexes
foreach (IIndexPopulator populator in _populators)
try
{
if (cancellationToken.IsCancellationRequested)
if (!Monitor.TryEnter(_rebuildLocker))
{
return;
_logger.LogWarning($"Call was made to {nameof(RebuildIndexes)} but the task runner for rebuilding is already running");
}

try
else
{
populator.Populate(indexes);
// If an index exists but it has zero docs we'll consider it empty and rebuild
IIndex[] indexes = (onlyEmptyIndexes
? _examineManager.Indexes.Where(ShouldRebuild)
: _examineManager.Indexes)
.Where(HasRegisteredPopulator)
.ToArray();

if (indexes.Length == 0)
{
return;
}

foreach (IIndex index in indexes)
{
index.CreateIndex(); // clear the index
}

// run each populator over the indexes
foreach (IIndexPopulator populator in _populators)
{
if (cancellationToken.IsCancellationRequested)
{
return;
}

try
{
populator.Populate(indexes);
}
catch (Exception e)
{
_logger.LogError(e, "Index populating failed for populator {Populator}", populator.GetType());
}
}
}
catch (Exception e)
}
finally
{
if (Monitor.IsEntered(_rebuildLocker))
{
_logger.LogError(e, "Index populating failed for populator {Populator}", populator.GetType());
Monitor.Exit(_rebuildLocker);

Check warning on line 248 in src/Umbraco.Infrastructure/Examine/ExamineIndexRebuilder.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Code Health Review (main)

❌ New issue: Complex Method

RebuildIndexes has a cyclomatic complexity of 10, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.

Check warning on line 248 in src/Umbraco.Infrastructure/Examine/ExamineIndexRebuilder.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Code Health Review (main)

❌ New issue: Bumpy Road Ahead

RebuildIndexes has 2 blocks with nested conditional logic. Any nesting of 2 or deeper is considered. Threshold is 2 blocks per function. The Bumpy Road code smell is a function that contains multiple chunks of nested conditional logic. The deeper the nesting and the more bumps, the lower the code health.
}
}
}
Expand Down
Loading
Loading