use of com.b2international.snowowl.core.events.util.Promise in project snow-owl by b2ihealthcare.
the class SnomedExportApiTest method executeMultipleExportsAtTheSameTime.
@Test
public void executeMultipleExportsAtTheSameTime() throws Exception {
Promise<Attachment> first = SnomedRequests.rf2().prepareExport().setReleaseType(Rf2ReleaseType.FULL).setCountryNamespaceElement("INT").setRefSetExportLayout(Rf2RefSetExportLayout.COMBINED).setLocales(LOCALES).build(branchPath.getPath()).execute(getBus());
Promise<Attachment> second = SnomedRequests.rf2().prepareExport().setCountryNamespaceElement("INT").setRefSetExportLayout(Rf2RefSetExportLayout.COMBINED).setReleaseType(Rf2ReleaseType.SNAPSHOT).setLocales(LOCALES).build(branchPath.getPath()).execute(getBus());
String message = Promise.all(first, second).then(input -> {
Attachment firstResult = (Attachment) input.get(0);
Attachment secondResult = (Attachment) input.get(1);
InternalAttachmentRegistry fileRegistry = (InternalAttachmentRegistry) ApplicationContext.getServiceForClass(AttachmentRegistry.class);
File firstArchive = fileRegistry.getAttachment(firstResult.getAttachmentId());
File secondArchive = fileRegistry.getAttachment(secondResult.getAttachmentId());
final Map<String, Boolean> firstArchiveMap = ImmutableMap.<String, Boolean>builder().put("sct2_Concept_Full", true).build();
final Map<String, Boolean> secondArchiveMap = ImmutableMap.<String, Boolean>builder().put("sct2_Concept_Snapshot", true).build();
try {
assertArchiveContainsFiles(firstArchive, firstArchiveMap);
assertArchiveContainsFiles(secondArchive, secondArchiveMap);
} catch (Exception e) {
return e.getMessage();
}
fileRegistry.delete(firstResult.getAttachmentId());
fileRegistry.delete(secondResult.getAttachmentId());
return null;
}).fail(input -> input.getMessage()).getSync(2, TimeUnit.MINUTES);
assertNull(message, message);
}
use of com.b2international.snowowl.core.events.util.Promise in project snow-owl by b2ihealthcare.
the class SnomedBranchRequestTest method createTwoBranchesSameTimeWithSameName.
@Test
public void createTwoBranchesSameTimeWithSameName() throws Exception {
final Branching branches = RepositoryRequests.branching();
// try to create two branches at the same time
final String branchName = UUID.randomUUID().toString();
final Promise<String> first = branches.prepareCreate().setParent(branchPath).setName(branchName).build(REPOSITORY_ID).execute(bus);
final Promise<String> second = branches.prepareCreate().setParent(branchPath).setName(branchName).build(REPOSITORY_ID).execute(bus);
final boolean sameBaseTimestamp = Promise.all(first, second).then(input -> {
final Branch first1 = branches.prepareGet((String) input.get(0)).build(REPOSITORY_ID).execute(bus).getSync();
final Branch second1 = branches.prepareGet((String) input.get(1)).build(REPOSITORY_ID).execute(bus).getSync();
return first1.baseTimestamp() == second1.baseTimestamp();
}).fail(e -> {
if (e instanceof AlreadyExistsException) {
return true;
} else {
throw new RuntimeException(e);
}
}).getSync();
// fail the test only when the API managed to create two branches with different base timestamp which should not happen
if (!sameBaseTimestamp) {
fail("Two branches created with the same name but different baseTimestamp");
}
}
use of com.b2international.snowowl.core.events.util.Promise in project snow-owl by b2ihealthcare.
the class SnomedBranchRequestTest method createTwoBranchesSameTimeWithDifferentName.
@Test
public void createTwoBranchesSameTimeWithDifferentName() throws Exception {
final Branching branches = RepositoryRequests.branching();
// try to create two branches at the same time
final String branchA = UUID.randomUUID().toString();
final String branchB = UUID.randomUUID().toString();
final Promise<String> first = branches.prepareCreate().setParent(branchPath).setName(branchA).build(REPOSITORY_ID).execute(bus);
final Promise<String> second = branches.prepareCreate().setParent(branchPath).setName(branchB).build(REPOSITORY_ID).execute(bus);
Promise.all(first, second).then(input -> {
final Branch firstBranch = branches.prepareGet((String) input.get(0)).build(REPOSITORY_ID).execute(bus).getSync();
final Branch secondBranch = branches.prepareGet((String) input.get(1)).build(REPOSITORY_ID).execute(bus).getSync();
assertBranchesCreated(branchA, branchB, firstBranch, secondBranch);
assertBranchSegmentsValid(branchPath, firstBranch.path(), secondBranch.path());
return null;
}).getSync();
}
use of com.b2international.snowowl.core.events.util.Promise in project snow-owl by b2ihealthcare.
the class ValidationThreadPool method submit.
public Promise<Object> submit(CheckType checkType, Runnable runnable) {
final Job job = new ValidationJob(checkType.getName(), runnable);
final String uniqueRuleId = UUID.randomUUID().toString();
final ISchedulingRule schedulingRule = new ValidationRuleSchedulingRule(checkType, maxValidationThreadCount, maxConcurrentExpensiveJobs, maxConcurrentNormalJobs, uniqueRuleId);
final Promise<Object> promise = new Promise<>();
job.setSystem(true);
job.setRule(schedulingRule);
job.addJobChangeListener(new JobChangeAdapter() {
@Override
public void done(IJobChangeEvent event) {
if (event.getResult().isOK()) {
promise.resolve(Boolean.TRUE);
} else {
promise.reject(new ValidationException(String.format("Validation job failed with status %s.", event.getResult())));
}
}
});
job.schedule();
return promise;
}
use of com.b2international.snowowl.core.events.util.Promise in project snow-owl by b2ihealthcare.
the class ValidateRequest method doValidate.
private ValidationResult doValidate(BranchContext context, Writer index) throws IOException {
final String branchPath = context.path();
ValidationRuleSearchRequestBuilder req = ValidationRequests.rules().prepareSearch();
TerminologyResource resource = context.service(TerminologyResource.class);
ResourceURI resourceURI = resource.getResourceURI(branchPath);
if (!CompareUtils.isEmpty(ruleIds)) {
req.filterByIds(ruleIds);
}
final ValidationRules rules = req.all().build().execute(context);
final ValidationThreadPool pool = context.service(ValidationThreadPool.class);
final BlockingQueue<IssuesToPersist> issuesToPersistQueue = Queues.newLinkedBlockingDeque();
final List<Promise<Object>> validationPromises = Lists.newArrayList();
// evaluate selected rules
for (ValidationRule rule : rules) {
checkArgument(rule.getCheckType() != null, "CheckType is missing for rule " + rule.getId());
final ValidationRuleEvaluator evaluator = ValidationRuleEvaluator.Registry.get(rule.getType());
if (evaluator != null) {
validationPromises.add(pool.submit(rule.getCheckType(), () -> {
Stopwatch w = Stopwatch.createStarted();
try {
LOG.info("Executing rule '{}'...", rule.getId());
final List<?> evaluationResponse = evaluator.eval(context, rule, ruleParameters);
issuesToPersistQueue.offer(new IssuesToPersist(rule.getId(), evaluationResponse));
LOG.info("Execution of rule '{}' successfully completed in '{}'.", rule.getId(), w);
// TODO report successfully executed validation rule
} catch (Exception e) {
// TODO report failed validation rule
LOG.error("Execution of rule '{}' failed after '{}'.", rule.getId(), w, e);
}
}));
}
}
final Set<String> ruleIds = rules.stream().map(ValidationRule::getId).collect(Collectors.toSet());
final Multimap<String, ComponentIdentifier> whiteListedEntries = fetchWhiteListEntries(context, ruleIds);
final Promise<List<Object>> promise = Promise.all(validationPromises);
while (!promise.isDone() || !issuesToPersistQueue.isEmpty()) {
if (!issuesToPersistQueue.isEmpty()) {
final Collection<IssuesToPersist> issuesToPersist = newArrayList();
issuesToPersistQueue.drainTo(issuesToPersist);
if (!issuesToPersist.isEmpty()) {
final List<String> rulesToPersist = issuesToPersist.stream().map(itp -> itp.ruleId).collect(Collectors.toList());
LOG.info("Persisting issues generated by rules '{}'...", rulesToPersist);
// persist new issues generated by rules so far, extending them using the Issue Extension API
int persistedIssues = 0;
final Multimap<String, ValidationIssue> issuesToExtendWithDetailsByToolingId = HashMultimap.create();
for (IssuesToPersist ruleIssues : Iterables.consumingIterable(issuesToPersist)) {
final String ruleId = ruleIssues.ruleId;
final List<ValidationIssue> existingRuleIssues = ValidationRequests.issues().prepareSearch().all().filterByResourceUri(resourceURI).filterByRule(ruleId).build().execute(context).getItems();
final Set<String> issueIdsToDelete = Sets.newHashSet();
final Map<ComponentIdentifier, ValidationIssue> existingIsssuesByComponentIdentifier = new HashMap<>();
for (ValidationIssue issue : existingRuleIssues) {
if (existingIsssuesByComponentIdentifier.containsKey(issue.getAffectedComponent())) {
issueIdsToDelete.add(issue.getId());
} else {
existingIsssuesByComponentIdentifier.put(issue.getAffectedComponent(), issue);
}
}
// remove all processed whitelist entries
final Collection<ComponentIdentifier> ruleWhiteListEntries = whiteListedEntries.removeAll(ruleId);
final String toolingId = rules.stream().filter(rule -> ruleId.equals(rule.getId())).findFirst().get().getToolingId();
for (ValidationIssueDetails issueDetails : ruleIssues.issueDetails) {
final ValidationIssue validationIssue;
ComponentIdentifier componentIdentifier = issueDetails.affectedComponentId;
if (!existingIsssuesByComponentIdentifier.containsKey(componentIdentifier)) {
validationIssue = new ValidationIssue(UUID.randomUUID().toString(), ruleId, ComponentURI.of(resourceURI, componentIdentifier), ruleWhiteListEntries.contains(componentIdentifier));
} else {
final ValidationIssue issueToCopy = existingIsssuesByComponentIdentifier.get(componentIdentifier);
validationIssue = new ValidationIssue(issueToCopy.getId(), issueToCopy.getRuleId(), ComponentURI.of(resourceURI, issueToCopy.getAffectedComponent()), ruleWhiteListEntries.contains(issueToCopy.getAffectedComponent()));
existingIsssuesByComponentIdentifier.remove(componentIdentifier);
}
validationIssue.setDetails(ValidationIssueDetails.HIGHLIGHT_DETAILS, issueDetails.stylingDetails);
issuesToExtendWithDetailsByToolingId.put(toolingId, validationIssue);
persistedIssues++;
}
existingRuleIssues.stream().filter(issue -> existingIsssuesByComponentIdentifier.containsKey(issue.getAffectedComponent())).forEach(issue -> issueIdsToDelete.add(issue.getId()));
if (!issueIdsToDelete.isEmpty()) {
index.removeAll(Collections.singletonMap(ValidationIssue.class, issueIdsToDelete));
}
}
for (String toolingId : issuesToExtendWithDetailsByToolingId.keySet()) {
final ValidationIssueDetailExtension extensions = context.service(ValidationIssueDetailExtensionProvider.class).getExtensions(toolingId);
final Collection<ValidationIssue> issues = issuesToExtendWithDetailsByToolingId.removeAll(toolingId);
extensions.extendIssues(context, issues, ruleParameters);
for (ValidationIssue issue : issues) {
index.put(issue);
}
}
index.commit();
LOG.info("Persisted '{}' issues generated by rules '{}'.", persistedIssues, rulesToPersist);
}
} else {
try {
// wait at least number of rules * 50ms for the next responses
Thread.sleep(Math.min(((long) ruleIds.size()) * 100, POLL_INTERVAL_MAX));
} catch (InterruptedException e) {
throw new SnowowlRuntimeException(e);
}
}
}
// TODO return ValidationResult object with status and new issue IDs as set
return new ValidationResult(context.info().id(), context.path());
}
Aggregations