-
Notifications
You must be signed in to change notification settings - Fork 581
Expand file tree
/
Copy pathSearchParameterOperations.cs
More file actions
439 lines (381 loc) · 23.2 KB
/
SearchParameterOperations.cs
File metadata and controls
439 lines (381 loc) · 23.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
// -------------------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// -------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using EnsureThat;
using Hl7.Fhir.ElementModel;
using Microsoft.Extensions.Logging;
using Microsoft.Health.Extensions.DependencyInjection;
using Microsoft.Health.Fhir.Core.Exceptions;
using Microsoft.Health.Fhir.Core.Extensions;
using Microsoft.Health.Fhir.Core.Features.Definition;
using Microsoft.Health.Fhir.Core.Features.Definition.BundleWrappers;
using Microsoft.Health.Fhir.Core.Features.Persistence;
using Microsoft.Health.Fhir.Core.Features.Search.Registry;
using Microsoft.Health.Fhir.Core.Models;
namespace Microsoft.Health.Fhir.Core.Features.Search.Parameters
{
public class SearchParameterOperations : ISearchParameterOperations
{
private readonly ISearchParameterDefinitionManager _searchParameterDefinitionManager;
private readonly SearchParameterStatusManager _searchParameterStatusManager;
private readonly IModelInfoProvider _modelInfoProvider;
private readonly ISearchParameterSupportResolver _searchParameterSupportResolver;
private readonly IDataStoreSearchParameterValidator _dataStoreSearchParameterValidator;
private readonly Func<IScoped<ISearchService>> _searchServiceFactory;
private readonly ILogger _logger;
private DateTimeOffset? _searchParamLastUpdated;
public SearchParameterOperations(
SearchParameterStatusManager searchParameterStatusManager,
ISearchParameterDefinitionManager searchParameterDefinitionManager,
IModelInfoProvider modelInfoProvider,
ISearchParameterSupportResolver searchParameterSupportResolver,
IDataStoreSearchParameterValidator dataStoreSearchParameterValidator,
Func<IScoped<ISearchService>> searchServiceFactory,
ILogger<SearchParameterOperations> logger)
{
EnsureArg.IsNotNull(searchParameterStatusManager, nameof(searchParameterStatusManager));
EnsureArg.IsNotNull(searchParameterDefinitionManager, nameof(searchParameterDefinitionManager));
EnsureArg.IsNotNull(modelInfoProvider, nameof(modelInfoProvider));
EnsureArg.IsNotNull(searchParameterSupportResolver, nameof(searchParameterSupportResolver));
EnsureArg.IsNotNull(dataStoreSearchParameterValidator, nameof(dataStoreSearchParameterValidator));
EnsureArg.IsNotNull(searchServiceFactory, nameof(searchServiceFactory));
EnsureArg.IsNotNull(logger, nameof(logger));
_searchParameterStatusManager = searchParameterStatusManager;
_searchParameterDefinitionManager = searchParameterDefinitionManager;
_modelInfoProvider = modelInfoProvider;
_searchParameterSupportResolver = searchParameterSupportResolver;
_dataStoreSearchParameterValidator = dataStoreSearchParameterValidator;
_searchServiceFactory = searchServiceFactory;
_logger = logger;
}
public DateTimeOffset? SearchParamLastUpdated => _searchParamLastUpdated;
public string GetSearchParameterHash(string resourceType)
{
EnsureArg.IsNotNullOrWhiteSpace(resourceType, nameof(resourceType));
if (_searchParameterDefinitionManager?.SearchParameterHashMap == null)
{
return null;
}
else
{
return _searchParameterDefinitionManager.SearchParameterHashMap.TryGetValue(resourceType, out string hash) ? hash : null;
}
}
public async Task AddSearchParameterAsync(ITypedElement searchParam, CancellationToken cancellationToken)
{
var searchParameterWrapper = new SearchParameterWrapper(searchParam);
var searchParameterUrl = searchParameterWrapper.Url;
await SearchParameterConcurrencyManager.ExecuteWithLockAsync(
searchParameterUrl,
async () =>
{
try
{
// We need to make sure we have the latest search parameters before trying to add
// a search parameter. This is to avoid creating a duplicate search parameter that
// was recently added and that hasn't propogated to all fhir-server instances.
await GetAndApplySearchParameterUpdates(cancellationToken);
// verify the parameter is supported before continuing
var searchParameterInfo = new SearchParameterInfo(searchParameterWrapper);
if (searchParameterInfo.Component?.Any() == true)
{
foreach (SearchParameterComponentInfo c in searchParameterInfo.Component)
{
c.ResolvedSearchParameter = _searchParameterDefinitionManager.GetSearchParameter(c.DefinitionUrl.OriginalString);
}
}
(bool Supported, bool IsPartiallySupported) supportedResult = _searchParameterSupportResolver.IsSearchParameterSupported(searchParameterInfo);
if (!supportedResult.Supported)
{
throw new SearchParameterNotSupportedException(string.Format(Core.Resources.NoConverterForSearchParamType, searchParameterInfo.Type, searchParameterInfo.Expression));
}
// check data store specific support for SearchParameter
if (!_dataStoreSearchParameterValidator.ValidateSearchParameter(searchParameterInfo, out var errorMessage))
{
throw new SearchParameterNotSupportedException(errorMessage);
}
_logger.LogInformation("Adding the search parameter '{Url}'", searchParameterWrapper.Url);
_searchParameterDefinitionManager.AddNewSearchParameters(new List<ITypedElement> { searchParam });
await _searchParameterStatusManager.AddSearchParameterStatusAsync(new List<string> { searchParameterWrapper.Url }, cancellationToken);
}
catch (FhirException fex)
{
_logger.LogError(fex, "Error adding search parameter.");
fex.Issues.Add(new OperationOutcomeIssue(
OperationOutcomeConstants.IssueSeverity.Error,
OperationOutcomeConstants.IssueType.Exception,
Core.Resources.CustomSearchCreateError));
throw;
}
catch (Exception ex)
{
_logger.LogError(ex, "Unexpected error adding search parameter.");
var customSearchException = new ConfigureCustomSearchException(Core.Resources.CustomSearchCreateError);
customSearchException.Issues.Add(new OperationOutcomeIssue(
OperationOutcomeConstants.IssueSeverity.Error,
OperationOutcomeConstants.IssueType.Exception,
ex.Message));
throw customSearchException;
}
},
_logger,
cancellationToken);
}
/// <summary>
/// Marks the Search Parameter as PendingDelete.
/// </summary>
/// <param name="searchParamResource">Search Parameter to update to Pending Delete status.</param>
/// <param name="cancellationToken">Cancellation Token</param>
/// <param name="ignoreSearchParameterNotSupportedException">The value indicating whether to ignore SearchParameterNotSupportedException.</param>
public async Task DeleteSearchParameterAsync(RawResource searchParamResource, CancellationToken cancellationToken, bool ignoreSearchParameterNotSupportedException = false)
{
var searchParam = _modelInfoProvider.ToTypedElement(searchParamResource);
var searchParameterUrl = searchParam.GetStringScalar("url");
await SearchParameterConcurrencyManager.ExecuteWithLockAsync(
searchParameterUrl,
async () =>
{
try
{
// We need to make sure we have the latest search parameters before trying to delete
// existing search parameter. This is to avoid trying to update a search parameter that
// was recently added and that hasn't propogated to all fhir-server instances.
await GetAndApplySearchParameterUpdates(cancellationToken);
// First we delete the status metadata from the data store as this function depends on
// the in memory definition manager. Once complete we remove the SearchParameter from
// the definition manager.
_logger.LogInformation("Deleting the search parameter '{Url}'", searchParameterUrl);
await _searchParameterStatusManager.UpdateSearchParameterStatusAsync(new List<string>() { searchParameterUrl }, SearchParameterStatus.PendingDelete, cancellationToken, ignoreSearchParameterNotSupportedException);
// Update the status of the search parameter in the definition manager once the status is updated in the store.
_searchParameterDefinitionManager.UpdateSearchParameterStatus(searchParameterUrl, SearchParameterStatus.PendingDelete);
}
catch (FhirException fex)
{
_logger.LogError(fex, "Error deleting search parameter.");
fex.Issues.Add(new OperationOutcomeIssue(
OperationOutcomeConstants.IssueSeverity.Error,
OperationOutcomeConstants.IssueType.Exception,
Core.Resources.CustomSearchDeleteError));
throw;
}
catch (Exception ex) when (!(ex is FhirException))
{
_logger.LogError(ex, "Unexpected error deleting search parameter.");
var customSearchException = new ConfigureCustomSearchException(Core.Resources.CustomSearchDeleteError);
customSearchException.Issues.Add(new OperationOutcomeIssue(
OperationOutcomeConstants.IssueSeverity.Error,
OperationOutcomeConstants.IssueType.Exception,
ex.Message));
throw customSearchException;
}
},
_logger,
cancellationToken);
}
public async Task UpdateSearchParameterAsync(ITypedElement searchParam, RawResource previousSearchParam, CancellationToken cancellationToken)
{
var prevSearchParam = _modelInfoProvider.ToTypedElement(previousSearchParam);
var prevSearchParamUrl = prevSearchParam.GetStringScalar("url");
await SearchParameterConcurrencyManager.ExecuteWithLockAsync(
prevSearchParamUrl,
async () =>
{
try
{
// We need to make sure we have the latest search parameters before trying to update
// existing search parameter. This is to avoid trying to update a search parameter that
// was recently added and that hasn't propogated to all fhir-server instances.
await GetAndApplySearchParameterUpdates(cancellationToken);
var searchParameterWrapper = new SearchParameterWrapper(searchParam);
var searchParameterInfo = new SearchParameterInfo(searchParameterWrapper);
(bool Supported, bool IsPartiallySupported) supportedResult = _searchParameterSupportResolver.IsSearchParameterSupported(searchParameterInfo);
if (!supportedResult.Supported)
{
throw new SearchParameterNotSupportedException(searchParameterInfo.Url);
}
// check data store specific support for SearchParameter
if (!_dataStoreSearchParameterValidator.ValidateSearchParameter(searchParameterInfo, out var errorMessage))
{
throw new SearchParameterNotSupportedException(errorMessage);
}
// As any part of the SearchParameter may have been changed, including the URL
// the most reliable method of updating the SearchParameter is to delete the previous
// data and insert the updated version
if (!searchParameterWrapper.Url.Equals(prevSearchParamUrl, StringComparison.Ordinal))
{
_logger.LogInformation("Deleting the search parameter '{Url}' (update step 1/2)", prevSearchParamUrl);
await _searchParameterStatusManager.DeleteSearchParameterStatusAsync(prevSearchParamUrl, cancellationToken);
try
{
_searchParameterDefinitionManager.DeleteSearchParameter(prevSearchParam);
}
catch (ResourceNotFoundException)
{
// do nothing, there may not be a search parameter to remove
}
}
_logger.LogInformation("Adding the search parameter '{Url}' (update step 2/2)", searchParameterWrapper.Url);
_searchParameterDefinitionManager.AddNewSearchParameters(new List<ITypedElement>() { searchParam });
await _searchParameterStatusManager.AddSearchParameterStatusAsync(new List<string>() { searchParameterWrapper.Url }, cancellationToken);
}
catch (FhirException fex)
{
_logger.LogError(fex, "Error updating search parameter.");
fex.Issues.Add(new OperationOutcomeIssue(
OperationOutcomeConstants.IssueSeverity.Error,
OperationOutcomeConstants.IssueType.Exception,
Core.Resources.CustomSearchUpdateError));
throw;
}
catch (Exception ex) when (!(ex is FhirException))
{
_logger.LogError(ex, "Unexpected error updating search parameter.");
var customSearchException = new ConfigureCustomSearchException(Core.Resources.CustomSearchUpdateError);
customSearchException.Issues.Add(new OperationOutcomeIssue(
OperationOutcomeConstants.IssueSeverity.Error,
OperationOutcomeConstants.IssueType.Exception,
ex.Message));
throw customSearchException;
}
},
_logger,
cancellationToken);
}
/// <summary>
/// This method should be called periodically to get any updates to SearchParameters
/// added to the DB by other service instances.
/// It should also be called when a user starts a reindex job
/// </summary>
/// <param name="cancellationToken">Cancellation token</param>
/// <param name="forceFullRefresh">When true, forces a full refresh from database instead of incremental updates</param>
/// <returns>A task.</returns>
public async Task GetAndApplySearchParameterUpdates(CancellationToken cancellationToken = default, bool forceFullRefresh = false)
{
var results = await _searchParameterStatusManager.GetSearchParameterStatusUpdates(cancellationToken, forceFullRefresh ? null : _searchParamLastUpdated);
var statuses = results.Statuses;
// First process any deletes or disables, then we will do any adds or updates
// this way any deleted or params which might have the same code or name as a new
// parameter will not cause conflicts. Disabled params just need to be removed when calculating the hash.
foreach (var searchParam in statuses.Where(p => p.Status == SearchParameterStatus.Deleted))
{
DeleteSearchParameter(searchParam.Uri.OriginalString);
}
foreach (var searchParam in statuses.Where(p => p.Status == SearchParameterStatus.PendingDelete))
{
_searchParameterDefinitionManager.UpdateSearchParameterStatus(searchParam.Uri.OriginalString, SearchParameterStatus.PendingDelete);
}
var statusesToProcess = statuses.Where(p => p.Status == SearchParameterStatus.Enabled || p.Status == SearchParameterStatus.Supported).ToList();
// Batch fetch all SearchParameter resources in one call
var searchParamResources = await GetSearchParametersByUrls(statusesToProcess.Select(p => p.Uri.OriginalString).ToList(), cancellationToken);
var paramsToAdd = new List<ITypedElement>();
foreach (var searchParam in statusesToProcess)
{
if (!searchParamResources.TryGetValue(searchParam.Uri.OriginalString, out var searchParamResource))
{
_logger.LogInformation(
"Updated SearchParameter status found for SearchParameter: {Url}, but did not find any SearchParameter resources when querying for this url.",
searchParam.Uri);
continue;
}
// check if search param is in cache and add if does not exist
if (_searchParameterDefinitionManager.TryGetSearchParameter(searchParam.Uri.OriginalString, out var existingSearchParam))
{
// if the previous version of the search parameter exists we should delete the old information currently stored
DeleteSearchParameter(searchParam.Uri.OriginalString);
}
paramsToAdd.Add(searchParamResource);
}
// Now add the new or updated parameters to the SearchParameterDefinitionManager
if (paramsToAdd.Any())
{
_searchParameterDefinitionManager.AddNewSearchParameters(paramsToAdd);
}
// Once added to the definition manager we can update their status
await _searchParameterStatusManager.ApplySearchParameterStatus(statuses, cancellationToken);
var inCache = await ParametersAreInCache(statusesToProcess, cancellationToken);
if (results.LastUpdated.HasValue && inCache) // this should be the ony place in the code to assign last updated
{
_searchParamLastUpdated = results.LastUpdated.Value;
}
}
// This should handle racing condition between saving new parameter on one VM and refreshing cache on the other,
// when refresh is invoked between saving status and saving resource.
// This will not be needed when order of saves is reversed (resource first, then status)
private async Task<bool> ParametersAreInCache(IReadOnlyCollection<ResourceSearchParameterStatus> statuses, CancellationToken cancellationToken)
{
var inCache = true;
foreach (var status in statuses)
{
_searchParameterDefinitionManager.TryGetSearchParameter(status.Uri.OriginalString, out var existingSearchParam);
using IScoped<ISearchService> search = _searchServiceFactory.Invoke();
if (existingSearchParam == null)
{
inCache = false;
var msg = $"Did not find in cache uri={status.Uri.OriginalString} status={status.Status}";
_logger.LogInformation(msg);
await search.Value.TryLogEvent("SearchParameterOperations.GetAndApplySearchParameterUpdates", "Error", msg, null, cancellationToken);
}
}
return inCache;
}
private void DeleteSearchParameter(string url)
{
try
{
_searchParameterDefinitionManager.DeleteSearchParameter(url, false);
}
catch (ResourceNotFoundException)
{
// do nothing, there may not be a search parameter to remove
}
}
private async Task<Dictionary<string, ITypedElement>> GetSearchParametersByUrls(List<string> urls, CancellationToken cancellationToken)
{
if (!urls.Any())
{
return new Dictionary<string, ITypedElement>();
}
const int chunkSize = 1500;
var searchParametersByUrl = new Dictionary<string, ITypedElement>();
// Process URLs in chunks to avoid SQL query limitations
for (int i = 0; i < urls.Count; i += chunkSize)
{
var urlChunk = urls.Skip(i).Take(chunkSize).ToList();
using IScoped<ISearchService> search = _searchServiceFactory.Invoke();
// Build a query like: url=url1,url2,url3
var urlQueryValue = string.Join(",", urlChunk);
var queryParams = new List<Tuple<string, string>>
{
new Tuple<string, string>("url", urlQueryValue),
};
var result = await search.Value.SearchAsync(KnownResourceTypes.SearchParameter, queryParams, cancellationToken);
if (result != null)
{
foreach (var searchResultEntry in result.Results)
{
var typedElement = searchResultEntry.Resource.RawResource.ToITypedElement(_modelInfoProvider);
var url = typedElement.GetStringScalar("url");
if (!string.IsNullOrEmpty(url))
{
if (!searchParametersByUrl.ContainsKey(url))
{
searchParametersByUrl[url] = typedElement;
}
else
{
_logger.LogWarning("More than one SearchParameter found with url {Url}. Using the first one found.", url);
}
}
}
}
}
return searchParametersByUrl;
}
}
}