Improved the pauses between indexing steps and between files of the Batch Processing Assistant (#1240)

This commit is contained in:
Thorsten Sommer authored and GitHub committed 2026-11-27 14:39:17 +01:00
1 parent a117074972
commit 899139c88b
32 files changed
+787 -425

No files matched your search

@@ -1,7 +1,7 @@
namespace AIStudio.Tools.Services.Indexing;
namespace AIStudio.Tools.Pauses;
/// <summary>
/// How long the indexing step which is about to start waits, cf. PauseBetweenSteps.GetPauseBeforeNextStep.
/// How long the step which is about to start waits, cf. PauseBetweenSteps.GetPauseBeforeNextStep.
/// </summary>
/// <param name="Pause">The pause drawn for the gap since the last step.</param>
/// <param name="Remaining">What is left of the pause now. More than zero, unless this is NONE.</param>
@@ -1,24 +1,24 @@
namespace AIStudio.Tools.Services.Indexing;
namespace AIStudio.Tools.Pauses;
/// <summary>
/// The pause between two indexing steps, which spares an embedding server that many people share.
/// The pause between two steps, e.g., two indexing steps, which spares a server that many people share.
/// </summary>
/// <remarks>
/// Not to be confused with the pauses of the indexing as a whole, e.g., while a backup copies the
/// indexes, cf. IndexingPauses.<br/><br/>
///
/// The pause is a gap between the end of one step and the start of the next, whichever data source
/// either of them belongs to: it is about the load on the embedding server, not about the order of
/// the data sources. Only a step which worked on documents opens a gap. Its pause is drawn once the
/// next step is about to start, from the bounds which apply then, and the time which passed in the
/// meantime counts toward it. When no step comes, nothing is drawn.
/// The pause is a gap between the end of one step and the start of the next, whatever either of
/// them works on: it is about the load on the server, not about the order of the work, e.g., of the
/// data sources of the indexing. Only a step which caused load opens a gap. Its pause is drawn once
/// the next step is about to start, from the bounds which apply then, and the time which passed in
/// the meantime counts toward it. When no step comes, nothing is drawn.
/// </remarks>
internal sealed class PauseBetweenSteps
{
private readonly Lock stateLock = new();
/// <summary>
/// When the last step which worked on documents ended. Null before the first one.
/// When the last step which caused load ended. Null before the first one.
/// </summary>
private DateTimeOffset? lastStepEndedUtc;
@@ -28,7 +28,7 @@ internal sealed class PauseBetweenSteps
private TimeSpan? drawnPause;
/// <summary>
/// Notes that a step which worked on documents ended, which opens a new gap.
/// Notes that a step which caused load ended, e.g., one which worked on documents, which opens a new gap.
/// </summary>
/// <param name="nowUtc">The current time, passed in so the pause can be tested.</param>
public void RecordStepEnded(DateTimeOffset nowUtc)
@@ -0,0 +1,27 @@
namespace AIStudio.Tools.Pauses;
/// <summary>
/// The shortest and the longest pause a feature allows for either bound of its random pause, cf. PauseRange.
/// </summary>
/// <remarks>
/// Each feature sets its own limits next to its settings: the indexing allows no pause at all, for
/// example, while the batch processing always pauses for a few seconds.
/// </remarks>
/// <param name="ShortestSeconds">The shortest pause either bound may ask for.</param>
/// <param name="LongestSeconds">The longest pause either bound may ask for.</param>
public sealed record PauseLimits(int ShortestSeconds, int LongestSeconds)
{
/// <summary>
/// Whether a bound lies within the limits, e.g., one which a configuration plugin sets.
/// </summary>
/// <param name="seconds">The bound.</param>
/// <returns>True when the feature allows the bound.</returns>
public bool Allows(int seconds) => seconds >= this.ShortestSeconds && seconds <= this.LongestSeconds;
/// <summary>
/// Moves a bound into the limits.
/// </summary>
/// <param name="seconds">The bound.</param>
/// <returns>The bound, or the limit it went beyond.</returns>
public int Clamp(int seconds) => Math.Clamp(seconds, this.ShortestSeconds, this.LongestSeconds);
}
@@ -0,0 +1,50 @@
namespace AIStudio.Tools.Pauses;
/// <summary>
/// The bounds of a random pause, e.g., between two indexing steps, cf. PauseBetweenSteps.
/// </summary>
/// <param name="MinimumSeconds">The shortest pause. Between the shortest limit of the feature and MaximumSeconds.</param>
/// <param name="MaximumSeconds">The longest pause, which may be drawn as well. At most the longest limit of the feature.</param>
public sealed record PauseRange(int MinimumSeconds, int MaximumSeconds)
{
/// <summary>
/// No pause at all, which is the default of the indexing.
/// </summary>
public static readonly PauseRange NONE = new(0, 0);
/// <summary>
/// Whether the next step starts right away.
/// </summary>
public bool IsNone => this.MaximumSeconds is 0;
/// <summary>
/// Works out the bounds which apply, so that the lower one never exceeds the upper one.
/// </summary>
/// <remarks>
/// Both bounds are moved into the limits of the feature first. A bound which a configuration plugin
/// locks wins, and the other one adjusts to it. A bound the plugin only presets counts like one the
/// user set. Otherwise, the lower bound wins, i.e., the longer pause: it spares the server, which
/// is what the pause is for. That holds as well when a plugin locks both bounds the wrong way round.<br/><br/>
///
/// Which bound is locked matters only while the bounds are the wrong way round. Asking reads the
/// managed state of the whole app, which bounds in order do without, so the locks are asked only then.
/// </remarks>
/// <param name="limits">The shortest and the longest pause the feature allows.</param>
/// <param name="minimumSeconds">The lower bound as set.</param>
/// <param name="isMinimumLocked">Whether a configuration plugin locks the lower bound.</param>
/// <param name="maximumSeconds">The upper bound as set.</param>
/// <param name="isMaximumLocked">Whether a configuration plugin locks the upper bound.</param>
/// <returns>The bounds which apply.</returns>
public static PauseRange GetEffective(PauseLimits limits, int minimumSeconds, Func<bool> isMinimumLocked, int maximumSeconds, Func<bool> isMaximumLocked)
{
var minimum = limits.Clamp(minimumSeconds);
var maximum = limits.Clamp(maximumSeconds);
if (minimum <= maximum)
return new(minimum, maximum);
if (isMaximumLocked() && !isMinimumLocked())
return new(maximum, maximum);
return new(minimum, minimum);
}
}
@@ -398,13 +398,13 @@ public sealed class PluginConfiguration(bool isInternal, LuaState state, PluginT
ManagedConfiguration.TryProcessConfiguration(x => x.MailboxSettings, x => x.AllowOnlyOrganizationMailServers, this.Id, settingsTable, dryRun);
// Config: the random pause between two indexing steps of local data sources
var minimumPauseIsValid = ManagedConfiguration.TryProcessConfiguration(x => x.App.DataSourceIndexing, x => x.MinimumPauseBetweenStepsSeconds, this.Id, settingsTable, dryRun, validator: value => value is >= 0 and <= DataDataSourceIndexing.MAX_PAUSE_BETWEEN_STEPS_SECONDS);
var minimumPauseIsValid = ManagedConfiguration.TryProcessConfiguration(x => x.App.DataSourceIndexing, x => x.MinimumPauseBetweenStepsSeconds, this.Id, settingsTable, dryRun, validator: DataDataSourceIndexing.PAUSE_LIMITS.Allows);
if (!minimumPauseIsValid && settingsTable.TryGetValue("DataDataSourceIndexing.MinimumPauseBetweenStepsSeconds", out _))
LOG.LogWarning("The lower bound of the pause between indexing steps configured by plugin {ConfigPluginId} must be between 0 and {MaximumPauseSeconds} seconds.", this.Id, DataDataSourceIndexing.MAX_PAUSE_BETWEEN_STEPS_SECONDS);
LOG.LogWarning("The lower bound of the pause between indexing steps configured by plugin {ConfigPluginId} must be between {ShortestPauseSeconds} and {LongestPauseSeconds} seconds.", this.Id, DataDataSourceIndexing.PAUSE_LIMITS.ShortestSeconds, DataDataSourceIndexing.PAUSE_LIMITS.LongestSeconds);
var maximumPauseIsValid = ManagedConfiguration.TryProcessConfiguration(x => x.App.DataSourceIndexing, x => x.MaximumPauseBetweenStepsSeconds, this.Id, settingsTable, dryRun, validator: value => value is >= 0 and <= DataDataSourceIndexing.MAX_PAUSE_BETWEEN_STEPS_SECONDS);
var maximumPauseIsValid = ManagedConfiguration.TryProcessConfiguration(x => x.App.DataSourceIndexing, x => x.MaximumPauseBetweenStepsSeconds, this.Id, settingsTable, dryRun, validator: DataDataSourceIndexing.PAUSE_LIMITS.Allows);
if (!maximumPauseIsValid && settingsTable.TryGetValue("DataDataSourceIndexing.MaximumPauseBetweenStepsSeconds", out _))
LOG.LogWarning("The upper bound of the pause between indexing steps configured by plugin {ConfigPluginId} must be between 0 and {MaximumPauseSeconds} seconds.", this.Id, DataDataSourceIndexing.MAX_PAUSE_BETWEEN_STEPS_SECONDS);
LOG.LogWarning("The upper bound of the pause between indexing steps configured by plugin {ConfigPluginId} must be between {ShortestPauseSeconds} and {LongestPauseSeconds} seconds.", this.Id, DataDataSourceIndexing.PAUSE_LIMITS.ShortestSeconds, DataDataSourceIndexing.PAUSE_LIMITS.LongestSeconds);
if (minimumPauseIsValid && maximumPauseIsValid
&& settingsTable.TryGetValue("DataDataSourceIndexing.MinimumPauseBetweenStepsSeconds", out var minimumPauseValue) && minimumPauseValue.TryRead<int>(out var minimumPauseSeconds)
@@ -519,9 +519,20 @@ public sealed class PluginConfiguration(bool isInternal, LuaState state, PluginT
ManagedConfiguration.TryProcessConfiguration(x => x.BatchProcessing, x => x.CsvSeparator, this.Id, settingsTable, dryRun);
ManagedConfiguration.TryProcessConfiguration(x => x.BatchProcessing, x => x.CustomCsvSeparator, this.Id, settingsTable, dryRun);
var minimumDelayIsValid = ManagedConfiguration.TryProcessConfiguration(x => x.BatchProcessing, x => x.MinimumDelaySeconds, this.Id, settingsTable, dryRun, validator: value => value is >= DataBatchProcessing.MIN_DELAY_SECONDS and <= DataBatchProcessing.MAX_DELAY_SECONDS);
// Config: the random pause between two files of the Batch Processing Assistant
var minimumDelayIsValid = ManagedConfiguration.TryProcessConfiguration(x => x.BatchProcessing, x => x.MinimumDelaySeconds, this.Id, settingsTable, dryRun, validator: DataBatchProcessing.PAUSE_LIMITS.Allows);
if (!minimumDelayIsValid && settingsTable.TryGetValue("DataBatchProcessing.MinimumDelaySeconds", out _))
LOG.LogWarning("The Batch Processing minimum delay configured by plugin {ConfigPluginId} must be between {MinimumDelaySeconds} and {MaximumDelaySeconds} seconds.", this.Id, DataBatchProcessing.MIN_DELAY_SECONDS, DataBatchProcessing.MAX_DELAY_SECONDS);
LOG.LogWarning("The Batch Processing minimum delay configured by plugin {ConfigPluginId} must be between {MinimumDelaySeconds} and {MaximumDelaySeconds} seconds.", this.Id, DataBatchProcessing.PAUSE_LIMITS.ShortestSeconds, DataBatchProcessing.PAUSE_LIMITS.LongestSeconds);
var maximumDelayIsValid = ManagedConfiguration.TryProcessConfiguration(x => x.BatchProcessing, x => x.MaximumDelaySeconds, this.Id, settingsTable, dryRun, validator: DataBatchProcessing.PAUSE_LIMITS.Allows);
if (!maximumDelayIsValid && settingsTable.TryGetValue("DataBatchProcessing.MaximumDelaySeconds", out _))
LOG.LogWarning("The Batch Processing maximum delay configured by plugin {ConfigPluginId} must be between {MinimumDelaySeconds} and {MaximumDelaySeconds} seconds.", this.Id, DataBatchProcessing.PAUSE_LIMITS.ShortestSeconds, DataBatchProcessing.PAUSE_LIMITS.LongestSeconds);
if (minimumDelayIsValid && maximumDelayIsValid
&& settingsTable.TryGetValue("DataBatchProcessing.MinimumDelaySeconds", out var minimumDelayValue) && minimumDelayValue.TryRead<int>(out var minimumDelaySeconds)
&& settingsTable.TryGetValue("DataBatchProcessing.MaximumDelaySeconds", out var maximumDelayValue) && maximumDelayValue.TryRead<int>(out var maximumDelaySeconds)
&& maximumDelaySeconds < minimumDelaySeconds)
LOG.LogWarning("The Batch Processing maximum delay configured by plugin {ConfigPluginId} is below its minimum delay. The minimum delay of {MinimumDelaySeconds} seconds wins.", this.Id, minimumDelaySeconds);
ManagedConfiguration.TryProcessConfiguration(x => x.BatchProcessing, x => x.MinimumProviderConfidence, this.Id, settingsTable, dryRun);
ManagedConfiguration.TryProcessConfiguration(x => x.BatchProcessing, x => x.PreselectedProvider, Guid.Empty, this.Id, settingsTable, dryRun);
@@ -8,6 +8,7 @@ using AIStudio.Tools.Databases;
using AIStudio.Tools.Databases.IndexStore;
using AIStudio.Tools.Databases.VectorStore;
using AIStudio.Tools.Media;
using AIStudio.Tools.Pauses;
using AIStudio.Tools.PluginSystem;
using AIStudio.Tools.Security;
using AIStudio.Tools.Services.Indexing;
@@ -768,7 +769,7 @@ public sealed partial class DataSourceEmbeddingService(SettingsManager settingsM
/// <param name="token">The token of the run.</param>
private async Task WaitForPauseBetweenStepsAsync(IIndexedDataSource dataSource, DataSourceEmbeddingRefreshMode refreshMode, CancellationToken token)
{
var range = PauseRange.FromSettings(settingsManager.ConfigurationData.App.DataSourceIndexing);
var range = settingsManager.ConfigurationData.App.DataSourceIndexing.EffectivePauseRange;
var pause = this.pauseBetweenSteps.GetPauseBeforeNextStep(DateTimeOffset.UtcNow, range, rng);
if (pause.Remaining <= TimeSpan.Zero)
return;
@@ -41,9 +41,11 @@ The parts:
`MORE_TO_DO` gets its next step in the back lane of `DataSourceRunQueue`, behind every data source which begins
a pass, so a mailbox waits for one step at most.
- **`PauseBetweenSteps`** - a gap after every step which worked on documents, so that many people can share one
embedding server. The next run, of whichever data source, waits for a random pause between the bounds of the
settings (`PauseRange`: 0 to 300 seconds, none by default), less the time which passed since the step ended.
Not to be confused with the pause of the indexing as a whole (`IndexingPauses`), which a backup takes.
embedding server. The next run, of whichever data source, waits for a random pause between the bounds which
apply (`DataDataSourceIndexing.EffectivePauseRange` within `PAUSE_LIMITS`: 0 to 300 seconds, none by default),
less the time which passed since the step ended. The pause lives in `Tools/Pauses/` and is shared with other
features, so keep everything about the indexing out of it. Not to be confused with the pause of the indexing
as a whole (`IndexingPauses`), which a backup takes.
- **`IndexingPass`** - the steps of one data source, from its first step until one of them is done. It remembers
which documents failed during an earlier step, so the next steps do not read them again, and which failures
were reported to the user already.
@@ -1,68 +0,0 @@
using AIStudio.Settings;
using AIStudio.Settings.DataModel;
namespace AIStudio.Tools.Services.Indexing;
/// <summary>
/// The bounds of the random pause between two indexing steps, cf. PauseBetweenSteps.
/// </summary>
/// <param name="MinimumSeconds">The shortest pause. Between zero and MaximumSeconds.</param>
/// <param name="MaximumSeconds">The longest pause, which may be drawn as well. At most DataDataSourceIndexing.MAX_PAUSE_BETWEEN_STEPS_SECONDS.</param>
internal sealed record PauseRange(int MinimumSeconds, int MaximumSeconds)
{
/// <summary>
/// No pause at all, which is the default.
/// </summary>
public static readonly PauseRange NONE = new(0, 0);
/// <summary>
/// Whether the next step starts right away.
/// </summary>
public bool IsNone => this.MaximumSeconds is 0;
/// <summary>
/// The bounds which apply now, as the user set them and as configuration plugins lock them.
/// </summary>
/// <param name="settings">The settings of the indexing.</param>
/// <returns>The bounds which apply.</returns>
public static PauseRange FromSettings(DataDataSourceIndexing settings)
{
var minimumSeconds = settings.MinimumPauseBetweenStepsSeconds;
var maximumSeconds = settings.MaximumPauseBetweenStepsSeconds;
//
// Which bound a configuration plugin locks matters only while the bounds are the wrong way
// round. Asking reads the managed state of the whole app, which bounds in order do without:
//
if (minimumSeconds <= maximumSeconds)
return GetEffective(minimumSeconds, false, maximumSeconds, false);
var isMinimumLocked = ManagedConfiguration.TryGet(x => x.App.DataSourceIndexing, x => x.MinimumPauseBetweenStepsSeconds, out var minimumMeta) && minimumMeta.IsLocked;
var isMaximumLocked = ManagedConfiguration.TryGet(x => x.App.DataSourceIndexing, x => x.MaximumPauseBetweenStepsSeconds, out var maximumMeta) && maximumMeta.IsLocked;
return GetEffective(minimumSeconds, isMinimumLocked, maximumSeconds, isMaximumLocked);
}
/// <summary>
/// Works out the bounds which apply, so that the lower one never exceeds the upper one.
/// </summary>
/// <remarks>
/// A bound which a configuration plugin locks wins, and the other one adjusts to it. A bound the
/// plugin only presets counts like one the user set. Otherwise, the lower bound wins, i.e., the
/// longer pause: it spares the embedding server, which is what the pause is for. That holds as
/// well when a plugin locks both bounds the wrong way round.
/// </remarks>
/// <param name="minimumSeconds">The lower bound as set.</param>
/// <param name="isMinimumLocked">Whether a configuration plugin locks the lower bound.</param>
/// <param name="maximumSeconds">The upper bound as set.</param>
/// <param name="isMaximumLocked">Whether a configuration plugin locks the upper bound.</param>
/// <returns>The bounds which apply.</returns>
public static PauseRange GetEffective(int minimumSeconds, bool isMinimumLocked, int maximumSeconds, bool isMaximumLocked)
{
var minimum = Math.Clamp(minimumSeconds, 0, DataDataSourceIndexing.MAX_PAUSE_BETWEEN_STEPS_SECONDS);
var maximum = Math.Clamp(maximumSeconds, 0, DataDataSourceIndexing.MAX_PAUSE_BETWEEN_STEPS_SECONDS);
if (isMaximumLocked && !isMinimumLocked)
return new(Math.Min(minimum, maximum), maximum);
return new(minimum, Math.Max(minimum, maximum));
}
}