Skip to content

Commit

Permalink
Add DB locking for container APIs (#15870)
Browse files Browse the repository at this point in the history
  • Loading branch information
kjac authored Mar 18, 2024
1 parent e9cfcf4 commit 39085b9
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 6 deletions.
5 changes: 5 additions & 0 deletions src/Umbraco.Core/Services/ContentTypeContainerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Persistence.Repositories;
using Umbraco.Cms.Core.Scoping;
using Umbraco.Cms.Core.Services.Locking;

namespace Umbraco.Cms.Core.Services;

Expand All @@ -23,4 +24,8 @@ public ContentTypeContainerService(
protected override Guid ContainedObjectType => Constants.ObjectTypes.DocumentType;

protected override UmbracoObjectTypes ContainerObjectType => UmbracoObjectTypes.DocumentTypeContainer;

protected override int[] ReadLockIds => ContentTypeLocks.ReadLockIds;

protected override int[] WriteLockIds => ContentTypeLocks.WriteLockIds;
}
6 changes: 3 additions & 3 deletions src/Umbraco.Core/Services/ContentTypeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Umbraco.Cms.Core.Persistence.Repositories;
using Umbraco.Cms.Core.Scoping;
using Umbraco.Cms.Core.Services.Changes;
using Umbraco.Cms.Core.Services.Locking;
using Umbraco.Cms.Core.Services.OperationStatus;

namespace Umbraco.Cms.Core.Services;
Expand Down Expand Up @@ -64,10 +65,9 @@ public ContentTypeService(
StaticServiceProvider.Instance.GetRequiredService<IUserIdKeyResolver>())
{ }

// beware! order is important to avoid deadlocks
protected override int[] ReadLockIds { get; } = { Constants.Locks.ContentTypes };
protected override int[] ReadLockIds => ContentTypeLocks.ReadLockIds;

protected override int[] WriteLockIds { get; } = { Constants.Locks.ContentTree, Constants.Locks.ContentTypes };
protected override int[] WriteLockIds => ContentTypeLocks.WriteLockIds;

protected override Guid ContainedObjectType => Constants.ObjectTypes.DocumentType;

Expand Down
6 changes: 6 additions & 0 deletions src/Umbraco.Core/Services/DataTypeContainerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,10 @@ public DataTypeContainerService(
protected override Guid ContainedObjectType => Constants.ObjectTypes.DataType;

protected override UmbracoObjectTypes ContainerObjectType => UmbracoObjectTypes.DataTypeContainer;

// data types do not have read/write locks (yet)
protected override int[] ReadLockIds => [];

// data types do not have read/write locks (yet)
protected override int[] WriteLockIds => [];
}
24 changes: 24 additions & 0 deletions src/Umbraco.Core/Services/EntityTypeContainerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ internal abstract class EntityTypeContainerService<TTreeEntity, TEntityContainer

protected abstract UmbracoObjectTypes ContainerObjectType { get; }

protected abstract int[] ReadLockIds { get; }

protected abstract int[] WriteLockIds { get; }

protected EntityTypeContainerService(
ICoreScopeProvider provider,
ILoggerFactory loggerFactory,
Expand All @@ -42,6 +46,7 @@ protected EntityTypeContainerService(
public async Task<EntityContainer?> GetAsync(Guid id)
{
using ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true);
ReadLock(scope);
return await Task.FromResult(_entityContainerRepository.Get(id));
}

Expand Down Expand Up @@ -134,6 +139,7 @@ protected EntityTypeContainerService(
public async Task<Attempt<EntityContainer?, EntityContainerOperationStatus>> DeleteAsync(Guid id, Guid userKey)
{
using ICoreScope scope = ScopeProvider.CreateCoreScope();
WriteLock(scope);

EntityContainer? container = _entityContainerRepository.Get(id);
if (container == null)
Expand Down Expand Up @@ -178,6 +184,7 @@ protected EntityTypeContainerService(
}

using ICoreScope scope = ScopeProvider.CreateCoreScope();
WriteLock(scope);

EntityContainerOperationStatus operationValidationStatus = operationValidation();
if (operationValidationStatus != EntityContainerOperationStatus.Success)
Expand Down Expand Up @@ -212,9 +219,26 @@ protected EntityTypeContainerService(
}

using ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true);
ReadLock(scope);
return _entityContainerRepository.Get(treeEntity.ParentId);
}

private void Audit(AuditType type, int userId, int objectId)
=> _auditRepository.Save(new AuditItem(objectId, type, userId, ContainerObjectType.GetName()));

private void ReadLock(ICoreScope scope)
{
if (ReadLockIds.Any())
{
scope.ReadLock(ReadLockIds);
}
}

private void WriteLock(ICoreScope scope)
{
if (WriteLockIds.Any())
{
scope.WriteLock(WriteLockIds);
}
}
}
9 changes: 9 additions & 0 deletions src/Umbraco.Core/Services/Locking/ContentTypeLocks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Umbraco.Cms.Core.Services.Locking;

internal static class ContentTypeLocks
{
// beware! order is important to avoid deadlocks
internal static int[] ReadLockIds { get; } = { Constants.Locks.ContentTypes };

internal static int[] WriteLockIds { get; } = { Constants.Locks.ContentTree, Constants.Locks.ContentTypes };
}
9 changes: 9 additions & 0 deletions src/Umbraco.Core/Services/Locking/MediaTypeLocks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Umbraco.Cms.Core.Services.Locking;

internal static class MediaTypeLocks
{
// beware! order is important to avoid deadlocks
internal static int[] ReadLockIds { get; } = { Constants.Locks.MediaTypes };

internal static int[] WriteLockIds { get; } = { Constants.Locks.MediaTree, Constants.Locks.MediaTypes };
}
5 changes: 5 additions & 0 deletions src/Umbraco.Core/Services/MediaTypeContainerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Persistence.Repositories;
using Umbraco.Cms.Core.Scoping;
using Umbraco.Cms.Core.Services.Locking;

namespace Umbraco.Cms.Core.Services;

Expand All @@ -23,4 +24,8 @@ public MediaTypeContainerService(
protected override Guid ContainedObjectType => Constants.ObjectTypes.MediaType;

protected override UmbracoObjectTypes ContainerObjectType => UmbracoObjectTypes.MediaTypeContainer;

protected override int[] ReadLockIds => MediaTypeLocks.ReadLockIds;

protected override int[] WriteLockIds => MediaTypeLocks.WriteLockIds;
}
6 changes: 3 additions & 3 deletions src/Umbraco.Core/Services/MediaTypeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Umbraco.Cms.Core.Persistence.Repositories;
using Umbraco.Cms.Core.Scoping;
using Umbraco.Cms.Core.Services.Changes;
using Umbraco.Cms.Core.Services.Locking;

namespace Umbraco.Cms.Core.Services;

Expand Down Expand Up @@ -60,10 +61,9 @@ public MediaTypeService(
}


// beware! order is important to avoid deadlocks
protected override int[] ReadLockIds { get; } = { Constants.Locks.MediaTypes };
protected override int[] ReadLockIds => MediaTypeLocks.ReadLockIds;

protected override int[] WriteLockIds { get; } = { Constants.Locks.MediaTree, Constants.Locks.MediaTypes };
protected override int[] WriteLockIds => MediaTypeLocks.WriteLockIds;

protected override Guid ContainedObjectType => Constants.ObjectTypes.MediaType;

Expand Down

0 comments on commit 39085b9

Please sign in to comment.