Files

109 lines
4.7 KiB
C#
Raw Permalink Normal View History

Load balancing: Load balance isolated caches to allow the backoffice to be load balanced (#20417) * V16: Cache Version Mechanism (#19747) * Add RepositoryCacheVersion table * Add repository * Add Cache version lock * Add GetAll method to repository * Add RepositoryCacheVersionService * Remember to add lock in data creator * Work my way out of constructor hell This is why we use DI folks. 🤦 * Add checks to specific cache policies * Fix migration * Add to schema creator * Fix database access * Initialize the cache version on in memory miss * Make cache version service internal * Add tests * Apply suggestions from code review Co-authored-by: Andy Butland <abutland73@gmail.com> * Add missing obsoletions * Prefer full name --------- Co-authored-by: Andy Butland <abutland73@gmail.com> * fixed merge * V16/feature/move last synced id to db (#19884) * Foundation work for moving last synced id * register manager and repo in dependency injection * Fixing to make tests work * Replacing the use of the old LastSyncedFileManager.cs with the new LastSyncedManager.cs * Testing to delete out of sync id and old entries * changing some stuff to please the reviewer. * Inverted saving methods id check and fixed documentation mishaps * Loadbalancing: Add Cache Sync service to allow us to roll forward isolated caches when backoffice is load balanced. (#20398) * Split cache refreshers into internal and external caches * Add obsolete constructor for CacheInstructionsPruningJob * Add xml docs * Move lastID management into CacheInstructionService * Cache last synced ids in memory * Lock when processing instructions * Sync caches when out of sync * Fix constructors for ICacheSyncService * Cache version on request * Register caches as synced when instructions are processed * Rename CacheVersionAccessor to IRepositoryCacheVersionAccessor * Set caches as synced before actually syncing the caches * Set caches as synced before syncing, within scope, this should also lock the cache version from being written to whilst updating caches * Only check version for backoffice requests * Clear request cache when caches are syned * Default to using NOOP cache version service * Don't generate local identity in database server messenger anymore * Fix ambiguous constructor * Add helper method to switch to load balanced isolated caches * Fix LastSyncedManagerTests * Fix RepositoryCacheVersionServiceTests * Fix DefaultCachePolicyTests * Use correct constructor in FullDataSetRepositoryCachePolicy * Minor cleanup * Add XML docs * Add more xml docs * Apply suggestions from code review Co-authored-by: Nikolaj Geisle <70372949+Zeegaan@users.noreply.github.com> --------- Co-authored-by: Zeegaan <skrivdetud@gmail.com> Co-authored-by: Nikolaj Geisle <70372949+Zeegaan@users.noreply.github.com> * Fix migration plan * fix tests * Fix integration tests * Fix changes from github review * Move premigrations to v17 * Make lock constantws sequential * Fix comment * Make IRepositoryCacheVersionService and ICacheSyncService protected on EntityRepositoryBase --------- Co-authored-by: Andy Butland <abutland73@gmail.com> Co-authored-by: Nicklas Kramer <nik@umbraco.dk> Co-authored-by: NillasKA <kramernicklas@gmail.com> Co-authored-by: Zeegaan <skrivdetud@gmail.com> Co-authored-by: Nikolaj Geisle <70372949+Zeegaan@users.noreply.github.com>
2025-10-08 14:55:50 +02:00
using NUnit.Framework;
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Persistence.Repositories;
using Umbraco.Cms.Core.Scoping;
using Umbraco.Cms.Tests.Common.Testing;
using Umbraco.Cms.Tests.Integration.Testing;
namespace Umbraco.Cms.Tests.Integration.Umbraco.Core.Services;
[TestFixture]
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest, Logger = UmbracoTestOptions.Logger.Console)]
internal sealed class RepositoryCacheVersionServiceTests : UmbracoIntegrationTest
{
private IRepositoryCacheVersionService RepositoryCacheVersionService => GetRequiredService<IRepositoryCacheVersionService>();
private IRepositoryCacheVersionRepository RepositoryCacheVersionRepository => GetRequiredService<IRepositoryCacheVersionRepository>();
private ICoreScopeProvider CoreScopeProvider => GetRequiredService<ICoreScopeProvider>();
protected override void CustomTestSetup(IUmbracoBuilder builder) => builder.LoadBalanceIsolatedCaches();
[Test]
public async Task Cache_Is_Initially_Synced()
{
var isSynced = await RepositoryCacheVersionService.IsCacheSyncedAsync<IContent>();
Assert.IsTrue(isSynced, "Cache should be initially synced.");
}
[Test]
public async Task SetCacheUpdatedAsync_Writes_Version_To_Database()
{
using var scope = CoreScopeProvider.CreateCoreScope(autoComplete: true);
var initial = await RepositoryCacheVersionRepository.GetAsync(GetCacheKey());
Assert.IsNull(initial?.Version, "Initial cache version should be null before update.");
await RepositoryCacheVersionService.SetCacheUpdatedAsync<IContent>();
var cacheVersion = await RepositoryCacheVersionRepository.GetAsync(GetCacheKey());
Assert.IsNotNull(cacheVersion, "Cache version should exist in the database after update.");
Assert.IsFalse(string.IsNullOrEmpty(cacheVersion.Version), "Cache version string should not be null or empty.");
}
[Test]
public async Task Cache_Is_Out_Of_Sync_If_Updated_Remotely()
{
// Simulate an update
await RepositoryCacheVersionService.SetCacheUpdatedAsync<IContent>();
var isSynced = await RepositoryCacheVersionService.IsCacheSyncedAsync<IContent>();
// We should be synced now.
Assert.IsTrue(isSynced);
using (var scope = CoreScopeProvider.CreateCoreScope())
{
// Simulate a remote update to the database
await RepositoryCacheVersionRepository.SaveAsync(GetRepositoryRandomCacheVersion());
scope.Complete();
}
// Now the cache should be out of sync
isSynced = await RepositoryCacheVersionService.IsCacheSyncedAsync<IContent>();
Assert.IsFalse(isSynced, "Cache should be out of sync after remote update.");
}
[Test]
public async Task SetCacheUpdatedAsync_Updates_Cache_Version()
{
using var scope = CoreScopeProvider.CreateCoreScope(autoComplete: true);
await RepositoryCacheVersionService.SetCacheUpdatedAsync<IContent>();
var initialCacheVersion = await RepositoryCacheVersionRepository.GetAsync(GetCacheKey());
Assert.IsNotNull(initialCacheVersion, "Initial cache version should not be null.");
await RepositoryCacheVersionService.SetCacheUpdatedAsync<IContent>();
var updatedCacheVersion = await RepositoryCacheVersionRepository.GetAsync(GetCacheKey());
Assert.IsNotNull(updatedCacheVersion, "Updated cache version should not be null.");
Assert.AreNotEqual(initialCacheVersion.Version, updatedCacheVersion.Version, "Cache version should be updated.");
}
[Test]
public async Task CacheVersion_Is_Unique_Per_Repository_Type()
{
using var scope = CoreScopeProvider.CreateCoreScope(autoComplete: true);
await RepositoryCacheVersionService.SetCacheUpdatedAsync<IContent>();
await RepositoryCacheVersionService.SetCacheUpdatedAsync<IMedia>();
var contentVersion = (await RepositoryCacheVersionRepository.GetAsync(GetCacheKey()))?.Version;
var mediaKey = ((RepositoryCacheVersionService)RepositoryCacheVersionService).GetCacheKey<IMedia>();
var mediaVersion = (await RepositoryCacheVersionRepository.GetAsync(mediaKey))?.Version;
Assert.IsNotNull(contentVersion);
Assert.IsNotNull(mediaVersion);
Assert.AreNotEqual(contentVersion, mediaVersion, "Cache versions should be unique for different repository types.");
}
private RepositoryCacheVersion GetRepositoryRandomCacheVersion()
=> new()
{
Identifier = GetCacheKey(),
Version = Guid.NewGuid().ToString(),
};
private string GetCacheKey()
=> ((RepositoryCacheVersionService)RepositoryCacheVersionService).GetCacheKey<IContent>();
}