From ea977dc33bfafa6c4b08bee6ae268def97172489 Mon Sep 17 00:00:00 2001 From: Oleg Zhuk Date: Tue, 10 Dec 2024 14:42:34 +0200 Subject: [PATCH 1/2] VCST-2432: Add global tenant ID option for sequence number generation feat: Introduced a new `UseGlobalTenantId` property in the `SequenceNumberGeneratorOptions` class to support a global counter across all tenants/stores. By default, false. Set VirtoCommerce:SequenceNumberGenerator:UseGlobalTenantId to true, to enable it. --- .../Common/SequenceNumberGeneratorOptions.cs | 6 ++++++ .../Services/SequenceNumberGeneratorService.cs | 16 ++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/VirtoCommerce.CoreModule.Core/Common/SequenceNumberGeneratorOptions.cs b/src/VirtoCommerce.CoreModule.Core/Common/SequenceNumberGeneratorOptions.cs index 8b58b037..84356be4 100644 --- a/src/VirtoCommerce.CoreModule.Core/Common/SequenceNumberGeneratorOptions.cs +++ b/src/VirtoCommerce.CoreModule.Core/Common/SequenceNumberGeneratorOptions.cs @@ -10,5 +10,11 @@ public class SequenceNumberGeneratorOptions /// Defines the delay between retries in seconds. /// public int RetryDelay { get; set; } = 5; + + /// + /// Defines usage of the static tenant id instead of store id for generating unique number. + /// Can be useful when you want to have golobal counter for all stores/tenants. + /// + public bool UseGlobalTenantId { get; set; } = false; } } diff --git a/src/VirtoCommerce.CoreModule.Data/Services/SequenceNumberGeneratorService.cs b/src/VirtoCommerce.CoreModule.Data/Services/SequenceNumberGeneratorService.cs index 805a5b0a..48a6a62f 100644 --- a/src/VirtoCommerce.CoreModule.Data/Services/SequenceNumberGeneratorService.cs +++ b/src/VirtoCommerce.CoreModule.Data/Services/SequenceNumberGeneratorService.cs @@ -141,7 +141,7 @@ protected virtual RetryPolicy ConfigureRetryPolicy() /// protected virtual int RequestNextCounter(string tenantId, CounterOptions counterOptions) { - var sequenceKey = string.IsNullOrEmpty(tenantId) ? counterOptions.NumberTemplate : $"{tenantId}/{counterOptions.NumberTemplate}"; + var sequenceKey = GetSequenceKey(tenantId, counterOptions); using var repository = _repositoryFactory(); var sequence = repository.Sequences.SingleOrDefault(s => s.ObjectType == sequenceKey); @@ -177,6 +177,18 @@ protected virtual int RequestNextCounter(string tenantId, CounterOptions counter return sequence.Value; } + protected virtual string GetSequenceKey(string tenantId, CounterOptions counterOptions) + { + if(_options.UseGlobalTenantId) + { + tenantId = "__GlobalTenant"; + } + + return string.IsNullOrEmpty(tenantId) ? + counterOptions.NumberTemplate : + $"{tenantId}/{counterOptions.NumberTemplate}"; + } + /// /// Returns true if counter should be reset. /// @@ -193,7 +205,7 @@ protected virtual bool ShouldResetCounter(DateTime lastResetDate, ResetCounterTy return currentUtcDate.Date > lastResetDate.Date; case ResetCounterType.Weekly: // Reset every Monday - int daysUntilTargetDay = ((int)DayOfWeek.Monday - (int)lastResetDate.DayOfWeek + 7) % 7; + var daysUntilTargetDay = ((int)DayOfWeek.Monday - (int)lastResetDate.DayOfWeek + 7) % 7; var nextMondayDate = lastResetDate.Date.AddDays(daysUntilTargetDay); return currentUtcDate >= nextMondayDate; case ResetCounterType.Monthly: From 44863cb5da37c77f57c650d3464354d48ecee82f Mon Sep 17 00:00:00 2001 From: Oleg Zhuk Date: Tue, 10 Dec 2024 14:54:32 +0200 Subject: [PATCH 2/2] fix --- .../Services/SequenceNumberGeneratorService.cs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/VirtoCommerce.CoreModule.Data/Services/SequenceNumberGeneratorService.cs b/src/VirtoCommerce.CoreModule.Data/Services/SequenceNumberGeneratorService.cs index 48a6a62f..9fec7208 100644 --- a/src/VirtoCommerce.CoreModule.Data/Services/SequenceNumberGeneratorService.cs +++ b/src/VirtoCommerce.CoreModule.Data/Services/SequenceNumberGeneratorService.cs @@ -179,14 +179,12 @@ protected virtual int RequestNextCounter(string tenantId, CounterOptions counter protected virtual string GetSequenceKey(string tenantId, CounterOptions counterOptions) { - if(_options.UseGlobalTenantId) + if(_options.UseGlobalTenantId || string.IsNullOrEmpty(tenantId)) { - tenantId = "__GlobalTenant"; + return counterOptions.NumberTemplate; } - return string.IsNullOrEmpty(tenantId) ? - counterOptions.NumberTemplate : - $"{tenantId}/{counterOptions.NumberTemplate}"; + return $"{tenantId}/{counterOptions.NumberTemplate}"; } ///