-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Updates to the canonical validation resource cache in VersionSpecificWorkerContextWrapper
#6506
Open
codeforgreen
wants to merge
4
commits into
master
Choose a base branch
from
6505-validation-errors-profile-cache-expiry
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c37ed40
Increase canonical resource validation cache timeout. Update logic so…
codeforgreen 2091291
Revert change to supportsSystem method and fix test.
codeforgreen fe2a710
Merge remote-tracking branch 'origin/master' into 6505-validation-err…
codeforgreen d4fdee7
Small change in test
codeforgreen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
...sources/ca/uhn/hapi/fhir/changelog/7_6_0/6505-validation-errors-profile-cache-expiry.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
type: fix | ||
issue: 6505 | ||
title: "Previously, when invoking a $validate operation that took more than 10s some unexpected errors | ||
would be returned which suggest the profile was not applied properly or used. | ||
That happens because the validation cache entry expired within the $validate call | ||
and the FHIR Core Validation library expects the same object instance for the same profile to be returned. | ||
This has been fixed by increasing the cache expiry to 10min." | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
202 changes: 202 additions & 0 deletions
202
...-test-r4/src/test/java/ca/uhn/fhir/jpa/validation/VersionSpecificWorkerContextR4Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,202 @@ | ||
package ca.uhn.fhir.jpa.validation; | ||
|
||
import ca.uhn.fhir.context.support.IValidationSupport; | ||
import ca.uhn.fhir.jpa.provider.BaseResourceProviderR4Test; | ||
import ca.uhn.fhir.system.HapiSystemProperties; | ||
import ca.uhn.fhir.util.ClasspathUtil; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.hl7.fhir.common.hapi.validation.validator.FhirInstanceValidator; | ||
import org.hl7.fhir.common.hapi.validation.validator.VersionSpecificWorkerContextWrapper; | ||
import org.hl7.fhir.r4.model.Bundle; | ||
import org.hl7.fhir.r4.model.CodeSystem; | ||
import org.hl7.fhir.r4.model.ElementDefinition; | ||
import org.hl7.fhir.r4.model.StructureDefinition; | ||
import org.hl7.fhir.r4.model.ValueSet; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
import java.util.List; | ||
|
||
import static ca.uhn.fhir.test.utilities.validation.ValidationTestUtil.getValidationErrors; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class VersionSpecificWorkerContextR4Test extends BaseResourceProviderR4Test { | ||
@Autowired | ||
private FhirInstanceValidator myInstanceValidator; | ||
@Autowired | ||
private IValidationSupport myValidationSupport; | ||
|
||
@Nested | ||
public class ValidateWithCacheNegativeTest { | ||
@BeforeEach | ||
public void before() { | ||
HapiSystemProperties.setValidationResourceCacheTimeoutMillis(1); | ||
myInstanceValidator.resetWorkerContext(); | ||
} | ||
|
||
/** | ||
* If the cache is set to expire quickly, the second retrieval of the profile will result in a different StructureDefinition instance. | ||
* The FHIR Core validation library currently expects the same instance of StructureDefinition to be returned for the same profile. | ||
* This test can be removed if the FHIR Core library is updated to use equals/hashcode but for not this is how it works. | ||
*/ | ||
@Test | ||
public void validate_returnsUnexpectedErrors() { | ||
// setup | ||
StructureDefinition profileProcedure = ClasspathUtil.loadResource(myFhirContext, StructureDefinition.class, "validation/practitionerrole/profile-practitionerrole.json"); | ||
myClient.update().resource(profileProcedure).execute(); | ||
|
||
Bundle bundle = ClasspathUtil.loadResource(myFhirContext, Bundle.class, "validation/practitionerrole/bundle-with-medicationdispense.json"); | ||
|
||
// execute and verify | ||
// the cache entry for the StructureDefinition (profile) will expire at the timeout (1ms) after it is written | ||
// there are multiple calls to fetch the profile within a single validate call, which means subsequent ones will get a different instance of StructureDefinition | ||
List<String> errors = getValidationErrors(myClient, bundle); | ||
assertThat(StringUtils.join("", errors)).contains("PractitionerRole.telecom:TelecomPhone: max allowed = 1, but found 2"); | ||
} | ||
} | ||
|
||
@Nested | ||
public class ValidateWithCachePositiveTest { | ||
@BeforeEach | ||
public void before() { | ||
HapiSystemProperties.restoreDefaultValidationResourceCacheTimeoutMillis(); | ||
myInstanceValidator.resetWorkerContext(); | ||
} | ||
|
||
@Test | ||
public void validate_profileResourceFetchedMultipleTimes_returnsNoErrors() { | ||
// setup | ||
StructureDefinition profileProcedure = ClasspathUtil.loadResource(myFhirContext, StructureDefinition.class, "validation/practitionerrole/profile-practitionerrole.json"); | ||
myClient.update().resource(profileProcedure).execute(); | ||
|
||
Bundle bundle = ClasspathUtil.loadResource(myFhirContext, Bundle.class, "validation/practitionerrole/bundle-with-medicationdispense.json"); | ||
|
||
// execute and verify | ||
List<String> errors = getValidationErrors(myClient, bundle); | ||
assertThat(errors).isEmpty(); | ||
} | ||
} | ||
|
||
@Nested | ||
public class FetchTests { | ||
private VersionSpecificWorkerContextWrapper myWorkerContext; | ||
|
||
@BeforeEach | ||
public void before() { | ||
myWorkerContext = VersionSpecificWorkerContextWrapper.newVersionSpecificWorkerContextWrapper(myValidationSupport); | ||
} | ||
|
||
@Test | ||
public void fetchResource_withStructureDefinition_usesCache() { | ||
final String resourceUrl = createStructureDefinition(); | ||
|
||
// verify that the snapshot is generated | ||
org.hl7.fhir.r5.model.StructureDefinition fetchedStructureDefinition1 = myWorkerContext.fetchResource(org.hl7.fhir.r5.model.StructureDefinition.class, resourceUrl); | ||
assertThat(fetchedStructureDefinition1.hasSnapshot()).isTrue(); | ||
|
||
// verify that the sub-subsequent fetchResource returns the same resource instance from the cache | ||
org.hl7.fhir.r5.model.StructureDefinition fetchedStructureDefinition2 = myWorkerContext.fetchResource(org.hl7.fhir.r5.model.StructureDefinition.class, resourceUrl); | ||
assertThat(fetchedStructureDefinition2).isSameAs(fetchedStructureDefinition1); | ||
} | ||
|
||
@Test | ||
public void fetchResource_withCodeSystem_cacheIsUsed() { | ||
final String resourceUrl = "http://example.com/CodeSystem/example-system"; | ||
|
||
CodeSystem codeSystem = new CodeSystem(); | ||
codeSystem.setId("CodeSystem/example-system"); | ||
codeSystem.setUrl(resourceUrl); | ||
|
||
myCodeSystemDao.create(codeSystem, mySrd); | ||
|
||
org.hl7.fhir.r5.model.CodeSystem fetchedCodeSystem1 = myWorkerContext.fetchResource(org.hl7.fhir.r5.model.CodeSystem.class, resourceUrl); | ||
|
||
// verify that the sub-subsequent fetchResource returns the same resource instance from the cache | ||
org.hl7.fhir.r5.model.CodeSystem fetchedCodeSystem2 = myWorkerContext.fetchResource(org.hl7.fhir.r5.model.CodeSystem.class, resourceUrl); | ||
assertThat(fetchedCodeSystem2).isSameAs(fetchedCodeSystem1); | ||
} | ||
|
||
@Test | ||
public void fetchCodeSystem_cacheIsUsed() { | ||
final String resourceUrl = "http://example.com/CodeSystem/example-system"; | ||
|
||
CodeSystem codeSystem = new CodeSystem(); | ||
codeSystem.setId("CodeSystem/example-system"); | ||
codeSystem.setUrl(resourceUrl); | ||
|
||
myCodeSystemDao.create(codeSystem, mySrd); | ||
|
||
org.hl7.fhir.r5.model.CodeSystem fetchedCodeSystem1 = myWorkerContext.fetchCodeSystem(resourceUrl); | ||
|
||
// verify that the sub-subsequent fetchCodeSystem returns the same resource instance from the cache | ||
org.hl7.fhir.r5.model.CodeSystem fetchedCodeSystem2 = myWorkerContext.fetchCodeSystem(resourceUrl); | ||
assertThat(fetchedCodeSystem2).isSameAs(fetchedCodeSystem1); | ||
} | ||
|
||
@Test | ||
public void fetchCodeSystem_usesSameCacheAsFetchResource() { | ||
final String resourceUrl = "http://example.com/CodeSystem/example-system"; | ||
|
||
CodeSystem codeSystem = new CodeSystem(); | ||
codeSystem.setId("CodeSystem/example-system"); | ||
codeSystem.setUrl(resourceUrl); | ||
|
||
myCodeSystemDao.create(codeSystem, mySrd); | ||
|
||
org.hl7.fhir.r5.model.CodeSystem fetchedCodeSystem1 = myWorkerContext.fetchCodeSystem(resourceUrl); | ||
|
||
// verify that the sub-subsequent fetchResource returns the same resource instance from the cache | ||
org.hl7.fhir.r5.model.CodeSystem fetchedCodeSystem2 = myWorkerContext.fetchResource(org.hl7.fhir.r5.model.CodeSystem.class, resourceUrl); | ||
assertThat(fetchedCodeSystem2).isSameAs(fetchedCodeSystem1); | ||
} | ||
|
||
@Test | ||
public void fetchResource_withValueSet_cacheIsUsed() { | ||
final String resourceUrl = "http://example.com/ValueSet/example-set"; | ||
|
||
ValueSet valueSet = new ValueSet(); | ||
valueSet.setId("ValueSet/example-set"); | ||
valueSet.setUrl(resourceUrl); | ||
|
||
myValueSetDao.create(valueSet, mySrd); | ||
|
||
org.hl7.fhir.r5.model.ValueSet fetchedCodeSystem1 = myWorkerContext.fetchResource(org.hl7.fhir.r5.model.ValueSet.class, resourceUrl); | ||
|
||
// verify that the sub-subsequent fetchResource returns the same resource instance from the cache | ||
org.hl7.fhir.r5.model.ValueSet fetchedCodeSystem2 = myWorkerContext.fetchResource(org.hl7.fhir.r5.model.ValueSet.class, resourceUrl); | ||
assertThat(fetchedCodeSystem2).isSameAs(fetchedCodeSystem1); | ||
} | ||
|
||
@Test | ||
public void fetchResource_expireCache_cacheWorksAsExpected() { | ||
final String resourceUrl = createStructureDefinition(); | ||
|
||
org.hl7.fhir.r5.model.StructureDefinition fetchedStructureDefinition1 = myWorkerContext.fetchResource(org.hl7.fhir.r5.model.StructureDefinition.class, resourceUrl); | ||
|
||
// simulate cache timeout by invalidating cache | ||
myWorkerContext.invalidateCaches(); | ||
|
||
// verify that the sub-subsequent fetchResource returns a different resource instance from the cache | ||
org.hl7.fhir.r5.model.StructureDefinition fetchedStructureDefinition3 = myWorkerContext.fetchResource(org.hl7.fhir.r5.model.StructureDefinition.class, resourceUrl); | ||
assertThat(fetchedStructureDefinition3).isNotSameAs(fetchedStructureDefinition1); | ||
} | ||
} | ||
|
||
private String createStructureDefinition() { | ||
final String resourceUrl = "http://example.com/StructureDefinition/example-profile-patient"; | ||
StructureDefinition structureDefinition = new StructureDefinition(); | ||
structureDefinition.setId("StructureDefinition/example-profile-patient"); | ||
structureDefinition.setUrl(resourceUrl); | ||
structureDefinition.setType("Patient"); | ||
structureDefinition.setBaseDefinition("http://hl7.org/fhir/StructureDefinition/Patient"); | ||
structureDefinition.setDerivation(StructureDefinition.TypeDerivationRule.CONSTRAINT); | ||
ElementDefinition telecomElement = structureDefinition.getDifferential().addElement().setPath("Patient.telecom"); | ||
telecomElement.setMax("1"); | ||
|
||
myStructureDefinitionDao.create(structureDefinition, mySrd); | ||
|
||
return resourceUrl; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: Does this mean we are still vulnerable to rare failures if a cache entry expires in the middle of processing?