Skip to content

Commit

Permalink
Adds support for consistency headers (#8)
Browse files Browse the repository at this point in the history
The service now supports x-ms-consistency-level and x-ms-session-token HTTP headers for controlling Cosmos DB consistency. There is also a configuration setting for the default consistency level to use.

We're adding the HTTP request and response headers to the IFhirContext, and a DocumentClientWithConsistencyLevelFromContext class, which implements IDocumentClient, is responsible biding a database request's consistency settings with the IFhirContext's headers.

Most of DocumentClientWithConsistencyLevelFromContext is generated at build time (with incremental build awareness) with a tool. This tool uses Roslyn to generate the code and can be used in the future for other coge generation needs (such as an instrumented IDocumentClient for telemetry).
  • Loading branch information
johnstairs authored Sep 21, 2018
1 parent 1848a29 commit 1528b10
Show file tree
Hide file tree
Showing 24 changed files with 2,288 additions and 41 deletions.
11 changes: 10 additions & 1 deletion Microsoft.Health.Fhir.sln
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Health.Extensions
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Health.Extensions.DependencyInjection.UnitTests", "src\Microsoft.Health.Extensions.DependencyInjection.UnitTests\Microsoft.Health.Extensions.DependencyInjection.UnitTests.csproj", "{614DE061-52A0-4471-A3F1-50C9C907FFDB}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tools", "tools", "{B70945F4-01A6-4351-955B-C4A2943B5E3B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Health.Extensions.BuildTimeCodeGenerator", "tools\Microsoft.Health.Extensions.BuildTimeCodeGenerator\Microsoft.Health.Extensions.BuildTimeCodeGenerator.csproj", "{CA276939-8071-4734-9FE4-ADC825B72116}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -91,6 +95,10 @@ Global
{614DE061-52A0-4471-A3F1-50C9C907FFDB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{614DE061-52A0-4471-A3F1-50C9C907FFDB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{614DE061-52A0-4471-A3F1-50C9C907FFDB}.Release|Any CPU.Build.0 = Release|Any CPU
{CA276939-8071-4734-9FE4-ADC825B72116}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CA276939-8071-4734-9FE4-ADC825B72116}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CA276939-8071-4734-9FE4-ADC825B72116}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CA276939-8071-4734-9FE4-ADC825B72116}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -109,9 +117,10 @@ Global
{C015551C-0009-49CE-8C8D-B206BED0BB42} = {8AD2A324-DAB5-4380-94A5-31F7D817C384}
{E2C2B117-32CD-449F-A4A8-4C8BA874D693} = {8AD2A324-DAB5-4380-94A5-31F7D817C384}
{614DE061-52A0-4471-A3F1-50C9C907FFDB} = {8AD2A324-DAB5-4380-94A5-31F7D817C384}
{CA276939-8071-4734-9FE4-ADC825B72116} = {B70945F4-01A6-4351-955B-C4A2943B5E3B}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
RESX_SortFileContentOnSave = True
SolutionGuid = {E370FB31-CF95-47D1-B1E1-863A77973FF8}
RESX_SortFileContentOnSave = True
EndGlobalSection
EndGlobal
22 changes: 16 additions & 6 deletions samples/templates/default-azuredeploy.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@
"metadata": {
"description": "Source code branch to deploy."
}
},
"cosmosDbAccountConsistencyPolicy": {
"type": "object",
"defaultValue": {
"defaultConsistencyLevel": "Strong"
},
"metadata": {
"description": "An object representing the default consistency policy for the Cosmos DB account. See https://docs.microsoft.com/en-us/azure/templates/microsoft.documentdb/databaseaccounts#ConsistencyPolicy"
}
}
},
"variables": {
Expand Down Expand Up @@ -154,14 +163,14 @@
"type": "sourcecontrols",
"condition": "[variables('deploySourceCode')]",
"dependsOn": [
"[resourceId('Microsoft.Web/Sites', variables('serviceName'))]"
"[resourceId('Microsoft.Web/Sites', variables('serviceName'))]"
],
"properties": {
"RepoUrl": "[parameters('repositoryUrl')]",
"branch": "[parameters('repositoryBranch')]",
"IsManualIntegration": true
"RepoUrl": "[parameters('repositoryUrl')]",
"branch": "[parameters('repositoryBranch')]",
"IsManualIntegration": true
}
}
}
]
},
{
Expand All @@ -172,6 +181,7 @@
"properties": {
"name": "[variables('serviceName')]",
"databaseAccountOfferType": "Standard",
"consistencyPolicy": "[parameters('cosmosDbAccountConsistencyPolicy')]",
"locations": [
{
"locationName": "[resourceGroup().location]",
Expand Down Expand Up @@ -237,4 +247,4 @@
]
}
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ public Task Invoke(HttpContext context, IFhirRequestContextAccessor fhirRequestC
request.QueryString);

var fhirRequestContext = new FhirRequestContext(
request.Method,
uriInString,
baseUriInString,
ValueSets.AuditEventType.RestFulOperation,
correlationIdProvider.Invoke());
method: request.Method,
uriString: uriInString,
baseUriString: baseUriInString,
requestType: ValueSets.AuditEventType.RestFulOperation,
correlationId: correlationIdProvider.Invoke(),
requestHeaders: context.Request.Headers,
responseHeaders: context.Response.Headers);

if (context.User != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
// -------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Security.Claims;
using System.Threading;
using EnsureThat;
using Hl7.Fhir.Model;
using Microsoft.Extensions.Primitives;

namespace Microsoft.Health.Fhir.Core.Features.Context
{
Expand All @@ -25,38 +26,32 @@ public FhirRequestContext(
string uriString,
string baseUriString,
Coding requestType,
string correlationId)
string correlationId,
IDictionary<string, StringValues> requestHeaders,
IDictionary<string, StringValues> responseHeaders)
{
EnsureArg.IsNotNullOrWhiteSpace(method, nameof(method));
EnsureArg.IsNotNullOrWhiteSpace(uriString, nameof(uriString));
EnsureArg.IsNotNullOrWhiteSpace(baseUriString, nameof(baseUriString));
EnsureArg.IsNotNull(requestType, nameof(requestType));
EnsureArg.IsNotNullOrWhiteSpace(correlationId, nameof(correlationId));
EnsureArg.IsNotNull(responseHeaders, nameof(responseHeaders));
EnsureArg.IsNotNull(requestHeaders, nameof(requestHeaders));

Method = method;
_uriString = uriString;
_baseUriString = baseUriString;
RequestType = requestType;
CorrelationId = correlationId;
RequestHeaders = requestHeaders;
ResponseHeaders = responseHeaders;
}

public string Method { get; }

public Uri BaseUri
{
get
{
return _baseUri ?? (_baseUri = new Uri(_baseUriString));
}
}
public Uri BaseUri => _baseUri ?? (_baseUri = new Uri(_baseUriString));

public Uri Uri
{
get
{
return _uri ?? (_uri = new Uri(_uriString));
}
}
public Uri Uri => _uri ?? (_uri = new Uri(_uriString));

public string CorrelationId { get; }

Expand All @@ -67,5 +62,9 @@ public Uri Uri
public string RouteName { get; set; }

public ClaimsPrincipal Principal { get; set; }

public IDictionary<string, StringValues> RequestHeaders { get; }

public IDictionary<string, StringValues> ResponseHeaders { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
// -------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Security.Claims;
using Hl7.Fhir.Model;
using Microsoft.Extensions.Primitives;

namespace Microsoft.Health.Fhir.Core.Features.Context
{
Expand All @@ -26,5 +28,9 @@ public interface IFhirRequestContext
string RouteName { get; set; }

ClaimsPrincipal Principal { get; set; }

IDictionary<string, StringValues> RequestHeaders { get; }

IDictionary<string, StringValues> ResponseHeaders { get; }
}
}
Loading

0 comments on commit 1528b10

Please sign in to comment.