Search in sources :

Example 1 with MemoryMonitor

use of edu.stanford.bmir.protege.web.server.util.MemoryMonitor in project webprotege by protegeproject.

the class LoadProjectActionHandler method execute.

@Nonnull
@Override
public LoadProjectResult execute(@Nonnull final LoadProjectAction action, @Nonnull ExecutionContext executionContext) {
    Stopwatch stopwatch = Stopwatch.createStarted();
    logger.info("{} is being loaded due to request by {}", action.getProjectId(), executionContext.getUserId());
    projectManager.getProject(action.getProjectId(), executionContext.getUserId());
    stopwatch.stop();
    logger.info("{} was loaded in {} ms due to request by {}", action.getProjectId(), stopwatch.elapsed(TimeUnit.MILLISECONDS), executionContext.getUserId());
    MemoryMonitor memoryMonitor = new MemoryMonitor(logger);
    memoryMonitor.monitorMemoryUsage();
    memoryMonitor.logMemoryUsage();
    final ProjectId projectId = action.getProjectId();
    ProjectDetails projectDetails = projectDetailsManager.getProjectDetails(projectId);
    if (!executionContext.getUserId().isGuest()) {
        userActivityManager.addRecentProject(executionContext.getUserId(), action.getProjectId(), System.currentTimeMillis());
    }
    return new LoadProjectResult(action.getProjectId(), executionContext.getUserId(), projectDetails);
}
Also used : MemoryMonitor(edu.stanford.bmir.protege.web.server.util.MemoryMonitor) ProjectDetails(edu.stanford.bmir.protege.web.shared.project.ProjectDetails) Stopwatch(com.google.common.base.Stopwatch) ProjectId(edu.stanford.bmir.protege.web.shared.project.ProjectId) LoadProjectResult(edu.stanford.bmir.protege.web.shared.project.LoadProjectResult) Nonnull(javax.annotation.Nonnull)

Example 2 with MemoryMonitor

use of edu.stanford.bmir.protege.web.server.util.MemoryMonitor in project webprotege by protegeproject.

the class CreateDownloadTask method call.

@Override
public Void call() throws Exception {
    if (Files.exists(downloadPath)) {
        logger.info("{} {} Project download already exists.  Not recreating download. ({})", projectId, userId, downloadPath.toAbsolutePath());
        return null;
    }
    logger.info("{} {} Creating project download", projectId, userId);
    MemoryMonitor memoryMonitor = new MemoryMonitor(logger);
    memoryMonitor.monitorMemoryUsage();
    Project project = projectManager.getProject(projectId, userId);
    memoryMonitor.monitorMemoryUsage();
    ProjectDownloader downloader = new ProjectDownloader(projectDisplayName, project, revisionNumber, format, applicationName);
    logger.info("{} {} Writing download to file: {}", projectId, userId, downloadPath);
    Files.createDirectories(downloadPath.getParent());
    try (BufferedOutputStream outputStream = new BufferedOutputStream(Files.newOutputStream(downloadPath))) {
        downloader.writeProject(outputStream);
    }
    double sizeInMB = Files.size(downloadPath) / (1024.0 * 1024);
    logger.info("{} {} Finished creating download ({} MB)", projectId, userId, String.format("%.4f", sizeInMB));
    memoryMonitor.monitorMemoryUsage();
    return null;
}
Also used : MemoryMonitor(edu.stanford.bmir.protege.web.server.util.MemoryMonitor) Project(edu.stanford.bmir.protege.web.server.project.Project) BufferedOutputStream(java.io.BufferedOutputStream)

Example 3 with MemoryMonitor

use of edu.stanford.bmir.protege.web.server.util.MemoryMonitor in project webprotege by protegeproject.

the class ProjectDocumentStore method loadProjectOntologiesIntoManager.

private OWLOntology loadProjectOntologiesIntoManager(OWLOntologyManager manager) throws OWLOntologyCreationException {
    logger.info("{} Loading project", projectId);
    long t0 = System.currentTimeMillis();
    OWLOntologyLoaderListener loaderListener = new OWLOntologyLoaderListener() {

        public void startedLoadingOntology(LoadingStartedEvent event) {
            logger.info("{} Ontology loading started: {}", projectId, event.getDocumentIRI());
        }

        public void finishedLoadingOntology(LoadingFinishedEvent event) {
            // Give something else a chance - in case we have LOTS of imports
            Thread.yield();
            if (event.isSuccessful()) {
                logger.info("{} Ontology loading finished: (Loaded:  {})", projectId, event.getDocumentIRI(), event.getOntologyID());
                MemoryMonitor memoryMonitor = new MemoryMonitor(logger);
                memoryMonitor.monitorMemoryUsage();
            } else {
                logger.info("Ontology loading failed: {} (Reason: )", projectId, event.getException().getMessage());
            }
        }
    };
    manager.addOntologyLoaderListener(loaderListener);
    final MissingImportListener missingImportListener = (MissingImportListener) missingImportEvent -> logger.info("{} Missing import: {} due to {}", projectId, missingImportEvent.getImportedOntologyURI(), missingImportEvent.getCreationException().getMessage());
    manager.addMissingImportListener(missingImportListener);
    manager.getIRIMappers().add((OWLOntologyIRIMapper) iri -> {
        logger.info("{} Fetching imported ontology from {}.", projectId, iri.toQuotedString());
        return iri;
    });
    // Important - add last
    ImportsCacheManager importsCacheManager = importsCacheManagerProvider.get();
    OWLOntologyIRIMapper iriMapper = importsCacheManager.getIRIMapper();
    manager.getIRIMappers().add(iriMapper);
    try {
        OWLOntologyLoaderConfiguration config = new OWLOntologyLoaderConfiguration();
        config = config.setMissingImportHandlingStrategy(MissingImportHandlingStrategy.SILENT);
        config = config.setReportStackTraces(true);
        // It is safe to turn of illegal punning fixing as we've already parsed (and saved) the ontology
        // using a manager with this turned on.
        config = config.setRepairIllegalPunnings(false);
        logger.info("{} Loading root ontology imports closure.", projectId);
        ProjectInputSource projectInputSource = new ProjectInputSource(rootOntologyDocument);
        OWLOntology rootOntology = manager.loadOntologyFromOntologyDocument(projectInputSource, config);
        importsCacheManager.cacheImports(rootOntology);
        return rootOntology;
    } finally {
        long t1 = System.currentTimeMillis();
        logger.info("{} Ontology loading completed in {} ms.", projectId, (t1 - t0));
        MemoryMonitor memoryMonitor = new MemoryMonitor(logger);
        memoryMonitor.monitorMemoryUsage();
        memoryMonitor.logMemoryUsage();
        manager.removeIRIMapper(iriMapper);
        manager.removeOntologyLoaderListener(loaderListener);
        manager.removeMissingImportListener(missingImportListener);
    }
}
Also used : ArrayListMultimap(com.google.common.collect.ArrayListMultimap) IdUtil(edu.stanford.bmir.protege.web.server.util.IdUtil) Provider(javax.inject.Provider) ListMultimap(com.google.common.collect.ListMultimap) org.semanticweb.owlapi.model(org.semanticweb.owlapi.model) BinaryOWLOntologyDocumentFormat(org.semanticweb.binaryowl.owlapi.BinaryOWLOntologyDocumentFormat) LoggerFactory(org.slf4j.LoggerFactory) BinaryOWLOntologyDocumentSerializer(org.semanticweb.binaryowl.BinaryOWLOntologyDocumentSerializer) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) MemoryMonitor(edu.stanford.bmir.protege.web.server.util.MemoryMonitor) BinaryOWLMetadata(org.semanticweb.binaryowl.BinaryOWLMetadata) OntologyChangeDataList(org.semanticweb.binaryowl.change.OntologyChangeDataList) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) ProjectSingleton(edu.stanford.bmir.protege.web.shared.inject.ProjectSingleton) OWLOntologyDocumentSource(org.semanticweb.owlapi.io.OWLOntologyDocumentSource) SimpleIRIMapper(org.semanticweb.owlapi.util.SimpleIRIMapper) Logger(org.slf4j.Logger) RootOntologyDocument(edu.stanford.bmir.protege.web.server.inject.project.RootOntologyDocument) OWLOntologyChangeRecord(org.semanticweb.owlapi.change.OWLOntologyChangeRecord) OWLOntologyChangeData(org.semanticweb.owlapi.change.OWLOntologyChangeData) List(java.util.List) Lock(java.util.concurrent.locks.Lock) java.io(java.io) FileDocumentSource(org.semanticweb.owlapi.io.FileDocumentSource) StreamDocumentSource(org.semanticweb.owlapi.io.StreamDocumentSource) ProjectId(edu.stanford.bmir.protege.web.shared.project.ProjectId) OWLOntologyCreationIOException(org.semanticweb.owlapi.io.OWLOntologyCreationIOException) MemoryMonitor(edu.stanford.bmir.protege.web.server.util.MemoryMonitor)

Example 4 with MemoryMonitor

use of edu.stanford.bmir.protege.web.server.util.MemoryMonitor in project webprotege by protegeproject.

the class ProjectDownloader method saveImportsClosureToStream.

private void saveImportsClosureToStream(@Nonnull String projectDisplayName, @Nonnull OWLOntology rootOntology, @Nonnull DownloadFormat format, @Nonnull OutputStream outputStream, @Nonnull RevisionNumber revisionNumber) throws IOException, OWLOntologyStorageException {
    MemoryMonitor memoryMonitor = new MemoryMonitor(logger);
    // TODO: Separate object
    try (ZipOutputStream zipOutputStream = new ZipOutputStream(new BufferedOutputStream(outputStream))) {
        String baseFolder = projectDisplayName.replace(" ", "-") + "-ontologies-" + format.getExtension();
        baseFolder = baseFolder.toLowerCase();
        baseFolder = baseFolder + "-REVISION-" + (revisionNumber.isHead() ? "HEAD" : revisionNumber.getValue());
        ZipEntry rootOntologyEntry = new ZipEntry(baseFolder + "/root-ontology." + format.getExtension());
        zipOutputStream.putNextEntry(rootOntologyEntry);
        rootOntology.getOWLOntologyManager().saveOntology(rootOntology, format.getDocumentFormat(), zipOutputStream);
        zipOutputStream.closeEntry();
        int importCount = 0;
        for (OWLOntology ontology : rootOntology.getImportsClosure()) {
            if (!ontology.equals(rootOntology)) {
                importCount++;
                ZipEntry zipEntry = new ZipEntry(baseFolder + "/imported-ontology-" + importCount + "." + format.getExtension());
                zipOutputStream.putNextEntry(zipEntry);
                ontology.getOWLOntologyManager().saveOntology(ontology, format.getDocumentFormat(), zipOutputStream);
                zipOutputStream.closeEntry();
                memoryMonitor.monitorMemoryUsage();
            }
        }
        zipOutputStream.finish();
        zipOutputStream.flush();
    }
}
Also used : MemoryMonitor(edu.stanford.bmir.protege.web.server.util.MemoryMonitor) ZipOutputStream(java.util.zip.ZipOutputStream) ZipEntry(java.util.zip.ZipEntry) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) BufferedOutputStream(java.io.BufferedOutputStream)

Aggregations

MemoryMonitor (edu.stanford.bmir.protege.web.server.util.MemoryMonitor)4 ProjectId (edu.stanford.bmir.protege.web.shared.project.ProjectId)2 BufferedOutputStream (java.io.BufferedOutputStream)2 Stopwatch (com.google.common.base.Stopwatch)1 ArrayListMultimap (com.google.common.collect.ArrayListMultimap)1 ListMultimap (com.google.common.collect.ListMultimap)1 RootOntologyDocument (edu.stanford.bmir.protege.web.server.inject.project.RootOntologyDocument)1 Project (edu.stanford.bmir.protege.web.server.project.Project)1 IdUtil (edu.stanford.bmir.protege.web.server.util.IdUtil)1 ProjectSingleton (edu.stanford.bmir.protege.web.shared.inject.ProjectSingleton)1 LoadProjectResult (edu.stanford.bmir.protege.web.shared.project.LoadProjectResult)1 ProjectDetails (edu.stanford.bmir.protege.web.shared.project.ProjectDetails)1 java.io (java.io)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Lock (java.util.concurrent.locks.Lock)1 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)1 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)1 ZipEntry (java.util.zip.ZipEntry)1 ZipOutputStream (java.util.zip.ZipOutputStream)1