-
Notifications
You must be signed in to change notification settings - Fork 2.8k
V16/feature/move last synced id to db #19884
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
Merged
nikolajlauridsen
merged 7 commits into
v16/feature/load-balancing-isolated-caches
from
v16/feature/move-last-synced-id-to-db
Aug 11, 2025
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4437b48
Foundation work for moving last synced id
NillasKA 646cdb7
register manager and repo in dependency injection
NillasKA d9e5c2c
Fixing to make tests work
NillasKA 1b25513
Replacing the use of the old LastSyncedFileManager.cs with the new La…
NillasKA 09c4ac6
Testing to delete out of sync id and old entries
NillasKA 408a573
changing some stuff to please the reviewer.
NillasKA 9517e7c
Inverted saving methods id check and fixed documentation mishaps
NillasKA File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace Umbraco.Cms.Core.Factories; | ||
|
||
/// <summary> | ||
/// Fetches information of the host machine. | ||
/// </summary> | ||
public interface IMachineInfoFactory | ||
{ | ||
/// <summary> | ||
/// Fetches the name of the Host Machine for identification. | ||
/// </summary> | ||
/// <returns>A name of the host machine.</returns> | ||
public string GetMachineIdentifier(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace Umbraco.Cms.Core.Factories; | ||
|
||
internal sealed class MachineInfoFactory : IMachineInfoFactory | ||
{ | ||
|
||
/// <inheritdoc /> | ||
public string GetMachineIdentifier() => Environment.MachineName; | ||
} |
38 changes: 38 additions & 0 deletions
38
src/Umbraco.Core/Persistence/Repositories/ILastSyncedRepository.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
namespace Umbraco.Cms.Core.Persistence.Repositories; | ||
|
||
/// <summary> | ||
/// Handles saving and pruning of the LastSynced database table. | ||
/// </summary> | ||
public interface ILastSyncedRepository | ||
{ | ||
/// <summary> | ||
/// Fetches the last synced internal ID from the database. | ||
/// </summary> | ||
/// <returns>The Internal ID from the database.</returns> | ||
Task<int?> GetInternalIdAsync(); | ||
|
||
/// <summary> | ||
/// Fetches the last synced external ID from the database. | ||
/// </summary> | ||
/// <returns>The External ID from the database.</returns> | ||
Task<int?> GetExternalIdAsync(); | ||
|
||
/// <summary> | ||
/// Saves the last synced Internal ID to the Database. | ||
/// </summary> | ||
/// <param name="id">The last synced internal ID.</param> | ||
Task SaveInternalIdAsync(int id); | ||
|
||
/// <summary> | ||
/// Saves the last synced External ID to the Database. | ||
/// </summary> | ||
/// <param name="id">The last synced external ID.</param> | ||
Task SaveExternalIdAsync(int id); | ||
|
||
/// <summary> | ||
/// Deletes entries older than the set parameter. This method also removes any entries where both | ||
/// IDs are higher than the lowest synced CacheInstruction ID. | ||
/// </summary> | ||
/// <param name="pruneDate">Any date entries in the DB before this parameter, will be removed from the Database.</param> | ||
Task DeleteEntriesOlderThanAsync(DateTime pruneDate); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
namespace Umbraco.Cms.Core.Sync; | ||
|
||
/// <summary> | ||
/// Handles saving and pruning of the LastSynced database table. | ||
/// </summary> | ||
public interface ILastSyncedManager | ||
{ | ||
/// <summary> | ||
/// Fetches the last synced internal ID from the database. | ||
/// </summary> | ||
/// <returns>The Internal ID from the database.</returns> | ||
Task<int?> GetLastSyncedInternalAsync(); | ||
|
||
/// <summary> | ||
/// Fetches the last synced external ID from the database. | ||
/// </summary> | ||
/// <returns>The External ID from the database.</returns> | ||
Task<int?> GetLastSyncedExternalAsync(); | ||
|
||
/// <summary> | ||
/// Saves the last synced Internal ID to the Database. | ||
/// </summary> | ||
/// <param name="id">The last synced internal ID.</param> | ||
Task SaveLastSyncedInternalAsync(int id); | ||
|
||
/// <summary> | ||
/// Saves the last synced External ID to the Database. | ||
/// </summary> | ||
/// <param name="id">The last synced external ID.</param> | ||
Task SaveLastSyncedExternalAsync(int id); | ||
|
||
/// <summary> | ||
/// Deletes entries older than the set parameter. This method also removes any entries where both | ||
/// IDs are higher than the lowest synced CacheInstruction ID. | ||
/// </summary> | ||
/// <param name="pruneDate">Any date entries in the DB before this parameter, will be removed from the Database.</param> | ||
Task DeleteOlderThanAsync(DateTime date); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using Umbraco.Cms.Core.Persistence.Repositories; | ||
using Umbraco.Cms.Core.Scoping; | ||
|
||
namespace Umbraco.Cms.Core.Sync; | ||
|
||
/// <inheritdoc/> | ||
internal sealed class LastSyncedManager : ILastSyncedManager | ||
{ | ||
private readonly ILastSyncedRepository _lastSyncedRepository; | ||
private readonly ICoreScopeProvider _coreScopeProvider; | ||
|
||
public LastSyncedManager(ILastSyncedRepository lastSyncedRepository, ICoreScopeProvider coreScopeProvider) | ||
{ | ||
_lastSyncedRepository = lastSyncedRepository; | ||
_coreScopeProvider = coreScopeProvider; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public async Task<int?> GetLastSyncedInternalAsync() | ||
{ | ||
using ICoreScope scope = _coreScopeProvider.CreateCoreScope(); | ||
int? internalId = await _lastSyncedRepository.GetInternalIdAsync(); | ||
scope.Complete(); | ||
|
||
return internalId; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public async Task<int?> GetLastSyncedExternalAsync() | ||
{ | ||
using ICoreScope scope = _coreScopeProvider.CreateCoreScope(); | ||
int? externalId = await _lastSyncedRepository.GetExternalIdAsync(); | ||
scope.Complete(); | ||
|
||
return externalId; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public async Task SaveLastSyncedInternalAsync(int id) | ||
NillasKA marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
if (id < 0) | ||
{ | ||
throw new ArgumentException("Invalid last synced id. Must be non-negative."); | ||
} | ||
|
||
using ICoreScope scope = _coreScopeProvider.CreateCoreScope(); | ||
await _lastSyncedRepository.SaveInternalIdAsync(id); | ||
scope.Complete(); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public async Task SaveLastSyncedExternalAsync(int id) | ||
{ | ||
if (id < 0) | ||
{ | ||
throw new ArgumentException("Invalid last synced id. Must be non-negative."); | ||
} | ||
|
||
using ICoreScope scope = _coreScopeProvider.CreateCoreScope(); | ||
await _lastSyncedRepository.SaveExternalIdAsync(id); | ||
scope.Complete(); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public async Task DeleteOlderThanAsync(DateTime date) | ||
{ | ||
using ICoreScope scope = _coreScopeProvider.CreateCoreScope(); | ||
await _lastSyncedRepository.DeleteEntriesOlderThanAsync(date); | ||
scope.Complete(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/Umbraco.Infrastructure/Migrations/Upgrade/V_16_2_0/AddLastSyncedTable.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using Umbraco.Cms.Infrastructure.Persistence.Dtos; | ||
|
||
namespace Umbraco.Cms.Infrastructure.Migrations.Upgrade.V_16_2_0; | ||
|
||
public class AddLastSyncedTable : AsyncMigrationBase | ||
{ | ||
public AddLastSyncedTable(IMigrationContext context) | ||
: base(context) | ||
{ | ||
} | ||
|
||
protected override Task MigrateAsync() | ||
{ | ||
if (TableExists(LastSyncedDto.TableName) is false) | ||
{ | ||
Create.Table<LastSyncedDto>().Do(); | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/Umbraco.Infrastructure/Persistence/Dtos/LastSyncedDto.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using NPoco; | ||
using Umbraco.Cms.Infrastructure.Persistence.DatabaseAnnotations; | ||
using Constants = Umbraco.Cms.Core.Constants; | ||
|
||
namespace Umbraco.Cms.Infrastructure.Persistence.Dtos; | ||
|
||
|
||
[TableName(TableName)] | ||
[PrimaryKey("machineId", AutoIncrement = false)] | ||
[ExplicitColumns] | ||
public class LastSyncedDto | ||
{ | ||
internal const string TableName = Constants.DatabaseSchema.Tables.LastSynced; | ||
|
||
[Column("machineId")] | ||
[PrimaryKeyColumn(Name = "PK_lastSyncedMachineId", AutoIncrement = false, Clustered = true)] | ||
public required string MachineId { get; set; } | ||
|
||
[Column("lastSyncedInternalId")] | ||
[NullSetting(NullSetting = NullSettings.Null)] | ||
public int? LastSyncedInternalId { get; set; } | ||
|
||
[Column("lastSyncedExternalId")] | ||
[NullSetting(NullSetting = NullSettings.Null)] | ||
public int? LastSyncedExternalId { get; set; } | ||
|
||
[Column("lastSyncedDate")] | ||
public DateTime LastSyncedDate { get; set; } | ||
} |
103 changes: 103 additions & 0 deletions
103
src/Umbraco.Infrastructure/Persistence/Repositories/Implement/LastSyncedRepository.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
using NPoco; | ||
using Umbraco.Cms.Core.Cache; | ||
using Umbraco.Cms.Core.Factories; | ||
using Umbraco.Cms.Core.Persistence.Repositories; | ||
using Umbraco.Cms.Infrastructure.Persistence.Dtos; | ||
using Umbraco.Cms.Infrastructure.Scoping; | ||
using Umbraco.Extensions; | ||
|
||
namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement; | ||
|
||
/// <inheritdoc cref="ILastSyncedRepository"/> | ||
public class LastSyncedRepository : RepositoryBase, ILastSyncedRepository | ||
{ | ||
private readonly IMachineInfoFactory _machineInfoFactory; | ||
|
||
public LastSyncedRepository(IScopeAccessor scopeAccessor, AppCaches appCaches, IMachineInfoFactory machineInfoFactory) | ||
: base(scopeAccessor, appCaches) | ||
{ | ||
_machineInfoFactory = machineInfoFactory; | ||
} | ||
|
||
|
||
/// <inheritdoc /> | ||
public async Task<int?> GetInternalIdAsync() | ||
{ | ||
string machineName = _machineInfoFactory.GetMachineIdentifier(); | ||
|
||
Sql<ISqlContext> sql = Database.SqlContext.Sql() | ||
.Select<LastSyncedDto>(x => x.LastSyncedInternalId) | ||
.From<LastSyncedDto>() | ||
.Where<LastSyncedDto>(x => x.MachineId == machineName); | ||
|
||
return await Database.ExecuteScalarAsync<int?>(sql); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task<int?> GetExternalIdAsync() | ||
{ | ||
string machineName = _machineInfoFactory.GetMachineIdentifier(); | ||
|
||
Sql<ISqlContext> sql = Database.SqlContext.Sql() | ||
.Select<LastSyncedDto>(x => x.LastSyncedExternalId) | ||
.From<LastSyncedDto>() | ||
.Where<LastSyncedDto>(x => x.MachineId == machineName); | ||
|
||
return await Database.ExecuteScalarAsync<int?>(sql); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task SaveInternalIdAsync(int id) | ||
{ | ||
LastSyncedDto dto = new LastSyncedDto() | ||
{ | ||
MachineId = _machineInfoFactory.GetMachineIdentifier(), | ||
LastSyncedInternalId = id, | ||
LastSyncedDate = DateTime.Now, | ||
}; | ||
|
||
await Database.InsertOrUpdateAsync( | ||
dto, | ||
"SET lastSyncedInternalId=@LastSyncedInternalId, lastSyncedDate=@LastSyncedDate WHERE machineId=@MachineId", | ||
new | ||
{ | ||
dto.LastSyncedInternalId, | ||
dto.LastSyncedDate, | ||
dto.MachineId, | ||
}); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task SaveExternalIdAsync(int id) | ||
{ | ||
LastSyncedDto dto = new LastSyncedDto() | ||
{ | ||
MachineId = _machineInfoFactory.GetMachineIdentifier(), | ||
LastSyncedExternalId = id, | ||
LastSyncedDate = DateTime.Now, | ||
}; | ||
|
||
await Database.InsertOrUpdateAsync( | ||
dto, | ||
"SET lastSyncedExternalId=@LastSyncedExternalId, lastSyncedDate=@LastSyncedDate WHERE machineId=@MachineId", | ||
new | ||
{ | ||
dto.LastSyncedExternalId, | ||
dto.LastSyncedDate, | ||
dto.MachineId, | ||
}); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task DeleteEntriesOlderThanAsync(DateTime pruneDate) | ||
{ | ||
var maxId = Database.ExecuteScalar<int>($"SELECT MAX(Id) FROM umbracoCacheInstruction;"); | ||
|
||
Sql sql = | ||
new Sql().Append( | ||
@"DELETE FROM umbracoLastSynced WHERE lastSyncedDate < @pruneDate OR lastSyncedInternalId > @maxId AND lastSyncedExternalId > @maxId;", | ||
new { pruneDate, maxId }); | ||
|
||
await Database.ExecuteAsync(sql); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.