From 0c29e5e85b5618407a093bb30a87a641ae28fde2 Mon Sep 17 00:00:00 2001 From: Sergey Galuzo Date: Mon, 27 Apr 2026 14:46:55 -0700 Subject: [PATCH 1/2] Removed incorrect retries Removed reads from mergesearch params --- ...SqlServerSearchParameterStatusDataStore.cs | 70 +------------------ 1 file changed, 2 insertions(+), 68 deletions(-) diff --git a/src/Microsoft.Health.Fhir.SqlServer/Features/Storage/Registry/SqlServerSearchParameterStatusDataStore.cs b/src/Microsoft.Health.Fhir.SqlServer/Features/Storage/Registry/SqlServerSearchParameterStatusDataStore.cs index c49b847a22..5f35503cb5 100644 --- a/src/Microsoft.Health.Fhir.SqlServer/Features/Storage/Registry/SqlServerSearchParameterStatusDataStore.cs +++ b/src/Microsoft.Health.Fhir.SqlServer/Features/Storage/Registry/SqlServerSearchParameterStatusDataStore.cs @@ -136,80 +136,14 @@ public async Task UpsertStatuses(IReadOnlyCollection statuses, int maxRetries, CancellationToken cancellationToken) - { - var currentStatuses = statuses.ToList(); - int retryCount = 0; - - while (retryCount <= maxRetries) - { - try - { - await UpsertStatusesInternal(currentStatuses, cancellationToken); - return; // Success - } - catch (SqlException sqlEx) when (sqlEx.Number == 50001 && retryCount < maxRetries) // Our custom concurrency error - { - // Optimistic concurrency conflict detected - refresh and retry - retryCount++; - _logger.LogWarning("Optimistic concurrency conflict detected on attempt {RetryCount}. Retrying...", retryCount); - - // Refresh the statuses with current LastUpdated values - var refreshedStatuses = await GetSearchParameterStatuses(cancellationToken); - var refreshedDict = refreshedStatuses.ToDictionary(s => s.Uri.OriginalString, s => s); - - // Update our statuses with fresh LastUpdated values - foreach (var status in currentStatuses) - { - if (refreshedDict.TryGetValue(status.Uri.OriginalString, out var refreshed)) - { - status.LastUpdated = refreshed.LastUpdated; - } - } - - // Wait before retry to reduce contention - await Task.Delay(TimeSpan.FromMilliseconds(100.0 * retryCount), cancellationToken); - } - catch (SqlException sqlEx) when (sqlEx.Number == 50001) - { - // Max retries exceeded - throw new SearchParameterConcurrencyException("Maximum retry attempts exceeded due to concurrency conflicts", sqlEx); - } - } - } - - private async Task UpsertStatusesInternal(IReadOnlyCollection statuses, CancellationToken cancellationToken) - { using var cmd = new SqlCommand(); cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "dbo.MergeSearchParams"; new SearchParamListTableValuedParameterDefinition("@SearchParams").AddParameter(cmd.Parameters, new SearchParamListRowGenerator().GenerateRows(statuses.ToList())); - - var results = await cmd.ExecuteReaderAsync( - _sqlRetryService, - (reader) => { return reader.ReadRow(VLatest.SearchParam.SearchParamId, VLatest.SearchParam.Uri, VLatest.SearchParam.LastUpdated); }, - _logger, - cancellationToken); - - foreach (var result in results) - { - (short searchParamId, string searchParamUri, DateTimeOffset lastUpdated) = result; - - // Add the new search parameters to the FHIR model dictionary. - _fhirModel.TryAddSearchParamIdToUriMapping(searchParamUri, searchParamId); - - // Update the LastUpdated in our original collection for future operations - var matchingStatus = statuses.FirstOrDefault(s => s.Uri.OriginalString == searchParamUri); - if (matchingStatus != null) - { - matchingStatus.LastUpdated = lastUpdated; - } - } + await cmd.ExecuteNonQueryAsync(_sqlRetryService, _logger, cancellationToken); } + // Synchronize the FHIR model dictionary with the data in SQL search parameter status table public void SyncStatuses(IReadOnlyCollection statuses) { From 1242c483b6c4139f32edc03c1839250600b99a45 Mon Sep 17 00:00:00 2001 From: Sergey Galuzo Date: Tue, 28 Apr 2026 11:02:37 -0700 Subject: [PATCH 2/2] 2 CRs -> 1 CR --- .../Storage/Registry/SqlServerSearchParameterStatusDataStore.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Microsoft.Health.Fhir.SqlServer/Features/Storage/Registry/SqlServerSearchParameterStatusDataStore.cs b/src/Microsoft.Health.Fhir.SqlServer/Features/Storage/Registry/SqlServerSearchParameterStatusDataStore.cs index 5f35503cb5..807b33cf3f 100644 --- a/src/Microsoft.Health.Fhir.SqlServer/Features/Storage/Registry/SqlServerSearchParameterStatusDataStore.cs +++ b/src/Microsoft.Health.Fhir.SqlServer/Features/Storage/Registry/SqlServerSearchParameterStatusDataStore.cs @@ -143,7 +143,6 @@ public async Task UpsertStatuses(IReadOnlyCollection statuses) {