perf(deployment,template-engine): batched untracked staleness loads, staging-failure detach, lock-held Disabled re-check, targeted single-entity loads (DEP-4, DEP-5, DEP-6, TPL-4, WP6 task 6.6)

DEP-4: new ITemplateEngineRepository.GetInstancesByIdsUntrackedAsync (one
AsNoTracking split query per 500-id chunk). GetInstanceStalenessAsync preloads
each chunk into its FlattenSession; FlattenCoreAsync takes a preloaded
instance (consumed once) before falling back to the tracked per-instance load.
Deploy sessions never preload, so prepare keeps its tracked load.
  Throwaway harness, SQLite, 1,000 instances x 50 overrides:
  before: 5,000 reader commands, 51,000 tracked entities
  after:  10 reader commands, 0 tracked entities
  (wall time on in-process SQLite 2.7 s -> 2.3 s; the win on SQL Server is
  the 4,990 saved round-trips and the change tracker staying empty)

DEP-5: AddPendingDeploymentAsync supersedes prior rows with a set-based
delete (DeletePendingWhereAsync) instead of tracked key stubs, and a failed
staging save calls DetachPendingChanges under dbGate so it cannot poison the
next instance's staging save on the shared bulk context.
  Regression: prior row purged between supersede and save -> before:
  DbUpdateConcurrencyException; after: stages cleanly.

DEP-6: bulk prepare (reconcileGate != null) re-reads the instance state
untracked (new IDeploymentManagerRepository.GetInstanceStateAsync) once the
operation lock is held; Disabled -> reported in SkippedInstances with the
Disabled reason, never flattened/sent/re-enabled. Placed at the top of
PrepareUnderLockAsync so it is independent of WP7's lock-ordering change.

TPL-4: area delete uses the new shallow untracked GetInstancesBySiteIdShallowAsync;
SetAlarmOverride and Delete{Attribute,Alarm,Script} read the analysis closure
instead of the whole template table; MoveTemplate's collision check is the new
TemplateNameExistsInFolderAsync (one folder's names). SiteService already used
the include-free ISiteRepository read (unchanged).
This commit is contained in:
Joseph Doherty
2026-12-03 10:09:41 -05:00
parent 04e3969071
commit 59817f5017
19 changed files with 493 additions and 57 deletions
@@ -501,6 +501,29 @@ public class TemplateEngineRepository : ITemplateEngineRepository
.FirstOrDefaultAsync(i => i.Id == id, cancellationToken);
}
/// <inheritdoc />
public async Task<IReadOnlyList<Instance>> GetInstancesByIdsUntrackedAsync(
IReadOnlyCollection<int> ids, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(ids);
var result = new List<Instance>(ids.Count);
// Chunked so the IN list stays well under SQL Server's 2,100-parameter limit.
foreach (var chunk in ids.Distinct().Chunk(500))
{
result.AddRange(await _context.Instances
.AsNoTracking()
.Where(i => chunk.Contains(i.Id))
.Include(i => i.AttributeOverrides)
.Include(i => i.AlarmOverrides)
.Include(i => i.ConnectionBindings)
.Include(i => i.NativeAlarmSourceOverrides)
.AsSplitQuery()
.ToListAsync(cancellationToken));
}
return result;
}
/// <inheritdoc />
public async Task<IReadOnlyList<Instance>> GetAllInstancesAsync(CancellationToken cancellationToken = default)
{
@@ -521,6 +544,30 @@ public class TemplateEngineRepository : ITemplateEngineRepository
.ToListAsync(cancellationToken);
}
/// <inheritdoc />
public async Task<IReadOnlyList<Instance>> GetInstancesBySiteIdShallowAsync(int siteId, CancellationToken cancellationToken = default)
{
return await _context.Instances
.AsNoTracking()
.Where(i => i.SiteId == siteId)
.ToListAsync(cancellationToken);
}
/// <inheritdoc />
public async Task<bool> TemplateNameExistsInFolderAsync(
int? folderId, string name, int excludeTemplateId, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(name);
// Only the destination folder's names are read (one narrow projection), then compared
// case-insensitively in memory — independent of the database collation.
var siblingNames = await _context.Templates
.AsNoTracking()
.Where(t => t.FolderId == folderId && t.Id != excludeTemplateId)
.Select(t => t.Name)
.ToListAsync(cancellationToken);
return siblingNames.Any(n => string.Equals(n, name, StringComparison.OrdinalIgnoreCase));
}
/// <inheritdoc />
public async Task<IReadOnlyList<Instance>> GetInstancesBySiteIdAsync(int siteId, CancellationToken cancellationToken = default)
{