Skip to content

Commit

Permalink
Feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
brendankowitz committed Sep 5, 2024
1 parent 92e84ec commit db42bec
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class ResourceManagerCollectionSetup : ICollectionSetup
private CosmosDBSqlDatabaseResource _database;
private AzureLocation? _location;
private readonly CosmosDBAccountResource _account;
private const string FhirServerResourceManagerDataStoreResourceId = "FhirServer:ResourceManager:DataStoreResourceId";

public ResourceManagerCollectionSetup(
IOptionsMonitor<CosmosCollectionConfiguration> cosmosCollectionConfiguration,
Expand All @@ -55,8 +56,12 @@ public ResourceManagerCollectionSetup(
_storeProceduresMetadata = EnsureArg.IsNotNull(storedProcedures, nameof(storedProcedures));
_logger = EnsureArg.IsNotNull(logger, nameof(logger));

var dataStoreResourceId = EnsureArg.IsNotNullOrWhiteSpace(
genericConfiguration.GetValue(FhirServerResourceManagerDataStoreResourceId, string.Empty),
nameof(genericConfiguration),
fn => fn.WithMessage($"{FhirServerResourceManagerDataStoreResourceId} must be set."));

_armClient = new ArmClient(new DefaultAzureCredential());
var dataStoreResourceId = genericConfiguration.GetValue("FhirServer:ResourceManager:DataStoreResourceId", string.Empty);
_resourceIdentifier = ResourceIdentifier.Parse(dataStoreResourceId);
_account = _armClient.GetCosmosDBAccountResource(_resourceIdentifier);
}
Expand All @@ -75,6 +80,9 @@ private CosmosDBSqlDatabaseResource Database
}
}

/// <summary>
/// Reads the location from an existing CosmosDB account.
/// </summary>
private AzureLocation Location
{
get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,18 +133,13 @@ private async Task InitializeDataStoreAsync(
await collectionSetup.CreateCollectionAsync(collectionInitializers, retryPolicyFactory.RetryPolicy, cancellationTokenSource.Token);
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Error while checking if Cosmos setup is required.");
}

// When the collection exists we can start a distributed lock to ensure only one instance of the service does the rest of the setup
ICosmosDbDistributedLock setupLock = _distributedLockFactory.Create(_container.Value, nameof(InitializeDataStoreAsync));

await setupLock.AcquireLock(cancellationTokenSource.Token);
try
{
await setupLock.AcquireLock(cancellationTokenSource.Token);

if (cosmosDataStoreConfiguration.AllowCollectionSetup)
{
await collectionSetup.InstallStoredProcs(cancellationTokenSource.Token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,16 @@ private static IFhirServerBuilder AddCosmosDbPersistence(this IFhirServerBuilder
services.Add(c =>
{
CosmosDataStoreConfiguration config = c.GetRequiredService<CosmosDataStoreConfiguration>();
return config.UseManagedIdentity
? (ICollectionSetup)c.GetRequiredService<ResourceManagerCollectionSetup>()
: c.GetRequiredService<DataPlaneCollectionSetup>();
if (config.AllowDatabaseCreation || config.AllowCollectionSetup)
{
return config.UseManagedIdentity
? (ICollectionSetup)c.GetRequiredService<ResourceManagerCollectionSetup>()
: c.GetRequiredService<DataPlaneCollectionSetup>();
}
// If collection setup is not required then return the noop implementation.
return new DefaultCollectionSetup();
})
.Singleton()
.AsService<ICollectionSetup>();
Expand Down
3 changes: 2 additions & 1 deletion src/Microsoft.Health.Fhir.Shared.Web/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,9 @@
"CosmosDb": {
"Host": null,
"Key": null,
"UseManagedIdentity": false,
"UseManagedIdentity": false, // False for localhost/emulator
"AllowDatabaseCreation": true,
"AllowCollectionSetup": true,
"DatabaseId": "health",
"InitialDatabaseThroughput": null,
"ConnectionMode": "Direct",
Expand Down

0 comments on commit db42bec

Please sign in to comment.