1
0
mirror of https://github.com/bitwarden/server synced 2026-02-28 02:13:19 +00:00

Ac/pm 32125/remove ivnextinmemoryapplicationcacheservice (#7067)

This commit is contained in:
Jimmy Vo
2026-02-26 09:30:06 -05:00
committed by GitHub
parent 5eb7bd28f6
commit 47b60ef6cd
9 changed files with 81 additions and 1060 deletions

View File

@@ -1,19 +0,0 @@
using Bit.Core.AdminConsole.Entities;
using Bit.Core.AdminConsole.Entities.Provider;
using Bit.Core.AdminConsole.Models.Data.Provider;
using Bit.Core.Models.Data.Organizations;
namespace Bit.Core.AdminConsole.AbilitiesCache;
public interface IVNextInMemoryApplicationCacheService
{
Task<IDictionary<Guid, OrganizationAbility>> GetOrganizationAbilitiesAsync();
#nullable enable
Task<OrganizationAbility?> GetOrganizationAbilityAsync(Guid orgId);
#nullable disable
Task<IDictionary<Guid, ProviderAbility>> GetProviderAbilitiesAsync();
Task UpsertOrganizationAbilityAsync(Organization organization);
Task UpsertProviderAbilityAsync(Provider provider);
Task DeleteOrganizationAbilityAsync(Guid organizationId);
Task DeleteProviderAbilityAsync(Guid providerId);
}

View File

@@ -1,137 +0,0 @@
using System.Collections.Concurrent;
using Bit.Core.AdminConsole.Entities;
using Bit.Core.AdminConsole.Entities.Provider;
using Bit.Core.AdminConsole.Models.Data.Provider;
using Bit.Core.AdminConsole.Repositories;
using Bit.Core.Models.Data.Organizations;
using Bit.Core.Repositories;
namespace Bit.Core.AdminConsole.AbilitiesCache;
public class VNextInMemoryApplicationCacheService(
IOrganizationRepository organizationRepository,
IProviderRepository providerRepository,
TimeProvider timeProvider) : IVNextInMemoryApplicationCacheService
{
private ConcurrentDictionary<Guid, OrganizationAbility> _orgAbilities = new();
private readonly SemaphoreSlim _orgInitLock = new(1, 1);
private DateTimeOffset _lastOrgAbilityRefresh = DateTimeOffset.MinValue;
private ConcurrentDictionary<Guid, ProviderAbility> _providerAbilities = new();
private readonly SemaphoreSlim _providerInitLock = new(1, 1);
private DateTimeOffset _lastProviderAbilityRefresh = DateTimeOffset.MinValue;
private readonly TimeSpan _refreshInterval = TimeSpan.FromMinutes(10);
public virtual async Task<IDictionary<Guid, OrganizationAbility>> GetOrganizationAbilitiesAsync()
{
await InitOrganizationAbilitiesAsync();
return _orgAbilities;
}
public async Task<OrganizationAbility?> GetOrganizationAbilityAsync(Guid organizationId)
{
(await GetOrganizationAbilitiesAsync())
.TryGetValue(organizationId, out var organizationAbility);
return organizationAbility;
}
public virtual async Task<IDictionary<Guid, ProviderAbility>> GetProviderAbilitiesAsync()
{
await InitProviderAbilitiesAsync();
return _providerAbilities;
}
public virtual async Task UpsertProviderAbilityAsync(Provider provider)
{
await InitProviderAbilitiesAsync();
_providerAbilities.AddOrUpdate(
provider.Id,
static (_, provider) => new ProviderAbility(provider),
static (_, _, provider) => new ProviderAbility(provider),
provider);
}
public virtual async Task UpsertOrganizationAbilityAsync(Organization organization)
{
await InitOrganizationAbilitiesAsync();
_orgAbilities.AddOrUpdate(
organization.Id,
static (_, organization) => new OrganizationAbility(organization),
static (_, _, organization) => new OrganizationAbility(organization),
organization);
}
public virtual Task DeleteOrganizationAbilityAsync(Guid organizationId)
{
_orgAbilities.TryRemove(organizationId, out _);
return Task.CompletedTask;
}
public virtual Task DeleteProviderAbilityAsync(Guid providerId)
{
_providerAbilities.TryRemove(providerId, out _);
return Task.CompletedTask;
}
private async Task InitOrganizationAbilitiesAsync() =>
await InitAbilitiesAsync<OrganizationAbility>(
dict => _orgAbilities = dict,
() => _lastOrgAbilityRefresh,
dt => _lastOrgAbilityRefresh = dt,
_orgInitLock,
async () => await organizationRepository.GetManyAbilitiesAsync(),
_refreshInterval,
ability => ability.Id);
private async Task InitProviderAbilitiesAsync() =>
await InitAbilitiesAsync<ProviderAbility>(
dict => _providerAbilities = dict,
() => _lastProviderAbilityRefresh,
dateTime => _lastProviderAbilityRefresh = dateTime,
_providerInitLock,
async () => await providerRepository.GetManyAbilitiesAsync(),
_refreshInterval,
ability => ability.Id);
private async Task InitAbilitiesAsync<TAbility>(
Action<ConcurrentDictionary<Guid, TAbility>> setCache,
Func<DateTimeOffset> getLastRefresh,
Action<DateTimeOffset> setLastRefresh,
SemaphoreSlim @lock,
Func<Task<IEnumerable<TAbility>>> fetchFunc,
TimeSpan refreshInterval,
Func<TAbility, Guid> getId)
{
if (SkipRefresh())
{
return;
}
await @lock.WaitAsync();
try
{
if (SkipRefresh())
{
return;
}
var sources = await fetchFunc();
var abilities = new ConcurrentDictionary<Guid, TAbility>(
sources.ToDictionary(getId));
setCache(abilities);
setLastRefresh(timeProvider.GetUtcNow());
}
finally
{
@lock.Release();
}
bool SkipRefresh()
{
return timeProvider.GetUtcNow() - getLastRefresh() <= refreshInterval;
}
}
}

View File

@@ -140,7 +140,6 @@ public static class FeatureFlagKeys
public const string CreateDefaultLocation = "pm-19467-create-default-location";
public const string AutomaticConfirmUsers = "pm-19934-auto-confirm-organization-users";
public const string ScimRevokeV2 = "pm-32394-scim-revoke-put-v2";
public const string PM23845_VNextApplicationCache = "pm-24957-refactor-memory-application-cache";
public const string DefaultUserCollectionRestore = "pm-30883-my-items-restored-users";
public const string RefactorMembersComponent = "pm-29503-refactor-members-inheritance";
public const string BulkReinviteUI = "pm-28416-bulk-reinvite-ux-improvements";

View File

@@ -7,10 +7,12 @@ namespace Bit.Core.Services;
public interface IApplicationCacheService
{
[Obsolete("We are transitioning to a new cache pattern. Please consult the Admin Console team before using.", false)]
Task<IDictionary<Guid, OrganizationAbility>> GetOrganizationAbilitiesAsync();
#nullable enable
Task<OrganizationAbility?> GetOrganizationAbilityAsync(Guid orgId);
#nullable disable
[Obsolete("We are transitioning to a new cache pattern. Please consult the Admin Console team before using.", false)]
Task<IDictionary<Guid, ProviderAbility>> GetProviderAbilitiesAsync();
Task UpsertOrganizationAbilityAsync(Organization organization);
Task UpsertProviderAbilityAsync(Provider provider);

View File

@@ -6,147 +6,52 @@ using Bit.Core.Models.Data.Organizations;
namespace Bit.Core.Services.Implementations;
/// <summary>
/// A feature-flagged routing service for application caching that bridges the gap between
/// scoped dependency injection (IFeatureService) and singleton services (cache implementations).
/// This service allows dynamic routing between IVCurrentInMemoryApplicationCacheService and
/// IVNextInMemoryApplicationCacheService based on the PM23845_VNextApplicationCache feature flag.
/// </summary>
/// <remarks>
/// This service is necessary because:
/// - IFeatureService is registered as Scoped in the DI container
/// - IVNextInMemoryApplicationCacheService and IVCurrentInMemoryApplicationCacheService are registered as Singleton
/// - We need to evaluate feature flags at request time while maintaining singleton cache behavior
///
/// The service acts as a scoped proxy that can access the scoped IFeatureService while
/// delegating actual cache operations to the appropriate singleton implementation.
/// </remarks>
public class FeatureRoutedCacheService(
IFeatureService featureService,
IVNextInMemoryApplicationCacheService vNextInMemoryApplicationCacheService,
IVCurrentInMemoryApplicationCacheService inMemoryApplicationCacheService,
IApplicationCacheServiceBusMessaging serviceBusMessaging)
IVCurrentInMemoryApplicationCacheService inMemoryApplicationCacheService)
: IApplicationCacheService
{
public async Task<IDictionary<Guid, OrganizationAbility>> GetOrganizationAbilitiesAsync()
{
if (featureService.IsEnabled(FeatureFlagKeys.PM23845_VNextApplicationCache))
{
return await vNextInMemoryApplicationCacheService.GetOrganizationAbilitiesAsync();
}
public Task<IDictionary<Guid, OrganizationAbility>> GetOrganizationAbilitiesAsync() =>
inMemoryApplicationCacheService.GetOrganizationAbilitiesAsync();
return await inMemoryApplicationCacheService.GetOrganizationAbilitiesAsync();
}
public Task<OrganizationAbility?> GetOrganizationAbilityAsync(Guid orgId) =>
inMemoryApplicationCacheService.GetOrganizationAbilityAsync(orgId);
public async Task<OrganizationAbility?> GetOrganizationAbilityAsync(Guid orgId)
{
if (featureService.IsEnabled(FeatureFlagKeys.PM23845_VNextApplicationCache))
{
return await vNextInMemoryApplicationCacheService.GetOrganizationAbilityAsync(orgId);
}
return await inMemoryApplicationCacheService.GetOrganizationAbilityAsync(orgId);
}
public Task<IDictionary<Guid, ProviderAbility>> GetProviderAbilitiesAsync() =>
inMemoryApplicationCacheService.GetProviderAbilitiesAsync();
public async Task<IDictionary<Guid, ProviderAbility>> GetProviderAbilitiesAsync()
{
if (featureService.IsEnabled(FeatureFlagKeys.PM23845_VNextApplicationCache))
{
return await vNextInMemoryApplicationCacheService.GetProviderAbilitiesAsync();
}
return await inMemoryApplicationCacheService.GetProviderAbilitiesAsync();
}
public Task UpsertOrganizationAbilityAsync(Organization organization) =>
inMemoryApplicationCacheService.UpsertOrganizationAbilityAsync(organization);
public async Task UpsertOrganizationAbilityAsync(Organization organization)
{
if (featureService.IsEnabled(FeatureFlagKeys.PM23845_VNextApplicationCache))
{
await vNextInMemoryApplicationCacheService.UpsertOrganizationAbilityAsync(organization);
await serviceBusMessaging.NotifyOrganizationAbilityUpsertedAsync(organization);
}
else
{
await inMemoryApplicationCacheService.UpsertOrganizationAbilityAsync(organization);
}
}
public Task UpsertProviderAbilityAsync(Provider provider) =>
inMemoryApplicationCacheService.UpsertProviderAbilityAsync(provider);
public async Task UpsertProviderAbilityAsync(Provider provider)
{
if (featureService.IsEnabled(FeatureFlagKeys.PM23845_VNextApplicationCache))
{
await vNextInMemoryApplicationCacheService.UpsertProviderAbilityAsync(provider);
}
else
{
await inMemoryApplicationCacheService.UpsertProviderAbilityAsync(provider);
}
}
public Task DeleteOrganizationAbilityAsync(Guid organizationId) =>
inMemoryApplicationCacheService.DeleteOrganizationAbilityAsync(organizationId);
public async Task DeleteOrganizationAbilityAsync(Guid organizationId)
{
if (featureService.IsEnabled(FeatureFlagKeys.PM23845_VNextApplicationCache))
{
await vNextInMemoryApplicationCacheService.DeleteOrganizationAbilityAsync(organizationId);
await serviceBusMessaging.NotifyOrganizationAbilityDeletedAsync(organizationId);
}
else
{
await inMemoryApplicationCacheService.DeleteOrganizationAbilityAsync(organizationId);
}
}
public async Task DeleteProviderAbilityAsync(Guid providerId)
{
if (featureService.IsEnabled(FeatureFlagKeys.PM23845_VNextApplicationCache))
{
await vNextInMemoryApplicationCacheService.DeleteProviderAbilityAsync(providerId);
await serviceBusMessaging.NotifyProviderAbilityDeletedAsync(providerId);
}
else
{
await inMemoryApplicationCacheService.DeleteProviderAbilityAsync(providerId);
}
}
public Task DeleteProviderAbilityAsync(Guid providerId) =>
inMemoryApplicationCacheService.DeleteProviderAbilityAsync(providerId);
public async Task BaseUpsertOrganizationAbilityAsync(Organization organization)
{
if (featureService.IsEnabled(FeatureFlagKeys.PM23845_VNextApplicationCache))
if (inMemoryApplicationCacheService is InMemoryServiceBusApplicationCacheService serviceBusCache)
{
await vNextInMemoryApplicationCacheService.UpsertOrganizationAbilityAsync(organization);
await serviceBusCache.BaseUpsertOrganizationAbilityAsync(organization);
}
else
{
// NOTE: This is a temporary workaround InMemoryServiceBusApplicationCacheService legacy implementation.
// Avoid using this approach in new code.
if (inMemoryApplicationCacheService is InMemoryServiceBusApplicationCacheService serviceBusCache)
{
await serviceBusCache.BaseUpsertOrganizationAbilityAsync(organization);
}
else
{
throw new InvalidOperationException($"Expected {nameof(inMemoryApplicationCacheService)} to be of type {nameof(InMemoryServiceBusApplicationCacheService)}");
}
throw new InvalidOperationException($"Expected {nameof(inMemoryApplicationCacheService)} to be of type {nameof(InMemoryServiceBusApplicationCacheService)}");
}
}
public async Task BaseDeleteOrganizationAbilityAsync(Guid organizationId)
{
if (featureService.IsEnabled(FeatureFlagKeys.PM23845_VNextApplicationCache))
if (inMemoryApplicationCacheService is InMemoryServiceBusApplicationCacheService serviceBusCache)
{
await vNextInMemoryApplicationCacheService.DeleteOrganizationAbilityAsync(organizationId);
await serviceBusCache.BaseDeleteOrganizationAbilityAsync(organizationId);
}
else
{
// NOTE: This is a temporary workaround InMemoryServiceBusApplicationCacheService legacy implementation.
// Avoid using this approach in new code.
if (inMemoryApplicationCacheService is InMemoryServiceBusApplicationCacheService serviceBusCache)
{
await serviceBusCache.BaseDeleteOrganizationAbilityAsync(organizationId);
}
else
{
throw new InvalidOperationException($"Expected {nameof(inMemoryApplicationCacheService)} to be of type {nameof(InMemoryServiceBusApplicationCacheService)}");
}
throw new InvalidOperationException($"Expected {nameof(inMemoryApplicationCacheService)} to be of type {nameof(InMemoryServiceBusApplicationCacheService)}");
}
}
}

View File

@@ -56,17 +56,14 @@ public class Startup
var usingServiceBusAppCache = CoreHelpers.SettingHasValue(globalSettings.ServiceBus.ConnectionString) &&
CoreHelpers.SettingHasValue(globalSettings.ServiceBus.ApplicationCacheTopicName);
services.AddScoped<IApplicationCacheService, FeatureRoutedCacheService>();
services.AddSingleton<IVNextInMemoryApplicationCacheService, VNextInMemoryApplicationCacheService>();
if (usingServiceBusAppCache)
{
services.AddSingleton<IVCurrentInMemoryApplicationCacheService, InMemoryServiceBusApplicationCacheService>();
services.AddSingleton<IApplicationCacheServiceBusMessaging, ServiceBusApplicationCacheMessaging>();
}
else
{
services.AddSingleton<IVCurrentInMemoryApplicationCacheService, InMemoryApplicationCacheService>();
services.AddSingleton<IApplicationCacheServiceBusMessaging, NoOpApplicationCacheMessaging>();
}
services.AddEventWriteServices(globalSettings);

View File

@@ -292,19 +292,16 @@ public static class ServiceCollectionExtensions
services.AddOptionality();
services.AddTokenizers();
services.AddSingleton<IVNextInMemoryApplicationCacheService, VNextInMemoryApplicationCacheService>();
services.AddScoped<IApplicationCacheService, FeatureRoutedCacheService>();
if (CoreHelpers.SettingHasValue(globalSettings.ServiceBus.ConnectionString) &&
CoreHelpers.SettingHasValue(globalSettings.ServiceBus.ApplicationCacheTopicName))
{
services.AddSingleton<IVCurrentInMemoryApplicationCacheService, InMemoryServiceBusApplicationCacheService>();
services.AddSingleton<IApplicationCacheServiceBusMessaging, ServiceBusApplicationCacheMessaging>();
}
else
{
services.AddSingleton<IVCurrentInMemoryApplicationCacheService, InMemoryApplicationCacheService>();
services.AddSingleton<IApplicationCacheServiceBusMessaging, NoOpApplicationCacheMessaging>();
}
var awsConfigured = CoreHelpers.SettingHasValue(globalSettings.Amazon?.AccessKeySecret);