Search in sources :

Example 1 with Locks

use of com.b2international.snowowl.core.locks.Locks in project snow-owl by b2ihealthcare.

the class ClassificationJobRequest method executeClassification.

private void executeClassification(final BranchContext context, final String classificationId, final ClassificationTracker tracker) {
    final RevisionSearcher revisionSearcher = context.service(RevisionSearcher.class);
    TerminologyResource resource = context.service(TerminologyResource.class);
    @SuppressWarnings("unchecked") final Set<String> reasonerExcludedModuleIds = Collections3.toImmutableSet((Iterable) resource.getSettings().getOrDefault(REASONER_EXCLUDE_MODULE_IDS, Collections.emptySet()));
    final SnomedCoreConfiguration configuration = context.service(SnomedCoreConfiguration.class);
    final boolean concreteDomainSupported = configuration.isConcreteDomainSupported();
    final ReasonerTaxonomy taxonomy;
    try (Locks locks = Locks.on(context).lock(DatastoreLockContextDescriptions.CLASSIFY, parentLockContext)) {
        taxonomy = buildTaxonomy(revisionSearcher, reasonerExcludedModuleIds, concreteDomainSupported);
    } catch (final LockedException e) {
        throw new ReasonerApiException("Couldn't acquire exclusive access to terminology store for classification; %s", e.getMessage(), e);
    }
    final OWLOntologyManager ontologyManager = OWLManager.createOWLOntologyManager();
    ontologyManager.getOntologyFactories().add(new DelegateOntologyFactory(taxonomy));
    // TODO: custom moduleId in ontology IRI?
    final IRI ontologyIRI = IRI.create(DelegateOntology.NAMESPACE_SCTM + Concepts.MODULE_SCT_CORE);
    try {
        final DelegateOntology ontology = (DelegateOntology) ontologyManager.createOntology(ontologyIRI);
        final ReasonerTaxonomyInferrer inferrer = new ReasonerTaxonomyInferrer(reasonerId, ontology, context);
        final ReasonerTaxonomy inferredTaxonomy = inferrer.addInferences(taxonomy);
        final NormalFormGenerator normalFormGenerator = new NormalFormGenerator(inferredTaxonomy);
        tracker.classificationCompleted(classificationId, inferredTaxonomy, normalFormGenerator);
    } catch (final OWLOntologyCreationException e) {
        throw new ReasonerApiException("Exception caught while creating ontology instance.", e);
    } catch (final ReasonerInterruptedException | OWLReasonerRuntimeException e) {
        throw new ReasonerApiException("Exception caught while classifying the ontology.", e);
    }
}
Also used : ReasonerTaxonomy(com.b2international.snowowl.snomed.datastore.index.taxonomy.ReasonerTaxonomy) IRI(org.semanticweb.owlapi.model.IRI) LockedException(com.b2international.commons.exceptions.LockedException) ReasonerInterruptedException(org.semanticweb.owlapi.reasoner.ReasonerInterruptedException) SnomedCoreConfiguration(com.b2international.snowowl.snomed.datastore.config.SnomedCoreConfiguration) ReasonerApiException(com.b2international.snowowl.snomed.reasoner.exceptions.ReasonerApiException) DelegateOntologyFactory(com.b2international.snowowl.snomed.reasoner.ontology.DelegateOntologyFactory) Locks(com.b2international.snowowl.core.locks.Locks) NormalFormGenerator(com.b2international.snowowl.snomed.reasoner.normalform.NormalFormGenerator) OWLReasonerRuntimeException(org.semanticweb.owlapi.reasoner.OWLReasonerRuntimeException) DelegateOntology(com.b2international.snowowl.snomed.reasoner.ontology.DelegateOntology) ReasonerTaxonomyInferrer(com.b2international.snowowl.snomed.reasoner.classification.ReasonerTaxonomyInferrer) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) TerminologyResource(com.b2international.snowowl.core.TerminologyResource) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) RevisionSearcher(com.b2international.index.revision.RevisionSearcher)

Example 2 with Locks

use of com.b2international.snowowl.core.locks.Locks in project snow-owl by b2ihealthcare.

the class SaveJobRequest method execute.

@Override
public Boolean execute(final BranchContext context) {
    final IProgressMonitor monitor = context.service(IProgressMonitor.class);
    final ClassificationTracker tracker = context.service(ClassificationTracker.class);
    final String user = !Strings.isNullOrEmpty(userId) ? userId : context.service(User.class).getUsername();
    try (Locks locks = Locks.on(context).user(user).lock(DatastoreLockContextDescriptions.SAVE_CLASSIFICATION_RESULTS, parentLockContext)) {
        return persistChanges(context, monitor);
    } catch (final LockedException e) {
        tracker.classificationFailed(classificationId);
        throw new ReasonerApiException("Couldn't acquire exclusive access to terminology store for persisting classification changes; %s", e.getMessage(), e);
    } catch (final Exception e) {
        LOG.error("Unexpected error while persisting classification changes.", e);
        tracker.classificationSaveFailed(classificationId);
        throw new ReasonerApiException("Error while persisting classification changes on '%s'.", context.path(), e);
    } finally {
        monitor.done();
    }
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) LockedException(com.b2international.commons.exceptions.LockedException) ReasonerApiException(com.b2international.snowowl.snomed.reasoner.exceptions.ReasonerApiException) Locks(com.b2international.snowowl.core.locks.Locks) ClassificationTracker(com.b2international.snowowl.snomed.reasoner.classification.ClassificationTracker) BadRequestException(com.b2international.commons.exceptions.BadRequestException) LockedException(com.b2international.commons.exceptions.LockedException) ReasonerApiException(com.b2international.snowowl.snomed.reasoner.exceptions.ReasonerApiException)

Example 3 with Locks

use of com.b2international.snowowl.core.locks.Locks in project snow-owl by b2ihealthcare.

the class SnomedRf2ImportRequest method execute.

@Override
public ImportResponse execute(BranchContext context) {
    log = LoggerFactory.getLogger("import");
    context = context.inject().bind(Logger.class, log).build();
    Rf2ImportConfiguration importConfig = new Rf2ImportConfiguration(releaseType, createVersions);
    validate(context, importConfig);
    final InternalAttachmentRegistry fileReg = (InternalAttachmentRegistry) context.service(AttachmentRegistry.class);
    final File rf2Archive = fileReg.getAttachment(this.rf2Archive.getAttachmentId());
    try (Locks locks = Locks.on(context).lock(DatastoreLockContextDescriptions.IMPORT)) {
        return doImport(context, rf2Archive, importConfig);
    } catch (Exception e) {
        if (e instanceof ApiException) {
            throw (ApiException) e;
        }
        throw SnowowlRuntimeException.wrap(e);
    }
}
Also used : InternalAttachmentRegistry(com.b2international.snowowl.core.attachments.InternalAttachmentRegistry) AttachmentRegistry(com.b2international.snowowl.core.attachments.AttachmentRegistry) InternalAttachmentRegistry(com.b2international.snowowl.core.attachments.InternalAttachmentRegistry) Locks(com.b2international.snowowl.core.locks.Locks) ZipFile(java.util.zip.ZipFile) File(java.io.File) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException) ApiException(com.b2international.commons.exceptions.ApiException) BadRequestException(com.b2international.commons.exceptions.BadRequestException) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ApiException(com.b2international.commons.exceptions.ApiException)

Example 4 with Locks

use of com.b2international.snowowl.core.locks.Locks in project snow-owl by b2ihealthcare.

the class RepositoryTransactionContext method commit.

@Override
public Optional<Commit> commit(String author, String commitComment, String parentLockContext) {
    if (!isDirty()) {
        return Optional.empty();
    }
    // fall back to the current lock context or ROOT if none is present
    if (Strings.isNullOrEmpty(parentLockContext)) {
        parentLockContext = optionalService(Locks.class).map(Locks::lockContext).orElse(DatastoreLockContextDescriptions.ROOT);
    }
    final DatastoreLockContext lockContext = createLockContext(service(User.class).getUsername(), parentLockContext);
    final DatastoreLockTarget lockTarget = createLockTarget(info().id(), path());
    IOperationLockManager locks = service(IOperationLockManager.class);
    Commit commit = null;
    try {
        locks.lock(lockContext, 1000L, lockTarget);
        final long timestamp = service(TimestampProvider.class).getTimestamp();
        log().info("Persisting changes to {}@{}", path(), timestamp);
        commit = staging.commit(null, timestamp, author, commitComment);
        log().info("Changes have been successfully persisted to {}@{}.", path(), timestamp);
        return Optional.ofNullable(commit);
    } catch (final IndexException e) {
        Throwable rootCause = Throwables.getRootCause(e);
        if (rootCause instanceof CycleDetectedException) {
            throw (CycleDetectedException) rootCause;
        }
        throw new SnowowlRuntimeException(e.getMessage(), e);
    } finally {
        locks.unlock(lockContext, lockTarget);
        if (commit != null && isNotificationEnabled()) {
            service(RepositoryCommitNotificationSender.class).publish(this, commit);
        }
        clear();
    }
}
Also used : IndexException(com.b2international.index.IndexException) CycleDetectedException(com.b2international.commons.exceptions.CycleDetectedException) Locks(com.b2international.snowowl.core.locks.Locks) IOperationLockManager(com.b2international.snowowl.core.locks.IOperationLockManager) DatastoreLockTarget(com.b2international.snowowl.core.internal.locks.DatastoreLockTarget) DatastoreLockContext(com.b2international.snowowl.core.internal.locks.DatastoreLockContext) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException)

Aggregations

Locks (com.b2international.snowowl.core.locks.Locks)4 BadRequestException (com.b2international.commons.exceptions.BadRequestException)2 LockedException (com.b2international.commons.exceptions.LockedException)2 SnowowlRuntimeException (com.b2international.snowowl.core.api.SnowowlRuntimeException)2 ReasonerApiException (com.b2international.snowowl.snomed.reasoner.exceptions.ReasonerApiException)2 ApiException (com.b2international.commons.exceptions.ApiException)1 CycleDetectedException (com.b2international.commons.exceptions.CycleDetectedException)1 IndexException (com.b2international.index.IndexException)1 RevisionSearcher (com.b2international.index.revision.RevisionSearcher)1 TerminologyResource (com.b2international.snowowl.core.TerminologyResource)1 AttachmentRegistry (com.b2international.snowowl.core.attachments.AttachmentRegistry)1 InternalAttachmentRegistry (com.b2international.snowowl.core.attachments.InternalAttachmentRegistry)1 DatastoreLockContext (com.b2international.snowowl.core.internal.locks.DatastoreLockContext)1 DatastoreLockTarget (com.b2international.snowowl.core.internal.locks.DatastoreLockTarget)1 IOperationLockManager (com.b2international.snowowl.core.locks.IOperationLockManager)1 SnomedCoreConfiguration (com.b2international.snowowl.snomed.datastore.config.SnomedCoreConfiguration)1 ReasonerTaxonomy (com.b2international.snowowl.snomed.datastore.index.taxonomy.ReasonerTaxonomy)1 ClassificationTracker (com.b2international.snowowl.snomed.reasoner.classification.ClassificationTracker)1 ReasonerTaxonomyInferrer (com.b2international.snowowl.snomed.reasoner.classification.ReasonerTaxonomyInferrer)1 NormalFormGenerator (com.b2international.snowowl.snomed.reasoner.normalform.NormalFormGenerator)1